gtk.UIManager — construct menus and toolbars from an XML description (new in PyGTK 2.4)
class gtk.UIManager( |
|
def callback( | |
def callback( | |
def callback( | |
def callback( | |
def callback( | |
def callback( |
This object is available in PyGTK 2.4 and above.
A gtk.UIManager
constructs a user interface (menus and toolbars) from one or more UI
definitions, which reference actions from one or more action groups.
The UI definitions are specified in an XML format which can be roughly described by the following DTD.
<!ELEMENT ui (menubar|toolbar|popup|accelerator)* > <!ELEMENT menubar (menuitem|separator|placeholder|menu)* > <!ELEMENT menu (menuitem|separator|placeholder|menu)* > <!ELEMENT popup (menuitem|separator|placeholder|menu)* > <!ELEMENT toolbar (toolitem|separator|placeholder)* > <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* > <!ELEMENT menuitem EMPTY > <!ELEMENT toolitem EMPTY > <!ELEMENT separator EMPTY > <!ELEMENT accelerator EMPTY > <!ATTLIST menubar name #IMPLIED > <!ATTLIST toolbar name #IMPLIED > <!ATTLIST popup name #IMPLIED > <!ATTLIST placeholder name #IMPLIED > <!ATTLIST menu name #IMPLIED action #REQUIRED position (top|bot) #IMPLIED > <!ATTLIST menuitem name #IMPLIED action #REQUIRED position (top|bot) #IMPLIED > <!ATTLIST toolitem name #IMPLIED action #REQUIRED position (top|bot) #IMPLIED > <!ATTLIST accelerator name #IMPLIED action #REQUIRED >
There are some additional restrictions beyond those specified in the DTD, e.g. every toolitem must have a toolbar in its ancestry and every menuitem must have a menubar or popup in its ancestry. Since a GMarkup parser is used to parse the UI description, it must not only be valid XML, but valid GMarkup. If a name is not specified, it defaults to the action. If an action is not specified either, the element name is used.
Example 5. A UI definition
<ui> <menubar> <menu name="FileMenu" action="FileMenuAction"> <menuitem name="New" action="New2Action" /> <placeholder name="FileMenuAdditions" /> </menu> <menu name="JustifyMenu" action="JustifyMenuAction"> <menuitem name="Left" action="justify-left"/> <menuitem name="Centre" action="justify-center"/> <menuitem name="Right" action="justify-right"/> <menuitem name="Fill" action="justify-fill"/> </menu> </menubar> <toolbar action="toolbar1"> <placeholder name="JustifyToolItems"> <separator/> <toolitem name="Left" action="justify-left"/> <toolitem name="Centre" action="justify-center"/> <toolitem name="Right" action="justify-right"/> <toolitem name="Fill" action="justify-fill"/> <separator/> </placeholder> </toolbar> </ui>
The constructed widget hierarchy is very similar to the element tree of the XML, with the exception that placeholders are merged into their parents. The correspondence of XML elements to widgets should be almost obvious:
menubar | a gtk.MenuBar |
toolbar | a gtk.Toolbar |
popup | a toplevel gtk.Menu |
menu | a gtk.Menu
attached to a menuitem |
menuitem | a gtk.MenuItem
subclass, the exact type depends on the action |
toolitem | a gtk.ToolItem
subclass, the exact type depends on the action |
separator | a gtk.SeparatorMenuItem
or gtk.SeparatorToolItem |
accelerator | a keyboard accelerator |
The "position" attribute determines where a constructed widget is positioned with respect to its siblings in the partially constructed tree. If it is "top", the widget is prepended, otherwise it is appended.
The most remarkable feature of gtk.UIManager
is
that it can overlay a set of menuitems and toolitems over another one, and
demerge them later.
Merging is done based on the name of the XML elements. Each
element is identified by a path which consists of the names of its
ancestors, separated by slashes. For example, the menuitem named "Left" in
the example above has the path
/ui/menubar/JustifyMenu/Left
and the toolitem with the
same name has path
/ui/toolbar1/JustifyToolItems/Left
.
Every action has an accelerator path. Accelerators are installed together with menuitem proxies, but they can also be explicitly added with <accelerator> elements in the UI definition. This makes it possible to have accelerators for actions even if they have no visible proxies.
The separators created by gtk.UIManager
are
"smart", i.e. they do not show up in the UI unless they end up between two
visible menu or tool items. Separators which are located at the very
beginning or end of the menu or toolbar containing them, or multiple
separators next to each other, are hidden. This is a useful feature, since
the merging of UI elements from multiple sources can make it hard or
impossible to determine in advance whether a separator will end up in such
an unfortunate position.
Submenus pose similar problems to separators in connection with
merging. It is impossible to know in advance whether they will end up empty
after merging. gtk.UIManager
offers two ways to treat empty submenus:
The behavior is chosen based on the "is_important" property of the action to which the submenu is associated.
gtk.UIManager()
Returns : | a new ui manager object. |
This constructor is available in PyGTK 2.4 and above.
Creates a new gtk.UIManager
object.
def set_add_tearoffs(add_tearoffs
)
| True if tearoff menu items
are added to regular menus |
This method is available in PyGTK 2.4 and above.
The add_tearoffs
() method sets the
"add_tearoffs" property to the value of
add_tearoffs
. If add_tearoffs
is True
regular menus generated by this gtk.UIManager
will have tearoff menu items.
Note that this only affects regular menus. Generated popup menus never have tearoff menu items.
def get_add_tearoffs()
Returns : | True if tearoff menu items
are added |
This method is available in PyGTK 2.4 and above.
The get_add_tearoffs
() method returns
the value of the "add-tearoffs" property. If "add-tearoffs" is
True
regular menus generated will have tearoff menu
items.
def insert_action_group(action_group
, pos
=-1)
| the action group to be inserted |
| the position at which the group will be
inserted. If pos is negative
action_group is inserted at the end of the
list. |
This method is available in PyGTK 2.4 and above.
pos
has default value since PyGTK 2.12.
The insert_action_group
() method
inserts the gtk.ActionGroup
specified by action_group
into the list of associated
action groups at the position specified by
pos
. If pos
is negative, new
action group is inserted at the end of the list. Actions in earlier groups
hide actions with the same name in later groups.
def remove_action_group(action_group
)
| the action group to be removed |
This method is available in PyGTK 2.4 and above.
The remove_action_group
() method
removes the gtk.ActionGroup
specified by action_group
from the list of
associated action groups.
def get_action_groups()
Returns : | a list of associated action groups. |
This method is available in PyGTK 2.4 and above.
The get_action_groups
() method returns
the list of associated gtk.ActionGroup
objects.
def get_accel_group()
Returns : | the gtk.AccelGroup . |
This method is available in PyGTK 2.4 and above.
The get_accel_group
() method returns
the associated gtk.AccelGroup
.
def get_widget(path
)
| a path |
Returns : | the widget found by following the path, or
None if no widget was
found. |
This method is available in PyGTK 2.4 and above.
The get_widget
() method looks up a
widget by following the path specified by path
. The
path consists of the names specified in the XML description of the UI.
separated by '/'. Elements that don't have a name or action attribute in
the XML (e.g. <popup>) can be addressed by their XML element name
(e.g. "popup"). The root element ("/ui") can be omitted in the path.
def get_toplevels(types
)
| specifies the types of toplevel widgets to
include. Allowed types are
gtk.UI_MANAGER_MENUBAR ,
gtk.UI_MANAGER_TOOLBAR and
gtk.UI_MANAGER_POPUP . |
Returns : | a list of all toplevel widgets of the requested types. |
This method is available in PyGTK 2.4 and above.
The get_toplevels
() method returns a
list of all toplevel widgets of the types specified by
types
.
def get_action(path
)
| a path |
Returns : | the action whose proxy widget is found by
following the path, or None if no widget was
found. |
This method is available in PyGTK 2.4 and above.
The get_action
() method looks up a
gtk.Action
by
following a path. See the get_widget()
method for more information about paths.
def add_ui_from_string(buffer
)
| the string to parse |
Returns : | The merge id for the merged UI. The merge id
can be used to unmerge the UI with the remove_ui()
method. |
This method is available in PyGTK 2.4 and above.
The add_ui_from_string
() method parses
the string specified by buffer
that contains a UI definition and merges it with the current
contents of the ui manager. An enclosing <ui> element is added if it
is missing.
This method raise the GError exception if an error occurs during the parsing of the string.
def add_ui_from_file(filename
)
| the name of the file to parse |
Returns : | The merge id for the merged UI. The merge id
can be used to unmerge the UI with the gtk.UIManager.remove_ui()
method. |
This method is available in PyGTK 2.4 and above.
The add_ui_from_file
() method parses
the file specified by filename
that contains a UI definition and merges it with the current
contents of the ui manager.
This method raise the GError exception if an error occurs during the parsing of the file.
def add_ui(merge_id
, path
, name
, action
, type
, top
)
| the merge id for the merged UI, see new_merge_id() |
| a path where the element should be added |
| the name for the added UI element |
| the name of the action to be proxied, or
None to add a separator |
| the type of UI element to add. |
| if True , the UI element is
added before its siblings, otherwise it is added after its
siblings. |
This method is available in PyGTK 2.4 and above.
The add_ui
() method adds a UI element
of the type specified by type
to the current contents
of the ui manager at the location specified by
path
. Note that path must not start with "/ui" though
"ui" is acceptable. For example "/menubar" or "ui/menubar" is acceptable but
"/ui/menubar" is not. type
must be one of:
| The type of the UI element (menuitem, toolitem or separator) is set according to the context. |
| A menubar. |
| A menu. |
| A toolbar. |
| A placeholder. |
| A popup menu. |
| A menuitem. |
| A toolitem. |
| A separator. |
| An accelerator. |
def remove_ui(merge_id
)
| a merge id |
This method is available in PyGTK 2.4 and above.
The remove_ui
() method unmerges the
part of the ui manager content identified by
merge_id
.
def get_ui()
Returns : | A string containing an XML representation of the merged UI. |
This method is available in PyGTK 2.4 and above.
The get_ui
() method creates a UI definition of the merged UI.
def ensure_update()
This method is available in PyGTK 2.4 and above.
The ensure_update
() method makes sure
that all pending updates to the UI have been completed. This may
occasionally be necessary, since gtk.UIManager
updates the UI in an idle function. A typical example where this method is
useful is to enforce that the menubar and toolbar have been added to the
main window before showing it:
window.add(vbox) merge.connect("add_widget", add_widget, vbox) merge.add_ui_from_file("my-menus") merge.add_ui_from_file("my-toolbars") merge.ensure_update() window.show()
def new_merge_id()
Returns : | an unused merge id. |
This method is available in PyGTK 2.4 and above.
The new_merge_id
() method returns an
unused merge id, suitable for use with the add_ui()
method. The returned merge ids are monotonically increasing integer
values.
def callback(uimanager
, user_param1
, ...
)
| the uimanager that received the signal |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "actions-changed" signal is emitted when the set of actions changes.
def callback(uimanager
, widget
, user_param1
, ...
)
| the uimanager that received the signal |
| the added widget |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "add-widget" signal is emitted for each generated menubar
and toolbar. The added widget is specified by
widget
. It is not emitted for generated popup menus,
which can be retrieved by the get_widget
()
method.
def callback(uimanager
, action
, widget
, user_param1
, ...
)
| the uimanager that received the signal |
| the action |
| the proxy widget |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "connect-proxy" signal is emitted after connecting the proxy
widget specified by widget
to the gtk.Action
specified
by action
in the group. This is intended for simple
customizations for which a custom action class would be too clumsy,
e.g. showing tooltips for menuitems in the statusbar.
def callback(uimanager
, action
, widget
, user_param1
, ...
)
| the uimanager that received the signal |
| the action |
| the proxy widget |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "disconnect-proxy" signal is emitted when
widget
is disconnected from
action
.
def callback(uimanager
, action
, user_param1
, ...
)
| the uimanager that received the signal |
| the action |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "post-activate" signal is emitted after
action
is activated. This signal is intended for
applications to get notification after any action is activated.
def callback(uimanager
, action
, user_param1
, ...
)
| the uimanager that received the signal |
| the action |
| the first user parameter (if any) specified
with the connect () |
| additional user parameters (if any) |
This signal is available in GTK+ 2.4 and above.
The "pre-activate" signal is emitted before
action
is activated. This signal is intended for
applications to get notification before any action is activated.