diff options
| author | Alexander Valavanis <valavanisalex@gmail.com> | 2017-06-28 13:17:39 +0000 |
|---|---|---|
| committer | Alexander Valavanis <valavanisalex@gmail.com> | 2017-06-28 13:17:39 +0000 |
| commit | d483c2ab23354cf5ea58a2d2225ab464b77f6614 (patch) | |
| tree | 7fcc5c570093c44afdc23eef64a7999cb13d495a /src | |
| parent | Partial fix for menu items and split contextmenu into separate file (diff) | |
| download | inkscape-d483c2ab23354cf5ea58a2d2225ab464b77f6614.tar.gz inkscape-d483c2ab23354cf5ea58a2d2225ab464b77f6614.zip | |
menu-items: C++ify
Diffstat (limited to 'src')
| -rw-r--r-- | src/shortcuts.cpp | 14 | ||||
| -rw-r--r-- | src/shortcuts.h | 1 | ||||
| -rw-r--r-- | src/ui/interface.cpp | 120 | ||||
| -rw-r--r-- | src/ui/interface.h | 9 | ||||
| -rw-r--r-- | src/widgets/desktop-widget.cpp | 13 | ||||
| -rw-r--r-- | src/widgets/desktop-widget.h | 6 |
6 files changed, 93 insertions, 70 deletions
diff --git a/src/shortcuts.cpp b/src/shortcuts.cpp index e74d60abc..f7ce2643d 100644 --- a/src/shortcuts.cpp +++ b/src/shortcuts.cpp @@ -651,18 +651,6 @@ sp_shortcut_unset(unsigned int const shortcut) } } -GtkAccelGroup * -sp_shortcut_get_accel_group() -{ - static GtkAccelGroup *accel_group = NULL; - - if (!accel_group) { - accel_group = gtk_accel_group_new (); - } - - return accel_group; -} - /** * Adds a gtk accelerator to a widget * Used to display the keyboard shortcuts in the main menu items @@ -678,7 +666,7 @@ sp_shortcut_add_accelerator(GtkWidget *item, unsigned int const shortcut) if (accel_key > 0) { gtk_widget_add_accelerator (item, "activate", - sp_shortcut_get_accel_group(), + gtk_accel_group_new(), accel_key, sp_shortcut_get_modifiers(shortcut), GTK_ACCEL_VISIBLE); diff --git a/src/shortcuts.h b/src/shortcuts.h index f24a82603..f73d178d7 100644 --- a/src/shortcuts.h +++ b/src/shortcuts.h @@ -53,7 +53,6 @@ void sp_shortcut_file_export(); bool sp_shortcut_file_import(); void sp_shortcut_file_import_do(char const *importname); void sp_shortcut_file_export_do(char const *exportname); -GtkAccelGroup *sp_shortcut_get_accel_group(); void sp_shortcut_add_accelerator(GtkWidget *item, unsigned int const shortcut); #endif diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp index c2ce9e568..c9c00df02 100644 --- a/src/ui/interface.cpp +++ b/src/ui/interface.cpp @@ -24,7 +24,12 @@ #endif #include "ui/dialog/dialog-manager.h" + #include <gtkmm/icontheme.h> +#include <gtkmm/menubar.h> +#include <gtkmm/radiomenuitem.h> +#include <gtkmm/separatormenuitem.h> + #include "file.h" #include <glibmm/miscutils.h> @@ -438,14 +443,18 @@ sp_ui_dialog_title_string(Inkscape::Verb *verb, gchar *c) * * @see ContextMenu::AppendItemFromVerb for a c++ified alternative. Consider dropping sp_ui_menu_append_item_from_verb when c++ifying interface.cpp. */ -static GtkWidget *sp_ui_menu_append_item_from_verb(GtkMenu *menu, Inkscape::Verb *verb, Inkscape::UI::View::View *view, bool radio = false, GSList *group = NULL) +static Gtk::MenuItem * +sp_ui_menu_append_item_from_verb(Gtk::Menu *menu, + Inkscape::Verb *verb, + Inkscape::UI::View::View *view, + bool radio = false) { - GtkWidget *item; + Gtk::MenuItem *item; // Just create a menu separator if this isn't a real action. // Otherwise, create a real menu item if (verb->get_code() == SP_VERB_NONE) { - item = gtk_separator_menu_item_new(); + item = Gtk::manage(new Gtk::SeparatorMenuItem()); } else { SPAction *action = verb->get_action(Inkscape::ActionContext(view)); @@ -453,76 +462,70 @@ static GtkWidget *sp_ui_menu_append_item_from_verb(GtkMenu *menu, Inkscape::Verb // Create the menu item itself, either as a radio menu item, or just // a regular menu item depending on whether the "radio" flag is set - if (radio) { - item = gtk_radio_menu_item_new(group); - } else { - item = gtk_menu_item_new(); - } + if(radio) item = Gtk::manage(new Gtk::RadioMenuItem()); + else item = Gtk::manage(new Gtk::MenuItem()); // Create a box to contain all the widgets (icon, label, accelerator) // that will go inside the menu item - GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + Gtk::Box *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0)); // Now create the label and add it to the menu item - GtkWidget *label = gtk_accel_label_new(action->name); - gtk_label_set_markup_with_mnemonic( GTK_LABEL(label), action->name); - gtk_label_set_use_underline(GTK_LABEL(label), true); - gtk_label_set_xalign(GTK_LABEL(label), 0.0); + Gtk::AccelLabel *label = Gtk::manage(new Gtk::AccelLabel(action->name)); + label->set_markup_with_mnemonic(action->name); + label->set_use_underline(true); + label->set_xalign(0.0); - GtkAccelGroup *accel_group = sp_shortcut_get_accel_group(); - gtk_menu_set_accel_group(menu, accel_group); + auto accel_group = Gtk::AccelGroup::create(); + menu->set_accel_group(accel_group); - sp_shortcut_add_accelerator(item, sp_shortcut_get_primary(verb)); - gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(label), item); + sp_shortcut_add_accelerator(GTK_WIDGET(item->gobj()), sp_shortcut_get_primary(verb)); + label->set_accel_widget(*item); // If there is an image associated with the action, then we can add it as an // icon for the menu item. If not, give the label a bit more space if(action->image) { - GtkWidget *icon = gtk_image_new_from_icon_name(action->image, GTK_ICON_SIZE_MENU); - gtk_box_pack_start(GTK_BOX(box), icon, FALSE, TRUE, 0); + Gtk::Image *icon = Gtk::manage(new Gtk::Image()); + icon->set_from_icon_name(action->image, Gtk::ICON_SIZE_MENU); + box->pack_start(*icon, false, true, 0); } else { - GtkWidget *fake_icon = gtk_label_new(""); - gtk_widget_set_size_request(label, 16, 16); - gtk_widget_set_hexpand(label, true); - gtk_box_pack_start(GTK_BOX(box), fake_icon, FALSE, TRUE, 16); + Gtk::Widget *fake_icon = Gtk::manage(new Gtk::Label("")); + label->set_size_request(16, 16); + label->set_hexpand(true); + box->pack_start(*fake_icon, false, true, 16); } - gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0); - + box->pack_start(*label, true, true, 0); // Finally, pack all the widgets into the menu item - gtk_container_add(GTK_CONTAINER(item), box); - + item->add(*box); + // Handle signals from the SPAction action->signal_set_sensitive.connect( - sigc::bind<0>( - sigc::ptr_fun(>k_widget_set_sensitive), - item)); + sigc::mem_fun(*item, &Gtk::MenuItem::set_sensitive)); + action->signal_set_name.connect( sigc::bind<0>( sigc::ptr_fun(&sp_ui_menu_item_set_name), - item)); + GTK_WIDGET(item->gobj()))); if (!action->sensitive) { - gtk_widget_set_sensitive(item, FALSE); + item->set_sensitive(false); } - gtk_widget_set_events(item, GDK_KEY_PRESS_MASK); + item->set_events(Gdk::KEY_PRESS_MASK); g_object_set_data(G_OBJECT(item), "view", (gpointer) view); g_signal_connect( G_OBJECT(item), "activate", G_CALLBACK(sp_ui_menu_activate), action ); g_signal_connect( G_OBJECT(item), "select", G_CALLBACK(sp_ui_menu_select_action), action ); g_signal_connect( G_OBJECT(item), "deselect", G_CALLBACK(sp_ui_menu_deselect_action), action ); } - gtk_widget_show_all(item); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + item->show_all(); + menu->append(*item); return item; - } // end of sp_ui_menu_append_item_from_verb - Glib::ustring getLayoutPrefPath( Inkscape::UI::View::View *view ) { Glib::ustring prefPath; @@ -797,7 +800,7 @@ private: /** * This function turns XML into a menu. * - * This function is realitively simple as it just goes through the XML + * This function is relatively simple as it just goes through the XML * and parses the individual elements. In the case of a submenu, it * just calls itself recursively. Because it is only reasonable to have * a couple of submenus, it is unlikely this will go more than two or @@ -811,18 +814,19 @@ private: * @param menu Menu to be added to * @param view The View that this menu is being built for */ -static void sp_ui_build_dyn_menus(Inkscape::XML::Node *menus, GtkWidget *menu, Inkscape::UI::View::View *view) +static void sp_ui_build_dyn_menus(Inkscape::XML::Node *menus, + Gtk::Menu *menu, + Inkscape::UI::View::View *view) { if (menus == NULL) return; if (menu == NULL) return; - GSList *group = NULL; for (Inkscape::XML::Node *menu_pntr = menus; menu_pntr != NULL; menu_pntr = menu_pntr->next()) { if (!strcmp(menu_pntr->name(), "submenu")) { GtkWidget *mitem = gtk_menu_item_new_with_mnemonic(_(menu_pntr->attribute("name"))); - GtkWidget *submenu = gtk_menu_new(); + Gtk::Menu *submenu = Gtk::manage(new Gtk::Menu()); sp_ui_build_dyn_menus(menu_pntr->firstChild(), submenu, view); gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), GTK_WIDGET(submenu)); gtk_menu_shell_append(GTK_MENU_SHELL(menu), mitem); @@ -833,9 +837,23 @@ static void sp_ui_build_dyn_menus(Inkscape::XML::Node *menus, GtkWidget *menu, I Inkscape::Verb *verb = Inkscape::Verb::getbyid(verb_name); if (verb != NULL) { + + // Add a radio item to the menu if (menu_pntr->attribute("radio") != NULL) { - GtkWidget *item = sp_ui_menu_append_item_from_verb (GTK_MENU(menu), verb, view, true, group); - group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item)); + auto item = sp_ui_menu_append_item_from_verb (menu, verb, view, true); + + // If the last item was also a radio item, then we need to add the new item to + // its group. Note that the Inkscape dynamic menu XML files do not support + // explicit grouping yet. Instead, groups consist of ALL adjacent radio menu items + // in the file. + Gtk::RadioMenuItem *last_item = dynamic_cast<Gtk::RadioMenuItem *>(menu->get_children().back()); + + if(last_item != nullptr) { + auto radioitem = dynamic_cast<Gtk::RadioMenuItem *>(item); + radioitem->join_group(*last_item); + } + + if (menu_pntr->attribute("default") != NULL) { gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE); } @@ -850,8 +868,7 @@ static void sp_ui_build_dyn_menus(Inkscape::XML::Node *menus, GtkWidget *menu, I checkitem_toggled, checkitem_update, verb); } } else { - sp_ui_menu_append_item_from_verb(GTK_MENU(menu), verb, view); - group = NULL; + sp_ui_menu_append_item_from_verb(menu, verb, view); } } else { gchar string[120]; @@ -910,11 +927,16 @@ static void sp_ui_build_dyn_menus(Inkscape::XML::Node *menus, GtkWidget *menu, I } } -GtkWidget *sp_ui_main_menubar(Inkscape::UI::View::View *view) +Gtk::MenuBar * +sp_ui_main_menubar(Inkscape::UI::View::View *view) { - GtkWidget *mbar = gtk_menu_bar_new(); - sp_ui_build_dyn_menus(INKSCAPE.get_menus(), mbar, view); - return mbar; + auto menu_bar = Gtk::manage(new Gtk::MenuBar()); + + sp_ui_build_dyn_menus(INKSCAPE.get_menus(), + dynamic_cast<Gtk::Menu *>(menu_bar), + view); + + return menu_bar; } diff --git a/src/ui/interface.h b/src/ui/interface.h index 6e95c161c..6b687aa78 100644 --- a/src/ui/interface.h +++ b/src/ui/interface.h @@ -23,6 +23,10 @@ class SPViewWidget; typedef struct _GtkWidget GtkWidget; +namespace Gtk { +class MenuBar; +} + namespace Inkscape { class Verb; @@ -30,9 +34,12 @@ namespace UI { namespace View { class View; } // namespace View + + } // namespace UI } // namespace Inkscape + /** * Create a new document window. */ @@ -70,7 +77,7 @@ unsigned int sp_ui_close_all (void); * * @param view View to build the bar for */ -GtkWidget *sp_ui_main_menubar (Inkscape::UI::View::View *view); +Gtk::MenuBar * sp_ui_main_menubar(Inkscape::UI::View::View *view); void sp_menu_append_recent_documents (GtkWidget *menu); void sp_ui_dialog_title_string (Inkscape::Verb * verb, char* c); diff --git a/src/widgets/desktop-widget.cpp b/src/widgets/desktop-widget.cpp index ce4c5936c..fa7e680c6 100644 --- a/src/widgets/desktop-widget.cpp +++ b/src/widgets/desktop-widget.cpp @@ -66,6 +66,7 @@ #include <gtkmm/cssprovider.h> #include <gtkmm/paned.h> #include <gtkmm/messagedialog.h> +#include <gtkmm/menubar.h> #include "inkscape-version.h" using Inkscape::UI::Widget::UnitTracker; @@ -1439,9 +1440,9 @@ void SPDesktopWidget::layoutWidgets() } if (!prefs->getBool(pref_root + "menu/state", true)) { - gtk_widget_hide (dtw->menubar); + dtw->menubar->hide(); } else { - gtk_widget_show_all (dtw->menubar); + dtw->menubar->show_all(); } if (!prefs->getBool(pref_root + "commands/state", true)) { @@ -1647,10 +1648,12 @@ SPDesktopWidget* SPDesktopWidget::createInstance(SPNamedView *namedview) dtw->layer_selector->setDesktop(dtw->desktop); dtw->menubar = sp_ui_main_menubar (dtw->desktop); - gtk_widget_set_name(dtw->menubar, "MenuBar"); - gtk_widget_show_all (dtw->menubar); + dtw->menubar->set_name("MenuBar"); + dtw->menubar->show_all(); SPNamedView *nv = dtw->desktop->namedview; - gtk_box_pack_start (GTK_BOX (dtw->vbox), dtw->menubar, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (dtw->vbox), + GTK_WIDGET(dtw->menubar->gobj()), + FALSE, FALSE, 0); dtw->layoutWidgets(); std::vector<GtkWidget *> toolboxes; diff --git a/src/widgets/desktop-widget.h b/src/widgets/desktop-widget.h index 0d5f40987..1aaa20afe 100644 --- a/src/widgets/desktop-widget.h +++ b/src/widgets/desktop-widget.h @@ -28,6 +28,9 @@ class SPDesktop; struct SPDesktopWidget; class SPObject; +namespace Gtk { +class MenuBar; +} #define SP_TYPE_DESKTOP_WIDGET SPDesktopWidget::getType() #define SP_DESKTOP_WIDGET(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), SP_TYPE_DESKTOP_WIDGET, SPDesktopWidget)) @@ -79,7 +82,8 @@ struct SPDesktopWidget { GtkWidget *hbox; - GtkWidget *menubar, *statusbar; + Gtk::MenuBar *menubar; + GtkWidget *statusbar; Inkscape::UI::Dialogs::SwatchesPanel *panels; |
