Initial OGComboBox implementation.

Only support for text so far.
This commit is contained in:
Jonathan Schleifer 2011-12-28 20:23:01 +01:00
parent 73e7da5fb7
commit 45380da631
5 changed files with 148 additions and 1 deletions

78
gtk/OGComboBox.m Normal file
View file

@ -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 <OGComboBoxDataSource>)dataSource
{
return dataSource;
}
- (void)setDataSource: (id <OGComboBoxDataSource>)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

31
gtk/OGComboBoxItem.m Normal file
View file

@ -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

View file

@ -2,7 +2,6 @@
@interface OGBox: OGWidget
+ box;
- (void)appendChild: (OGWidget*)child
expand: (BOOL)expand
fill: (BOOL)fill

27
headers/OGComboBox.h Normal file
View file

@ -0,0 +1,27 @@
#import "OGWidget.h"
#import "OGComboBoxItem.h"
@class OGComboBox;
@protocol OGComboBoxDelegate <OFObject>
@optional
- (void)comboBoxWasChanged: (OGComboBox*)comboBox;
@end
@protocol OGComboBoxDataSource <OFObject>
- (size_t)numberOfItemsInComboBox: (OGComboBox*)comboBox;
- (OGComboBoxItem*)comboBox: (OGComboBox*)comboBox
itemAtIndex: (size_t)index;
@end
@interface OGComboBox: OGWidget
{
id <OGComboBoxDelegate> delegate;
id <OGComboBoxDataSource> dataSource;
}
@property (assign) id <OGComboBoxDelegate> delegate;
@property (assign) id <OGComboBoxDataSource> dataSource;
+ comboBox;
@end

12
headers/OGComboBoxItem.h Normal file
View file

@ -0,0 +1,12 @@
#import <ObjFW/OFString.h>
@interface OGComboBoxItem: OFObject
{
OFString *label;
}
@property (copy) OFString *label;
+ comboBoxItemWithLabel: (OFString*)label;
- initWithLabel: (OFString*)label;
@end