Blame platform-demos/C/gmenu.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="gmenu.py">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">GMenu (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
Packit 1470ea
    <link type="seealso" xref="signals-callbacks.py" />
Packit 1470ea
    <link type="next" xref="menubutton.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-04-28" 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">
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
    <desc>A simple implementation of GMenu</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>GMenu</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/gmenu.py.png"/>
Packit 1470ea
  

A GtkApplication with a simple GMenu and SimpleActions

Packit 1470ea
Packit 1470ea
  <links type="section" />
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Code used to generate this example</title>
Packit 1470ea
    
Packit 1470ea
    <xi:include href="samples/gmenu.py" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>Useful methods for a GSimpleAction and a GMenu</title>
Packit 1470ea
Packit 1470ea
    

In line 33 the signal "activate" from the action new_action (not the menu!) is connected to the callback function new_cb() using action.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
Packit 1470ea
    

Useful methods for a GSimpleAction:

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

To create a new action that is stateless, that is, an action that do not retain or depend on a state given by the action itself, use

Packit 1470ea
      
Packit 1470ea
action = Gio.SimpleAction.new("name", parameter_type)
Packit 1470ea
      

where "name" is the name of the action and parameter_type is the type of the parameters that the action receives when being activated. This can be None, or GLib.VariantType.new('s') if the parameter is of type str, or instead of 's' a character as described <link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">here</link>. To create a new stateful (i.e. not stateless) action, use

Packit 1470ea
      
Packit 1470ea
action = Gio.SimpleAction.new_stateful("name", parameter_type, initial_state)
Packit 1470ea
      

where initial_state is defined as a GVariant - for instance Glib.Variant.new_string('start'); for a list of possibilities see <link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">here</link>.

</item>
Packit 1470ea
      <item>

set_enabled(True) sets the action as enabled; an action must be enabled in order to be activated or in order to have its state changed from outside callers. This should only be called by the implementor of the action. Users of the action should not attempt to modify its enabled flag.

</item>
Packit 1470ea
      <item>

set_state(state), where state is a GVariant, sets the state of the action, updating the 'state' property to the given value. This should only be called by the implementor of the action; users of the action should instead call change_state(state) (where state is as above) to request the change.

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

Useful methods for a GMenu:

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

To insert an item in the menu in position position, use insert(position, label, detailed_action), where label is the label that will appear in the menu and detailed_action is a string composed of the name of the action to which we prepend the prefix app.. A more detailed discussion of this can be found in <link xref="menubar.py#win-app" />.

Packit 1470ea
      

To append or prepend an item in the menu use respectively append(label, detailed_action) and prepend(label, detailed_action).

</item>
Packit 1470ea
      <item>

Another way of adding items to the menu is to create them as GMenuItems and use insert_item(position, item), append_item(item), or prepend_item(item); so for instance we might have:

Packit 1470ea
      
Packit 1470ea
about = Gio.MenuItem.new("About", "app.about")
Packit 1470ea
menu.append_item(about)
Packit 1470ea
      </item>
Packit 1470ea
      <item>

We can also add a whole subsection in a menu using insert_section(position, label, section), append_section(label, section), or prepend_section(label, section), where label is the title of the subsection.

</item>
Packit 1470ea
      <item>

To add a submenu that will expand and collapse, use insert_submenu(position, label, section), append_submenu(label, section), or prepend_submenu(label, section), where label is the title of the subsection.

</item>
Packit 1470ea
      <item>

To remove an item from the menu, use remove(position).

</item>
Packit 1470ea
      <item>

To set a label for the menu, use set_label(label).

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

In this sample we used the following:

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

<link href="http://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">GVariantType</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">GVariant</link>

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