A while back I made it my goal to put together an open source project as my way of contributing back to the community. Well fast forward a couple of months and my hobby project is finally ready to be shown the light of day. I give you… CoreGTK

CoreGTK is an Objective-C binding for the GTK+ library which wraps all objects descending from GtkWidget (plus a few others here and there). Like other “core” Objective-C libraries it is designed to be a very thin wrapper, so that anyone familiar with the C version of GTK+ should be able to pick it up easily.

However the real goal of CoreGTK is not to replace the C implementation for every day use but instead to allow developers to more easily code GTK+ interfaces using Objective-C. This could be especially useful if a developer already has a program, say one they are developing for the Mac, and they want to port it to Linux or Windows. With a little bit of MVC a savvy developer would only need to re-write the GUI portion of their application in CoreGTK.

So what does a CoreGTK application look like? Pretty much like a normal Objective-C program:

/* * Objective-C imports */ #import <Foundation/Foundation.h> #import "CGTK.h" #import "CGTKButton.h" #import "CGTKSignalConnector.h" #import "CGTKWindow.h" /* * C imports */ #import <gtk/gtk.h> @interface HelloWorld : NSObject /* This is a callback function. The data arguments are ignored * in this example. More callbacks below. */ +(void)hello; /* Another callback */ +(void)destroy; @end @implementation HelloWorld int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; /* We could use also CGTKWidget here instead */ CGTKWindow *window; CGTKButton *button; /* This is called in all GTK applications. Arguments are parsed * from the command line and are returned to the application. */ [CGTK autoInitWithArgc:argc andArgv:argv]; /* Create a new window */ window = [[CGTKWindow alloc] initWithGtkWindowType:GTK_WINDOW_TOPLEVEL]; /* Here we connect the "destroy" event to a signal handler in the HelloWorld class */ [CGTKSignalConnector connectGpointer:[window WIDGET] withSignal:@"destroy" toTarget:[HelloWorld class] withSelector:@selector(destroy) andData:NULL]; /* Sets the border width of the window */ [window setBorderWidth: [NSNumber numberWithInt:10]]; /* Creates a new button with the label "Hello World" */ button = [[CGTKButton alloc] initWithLabel:@"Hello World"]; /* When the button receives the "clicked" signal, it will call the * function hello() in the HelloWorld class (below) */ [CGTKSignalConnector connectGpointer:[button WIDGET] withSignal:@"clicked" toTarget:[HelloWorld class] withSelector:@selector(hello) andData:NULL]; /* This packs the button into the window (a gtk container) */ [window add:button]; /* The final step is to display this newly created widget */ [button show]; /* and the window */ [window show]; /* All GTK applications must have a [CGTK main] call. Control ends here * and waits for an event to occur (like a key press or * mouse event). */ [CGTK main]; [pool release]; return 0; } +(void)hello { NSLog(@"Hello World"); } +(void)destroy { [CGTK mainQuit]; } @end

Hello World in action

And because Objective-C is completely compatible with regular old C code there is nothing stopping you from simply extracting the GTK+ objects and using them like normal.

// Use it as an Objective-C CoreGTK object! <code>CGTKWindow *cWindow = [[CGTKWindow alloc] initWithGtkWindowType:GTK_WINDOW_TOPLEVEL]; // Or as a C GTK+ window! GtkWindow *gWindow = [cWindow WINDOW]; // Or even as a C GtkWidget! GtkWidget *gWidget = [cWindow WIDGET]; // This... [cWindow show]; // ...is the same as this: gtk_widget_show([cWindow WIDGET]);</code>

You can even use a UI builder like GLADE, import the XML and wire up the signals to Objective-C instance and class methods.

CGTKBuilder *builder = [[CGTKBuilder alloc] init]; if(![builder addFromFile:@"test.glade"]) { NSLog(@"Error loading GUI file"); return 1; } [CGTKBuilder setDebug:YES]; NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys: [CGTKCallbackData withObject:[CGTK class] andSEL:@selector(mainQuit)], @"endMainLoop", [CGTKCallbackData withObject:[HelloWorld class] andSEL:@selector(hello)], @"on_button2_clicked", [CGTKCallbackData withObject:[HelloWorld class] andSEL:@selector(hello)], @"on_button1_activate", nil]; [builder connectSignalsToObjects:dic]; CGTKWidget *w = [builder getWidgetWithName:@"window1"]; if(w != nil) { [w showAll]; } [builder release];

So there you have it that’s CoreGTK in a nutshell.

There are a variety of ways to help me out with this project if you are so inclined to do so. The first task is probably just to get familiar with it. Download CoreGTK from the GitHub project page and play around with it. If you find a bug (very likely) please create an issue for it.

Another easy way to get familiar with CoreGTK is to help write/fix documentation – a lot of which is written in the source code itself. Sadly most of the current documentation simply states which underlying GTK+ function is called and so it could be cleaned up quite a bit.

At the moment there really isn’t anything more formal than that in place but of course code contributions would also be welcome!

Update: added some pictures of the same program running on all three operating systems.

Hello World on Windows

Hello World on Mac