blog/posts/2013-03-26-starting-with-gtk-programming.markdown

83 lines
3.7 KiB
Markdown
Raw Normal View History

---
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>
2017-06-10 14:13:43 +02:00
<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>
2017-06-10 14:13:43 +02:00
<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>
2017-06-10 14:13:43 +02:00
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.
2017-06-10 14:13:43 +02:00
First I just typed the below program in the Nano editor.
2017-06-10 14:13:43 +02:00
```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;
}
2017-06-10 14:13:43 +02:00
```
2017-06-10 14:13:43 +02:00
You can compile the above code by typing the following on command line.
2017-06-10 14:13:43 +02:00
```c
gcc -o hello hello.c `pkg-config --libs --cflags gtk+-2.0`
2017-06-10 14:13:43 +02:00
```
2017-06-10 14:13:43 +02:00
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).
2017-06-10 14:13:43 +02:00
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.