Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Xfc::Gtk::UIManager Class Reference

A GtkUIManager C++ wrapper class. More...

#include <xfc/gtk/uimanager.hh>

Inheritance diagram for Xfc::Gtk::UIManager:

Xfc::G::Object Xfc::G::TypeInstance Xfc::Trackable List of all members.

Signal Prototypes

Public Member Functions

Constructors
Accessors
Methods
Signal Proxies

Protected Member Functions

Constructors

Detailed Description

A GtkUIManager C++ wrapper class.

A UIManager constructs a user interface (menus and toolbars) from one or more XML UI definitions, which reference actions from one or more action groups.

UI Definitions

The UI definitions are specified in an XML format which can be roughly described by the following DTD.

    &lt;!ELEMENT ui          (menubar|toolbar|popup|accelerator)* &gt;
    &lt;!ELEMENT menubar     (menuitem|separator|placeholder|menu)* &gt;
    &lt;!ELEMENT menu        (menuitem|separator|placeholder|menu)* &gt;
    &lt;!ELEMENT popup       (menuitem|separator|placeholder|menu)* &gt;
    
    &lt;!ELEMENT toolbar     (toolitem|separator|placeholder)* &gt;
    &lt;!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* &gt;
    &lt;!ELEMENT menuitem     EMPTY &gt;
    &lt;!ELEMENT toolitem     EMPTY &gt;
    &lt;!ELEMENT separator    EMPTY &gt;
    &lt;!ELEMENT accelerator  EMPTY &gt;
    &lt;!ATTLIST menubar      name               #IMPLIED &gt;
    &lt;!ATTLIST toolbar      name               #IMPLIED &gt;
    &lt;!ATTLIST popup        name               #IMPLIED &gt;
    
    &lt;!ATTLIST placeholder  name               #IMPLIED &gt;
    &lt;!ATTLIST separator    name               #IMPLIED &gt;
    &lt;!ATTLIST menu         name               #IMPLIED
                           action             #REQUIRED
                           position (top|bot) #IMPLIED &gt;
    &lt;!ATTLIST menuitem     name               #IMPLIED
                           action             #REQUIRED
                           position (top|bot) #IMPLIED &gt;
    &lt;!ATTLIST toolitem     name               #IMPLIED
                           action             #REQUIRED
                           position (top|bot) #IMPLIED &gt;
    &lt;!ATTLIST accelerator  name               #IMPLIED
                           action             #REQUIRED &gt;

There are some additional restrictions beyond those specified in the DTD, e.g. every toolitem must have a toolbar in its anchestry and every menuitem must have a menubar or popup in its anchestry. 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:. A UI definition

    &lt;ui&gt;
      &lt;menubar&gt;
        &lt;menu name="FileMenu" action="FileMenuAction"&gt;
          &lt;menuitem name="New" action="New2Action" /&gt;
          &lt;placeholder name="FileMenuAdditions" /&gt;
        &lt;/menu&gt;
        &lt;menu name="JustifyMenu" action="JustifyMenuAction"&gt;
          &lt;menuitem name="Left" action="justify-left"/&gt;
          &lt;menuitem name="Centre" action="justify-center"/&gt;
          &lt;menuitem name="Right" action="justify-right"/&gt;
          &lt;menuitem name="Fill" action="justify-fill"/&gt;
        &lt;/menu&gt;
      &lt;/menubar&gt;
      &lt;toolbar action="toolbar1"&gt;
        &lt;placeholder name="JustifyToolItems"&gt;
          &lt;separator/&gt;
          &lt;toolitem name="Left" action="justify-left"/&gt;
          &lt;toolitem name="Centre" action="justify-center"/&gt;
          &lt;toolitem name="Right" action="justify-right"/&gt;
          &lt;toolitem name="Fill" action="justify-fill"/&gt;
          &lt;separator/&gt;
        &lt;/placeholder&gt;
      &lt;/toolbar&gt;
    &lt;/ui&gt;

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.


UI Merging

The most remarkable feature of 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 names of the XML elements. Each element is identified by a path which consists of the names of its anchestors, 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.


Accelerators

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.


Smart Separators

The separators created by 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.


Empty Menus

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. UIManager offers two ways to treat empty submenus:

The behaviour is chosen based on the "is_important" property of the action to which the submenu is associated.

Note: dynamically allocated objects must either be unreferenced or assigned to a smart pointer. Stack objects are automatically unreferenced when they go out of scope.

