]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | ||
13 | // ============================================================================ | |
14 | // headers & declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // wxWindows headers | |
18 | // ----------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menu.h" | |
22 | #pragma implementation "menuitem.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/menu.h" | |
26 | #include "wx/menuitem.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/utils.h" | |
50414e24 JS |
29 | #include "wx/app.h" |
30 | #include "wx/frame.h" | |
4bb6408c JS |
31 | |
32 | #include <Xm/Label.h> | |
33 | #include <Xm/LabelG.h> | |
34 | #include <Xm/CascadeBG.h> | |
35 | #include <Xm/CascadeB.h> | |
36 | #include <Xm/SeparatoG.h> | |
37 | #include <Xm/PushBG.h> | |
38 | #include <Xm/ToggleB.h> | |
39 | #include <Xm/ToggleBG.h> | |
40 | #include <Xm/RowColumn.h> | |
41 | ||
50414e24 JS |
42 | #include "wx/motif/private.h" |
43 | ||
4bb6408c JS |
44 | // other standard headers |
45 | // ---------------------- | |
46 | #include <string.h> | |
47 | ||
4bb6408c JS |
48 | #if !USE_SHARED_LIBRARY |
49 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
50 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
51 | #endif | |
52 | ||
53 | // ============================================================================ | |
54 | // implementation | |
55 | // ============================================================================ | |
56 | ||
57 | // Menus | |
58 | ||
59 | // Construct a menu with optional title (then use append) | |
60 | wxMenu::wxMenu(const wxString& title, const wxFunction func) | |
61 | { | |
62 | m_title = title; | |
63 | m_parent = (wxEvtHandler*) NULL; | |
64 | m_eventHandler = this; | |
65 | m_noItems = 0; | |
66 | m_menuBar = NULL; | |
67 | ||
68 | //// Motif-specific members | |
69 | m_numColumns = 1; | |
70 | m_menuWidget = (WXWidget) NULL; | |
71 | m_popupShell = (WXWidget) NULL; | |
72 | m_buttonWidget = (WXWidget) NULL; | |
73 | m_menuId = 0; | |
50414e24 | 74 | m_topLevelMenu = (wxMenu*) NULL; |
4bb6408c JS |
75 | m_ownedByMenuBar = FALSE; |
76 | m_menuParent = (wxMenu*) NULL; | |
3dd4e4e0 | 77 | m_clientData = (void*) NULL; |
4bb6408c JS |
78 | |
79 | if (m_title != "") | |
80 | { | |
50414e24 | 81 | Append(ID_SEPARATOR, m_title) ; |
4bb6408c JS |
82 | AppendSeparator() ; |
83 | } | |
84 | ||
85 | Callback(func); | |
4bb6408c JS |
86 | } |
87 | ||
88 | // The wxWindow destructor will take care of deleting the submenus. | |
89 | wxMenu::~wxMenu() | |
90 | { | |
50414e24 JS |
91 | if (m_menuWidget) |
92 | { | |
93 | if (m_menuParent) | |
94 | DestroyMenu(TRUE); | |
95 | else | |
96 | DestroyMenu(FALSE); | |
97 | } | |
98 | ||
99 | // Not sure if this is right | |
100 | if (m_menuParent && m_menuBar) | |
101 | { | |
102 | m_menuParent = NULL; | |
103 | // m_menuBar = NULL; | |
104 | } | |
4bb6408c JS |
105 | |
106 | wxNode *node = m_menuItems.First(); | |
107 | while (node) | |
108 | { | |
109 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
110 | ||
50414e24 | 111 | /* |
4bb6408c JS |
112 | if (item->GetSubMenu()) |
113 | item->DeleteSubMenu(); | |
50414e24 | 114 | */ |
4bb6408c JS |
115 | |
116 | wxNode *next = node->Next(); | |
117 | delete item; | |
118 | delete node; | |
119 | node = next; | |
120 | } | |
121 | } | |
122 | ||
123 | void wxMenu::Break() | |
124 | { | |
50414e24 | 125 | m_numColumns ++; |
4bb6408c JS |
126 | } |
127 | ||
128 | // function appends a new item or submenu to the menu | |
129 | void wxMenu::Append(wxMenuItem *pItem) | |
130 | { | |
4bb6408c JS |
131 | wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" ); |
132 | ||
133 | m_menuItems.Append(pItem); | |
134 | ||
50414e24 JS |
135 | if (m_menuWidget) |
136 | pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append | |
137 | ||
4bb6408c JS |
138 | m_noItems++; |
139 | } | |
140 | ||
141 | void wxMenu::AppendSeparator() | |
142 | { | |
4bb6408c JS |
143 | Append(new wxMenuItem(this, ID_SEPARATOR)); |
144 | } | |
145 | ||
146 | // Pullright item | |
50414e24 JS |
147 | // N.B.: difference between old and new code. |
148 | // Old code stores subMenu in 'children' for later deletion, | |
149 | // as well as in m_menuItems, whereas we only store it in | |
150 | // m_menuItems here. What implications does this have? | |
151 | ||
152 | void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu, | |
4bb6408c JS |
153 | const wxString& helpString) |
154 | { | |
50414e24 JS |
155 | Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu)); |
156 | ||
157 | subMenu->m_topLevelMenu = m_topLevelMenu; | |
4bb6408c JS |
158 | } |
159 | ||
160 | // Ordinary menu item | |
50414e24 | 161 | void wxMenu::Append(int id, const wxString& label, |
4bb6408c JS |
162 | const wxString& helpString, bool checkable) |
163 | { | |
164 | // 'checkable' parameter is useless for Windows. | |
50414e24 | 165 | Append(new wxMenuItem(this, id, label, helpString, checkable)); |
4bb6408c JS |
166 | } |
167 | ||
168 | void wxMenu::Delete(int id) | |
169 | { | |
170 | wxNode *node; | |
171 | wxMenuItem *item; | |
172 | int pos; | |
173 | ||
50414e24 JS |
174 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) |
175 | { | |
4bb6408c JS |
176 | item = (wxMenuItem *)node->Data(); |
177 | if (item->GetId() == id) | |
178 | break; | |
179 | } | |
180 | ||
181 | if (!node) | |
182 | return; | |
183 | ||
50414e24 JS |
184 | item->DestroyItem(TRUE); |
185 | ||
186 | // See also old code - don't know if this is needed (seems redundant). | |
187 | /* | |
188 | if (item->GetSubMenu()) { | |
189 | item->subMenu->top_level_menu = item->GetSubMenu(); | |
190 | item->subMenu->window_parent = NULL; | |
191 | children->DeleteObject(item->GetSubMenu()); | |
192 | } | |
193 | */ | |
194 | ||
4bb6408c JS |
195 | m_menuItems.DeleteNode(node); |
196 | delete item; | |
4bb6408c JS |
197 | } |
198 | ||
50414e24 | 199 | void wxMenu::Enable(int id, bool flag) |
4bb6408c | 200 | { |
50414e24 | 201 | wxMenuItem *item = FindItemForId(id); |
4bb6408c JS |
202 | wxCHECK_RET( item != NULL, "can't enable non-existing menu item" ); |
203 | ||
50414e24 | 204 | item->Enable(flag); |
4bb6408c JS |
205 | } |
206 | ||
207 | bool wxMenu::Enabled(int Id) const | |
208 | { | |
209 | wxMenuItem *item = FindItemForId(Id); | |
210 | wxCHECK( item != NULL, FALSE ); | |
211 | ||
212 | return item->IsEnabled(); | |
213 | } | |
214 | ||
215 | void wxMenu::Check(int Id, bool Flag) | |
216 | { | |
217 | wxMenuItem *item = FindItemForId(Id); | |
218 | wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" ); | |
219 | ||
220 | item->Check(Flag); | |
221 | } | |
222 | ||
50414e24 | 223 | bool wxMenu::Checked(int id) const |
4bb6408c | 224 | { |
50414e24 | 225 | wxMenuItem *item = FindItemForId(id); |
4bb6408c JS |
226 | wxCHECK( item != NULL, FALSE ); |
227 | ||
228 | return item->IsChecked(); | |
229 | } | |
230 | ||
231 | void wxMenu::SetTitle(const wxString& label) | |
232 | { | |
233 | m_title = label ; | |
50414e24 JS |
234 | |
235 | wxNode *node = m_menuItems.First (); | |
236 | if (!node) | |
237 | return; | |
238 | ||
239 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
240 | Widget widget = (Widget) item->GetButtonWidget(); | |
241 | if (!widget) | |
242 | return; | |
243 | ||
244 | XmString title_str = XmStringCreateSimple ((char*) (const char*) label); | |
245 | XtVaSetValues (widget, | |
246 | XmNlabelString, title_str, | |
247 | NULL); | |
248 | // TODO: should we delete title_str now? | |
4bb6408c JS |
249 | } |
250 | ||
251 | const wxString wxMenu::GetTitle() const | |
252 | { | |
253 | return m_title; | |
254 | } | |
255 | ||
256 | void wxMenu::SetLabel(int id, const wxString& label) | |
257 | { | |
50414e24 JS |
258 | wxMenuItem *item = FindItemForId(id); |
259 | if (item == (wxMenuItem*) NULL) | |
260 | return; | |
4bb6408c | 261 | |
50414e24 | 262 | item->SetLabel(label); |
4bb6408c JS |
263 | } |
264 | ||
50414e24 | 265 | wxString wxMenu::GetLabel(int id) const |
4bb6408c | 266 | { |
50414e24 JS |
267 | wxMenuItem *it = NULL; |
268 | WXWidget w = FindMenuItem (id, &it); | |
269 | if (w) | |
270 | { | |
271 | XmString text; | |
272 | char *s; | |
273 | XtVaGetValues ((Widget) w, | |
274 | XmNlabelString, &text, | |
275 | NULL); | |
276 | ||
277 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
278 | { | |
279 | wxString str(s); | |
280 | XtFree (s); | |
281 | return str; | |
282 | } | |
283 | else | |
284 | { | |
285 | XmStringFree (text); | |
286 | return wxEmptyString; | |
287 | } | |
288 | } | |
289 | else | |
290 | return wxEmptyString; | |
4bb6408c JS |
291 | } |
292 | ||
293 | // Finds the item id matching the given string, -1 if not found. | |
294 | int wxMenu::FindItem (const wxString& itemString) const | |
295 | { | |
296 | char buf1[200]; | |
297 | char buf2[200]; | |
298 | wxStripMenuCodes ((char *)(const char *)itemString, buf1); | |
299 | ||
300 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
301 | { | |
302 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
303 | if (item->GetSubMenu()) | |
304 | { | |
305 | int ans = item->GetSubMenu()->FindItem(itemString); | |
306 | if (ans > -1) | |
307 | return ans; | |
308 | } | |
309 | if ( !item->IsSeparator() ) | |
310 | { | |
311 | wxStripMenuCodes((char *)item->GetName().c_str(), buf2); | |
312 | if (strcmp(buf1, buf2) == 0) | |
313 | return item->GetId(); | |
314 | } | |
315 | } | |
316 | ||
317 | return -1; | |
318 | } | |
319 | ||
320 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const | |
321 | { | |
322 | if (itemMenu) | |
323 | *itemMenu = NULL; | |
324 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
325 | { | |
326 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
327 | ||
328 | if (item->GetId() == itemId) | |
329 | { | |
330 | if (itemMenu) | |
331 | *itemMenu = (wxMenu *) this; | |
332 | return item; | |
333 | } | |
334 | ||
335 | if (item->GetSubMenu()) | |
336 | { | |
337 | wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu); | |
338 | if (ans) | |
339 | return ans; | |
340 | } | |
341 | } | |
342 | ||
343 | if (itemMenu) | |
344 | *itemMenu = NULL; | |
345 | return NULL; | |
346 | } | |
347 | ||
348 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) | |
349 | { | |
350 | wxMenuItem *item = FindItemForId (itemId); | |
351 | if (item) | |
352 | item->SetHelp(helpString); | |
353 | } | |
354 | ||
355 | wxString wxMenu::GetHelpString (int itemId) const | |
356 | { | |
357 | wxMenuItem *item = FindItemForId (itemId); | |
358 | wxString str(""); | |
359 | return (item == NULL) ? str : item->GetHelp(); | |
360 | } | |
361 | ||
362 | void wxMenu::ProcessCommand(wxCommandEvent & event) | |
363 | { | |
364 | bool processed = FALSE; | |
365 | ||
366 | // Try a callback | |
367 | if (m_callback) | |
368 | { | |
369 | (void) (*(m_callback)) (*this, event); | |
370 | processed = TRUE; | |
371 | } | |
372 | ||
373 | // Try the menu's event handler | |
374 | if ( !processed && GetEventHandler()) | |
375 | { | |
376 | processed = GetEventHandler()->ProcessEvent(event); | |
377 | } | |
378 | /* TODO | |
379 | // Try the window the menu was popped up from (and up | |
380 | // through the hierarchy) | |
381 | if ( !processed && GetInvokingWindow()) | |
382 | processed = GetInvokingWindow()->ProcessEvent(event); | |
383 | */ | |
384 | } | |
385 | ||
386 | bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) | |
387 | { | |
50414e24 JS |
388 | Widget widget = (Widget) GetMainWidget(); |
389 | ||
390 | /* The menuId field seems to be usused, so we'll use it to | |
391 | indicate whether a menu is popped up or not: | |
392 | 0: Not currently created as a popup | |
393 | -1: Created as a popup, but not active | |
394 | 1: Active popup. | |
395 | */ | |
396 | ||
397 | if (menu->GetParent() && (menu->GetId() != -1)) | |
4bb6408c | 398 | return FALSE; |
50414e24 JS |
399 | |
400 | if (menu->GetMainWidget()) { | |
401 | menu->DestroyMenu(TRUE); | |
402 | } | |
403 | ||
404 | wxWindow *parent = this; | |
405 | ||
406 | menu->SetId(1); /* Mark as popped-up */ | |
407 | menu->CreateMenu(NULL, widget, menu); | |
408 | // menu->SetParent(parent); | |
409 | // parent->children->Append(menu); // Store menu for later deletion | |
410 | ||
411 | Widget menuWidget = (Widget) menu->GetMainWidget(); | |
412 | ||
413 | int rootX = 0; | |
414 | int rootY = 0; | |
415 | ||
416 | int deviceX = x; | |
417 | int deviceY = y; | |
418 | /* | |
419 | if (this->IsKindOf(CLASSINFO(wxCanvas))) | |
420 | { | |
421 | wxCanvas *canvas = (wxCanvas *) this; | |
422 | deviceX = canvas->GetDC ()->LogicalToDeviceX (x); | |
423 | deviceY = canvas->GetDC ()->LogicalToDeviceY (y); | |
424 | } | |
425 | */ | |
426 | ||
427 | Display *display = XtDisplay (widget); | |
428 | Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget)); | |
429 | Window thisWindow = XtWindow (widget); | |
430 | Window childWindow; | |
431 | XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY, | |
432 | &rootX, &rootY, &childWindow); | |
433 | ||
434 | XButtonPressedEvent event; | |
435 | event.type = ButtonPress; | |
436 | event.button = 1; | |
437 | ||
438 | event.x = deviceX; | |
439 | event.y = deviceY; | |
440 | ||
441 | event.x_root = rootX; | |
442 | event.y_root = rootY; | |
443 | ||
444 | XmMenuPosition (menuWidget, &event); | |
445 | XtManageChild (menuWidget); | |
446 | ||
447 | return TRUE; | |
4bb6408c JS |
448 | } |
449 | ||
450 | // Menu Bar | |
451 | wxMenuBar::wxMenuBar() | |
452 | { | |
453 | m_eventHandler = this; | |
454 | m_menuCount = 0; | |
455 | m_menus = NULL; | |
456 | m_titles = NULL; | |
457 | m_menuBarFrame = NULL; | |
4bb6408c JS |
458 | } |
459 | ||
460 | wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[]) | |
461 | { | |
462 | m_eventHandler = this; | |
463 | m_menuCount = n; | |
464 | m_menus = menus; | |
465 | m_titles = new wxString[n]; | |
466 | int i; | |
467 | for ( i = 0; i < n; i++ ) | |
468 | m_titles[i] = titles[i]; | |
469 | m_menuBarFrame = NULL; | |
4bb6408c JS |
470 | } |
471 | ||
472 | wxMenuBar::~wxMenuBar() | |
473 | { | |
474 | int i; | |
475 | for (i = 0; i < m_menuCount; i++) | |
476 | { | |
477 | delete m_menus[i]; | |
478 | } | |
479 | delete[] m_menus; | |
480 | delete[] m_titles; | |
4bb6408c JS |
481 | } |
482 | ||
483 | // Must only be used AFTER menu has been attached to frame, | |
484 | // otherwise use individual menus to enable/disable items | |
485 | void wxMenuBar::Enable(int id, bool flag) | |
486 | { | |
487 | wxMenu *itemMenu = NULL; | |
488 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
489 | if (!item) | |
490 | return; | |
50414e24 | 491 | item->Enable(flag); |
4bb6408c JS |
492 | } |
493 | ||
494 | void wxMenuBar::EnableTop(int pos, bool flag) | |
495 | { | |
496 | // TODO | |
497 | } | |
498 | ||
499 | // Must only be used AFTER menu has been attached to frame, | |
500 | // otherwise use individual menus | |
501 | void wxMenuBar::Check(int id, bool flag) | |
502 | { | |
503 | wxMenu *itemMenu = NULL; | |
504 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
505 | if (!item) | |
506 | return; | |
507 | ||
508 | if (!item->IsCheckable()) | |
509 | return ; | |
510 | ||
50414e24 | 511 | item->Check(flag); |
4bb6408c JS |
512 | } |
513 | ||
514 | bool wxMenuBar::Checked(int id) const | |
515 | { | |
516 | wxMenu *itemMenu = NULL; | |
517 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
518 | if (!item) | |
519 | return FALSE; | |
520 | ||
50414e24 | 521 | return item->IsChecked(); |
4bb6408c JS |
522 | } |
523 | ||
524 | bool wxMenuBar::Enabled(int id) const | |
525 | { | |
526 | wxMenu *itemMenu = NULL; | |
527 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
528 | if (!item) | |
529 | return FALSE; | |
530 | ||
50414e24 | 531 | return item->IsEnabled(); |
4bb6408c JS |
532 | } |
533 | ||
4bb6408c JS |
534 | void wxMenuBar::SetLabel(int id, const wxString& label) |
535 | { | |
536 | wxMenu *itemMenu = NULL; | |
537 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
538 | ||
539 | if (!item) | |
540 | return; | |
541 | ||
50414e24 | 542 | item->SetLabel(label); |
4bb6408c JS |
543 | } |
544 | ||
545 | wxString wxMenuBar::GetLabel(int id) const | |
546 | { | |
547 | wxMenu *itemMenu = NULL; | |
548 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
549 | ||
550 | if (!item) | |
551 | return wxString(""); | |
552 | ||
50414e24 | 553 | return item->GetLabel(); |
4bb6408c JS |
554 | } |
555 | ||
556 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
557 | { | |
50414e24 JS |
558 | wxASSERT( (pos < m_menuCount) ); |
559 | ||
560 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
561 | if (w) | |
562 | { | |
563 | XmString label_str = XmStringCreateSimple ((char*) (const char*) label); | |
564 | XtVaSetValues (w, | |
565 | XmNlabelString, label_str, | |
566 | NULL); | |
567 | XmStringFree (label_str); | |
568 | } | |
4bb6408c JS |
569 | } |
570 | ||
571 | wxString wxMenuBar::GetLabelTop(int pos) const | |
572 | { | |
50414e24 JS |
573 | wxASSERT( (pos < m_menuCount) ); |
574 | ||
575 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
576 | if (w) | |
577 | { | |
578 | XmString text; | |
579 | char *s; | |
580 | XtVaGetValues (w, | |
581 | XmNlabelString, &text, | |
582 | NULL); | |
583 | ||
584 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
585 | { | |
586 | wxString str(s); | |
587 | XtFree (s); | |
588 | return str; | |
589 | } | |
590 | else | |
591 | { | |
592 | return wxEmptyString; | |
593 | } | |
594 | } | |
595 | else | |
596 | return wxEmptyString; | |
597 | ||
4bb6408c JS |
598 | } |
599 | ||
50414e24 | 600 | bool wxMenuBar::OnDelete(wxMenu *menu, int pos) |
4bb6408c | 601 | { |
50414e24 JS |
602 | // Only applies to dynamic deletion (when set in frame) |
603 | if (!m_menuBarFrame) | |
604 | return TRUE; | |
605 | ||
606 | menu->DestroyMenu(TRUE); | |
607 | return TRUE; | |
4bb6408c JS |
608 | } |
609 | ||
50414e24 | 610 | bool wxMenuBar::OnAppend(wxMenu *menu, const char *title) |
4bb6408c | 611 | { |
50414e24 JS |
612 | // Only applies to dynamic append (when set in frame) |
613 | if (!m_menuBarFrame) | |
614 | return TRUE; | |
615 | ||
616 | // Probably should be an assert here | |
617 | if (menu->GetParent()) | |
618 | return FALSE; | |
619 | ||
620 | // Has already been appended | |
621 | if (menu->GetButtonWidget()) | |
622 | return FALSE; | |
623 | ||
624 | WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE); | |
625 | menu->SetButtonWidget(w); | |
626 | ||
627 | return TRUE; | |
4bb6408c JS |
628 | } |
629 | ||
630 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) | |
631 | { | |
632 | if (!OnAppend(menu, title)) | |
633 | return; | |
634 | ||
635 | m_menuCount ++; | |
636 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
637 | wxString *new_titles = new wxString[m_menuCount]; | |
638 | int i; | |
639 | ||
640 | for (i = 0; i < m_menuCount - 1; i++) | |
641 | { | |
642 | new_menus[i] = m_menus[i]; | |
643 | m_menus[i] = NULL; | |
644 | new_titles[i] = m_titles[i]; | |
645 | m_titles[i] = ""; | |
646 | } | |
647 | if (m_menus) | |
648 | { | |
649 | delete[]m_menus; | |
650 | delete[]m_titles; | |
651 | } | |
652 | m_menus = new_menus; | |
653 | m_titles = new_titles; | |
654 | ||
655 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
656 | m_titles[m_menuCount - 1] = title; | |
657 | ||
50414e24 JS |
658 | menu->SetMenuBar(this); |
659 | menu->SetParent(this); | |
4bb6408c JS |
660 | } |
661 | ||
662 | void wxMenuBar::Delete(wxMenu * menu, int i) | |
663 | { | |
664 | int j; | |
665 | int ii = (int) i; | |
666 | ||
667 | if (menu != 0) | |
668 | { | |
669 | for (ii = 0; ii < m_menuCount; ii++) | |
670 | { | |
671 | if (m_menus[ii] == menu) | |
672 | break; | |
673 | } | |
674 | if (ii >= m_menuCount) | |
675 | return; | |
676 | } else | |
677 | { | |
678 | if (ii < 0 || ii >= m_menuCount) | |
679 | return; | |
680 | menu = m_menus[ii]; | |
681 | } | |
682 | ||
683 | if (!OnDelete(menu, ii)) | |
684 | return; | |
685 | ||
50414e24 | 686 | menu->SetParent((wxEvtHandler*) NULL); |
4bb6408c JS |
687 | |
688 | -- m_menuCount; | |
689 | for (j = ii; j < m_menuCount; j++) | |
690 | { | |
691 | m_menus[j] = m_menus[j + 1]; | |
692 | m_titles[j] = m_titles[j + 1]; | |
693 | } | |
694 | } | |
695 | ||
696 | // Find the menu menuString, item itemString, and return the item id. | |
697 | // Returns -1 if none found. | |
698 | int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const | |
699 | { | |
700 | char buf1[200]; | |
701 | char buf2[200]; | |
702 | wxStripMenuCodes ((char *)(const char *)menuString, buf1); | |
703 | int i; | |
704 | for (i = 0; i < m_menuCount; i++) | |
705 | { | |
706 | wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); | |
707 | if (strcmp (buf1, buf2) == 0) | |
708 | return m_menus[i]->FindItem (itemString); | |
709 | } | |
710 | return -1; | |
711 | } | |
712 | ||
50414e24 | 713 | wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const |
4bb6408c JS |
714 | { |
715 | if (itemMenu) | |
716 | *itemMenu = NULL; | |
717 | ||
718 | wxMenuItem *item = NULL; | |
719 | int i; | |
720 | for (i = 0; i < m_menuCount; i++) | |
50414e24 | 721 | if ((item = m_menus[i]->FindItemForId (id, itemMenu))) |
4bb6408c JS |
722 | return item; |
723 | return NULL; | |
724 | } | |
725 | ||
50414e24 | 726 | void wxMenuBar::SetHelpString (int id, const wxString& helpString) |
4bb6408c JS |
727 | { |
728 | int i; | |
729 | for (i = 0; i < m_menuCount; i++) | |
730 | { | |
50414e24 | 731 | if (m_menus[i]->FindItemForId (id)) |
4bb6408c | 732 | { |
50414e24 | 733 | m_menus[i]->SetHelpString (id, helpString); |
4bb6408c JS |
734 | return; |
735 | } | |
736 | } | |
737 | } | |
738 | ||
50414e24 | 739 | wxString wxMenuBar::GetHelpString (int id) const |
4bb6408c JS |
740 | { |
741 | int i; | |
742 | for (i = 0; i < m_menuCount; i++) | |
743 | { | |
50414e24 JS |
744 | if (m_menus[i]->FindItemForId (id)) |
745 | return wxString(m_menus[i]->GetHelpString (id)); | |
4bb6408c JS |
746 | } |
747 | return wxString(""); | |
748 | } | |
749 | ||
50414e24 JS |
750 | //// Motif-specific |
751 | ||
752 | extern wxApp *wxTheApp; | |
753 | static XtWorkProcId WorkProcMenuId; | |
754 | ||
755 | /* Since PopupMenu under Motif stills grab right mouse button events | |
756 | * after it was closed, we need to delete the associated widgets to | |
757 | * allow next PopUpMenu to appear... | |
758 | */ | |
759 | ||
760 | int PostDeletionOfMenu( XtPointer* clientData ) | |
761 | { | |
762 | XtRemoveWorkProc(WorkProcMenuId); | |
763 | wxMenu *menu = (wxMenu *)clientData; | |
764 | ||
765 | if (menu->GetMainWidget()) { | |
766 | wxList& list = menu->GetParent()->GetItems(); | |
767 | list.DeleteObject(menu); | |
768 | menu->DestroyMenu(TRUE); | |
769 | } | |
770 | /* Mark as no longer popped up */ | |
771 | menu->m_menuId = -1; | |
772 | return TRUE; | |
773 | } | |
774 | ||
775 | void | |
776 | wxMenuPopdownCallback(Widget w, XtPointer clientData, | |
777 | XtPointer ptr) | |
778 | { | |
779 | wxMenu *menu = (wxMenu *)clientData; | |
780 | ||
781 | // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr> | |
782 | /* Since Callbacks of MenuItems are not yet processed, we put a | |
783 | * background job which will be done when system will be idle. | |
784 | * What awful hack!! :( | |
785 | */ | |
786 | ||
787 | WorkProcMenuId = XtAppAddWorkProc( | |
788 | (XtAppContext) wxTheApp->GetAppContext(), | |
789 | (XtWorkProc) PostDeletionOfMenu, | |
790 | (XtPointer) menu ); | |
791 | // Apparently not found in Motif headers | |
792 | // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL ); | |
793 | } | |
794 | ||
795 | /* | |
796 | * Create a popup or pulldown menu. | |
797 | * Submenus of a popup will be pulldown. | |
798 | * | |
799 | */ | |
800 | ||
801 | WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown) | |
802 | { | |
803 | Widget menu = (Widget) 0; | |
804 | Widget buttonWidget = (Widget) 0; | |
805 | Arg args[5]; | |
806 | XtSetArg (args[0], XmNnumColumns, m_numColumns); | |
807 | XtSetArg (args[1], XmNpacking, XmPACK_COLUMN); | |
808 | ||
809 | if (!pullDown) | |
810 | { | |
811 | menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2); | |
812 | XtAddCallback(menu, | |
813 | XmNunmapCallback, | |
814 | (XtCallbackProc)wxMenuPopdownCallback, | |
815 | (XtPointer)this); | |
816 | } | |
817 | else | |
818 | { | |
819 | char mnem = wxFindMnemonic (title); | |
820 | wxStripMenuCodes ((char*) (const char*) title, wxBuffer); | |
821 | ||
822 | menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2); | |
823 | ||
824 | XmString label_str = XmStringCreateSimple (wxBuffer); | |
825 | buttonWidget = XtVaCreateManagedWidget (wxBuffer, | |
47d67540 | 826 | #if wxUSE_GADGETS |
50414e24 JS |
827 | xmCascadeButtonGadgetClass, (Widget) parent, |
828 | #else | |
829 | xmCascadeButtonWidgetClass, (Widget) parent, | |
830 | #endif | |
831 | XmNlabelString, label_str, | |
832 | XmNsubMenuId, menu, | |
833 | NULL); | |
834 | ||
835 | if (mnem != 0) | |
836 | XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL); | |
837 | ||
838 | XmStringFree (label_str); | |
839 | } | |
840 | ||
841 | m_menuWidget = (WXWidget) menu; | |
842 | ||
843 | m_menuBar = menuBar; | |
844 | m_topLevelMenu = topMenu; | |
845 | ||
846 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
847 | { | |
848 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
849 | item->CreateItem (menu, menuBar, topMenu); | |
850 | } | |
851 | ||
852 | return buttonWidget; | |
853 | } | |
854 | ||
855 | // Destroys the Motif implementation of the menu, | |
856 | // but maintains the wxWindows data structures so we can | |
857 | // do a CreateMenu again. | |
858 | void wxMenu::DestroyMenu (bool full) | |
859 | { | |
860 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
861 | { | |
862 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
863 | item->SetMenuBar((wxMenuBar*) NULL); | |
864 | ||
865 | item->DestroyItem(full); | |
866 | } // for() | |
867 | ||
868 | if (m_buttonWidget) | |
869 | { | |
870 | if (full) | |
871 | { | |
872 | XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL); | |
873 | XtDestroyWidget ((Widget) m_buttonWidget); | |
874 | m_buttonWidget = (WXWidget) 0; | |
875 | } | |
876 | } | |
877 | if (m_menuWidget && full) | |
878 | { | |
879 | XtDestroyWidget((Widget) m_menuWidget); | |
880 | m_menuWidget = (WXWidget) NULL; | |
881 | } | |
882 | } | |
883 | ||
884 | WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const | |
885 | { | |
886 | if (id == m_menuId) | |
887 | { | |
888 | if (it) | |
889 | *it = (wxMenuItem*) NULL; | |
890 | return m_buttonWidget; | |
891 | } | |
4bb6408c | 892 | |
50414e24 JS |
893 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) |
894 | { | |
895 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
896 | if (item->GetId() == id) | |
897 | { | |
898 | if (it) | |
899 | *it = item; | |
900 | return item->GetButtonWidget(); | |
901 | } | |
902 | ||
903 | if (item->GetSubMenu()) | |
904 | { | |
905 | WXWidget w = item->GetSubMenu()->FindMenuItem (id, it); | |
906 | if (w) | |
907 | { | |
908 | return w; | |
909 | } | |
910 | } | |
911 | } // for() | |
912 | ||
913 | if (it) | |
914 | *it = (wxMenuItem*) NULL; | |
915 | return (WXWidget) NULL; | |
916 | } |