Re: Drop down MenuToolButton



Thanks Kjell for your reply,

the documentation on the gtkmm site is a great reference but I am learning you have to read between the lines. Since Gtkmm is a wrapper around Gtk, I figured out that I should be looking for examples from there in C or python. I found a great explanation about the UI Manager here. I was able to follow along and convert the gtk-perl example over to gtkmm. The code below shows what I did to create a toolbar button that would show a drop-down menu of recent files. I used get_widget to get the Gtk::MenuItem of my recent files sub-menu in the main menubar and then use it to create the same sub-menu for the toolbar drop-down. Here some code:

1. Define you action groups:

    /* File Menu */
    ref_action_group_->add(Gtk::Action::create("FileMenu", "File"));

    // File|New:
    ref_action_group_->add(Gtk::Action::create("FileNew",
                Gtk::Stock::NEW, "_New", "Create a new project"),
                sigc::mem_fun(*this, &MainWindow::onMenuFileNew));
    // File|Open:
    ref_action_group_->add(Gtk::Action::create("FileOpen",
                Gtk::Stock::OPEN, "Open", "Open an existing project"),
                sigc::mem_fun(*this, &MainWindow::onMenuFileOpen));
    // File|Recent Projects:
    ref_action_group_->add(Gtk::Action::create("FileRecent",
                "Recent Projects", "Open a recent project"));
                // Gtk::Stock::OPEN, "Recent Projects", "Open a recent project"),
                // sigc::mem_fun(*this, &MainWindow::onMenuFileRecent));

// Some more action groups here....

2. Create the UIManager, insert the action groups and add the accel group to your window:

    ref_ui_manager_ = Gtk::UIManager::create();
    ref_ui_manager_->insert_action_group(ref_action_group_);

    add_accel_group(ref_ui_manager_->get_accel_group());

3. Create the ui string:

    Glib::ustring ui_info =
        "<ui>"
        "  <menubar name='MenuBar'>"
        "    <menu action="">        "      <menuitem action="">        "       <separator />"
        "      <menuitem action="">         "       <menu action="">        "        <placeholder name='RecentProjects'/>"  // **** This is where my recent file list gets inserted.
        "       </menu>"
        "    </menu>"
        "    <menu action="">        "      <menuitem action="">        "      <menuitem action="">         "      <menuitem action="">        "       <separator />"
        "      <menuitem action="">        "    </menu>"
        "    <menu action="">        "      <menuitem action="">        "      <menuitem action="">         "      <menuitem action="">        "    </menu>"
        "    <menu action="">        "      <menuitem action="">         "       <separator />"
        "      <menuitem action="">        "      <menuitem action="">        "      <menuitem action="">         "      <menuitem action="">        "    </menu>"
        "    <menu action="">        "      <menuitem action="">         "      <menuitem action="">        "      <menuitem action="">        "      <menuitem action="">         "    </menu>"
        "  </menubar>"
        "  <toolbar  name='ToolBar'>"
        "    <toolitem action="">        "    <toolitem action="">         "     <placeholder name='recent_projects'/>" // *** This is where my MenuToolButton is going
        "    <toolitem action="">        "     <separator />"
        "    <toolitem action="">        "     <separator />"
        "    <toolitem action="">        "    <toolitem action="">         "     <separator />"
        "    <toolitem action="">        "  </toolbar>"
        "</ui>";


4. Add the UI string:

    ref_ui_manager_->add_ui_from_string(ui_info);

5. Create the MenuToolButton:

    Gtk::MenuToolButton*  pRecent_menu_tool_btn_ = manage(new Gtk::MenuToolButton("Recent"));

6. Get the menu item from the main menubar:

    Gtk::MenuItem* menu_item = dynamic_cast<Gtk::MenuItem*>(ref_ui_manager_->get_widget("ui/MenuBar/FileMenu/FileRecent"));

7. Use the menu item to set the menu on the toolbar:
    pRecent_menu_tool_btn_->set_menu(*(menu_item->get_submenu()));
    pToolbar->insert(*pRecent_menu_tool_btn_,2);





On Tue, Mar 5, 2013 at 9:42 AM, Kjell Ahlstedt <kjell ahlstedt bredband net> wrote:
Have you read about menus and toolbars in the gtkmm tutorial, chapter 12?
http://developer.gnome.org/gtkmm-tutorial/stable/
It contains an example with UIManager. However there is no example of a menu in a toolbar. I don't think that's possible. The description of Gtk::UIManager at
http://developer.gnome.org/gtkmm/stable/classGtk_1_1UIManager.html#details
says "every menuitem must have a menubar or popup in its ancestry". A toolbar won't do, it seems.

Kjell

2013-03-02 19:45, Steve Holmes skrev:

Hi everyone,

I'm trying to create a drop-down menu on a toolbar using the UIManager and I can't figure it out. The documentation mentions something about specifying a Gtk::MenuToolButton as a proxy for the associated action but I'm not sure how to do that. One thing I thought was that I could manually create it like this:

Gtk::MenuToolButton* menu_button = manage(new Gtk::MenuToolButton());
menu_button->set_related_action(ref_manager_action_group->get_action("MyAction");

and then for the ui string:

<ui>...
...
<toolbar name="Toolbar">
  <toolitem action="">     <menu action="">      <menuitem action="">      <menuitem action="">     </menu>
   </toolitem>
</toolbar>
...
</ui>

Any help is appreciated!

Steve


--
Steve Holmes
M: 416-791-3868





--
Steve Holmes
M: 416-791-3868


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]