Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
Toggle toolbar by context menu?
ollydbg:
--- Quote from: oBFusCATed on April 16, 2013, 11:43:41 am ---Why don't you connect the event to the toolbars themselves?
--- End quote ---
Do you mean popup the menu when click click on the toolbar button? In-fact I don't know how to do it. :)
oBFusCATed:
Not a button, but the whole toolbar.
Have you tried mytoolbar->Connect(...) ?
ollydbg:
--- Quote from: oBFusCATed on April 16, 2013, 12:29:59 pm ---Not a button, but the whole toolbar.
Have you tried mytoolbar->Connect(...) ?
--- End quote ---
Ok, thanks for the suggestion, here is the patch:
--- Code: ---Index: main.cpp
===================================================================
--- main.cpp (revision 8991)
+++ main.cpp (working copy)
@@ -529,6 +529,8 @@
EVT_MENU(idShiftTab, MainFrame::OnShiftTab)
EVT_MENU(idCtrlAltTab, MainFrame::OnCtrlAltTab)
+ //EVT_RIGHT_UP(MainFrame::OnMouseRightUp)
+
END_EVENT_TABLE()
MainFrame::MainFrame(wxWindow* parent)
@@ -1057,9 +1059,13 @@
Manager::Get()->AddonToolBar(m_pToolbar, xrcToolbarName);
m_pToolbar->Realize();
+ m_pToolbar->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnMouseRightUp) );
+
m_pToolbar->SetInitialSize();
+ m_debuggerToolbarHandler->GetToolbar()->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnMouseRightUp) );
+
std::vector<ToolbarInfo> toolbars;
toolbars.push_back(ToolbarInfo(m_pToolbar, wxAuiPaneInfo().Name(wxT("MainToolbar")).Caption(_("Main Toolbar")), 0));
@@ -1076,7 +1082,11 @@
{
ToolbarInfo info = DoAddPluginToolbar(plug);
if (info.toolbar)
+ {
toolbars.push_back(info);
+ info.toolbar->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnMouseRightUp) );
+ }
+
}
}
@@ -4829,3 +4839,20 @@
return sb;
}
+// Let the user toggle the toolbar and logs from the context menu
+void MainFrame::OnMouseRightUp(wxCommandEvent& event)
+{
+ wxMenuBar* menuBar = Manager::Get()->GetAppFrame()->GetMenuBar();
+ int idx = menuBar->FindMenu(_("&View"));
+ if (idx != wxNOT_FOUND)
+ {
+ wxMenu* viewMenu = menuBar->GetMenu(idx);
+ idx = viewMenu->FindItem(_("Toolbars"));
+ if (idx != wxNOT_FOUND)
+ {
+ wxMenu* toolbarMenu = viewMenu->FindItem(idx)->GetSubMenu();
+ PopupMenu(toolbarMenu);
+ }
+ }
+ event.Skip();
+}
Index: main.h
===================================================================
--- main.h (revision 8991)
+++ main.h (working copy)
@@ -95,6 +95,8 @@
void OnApplicationClose(wxCloseEvent& event);
void OnStartHereLink(wxCommandEvent& event);
+ void OnMouseRightUp(wxCommandEvent& event);
+
// File->New submenu entries handler
void OnFileNewWhat(wxCommandEvent& event);
--- End code ---
Question: what is the best place to disconnet the event handler?
MortenMacFly:
--- Quote from: ollydbg on April 16, 2013, 03:28:23 pm ---Question: what is the best place to disconnet the event handler?
--- End quote ---
Hard to tell. As a rule of thumb if you put the Connect into the constructor, put the Disconnect in the destructor. If you put Connect in a "Init" method, put it in "UnInit" or alike. Whats most important is that you ensure it gets disconnected early enough, so that late calls into the method won't access invalid references. Its an event, so it literally can come at "any time".
ollydbg:
Thanks, here is the patch
1, handle right click on either toolbar(red rectangle) or the space next to toolbar(green rectangle). See image shot below:
2, disconnect the event handler of toolbar correctly.
--- Code: ---Index: main.cpp
===================================================================
--- main.cpp (revision 8991)
+++ main.cpp (working copy)
@@ -529,6 +529,8 @@
EVT_MENU(idShiftTab, MainFrame::OnShiftTab)
EVT_MENU(idCtrlAltTab, MainFrame::OnCtrlAltTab)
+ EVT_RIGHT_UP(MainFrame::OnMouseRightUp)
+
END_EVENT_TABLE()
MainFrame::MainFrame(wxWindow* parent)
@@ -1057,9 +1059,13 @@
Manager::Get()->AddonToolBar(m_pToolbar, xrcToolbarName);
m_pToolbar->Realize();
+ m_pToolbar->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick) );
+
m_pToolbar->SetInitialSize();
+ m_debuggerToolbarHandler->GetToolbar()->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick) );
+
std::vector<ToolbarInfo> toolbars;
toolbars.push_back(ToolbarInfo(m_pToolbar, wxAuiPaneInfo().Name(wxT("MainToolbar")).Caption(_("Main Toolbar")), 0));
@@ -1076,7 +1082,11 @@
{
ToolbarInfo info = DoAddPluginToolbar(plug);
if (info.toolbar)
+ {
toolbars.push_back(info);
+ info.toolbar->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick) );
+ }
+
}
}
@@ -1710,6 +1720,8 @@
}
wxAuiPaneInfo paneInfo(toolbarInfo.paneInfo);
m_LayoutManager.AddPane(toolbarInfo.toolbar, paneInfo. ToolbarPane().Top().Row(row).Position(position));
+ // add the event handler for mouse right click
+ toolbarInfo.toolbar->Connect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick));
DoUpdateLayout();
}
@@ -2752,6 +2764,17 @@
m_pInfoPane = 0L;
}
+ // Disconnect the event handler for toolbar's mouse right click event before the plugin is
+ // unloaded in Manager::Shutdown().
+ PluginToolbarsMap::iterator it;
+ for( it = m_PluginsTools.begin(); it != m_PluginsTools.end(); ++it )
+ {
+ wxToolBar* toolbar = it->second;
+ if (toolbar)//Disconnect the mouse right click event handler before the toolbar is destroyed
+ toolbar->Disconnect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick));
+
+ }
+
Manager::Shutdown(); // Shutdown() is not Free(), Manager is automatically destroyed at exit
Destroy();
@@ -4456,6 +4479,8 @@
// remove toolbar, if any
if (m_PluginsTools[plugin])
{
+ //Disconnect the mouse right click event handler before the toolbar is destroyed
+ m_PluginsTools[plugin]->Disconnect(wxID_ANY, wxEVT_COMMAND_TOOL_RCLICKED, wxCommandEventHandler(MainFrame::OnToolBarRightClick));
m_LayoutManager.DetachPane(m_PluginsTools[plugin]);
m_PluginsTools[plugin]->Destroy();
m_PluginsTools.erase(plugin);
@@ -4829,3 +4854,33 @@
return sb;
}
+// Let the user toggle the toolbar and logs from the context menu
+void MainFrame::OnMouseRightUp(wxMouseEvent& event)
+{
+ PopupToggleToolbarMenu();
+ event.Skip();
+}
+
+void MainFrame::OnToolBarRightClick(wxCommandEvent& event)
+{
+ PopupToggleToolbarMenu();
+ event.Skip();
+}
+
+void MainFrame::PopupToggleToolbarMenu()
+{
+ wxMenuBar* menuBar = Manager::Get()->GetAppFrame()->GetMenuBar();
+ int idx = menuBar->FindMenu(_("&View"));
+ if (idx != wxNOT_FOUND)
+ {
+ wxMenu* viewMenu = menuBar->GetMenu(idx);
+ idx = viewMenu->FindItem(_("Toolbars"));
+ if (idx != wxNOT_FOUND)
+ {
+ wxMenu* toolbarMenu = viewMenu->FindItem(idx)->GetSubMenu();
+ PopupMenu(toolbarMenu);
+ }
+ }
+}
+
+
Index: main.h
===================================================================
--- main.h (revision 8991)
+++ main.h (working copy)
@@ -95,6 +95,14 @@
void OnApplicationClose(wxCloseEvent& event);
void OnStartHereLink(wxCommandEvent& event);
+ // the two functions below are used to show context menu to toggle toolbar view status
+ // OnMouseRightUp is handler for right click on MainFrame client space not covered by
+ // any other panels, OnToolBarRightClick is used to response the mouse right click command
+ // on the toolbar.
+ void OnMouseRightUp(wxMouseEvent& event);
+ void OnToolBarRightClick(wxCommandEvent& event);
+ void PopupToggleToolbarMenu();
+
// File->New submenu entries handler
void OnFileNewWhat(wxCommandEvent& event);
@@ -348,14 +356,14 @@
LogManager* m_pLogMan;
InfoPane* m_pInfoPane;
- wxToolBar* m_pToolbar;
- PluginToolbarsMap m_PluginsTools;
+ wxToolBar* m_pToolbar; // main toolbar
+ PluginToolbarsMap m_PluginsTools; // plugin -> toolbar map
PluginIDsMap m_PluginIDsMap;
wxMenu* m_ToolsMenu;
wxMenu* m_PluginsMenu;
wxMenu* m_HelpPluginsMenu;
- bool m_ScanningForPlugins;
+ bool m_ScanningForPlugins; // this variable is used to delay the UI construction
bool m_SmallToolBar;
bool m_StartupDone;
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version