]>
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; | |
621793f4 | 458 | m_mainWidget = (WXWidget) NULL; |
4bb6408c JS |
459 | } |
460 | ||
461 | wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[]) | |
462 | { | |
463 | m_eventHandler = this; | |
464 | m_menuCount = n; | |
465 | m_menus = menus; | |
466 | m_titles = new wxString[n]; | |
467 | int i; | |
468 | for ( i = 0; i < n; i++ ) | |
469 | m_titles[i] = titles[i]; | |
470 | m_menuBarFrame = NULL; | |
4bb6408c JS |
471 | } |
472 | ||
473 | wxMenuBar::~wxMenuBar() | |
474 | { | |
475 | int i; | |
476 | for (i = 0; i < m_menuCount; i++) | |
477 | { | |
478 | delete m_menus[i]; | |
479 | } | |
480 | delete[] m_menus; | |
481 | delete[] m_titles; | |
4bb6408c JS |
482 | } |
483 | ||
484 | // Must only be used AFTER menu has been attached to frame, | |
485 | // otherwise use individual menus to enable/disable items | |
486 | void wxMenuBar::Enable(int id, bool flag) | |
487 | { | |
488 | wxMenu *itemMenu = NULL; | |
489 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
490 | if (!item) | |
491 | return; | |
50414e24 | 492 | item->Enable(flag); |
4bb6408c JS |
493 | } |
494 | ||
495 | void wxMenuBar::EnableTop(int pos, bool flag) | |
496 | { | |
497 | // TODO | |
498 | } | |
499 | ||
500 | // Must only be used AFTER menu has been attached to frame, | |
501 | // otherwise use individual menus | |
502 | void wxMenuBar::Check(int id, bool flag) | |
503 | { | |
504 | wxMenu *itemMenu = NULL; | |
505 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
506 | if (!item) | |
507 | return; | |
508 | ||
509 | if (!item->IsCheckable()) | |
510 | return ; | |
511 | ||
50414e24 | 512 | item->Check(flag); |
4bb6408c JS |
513 | } |
514 | ||
515 | bool wxMenuBar::Checked(int id) const | |
516 | { | |
517 | wxMenu *itemMenu = NULL; | |
518 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
519 | if (!item) | |
520 | return FALSE; | |
521 | ||
50414e24 | 522 | return item->IsChecked(); |
4bb6408c JS |
523 | } |
524 | ||
525 | bool wxMenuBar::Enabled(int id) const | |
526 | { | |
527 | wxMenu *itemMenu = NULL; | |
528 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
529 | if (!item) | |
530 | return FALSE; | |
531 | ||
50414e24 | 532 | return item->IsEnabled(); |
4bb6408c JS |
533 | } |
534 | ||
4bb6408c JS |
535 | void wxMenuBar::SetLabel(int id, const wxString& label) |
536 | { | |
537 | wxMenu *itemMenu = NULL; | |
538 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
539 | ||
540 | if (!item) | |
541 | return; | |
542 | ||
50414e24 | 543 | item->SetLabel(label); |
4bb6408c JS |
544 | } |
545 | ||
546 | wxString wxMenuBar::GetLabel(int id) const | |
547 | { | |
548 | wxMenu *itemMenu = NULL; | |
549 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
550 | ||
551 | if (!item) | |
552 | return wxString(""); | |
553 | ||
50414e24 | 554 | return item->GetLabel(); |
4bb6408c JS |
555 | } |
556 | ||
557 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
558 | { | |
50414e24 JS |
559 | wxASSERT( (pos < m_menuCount) ); |
560 | ||
561 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
562 | if (w) | |
563 | { | |
564 | XmString label_str = XmStringCreateSimple ((char*) (const char*) label); | |
565 | XtVaSetValues (w, | |
566 | XmNlabelString, label_str, | |
567 | NULL); | |
568 | XmStringFree (label_str); | |
569 | } | |
4bb6408c JS |
570 | } |
571 | ||
572 | wxString wxMenuBar::GetLabelTop(int pos) const | |
573 | { | |
50414e24 JS |
574 | wxASSERT( (pos < m_menuCount) ); |
575 | ||
576 | Widget w = (Widget) m_menus[pos]->GetButtonWidget(); | |
577 | if (w) | |
578 | { | |
579 | XmString text; | |
580 | char *s; | |
581 | XtVaGetValues (w, | |
582 | XmNlabelString, &text, | |
583 | NULL); | |
584 | ||
585 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
586 | { | |
587 | wxString str(s); | |
588 | XtFree (s); | |
589 | return str; | |
590 | } | |
591 | else | |
592 | { | |
593 | return wxEmptyString; | |
594 | } | |
595 | } | |
596 | else | |
597 | return wxEmptyString; | |
598 | ||
4bb6408c JS |
599 | } |
600 | ||
50414e24 | 601 | bool wxMenuBar::OnDelete(wxMenu *menu, int pos) |
4bb6408c | 602 | { |
50414e24 JS |
603 | // Only applies to dynamic deletion (when set in frame) |
604 | if (!m_menuBarFrame) | |
605 | return TRUE; | |
606 | ||
607 | menu->DestroyMenu(TRUE); | |
608 | return TRUE; | |
4bb6408c JS |
609 | } |
610 | ||
50414e24 | 611 | bool wxMenuBar::OnAppend(wxMenu *menu, const char *title) |
4bb6408c | 612 | { |
50414e24 JS |
613 | // Only applies to dynamic append (when set in frame) |
614 | if (!m_menuBarFrame) | |
615 | return TRUE; | |
616 | ||
617 | // Probably should be an assert here | |
618 | if (menu->GetParent()) | |
619 | return FALSE; | |
620 | ||
621 | // Has already been appended | |
622 | if (menu->GetButtonWidget()) | |
623 | return FALSE; | |
624 | ||
625 | WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE); | |
626 | menu->SetButtonWidget(w); | |
627 | ||
628 | return TRUE; | |
4bb6408c JS |
629 | } |
630 | ||
631 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) | |
632 | { | |
633 | if (!OnAppend(menu, title)) | |
634 | return; | |
635 | ||
636 | m_menuCount ++; | |
637 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
638 | wxString *new_titles = new wxString[m_menuCount]; | |
639 | int i; | |
640 | ||
641 | for (i = 0; i < m_menuCount - 1; i++) | |
642 | { | |
643 | new_menus[i] = m_menus[i]; | |
644 | m_menus[i] = NULL; | |
645 | new_titles[i] = m_titles[i]; | |
646 | m_titles[i] = ""; | |
647 | } | |
648 | if (m_menus) | |
649 | { | |
650 | delete[]m_menus; | |
651 | delete[]m_titles; | |
652 | } | |
653 | m_menus = new_menus; | |
654 | m_titles = new_titles; | |
655 | ||
656 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
657 | m_titles[m_menuCount - 1] = title; | |
658 | ||
50414e24 JS |
659 | menu->SetMenuBar(this); |
660 | menu->SetParent(this); | |
4bb6408c JS |
661 | } |
662 | ||
663 | void wxMenuBar::Delete(wxMenu * menu, int i) | |
664 | { | |
665 | int j; | |
666 | int ii = (int) i; | |
667 | ||
668 | if (menu != 0) | |
669 | { | |
670 | for (ii = 0; ii < m_menuCount; ii++) | |
671 | { | |
672 | if (m_menus[ii] == menu) | |
673 | break; | |
674 | } | |
675 | if (ii >= m_menuCount) | |
676 | return; | |
677 | } else | |
678 | { | |
679 | if (ii < 0 || ii >= m_menuCount) | |
680 | return; | |
681 | menu = m_menus[ii]; | |
682 | } | |
683 | ||
684 | if (!OnDelete(menu, ii)) | |
685 | return; | |
686 | ||
50414e24 | 687 | menu->SetParent((wxEvtHandler*) NULL); |
4bb6408c JS |
688 | |
689 | -- m_menuCount; | |
690 | for (j = ii; j < m_menuCount; j++) | |
691 | { | |
692 | m_menus[j] = m_menus[j + 1]; | |
693 | m_titles[j] = m_titles[j + 1]; | |
694 | } | |
695 | } | |
696 | ||
697 | // Find the menu menuString, item itemString, and return the item id. | |
698 | // Returns -1 if none found. | |
699 | int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const | |
700 | { | |
701 | char buf1[200]; | |
702 | char buf2[200]; | |
703 | wxStripMenuCodes ((char *)(const char *)menuString, buf1); | |
704 | int i; | |
705 | for (i = 0; i < m_menuCount; i++) | |
706 | { | |
707 | wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); | |
708 | if (strcmp (buf1, buf2) == 0) | |
709 | return m_menus[i]->FindItem (itemString); | |
710 | } | |
711 | return -1; | |
712 | } | |
713 | ||
50414e24 | 714 | wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const |
4bb6408c JS |
715 | { |
716 | if (itemMenu) | |
717 | *itemMenu = NULL; | |
718 | ||
719 | wxMenuItem *item = NULL; | |
720 | int i; | |
721 | for (i = 0; i < m_menuCount; i++) | |
50414e24 | 722 | if ((item = m_menus[i]->FindItemForId (id, itemMenu))) |
4bb6408c JS |
723 | return item; |
724 | return NULL; | |
725 | } | |
726 | ||
50414e24 | 727 | void wxMenuBar::SetHelpString (int id, const wxString& helpString) |
4bb6408c JS |
728 | { |
729 | int i; | |
730 | for (i = 0; i < m_menuCount; i++) | |
731 | { | |
50414e24 | 732 | if (m_menus[i]->FindItemForId (id)) |
4bb6408c | 733 | { |
50414e24 | 734 | m_menus[i]->SetHelpString (id, helpString); |
4bb6408c JS |
735 | return; |
736 | } | |
737 | } | |
738 | } | |
739 | ||
50414e24 | 740 | wxString wxMenuBar::GetHelpString (int id) const |
4bb6408c JS |
741 | { |
742 | int i; | |
743 | for (i = 0; i < m_menuCount; i++) | |
744 | { | |
50414e24 JS |
745 | if (m_menus[i]->FindItemForId (id)) |
746 | return wxString(m_menus[i]->GetHelpString (id)); | |
4bb6408c JS |
747 | } |
748 | return wxString(""); | |
749 | } | |
750 | ||
621793f4 JS |
751 | // Create menubar |
752 | bool wxMenuBar::CreateMenuBar(wxFrame* parent) | |
753 | { | |
754 | if (m_mainWidget) | |
755 | { | |
756 | XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL); | |
757 | /* | |
758 | if (!XtIsManaged((Widget) m_mainWidget)) | |
759 | XtManageChild((Widget) m_mainWidget); | |
760 | */ | |
761 | XtMapWidget((Widget) m_mainWidget); | |
762 | return TRUE; | |
763 | } | |
764 | ||
765 | Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0); | |
766 | m_mainWidget = (WXWidget) menuBarW; | |
767 | ||
768 | int i; | |
769 | for (i = 0; i < GetMenuCount(); i++) | |
770 | { | |
771 | wxMenu *menu = GetMenu(i); | |
772 | wxString title(m_titles[i]); | |
773 | menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE)); | |
774 | ||
775 | /* | |
776 | * COMMENT THIS OUT IF YOU DON'T LIKE A RIGHT-JUSTIFIED HELP MENU | |
777 | */ | |
778 | wxStripMenuCodes ((char*) (const char*) title, wxBuffer); | |
779 | ||
780 | if (strcmp (wxBuffer, "Help") == 0) | |
781 | XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL); | |
782 | } | |
783 | ||
784 | XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL); | |
785 | XtRealizeWidget ((Widget) menuBarW); | |
786 | XtManageChild ((Widget) menuBarW); | |
787 | SetMenuBarFrame(parent); | |
788 | ||
789 | return TRUE; | |
790 | } | |
791 | ||
792 | // Destroy menubar, but keep data structures intact so we can recreate it. | |
793 | bool wxMenuBar::DestroyMenuBar() | |
794 | { | |
795 | if (!m_mainWidget) | |
796 | { | |
797 | SetMenuBarFrame((wxFrame*) NULL); | |
798 | return FALSE; | |
799 | } | |
800 | ||
801 | XtUnmanageChild ((Widget) m_mainWidget); | |
802 | XtUnrealizeWidget ((Widget) m_mainWidget); | |
803 | ||
804 | int i; | |
805 | for (i = 0; i < GetMenuCount(); i++) | |
806 | { | |
807 | wxMenu *menu = GetMenu(i); | |
808 | menu->DestroyMenu(TRUE); | |
809 | ||
810 | } | |
811 | XtDestroyWidget((Widget) m_mainWidget); | |
812 | m_mainWidget = (WXWidget) 0; | |
813 | ||
814 | SetMenuBarFrame((wxFrame*) NULL); | |
815 | ||
816 | return TRUE; | |
817 | } | |
818 | ||
50414e24 JS |
819 | //// Motif-specific |
820 | ||
821 | extern wxApp *wxTheApp; | |
822 | static XtWorkProcId WorkProcMenuId; | |
823 | ||
824 | /* Since PopupMenu under Motif stills grab right mouse button events | |
825 | * after it was closed, we need to delete the associated widgets to | |
826 | * allow next PopUpMenu to appear... | |
827 | */ | |
828 | ||
829 | int PostDeletionOfMenu( XtPointer* clientData ) | |
830 | { | |
831 | XtRemoveWorkProc(WorkProcMenuId); | |
832 | wxMenu *menu = (wxMenu *)clientData; | |
833 | ||
834 | if (menu->GetMainWidget()) { | |
7fe7d506 JS |
835 | if (menu->GetParent()) |
836 | { | |
837 | wxList& list = menu->GetParent()->GetItems(); | |
838 | list.DeleteObject(menu); | |
839 | } | |
50414e24 JS |
840 | menu->DestroyMenu(TRUE); |
841 | } | |
842 | /* Mark as no longer popped up */ | |
843 | menu->m_menuId = -1; | |
844 | return TRUE; | |
845 | } | |
846 | ||
847 | void | |
848 | wxMenuPopdownCallback(Widget w, XtPointer clientData, | |
849 | XtPointer ptr) | |
850 | { | |
851 | wxMenu *menu = (wxMenu *)clientData; | |
852 | ||
853 | // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr> | |
854 | /* Since Callbacks of MenuItems are not yet processed, we put a | |
855 | * background job which will be done when system will be idle. | |
856 | * What awful hack!! :( | |
857 | */ | |
858 | ||
859 | WorkProcMenuId = XtAppAddWorkProc( | |
860 | (XtAppContext) wxTheApp->GetAppContext(), | |
861 | (XtWorkProc) PostDeletionOfMenu, | |
862 | (XtPointer) menu ); | |
863 | // Apparently not found in Motif headers | |
864 | // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL ); | |
865 | } | |
866 | ||
867 | /* | |
868 | * Create a popup or pulldown menu. | |
869 | * Submenus of a popup will be pulldown. | |
870 | * | |
871 | */ | |
872 | ||
873 | WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown) | |
874 | { | |
875 | Widget menu = (Widget) 0; | |
876 | Widget buttonWidget = (Widget) 0; | |
877 | Arg args[5]; | |
878 | XtSetArg (args[0], XmNnumColumns, m_numColumns); | |
879 | XtSetArg (args[1], XmNpacking, XmPACK_COLUMN); | |
880 | ||
881 | if (!pullDown) | |
882 | { | |
883 | menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2); | |
884 | XtAddCallback(menu, | |
885 | XmNunmapCallback, | |
886 | (XtCallbackProc)wxMenuPopdownCallback, | |
887 | (XtPointer)this); | |
888 | } | |
889 | else | |
890 | { | |
891 | char mnem = wxFindMnemonic (title); | |
892 | wxStripMenuCodes ((char*) (const char*) title, wxBuffer); | |
893 | ||
894 | menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2); | |
895 | ||
896 | XmString label_str = XmStringCreateSimple (wxBuffer); | |
897 | buttonWidget = XtVaCreateManagedWidget (wxBuffer, | |
47d67540 | 898 | #if wxUSE_GADGETS |
50414e24 JS |
899 | xmCascadeButtonGadgetClass, (Widget) parent, |
900 | #else | |
901 | xmCascadeButtonWidgetClass, (Widget) parent, | |
902 | #endif | |
903 | XmNlabelString, label_str, | |
904 | XmNsubMenuId, menu, | |
905 | NULL); | |
906 | ||
907 | if (mnem != 0) | |
908 | XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL); | |
909 | ||
910 | XmStringFree (label_str); | |
911 | } | |
912 | ||
913 | m_menuWidget = (WXWidget) menu; | |
914 | ||
915 | m_menuBar = menuBar; | |
916 | m_topLevelMenu = topMenu; | |
917 | ||
918 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
919 | { | |
920 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
921 | item->CreateItem (menu, menuBar, topMenu); | |
922 | } | |
923 | ||
924 | return buttonWidget; | |
925 | } | |
926 | ||
927 | // Destroys the Motif implementation of the menu, | |
928 | // but maintains the wxWindows data structures so we can | |
929 | // do a CreateMenu again. | |
930 | void wxMenu::DestroyMenu (bool full) | |
931 | { | |
932 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
933 | { | |
934 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
935 | item->SetMenuBar((wxMenuBar*) NULL); | |
936 | ||
937 | item->DestroyItem(full); | |
938 | } // for() | |
939 | ||
940 | if (m_buttonWidget) | |
941 | { | |
942 | if (full) | |
943 | { | |
944 | XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL); | |
945 | XtDestroyWidget ((Widget) m_buttonWidget); | |
946 | m_buttonWidget = (WXWidget) 0; | |
947 | } | |
948 | } | |
949 | if (m_menuWidget && full) | |
950 | { | |
951 | XtDestroyWidget((Widget) m_menuWidget); | |
952 | m_menuWidget = (WXWidget) NULL; | |
953 | } | |
954 | } | |
955 | ||
956 | WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const | |
957 | { | |
958 | if (id == m_menuId) | |
959 | { | |
960 | if (it) | |
961 | *it = (wxMenuItem*) NULL; | |
962 | return m_buttonWidget; | |
963 | } | |
4bb6408c | 964 | |
50414e24 JS |
965 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) |
966 | { | |
967 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
968 | if (item->GetId() == id) | |
969 | { | |
970 | if (it) | |
971 | *it = item; | |
972 | return item->GetButtonWidget(); | |
973 | } | |
974 | ||
975 | if (item->GetSubMenu()) | |
976 | { | |
977 | WXWidget w = item->GetSubMenu()->FindMenuItem (id, it); | |
978 | if (w) | |
979 | { | |
980 | return w; | |
981 | } | |
982 | } | |
983 | } // for() | |
984 | ||
985 | if (it) | |
986 | *it = (wxMenuItem*) NULL; | |
987 | return (WXWidget) NULL; | |
988 | } |