1 gtkbuilder-porting-guide.txt
2 ============================
4 This document describes some tips and rules for porting UI code
5 written with GTK+ and C to GtkBuilder + Glade.
12 1. Locate code to port
13 2. Start a new UI file with Glade
14 3. Systematically convert the code to Glade
15 4. Construct UI with GtkBuilder and do setup of widgets
16 5. Add .ui file to build system
18 7. Enjoy less UI C code
26 Look for code that looks like this:
28 // Create a widget and add to hierarchy
29 widget = gtk_some_widget_new (some_params);
30 gtk_some_container_add (container, widget)
31 gtk_widget_show (widget);
37 Start a new UI file with Glade
38 ------------------------------
40 Start glade-3. Pick project file format 'GtkBuilder' (not
41 'Libglade'). For maximum compatibility, use the minimal gtk+ catalog
42 possible. The file extension shall be .ui. Look where other files are
43 put and how they are named.
47 Systematically convert the code to Glade
48 ----------------------------------------
50 Go through the code that you want to convert line by line and add
51 widgets in Glade as you remove lines. For example:
53 main_vbox = gtk_vbox_new (FALSE, 12);
54 gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
55 gtk_container_add (GTK_CONTAINER (dialog_vbox),
57 gtk_widget_show (main_vbox);
61 <object class="GtkVBox" id="main-vbox">
62 <property name="visible">True</property>
63 <property name="border_width">12</property>
64 <property name="orientation">vertical</property>
65 <property name="spacing">12</property>
71 in the UI declaration produced by Glade.
75 Construct UI with GtkBuilder and do setup of widgets
76 ----------------------------------------------------
78 The code to construct the UI will look something like this:
80 builder = gtk_builder_new ();
81 ui_file = g_build_filename (gimp_data_directory (),
82 "ui/plug-ins/plug-in-file-gif-save.ui",
84 if (! gtk_builder_add_from_file (builder, ui_file, &error))
85 g_printerr (_("Error loading UI file '%s':\n%s"),
86 ui_file, error ? error->message : "???");
89 and then you do setup of widgets using:
91 widget = GTK_WIDGET (gtk_builder_get_object (builder, "widget-name"));
92 gtk_widget_whatever (widget, params);
94 Look in plug-ins/common/file-gif-save.c for helper function you can
95 use for some tricky widgets.
99 Add .ui file to build system
100 ----------------------------
102 The UI declarations are installed as data files, see
103 plug-ins/ui/Makefile.am for example, and it needs to be added to
104 POTFILES.in for translations.
111 When you're done, make sure
113 1. that translations still work. If they don't, maybe you forgot to
114 add the UI file to the relevant POTFILES.in or maybe you changed
115 strings, for example by adding markup. In the latter case, use pango
116 text styles instead of markup (use GKT+ 2.16 UI files).
118 2. that mnemonics still work, in particular when the mnemonic is not
119 on the widget to be activated. For e.g. labels you need to explicitly
120 assign a widget that will be actiated when the label mnemonic is
123 3. that the spacing and other layout detals are still correct.
137 If your GtkComboBox doesn't draw any items it's probably because it
138 doesn't have a cell renderer. Appearently there is no UI to add one in
139 GLade-3, so add it manually in the UI file, see the GTK+ doc for
140 GtkCellLayout; this is what you need to add:
142 <object class="GtkComboBox" name="some-id">
145 <object class="GtkCellRendererText"/>
147 <attribute name="text">0</attribute>