See also: the User Interface Manager HOWTO and example.


Constructor & Destructor Documentation

Xfc::Gtk::UIManager::UIManager GtkUIManager *  ui_manager,
bool  owns_reference = true
[explicit, protected]
 

Construct a new UIManager from an existing GtkUIManager.

Parameters:
ui_manager A pointer to a GtkUIManager.
owns_reference Set false if the initial reference count is floating, set true if it's not.
The ui_manager can be a newly created GtkUIManager or an existing GtkUIManager (see G::Object::Object).


Member Function Documentation

void Xfc::Gtk::UIManager::add_ui unsigned int  merge_id,
const String path,
const String name,
const String action,
UIManagerItemType  type = UI_MANAGER_AUTO,
bool  top = false
 

Adds a UI element to the current contents of the UIManager.

Parameters:
merge_id The merge id for the merged UI (see new_merge_id()).
path A path.
name The name for the added UI element.
action The name of the action to be proxied, or null to add a separator.
type The type of UI element to add.
top If true the UI element is added before its siblings, otherwise it's added after.
If type is Gtk::UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or separator if such an element can be inserted at the place determined by path. Otherwise type must indicate an element that can be inserted at the place determined by path. When adding several UI elements you will want to use the same merge_id for each element. This will avoid unecessary multiple calls to new_merge_id() and will make it easier to unmerge several elements with one call to remove_ui();

unsigned int Xfc::Gtk::UIManager::add_ui_from_file const String filename,
G::Error error = 0
 

Parses a file containing a UI definition and merges it with the current contents of the UIManager.

Parameters:
filename The name of the file to parse.
error The return location for an error.
Returns:
The merge id for the merged UI.
An enclosing <ui> element is added if it is missing. The merge id can be used to unmerge the UI with remove_ui(). If an error occurred, the return value is 0.

unsigned int Xfc::Gtk::UIManager::add_ui_from_string const String buffer,
G::Error error = 0
 

Parses a string containing a UI definition and merges it with the current contents of the UIManager.

Parameters:
buffer The string to parse.
error The return location for an error.
Returns:
The merge id for the merged UI.
An enclosing <ui> element is added if it is missing. The merge id can be used to unmerge the UI with remove_ui(). If an error occurred, the return value is 0.

unsigned int Xfc::Gtk::UIManager::add_ui_from_string const char *  buffer,
int  length = -1,
G::Error error = 0
 

Parses a string containing a UI definition and merges it with the current contents of the UIManager.

Parameters:
buffer The string to parse.
length The length of buffer, or -1 if buffer is null-terminated.
error The return location for an error.
Returns:
The merge id for the merged UI.
An enclosing <ui> element is added if it is missing. The merge id can be used to unmerge the UI with remove_ui(). If an error occurred, the return value is 0.

void Xfc::Gtk::UIManager::add_ui_separator unsigned int  merge_id,
const String path,
const String name,
bool  top = false
 

Adds a separator UI element to the current contents of the UIManager.

Parameters:
merge_id The merge id for the merged UI (see new_merge_id()).
path A path.
name The name for the added separator UI element.
top If true the separator is added before its siblings, otherwise it's added after.

void Xfc::Gtk::UIManager::ensure_update  ) 
 

Makes sure that all pending updates to the UI have been completed.

This may occasionally be necessary, since UIManager updates the UI in an idle function. A typical example where this function is useful is to enforce that the menubar and toolbar have been added to the main window before showing it:

             window->add(*vbox); 
             ui_manager->signal_add_widget().connect(sigc::mem_fun(vbox, &MyVBox::on_add_widget)); 
             ui_manager->add_ui_from_file("my-menus");
             ui_manager->add_ui_from_file("my-toolbars");
             ui_manager->ensure_update();  
             window->show();

AccelGroup* Xfc::Gtk::UIManager::get_accel_group  )  const
 

Gets the AccelGroup associated with the UIManager.

Returns:
The AccelGroup.

Action* Xfc::Gtk::UIManager::get_action const char *  path  )  const
 

Looks up an action by following a path (see get_widget() for more information about paths).

Parameters:
path A path.
Returns:
The action whose proxy widget is found by following the path, or null if no widget was found.

bool Xfc::Gtk::UIManager::get_action_groups std::vector< ActionGroup * > &  action_groups  )  const
 

