Archive / / / / / Echo.UI.ActionManager.vala
2008-11-27 23:48:48 UTC
previous next
/* vim: set noexpandtab tabstop=4 shiftwidth=4 nowrap textwidth=100 * * Echo Media Player * Copyright (C) 2008 Shane O'Connell * * [ The original file includes a copyright header in this location describing * the file as being released under the terms of the GNU General Public * License. It has been removed in order to display the file as part of the * archive. ] */ public class Echo.UI.ActionManager : GLib.Object { private Gtk.UIManager _ui_manager; private Gtk.ActionGroup _action_group; public ActionManager() { _action_group = new Gtk.ActionGroup("main-action-group"); _ui_manager = new Gtk.UIManager(); _ui_manager.insert_action_group(_action_group, 0); } // ------------------------------------------[Actions]------------------------------------------ public void add_action( string name, string? label, string? stock_id = null, string? accel = null) { _action_group.add_action_with_accel(new Gtk.Action(name, label, null, stock_id), accel); } public void add_toggle_action( string name, bool active, string? label, string? stock_id = null, string? accel = null) { var action = new Gtk.ToggleAction(name, label, null, stock_id); action.set_active(active); _action_group.add_action_with_accel(action, accel); } public void add_custom_action(Gtk.Action action, string? accel = null) { _action_group.add_action_with_accel(action, accel); } public weak Gtk.Action? get_action(string name) { return _action_group.get_action(name); } // -------------------------------------------[Menus]------------------------------------------- public void add_menubar(string name, string xml) { try { _ui_manager.add_ui_from_string( "<menubar name=\"menubar-%s\">%s</menubar>".printf(name, xml), -1); } catch(GLib.Error e) { error("Error processing menubar xml: %s\n\nXML was:\n%s\n\n", e.message, xml); } } public weak Gtk.MenuBar? get_menubar(string name) { return (Gtk.MenuBar)_ui_manager.get_widget("/menubar-%s".printf(name)); } public void add_menu(string name, string xml) { try { _ui_manager.add_ui_from_string( "<popup name=\"menu-%s\">%s</popup>".printf(name, xml), -1); } catch(GLib.Error e) { error("Error processing menu xml: %s\n\nXML was:\n%s\n\n", e.message, xml); } } public weak Gtk.Menu? get_menu(string name) { return (Gtk.Menu)_ui_manager.get_widget("/menu-%s".printf(name)); } // ------------------------------------------[Widgets]------------------------------------------ public Gtk.ToolButton get_toolbutton_proxy(string action_name) { var button = new Gtk.ToolButton(null, null); var action = get_action(action_name); assert(action != null); action.connect_proxy(button); return button; } public Gtk.MenuToolButton get_menutoolbutton_proxy(string action_name, string menu_name) { var button = new Gtk.MenuToolButton(null, null); var action = get_action(action_name); var menu = get_menu(menu_name); assert(action != null); assert(menu != null); action.connect_proxy(button); button.set_menu(menu); return button; } }