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

#import "MFSelector.h"

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

@implementation MFSelectorImpl


- (id)initWithObject:(::mforms::Selector*)aSelector
{
  self= [super initWithFrame:NSMakeRect(0, 0, 10, 20)];
  if (self)
  {    
    mOwner= aSelector;
    mOwner->set_data(self);
    [self setTarget: self];
    [self setAction: @selector(selectionChanged:)];
    [self sizeToFit];
    [self setTag: 0];
  }
  return self;
}


- (NSSize)minimumSize
{
  return [[self cell] cellSize];
}


- (void)selectionChanged:(id)sender
{
  mOwner->callback();
}


- (BOOL)heightIsFixed
{
  return YES;
}


static bool selector_create(::mforms::Selector *self)
{
  [[[MFSelectorImpl alloc] initWithObject:self] autorelease];
  
  return true;  
}


static void selector_clear(::mforms::Selector *self)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      [selector removeAllItems];
    }
  }
}

static void selector_add_items(::mforms::Selector *self, const std::list<std::string> &items)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      for (std::list<std::string>::const_iterator iter= items.begin(); iter != items.end(); ++iter)
      {
        [selector addItemWithTitle:wrap_nsstring(*iter)];
      }
    }
  }
}


static int selector_add_item(::mforms::Selector *self, const std::string &item)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      if (item == "-")
        [[selector menu] addItem:[NSMenuItem separatorItem]];
      else
        [selector addItemWithTitle:wrap_nsstring(item)];
      return [selector numberOfItems]-1;
    }
  }
  return -1;
}


static std::string selector_get_text(::mforms::Selector *self)
{
  if (self)
  {
    MFSelectorImpl* selector = self->get_data();
    
    if (selector && ([selector selectedItem] != nil))
      return [[selector titleOfSelectedItem] UTF8String];
  }
  return "";
}


static void selector_set_index(::mforms::Selector *self, int index)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      [selector selectItemAtIndex:index];
      self->callback();
    }
  }
}


static int selector_get_index(::mforms::Selector *self)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      return [selector indexOfSelectedItem];
    }
  }
  return -1;
}


static int selector_get_item_count(::mforms::Selector *self)
{
  if ( self )
  {
    MFSelectorImpl* selector = self->get_data();
    
    if ( selector )
    {
      return [selector numberOfItems];
    }
  }
  return 0;
}


void cf_selector_init()
{
  ::mforms::ControlFactory *f = ::mforms::ControlFactory::get_instance();
  
  f->_selector_impl.create= &selector_create;
  f->_selector_impl.clear= &selector_clear;
  f->_selector_impl.add_items= &selector_add_items;
  f->_selector_impl.add_item= &selector_add_item;
  f->_selector_impl.get_text= &selector_get_text;
  f->_selector_impl.set_index= &selector_set_index;
  f->_selector_impl.get_index= &selector_get_index;
  f->_selector_impl.get_item_count= &selector_get_item_count;
}


@end

