[home] [<-back] [index] [next->]
____________________________________________________________________ [10:. - [ Coding GTK+ and GNOME ] [nethunter] :. ] [nethunter@b0g.org] :. ] ____________________________________________________________________ o, you are into programming, and want to create cool apps for your GNOME desktop, so the whole free software movement will benefit? Or you just want all your friends to see how leet you are? Then you came to the right place! This tutorial will teach you how to code in GTK+ and GNOME. This is part of a beginners series. For this tutorial lets create a simple notepad like application. First, if you ever coded for any object oriented system, you know the concept of events. GTK+ signals are pretty much the same. Generally the idea is simple, you tell a button, "Here's the deal: When the (l)user is pressing you, call this function: button_callback_or_something, and I'll tell you what to do from there...". OK, lets begin. First we begin the program: int main(int argc, char *argv) { gnome_init(PACKAGE, VERSION, argc, argv); make_ui(); gnome_main(); } I don't think this requires explanation, but in case it does, here it is: gnome_init - initializes the gnome libraries. gnome_main - starts the main GTK+ loop. make_ui - will follow... OK - make_ui - for clearity I separated the user interface from the main function. Unless your lame, you won't do it in your own apps. Once you get the idea of GTK+, don't forget to read the guidelines on developer.gnome.org. Anyway, I'm off the subject. void make_ui() { GtkWidget *window, *entry; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_ref(window); gtk_widget_set_name(GTK_WIDGET(window), "main_window"); entry = gtk_entry_new(); gtk_widget_ref(entry); gtk_widget_set_name(GTK_WIDGET(entry), "text"); gtk_container_add(GTK_WIDGET(entry), GTK_CONTAINER(window)); gtk_widget_show_all(window); } If you will try to run this app, you will notice, that the progie does exit when you close the window. To make the app do that, you need to connect the "delete_event" signal from the window, to gtk_main_quit(). Anyway, we'll deal with it later. OK, the gtk_(something)_new, is, (duh), creating a new widget. It returns a pointer to the widget. gtk_window_new receives a argument - window type. It can be GTK_WINDOW_TOPLEVEL, GTK_WINDOW_DIALOG, or GTK_WINDOW_POPUP. gtk_widqet_ref - makes GTK+ notice the widget. It includes the widget in the event loop. It makes the widget emit signals. gtk_widget_set_name - obviously - set the "name" property of the widget. Since GTK can't guess how you named your variable, in which you contain the pointer to the widget, you must set it. gtk_container_add - this function adds the widget to the container. In this case, the container is the window, and the widget is the entry. K, now that we understand what it means, let's connect the signal. gtk_signal_connect(GTK_OBJECT(window), "delete_event", (GtkSignalFunc)ExitApp, NULL); void ExitApp(gpointer data) { gtk_main_quit(); } So, what do we got here? gtk_signal_connect - is used to tell GTK to call the function ExitApp when "delete_event" is emitted. This should be called somewhere after gtk_window_new(). gtk_main_quit() is exiting the event loop. Now, you see this NULL in there, its the parameter to the callback function. You can pass anything in there. In this case we don't need anything, so we pass NULL. Anyway, once the window is closed, the program exits. K, see ya next month, when k-rad-bob will get the next b0G out. The subject of my next article, will be packing boxes, when the ultimate goal is creating a gui for a DoS attack. Sorry for the lack of l33t sp34k, but this article was written on a palm, and the palm is not the best place to write l33t sp34k. Anyway, I didn't decide what DoS I will use, so I'm open to suggestions. Tell k-rad-bob your DoS of choice. PS. Compiling this thing: gcc `gnome-config --cflags` -o shit -O2 shit.c `gnome-config --libs` b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@! b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@! b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@! b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@! b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!b0g!#@!