Blame platform-demos/C/toolbar_builder.py.page

Packit 1470ea
Packit 1470ea
Packit 1470ea
      xmlns:its="http://www.w3.org/2005/11/its"
Packit 1470ea
      xmlns:xi="http://www.w3.org/2001/XInclude"
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="toolbar_builder.py">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">Toolbar created using Glade (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
Packit 1470ea
    <link type="seealso" xref="toolbar.py"/>
Packit 1470ea
    <link type="seealso" xref="grid.py"/>
Packit 1470ea
    <link type="next" xref="menubar.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-07-17" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Tiffany Antopolski</name>
Packit 1470ea
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright edit">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Sebastian Pölsterl</name>
Packit 1470ea
      <email its:translate="no">sebp@k-d-w.org</email>
Packit 1470ea
      <years>2011</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A bar of buttons and other widgets</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Toolbar created using Glade</title>
Packit 1470ea
Packit 1470ea
  <media type="image" mime="image/png" src="media/toolbar.png"/>
Packit 1470ea
  

This example is similar to <link xref="toolbar.py"/>, except we use Glade to create the toolbar in an XML .ui file.

Packit 1470ea
Packit 1470ea
<links type="sections" />
Packit 1470ea
Packit 1470ea
<section id="glade">
Packit 1470ea
<title>Creating the toolbar with Glade</title>
Packit 1470ea
  

Packit 1470ea
  To create the toolbar using the <link href="http://glade.gnome.org/">Glade Interface Designer</link>:
Packit 1470ea
  

Packit 1470ea
  <steps>
Packit 1470ea
    <item>

Open Glade, and save the file as <file>toolbar_builder.ui</file>

Packit 1470ea
          

<media type="image" src="media/glade_ui.png" width="900">

Packit 1470ea
              Screenshot of Glade ui
Packit 1470ea
           </media>

Packit 1470ea
    </item>
Packit 1470ea
Packit 1470ea
    <item>

Under <gui>Containers</gui> on the left hand side, right click on the toolbar icon and select <gui>Add widget as toplevel</gui>.

Packit 1470ea
          

<media type="image" src="media/glade_select_toolbar.png">

Packit 1470ea
           Screenshot of toolbar icon in Glade ui
Packit 1470ea
          </media>

Packit 1470ea
    </item>
Packit 1470ea
Packit 1470ea
    <item>

Under the <gui>General</gui> tab on the bottom right, change the <gui>Name</gui> to <input>toolbar</input> and <gui>Show Arrow</gui> to <gui>No</gui>.

Packit 1470ea
          

<media type="image" src="media/glade_toolbar_general.png">

Packit 1470ea
             Screenshot of General tab
Packit 1470ea
          </media>

Packit 1470ea
    </item>
Packit 1470ea
Packit 1470ea
    <item>

Under the <gui>Common</gui> tab, set <gui>Horizontal Expand</gui> to <gui>Yes</gui>.

Packit 1470ea
         

<media type="image" src="media/glade_toolbar_common.png">

Packit 1470ea
              Screenshot of Common tab
Packit 1470ea
          </media>

Packit 1470ea
     </item>
Packit 1470ea
Packit 1470ea
     <item>

Right click on the toolbar in the top right and select <gui>Edit</gui>. The <gui>Tool Bar Editor</gui> window will appear.

Packit 1470ea
         

<media type="image" src="media/glade_toolbar_edit.png">

Packit 1470ea
             Screenshot of where to right click to edit toolbar.
Packit 1470ea
          </media>

Packit 1470ea
   </item>
Packit 1470ea
Packit 1470ea
   <item>

We want to add 5 ToolButtons: New, Open, Undo, Fullscreen and Leave Fullscreen. First, we will add the New ToolButton.

Packit 1470ea
     

Packit 1470ea
     <steps>
Packit 1470ea
       <item>

Under <gui>Hierarchy</gui> tab, click <gui>Add</gui>.

</item>
Packit 1470ea
       <item>

Change the name of the ToolItem to <input>new_button</input>.

</item>
Packit 1470ea
       <item>

Scroll down and set <gui>Is important</gui> to <gui>Yes</gui>. This will cause the label of the ToolButton to be shown, when you view the toolbar.

</item>
Packit 1470ea
       <item>

Enter the <gui>action name</gui>: <input>app.new</input>.

</item>
Packit 1470ea
       <item>

Change the <gui>Label</gui> to <input>New</input>.

</item>
Packit 1470ea
       <item>

Select the <gui>New</gui> Stock Id from the drop down menu, or type <input>gtk-new</input>.

</item>
Packit 1470ea
     </steps>
Packit 1470ea
     

Packit 1470ea
    Repeat the above steps for the remaining ToolButtons, with the following properties:
Packit 1470ea
  

Packit 1470ea
  
Packit 1470ea
    
Packit 1470ea
      
Packit 1470ea
        

Name

Packit 1470ea
        

Is important

Packit 1470ea
        

Action name

Packit 1470ea
        

Label

Packit 1470ea
        

Stock Id

Packit 1470ea
      
Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
      

open_button

Packit 1470ea
      

