]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "menu.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/menu.h" | |
17 | #include "wx/log.h" | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // wxMenuBar | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
24 | ||
25 | wxMenuBar::wxMenuBar() | |
26 | { | |
27 | }; | |
28 | ||
29 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
30 | { | |
31 | m_menus.Append( menu ); | |
32 | menu->m_title = title; // ?????? | |
33 | ||
34 | int pos; | |
35 | do { | |
36 | pos = menu->m_title.First( '&' ); | |
37 | if (pos != -1) menu->m_title.Remove( pos, 1 ); | |
38 | } while (pos != -1); | |
39 | ||
40 | }; | |
41 | ||
42 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
43 | { | |
44 | if (menu->m_title == menuString) | |
45 | { | |
46 | int res = menu->FindItem( itemString ); | |
47 | if (res != -1) return res; | |
48 | }; | |
49 | wxNode *node = menu->m_items.First(); | |
50 | while (node) | |
51 | { | |
52 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
53 | if (item->IsSubMenu()) | |
54 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
55 | node = node->Next(); | |
56 | }; | |
57 | return -1; | |
58 | }; | |
59 | ||
60 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
61 | { | |
62 | wxNode *node = m_menus.First(); | |
63 | while (node) | |
64 | { | |
65 | wxMenu *menu = (wxMenu*)node->Data(); | |
66 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
67 | if (res != -1) return res; | |
68 | node = node->Next(); | |
69 | }; | |
70 | return -1; | |
71 | }; | |
72 | ||
73 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
74 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
75 | { | |
76 | wxMenuItem* result = menu->FindItem(id); | |
77 | ||
78 | wxNode *node = menu->m_items.First(); | |
79 | while ( node && result == NULL ) { | |
80 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
81 | if ( item->IsSubMenu() ) | |
82 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
83 | node = node->Next(); | |
84 | }; | |
85 | ||
86 | return result; | |
87 | }; | |
88 | ||
89 | wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const | |
90 | { | |
91 | wxMenuItem* result = 0; | |
92 | wxNode *node = m_menus.First(); | |
93 | while (node && result == 0) | |
94 | { | |
95 | wxMenu *menu = (wxMenu*)node->Data(); | |
96 | result = FindMenuItemByIdRecursive( menu, id ); | |
97 | node = node->Next(); | |
98 | } | |
99 | return result; | |
100 | } | |
101 | ||
102 | void wxMenuBar::Check( int id, bool check ) | |
103 | { | |
104 | wxMenuItem* item = FindMenuItemById( id ); | |
105 | if (item) item->Check(check); | |
106 | }; | |
107 | ||
108 | bool wxMenuBar::Checked( int id ) const | |
109 | { | |
110 | wxMenuItem* item = FindMenuItemById( id ); | |
111 | if (item) return item->IsChecked(); | |
112 | return FALSE; | |
113 | }; | |
114 | ||
115 | void wxMenuBar::Enable( int id, bool enable ) | |
116 | { | |
117 | wxMenuItem* item = FindMenuItemById( id ); | |
118 | if (item) item->Enable(enable); | |
119 | }; | |
120 | ||
121 | bool wxMenuBar::Enabled( int id ) const | |
122 | { | |
123 | wxMenuItem* item = FindMenuItemById( id ); | |
124 | if (item) return item->IsEnabled(); | |
125 | return FALSE; | |
126 | }; | |
127 | ||
128 | //----------------------------------------------------------------------------- | |
129 | // wxMenu | |
130 | //----------------------------------------------------------------------------- | |
131 | ||
132 | ||
133 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) | |
134 | ||
135 | wxMenuItem::wxMenuItem() | |
136 | { | |
137 | m_id = ID_SEPARATOR; | |
138 | m_isCheckMenu = FALSE; | |
139 | m_isChecked = FALSE; | |
140 | m_isEnabled = TRUE; | |
141 | m_subMenu = NULL; | |
142 | }; | |
143 | ||
144 | void wxMenuItem::SetText(const wxString& str) | |
145 | { | |
146 | for ( const char *pc = str; *pc != '\0'; pc++ ) { | |
147 | if ( *pc == '&' ) | |
148 | pc++; // skip it | |
149 | ||
150 | m_text << *pc; | |
151 | } | |
152 | } | |
153 | ||
154 | void wxMenuItem::Check( bool check ) | |
155 | { | |
156 | wxCHECK_RET( IsCheckable(), "can't check uncheckable item!" ) | |
157 | ||
158 | m_isChecked = check; | |
159 | } | |
160 | ||
161 | bool wxMenuItem::IsChecked() const | |
162 | { | |
163 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! | |
164 | ||
165 | return FALSE; | |
166 | } | |
167 | ||
168 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
169 | ||
170 | wxMenu::wxMenu( const wxString &title ) | |
171 | { | |
172 | m_title = title; | |
173 | m_items.DeleteContents( TRUE ); | |
174 | m_invokingWindow = NULL; | |
175 | }; | |
176 | ||
177 | void wxMenu::AppendSeparator() | |
178 | { | |
179 | wxMenuItem *mitem = new wxMenuItem(); | |
180 | mitem->SetId(ID_SEPARATOR); | |
181 | ||
182 | m_items.Append( mitem ); | |
183 | }; | |
184 | ||
185 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) | |
186 | { | |
187 | wxMenuItem *mitem = new wxMenuItem(); | |
188 | mitem->SetId(id); | |
189 | mitem->SetText(item); | |
190 | mitem->SetHelpString(helpStr); | |
191 | mitem->SetCheckable(checkable); | |
192 | ||
193 | m_items.Append( mitem ); | |
194 | }; | |
195 | ||
196 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) | |
197 | { | |
198 | wxMenuItem *mitem = new wxMenuItem(); | |
199 | mitem->SetId(id); | |
200 | mitem->SetText(text); | |
201 | mitem->SetHelpString(helpStr); | |
202 | mitem->SetSubMenu(subMenu); | |
203 | ||
204 | m_items.Append( mitem ); | |
205 | }; | |
206 | ||
207 | int wxMenu::FindItem( const wxString itemString ) const | |
208 | { | |
209 | wxString s( itemString ); | |
210 | ||
211 | int pos; | |
212 | do { | |
213 | pos = s.First( '&' ); | |
214 | if (pos != -1) s.Remove( pos, 1 ); | |
215 | } while (pos != -1); | |
216 | ||
217 | wxNode *node = m_items.First(); | |
218 | while (node) | |
219 | { | |
220 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
221 | if (item->GetText() == s) | |
222 | return item->GetId(); | |
223 | node = node->Next(); | |
224 | }; | |
225 | ||
226 | return -1; | |
227 | }; | |
228 | ||
229 | void wxMenu::Enable( int id, bool enable ) | |
230 | { | |
231 | wxMenuItem *item = FindItem(id); | |
232 | if ( item ) | |
233 | item->Enable(enable); | |
234 | }; | |
235 | ||
236 | bool wxMenu::IsEnabled( int id ) const | |
237 | { | |
238 | wxMenuItem *item = FindItem(id); | |
239 | if ( item ) | |
240 | return item->IsEnabled(); | |
241 | else | |
242 | return FALSE; | |
243 | }; | |
244 | ||
245 | void wxMenu::Check( int id, bool enable ) | |
246 | { | |
247 | wxMenuItem *item = FindItem(id); | |
248 | if ( item ) | |
249 | item->Check(enable); | |
250 | }; | |
251 | ||
252 | bool wxMenu::IsChecked( int id ) const | |
253 | { | |
254 | wxMenuItem *item = FindItem(id); | |
255 | if ( item ) | |
256 | return item->IsChecked(); | |
257 | else | |
258 | return FALSE; | |
259 | }; | |
260 | ||
261 | void wxMenu::SetLabel( int id, const wxString &label ) | |
262 | { | |
263 | wxMenuItem *item = FindItem(id); | |
264 | if ( item ) | |
265 | item->SetText(label); | |
266 | }; | |
267 | ||
268 | wxMenuItem *wxMenu::FindItem(int id) const | |
269 | { | |
270 | wxNode *node = m_items.First(); | |
271 | while (node) { | |
272 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
273 | if ( item->GetId() == id ) | |
274 | return item; | |
275 | node = node->Next(); | |
276 | }; | |
277 | ||
278 | wxLogDebug("wxMenu::FindItem: item %d not found.", id); | |
279 | ||
280 | return NULL; | |
281 | } | |
282 | ||
283 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
284 | { | |
285 | m_invokingWindow = win; | |
286 | }; | |
287 | ||
288 | wxWindow *wxMenu::GetInvokingWindow() | |
289 | { | |
290 | return m_invokingWindow; | |
291 | }; | |
292 | ||
293 |