]>
Commit | Line | Data |
---|---|---|
7c78e7c7 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
01b2eeec KB |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
7c78e7c7 RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | ||
01b2eeec KB |
13 | // ============================================================================ |
14 | // headers & declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // wxWindows headers | |
18 | // ----------------- | |
19 | ||
7c78e7c7 RR |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "menu.h" | |
01b2eeec | 22 | #pragma implementation "menuitem.h" |
7c78e7c7 RR |
23 | #endif |
24 | ||
25 | #include "wx/menu.h" | |
01b2eeec | 26 | #include "wx/menuitem.h" |
7c78e7c7 RR |
27 | #include "wx/log.h" |
28 | ||
01b2eeec KB |
29 | // other standard headers |
30 | // ---------------------- | |
31 | #include <string.h> | |
32 | ||
01b2eeec KB |
33 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
34 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
7c78e7c7 | 35 | |
01b2eeec KB |
36 | // ============================================================================ |
37 | // implementation | |
38 | // ============================================================================ | |
7c78e7c7 | 39 | |
01b2eeec KB |
40 | // Menus |
41 | ||
42 | // Construct a menu with optional title (then use append) | |
43 | wxMenu::wxMenu(const wxString& title, const wxFunction func) | |
7c78e7c7 | 44 | { |
01b2eeec KB |
45 | m_title = title; |
46 | m_parent = NULL; | |
47 | m_eventHandler = this; | |
48 | m_noItems = 0; | |
49 | m_menuBar = NULL; | |
50 | if (m_title != "") | |
51 | { | |
52 | Append(-2, m_title) ; | |
53 | AppendSeparator() ; | |
54 | } | |
55 | ||
56 | Callback(func); | |
57 | ||
58 | // TODO create menu | |
59 | } | |
7c78e7c7 | 60 | |
01b2eeec KB |
61 | // The wxWindow destructor will take care of deleting the submenus. |
62 | wxMenu::~wxMenu() | |
63 | { | |
64 | // TODO destroy menu and children | |
65 | ||
66 | wxNode *node = m_menuItems.First(); | |
67 | while (node) | |
68 | { | |
69 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
70 | ||
71 | // Delete child menus. | |
72 | // Beware: they must not be appended to children list!!! | |
73 | // (because order of delete is significant) | |
74 | if (item->GetSubMenu()) | |
75 | item->DeleteSubMenu(); | |
76 | ||
77 | wxNode *next = node->Next(); | |
78 | delete item; | |
79 | delete node; | |
80 | node = next; | |
81 | } | |
82 | } | |
7c78e7c7 | 83 | |
01b2eeec KB |
84 | void wxMenu::Break() |
85 | { | |
86 | // TODO | |
87 | } | |
7c78e7c7 | 88 | |
01b2eeec KB |
89 | // function appends a new item or submenu to the menu |
90 | void wxMenu::Append(wxMenuItem *pItem) | |
91 | { | |
92 | // TODO | |
7c78e7c7 | 93 | |
01b2eeec KB |
94 | wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" ); |
95 | ||
96 | m_menuItems.Append(pItem); | |
97 | ||
98 | m_noItems++; | |
99 | } | |
100 | ||
101 | void wxMenu::AppendSeparator() | |
7c78e7c7 | 102 | { |
01b2eeec KB |
103 | // TODO |
104 | Append(new wxMenuItem(this, ID_SEPARATOR)); | |
105 | } | |
7c78e7c7 | 106 | |
01b2eeec KB |
107 | // Pullright item |
108 | void wxMenu::Append(int Id, const wxString& label, wxMenu *SubMenu, | |
109 | const wxString& helpString) | |
7c78e7c7 | 110 | { |
01b2eeec KB |
111 | Append(new wxMenuItem(this, Id, label, helpString, FALSE, SubMenu)); |
112 | } | |
7c78e7c7 | 113 | |
01b2eeec KB |
114 | // Ordinary menu item |
115 | void wxMenu::Append(int Id, const wxString& label, | |
116 | const wxString& helpString, bool checkable) | |
7c78e7c7 | 117 | { |
01b2eeec KB |
118 | // 'checkable' parameter is useless for Windows. |
119 | Append(new wxMenuItem(this, Id, label, helpString, checkable)); | |
120 | } | |
121 | ||
122 | void wxMenu::Delete(int id) | |
123 | { | |
124 | wxNode *node; | |
125 | wxMenuItem *item; | |
126 | int pos; | |
127 | ||
128 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) { | |
129 | item = (wxMenuItem *)node->Data(); | |
130 | if (item->GetId() == id) | |
131 | break; | |
132 | } | |
7c78e7c7 | 133 | |
01b2eeec KB |
134 | if (!node) |
135 | return; | |
7c78e7c7 | 136 | |
01b2eeec KB |
137 | m_menuItems.DeleteNode(node); |
138 | delete item; | |
7c78e7c7 | 139 | |
01b2eeec KB |
140 | // TODO |
141 | } | |
142 | ||
143 | void wxMenu::Enable(int Id, bool Flag) | |
7c78e7c7 | 144 | { |
01b2eeec KB |
145 | wxMenuItem *item = FindItemForId(Id); |
146 | wxCHECK_RET( item != NULL, "can't enable non-existing menu item" ); | |
147 | ||
148 | item->Enable(Flag); | |
7c78e7c7 RR |
149 | } |
150 | ||
01b2eeec | 151 | bool wxMenu::Enabled(int Id) const |
7c78e7c7 | 152 | { |
01b2eeec KB |
153 | wxMenuItem *item = FindItemForId(Id); |
154 | wxCHECK( item != NULL, FALSE ); | |
155 | ||
156 | return item->IsEnabled(); | |
157 | } | |
7c78e7c7 | 158 | |
01b2eeec | 159 | void wxMenu::Check(int Id, bool Flag) |
7c78e7c7 | 160 | { |
01b2eeec KB |
161 | wxMenuItem *item = FindItemForId(Id); |
162 | wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" ); | |
7c78e7c7 | 163 | |
01b2eeec KB |
164 | item->Check(Flag); |
165 | } | |
166 | ||
167 | bool wxMenu::Checked(int Id) const | |
7c78e7c7 | 168 | { |
01b2eeec KB |
169 | wxMenuItem *item = FindItemForId(Id); |
170 | wxCHECK( item != NULL, FALSE ); | |
171 | ||
172 | return item->IsChecked(); | |
173 | } | |
7c78e7c7 | 174 | |
01b2eeec | 175 | void wxMenu::SetTitle(const wxString& label) |
7c78e7c7 | 176 | { |
01b2eeec KB |
177 | m_title = label ; |
178 | // TODO | |
179 | } | |
7c78e7c7 | 180 | |
01b2eeec KB |
181 | const wxString& wxMenu::GetTitle() const |
182 | { | |
183 | return m_title; | |
184 | } | |
7c78e7c7 | 185 | |
01b2eeec KB |
186 | void wxMenu::SetLabel(int id, const wxString& label) |
187 | { | |
188 | wxMenuItem *item = FindItemForId(id) ; | |
189 | if (item==NULL) | |
190 | return; | |
191 | ||
192 | if (item->GetSubMenu()==NULL) | |
193 | { | |
194 | // TODO | |
195 | } | |
196 | else | |
197 | { | |
198 | // TODO | |
199 | } | |
200 | item->SetName(label); | |
201 | } | |
7c78e7c7 | 202 | |
01b2eeec KB |
203 | wxString wxMenu::GetLabel(int Id) const |
204 | { | |
205 | // TODO | |
206 | return wxString("") ; | |
207 | } | |
7c78e7c7 | 208 | |
01b2eeec KB |
209 | // Finds the item id matching the given string, -1 if not found. |
210 | int wxMenu::FindItem (const wxString& itemString) const | |
7c78e7c7 | 211 | { |
01b2eeec KB |
212 | char buf1[200]; |
213 | char buf2[200]; | |
214 | wxStripMenuCodes ((char *)(const char *)itemString, buf1); | |
215 | ||
216 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
217 | { | |
218 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
219 | if (item->GetSubMenu()) | |
220 | { | |
221 | int ans = item->GetSubMenu()->FindItem(itemString); | |
222 | if (ans > -1) | |
223 | return ans; | |
224 | } | |
225 | if ( !item->IsSeparator() ) | |
226 | { | |
227 | wxStripMenuCodes((char *)item->GetName().c_str(), buf2); | |
228 | if (strcmp(buf1, buf2) == 0) | |
229 | return item->GetId(); | |
230 | } | |
231 | } | |
232 | ||
233 | return -1; | |
234 | } | |
7c78e7c7 | 235 | |
01b2eeec | 236 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const |
7c78e7c7 | 237 | { |
01b2eeec KB |
238 | if (itemMenu) |
239 | *itemMenu = NULL; | |
240 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
241 | { | |
242 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
243 | ||
244 | if (item->GetId() == itemId) | |
245 | { | |
246 | if (itemMenu) | |
247 | *itemMenu = (wxMenu *) this; | |
248 | return item; | |
249 | } | |
250 | ||
251 | if (item->GetSubMenu()) | |
252 | { | |
253 | wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu); | |
254 | if (ans) | |
255 | return ans; | |
256 | } | |
257 | } | |
258 | ||
259 | if (itemMenu) | |
260 | *itemMenu = NULL; | |
261 | return NULL; | |
262 | } | |
7c78e7c7 | 263 | |
01b2eeec KB |
264 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) |
265 | { | |
266 | wxMenuItem *item = FindItemForId (itemId); | |
267 | if (item) | |
268 | item->SetHelp(helpString); | |
7c78e7c7 RR |
269 | } |
270 | ||
01b2eeec | 271 | wxString wxMenu::GetHelpString (int itemId) const |
7c78e7c7 | 272 | { |
01b2eeec KB |
273 | wxMenuItem *item = FindItemForId (itemId); |
274 | wxString str(""); | |
275 | return (item == NULL) ? str : item->GetHelp(); | |
276 | } | |
7c78e7c7 | 277 | |
01b2eeec KB |
278 | void wxMenu::ProcessCommand(wxCommandEvent & event) |
279 | { | |
280 | bool processed = FALSE; | |
281 | ||
282 | // Try a callback | |
283 | if (m_callback) | |
284 | { | |
285 | (void) (*(m_callback)) (*this, event); | |
286 | processed = TRUE; | |
287 | } | |
288 | ||
289 | // Try the menu's event handler | |
290 | if ( !processed && GetEventHandler()) | |
291 | { | |
292 | processed = GetEventHandler()->ProcessEvent(event); | |
293 | } | |
294 | ||
295 | // Try the window the menu was popped up from (and up | |
296 | // through the hierarchy) | |
297 | if ( !processed && GetInvokingWindow()) | |
298 | processed = GetInvokingWindow()->ProcessEvent(event); | |
299 | } | |
300 | ||
301 | bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) | |
302 | { | |
303 | // TODO | |
304 | return FALSE; | |
7c78e7c7 RR |
305 | } |
306 | ||
01b2eeec KB |
307 | // Menu Bar |
308 | wxMenuBar::wxMenuBar() | |
7c78e7c7 | 309 | { |
01b2eeec KB |
310 | m_eventHandler = this; |
311 | m_menuCount = 0; | |
312 | m_menus = NULL; | |
313 | m_titles = NULL; | |
314 | m_menuBarFrame = NULL; | |
7c78e7c7 | 315 | |
01b2eeec | 316 | // TODO |
7c78e7c7 RR |
317 | } |
318 | ||
01b2eeec KB |
319 | wxMenuBar::wxMenuBar(int n, wxMenu *Mmnus[], const wxString titles[]) |
320 | { | |
321 | m_eventHandler = this; | |
322 | m_menuCount = n; | |
323 | m_menus = menus; | |
324 | m_titles = new wxString[n]; | |
325 | int i; | |
326 | for ( i = 0; i < n; i++ ) | |
327 | m_titles[i] = titles[i]; | |
328 | m_menuBarFrame = NULL; | |
329 | ||
330 | // TODO | |
331 | } | |
7c78e7c7 | 332 | |
01b2eeec | 333 | wxMenuBar::~wxMenuBar() |
7c78e7c7 | 334 | { |
01b2eeec KB |
335 | int i; |
336 | for (i = 0; i < m_menuCount; i++) | |
337 | { | |
338 | delete m_menus[i]; | |
339 | } | |
340 | delete[] m_menus; | |
341 | delete[] m_titles; | |
342 | ||
343 | // TODO | |
344 | } | |
7c78e7c7 | 345 | |
01b2eeec KB |
346 | // Must only be used AFTER menu has been attached to frame, |
347 | // otherwise use individual menus to enable/disable items | |
348 | void wxMenuBar::Enable(int id, bool flag) | |
349 | { | |
350 | wxMenu *itemMenu = NULL; | |
351 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
352 | if (!item) | |
353 | return; | |
354 | ||
355 | // TODO | |
356 | } | |
357 | ||
358 | void wxMenuBar::EnableTop(int pos, bool flag) | |
359 | { | |
360 | // TODO | |
361 | } | |
362 | ||
363 | // Must only be used AFTER menu has been attached to frame, | |
364 | // otherwise use individual menus | |
365 | void wxMenuBar::Check(int id, bool flag) | |
7c78e7c7 | 366 | { |
01b2eeec KB |
367 | wxMenu *itemMenu = NULL; |
368 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
369 | if (!item) | |
370 | return; | |
7c78e7c7 | 371 | |
01b2eeec KB |
372 | if (!item->IsCheckable()) |
373 | return ; | |
7c78e7c7 | 374 | |
01b2eeec KB |
375 | // TODO |
376 | } | |
377 | ||
378 | bool wxMenuBar::Checked(int id) const | |
7c78e7c7 | 379 | { |
01b2eeec KB |
380 | wxMenu *itemMenu = NULL; |
381 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
382 | if (!item) | |
383 | return FALSE; | |
384 | ||
385 | // TODO | |
386 | return FALSE; | |
387 | } | |
7c78e7c7 | 388 | |
01b2eeec | 389 | bool wxMenuBar::Enabled(int id) const |
7c78e7c7 | 390 | { |
01b2eeec KB |
391 | wxMenu *itemMenu = NULL; |
392 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
393 | if (!item) | |
394 | return FALSE; | |
395 | ||
396 | // TODO | |
397 | return FALSE ; | |
398 | } | |
7c78e7c7 | 399 | |
7c78e7c7 | 400 | |
01b2eeec | 401 | void wxMenuBar::SetLabel(int id, const wxString& label) |
7c78e7c7 | 402 | { |
01b2eeec KB |
403 | wxMenu *itemMenu = NULL; |
404 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
405 | ||
406 | if (!item) | |
407 | return; | |
7c78e7c7 | 408 | |
01b2eeec KB |
409 | // TODO |
410 | } | |
7c78e7c7 | 411 | |
01b2eeec KB |
412 | wxString wxMenuBar::GetLabel(int id) const |
413 | { | |
414 | wxMenu *itemMenu = NULL; | |
415 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
7c78e7c7 | 416 | |
01b2eeec KB |
417 | if (!item) |
418 | return wxString(""); | |
7c78e7c7 | 419 | |
01b2eeec KB |
420 | // TODO |
421 | return wxString("") ; | |
422 | } | |
423 | ||
424 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
7c78e7c7 | 425 | { |
01b2eeec KB |
426 | // TODO |
427 | } | |
7c78e7c7 | 428 | |
01b2eeec | 429 | wxString wxMenuBar::GetLabelTop(int pos) const |
7c78e7c7 | 430 | { |
01b2eeec KB |
431 | // TODO |
432 | return wxString(""); | |
433 | } | |
7c78e7c7 | 434 | |
01b2eeec | 435 | bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) |
7c78e7c7 | 436 | { |
01b2eeec KB |
437 | // TODO |
438 | return FALSE; | |
439 | } | |
7c78e7c7 | 440 | |
01b2eeec | 441 | bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title) |
7c78e7c7 | 442 | { |
01b2eeec | 443 | // TODO |
7c78e7c7 | 444 | return FALSE; |
01b2eeec | 445 | } |
7c78e7c7 | 446 | |
01b2eeec | 447 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) |
7c78e7c7 | 448 | { |
01b2eeec KB |
449 | if (!OnAppend(menu, title)) |
450 | return; | |
451 | ||
452 | m_menuCount ++; | |
453 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
454 | wxString *new_titles = new wxString[m_menuCount]; | |
455 | int i; | |
456 | ||
457 | for (i = 0; i < m_menuCount - 1; i++) | |
458 | { | |
459 | new_menus[i] = m_menus[i]; | |
460 | m_menus[i] = NULL; | |
461 | new_titles[i] = m_titles[i]; | |
462 | m_titles[i] = ""; | |
463 | } | |
464 | if (m_menus) | |
465 | { | |
466 | delete[]m_menus; | |
467 | delete[]m_titles; | |
468 | } | |
469 | m_menus = new_menus; | |
470 | m_titles = new_titles; | |
471 | ||
472 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
473 | m_titles[m_menuCount - 1] = title; | |
474 | ||
475 | // TODO | |
476 | } | |
7c78e7c7 | 477 | |
01b2eeec | 478 | void wxMenuBar::Delete(wxMenu * menu, int i) |
7c78e7c7 | 479 | { |
01b2eeec KB |
480 | int j; |
481 | int ii = (int) i; | |
482 | ||
483 | if (menu != 0) | |
484 | { | |
485 | for (ii = 0; ii < m_menuCount; ii++) | |
486 | { | |
487 | if (m_menus[ii] == menu) | |
488 | break; | |
489 | } | |
490 | if (ii >= m_menuCount) | |
491 | return; | |
492 | } else | |
493 | { | |
494 | if (ii < 0 || ii >= m_menuCount) | |
495 | return; | |
496 | menu = m_menus[ii]; | |
497 | } | |
498 | ||
499 | if (!OnDelete(menu, ii)) | |
500 | return; | |
501 | ||
502 | menu->SetParent(NULL); | |
503 | ||
504 | -- m_menuCount; | |
505 | for (j = ii; j < m_menuCount; j++) | |
506 | { | |
507 | m_menus[j] = m_menus[j + 1]; | |
508 | m_titles[j] = m_titles[j + 1]; | |
509 | } | |
510 | } | |
7c78e7c7 | 511 | |
01b2eeec KB |
512 | // Find the menu menuString, item itemString, and return the item id. |
513 | // Returns -1 if none found. | |
514 | int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const | |
515 | { | |
516 | char buf1[200]; | |
517 | char buf2[200]; | |
518 | wxStripMenuCodes ((char *)(const char *)menuString, buf1); | |
519 | int i; | |
520 | for (i = 0; i < m_menuCount; i++) | |
521 | { | |
522 | wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); | |
523 | if (strcmp (buf1, buf2) == 0) | |
524 | return m_menus[i]->FindItem (itemString); | |
525 | } | |
526 | return -1; | |
527 | } | |
7c78e7c7 | 528 | |
01b2eeec KB |
529 | wxMenuItem *wxMenuBar::FindItemForId (int Id, wxMenu ** itemMenu) const |
530 | { | |
531 | if (itemMenu) | |
532 | *itemMenu = NULL; | |
533 | ||
534 | wxMenuItem *item = NULL; | |
535 | int i; | |
536 | for (i = 0; i < m_menuCount; i++) | |
537 | if ((item = m_menus[i]->FindItemForId (Id, itemMenu))) | |
538 | return item; | |
539 | return NULL; | |
7c78e7c7 RR |
540 | } |
541 | ||
01b2eeec | 542 | void wxMenuBar::SetHelpString (int Id, const wxString& helpString) |
7c78e7c7 | 543 | { |
01b2eeec KB |
544 | int i; |
545 | for (i = 0; i < m_menuCount; i++) | |
546 | { | |
547 | if (m_menus[i]->FindItemForId (Id)) | |
548 | { | |
549 | m_menus[i]->SetHelpString (Id, helpString); | |
550 | return; | |
551 | } | |
552 | } | |
553 | } | |
7c78e7c7 | 554 | |
01b2eeec | 555 | wxString wxMenuBar::GetHelpString (int Id) const |
7c78e7c7 | 556 | { |
01b2eeec KB |
557 | int i; |
558 | for (i = 0; i < m_menuCount; i++) | |
559 | { | |
560 | if (m_menus[i]->FindItemForId (Id)) | |
561 | eturn wxString(m_menus[i]->GetHelpString (Id)); | |
562 | } | |
563 | return wxString(""); | |
564 | } | |
7c78e7c7 RR |
565 | |
566 |