GtkWidgetFactory



Umm, I've written a GtkWidgetFactory "widget" that I've developed enough to
be useful for my own uses. The widget configuration looks like ItemFactory,
and the widgets are accessed like libglade.

Here is the code to create a window with the title "GtkWidgetFactory",
working close button, a label reading "Who loves ya, baby?", a password
style entry box, and a button below reading "So who is it!?". The button
is connected to a routine to print what's in the text box.

********************************

  int
  main(int argc, char *argv[])
  {
    static GtkWidgetFactory widgets[] = {
      { "GtkWindow", NULL,     "window", "delete_event:delete,destroy:destroy", "title='GtkWidgetFactory'" },
      { "GtkVBox",   "window", "vbox",   NULL, NULL },
      { "GtkHBox",   "vbox",   "hbox",   NULL, NULL },
      { "GtkLabel",  "hbox",   NULL,     NULL, "label='Who loves ya, baby?'" },
      { "GtkEntry",  "hbox",   "entry",  NULL, "visibility=0" },
      { "GtkButton", "vbox",   NULL,     "clicked:reveal", "label='So who is it!?'" },
      { NULL,        NULL,     NULL,     NULL, NULL }
    };
    GtkWidget *window;

    gtk_init(&argc, &argv);

    hash = gtk_widget_factory_new(widgets);

    window = g_hash_table_lookup(hash, "window");
    gtk_widget_show_all(window);

    gtk_main();
    return(0);
  }

********************************

A brief explanation of the structure...

    { "GtkButton", "vbox",   "button", "clicked:reveal", "label='So who is it!?'" },

The arguments are:

  1: The type of widget to create
  2: The handle of the parent (for layout)
  3: The handle to give the widget
  4: The signals and functions to connect
  5: The arguments to pass to the widget

The widgets are then created and loaded into a hash table with
gtk_widget_factory_new(). Widgets can be retrieved from the hash
table by the handle given to the widget in the structure. This
is being used to get and display the main window widget.

The widget handle can be used to retrieve the widget for further changes.
This is what I do anyway, to avoid making the structure too hard to read.

This format lets me change widget layouts without changing any code as
everything's keyed off the handles. The embedded config strings can be made
into #defines to make the structure neater, which also allows global
settings by using the same configuration strings for a range of widgets.

I prefer this using this method than using a GUI builder. I find it easier
to change the layout by moving lines up and down and there's no incurred
library or program dependencies. I don't have to change any code either.

Does this widget sound useful to anyone else? It's so small and simple, I
feel silly offering this, but it is surprisingly useful (to me anyway).
It'd fit into the Gtk+ library with no trouble.

--
  spwhite@chariot.net.au
  whatever on IRC





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]