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
* C imports
@interface HelloWorld : NSObject
/* This is a callback function. The data arguments are ignored
* in this example. More callbacks below. */
/* Another callback */
int main(int argc, char *argv)
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* We could use also CGTKWidget here instead */
/* 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]
/* 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]
/* This packs the button into the window (a gtk container) */
/* The final step is to display this newly created widget */
/* and the window */
/* 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). */
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!
CGTKWindow *cWindow = [[CGTKWindow alloc]
// Or as a C GTK+ window!
GtkWindow *gWindow = [cWindow WINDOW];
// Or even as a C GtkWidget!
GtkWidget *gWidget = [cWindow WIDGET];
// ...is the same as this:
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];
NSLog(@"Error loading GUI file");
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
[CGTKCallbackData withObject:[CGTK class]
[CGTKCallbackData withObject:[HelloWorld class]
[CGTKCallbackData withObject:[HelloWorld class]
CGTKWidget *w = [builder getWidgetWithName:@"window1"];
if(w != nil)
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
Hello World on Linux
This post originally appeared on my personal website here.
I am currently running a variety of distributions, primarily Linux Mint Debian Edition.
Previously I was running KDE 4.3.3 on top of Fedora 11 (for the first experiment) and KDE 4.6.5 on top of Gentoo (for the second experiment).
Check out my profile for more information.