Author Topic: Add icon to submenu  (Read 4468 times)

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Add icon to submenu
« on: November 08, 2011, 05:36:36 pm »
Hi All,

I'm trying to add an icon to a submenu added by a plugin to no avail...

Here's what I'm trying to do
Code: [Select]
wxString prefix = ConfigManager::GetDataFolder() + _T("/images/cbvss/");
wxBitmap* bmp = new wxBitmap(cbLoadBitmap(prefix + _T("vss.png"), wxBITMAP_TYPE_PNG));

[... Build menu ...]

wxMenuItem* subMenu =  menu->AppendSubMenu(VssMenu, title, _("Help"));
subMenu->SetBitmap(*bmp);

The file vss.png exists under the prefix dir.

Any help?

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Add icon to submenu
« Reply #1 on: November 08, 2011, 11:09:21 pm »
This is how DoxyBlocks does it:
Code: [Select]
void DoxyBlocks::BuildMenu(wxMenuBar *menuBar)
{
    //The application is offering its menubar for your plugin,
    //to add any menu items you want...
    //Append any items you need in the menu...
    //NOTE: Be careful in here... The application's menubar is at your disposal.
//    NotImplemented(wxT("DoxyBlocks::BuildMenu()"));

    int idx = menuBar->FindMenu(_("P&lugins"));
    if(idx != wxNOT_FOUND){
        wxMenu *submenu = new wxMenu;
        wxString sDataFolder(ConfigManager::GetDataFolder());
        wxString prefix = sDataFolder + wxT("/images/DoxyBlocks/16x16/");

        wxMenuItem *MenuItemDoxywizard = new wxMenuItem(submenu, ID_MENU_DOXYWIZARD, _("&Doxywizard...\tCtrl-Alt-D"), _("Run doxywizard."));
        MenuItemDoxywizard->SetBitmap(wxBitmap(prefix + wxT("doxywizard.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemDoxywizard);
        wxMenuItem *MenuItemExtract = new wxMenuItem(submenu, ID_MENU_EXTRACTPROJECT, _("&Extract documentation\tCtrl-Alt-E"), _("Extract documentation for the current project."));
        MenuItemExtract->SetBitmap(wxBitmap(prefix + wxT("extract.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemExtract);
        submenu->AppendSeparator();
        wxMenuItem *MenuItemBlockComment = new wxMenuItem(submenu, ID_MENU_BLOCKCOMMENT, _("&Block comment\tCtrl-Alt-B"), _("Insert a comment block at the current line."));
        MenuItemBlockComment->SetBitmap(wxBitmap(prefix + wxT("comment_block.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemBlockComment);
        wxMenuItem *MenuItemLineComment = new wxMenuItem(submenu, ID_MENU_LINECOMMENT, _("&Line comment\tCtrl-Alt-L"), _("Insert a line comment at the current cursor position."));
        MenuItemLineComment->SetBitmap(wxBitmap(prefix + wxT("comment_line.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemLineComment);
        submenu->AppendSeparator();
        wxMenuItem *MenuItemRunHTML = new wxMenuItem(submenu, ID_MENU_RUNHTML, _("Run &HTML\tCtrl-Alt-H"), _("Run HTML documentation."));
        MenuItemRunHTML->SetBitmap(wxBitmap(prefix + wxT("html.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemRunHTML);
        wxMenuItem *MenuItemRunCHM = new wxMenuItem(submenu, ID_MENU_RUNCHM, _("Run &CHM\tCtrl-Alt-C"), _("Insert a comment block at the current line."));
        MenuItemRunCHM->SetBitmap(wxBitmap(prefix + wxT("chm.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemRunCHM);
        submenu->AppendSeparator();
        wxMenuItem *MenuItemConfig = new wxMenuItem(submenu, ID_MENU_CONFIG, _("Open &preferences...\tCtrl-Alt-P"), _("Open DoxyBlocks' preferences."));
        MenuItemConfig->SetBitmap(wxBitmap(prefix + wxT("configure.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemConfig);
        submenu->AppendSeparator();
        wxMenuItem *MenuItemLoadTemplate = new wxMenuItem(submenu, ID_MENU_LOAD_TEMPLATE, _("L&oad settings template"), _("Load saved settings template."));
        MenuItemLoadTemplate->SetBitmap(wxBitmap(sDataFolder + wxT("/images/16x16/fileopen.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemLoadTemplate);
        wxMenuItem *MenuItemSaveTemplate = new wxMenuItem(submenu, ID_MENU_SAVE_TEMPLATE, _("&Save settings template"), _("Save current settings for future use."));
        MenuItemSaveTemplate->SetBitmap(wxBitmap(sDataFolder + wxT("/images/16x16/filesave.png"), wxBITMAP_TYPE_PNG));
        submenu->Append(MenuItemSaveTemplate);

        Connect(ID_MENU_DOXYWIZARD, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::RunDoxywizard);
        Connect(ID_MENU_EXTRACTPROJECT, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::ExtractProject);
        Connect(ID_MENU_BLOCKCOMMENT, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::BlockComment);
        Connect(ID_MENU_LINECOMMENT, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::LineComment);
        Connect(ID_MENU_RUNHTML, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::RunHTML);
        Connect(ID_MENU_RUNCHM, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::RunCHM);
        Connect(ID_MENU_CONFIG, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::Configure);
        Connect(ID_MENU_LOAD_TEMPLATE, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::ReadPrefsTemplate);
        Connect(ID_MENU_SAVE_TEMPLATE, wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)&DoxyBlocks::WritePrefsTemplate);

        menuBar->Insert(idx + 1, submenu, wxT("Do&xyBlocks"));
    }
}

You could try:
Code: [Select]
subMenu->SetBitmap(wxBitmap(prefix + wxT("vss.png"), wxBITMAP_TYPE_PNG));

Edit:
I do not believe wxWidgets allows (sub)menus to have icons; see wxMenu Class Reference (as opposed to wxMenuItem's, which do allow icons).
« Last Edit: November 09, 2011, 12:03:18 am by Alpha »

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: Add icon to submenu
« Reply #2 on: November 09, 2011, 08:51:13 am »
@Alpha thanks for you reply!

I do not believe wxWidgets allows (sub)menus to have icons; see wxMenu Class Reference (as opposed to wxMenuItem's, which do allow icons).

I'm trying to add an icon to a menu item.
And following your post, I tried to add the icon to a menu item in the menu bar and it works, as for the DoxygenPlugin.

However, this is not what I want to do. I want to add an icon to a menu item in the context menu. I tried to do it in exactly the same way, but it's still not working.

Do anyone knows if this is even possible, i.e. to have an icon in a context menu item?

Edit:
Ok, I found the reason, there's a bug report here: http://trac.wxwidgets.org/ticket/3206

"The code from description of this ticket isn't working, because we adding image after append item to menu. It will be work correctly, if we added icon before added/append item to menu."
« Last Edit: November 09, 2011, 09:04:41 am by daniloz »