Debian Codegraph @

gtk/src/uimanager.hg

raw
1 /* Copyright (C) 2003 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 // To exclude this file from the documentation when build-deprecated-api=false.
18 _CONFIGINCLUDE(gtkmmconfig.h)
19
20 #include <vector>
21 #include <gtkmm/widget.h>
22 #include <gtkmm/action.h>
23 #include <gtkmm/actiongroup.h>
24
25 _DEFS(gtkmm,gtk)
26 _PINCLUDE(glibmm/private/object_p.h)
27
28 #m4 _PUSH(SECTION_CC_PRE_INCLUDES)
29 #undef GTK_DISABLE_DEPRECATED
30 #define GDK_DISABLE_DEPRECATION_WARNINGS 1
31 #m4 _POP()
32
33 _IS_DEPRECATED // This whole file is deprecated.
34
35 namespace Gtk
36 {
37
38 _WRAP_ENUM(UIManagerItemType, GtkUIManagerItemType, deprecated "Because Gtk::UIManager is deprecated.", decl_prefix GTKMM_API)
39
40 /** Constructing menus and toolbars from an XML description.
41 *
42 * A Gtk::UIManager constructs a user interface (menus and toolbars) from one or more UI definitions,
43 * which reference actions from one or more action groups.
44 *
45 * \par UI Definitions
46 *
47 * The UI definitions are specified in an XML format which can be roughly described by the following DTD.
48 * \code
49 * <!ELEMENT ui (menubar|toolbar|popup|accelerator)* >
50 * <!ELEMENT menubar (menuitem|separator|placeholder|menu)* >
51 * <!ELEMENT menu (menuitem|separator|placeholder|menu)* >
52 * <!ELEMENT popup (menuitem|separator|placeholder|menu)* >
53 * <!ELEMENT toolbar (toolitem|separator|placeholder)* >
54 * <!ELEMENT placeholder (menuitem|toolitem|separator|placeholder|menu)* >
55 * <!ELEMENT menuitem EMPTY >
56 * <!ELEMENT toolitem (menu?) >
57 * <!ELEMENT separator EMPTY >
58 * <!ELEMENT accelerator EMPTY >
59 * <!ATTLIST menubar name #IMPLIED
60 * action #IMPLIED >
61 * <!ATTLIST toolbar name #IMPLIED
62 * action #IMPLIED >
63 * <!ATTLIST popup name #IMPLIED
64 * action #IMPLIED >
65 * <!ATTLIST placeholder name #IMPLIED
66 * action #IMPLIED >
67 * <!ATTLIST separator name #IMPLIED
68 * action #IMPLIED
69 * expand (true|false) #IMPLIED >
70 * <!ATTLIST menu name #IMPLIED
71 * action #REQUIRED
72 * position (top|bot) #IMPLIED >
73 * <!ATTLIST menuitem name #IMPLIED
74 * action #REQUIRED
75 * position (top|bot) #IMPLIED >
76 * <!ATTLIST toolitem name #IMPLIED
77 * action #REQUIRED
78 * position (top|bot) #IMPLIED >
79 * <!ATTLIST accelerator name #IMPLIED
80 * action #REQUIRED >
81 * \endcode
82 *
83 * There are some additional restrictions beyond those specified in the DTD, e.g.
84 * every toolitem must have a toolbar in its anchestry and every menuitem must have a +
85 * menubar or popup in its anchestry. Since a GMarkup parser is used to parse the UI description,
86 * it must not only be valid XML, but valid GMarkup.
87 *
88 * If a name is not specified, it defaults to the action. If an action is not specified either,
89 * the element name is used. The name and action attributes must not contain '/' characters after
90 * parsing (since that would mess up path lookup) and must be usable as XML attributes when
91 * enclosed in doublequotes, thus they must not '"' characters or references to the &quot; entity.
92 *
93 * \par Example: UI Definition
94 * \code
95 * <ui>
96 * <menubar>
97 * <menu name="FileMenu" action="FileMenuAction">
98 * <menuitem name="New" action="New2Action" />
99 * <placeholder name="FileMenuAdditions" />
100 * </menu>
101 * <menu name="JustifyMenu" action="JustifyMenuAction">
102 * <menuitem name="Left" action="justify-left"/>
103 * <menuitem name="Centre" action="justify-center"/>
104 * <menuitem name="Right" action="justify-right"/>
105 * <menuitem name="Fill" action="justify-fill"/>
106 * </menu>
107 * </menubar>
108 * <toolbar action="toolbar1">
109 * <placeholder name="JustifyToolItems">
110 * <separator/>
111 * <toolitem name="Left" action="justify-left"/>
112 * <toolitem name="Centre" action="justify-center"/>
113 * <toolitem name="Right" action="justify-right"/>
114 * <toolitem name="Fill" action="justify-fill"/>
115 * <separator/>
116 * </placeholder>
117 * </toolbar>
118 * </ui>
119 * \endcode
120 *
121 * The constructed widget hierarchy is very similar to the element tree of the XML,
122 * with the exception that placeholders are merged into their parents. The correspondence
123 * of XML elements to widgets should be almost obvious:
124 * - menubar a Gtk::MenuBar
125 * - toolbar a Gtk::Toolbar
126 * - popup a toplevel Gtk::Menu
127 * - menu a Gtk::Menu attached to a menuitem
128 * - menuitem a Gtk::MenuItem subclass, the exact type depends on the action
129 * - toolitem a Gtk::ToolItem subclass, the exact type depends on the action.
130 * Note that toolitem elements may contain a menu element, but only if their
131 * associated action specifies a Gtk::MenuToolButton as proxy.
132 * - separator a Gtk::SeparatorMenuItem or Gtk::SeparatorToolItem
133 * - accelerator a keyboard accelerator
134 *
135 * The "position" attribute determines where a constructed widget is positioned wrt.
136 * to its siblings in the partially constructed tree. If it is "top", the widget is
137 * prepended, otherwise it is appended.
138 *
139 * \par UI Merging
140 *
141 * The most remarkable feature of Gtk::UIManager is that it can overlay a set of menuitems
142 * and toolitems over another one, and demerge them later.
143 *
144 * Merging is done based on the names of the XML elements. Each element is identified by
145 * a path which consists of the names of its anchestors, separated by slashes. For example,
146 * the menuitem named "Left" in the example above has the path /ui/menubar/JustifyMenu/Left
147 * and the toolitem with the same name has path /ui/toolbar1/JustifyToolItems/Left.
148 *
149 * \par Accelerators
150 *
151 * Every action has an accelerator path. Accelerators are installed together with menuitem
152 * proxies, but they can also be explicitly added with \<accelerator\> elements in the
153 * UI definition. This makes it possible to have accelerators for actions even
154 * if they have no visible proxies.
155 *
156 * \par Smart Separators
157 *
158 * The separators created by Gtk::UIManager are "smart", i.e. they do not show up in
159 * the UI unless they end up between two visible menu or tool items. Separators which are
160 * located at the very beginning or end of the menu or toolbar containing them, or multiple
161 * separators next to each other, are hidden. This is a useful feature, since the merging
162 * of UI elements from multiple sources can make it hard or impossible to determine in
163 * advance whether a separator will end up in such an unfortunate position.
164 *
165 * For separators in toolbars, you can set expand="true" to turn them from a small,
166 * visible separator to an expanding, invisible one. Toolitems following an expanding
167 * separator are effectively right-aligned.
168 *
169 * \par Empty Menus
170 *
171 * Submenus pose similar problems to separators inconnection with merging. It is impossible
172 * to know in advance whether they will end up empty after merging. Gtk::UIManager offers
173 * two ways to treat empty submenus:
174 *
175 * - make them disappear by hiding the menu item they're attached to
176 * - add an insensitive "Empty" item
177 *
178 * The behaviour is chosen based on the "hide_if_empty" property of the action to which the submenu is associated.
179 *
180 * @newin{2,4}
181 *
182 * @deprecated Use Gtk::Builder instead.<br>
183 * The menus and toolbars examples in the
184 * <a href="https://gnome.pages.gitlab.gnome.org/gtkmm-documentation/">Programming with gtkmm</a>
185 * book show how to construct menus and toolbars with Gtk::Builder.
186 * The online version of this tutorial describes gtkmm-4.0.
187 */
188 class GTKMM_API UIManager : public Glib::Object
189 {
190 _CLASS_GOBJECT(UIManager, GtkUIManager, GTK_UI_MANAGER, Glib::Object, GObject, , , GTKMM_API)
191 protected:
192 _CTOR_DEFAULT()
193
194 public:
195 _WRAP_CREATE()
196
197 _WRAP_METHOD(void set_add_tearoffs(bool add_tearoffs = true), gtk_ui_manager_set_add_tearoffs, deprecated "Tearoff menus are deprecated and should not be used in newly written code.")
198 _WRAP_METHOD(bool get_add_tearoffs() const, gtk_ui_manager_get_add_tearoffs, deprecated "Tearoff menus are deprecated and should not be used in newly written code.")
199 _WRAP_METHOD(void insert_action_group(const Glib::RefPtr<ActionGroup>& action_group, int pos = 0), gtk_ui_manager_insert_action_group,
200 deprecated "Use Gio::ActionGroup and Gtk::Widget::insert_action_group() instead.")
201 _WRAP_METHOD(void remove_action_group(const Glib::RefPtr<ActionGroup>& action_group), gtk_ui_manager_remove_action_group,
202 deprecated "Use Gio::ActionGroup and Gtk::Widget::remove_action_group() instead.")
203
204 #m4 dnl // Override the conversion in convert_gtk.m4, because the default behaviour is wrong for this method:
205 #m4 _CONVERSION(`GList*', `std::vector< Glib::RefPtr<ActionGroup> >', `Glib::ListHandler<Glib::RefPtr<ActionGroup> >::list_to_vector($3, Glib::OWNERSHIP_NONE)')
206 _WRAP_METHOD(std::vector< Glib::RefPtr<ActionGroup> > get_action_groups(), gtk_ui_manager_get_action_groups,
207 deprecated "Use Gtk::Widget::list_action_prefixes() and Gtk::Widget::get_action_group() instead.")
208 #m4 _CONVERSION(`GList*', `std::vector< Glib::RefPtr<const ActionGroup> >', `Glib::ListHandler<Glib::RefPtr<const ActionGroup> >::list_to_vector($3, Glib::OWNERSHIP_NONE)')
209 _WRAP_METHOD(std::vector< Glib::RefPtr<const ActionGroup> > get_action_groups() const, gtk_ui_manager_get_action_groups,
210 deprecated "Use Gtk::Widget::list_action_prefixes() and Gtk::Widget::get_action_group() instead.")
211
212 _WRAP_METHOD(Glib::RefPtr<AccelGroup> get_accel_group(), gtk_ui_manager_get_accel_group, refreturn,
213 deprecated "Use Gio::ActionGroup and the accelerator group on an associated Gtk::Menu instead.")
214 _WRAP_METHOD(Glib::RefPtr<const AccelGroup> get_accel_group() const, gtk_ui_manager_get_accel_group, refreturn, constversion,
215 deprecated "Use Gio::ActionGroup and the accelerator group on an associated Gtk::Menu instead.")
216 _WRAP_METHOD(Widget* get_widget(const Glib::ustring& path), gtk_ui_manager_get_widget,
217 deprecated "Use Gtk::Builder::get_widget() instead.")
218 _WRAP_METHOD(const Widget* get_widget(const Glib::ustring& path) const, gtk_ui_manager_get_widget, constversion,
219 deprecated "Use Gtk::Builder::get_widget() instead.")
220
221 #m4 _CONVERSION(`GSList*',`std::vector<Widget*>',`Glib::SListHandler<Widget*>::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
222 _WRAP_METHOD(std::vector<Widget*> get_toplevels(UIManagerItemType types), gtk_ui_manager_get_toplevels,
223 deprecated "There is no corresponding replacement when using Gtk::Builder.")
224 #m4 _CONVERSION(`GSList*',`std::vector<const Widget*>',`Glib::SListHandler<const Widget*>::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
225 _WRAP_METHOD(std::vector<const Widget*> get_toplevels(UIManagerItemType types) const, gtk_ui_manager_get_toplevels,
226 deprecated "There is no corresponding replacement when using Gtk::Builder.")
227
228 _WRAP_METHOD(Glib::RefPtr<Action> get_action(const Glib::ustring& path), gtk_ui_manager_get_action, refreturn,
229 deprecated "There is no corresponding replacement when using Gtk::Builder.")
230 _WRAP_METHOD(Glib::RefPtr<const Action> get_action(const Glib::ustring& path) const, gtk_ui_manager_get_action, refreturn, constversion,
231 deprecated "There is no corresponding replacement when using Gtk::Builder.")
232
233 typedef guint ui_merge_id;
234
235 _DEPRECATE_IFDEF_START
236 /** Parses a string containing a UI definition and
237 * merges it with the current contents. An enclosing &lt;ui&gt;
238 * element is added if it is missing.
239 *
240 * @param buffer the string to parse
241 * @result The merge id for the merged UI. The merge id can be used to unmerge the UI with remove_ui(). If an error occurred, the return value is 0.
242 * @throws exception
243 * @newin{2,4}
244 * @deprecated Use Gtk::Builder::add_from_string() instead.
245 */
246 ui_merge_id add_ui_from_string(const Glib::ustring& buffer);
247 _DEPRECATE_IFDEF_END
248 _IGNORE(gtk_ui_manager_add_ui_from_string)
249 _WRAP_METHOD(ui_merge_id add_ui_from_file(const Glib::ustring& filename), gtk_ui_manager_add_ui_from_file, errthrow,
250 deprecated "Use Gtk::Builder::add_from_file() instead.")
251 _WRAP_METHOD(ui_merge_id add_ui_from_resource(const Glib::ustring& resource_path), gtk_ui_manager_add_ui_from_resource, errthrow,
252 deprecated "Use Gtk::Builder::add_from_resource() instead.")
253
254 //TODO: Is top=true a good default?
255
256 _WRAP_METHOD(void add_ui(ui_merge_id merge_id, const Glib::ustring& path, const Glib::ustring& name, const Glib::ustring& action, UIManagerItemType type = Gtk::UI_MANAGER_AUTO, bool top = true), gtk_ui_manager_add_ui,
257 deprecated "There is no corresponding replacement when using Gtk::Builder.")
258
259 _DEPRECATE_IFDEF_START
260 /** Adds a separator UI element to the current contents.
261 *
262 * If @a type is Gtk::UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or
263 * separator if such an element can be inserted at the place determined by
264 * @a path . Otherwise @a type must indicate an element that can be inserted at
265 * the place determined by @a path.
266 *
267 * @see add_ui().
268 *
269 * @newin{2,4}
270 *
271 * @param merge_id The merge id for the merged UI, see gtk_ui_manager_new_merge_id().
272 * @param path A path.
273 * @param name The name for the added UI element.
274 * @param type The type of UI element to add.
275 * @param top If <tt>true</tt>, the UI element is added before its siblings, otherwise it
276 * is added after its siblings.
277 * @deprecated There is no corresponding replacement when using Gtk::Builder.
278 */
279 void add_ui_separator(ui_merge_id merge_id, const Glib::ustring& path, const Glib::ustring& name = "", UIManagerItemType type = Gtk::UI_MANAGER_AUTO, bool top = true);
280 _DEPRECATE_IFDEF_END
281
282 _WRAP_METHOD(void remove_ui(ui_merge_id merge_id), gtk_ui_manager_remove_ui, deprecated "There is no corresponding replacement when using Gtk::Builder.")
283 _WRAP_METHOD(Glib::ustring get_ui() const, gtk_ui_manager_get_ui, deprecated "There is no corresponding replacement when using Gtk::Builder.")
284 _WRAP_METHOD(void ensure_update(), gtk_ui_manager_ensure_update, deprecated "There is no corresponding replacement when using Gtk::Builder.")
285 _WRAP_METHOD(ui_merge_id new_merge_id(), gtk_ui_manager_new_merge_id, deprecated "There is no corresponding replacement when using Gtk::Builder.")
286
287 _WRAP_SIGNAL(void add_widget(Widget* widget), add_widget, deprecated "There is no corresponding replacement when using Gtk::Builder.")
288 _WRAP_SIGNAL(void actions_changed(), actions_changed, deprecated "There is no corresponding replacement when using Gtk::Builder.")
289
290 #m4 _CONVERSION(`GtkAction*',`const Glib::RefPtr<Action>&',`Glib::wrap($3, true)')
291 _WRAP_SIGNAL(void connect_proxy(const Glib::RefPtr<Action>& action, Widget* widget), connect_proxy, deprecated "There is no corresponding replacement when using Gtk::Builder.")
292 _WRAP_SIGNAL(void disconnect_proxy(const Glib::RefPtr<Action>& action, Widget* widget), disconnect_proxy, deprecated "There is no corresponding replacement when using Gtk::Builder.")
293 _WRAP_SIGNAL(void pre_activate(const Glib::RefPtr<Action>& action), pre_activate, deprecated "There is no corresponding replacement when using Gtk::Builder.")
294 _WRAP_SIGNAL(void post_activate(const Glib::RefPtr<Action>& action), post_activate, deprecated "There is no corresponding replacement when using Gtk::Builder.")
295
296 _WRAP_PROPERTY("add_tearoffs", bool, deprecated "Tearoff menus are deprecated and should not be used in newly written code.")
297 _WRAP_PROPERTY("ui", Glib::ustring, deprecated "There is no corresponding replacement when using Gtk::Builder.")
298 };
299
300 } // namespace Gtk