//
//  MFTextBox.mm
//  MySQLWorkbench
//
//  Created by Alfredo Kojima on 6/Feb/09.
//  Copyright 2009 Sun Microsystems Inc. All rights reserved.
//

#import "MFTextBox.h"
#include "mforms/mforms.h"

@implementation MFTextBoxImpl

- (id)initWithObject:(::mforms::TextBox*)aTextBox
{
  NSRect frame;
  
  frame.origin= NSMakePoint(0, 0);
  frame.size= [NSScrollView frameSizeForContentSize:[self minimumSize]
                              hasHorizontalScroller:NO
                                hasVerticalScroller:YES
                                         borderType:NSLineBorder];
  
  self= [super initWithFrame:frame];
  if (self)
  {
    mOwner= aTextBox;
    mOwner->set_data(self);

    [self setBorderType: NSBezelBorder];
    [self setHasVerticalScroller: YES];
    [self setHasHorizontalScroller: NO];
    
    frame.size= [self minimumSize];
    mTextView= [[[NSTextView alloc] initWithFrame:frame] autorelease];
    [self setDocumentView:mTextView];
    
    [mTextView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin];
    [mTextView setEditable:YES];
  }
  return self;
}

- (mforms::Object*)mformsObject
{
  return mOwner;
}


- (NSSize)minimumSize
{
  NSSize size;
  size.width= [NSScroller scrollerWidth] + 50 + mPadding*2;
  size.height= [NSScroller scrollerWidth] + 50 + mPadding*2;
  return size;
}


- (void)setFrame:(NSRect)rect
{
  rect.origin.x+= mPadding;
  rect.origin.y+= mPadding;
  rect.size.width-= mPadding*2;
  rect.size.height-= mPadding*2;
  [super setFrame: rect];
}


- (void)setTag:(NSInteger)tag
{
  mTag= tag;
}


- (NSInteger)tag
{
  return mTag;
}


- (void)setPadding:(float)pad
{
  mPadding= pad;
}


- (void)setBordered:(BOOL)flag
{
  [self setBorderType: flag ? NSBezelBorder : NSNoBorder];
}



static bool textbox_create(::mforms::TextBox *self, mforms::ScrollBars scrolls)
{
  MFTextBoxImpl *textbox= [[[MFTextBoxImpl alloc] initWithObject:self] autorelease];
  
  return textbox != nil;
}


static void textbox_set_text(::mforms::TextBox *self, const std::string &text)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    [[textbox->mTextView textStorage] setAttributedString:[[[NSAttributedString alloc] initWithString: wrap_nsstring(text)] autorelease]];
  }
}


static void textbox_append_text(mforms::TextBox *self, const std::string &text)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    [[textbox->mTextView textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString: wrap_nsstring(text)] autorelease]];
  }  
}


static std::string textbox_get_text(::mforms::TextBox *self)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    return [[textbox->mTextView string] UTF8String];
  }
  return "";
}


static void textbox_set_padding(::mforms::TextBox *self, int pad)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    [textbox setPadding:pad];
  }
}


static void textbox_set_bordered(::mforms::TextBox *self, bool flag)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    [textbox setBordered: flag];
  }
}


static void textbox_set_read_only(mforms::TextBox *self, bool flag)
{
  if ( self )
  {
    MFTextBoxImpl* textbox = self->get_data();
    
    [textbox->mTextView setEditable:flag?NO:YES];
  }  
}


void cf_textbox_init()
{
  ::mforms::ControlFactory *f = ::mforms::ControlFactory::get_instance();
  
  f->_textbox_impl.create= &textbox_create;
  f->_textbox_impl.set_text= &textbox_set_text;
  f->_textbox_impl.get_text= &textbox_get_text;
  f->_textbox_impl.set_padding= &textbox_set_padding;
  f->_textbox_impl.set_bordered= &textbox_set_bordered;
  f->_textbox_impl.set_read_only= &textbox_set_read_only;
  f->_textbox_impl.append_text= &textbox_append_text;
  
}

@end



