ADDED gtk/OGComboBox.m Index: gtk/OGComboBox.m ================================================================== --- gtk/OGComboBox.m +++ gtk/OGComboBox.m @@ -0,0 +1,78 @@ +#import "OGComboBox.h" + +@interface OGComboBox () +- (void)OG_changed; +@end + +static void +changed(GtkWidget *widget, gpointer data) +{ + [(OGComboBox*)data OG_changed]; +} + +@implementation OGComboBox +@synthesize delegate; + ++ comboBox +{ + return [[[self alloc] init] autorelease]; +} + +- init +{ + self = [super init]; + + widget = gtk_combo_box_new(); + GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); + gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, FALSE); + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(widget), renderer, + "text", 0, NULL); + g_signal_connect(G_OBJECT(widget), "changed", G_CALLBACK(changed), + self); + + g_signal_connect(G_OBJECT(widget), "destroy", G_CALLBACK(og_destroy), + self); + [self retain]; + + return self; +} + +- (id )dataSource +{ + return dataSource; +} + +- (void)setDataSource: (id )dataSource_ +{ + GtkListStore *listStore = gtk_list_store_new(1, G_TYPE_STRING); + GtkTreeIter iter; + OGComboBoxItem* (*itemAtIndex)(id, SEL, OGComboBox*, size_t); + + dataSource = dataSource_; + itemAtIndex = (OGComboBoxItem*(*)(id, SEL, OGComboBox*, size_t)) + [dataSource methodForSelector: @selector(comboBox:itemAtIndex:)]; + + size_t i, size = [dataSource numberOfItemsInComboBox: self]; + for (i = 0; i < size; i++) { + OGComboBoxItem *item = itemAtIndex(dataSource, + @selector(comboBox:itemAtIndex:), self, i); + + gtk_list_store_append(listStore, &iter); + gtk_list_store_set(listStore, &iter, 0, + [item.label UTF8String], -1); + } + + gtk_combo_box_set_model(GTK_COMBO_BOX(widget), + GTK_TREE_MODEL(listStore)); +} + +- (void)OG_changed +{ + OFAutoreleasePool *pool = [OFAutoreleasePool new]; + + if ([delegate respondsToSelector: @selector(comboBoxWasChanged:)]) + [delegate comboBoxWasChanged: self]; + + [pool release]; +} +@end ADDED gtk/OGComboBoxItem.m Index: gtk/OGComboBoxItem.m ================================================================== --- gtk/OGComboBoxItem.m +++ gtk/OGComboBoxItem.m @@ -0,0 +1,31 @@ +#import "OGComboBoxItem.h" + +@implementation OGComboBoxItem +@synthesize label; + ++ comboBoxItemWithLabel: (OFString*)label +{ + return [[[self alloc] initWithLabel: label] autorelease]; +} + +- initWithLabel: (OFString*)label_ +{ + self = [super init]; + + @try { + label = [label_ copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [label release]; + + [super dealloc]; +} +@end Index: headers/OGBox.h ================================================================== --- headers/OGBox.h +++ headers/OGBox.h @@ -1,14 +1,13 @@ #import "OGWidget.h" @interface OGBox: OGWidget + box; - - (void)appendChild: (OGWidget*)child expand: (BOOL)expand fill: (BOOL)fill padding: (float)padding; - (void)prependChild: (OGWidget*)child expand: (BOOL)expand fill: (BOOL)fill padding: (float)padding; @end ADDED headers/OGComboBox.h Index: headers/OGComboBox.h ================================================================== --- headers/OGComboBox.h +++ headers/OGComboBox.h @@ -0,0 +1,27 @@ +#import "OGWidget.h" +#import "OGComboBoxItem.h" + +@class OGComboBox; + +@protocol OGComboBoxDelegate +@optional +- (void)comboBoxWasChanged: (OGComboBox*)comboBox; +@end + +@protocol OGComboBoxDataSource +- (size_t)numberOfItemsInComboBox: (OGComboBox*)comboBox; +- (OGComboBoxItem*)comboBox: (OGComboBox*)comboBox + itemAtIndex: (size_t)index; +@end + +@interface OGComboBox: OGWidget +{ + id delegate; + id dataSource; +} + +@property (assign) id delegate; +@property (assign) id dataSource; + ++ comboBox; +@end ADDED headers/OGComboBoxItem.h Index: headers/OGComboBoxItem.h ================================================================== --- headers/OGComboBoxItem.h +++ headers/OGComboBoxItem.h @@ -0,0 +1,12 @@ +#import + +@interface OGComboBoxItem: OFObject +{ + OFString *label; +} + +@property (copy) OFString *label; + ++ comboBoxItemWithLabel: (OFString*)label; +- initWithLabel: (OFString*)label; +@end