99using namespace std ;
1010
1111// Static instance
12- MenuManager* MenuManager::s_menuManager;
12+ MenuManager* MenuManager::s_menuManager = NULL ;
1313
1414WNDPROC MenuManager::s_origWndProc;
1515
@@ -27,7 +27,6 @@ bool MenuManager::s_menuItemClicked;
2727void (*MenuManager::s_runScript)(int );
2828
2929
30-
3130MenuManager* MenuManager::create (HWND hNotepad, HINSTANCE hInst, void (*runScript)(const char *))
3231{
3332 if (NULL == s_menuManager)
@@ -208,29 +207,40 @@ bool MenuManager::populateScriptsMenu()
208207// Fills the Scripts menu
209208void MenuManager::refreshScriptsMenu ()
210209{
211- // This will try to delete all scripts menu items
212- // - scripts in sub directories will be unsuccessful,
213- // but less trouble (read: CPU) than trying to work out which ones we should delete (ie. root dir)
214- for (ScriptCommandsTD::iterator it = m_scriptCommands.begin (); it != m_scriptCommands.end (); ++it)
210+
211+ // Remove all the menu items (or submenus) from the scripts menu
212+ // Remove, not destroy, as we need to destroy all the sub-submenus too
213+ int menuCount = GetMenuItemCount (m_hScriptsMenu);
214+
215+ for (int position = 0 ; position < menuCount; ++position)
215216 {
216- DeleteMenu (m_hScriptsMenu, it-> first , MF_BYCOMMAND );
217+ RemoveMenu (m_hScriptsMenu, 0 , MF_BYPOSITION );
217218 }
218-
219+
220+
219221 m_scriptCommands.erase (m_scriptCommands.begin (), m_scriptCommands.end ());
220222 m_machineScriptNames.erase (m_machineScriptNames.begin (), m_machineScriptNames.end ());
223+
224+
225+
226+ // Destroy all the menus we've created
221227 for (SubmenusTD::iterator it = m_submenus.begin (); it != m_submenus.end (); ++it)
222228 {
223229 if (it->first != " \\ " )
224230 {
231+ // Destroy the menu
225232 DestroyMenu (it->second );
226233 }
227234 }
235+
236+ m_submenus.erase (m_submenus.begin (), m_submenus.end ());
228237
229238 int nextID = findScripts (m_hScriptsMenu, m_machineScriptsPath.size (), s_startCommandID, m_machineScriptsPath);
230239
231240 s_endCommandID = findScripts (m_hScriptsMenu, m_userScriptsPath.size (), nextID, m_userScriptsPath);
232241
233242
243+ DrawMenuBar (m_hNotepad);
234244}
235245
236246
@@ -563,4 +573,14 @@ void MenuManager::configureToolbarIcons()
563573
564574 if (currentToolbarID > s_startToolbarID)
565575 s_endToolbarID = currentToolbarID;
576+ }
577+
578+
579+ void MenuManager::deleteInstance ()
580+ {
581+ if (s_menuManager)
582+ {
583+ delete s_menuManager;
584+ s_menuManager = NULL ;
585+ }
566586}
0 commit comments