gtkbuilder-porting-guide.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. gtkbuilder-porting-guide.txt
  2. ============================
  3. This document describes some tips and rules for how to port UI code
  4. written with GTK+ and C to GtkBuilder + Glade.
  5. Overview
  6. --------
  7. 1. Locate code to port
  8. 2. Start a new UI file with Glade
  9. 3. Systematically convert the code to Glade
  10. 4. Construct UI with GtkBuilder and do setup of widgets
  11. 5. Add .ui file to build system
  12. 6. Test
  13. 7. Enjoy less UI C code
  14. 8. Troubleshooting
  15. Locate code to port
  16. -------------------
  17. Look for code that looks like this:
  18. // Create a widget and add to hierarchy
  19. widget = gtk_some_widget_new (some_params);
  20. gtk_some_container_add (container, widget)
  21. gtk_widget_show (widget);
  22. // Repeat...
  23. Start a new UI file with Glade
  24. ------------------------------
  25. Start glade-3. Pick project file format 'GtkBuilder' (not
  26. 'Libglade'). For maximum compatibility, use the minimal gtk+ catalog
  27. possible. The file extension shall be .ui. Look where other files are
  28. put and how they are named.
  29. Systematically convert the code to Glade
  30. ----------------------------------------
  31. Go through the code that you want to convert line by line and add
  32. widgets in Glade as you remove lines. For example:
  33. main_vbox = gtk_vbox_new (FALSE, 12);
  34. gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  35. gtk_container_add (GTK_CONTAINER (dialog_vbox),
  36. main_vbox);
  37. gtk_widget_show (main_vbox);
  38. is replaced by
  39. <object class="GtkVBox" id="main-vbox">
  40. <property name="visible">True</property>
  41. <property name="border_width">12</property>
  42. <property name="orientation">vertical</property>
  43. <property name="spacing">12</property>
  44. <child>
  45. <!-- ... -->
  46. </child>
  47. </object>
  48. in the UI declaration produced by Glade.
  49. Construct UI with GtkBuilder and do setup of widgets
  50. ----------------------------------------------------
  51. The code to construct the UI will look something like this:
  52. builder = gtk_builder_new ();
  53. ui_file = g_build_filename (gimp_data_directory (),
  54. "ui/plug-ins/plug-in-file-gif-save.ui",
  55. NULL);
  56. if (! gtk_builder_add_from_file (builder, ui_file, &error))
  57. g_printerr (_("Error loading UI file '%s':\n%s"),
  58. ui_file, error ? error->message : "???");
  59. g_free (ui_file);
  60. and then you do setup of widgets using:
  61. widget = GTK_WIDGET (gtk_builder_get_object (builder, "widget-name"));
  62. gtk_widget_whatever (widget, params);
  63. Look in plug-ins/common/file-gif-save.c for helper function you can
  64. use for some tricky widgets.
  65. Add .ui file to build system
  66. ----------------------------
  67. The UI declarations are installed as data files, see
  68. plug-ins/ui/Makefile.am for example, and it needs to be added to
  69. POTFILES.in for translations.
  70. Test
  71. ----
  72. When you're done, make sure
  73. 1. that translations still work. If they don't, maybe you forgot to
  74. add the UI file to the relevant POTFILES.in or maybe you changed
  75. strings, for example by adding markup. In the latter case, use pango
  76. text styles instead of markup (use GKT+ 2.16 UI files).
  77. 2. that mnemonics still work, in particular when the mnemonic is not
  78. on the widget to be activated. For e.g. labels you need to explicitly
  79. assign a widget that will be actiated when the label mnemonic is
  80. pressed.
  81. 3. that the spacing and other layout detals are still correct.
  82. Enjoy less UI C code
  83. --------------------
  84. Enjoy!
  85. Troubleshooting
  86. ---------------
  87. If your GtkComboBox doesn't draw any items it's probably because it
  88. doesn't have a cell renderer. Appearently there is no UI to add one in
  89. GLade-3, so add it manually in the UI file, see the GTK+ doc for
  90. GtkCellLayout; this is what you need to add:
  91. <object class="GtkComboBox" name="some-id">
  92. ...
  93. <child>
  94. <object class="GtkCellRendererText"/>
  95. <attributes>
  96. <attribute name="text">0</attribute>
  97. </attributes>
  98. </child>
  99. </object>