~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/sdk/menuitemsmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
4
 *
 
5
 * $Revision: 4909 $
 
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 $
 
8
 */
 
9
 
 
10
#ifndef CB_PRECOMP
 
11
    #include <wx/frame.h> // GetMenuBar
 
12
#endif
 
13
 
 
14
#include "sdk_precomp.h"
 
15
#include "manager.h"
 
16
#include "menuitemsmanager.h"
 
17
#include <wx/regex.h>
 
18
 
 
19
namespace
 
20
{
 
21
    wxRegEx reInsert(_T("([0-9]+):.+"));
 
22
};
 
23
 
 
24
MenuItemsManager::MenuItemsManager(bool autoClearOnDestroy)
 
25
    : m_AutoClearOnDestroy(autoClearOnDestroy)
 
26
{
 
27
    //ctor
 
28
}
 
29
 
 
30
MenuItemsManager::~MenuItemsManager()
 
31
{
 
32
    //dtor
 
33
    if (m_AutoClearOnDestroy)
 
34
    {
 
35
        Clear();
 
36
        m_MenuItems.Clear();
 
37
    }
 
38
}
 
39
 
 
40
/** @brief Add a menu item
 
41
  *
 
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.
 
47
  */
 
48
wxMenuItem* MenuItemsManager::Add(wxMenu* parent, int id, const wxString& caption, const wxString& helptext)
 
49
{
 
50
    if (!parent)
 
51
        return 0;
 
52
    wxMenuItem* ni = new wxMenuItem(parent, id, caption, helptext);
 
53
    m_MenuItems.Add(ni);
 
54
    parent->Append(ni);
 
55
    return ni;
 
56
}
 
57
 
 
58
/** @brief Insert a menu item
 
59
  *
 
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.
 
66
  */
 
67
wxMenuItem* MenuItemsManager::Insert(wxMenu* parent, int index, int id, const wxString& caption, const wxString& helptext)
 
68
{
 
69
    if (!parent)
 
70
        return 0;
 
71
    wxMenuItem* ni = new wxMenuItem(parent, id, caption, helptext);
 
72
    m_MenuItems.Add(ni);
 
73
    parent->Insert(index, ni);
 
74
    return ni;
 
75
} // end of Insert
 
76
 
 
77
/** @brief Clear all managed menu items
 
78
  */
 
79
void MenuItemsManager::Clear()
 
80
{
 
81
    for (unsigned int i = 0; i < m_MenuItems.Count(); ++i)
 
82
    {
 
83
        wxMenuItem* ni = m_MenuItems[i];
 
84
        wxMenu* menu = ni->GetMenu();
 
85
        wxMenu* subMenu = ni->GetSubMenu();
 
86
        if (menu)
 
87
        {
 
88
            // only delete if it's not a submenu or, if it is but it's empty
 
89
            if (!subMenu || subMenu->GetMenuItemCount() == 0)
 
90
                menu->Delete(ni);
 
91
        }
 
92
    }
 
93
    m_MenuItems.Clear();
 
94
} // end of Clear
 
95
 
 
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.
 
104
  */
 
105
wxMenuItem* MenuItemsManager::CreateFromString(const wxString& menuPath, int id)
 
106
{
 
107
    wxMenuBar* mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
 
108
    wxMenu* menu = 0;
 
109
    size_t pos = 0;
 
110
    while (true)
 
111
    {
 
112
        // ignore consecutive slashes
 
113
        while (pos < menuPath.Length() && menuPath.GetChar(pos) == _T('/'))
 
114
        {
 
115
            ++pos;
 
116
        }
 
117
 
 
118
        // find next slash
 
119
        size_t nextPos = pos;
 
120
        while (nextPos < menuPath.Length() && menuPath.GetChar(++nextPos) != _T('/'))
 
121
            ;
 
122
 
 
123
        wxString current = menuPath.Mid(pos, nextPos - pos);
 
124
        if (current.IsEmpty())
 
125
            break;
 
126
        bool isLast = nextPos >= menuPath.Length();
 
127
 
 
128
        bool insert = false;
 
129
        unsigned long insertIndex = 0;
 
130
        if (reInsert.Matches(current))
 
131
        {
 
132
            // insert menu instead append (format "insertIndex:menuName")
 
133
            wxString indexS = reInsert.GetMatch(current, 1);
 
134
            if (indexS.ToULong(&insertIndex, 10))
 
135
            {
 
136
                current.Remove(0, indexS.Length() + 1); // +1 to remove the ":" too
 
137
                insert = true;
 
138
            }
 
139
        }
 
140
 
 
141
        if (!menu)
 
142
        {
 
143
            if (isLast)
 
144
                return 0;
 
145
 
 
146
            // for first entry we must search on the menubar
 
147
            int menuPos = mbar->FindMenu(current);
 
148
            if (menuPos == wxNOT_FOUND)
 
149
            {
 
150
                menu = new wxMenu();
 
151
                mbar->Insert(insert ? insertIndex : mbar->GetMenuCount() - 2, menu, current); // -2 to be inserted before "Settings"
 
152
            }
 
153
            else
 
154
                menu = mbar->GetMenu(menuPos);
 
155
        }
 
156
        else
 
157
        {
 
158
            bool needsSep = current.StartsWith(_T("-"));
 
159
            if (needsSep)
 
160
                current.Remove(0, 1); // remove dash (-)
 
161
 
 
162
            wxMenu* existingMenu = 0;
 
163
            int existing = menu->FindItem(current);
 
164
            if (existing != wxNOT_FOUND)
 
165
            {
 
166
                // existing menu
 
167
                // if it is the final item we want to create, display error and stop
 
168
                if (isLast || existing >= (int)menu->GetMenuItemCount())
 
169
                    return 0;
 
170
 
 
171
                // else just keep the menu pointer updated
 
172
                existingMenu = menu->GetMenuItems()[existing]->GetSubMenu();
 
173
                if (!existingMenu)
 
174
                    return 0;
 
175
 
 
176
                menu = existingMenu;
 
177
            }
 
178
            else
 
179
            {
 
180
                if (needsSep)
 
181
                {
 
182
                    wxMenuItem* item = new wxMenuItem(menu, wxID_SEPARATOR);
 
183
                    menu->Insert(insert ? insertIndex : menu->GetMenuItemCount(), item);
 
184
                }
 
185
 
 
186
                if (current.IsEmpty()) // may be now if it was just a separator (-)
 
187
                    break;
 
188
 
 
189
                if (isLast)
 
190
                    return Insert(menu, insert ? insertIndex : menu->GetMenuItemCount(), id, current, wxEmptyString);
 
191
                else
 
192
                {
 
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);
 
196
                    menu = sub;
 
197
                }
 
198
            }
 
199
        }
 
200
 
 
201
        pos = nextPos; // prepare for next loop
 
202
    }
 
203
    return 0;
 
204
} // end of CreateFromString