User:Gnepets/chap1

From Wikibooks, open books for an open world
Jump to navigation Jump to search

In this chapter we will look at some very simple GTK+ examples. Theory will be at the bottom of this chapter, it should be much easier to digest once you have written some code.

An empty window[edit | edit source]

In this example we create a single window, set its title and size and connect an event that allows the application to close.

#include <gtk/gtk.h>

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

  /* Initialise GTK+ and  */
  gtk_init (&argc, &argv);

  /* create a new window, set values */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello World");
  gtk_widget_set_size_request (window, 200, 200);
  /* connect the windows "destroy" event */
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);  

  /* set the window as visible */
  gtk_widget_show (window);

  /* run the GTK+ main loop */
  gtk_main ();
  return 0;
}

Congratulations, you have created your first GTK+ window. You may not completely understand the code you have written yet, thats allright eventually you will. GTK+ is object oriented, as C is not designed as an object oriented language you must explicitly upcast and downcast objects, this will occur most often with the paramater passed to a function. You first cast window from a GtkWidget to a GtkWindow when you wrote GTK_WINDOW (window).

gtk_init (&argc, &argv);

This call initialise GTK+ and hands to it a list of command line arguments the program recieved when initialising.

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

This line creates a new GtkWindow down cast as a GtkWidget

GTK_WINDOW_TOPLEVEL A gtk window that registers itself to the window manager as a window.
gtk_window_set_title (GTK_WINDOW (window), "Hello World");

this function sets the title of a GtkWindow.

GTK_WINDOW (window) the main window, cast from a GtkWidget to a GtkWindow
"Hello World" the new title of the window
gtk_widget_set_size_request (window, 200, 200);

we are setting the windows size request, as the name suggests a size request does not guarantee you will get a specific size, you will learn more about this later.

window a GtkWidget
200 width
200 height
g_signal_connect (G_OBJECT (window), "destroy",
                  G_CALLBACK (gtk_main_quit), NULL);

we are connecting a callback (function) to be called when the "destroy" event is emitted from window.

G_OBJECT (window) window cast as a G_OBJECT
"destroy" the signal that is being connected to
G_CALLBACK (gtk_main_quit) the function that is called when the signal is sent
NULL an empty gpointer, for data that could be passed to the function
gtk_widget_show (window);

show the widget window, all widgets must be shown including all parent widgets.

window the widget that is being made visible
gtk_main ();

we rest in GTKs main loop until gtk_main_quit () is called; we have connected gtk_main_quit () to the windows "destroy" event.

Window with a button[edit | edit source]

TODO

Theory[edit | edit source]

TODO