GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 2:06 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: GTKbuilder vs GtkUIManager
PostPosted: Fri Jan 04, 2008 5:03 pm 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
Hello All,

Happy New Year to everyone. I am new to this forum!

I am starting to write an interpreter/compiler (which may generate C code dynamically and uses GTK on Linux/Debian/Sid/AMD64 with gtk2.12) which include a persistency mechanism: all the "heap" (from a particular root, in garbage collector parlance) is wriiten to a file which is reloaded at a later execution.

I am interested in being able to store most of the GTK GUI in a persistent fashion. So far, I see two options: using GtkBuilder or using GtkUIManager

The advantage of GtkBuilder is that apparently it has been designed for the very purpose of GTK serialization. Tjhe disadvantage is that it it quite new, and not very well documented.

The advantage of GtkUIManager is that it is easier for menu & buttons. Unfortunately; it cannot persist other widgets (like even partly text widgets).

My main wish is just to get several examples of GTKBuilder XML files or texts. Apparently, there are few examples!

More precisely, the following code does work as I would expect (i.e. making a menu with a single text item)

Code:
const char* xmlstr = "<interface>"
    "<object class='GtkMenu' id='globalmenu'>"
    "<child>"
    "<object class='GtkMenuItem' id='globitem1'>"
    "<child>"
    "<object class='GtkLabel' id='globlab1'>"
    "<property name='label'>first menu item</property>"
    "</object>"
    "</child>" "</object>" "</child>" "</object>" "</interface>";
gtk_builder_add_from_string (firstbuilder,
                         xmlstr,
                       (gsize) -1, &err);


But I don't really understand if I really have to name every widget with an id= attribute, even those simple widgets (like the label above) which are only the unique child of their container. I'm afraid of a namespace cluttering.

So if you have code to show, please do.

PS. My stuff will be GPL or LGPL, but it is to early to release anything.

Regards.

Basile Starynkevitch (France)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 5:30 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
GtkBuilder uses GtkUIManager. Once easy way to get examples, is to build and interface in Glade and then use gtk-builder-convert to make it into a GtkBuilder XML file. For example...

Create a window in glade and drop in a menu. The glade file looks like this:

example.glade
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.0 on Fri Jan  4 09:27:28 2008 -->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
    <child>
      <widget class="GtkMenuBar" id="menubar1">
        <property name="visible">True</property>
        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
        <child>
          <widget class="GtkMenuItem" id="menuitem1">
            <property name="visible">True</property>
            <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
            <property name="label" translatable="yes">_File</property>
            <property name="use_underline">True</property>
            <child>
              <widget class="GtkMenu" id="menu1">
                <property name="visible">True</property>
                <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem1">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-new</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem2">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-open</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem3">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-save</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem4">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-save-as</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkSeparatorMenuItem" id="separatormenuitem1">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem5">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-quit</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
              </widget>
            </child>
          </widget>
        </child>
        <child>
          <widget class="GtkMenuItem" id="menuitem2">
            <property name="visible">True</property>
            <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
            <property name="label" translatable="yes">_Edit</property>
            <property name="use_underline">True</property>
            <child>
              <widget class="GtkMenu" id="menu2">
                <property name="visible">True</property>
                <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem6">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-cut</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem7">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-copy</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem8">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-paste</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem9">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-delete</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
              </widget>
            </child>
          </widget>
        </child>
        <child>
          <widget class="GtkMenuItem" id="menuitem3">
            <property name="visible">True</property>
            <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
            <property name="label" translatable="yes">_View</property>
            <property name="use_underline">True</property>
          </widget>
        </child>
        <child>
          <widget class="GtkMenuItem" id="menuitem4">
            <property name="visible">True</property>
            <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
            <property name="label" translatable="yes">_Help</property>
            <property name="use_underline">True</property>
            <child>
              <widget class="GtkMenu" id="menu3">
                <property name="visible">True</property>
                <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                <child>
                  <widget class="GtkImageMenuItem" id="imagemenuitem10">
                    <property name="visible">True</property>
                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                    <property name="label" translatable="yes">gtk-about</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                  </widget>
                </child>
              </widget>
            </child>
          </widget>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>



Then, convert it to a GtkBuilder XML file using:
[code=]gtk-builder-convert example.glade example.xml[/code]

example.xml
Code:
<?xml version="1.0"?>
<!--Generated with glade3 3.4.0 on Fri Jan  4 09:27:28 2008 -->
<interface>
  <object class="GtkUIManager" id="uimanager1">
    <child>
      <object class="GtkActionGroup" id="actiongroup1">
        <child>
          <object class="GtkAction" id="menuitem1">
            <property name="name">menuitem1</property>
            <property name="label">_File</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem1">
            <property name="stock_id">gtk-new</property>
            <property name="name">imagemenuitem1</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem2">
            <property name="stock_id">gtk-open</property>
            <property name="name">imagemenuitem2</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem3">
            <property name="stock_id">gtk-save</property>
            <property name="name">imagemenuitem3</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem4">
            <property name="stock_id">gtk-save-as</property>
            <property name="name">imagemenuitem4</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem5">
            <property name="stock_id">gtk-quit</property>
            <property name="name">imagemenuitem5</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="menuitem2">
            <property name="name">menuitem2</property>
            <property name="label">_Edit</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem6">
            <property name="stock_id">gtk-cut</property>
            <property name="name">imagemenuitem6</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem7">
            <property name="stock_id">gtk-copy</property>
            <property name="name">imagemenuitem7</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem8">
            <property name="stock_id">gtk-paste</property>
            <property name="name">imagemenuitem8</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem9">
            <property name="stock_id">gtk-delete</property>
            <property name="name">imagemenuitem9</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="menuitem3">
            <property name="name">menuitem3</property>
            <property name="label">_View</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="menuitem4">
            <property name="name">menuitem4</property>
            <property name="label">_Help</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="imagemenuitem10">
            <property name="stock_id">gtk-about</property>
            <property name="name">imagemenuitem10</property>
          </object>
        </child>
      </object>
    </child>
    <ui>
      <menubar name="menubar1">
        <menu action="menuitem1">
          <menuitem action="imagemenuitem1"/>
          <menuitem action="imagemenuitem2"/>
          <menuitem action="imagemenuitem3"/>
          <menuitem action="imagemenuitem4"/>
          <separator/>
          <menuitem action="imagemenuitem5"/>
        </menu>
        <menu action="menuitem2">
          <menuitem action="imagemenuitem6"/>
          <menuitem action="imagemenuitem7"/>
          <menuitem action="imagemenuitem8"/>
          <menuitem action="imagemenuitem9"/>
        </menu>
        <menuitem action="menuitem3"/>
        <menu action="menuitem4">
          <menuitem action="imagemenuitem10"/>
        </menu>
      </menubar>
    </ui>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
    <child>
      <object class="GtkMenuBar" constructor="uimanager1" id="menubar1">
        <property name="visible">True</property>
        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
      </object>
    </child>
  </object>
</interface>

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot], Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group