2
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3
* http://www.gnu.org/licenses/lgpl-3.0.html
6
* $Id: menuitemsmanager.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
7
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/sdk/menuitemsmanager.cpp $
11
#include <wx/frame.h> // GetMenuBar
14
#include "sdk_precomp.h"
16
#include "menuitemsmanager.h"
21
wxRegEx reInsert(_T("([0-9]+):.+"));
24
MenuItemsManager::MenuItemsManager(bool autoClearOnDestroy)
25
: m_AutoClearOnDestroy(autoClearOnDestroy)
30
MenuItemsManager::~MenuItemsManager()
33
if (m_AutoClearOnDestroy)
40
/** @brief Add a menu item
42
* @param parent The menu to append the menu item to
43
* @param id The menu item ID (use wxID_SEPARATOR for adding a separator)
44
* @param caption The caption for the new menu item
45
* @param helptext The help text for the new menu item
46
* @return The new menu item or NULL for failure.
48
wxMenuItem* MenuItemsManager::Add(wxMenu* parent, int id, const wxString& caption, const wxString& helptext)
52
wxMenuItem* ni = new wxMenuItem(parent, id, caption, helptext);
58
/** @brief Insert a menu item
60
* @param parent The menu to insert the menu item to
61
* @param index The index where to insert the menu item
62
* @param id The menu item ID (use wxID_SEPARATOR for adding a separator)
63
* @param caption The caption for the new menu item
64
* @param helptext The help text for the new menu item
65
* @return The new menu item or NULL for failure.
67
wxMenuItem* MenuItemsManager::Insert(wxMenu* parent, int index, int id, const wxString& caption, const wxString& helptext)
71
wxMenuItem* ni = new wxMenuItem(parent, id, caption, helptext);
73
parent->Insert(index, ni);
77
/** @brief Clear all managed menu items
79
void MenuItemsManager::Clear()
81
for (unsigned int i = 0; i < m_MenuItems.Count(); ++i)
83
wxMenuItem* ni = m_MenuItems[i];
84
wxMenu* menu = ni->GetMenu();
85
wxMenu* subMenu = ni->GetSubMenu();
88
// only delete if it's not a submenu or, if it is but it's empty
89
if (!subMenu || subMenu->GetMenuItemCount() == 0)
96
/** @brief Create menu path from string
97
* @param menuPath The full menu path. This can be separated by slashes (/)
98
* to create submenus (e.g. "MyScripts/ASubMenu/MyItem").
99
* If the last part of the string ("MyItem" in the example)
100
* starts with a dash (-) (e.g. "-MyItem") then a menu
101
* separator is prepended before the actual menu item.
102
* @param id The menu item ID (use wxID_SEPARATOR for adding a separator)
103
* @return The newly created menu item or NULL for failure.
105
wxMenuItem* MenuItemsManager::CreateFromString(const wxString& menuPath, int id)
107
wxMenuBar* mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
112
// ignore consecutive slashes
113
while (pos < menuPath.Length() && menuPath.GetChar(pos) == _T('/'))
119
size_t nextPos = pos;
120
while (nextPos < menuPath.Length() && menuPath.GetChar(++nextPos) != _T('/'))
123
wxString current = menuPath.Mid(pos, nextPos - pos);
124
if (current.IsEmpty())
126
bool isLast = nextPos >= menuPath.Length();
129
unsigned long insertIndex = 0;
130
if (reInsert.Matches(current))
132
// insert menu instead append (format "insertIndex:menuName")
133
wxString indexS = reInsert.GetMatch(current, 1);
134
if (indexS.ToULong(&insertIndex, 10))
136
current.Remove(0, indexS.Length() + 1); // +1 to remove the ":" too
146
// for first entry we must search on the menubar
147
int menuPos = mbar->FindMenu(current);
148
if (menuPos == wxNOT_FOUND)
151
mbar->Insert(insert ? insertIndex : mbar->GetMenuCount() - 2, menu, current); // -2 to be inserted before "Settings"
154
menu = mbar->GetMenu(menuPos);
158
bool needsSep = current.StartsWith(_T("-"));
160
current.Remove(0, 1); // remove dash (-)
162
wxMenu* existingMenu = 0;
163
int existing = menu->FindItem(current);
164
if (existing != wxNOT_FOUND)
167
// if it is the final item we want to create, display error and stop
168
if (isLast || existing >= (int)menu->GetMenuItemCount())
171
// else just keep the menu pointer updated
172
existingMenu = menu->GetMenuItems()[existing]->GetSubMenu();
182
wxMenuItem* item = new wxMenuItem(menu, wxID_SEPARATOR);
183
menu->Insert(insert ? insertIndex : menu->GetMenuItemCount(), item);
186
if (current.IsEmpty()) // may be now if it was just a separator (-)
190
return Insert(menu, insert ? insertIndex : menu->GetMenuItemCount(), id, current, wxEmptyString);
193
wxMenu* sub = new wxMenu;
194
wxMenuItem* item = new wxMenuItem(menu, -1, current, wxEmptyString, wxITEM_NORMAL, sub);
195
menu->Insert(insert ? insertIndex : menu->GetMenuItemCount(), item);
201
pos = nextPos; // prepare for next loop
204
} // end of CreateFromString