Posts Tagged ‘gtk’


January 28th, 2014 2 comments

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. */

/* Another callback */

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

    NSLog(@"Hello World");

    [CGTK mainQuit];
Hello World in action

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];

// This...
[cWindow show];

// the same as this:
gtk_widget_show([cWindow WIDGET]);

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:@""])
    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",

[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 Windows

Hello World on Mac

Hello World on Mac

Hello World on Linux

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.

KDE4, LFS: Make GTK Applications Look Like QT4 Applications

November 3rd, 2011 2 comments

Do your GTK applications (i.e. Firefox) look like something designed in the 90′s in KDE? I think I can help you.

I installed the latest Firefox, (not the one in the screenshot, I stole this.) and was very disappointed to see something like the following:

Tyler pointed me to the Gentoo guide here, which helped me find out which packages I needed.

If you install Chakra-Gtk-Config, and either oxygen-gtk or qtcurve (make sure to download the gtk2 theme), you will have better looking GTK applications in no time. Note that there are probably tons of other GTK themes for KDE4, these are just some suggestions to get you started.

That is much better.

I am currently running ArchLinux (x86_64).
Check out my profile for more information.

Create a GTK+ application on Linux with Objective-C

December 8th, 2010 8 comments

As sort of follow-up-in-spirit to my older post I decided to share a really straight forward way to use Objective-C to build GTK+ applications.


Objective-C is an improvement to the iconic C programming language that remains backwards compatible while adding many new and interesting features. Chief among these additions is syntax for real objects (and thus object-oriented programming). Popularized by NeXT and eventually Apple, Objective-C is most commonly seen in development for Apple OSX and iOS based platforms. It ships with or without a large standard library (sometimes referred to as the Foundation Kit library) that makes it very easy for developers to quickly create fast and efficient programs. The result is a language that compiles down to binary, requires no virtual machines (just a runtime library), and achieves performance comparable to C and C++.

Marrying Objective-C with GTK+

Normally when writing a GTK+ application the language (or a library) will supply you with bindings that let you create GUIs in a way native to that language. So for instance in C++ you would create GTK+ objects, whereas in C you would create structures or ask functions for pointers back to the objects. Unfortunately while there used to exist a couple of different Objective-C bindings for GTK+, all of them are quite out of date. So instead we are going to rely on the fact that Objective-C is backwards compatible with C to get our program to work.

What you need to start

I’m going to assume that Ubuntu will be our operating system for development. To ensure that we have what we need to compile the programs, just install the following packages:

  1. gnustep-core-devel
  2. libgtk2.0-dev

As you can see from the list above we will be using GNUstep as our Objective-C library of choice.

Setting it all up

In order to make this work we will be creating two Objective-C classes, one that will house our GTK+ window and another that will actually start our program. I’m going to call my GTK+ object MainWindow and create the two necessary files: MainWindow.h and MainWindow.m. Finally I will create a main.m that will start the program and clean it up after it is done.

Let me apologize here for the poor code formatting; apparently WordPress likes to destroy whatever I try and do to make it better. If you want properly indented code please see the download link below.


In the MainWindow.h file put the following code:

#import <gtk/gtk.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>

//A pointer to this object (set on init) so C functions can call
//Objective-C functions
id myMainWindow;

* This class is responsible for initializing the GTK render loop
* as well as setting up the GUI for the user. It also handles all GTK
* callbacks for the winMain GtkWindow.
@interface MainWindow : NSObject
//The main GtkWindow
GtkWidget *winMain;
GtkWidget *button;

* Constructs the object and initializes GTK and the GUI for the
* application.
* *********************************************************************
* Input
* *********************************************************************
* argc (int *): A pointer to the arg count variable that was passed
* in at the application start. It will be returned
* with the count of the modified argv array.
* argv (char *[]): A pointer to the argument array that was passed in
* at the application start. It will be returned with
* the GTK arguments removed.
* *********************************************************************
* Returns
* *********************************************************************
* MainWindow (id): The constructed object or nil
* arc (int *): The modified input int as described above
* argv (char *[]): The modified input array modified as described above
-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv;

* Frees the Gtk widgets that we have control over

* Starts and hands off execution to the GTK main loop

* Example Objective-C function that prints some output

* C callback functions

* Called when the user closes the window
void on_MainWindow_destroy(GtkObject *object, gpointer user_data);

* Called when the user presses the button
void on_btnPushMe_clicked(GtkObject *object, gpointer user_data);



For the class’ actual code file fill it in as show below. This class will create a GTK+ window with a single button and will react to both the user pressing the button, and closing the window.

#import “MainWindow.h”

* For documentation see MainWindow.h

@implementation MainWindow

-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv
//call parent class’ init
if (self = [super init]) {

//setup the window
winMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_window_set_title (GTK_WINDOW (winMain), “Hello World”);
gtk_window_set_default_size(GTK_WINDOW(winMain), 230, 150);

//setup the button
button = gtk_button_new_with_label (“Push me!”);

gtk_container_add (GTK_CONTAINER (winMain), button);

//connect the signals
g_signal_connect (winMain, “destroy”, G_CALLBACK (on_MainWindow_destroy), NULL);
g_signal_connect (button, “clicked”, G_CALLBACK (on_btnPushMe_clicked), NULL);

//force show all

//assign C-compatible pointer
myMainWindow = self;

//return pointer to this object
return self;

//start gtk loop

NSLog(@”Printed from Objective-C’s NSLog function.”);
printf(“Also printed from standard printf function.\n”);


myMainWindow = NULL;

if(GTK_IS_WIDGET (button))
//clean up the button

if(GTK_IS_WIDGET (winMain))
//clean up the main window

[self destroyWidget];

[super dealloc];

void on_MainWindow_destroy(GtkObject *object, gpointer user_data)
//exit the main loop

void on_btnPushMe_clicked(GtkObject *object, gpointer user_data)
printf(“Button was clicked\n”);

//call Objective-C function from C function using global object pointer
[myMainWindow printSomething];



To finish I will write a main file and function that creates the MainWindow object and eventually cleans it up. Objective-C (1.0) does not support automatic garbage collection so it is important that we don’t forget to clean up after ourselves.

#import “MainWindow.h”
#import <Foundation/NSAutoreleasePool.h>

int main(int argc, char *argv[]) {

//create an AutoreleasePool
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//init gtk engine
gtk_init(&argc, &argv);

//set up GUI
MainWindow *mainWindow = [[MainWindow alloc] initWithArgCount:&argc andArgVals:argv];

//begin the GTK loop
[mainWindow startGtkMainLoop];

//free the GUI
[mainWindow release];

//drain the pool
[pool release];

//exit application
return 0;

Compiling it all together

Use the following command to compile the program. This will automatically include all .m files in the current directory so be careful when and where you run this.

gcc `pkg-config –cflags –libs gtk+-2.0` -lgnustep-base -fconstant-string-class=NSConstantString -o “./myprogram” $(find . -name ‘*.m’) -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -std=c99 -O3

Once complete you will notice a new executable in the directory called myprogram. Start this program and you will see our GTK+ window in action.

If you run it from the command line you can see the output that we coded when the button is pushed.

Wrapping it up

There you have it. We now have a program that is written in Objective-C, using C’s native GTK+ ‘bindings’ for the GUI, that can call both regular C and Objective-C functions and code. In addition, thanks to the porting of both GTK+ and GNUstep to Windows, this same code will also produce a cross-platform application that works on both Mac OSX and Windows.

Source Code Downloads

Source Only Package
File name:
File hashes: Download Here
File size: 2.4KB
File download: Download Here

Originally posted 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.

KDE on Gentoo: slightly less inflammatory but still difficult

October 24th, 2009 1 comment

After the shitstorm that was Dana’s post about KDE, I figured I’d go into more details about how my day to day use is going.

Multiple Libraries Make Baby Jesus Cry
All the base system software in the Gentoo kdebase-meta package compiles against the QT4 libraries, but many of the optional packages still depend on QT3. Popular programs like KTorrent and AmaroK either still haven’t been updated or tagged in the Portage repository, so at any given time a desktop user will have programs running that use two separate graphical widget libraries. My level of use is such that I have programs running with GTK+, QT3 and QT4 on the same monitor – not to mention apps like Songbird that draw their own custom interface.

From an efficiency and system resource standpoint, this is really poor utilization. I have 4GB of RAM for intensive tasks such as music library organization, not to show slightly different scrollbars and window controls in every third application. Under GNOME 2.26, there was nowhere near this level of display potpourri with the default system utilities. (It also helps that Firefox is GTK+, which is close to the top-used app on my machine.)

Some Applications Just Suck
I’ve attempted to use all of the built-in KDE applications to combat the mismatched desktop effect, and often I’ll find them wanting compared to the GNOME or GTK+ equivalents. Dolphin seems like a very capable file manager, but it will lock up when hovering over some video thumbnails or seemingly randomly when in my home directory. (The rest of the system remains responsive, so it looks like Dolphin’s the culprit.) Konqueror is fast, but the configuration and settings are confusing to say the least – and without proper add-on/AdBlock Plus support, I can’t consider making it my primary browser.

Another example of application suck is ksnapshot, which is supposed to do what you think it would – take screen captures of active windows or the entire desktop. I made the unfortunate selection of selecting to capture a region, specifically the “Settings” menu in Konqueror. After selecting a nice 300×300 pixel area, pressing Enter to confirm the region did nothing. Escape did nothing, nor did any combination of mouse buttons. Since ksnapshot takes focus away from the entire desktop, it wasn’t possible to exit the application. I had to SSH in from another machine and manually kill the process to regain control.

Desktop Effects Are Nice
Once I’d mangled xorg.conf and set up my nVidia drivers in TwinView mode, I still ran into issues enabling the built-in KDE compositing effects. The command in Gentoo to learn is “eselect”, which when combined with “eselect opengl list” allows a display of all the possible OpenGL rendering engines. Apparently even when the nVidia drivers are enabled, one must specifically tell X to use the correct renderer.

The problem I’ve encountered is that while some effects are smooth as butter, such as moving Wobbly Windows, resizing them is delayed and causes display tearing. I have no idea what’s causing this, and the behaviour shouldn’t exist.

Going back to GNOME
As of tomorrow, I have no doubts that I’ll be returning to GNOME for regular desktop use. KDE has some compelling features but my experience with it has been less than ideal. I can’t afford to have my file browser lock up during regular use – and GNOME’s environment seems much more predictable.

I am currently running various *BSD variants for this Experiment.
I currently run a mix of Windows, OS X and Linux systems for both work and personal use.
For Linux, I prefer Ubuntu LTS releases without Unity and still keep Windows 7 around for gaming.
Check out my profile for more information.