Yes

Packit 1470ea
      

app.open

Packit 1470ea
      

Open

Packit 1470ea
      

gtk-open

Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
      

undo_button

Packit 1470ea
      

Yes

Packit 1470ea
      

win.undo

Packit 1470ea
      

Undo

Packit 1470ea
      

gtk-undo

Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
      

fullscreen_button

Packit 1470ea
      

Yes

Packit 1470ea
      

win.fullscreen

Packit 1470ea
      

Fullscreen

Packit 1470ea
      

gtk-fullscreen

Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
      

leave_fullscreen_button

Packit 1470ea
      

Yes

Packit 1470ea
      

win.fullscreen

Packit 1470ea
      

Leave Fullscreen

Packit 1470ea
      

gtk-leave-fullscreen

Packit 1470ea
    
Packit 1470ea
    
Packit 1470ea
Packit 1470ea
          <media type="image" src="media/glade_toolbar_editor.png">
Packit 1470ea
Packit 1470ea
          </media>
Packit 1470ea
    </item>
Packit 1470ea
Packit 1470ea
    <item>

Close the <gui>Tool Bar Editor</gui>.

Packit 1470ea
   </item>
Packit 1470ea
Packit 1470ea
   <item>

When our program will first start, we do not want the <gui>Leave Fullscreen</gui> ToolButton to be visible, since the application will not be in fullscreen mode. You can set this in the <gui>Common</gui> tab, by clicking the <gui>Visible</gui> property to <gui>No</gui>. The ToolButton will still appear in the interface designer, but will behave correctly when the file is loaded into your program code. Note that the method show_all() would override this setting - so in the code we have to use show() separately on all the elements.

Packit 1470ea
          

<media type="image" src="media/glade_visible_no.png">

Packit 1470ea
                 Setting the visible property to No
Packit 1470ea
          </media>

Packit 1470ea
   </item>
Packit 1470ea
Packit 1470ea
    <item>

Save your work, and close Glade.

Packit 1470ea
   </item>
Packit 1470ea
Packit 1470ea
   <item>

The XML file created by Glade is shown below. This is the description of the toolbar. At the time of this writing, the option to add the class Gtk.STYLE_CLASS_PRIMARY_TOOLBAR in the Glade Interface did not exist. We can manually add this to the XML file. To do this, add the following XML code at line 9 of <file>toolbar_builder.ui</file>:

Packit 1470ea
   
Packit 1470ea
  <style>
Packit 1470ea
     <class name="primary-toolbar"/>
Packit 1470ea
  </style>
Packit 1470ea
  ]]>
Packit 1470ea
  

If you do not add this, the program will still work fine. The resulting toolbar will however look slightly different then the screenshot at the top of this page.

Packit 1470ea
   </item>
Packit 1470ea
</steps>
Packit 1470ea
  <xi:include href="samples/toolbar_builder.ui" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
<section id="code">
Packit 1470ea
<title>Code used to generate this example</title>
Packit 1470ea
Packit 1470ea
  

We now create the code below, which adds the toolbar from the file we just created.

Packit 1470ea
<xi:include href="samples/toolbar_builder.py" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
<section id="methods">
Packit 1470ea
<title>Useful methods for Gtk.Builder</title>
Packit 1470ea

For the useful methods for a Toolbar widget, see <link xref="toolbar.py" />

Packit 1470ea
Packit 1470ea

Gtk.Builder builds an interface from an XML UI definition.

Packit 1470ea
Packit 1470ea
<list>
Packit 1470ea
<item>

add_from_file(filename) loads and parses the given file and merges it with the current contents of the Gtk.Builder.

</item>
Packit 1470ea
<item>

add_from_string(string) parses the given string and merges it with the current contents of the Gtk.Builder.

</item>
Packit 1470ea
<item>

add_objects_from_file(filename, object_ids) is the same as add_from_file(), but it loads only the objects with the ids given in the object_ids list.

</item>
Packit 1470ea
<item>

add_objects_from_string(string, object_ids) is the same as add_from_string(), but it loads only the objects with the ids given in the object_ids list.

</item>
Packit 1470ea
<item>

get_object(object_id) retrieves the widget with the id object_id from the loaded objects in the builder.

</item>
Packit 1470ea
<item>

get_objects() returns all loaded objects.

</item>
Packit 1470ea
<item>

connect_signals(handler_object) connects the signals to the methods given in the handler_object. This can be any object which contains keys or attributes that are called like the signal handler names given in the interface description, e.g. a class or a dict. In line 39 the signal "activate" from the action undo_action is connected to the callback function undo_callback() using action.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
</item>
Packit 1470ea
</list>
Packit 1470ea
Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
<section id="references">
Packit 1470ea
<title>API References</title>
Packit 1470ea

Packit 1470ea
  In this sample we used the following:
Packit 1470ea

Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkGrid.html">GtkGrid</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkBuilder.html">GtkBuilder</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkWidget.html">GtkWidget</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gdk3/unstable/gdk3-Event-Structures.html#GdkEventWindowState">Event Structures</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
Packit 1470ea
</section>
Packit 1470ea
Packit 1470ea
</page>