Search code examples
cgtk

C coding for GTK+ (a simple case)


I am learning C and compiling the codes with GTK+. I have a simple GTK+ of

#include <gtk/gtk.h>

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

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(window);

  gtk_main();

  return 0;
}

Could you please give me a hint how can I insert C processing codes within this structure? For example, I want to read the content of a file and display within the gtk+ window.


Solution

  • You need to write a "callback" that will respond to some "event".

    This is how all GUI frameworks work - they're all "event driven".

    The specific GTK+ 2.0 API is "g_signal_connect()".

    Here's a very simple example:

    http://zetcode.com/tutorials/gtktutorial/gtkevents/

    Here is a much more extensive tutorial:

    http://developer.gnome.org/gtk-tutorial/2.90/

    If you work through this tutorial - you'll learn a lot of useful material about GTK+ - and quite possibly learn a lot of useful information about C, too.