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<http://gtk2-perl.sourceforge.net/doc/gtk2-perl-study-guide/c4973.html>.
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='FileMenu'>"
        "      <menuitem action='FileNew'/>"
        "       <separator />"
        "      <menuitem action='FileOpen'/>"
        "       <menu action='FileRecent'>"
        "        <placeholder name='RecentProjects'/>"  // **** This is
where my recent file list gets inserted.
        "       </menu>"
        "    </menu>"
        "    <menu action='EditMenu'>"
        "      <menuitem action='EditCut'/>"
        "      <menuitem action='EditCopy'/>"
        "      <menuitem action='EditPaste'/>"
        "       <separator />"
        "      <menuitem action='EditPreferences'/>"
        "    </menu>"
        "    <menu action='ViewMenu'>"
        "      <menuitem action='ViewReloadProject'/>"
        "      <menuitem action='ViewMessageWindow'/>"
        "      <menuitem action='ViewStatusbar'/>"
        "    </menu>"
        "    <menu action='ToolsMenu'>"
        "      <menuitem action='ToolsReloadConfig'/>"
        "       <separator />"
        "      <menuitem action='ToolsTagMonitor'/>"
        "      <menuitem action='ToolsDownload'/>"
        "      <menuitem action='ToolsBackup'/>"
        "      <menuitem action='ToolsDbAdmin'/>"
        "    </menu>"
        "    <menu action='HelpMenu'>"
        "      <menuitem action='HelpHelp'/>"
        "      <menuitem action='HelpWebsite'/>"
        "      <menuitem action='HelpShortCuts'/>"
        "      <menuitem action='HelpAbout'/>"
        "    </menu>"
        "  </menubar>"
        "  <toolbar  name='ToolBar'>"
        "    <toolitem action='FileNew'/>"
        "    <toolitem action='FileOpen'/>"
        "     <placeholder name='recent_projects'/>" // *** This is where
my MenuToolButton is going
        "    <toolitem action='FileSave'/>"
        "     <separator />"
        "    <toolitem action='FileReport'/>"
        "     <separator />"
        "    <toolitem action='ViewReloadProject'/>"
        "    <toolitem action='FileClose'/>"
        "     <separator />"
        "    <toolitem action='FileQuit'/>"
        "  </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
<[email protected]>wrote:

> Have you read about menus and toolbars in the gtkmm tutorial, chapter 12?
> http://developer.gnome.org/**gtkmm-tutorial/stable/<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<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="MyAction">
>>     <menu action='ToolMenu'>
>>      <menuitem action="Action1">
>>      <menuitem action="Action2">
>>     </menu>
>>    </toolitem>
>> </toolbar>
>> ...
>> </ui>
>>
>> Any help is appreciated!
>>
>> Steve
>>
>>
>> --
>> Steve Holmes
>> M: 416-791-3868
>>
>>
>


-- 
Steve Holmes**
M: 416-791-3868
_______________________________________________
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list

Reply via email to