From 45380da631323305523564533555b468685104f8 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Wed, 28 Dec 2011 20:23:01 +0100 Subject: [PATCH] Initial OGComboBox implementation. Only support for text so far. --- gtk/OGComboBox.m | 78 ++++++++++++++++++++++++++++++++++++++++ gtk/OGComboBoxItem.m | 31 ++++++++++++++++ headers/OGBox.h | 1 - headers/OGComboBox.h | 27 ++++++++++++++ headers/OGComboBoxItem.h | 12 +++++++ 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 gtk/OGComboBox.m create mode 100644 gtk/OGComboBoxItem.m create mode 100644 headers/OGComboBox.h create mode 100644 headers/OGComboBoxItem.h diff --git a/gtk/OGComboBox.m b/gtk/OGComboBox.m new file mode 100644 index 0000000..56f4106 --- /dev/null +++ b/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 diff --git a/gtk/OGComboBoxItem.m b/gtk/OGComboBoxItem.m new file mode 100644 index 0000000..3094bcc --- /dev/null +++ b/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 diff --git a/headers/OGBox.h b/headers/OGBox.h index 90b7d4b..0df22fc 100644 --- a/headers/OGBox.h +++ b/headers/OGBox.h @@ -2,7 +2,6 @@ @interface OGBox: OGWidget + box; - - (void)appendChild: (OGWidget*)child expand: (BOOL)expand fill: (BOOL)fill diff --git a/headers/OGComboBox.h b/headers/OGComboBox.h new file mode 100644 index 0000000..1d158be --- /dev/null +++ b/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 diff --git a/headers/OGComboBoxItem.h b/headers/OGComboBoxItem.h new file mode 100644 index 0000000..74012c0 --- /dev/null +++ b/headers/OGComboBoxItem.h @@ -0,0 +1,12 @@ +#import + +@interface OGComboBoxItem: OFObject +{ + OFString *label; +} + +@property (copy) OFString *label; + ++ comboBoxItemWithLabel: (OFString*)label; +- initWithLabel: (OFString*)label; +@end