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