<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TylerBurton.ca &#187; GNUstep</title>
	<atom:link href="http://www.tylerburton.ca/tag/gnustep/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tylerburton.ca</link>
	<description></description>
	<lastBuildDate>Tue, 10 Jan 2012 04:41:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create a GTK+ application on Linux with Objective-C</title>
		<link>http://www.tylerburton.ca/2010/12/create-a-gtk-application-on-linux-with-objective-c/</link>
		<comments>http://www.tylerburton.ca/2010/12/create-a-gtk-application-on-linux-with-objective-c/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 03:12:06 +0000</pubDate>
		<dc:creator>Tyler Burton</dc:creator>
				<category><![CDATA[Created by Tyler Burton]]></category>
		<category><![CDATA[F/OSS]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[GNUstep]]></category>
		<category><![CDATA[GTK+]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OSX]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.tylerburton.ca/?p=538</guid>
		<description><![CDATA[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-what? 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 [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>As sort of follow-up-in-spirit to <a href="http://www.tylerburton.ca/2010/03/the-case-for-objective-c/">my older post</a> I decided to share a really straight forward way to use Objective-C to build GTK+ applications.</p>
<p><strong>Objective-what?</strong></p>
<p><a href="http://en.wikipedia.org/wiki/Objective-C">Objective-C</a> is an improvement to the iconic <a href="http://en.wikipedia.org/wiki/C_%28programming_language%29">C programming language</a> that remains backwards compatible while adding many new and interesting features. Chief among these additions is syntax for real objects (and thus <a href="http://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a>). Popularized by <a href="http://en.wikipedia.org/wiki/NeXT">NeXT</a> and eventually <a href="http://www.apple.com">Apple</a>, 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 <a href="http://en.wikipedia.org/wiki/GNUstep#Foundation_Kit">Foundation Kit</a> 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++.</p>
<p><strong>Marrying Objective-C with GTK+</strong></p>
<p>Normally when writing a <a href="http://en.wikipedia.org/wiki/GTK%2B">GTK+</a> application the language (or a library) will supply you with <a href="http://en.wikipedia.org/wiki/GTK%2B#Programming_language_bindings">bindings</a> that let you create GUIs in a way native to that language. So for instance in <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a> 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.</p>
<p><strong>What you need to start</strong></p>
<p>I&#8217;m going to assume that <a href="http://www.ubuntu.com">Ubuntu</a> will be our operating system for development. To ensure that we have what we need to compile the programs, just install the following packages:</p>
<ol>
<li>gnustep-core-devel</li>
<li>libgtk2.0-dev</li>
</ol>
<p>As you can see from the list above we will be using <a href="http://en.wikipedia.org/wiki/GNUstep">GNUstep</a> as our Objective-C library of choice.</p>
<p><strong>Setting it all up</strong></p>
<p>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&#8217;m going to call my GTK+ object MainWindow and create the two necessary files: <em>MainWindow.h</em> and <em>MainWindow.m</em>. Finally I will create a <em>main.m</em> that will start the program and clean it up after it is done. <strong></strong></p>
<p><strong>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.</strong></p>
<p><strong>MainWindow.h</strong></p>
<p>In the MainWindow.h file put the following code:</p>
<blockquote><p>#import &lt;gtk/gtk.h&gt;<br />
#import &lt;Foundation/NSObject.h&gt;<br />
#import &lt;Foundation/NSString.h&gt;</p>
<p>//A pointer to this object (set on init) so C functions can call<br />
//Objective-C functions<br />
id myMainWindow;</p>
<p>/*<br />
* This class is responsible for initializing the GTK render loop<br />
* as well as setting up the GUI for the user. It also handles all GTK<br />
* callbacks for the winMain GtkWindow.<br />
*/<br />
@interface MainWindow : NSObject<br />
{<br />
//The main GtkWindow<br />
GtkWidget *winMain;<br />
GtkWidget *button;<br />
}</p>
<p>/*<br />
* Constructs the object and initializes GTK and the GUI for the<br />
* application.<br />
*<br />
* *********************************************************************<br />
* Input<br />
* *********************************************************************<br />
* argc (int *):    A pointer to the arg count variable that was passed<br />
*             in at the application start. It will be returned<br />
*            with the count of the modified argv array.<br />
* argv (char *[]):     A pointer to the argument array that was passed in<br />
*            at the application start. It will be returned with<br />
*            the GTK arguments removed.<br />
*<br />
* *********************************************************************<br />
* Returns<br />
* *********************************************************************<br />
* MainWindow (id):    The constructed object or nil<br />
* arc (int *):        The modified input int as described above<br />
* argv (char *[]):    The modified input array modified as described above<br />
*/<br />
-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv;</p>
<p>/*<br />
* Frees the Gtk widgets that we have control over<br />
*/<br />
-(void)destroyWidget;</p>
<p>/*<br />
* Starts and hands off execution to the GTK main loop<br />
*/<br />
-(void)startGtkMainLoop;</p>
<p>/*<br />
* Example Objective-C function that prints some output<br />
*/<br />
-(void)printSomething;</p>
<p>/*<br />
********************************************************<br />
* C callback functions<br />
********************************************************<br />
*/</p>
<p>/*<br />
* Called when the user closes the window<br />
*/<br />
void on_MainWindow_destroy(GtkObject *object, gpointer user_data);</p>
<p>/*<br />
* Called when the user presses the button<br />
*/<br />
void on_btnPushMe_clicked(GtkObject *object, gpointer user_data);</p>
<p>@end</p></blockquote>
<p><strong>MainWindow.m</strong></p>
<p>For the class&#8217; 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.</p>
<blockquote><p>#import &#8220;MainWindow.h&#8221;</p>
<p>/*<br />
* For documentation see MainWindow.h<br />
*/</p>
<p>@implementation MainWindow</p>
<p>-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv<br />
{<br />
//call parent class&#8217; init<br />
if (self = [super init]) {</p>
<p>//setup the window<br />
winMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);</p>
<p>gtk_window_set_title (GTK_WINDOW (winMain), &#8220;Hello World&#8221;);<br />
gtk_window_set_default_size(GTK_WINDOW(winMain), 230, 150);</p>
<p>//setup the button<br />
button = gtk_button_new_with_label (&#8220;Push me!&#8221;);</p>
<p>gtk_container_add (GTK_CONTAINER (winMain), button);</p>
<p>//connect the signals<br />
g_signal_connect (winMain, &#8220;destroy&#8221;, G_CALLBACK (on_MainWindow_destroy), NULL);<br />
g_signal_connect (button, &#8220;clicked&#8221;, G_CALLBACK (on_btnPushMe_clicked), NULL);</p>
<p>//force show all<br />
gtk_widget_show_all(winMain);<br />
}</p>
<p>//assign C-compatible pointer<br />
myMainWindow = self;</p>
<p>//return pointer to this object<br />
return self;<br />
}</p>
<p>-(void)startGtkMainLoop<br />
{<br />
//start gtk loop<br />
gtk_main();<br />
}</p>
<p>-(void)printSomething{<br />
NSLog(@&#8221;Printed from Objective-C&#8217;s NSLog function.&#8221;);<br />
printf(&#8220;Also printed from standard printf function.\n&#8221;);<br />
}</p>
<p>-(void)destroyWidget{</p>
<p>myMainWindow = NULL;</p>
<p>if(GTK_IS_WIDGET (button))<br />
{<br />
//clean up the button<br />
gtk_widget_destroy(button);<br />
}</p>
<p>if(GTK_IS_WIDGET (winMain))<br />
{<br />
//clean up the main window<br />
gtk_widget_destroy(winMain);<br />
}<br />
}</p>
<p>-(void)dealloc{<br />
[self destroyWidget];</p>
<p>[super dealloc];<br />
}</p>
<p>void on_MainWindow_destroy(GtkObject *object, gpointer user_data)<br />
{<br />
//exit the main loop<br />
gtk_main_quit();<br />
}</p>
<p>void on_btnPushMe_clicked(GtkObject *object, gpointer user_data)<br />
{<br />
printf(&#8220;Button was clicked\n&#8221;);</p>
<p>//call Objective-C function from C function using global object pointer<br />
[myMainWindow printSomething];<br />
}</p>
<p>@end</p></blockquote>
<p><strong>main.m</strong></p>
<p>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&#8217;t forget to clean up after ourselves.</p>
<blockquote><p>#import &#8220;MainWindow.h&#8221;<br />
#import &lt;Foundation/NSAutoreleasePool.h&gt;</p>
<p>int main(int argc, char *argv[]) {</p>
<p>//create an AutoreleasePool<br />
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];</p>
<p>//init gtk engine<br />
gtk_init(&amp;argc, &amp;argv);</p>
<p>//set up GUI<br />
MainWindow *mainWindow = [[MainWindow alloc] initWithArgCount:&amp;argc andArgVals:argv];</p>
<p>//begin the GTK loop<br />
[mainWindow startGtkMainLoop];</p>
<p>//free the GUI<br />
[mainWindow release];</p>
<p>//drain the pool<br />
[pool release];</p>
<p>//exit application<br />
return 0;<br />
}</p></blockquote>
<p><strong>Compiling it all together</strong></p>
<p>Use the following command to compile the program. This will automatically include <strong>all</strong> <em>.m</em> files in the current directory so be careful when and where you run this.</p>
<p>gcc `pkg-config &#8211;cflags &#8211;libs gtk+-2.0` -lgnustep-base -fconstant-string-class=NSConstantString -o &#8220;./myprogram&#8221; $(find . -name &#8216;*.m&#8217;) -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -std=c99 -O3</p>
<p>Once complete you will notice a new executable in the directory called <em>myprogram</em>. Start this program and you will see our GTK+ window in action.</p>
<p><a href="http://www.tylerburton.ca/files/wordpress/2010/12/window1.jpg"><img class="aligncenter size-full wp-image-546" title="window1" src="http://www.tylerburton.ca/files/wordpress/2010/12/window1.jpg" alt="" width="242" height="186" /></a></p>
<p>If you run it from the command line you can see the output that we coded when the button is pushed.</p>
<p><a href="http://www.tylerburton.ca/files/wordpress/2010/12/window2.jpg"><img class="aligncenter size-full wp-image-547" title="window2" src="http://www.tylerburton.ca/files/wordpress/2010/12/window2.jpg" alt="" width="658" height="289" /></a></p>
<p><strong>Wrapping it up</strong></p>
<p>There you have it. We now have a program that is written in Objective-C, using C&#8217;s native GTK+ &#8216;bindings&#8217; 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.</p>
<p><strong>Source Code Downloads</strong></p>
<table border="1">
<tbody>
<tr>
<td></td>
<td><strong>Source Only Package</strong></td>
</tr>
<tr>
<td><strong>File name:</strong></td>
<td>objective_c_gtk_source.zip</td>
</tr>
<tr>
<td><strong>File hashes:</strong></td>
<td style="text-align: center;"><a href="http://www.tylerburton.ca/files/hashes/objective_c_gtk_source.hashes.txt">Download Here</a></td>
</tr>
<tr>
<td><strong>File size:</strong></td>
<td>2.4KB</td>
</tr>
<tr>
<td><strong>File download:</strong></td>
<td style="text-align: center;"><a href="http://www.tylerburton.ca/files/apps/objective_c_gtk_source.zip">Download Here</a></td>
</tr>
</tbody>
</table>
<div class="shr-publisher-538"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.tylerburton.ca/2010/12/create-a-gtk-application-on-linux-with-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing gnustep-devel in Ubuntu 10.04</title>
		<link>http://www.tylerburton.ca/2010/05/fixing-gnustep-devel-in-ubuntu-10-04/</link>
		<comments>http://www.tylerburton.ca/2010/05/fixing-gnustep-devel-in-ubuntu-10-04/#comments</comments>
		<pubDate>Mon, 17 May 2010 22:00:44 +0000</pubDate>
		<dc:creator>Tyler Burton</dc:creator>
				<category><![CDATA[F/OSS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[GNUstep]]></category>
		<category><![CDATA[gnustep-devel]]></category>
		<category><![CDATA[gorm.app]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[ubuntu 10.04]]></category>

		<guid isPermaLink="false">http://www.tylerburton.ca/?p=288</guid>
		<description><![CDATA[When Ubuntu 10.04 was released it represented the most modern incarnation of Canonical&#8217;s premier Linux desktop distribution. However not all things were better in this release. For myself I immediately noticed a problem while trying to install the gnustep-devel development libraries for GNUstep and Objective-C. I was greeted with this oh so lovely error message: [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>When <a href="http://www.ubuntu.com" target="_blank">Ubuntu 10.04</a> was released it represented the most modern incarnation of Canonical&#8217;s premier Linux desktop distribution. However not all things were better in this release. For myself I immediately noticed a problem while trying to install the <a href="http://packages.ubuntu.com/lucid/gnustep-devel" target="_blank">gnustep-devel</a> development libraries for <a href="http://gnustep.org/" target="_blank">GNUstep</a> and <a href="http://en.wikipedia.org/wiki/Objective-C" target="_blank">Objective-C</a>. I was greeted with this oh so lovely error message:</p>
<blockquote><p>Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming.<br />
The following information may help resolve the situation:</p>
<p>The following packages have unmet dependencies:<br />
gnustep-devel: Depends: gorm.app but it is not installable<br />
E: Broken packages</p></blockquote>
<p>So essentially I was left with the following choice: install the missing <a href="http://wiki.gnustep.org/index.php/Gorm" target="_blank">Gorm.app</a> package from the repository (which wasn&#8217;t there) or don&#8217;t install gnustep-devel. Thankfully it was pointed out to me that I could perhaps see if the package still existed in the Debian repository instead. So off to <a href="http://www.debian.org/distrib/packages" target="_blank">http://www.debian.org/distrib/packages</a> I went and after a quick search I found what I was <a href="http://packages.debian.org/search?keywords=gorm.app&amp;searchon=names&amp;suite=all&amp;section=all" target="_blank">looking for</a>! I recalled reading somewhere that Ubuntu synchronizes with <a href="http://www.debian.org" target="_blank">Debian</a> testing (A.K.A. squeeze) at the start of every round of development, so I figured that would be the best package to grab. A short download and install later Gorm.app was finally on my system. With the dependencies now met it was a breeze to install the rest of the development files using a simple</p>
<blockquote><p>sudo apt-get install gnustep-devel</p></blockquote>
<p>And there you have it. By installing a single package from the Debian repository you too can get around the problem. For reference I have also filed a bug report with Ubuntu at Launchpad <a href="https://bugs.launchpad.net/ubuntu/+source/meta-gnustep/+bug/579735" target="_blank">here</a>.</p>
<div class="shr-publisher-288"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.tylerburton.ca/2010/05/fixing-gnustep-devel-in-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