Gets the list of action groups associated with the UIManager.

Parameters:
action_groups A vector of ActionGroup* to hold the action groups.
Returns:
true if any action groups were returned, false if there were none.

bool Xfc::Gtk::UIManager::get_add_tearoffs  )  const
 

Determines whether menus generated by this UIManager will have tearoff menu items.

Returns:
Whether tearoff menu items are added.

bool Xfc::Gtk::UIManager::get_toplevels std::vector< Widget * > &  toplevels,
UIManagerItemTypeField  types
const
 

Obtains a list of all toplevel widgets of the requested types.

Parameters:
toplevels A vector of widget* to hold the list of all toplevel widgets of the requested types.
types Specifies the Gtk::UIManagerItemType of toplevel widgets to include.
The allowed types are Gtk::UI_MANAGER_MENUBAR, Gtk::UI_MANAGER_TOOLBAR and Gtk::UI_MANAGER_POPUP.
Returns:
true if any widgets of the requested types were were returned, false otherwise.

String Xfc::Gtk::UIManager::get_ui  )  const
 

Creates a UI definition of the merged UI.

Returns:
A String containing an XML representation of the merged UI.

Widget* Xfc::Gtk::UIManager::get_widget const String path  )  const
 

Looks up a widget by following a path.

Parameters:
path A path.
Returns:
The widget found by following the path, or null if no widget was found.
The path consists of the names specified in the XML description of the UI separated by '/'. Elements which 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.

Note that the widget found by following a path that ends in a <menu> element is the menuitem to which the menu is attached, not the menu itself.

void Xfc::Gtk::UIManager::insert_action_group ActionGroup action_group,
int  pos = 0
 

Inserts an action group into the list of action groups associated with this UIManager.

Parameters:
action_group The action group to be inserted.
pos The position at which the group will be inserted.
Note actions in earlier groups hide actions with the same name in later groups.

unsigned int Xfc::Gtk::UIManager::new_merge_id  ) 
 

Obtains an unused merge id, suitable for use with add_ui().

Returns:
An unused merge id.

void Xfc::Gtk::UIManager::remove_action_group ActionGroup action_group  ) 
 

Removes an action group from the list of action groups associated with self.

Parameters:
action_group The action group to be removed.

void Xfc::Gtk::UIManager::remove_ui unsigned int  merge_id  ) 
 

Unmerges the part of the UIManager content identified by merge_id.

Parameters:
merge_id A merge id as returned by add_ui_from_string().

void Xfc::Gtk::UIManager::set_add_tearoffs bool  add_tearoffs  ) 
 

Sets whether menus generated by this UIManager will have tearoff menu items.

Parameters:
add_tearoffs Whether tearoff menu items are added.
Note that this only affects regular menus. Generated popup menus never have tearoff menu items.


Member Data Documentation

const ActionsChangedSignalType Xfc::Gtk::UIManager::actions_changed_signal [static, protected]
 

Actions changed signal (see signal_actions_changed()).

Calls a slot with the signature:

             void function();

const AddWidgetSignalType Xfc::Gtk::UIManager::add_widget_signal [static, protected]
 

Add widget signal (see signal_add_widget()).

Calls a slot with the signature:

             void function(Widget& widget);
             // widget: The added widget.

const ConnectProxySignalType Xfc::Gtk::UIManager::connect_proxy_signal [static, protected]
 

Connect proxy signal (see signal_connect_proxy()).

Calls a slot with the signature:

             void function(Action& action, Widget& proxy);
             // action: The action.
             // proxy: The proxy widget. 

const DisconnectProxySignalType Xfc::Gtk::UIManager::disconnect_proxy_signal [static, protected]
 

Disconnect proxy signal (see signal_disconnect_proxy()).

Calls a slot with the signature:

             void function(Action& action, Widget& proxy);
             // action: The action.
             // proxy: The proxy widget. 

const PostActivateSignalType Xfc::Gtk::UIManager::post_activate_signal [static, protected]
 

Post-activate signal (see signal_post_activate()).

Calls a slot with the signature:

             void function(Action& action);
             // action: The action.

const PreActivateSignalType Xfc::Gtk::UIManager::pre_activate_signal [static, protected]
 

Pre-activate signal (see signal_pre_activate()).

Calls a slot with the signature:

             void function(Action& action);
             // action: The action.


The documentation for this class was generated from the following file: Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC 4.3