110 lines
3.7 KiB
Markdown
110 lines
3.7 KiB
Markdown
|
---
|
|||
|
author: Sanchayan Maity
|
|||
|
title: Starting with GTK+ Programming
|
|||
|
tags: gtk, linux
|
|||
|
---
|
|||
|
|
|||
|
<p style='text-align: justify;'>It's been a while since my last post in January. I shifted to Bangalore for work and took some time to get used to the new work environment and schedule. Anyways, let's get on with the topic at hand.</p>
|
|||
|
|
|||
|
<p style='text-align: justify;'>My current work involves a lot of GUI development on WinCE in C# and since i wanted to work on Linux, it left much to be desired. So, i thought why not do some GUI development on Linux. I could have opted for Qt and an Integrated Development Environment does speed up the work a lot and though developing GUI applications is a breeze in C#/QT with an IDE, i don't like GUI development and it becomes boring if things become too simple, like it happened with C#. So, i chose GTK+ for this task and Nano as the editor of my choice on my new Arch Linux installation to amuse myself and keep myself from getting bored.</p>
|
|||
|
|
|||
|
<p style='text-align: justify;'>Also, on a different note, i recently tried my hands on Gentoo. For any package installation, having to download the source, compile the package and then install, turned me off it. Actually, i have wanted to contribute to the open source community for a while, and this [http://blog.dastergon.gr/get-involved-in-gentoo-linux/](http://blog.dastergon.gr/get-involved-in-gentoo-linux/) motivated me to go ahead with Gentoo, but, i was put off by the package management. I will stick with Arch.</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
I am using Xfce as my desktop environment in Arch Linux. At first, i was worried about having to take care of package installation and dependencies for starting with GTK programming, but, i think during the Xfce installation or something else this was taken care of.
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
First i just typed the below program in the Nano editor.
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
{% highlight c %}
|
|||
|
|
|||
|
#include <gtk/gtk.h>
|
|||
|
|
|||
|
static void hello( GtkWidget *widget, gpointer data )
|
|||
|
{
|
|||
|
g_print ("Hello World\n");
|
|||
|
}
|
|||
|
|
|||
|
static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data )
|
|||
|
{
|
|||
|
g_print ("Delete event occurred\n");
|
|||
|
|
|||
|
return FALSE;
|
|||
|
}
|
|||
|
|
|||
|
static void destroy( GtkWidget *widget, gpointer data )
|
|||
|
{
|
|||
|
gtk_main_quit ();
|
|||
|
}
|
|||
|
|
|||
|
int main( int argc, char *argv[] )
|
|||
|
{
|
|||
|
GtkWidget *window;
|
|||
|
GtkWidget *button;
|
|||
|
|
|||
|
gtk_init (&argc, &argv);
|
|||
|
|
|||
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|||
|
|
|||
|
g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
|
|||
|
|
|||
|
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
|
|||
|
|
|||
|
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
|||
|
|
|||
|
button = gtk_button_new_with_label ("Hello World");
|
|||
|
|
|||
|
g_signal_connect (button, "clicked", G_CALLBACK (hello), NULL);
|
|||
|
|
|||
|
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
|
|||
|
|
|||
|
gtk_container_add (GTK_CONTAINER (window), button);
|
|||
|
|
|||
|
gtk_widget_show (button);
|
|||
|
|
|||
|
gtk_widget_show (window);
|
|||
|
|
|||
|
gtk_main ();
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
{% endhighlight %}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
You can compile the above code by typing the following on command line.
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
{% highlight c %}
|
|||
|
gcc -o hello hello.c `pkg-config --libs --cflags gtk+-2.0`
|
|||
|
{% endhighlight %}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
After compilation, run the output file generated and see the output. I did not intend much with this tutorial, but, just wanted to share. This post is more sort of an incoherent musing and working on GUI's is boring and i can't make head or tails of how to make good user interfaces.
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
You can find a detail tutorial and explanation of various functions, API's and callbacks on the GNOME development center below.
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
[https://developer.gnome.org/gtk-tutorial/2.24/book1.html](https://developer.gnome.org/gtk-tutorial/2.24/book1.html).
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
I hope to put up some articles soon on PCB design, which i have been putting off and some other stuff related to programming may be.
|