]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menu.cpp
Added wxMotif data object classes
[wxWidgets.git] / src / motif / menu.cpp
CommitLineData
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"
94b49b93 31#include "wx/settings.h"
4bb6408c
JS
32
33#include <Xm/Label.h>
34#include <Xm/LabelG.h>
35#include <Xm/CascadeBG.h>
36#include <Xm/CascadeB.h>
37#include <Xm/SeparatoG.h>
38#include <Xm/PushBG.h>
39#include <Xm/ToggleB.h>
40#include <Xm/ToggleBG.h>
41#include <Xm/RowColumn.h>
42
50414e24
JS
43#include "wx/motif/private.h"
44
4bb6408c
JS
45// other standard headers
46// ----------------------
47#include <string.h>
48
4bb6408c
JS
49#if !USE_SHARED_LIBRARY
50IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
51IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
52#endif
53
54// ============================================================================
55// implementation
56// ============================================================================
57
58// Menus
59
60// Construct a menu with optional title (then use append)
61wxMenu::wxMenu(const wxString& title, const wxFunction func)
62{
63 m_title = title;
64 m_parent = (wxEvtHandler*) NULL;
65 m_eventHandler = this;
66 m_noItems = 0;
67 m_menuBar = NULL;
2d120f83 68
4bb6408c
JS
69 //// Motif-specific members
70 m_numColumns = 1;
71 m_menuWidget = (WXWidget) NULL;
72 m_popupShell = (WXWidget) NULL;
73 m_buttonWidget = (WXWidget) NULL;
74 m_menuId = 0;
50414e24 75 m_topLevelMenu = (wxMenu*) NULL;
4bb6408c
JS
76 m_ownedByMenuBar = FALSE;
77 m_menuParent = (wxMenu*) NULL;
3dd4e4e0 78 m_clientData = (void*) NULL;
2d120f83 79
4bb6408c
JS
80 if (m_title != "")
81 {
50414e24 82 Append(ID_SEPARATOR, m_title) ;
4bb6408c
JS
83 AppendSeparator() ;
84 }
94b49b93
JS
85 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
86 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
87 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
2d120f83 88
4bb6408c 89 Callback(func);
4bb6408c
JS
90}
91
92// The wxWindow destructor will take care of deleting the submenus.
93wxMenu::~wxMenu()
94{
50414e24
JS
95 if (m_menuWidget)
96 {
2d120f83
JS
97 if (m_menuParent)
98 DestroyMenu(TRUE);
99 else
100 DestroyMenu(FALSE);
50414e24 101 }
2d120f83 102
50414e24
JS
103 // Not sure if this is right
104 if (m_menuParent && m_menuBar)
105 {
2d120f83
JS
106 m_menuParent = NULL;
107 // m_menuBar = NULL;
50414e24 108 }
2d120f83 109
4bb6408c
JS
110 wxNode *node = m_menuItems.First();
111 while (node)
112 {
113 wxMenuItem *item = (wxMenuItem *)node->Data();
2d120f83
JS
114
115 /*
4bb6408c 116 if (item->GetSubMenu())
2d120f83 117 item->DeleteSubMenu();
50414e24 118 */
2d120f83 119
4bb6408c
JS
120 wxNode *next = node->Next();
121 delete item;
122 delete node;
123 node = next;
124 }
125}
126
127void wxMenu::Break()
128{
50414e24 129 m_numColumns ++;
4bb6408c
JS
130}
131
132// function appends a new item or submenu to the menu
133void wxMenu::Append(wxMenuItem *pItem)
134{
4bb6408c 135 wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
2d120f83 136
4bb6408c 137 m_menuItems.Append(pItem);
2d120f83 138
50414e24 139 if (m_menuWidget)
2d120f83
JS
140 pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append
141
4bb6408c
JS
142 m_noItems++;
143}
144
145void wxMenu::AppendSeparator()
146{
4bb6408c
JS
147 Append(new wxMenuItem(this, ID_SEPARATOR));
148}
149
150// Pullright item
50414e24
JS
151// N.B.: difference between old and new code.
152// Old code stores subMenu in 'children' for later deletion,
153// as well as in m_menuItems, whereas we only store it in
154// m_menuItems here. What implications does this have?
155
156void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu,
4bb6408c
JS
157 const wxString& helpString)
158{
50414e24 159 Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu));
2d120f83 160
50414e24 161 subMenu->m_topLevelMenu = m_topLevelMenu;
4bb6408c
JS
162}
163
164// Ordinary menu item
50414e24 165void wxMenu::Append(int id, const wxString& label,
4bb6408c
JS
166 const wxString& helpString, bool checkable)
167{
2d120f83 168 // 'checkable' parameter is useless for Windows.
50414e24 169 Append(new wxMenuItem(this, id, label, helpString, checkable));
4bb6408c
JS
170}
171
172void wxMenu::Delete(int id)
173{
174 wxNode *node;
175 wxMenuItem *item;
176 int pos;
2d120f83 177
50414e24
JS
178 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
179 {
2d120f83
JS
180 item = (wxMenuItem *)node->Data();
181 if (item->GetId() == id)
182 break;
4bb6408c 183 }
2d120f83 184
4bb6408c 185 if (!node)
2d120f83
JS
186 return;
187
50414e24 188 item->DestroyItem(TRUE);
2d120f83 189
50414e24
JS
190 // See also old code - don't know if this is needed (seems redundant).
191 /*
2d120f83 192 if (item->GetSubMenu()) {
50414e24
JS
193 item->subMenu->top_level_menu = item->GetSubMenu();
194 item->subMenu->window_parent = NULL;
195 children->DeleteObject(item->GetSubMenu());
2d120f83
JS
196 }
197 */
198
4bb6408c
JS
199 m_menuItems.DeleteNode(node);
200 delete item;
4bb6408c
JS
201}
202
50414e24 203void wxMenu::Enable(int id, bool flag)
4bb6408c 204{
50414e24 205 wxMenuItem *item = FindItemForId(id);
4bb6408c 206 wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
2d120f83 207
50414e24 208 item->Enable(flag);
4bb6408c
JS
209}
210
211bool wxMenu::Enabled(int Id) const
212{
213 wxMenuItem *item = FindItemForId(Id);
214 wxCHECK( item != NULL, FALSE );
2d120f83 215
4bb6408c
JS
216 return item->IsEnabled();
217}
218
219void wxMenu::Check(int Id, bool Flag)
220{
221 wxMenuItem *item = FindItemForId(Id);
222 wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" );
2d120f83 223
4bb6408c
JS
224 item->Check(Flag);
225}
226
50414e24 227bool wxMenu::Checked(int id) const
4bb6408c 228{
50414e24 229 wxMenuItem *item = FindItemForId(id);
4bb6408c 230 wxCHECK( item != NULL, FALSE );
2d120f83 231
4bb6408c
JS
232 return item->IsChecked();
233}
234
235void wxMenu::SetTitle(const wxString& label)
236{
237 m_title = label ;
2d120f83 238
50414e24
JS
239 wxNode *node = m_menuItems.First ();
240 if (!node)
2d120f83
JS
241 return;
242
50414e24
JS
243 wxMenuItem *item = (wxMenuItem *) node->Data ();
244 Widget widget = (Widget) item->GetButtonWidget();
245 if (!widget)
2d120f83
JS
246 return;
247
50414e24
JS
248 XmString title_str = XmStringCreateSimple ((char*) (const char*) label);
249 XtVaSetValues (widget,
2d120f83
JS
250 XmNlabelString, title_str,
251 NULL);
50414e24 252 // TODO: should we delete title_str now?
4bb6408c
JS
253}
254
255const wxString wxMenu::GetTitle() const
256{
257 return m_title;
258}
259
260void wxMenu::SetLabel(int id, const wxString& label)
261{
50414e24
JS
262 wxMenuItem *item = FindItemForId(id);
263 if (item == (wxMenuItem*) NULL)
2d120f83
JS
264 return;
265
50414e24 266 item->SetLabel(label);
4bb6408c
JS
267}
268
50414e24 269wxString wxMenu::GetLabel(int id) const
4bb6408c 270{
2d120f83
JS
271 wxMenuItem *it = NULL;
272 WXWidget w = FindMenuItem (id, &it);
273 if (w)
50414e24 274 {
2d120f83
JS
275 XmString text;
276 char *s;
277 XtVaGetValues ((Widget) w,
278 XmNlabelString, &text,
279 NULL);
280
281 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
282 {
283 wxString str(s);
284 XtFree (s);
285 return str;
286 }
287 else
288 {
289 XmStringFree (text);
290 return wxEmptyString;
291 }
50414e24 292 }
2d120f83
JS
293 else
294 return wxEmptyString;
4bb6408c
JS
295}
296
297// Finds the item id matching the given string, -1 if not found.
298int wxMenu::FindItem (const wxString& itemString) const
299{
300 char buf1[200];
301 char buf2[200];
302 wxStripMenuCodes ((char *)(const char *)itemString, buf1);
2d120f83 303
4bb6408c
JS
304 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
305 {
2d120f83
JS
306 wxMenuItem *item = (wxMenuItem *) node->Data ();
307 if (item->GetSubMenu())
308 {
309 int ans = item->GetSubMenu()->FindItem(itemString);
310 if (ans > -1)
311 return ans;
312 }
313 if ( !item->IsSeparator() )
314 {
315 wxStripMenuCodes((char *)item->GetName().c_str(), buf2);
316 if (strcmp(buf1, buf2) == 0)
317 return item->GetId();
318 }
4bb6408c 319 }
2d120f83 320
4bb6408c
JS
321 return -1;
322}
323
324wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
325{
326 if (itemMenu)
327 *itemMenu = NULL;
328 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
329 {
330 wxMenuItem *item = (wxMenuItem *) node->Data ();
2d120f83 331
4bb6408c
JS
332 if (item->GetId() == itemId)
333 {
334 if (itemMenu)
335 *itemMenu = (wxMenu *) this;
336 return item;
337 }
2d120f83 338
4bb6408c
JS
339 if (item->GetSubMenu())
340 {
341 wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
342 if (ans)
343 return ans;
344 }
345 }
2d120f83 346
4bb6408c
JS
347 if (itemMenu)
348 *itemMenu = NULL;
349 return NULL;
350}
351
352void wxMenu::SetHelpString(int itemId, const wxString& helpString)
353{
354 wxMenuItem *item = FindItemForId (itemId);
355 if (item)
356 item->SetHelp(helpString);
357}
358
359wxString wxMenu::GetHelpString (int itemId) const
360{
361 wxMenuItem *item = FindItemForId (itemId);
362 wxString str("");
363 return (item == NULL) ? str : item->GetHelp();
364}
365
366void wxMenu::ProcessCommand(wxCommandEvent & event)
367{
368 bool processed = FALSE;
2d120f83 369
4bb6408c
JS
370 // Try a callback
371 if (m_callback)
372 {
2d120f83
JS
373 (void) (*(m_callback)) (*this, event);
374 processed = TRUE;
4bb6408c 375 }
2d120f83 376
4bb6408c
JS
377 // Try the menu's event handler
378 if ( !processed && GetEventHandler())
379 {
2d120f83 380 processed = GetEventHandler()->ProcessEvent(event);
4bb6408c 381 }
2d120f83 382 /* TODO
4bb6408c
JS
383 // Try the window the menu was popped up from (and up
384 // through the hierarchy)
385 if ( !processed && GetInvokingWindow())
2d120f83
JS
386 processed = GetInvokingWindow()->ProcessEvent(event);
387 */
4bb6408c
JS
388}
389
390bool wxWindow::PopupMenu(wxMenu *menu, int x, int y)
391{
2d120f83
JS
392 Widget widget = (Widget) GetMainWidget();
393
394 /* The menuId field seems to be usused, so we'll use it to
395 indicate whether a menu is popped up or not:
396 0: Not currently created as a popup
397 -1: Created as a popup, but not active
398 1: Active popup.
399 */
400
401 if (menu->GetParent() && (menu->GetId() != -1))
402 return FALSE;
403
404 if (menu->GetMainWidget()) {
405 menu->DestroyMenu(TRUE);
406 }
407
408 wxWindow *parent = this;
409
410 menu->SetId(1); /* Mark as popped-up */
411 menu->CreateMenu(NULL, widget, menu);
412 // menu->SetParent(parent);
413 // parent->children->Append(menu); // Store menu for later deletion
414
415 Widget menuWidget = (Widget) menu->GetMainWidget();
416
417 int rootX = 0;
418 int rootY = 0;
419
420 int deviceX = x;
421 int deviceY = y;
422 /*
423 if (this->IsKindOf(CLASSINFO(wxCanvas)))
424 {
50414e24
JS
425 wxCanvas *canvas = (wxCanvas *) this;
426 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
427 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
2d120f83
JS
428 }
429 */
430
431 Display *display = XtDisplay (widget);
432 Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget));
433 Window thisWindow = XtWindow (widget);
434 Window childWindow;
435 XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY,
436 &rootX, &rootY, &childWindow);
437
438 XButtonPressedEvent event;
439 event.type = ButtonPress;
440 event.button = 1;
441
442 event.x = deviceX;
443 event.y = deviceY;
444
445 event.x_root = rootX;
446 event.y_root = rootY;
447
448 XmMenuPosition (menuWidget, &event);
449 XtManageChild (menuWidget);
450
451 return TRUE;
4bb6408c
JS
452}
453
454// Menu Bar
455wxMenuBar::wxMenuBar()
456{
457 m_eventHandler = this;
458 m_menuCount = 0;
459 m_menus = NULL;
460 m_titles = NULL;
461 m_menuBarFrame = NULL;
621793f4 462 m_mainWidget = (WXWidget) NULL;
94b49b93
JS
463 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
464 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
465 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
4bb6408c
JS
466}
467
468wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
469{
470 m_eventHandler = this;
471 m_menuCount = n;
472 m_menus = menus;
473 m_titles = new wxString[n];
474 int i;
475 for ( i = 0; i < n; i++ )
2d120f83 476 m_titles[i] = titles[i];
4bb6408c 477 m_menuBarFrame = NULL;
94b49b93
JS
478 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
479 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
480 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
4bb6408c
JS
481}
482
483wxMenuBar::~wxMenuBar()
484{
485 int i;
486 for (i = 0; i < m_menuCount; i++)
487 {
488 delete m_menus[i];
489 }
490 delete[] m_menus;
491 delete[] m_titles;
4bb6408c
JS
492}
493
494// Must only be used AFTER menu has been attached to frame,
495// otherwise use individual menus to enable/disable items
496void wxMenuBar::Enable(int id, bool flag)
497{
498 wxMenu *itemMenu = NULL;
499 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
500 if (!item)
501 return;
50414e24 502 item->Enable(flag);
4bb6408c
JS
503}
504
505void wxMenuBar::EnableTop(int pos, bool flag)
506{
507 // TODO
508}
509
510// Must only be used AFTER menu has been attached to frame,
511// otherwise use individual menus
512void wxMenuBar::Check(int id, bool flag)
513{
514 wxMenu *itemMenu = NULL;
515 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
516 if (!item)
517 return;
2d120f83 518
4bb6408c
JS
519 if (!item->IsCheckable())
520 return ;
2d120f83 521
50414e24 522 item->Check(flag);
4bb6408c
JS
523}
524
525bool wxMenuBar::Checked(int id) const
526{
527 wxMenu *itemMenu = NULL;
528 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
529 if (!item)
530 return FALSE;
2d120f83 531
50414e24 532 return item->IsChecked();
4bb6408c
JS
533}
534
535bool wxMenuBar::Enabled(int id) const
536{
537 wxMenu *itemMenu = NULL;
538 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
539 if (!item)
540 return FALSE;
2d120f83 541
50414e24 542 return item->IsEnabled();
4bb6408c
JS
543}
544
4bb6408c
JS
545void wxMenuBar::SetLabel(int id, const wxString& label)
546{
547 wxMenu *itemMenu = NULL;
548 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2d120f83 549
4bb6408c
JS
550 if (!item)
551 return;
2d120f83 552
50414e24 553 item->SetLabel(label);
4bb6408c
JS
554}
555
556wxString wxMenuBar::GetLabel(int id) const
557{
558 wxMenu *itemMenu = NULL;
559 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2d120f83 560
4bb6408c
JS
561 if (!item)
562 return wxString("");
2d120f83 563
50414e24 564 return item->GetLabel();
4bb6408c
JS
565}
566
567void wxMenuBar::SetLabelTop(int pos, const wxString& label)
568{
2d120f83
JS
569 wxASSERT( (pos < m_menuCount) );
570
571 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
572 if (w)
50414e24 573 {
2d120f83
JS
574 XmString label_str = XmStringCreateSimple ((char*) (const char*) label);
575 XtVaSetValues (w,
576 XmNlabelString, label_str,
577 NULL);
578 XmStringFree (label_str);
50414e24 579 }
4bb6408c
JS
580}
581
582wxString wxMenuBar::GetLabelTop(int pos) const
583{
2d120f83
JS
584 wxASSERT( (pos < m_menuCount) );
585
586 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
587 if (w)
50414e24 588 {
2d120f83
JS
589 XmString text;
590 char *s;
591 XtVaGetValues (w,
592 XmNlabelString, &text,
593 NULL);
594
595 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
596 {
597 wxString str(s);
598 XtFree (s);
599 return str;
600 }
601 else
602 {
603 return wxEmptyString;
604 }
50414e24 605 }
2d120f83
JS
606 else
607 return wxEmptyString;
608
4bb6408c
JS
609}
610
50414e24 611bool wxMenuBar::OnDelete(wxMenu *menu, int pos)
4bb6408c 612{
50414e24
JS
613 // Only applies to dynamic deletion (when set in frame)
614 if (!m_menuBarFrame)
615 return TRUE;
2d120f83 616
50414e24
JS
617 menu->DestroyMenu(TRUE);
618 return TRUE;
4bb6408c
JS
619}
620
50414e24 621bool wxMenuBar::OnAppend(wxMenu *menu, const char *title)
4bb6408c 622{
50414e24
JS
623 // Only applies to dynamic append (when set in frame)
624 if (!m_menuBarFrame)
625 return TRUE;
2d120f83 626
50414e24
JS
627 // Probably should be an assert here
628 if (menu->GetParent())
629 return FALSE;
2d120f83 630
50414e24
JS
631 // Has already been appended
632 if (menu->GetButtonWidget())
633 return FALSE;
2d120f83 634
50414e24
JS
635 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
636 menu->SetButtonWidget(w);
2d120f83 637
50414e24 638 return TRUE;
4bb6408c
JS
639}
640
641void wxMenuBar::Append (wxMenu * menu, const wxString& title)
642{
643 if (!OnAppend(menu, title))
644 return;
2d120f83 645
4bb6408c
JS
646 m_menuCount ++;
647 wxMenu **new_menus = new wxMenu *[m_menuCount];
648 wxString *new_titles = new wxString[m_menuCount];
649 int i;
2d120f83 650
4bb6408c 651 for (i = 0; i < m_menuCount - 1; i++)
2d120f83 652 {
4bb6408c
JS
653 new_menus[i] = m_menus[i];
654 m_menus[i] = NULL;
655 new_titles[i] = m_titles[i];
656 m_titles[i] = "";
657 }
658 if (m_menus)
659 {
660 delete[]m_menus;
661 delete[]m_titles;
662 }
663 m_menus = new_menus;
664 m_titles = new_titles;
2d120f83 665
4bb6408c
JS
666 m_menus[m_menuCount - 1] = (wxMenu *)menu;
667 m_titles[m_menuCount - 1] = title;
2d120f83 668
50414e24
JS
669 menu->SetMenuBar(this);
670 menu->SetParent(this);
4bb6408c
JS
671}
672
673void wxMenuBar::Delete(wxMenu * menu, int i)
674{
675 int j;
676 int ii = (int) i;
2d120f83 677
4bb6408c
JS
678 if (menu != 0)
679 {
2d120f83 680 for (ii = 0; ii < m_menuCount; ii++)
4bb6408c
JS
681 {
682 if (m_menus[ii] == menu)
2d120f83
JS
683 break;
684 }
4bb6408c
JS
685 if (ii >= m_menuCount)
686 return;
687 } else
688 {
689 if (ii < 0 || ii >= m_menuCount)
690 return;
691 menu = m_menus[ii];
692 }
2d120f83 693
4bb6408c
JS
694 if (!OnDelete(menu, ii))
695 return;
2d120f83 696
50414e24 697 menu->SetParent((wxEvtHandler*) NULL);
2d120f83 698
4bb6408c
JS
699 -- m_menuCount;
700 for (j = ii; j < m_menuCount; j++)
701 {
702 m_menus[j] = m_menus[j + 1];
703 m_titles[j] = m_titles[j + 1];
704 }
705}
706
707// Find the menu menuString, item itemString, and return the item id.
708// Returns -1 if none found.
709int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
710{
711 char buf1[200];
712 char buf2[200];
713 wxStripMenuCodes ((char *)(const char *)menuString, buf1);
714 int i;
715 for (i = 0; i < m_menuCount; i++)
716 {
717 wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
718 if (strcmp (buf1, buf2) == 0)
719 return m_menus[i]->FindItem (itemString);
720 }
721 return -1;
722}
723
50414e24 724wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const
4bb6408c
JS
725{
726 if (itemMenu)
727 *itemMenu = NULL;
2d120f83 728
4bb6408c
JS
729 wxMenuItem *item = NULL;
730 int i;
731 for (i = 0; i < m_menuCount; i++)
50414e24 732 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
4bb6408c 733 return item;
2d120f83 734 return NULL;
4bb6408c
JS
735}
736
50414e24 737void wxMenuBar::SetHelpString (int id, const wxString& helpString)
4bb6408c
JS
738{
739 int i;
740 for (i = 0; i < m_menuCount; i++)
741 {
50414e24 742 if (m_menus[i]->FindItemForId (id))
4bb6408c 743 {
50414e24 744 m_menus[i]->SetHelpString (id, helpString);
4bb6408c
JS
745 return;
746 }
747 }
748}
749
50414e24 750wxString wxMenuBar::GetHelpString (int id) const
4bb6408c
JS
751{
752 int i;
753 for (i = 0; i < m_menuCount; i++)
754 {
50414e24
JS
755 if (m_menus[i]->FindItemForId (id))
756 return wxString(m_menus[i]->GetHelpString (id));
4bb6408c
JS
757 }
758 return wxString("");
759}
760
621793f4
JS
761// Create menubar
762bool wxMenuBar::CreateMenuBar(wxFrame* parent)
763{
2d120f83 764 if (m_mainWidget)
621793f4 765 {
2d120f83
JS
766 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
767 /*
768 if (!XtIsManaged((Widget) m_mainWidget))
769 XtManageChild((Widget) m_mainWidget);
770 */
771 XtMapWidget((Widget) m_mainWidget);
772 return TRUE;
621793f4 773 }
2d120f83
JS
774
775 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
776 m_mainWidget = (WXWidget) menuBarW;
777
778 int i;
779 for (i = 0; i < GetMenuCount(); i++)
780 {
781 wxMenu *menu = GetMenu(i);
782 wxString title(m_titles[i]);
783 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
784
785 /*
786 * COMMENT THIS OUT IF YOU DON'T LIKE A RIGHT-JUSTIFIED HELP MENU
787 */
788 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
789
790 if (strcmp (wxBuffer, "Help") == 0)
791 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
792 }
793
794 SetBackgroundColour(m_backgroundColour);
795 SetForegroundColour(m_foregroundColour);
796 SetFont(m_font);
797
798 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
799 XtRealizeWidget ((Widget) menuBarW);
800 XtManageChild ((Widget) menuBarW);
801 SetMenuBarFrame(parent);
802
803 return TRUE;
621793f4
JS
804}
805
806// Destroy menubar, but keep data structures intact so we can recreate it.
807bool wxMenuBar::DestroyMenuBar()
808{
2d120f83 809 if (!m_mainWidget)
621793f4 810 {
2d120f83
JS
811 SetMenuBarFrame((wxFrame*) NULL);
812 return FALSE;
621793f4 813 }
2d120f83
JS
814
815 XtUnmanageChild ((Widget) m_mainWidget);
816 XtUnrealizeWidget ((Widget) m_mainWidget);
817
818 int i;
819 for (i = 0; i < GetMenuCount(); i++)
820 {
821 wxMenu *menu = GetMenu(i);
822 menu->DestroyMenu(TRUE);
823
824 }
825 XtDestroyWidget((Widget) m_mainWidget);
826 m_mainWidget = (WXWidget) 0;
827
828 SetMenuBarFrame((wxFrame*) NULL);
829
830 return TRUE;
621793f4
JS
831}
832
50414e24
JS
833//// Motif-specific
834
835extern wxApp *wxTheApp;
836static XtWorkProcId WorkProcMenuId;
837
838/* Since PopupMenu under Motif stills grab right mouse button events
2d120f83
JS
839* after it was closed, we need to delete the associated widgets to
840* allow next PopUpMenu to appear...
841*/
50414e24
JS
842
843int PostDeletionOfMenu( XtPointer* clientData )
844{
2d120f83
JS
845 XtRemoveWorkProc(WorkProcMenuId);
846 wxMenu *menu = (wxMenu *)clientData;
847
848 if (menu->GetMainWidget()) {
849 if (menu->GetParent())
850 {
851 wxList& list = menu->GetParent()->GetItems();
852 list.DeleteObject(menu);
853 }
854 menu->DestroyMenu(TRUE);
7fe7d506 855 }
2d120f83
JS
856 /* Mark as no longer popped up */
857 menu->m_menuId = -1;
858 return TRUE;
50414e24
JS
859}
860
861void
862wxMenuPopdownCallback(Widget w, XtPointer clientData,
2d120f83 863 XtPointer ptr)
50414e24 864{
2d120f83
JS
865 wxMenu *menu = (wxMenu *)clientData;
866
867 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
868 /* Since Callbacks of MenuItems are not yet processed, we put a
869 * background job which will be done when system will be idle.
870 * What awful hack!! :(
871 */
872
873 WorkProcMenuId = XtAppAddWorkProc(
874 (XtAppContext) wxTheApp->GetAppContext(),
875 (XtWorkProc) PostDeletionOfMenu,
876 (XtPointer) menu );
877 // Apparently not found in Motif headers
878 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
50414e24
JS
879}
880
881/*
2d120f83
JS
882* Create a popup or pulldown menu.
883* Submenus of a popup will be pulldown.
884*
885*/
50414e24
JS
886
887WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
888{
2d120f83
JS
889 Widget menu = (Widget) 0;
890 Widget buttonWidget = (Widget) 0;
891 Arg args[5];
892 XtSetArg (args[0], XmNnumColumns, m_numColumns);
893 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
894
895 if (!pullDown)
50414e24 896 {
2d120f83
JS
897 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
898 XtAddCallback(menu,
899 XmNunmapCallback,
900 (XtCallbackProc)wxMenuPopdownCallback,
901 (XtPointer)this);
50414e24 902 }
2d120f83 903 else
50414e24 904 {
2d120f83
JS
905 char mnem = wxFindMnemonic (title);
906 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
907
908 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
909
910 XmString label_str = XmStringCreateSimple (wxBuffer);
911 buttonWidget = XtVaCreateManagedWidget (wxBuffer,
47d67540 912#if wxUSE_GADGETS
2d120f83 913 xmCascadeButtonGadgetClass, (Widget) parent,
50414e24 914#else
2d120f83 915 xmCascadeButtonWidgetClass, (Widget) parent,
50414e24 916#endif
2d120f83
JS
917 XmNlabelString, label_str,
918 XmNsubMenuId, menu,
919 NULL);
920
921 if (mnem != 0)
922 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
923
924 XmStringFree (label_str);
50414e24 925 }
2d120f83
JS
926
927 m_menuWidget = (WXWidget) menu;
928
929 m_menuBar = menuBar;
930 m_topLevelMenu = topMenu;
931
932 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 933 {
2d120f83
JS
934 wxMenuItem *item = (wxMenuItem *) node->Data ();
935 item->CreateItem (menu, menuBar, topMenu);
50414e24 936 }
2d120f83
JS
937
938 SetBackgroundColour(m_backgroundColour);
939 SetForegroundColour(m_foregroundColour);
940 SetFont(m_font);
941
942 return buttonWidget;
50414e24
JS
943}
944
945// Destroys the Motif implementation of the menu,
946// but maintains the wxWindows data structures so we can
947// do a CreateMenu again.
948void wxMenu::DestroyMenu (bool full)
949{
2d120f83 950 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 951 {
2d120f83
JS
952 wxMenuItem *item = (wxMenuItem *) node->Data ();
953 item->SetMenuBar((wxMenuBar*) NULL);
954
955 item->DestroyItem(full);
50414e24 956 } // for()
2d120f83
JS
957
958 if (m_buttonWidget)
50414e24 959 {
2d120f83
JS
960 if (full)
961 {
962 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
963 XtDestroyWidget ((Widget) m_buttonWidget);
964 m_buttonWidget = (WXWidget) 0;
965 }
50414e24 966 }
2d120f83 967 if (m_menuWidget && full)
50414e24 968 {
2d120f83
JS
969 XtDestroyWidget((Widget) m_menuWidget);
970 m_menuWidget = (WXWidget) NULL;
50414e24
JS
971 }
972}
973
974WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
975{
2d120f83 976 if (id == m_menuId)
50414e24 977 {
2d120f83
JS
978 if (it)
979 *it = (wxMenuItem*) NULL;
980 return m_buttonWidget;
50414e24 981 }
2d120f83
JS
982
983 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 984 {
2d120f83
JS
985 wxMenuItem *item = (wxMenuItem *) node->Data ();
986 if (item->GetId() == id)
987 {
988 if (it)
989 *it = item;
990 return item->GetButtonWidget();
991 }
992
993 if (item->GetSubMenu())
994 {
995 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
996 if (w)
997 {
998 return w;
999 }
1000 }
50414e24 1001 } // for()
2d120f83
JS
1002
1003 if (it)
1004 *it = (wxMenuItem*) NULL;
1005 return (WXWidget) NULL;
50414e24 1006}
94b49b93
JS
1007
1008void wxMenu::SetBackgroundColour(const wxColour& col)
1009{
1010 m_backgroundColour = col;
1011 if (m_menuWidget)
2d120f83 1012 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
94b49b93 1013 if (m_buttonWidget)
2d120f83
JS
1014 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
1015
94b49b93
JS
1016 wxNode* node = m_menuItems.First();
1017 while (node)
1018 {
1019 wxMenuItem* item = (wxMenuItem*) node->Data();
1020 if (item->GetButtonWidget())
1021 {
2d120f83
JS
1022 // This crashes because it uses gadgets
1023 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
94b49b93
JS
1024 }
1025 if (item->GetSubMenu())
2d120f83 1026 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
94b49b93
JS
1027 node = node->Next();
1028 }
1029}
1030
1031void wxMenu::SetForegroundColour(const wxColour& col)
1032{
1033 m_foregroundColour = col;
1034 if (m_menuWidget)
2d120f83 1035 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
94b49b93 1036 if (m_buttonWidget)
2d120f83
JS
1037 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
1038
94b49b93
JS
1039 wxNode* node = m_menuItems.First();
1040 while (node)
1041 {
1042 wxMenuItem* item = (wxMenuItem*) node->Data();
1043 if (item->GetButtonWidget())
1044 {
2d120f83
JS
1045 // This crashes because it uses gadgets
1046 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
94b49b93
JS
1047 }
1048 if (item->GetSubMenu())
2d120f83 1049 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
94b49b93
JS
1050 node = node->Next();
1051 }
1052}
1053
1054void wxMenu::ChangeFont(bool keepOriginalSize)
1055{
2d120f83 1056 // lesstif 0.87 hangs when setting XmNfontList
94b49b93
JS
1057#ifndef LESSTIF_VERSION
1058 if (!m_font.Ok() || !m_menuWidget)
1059 return;
2d120f83 1060
94b49b93 1061 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
2d120f83 1062
94b49b93 1063 XtVaSetValues ((Widget) m_menuWidget,
2d120f83
JS
1064 XmNfontList, fontList,
1065 NULL);
94b49b93
JS
1066 if (m_buttonWidget)
1067 {
2d120f83
JS
1068 XtVaSetValues ((Widget) m_buttonWidget,
1069 XmNfontList, fontList,
1070 NULL);
94b49b93
JS
1071 }
1072 wxNode* node = m_menuItems.First();
1073 while (node)
1074 {
1075 wxMenuItem* item = (wxMenuItem*) node->Data();
1076 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
1077 {
2d120f83
JS
1078 XtVaSetValues ((Widget) item->GetButtonWidget(),
1079 XmNfontList, fontList,
1080 NULL);
94b49b93
JS
1081 }
1082 if (item->GetSubMenu())
2d120f83 1083 item->GetSubMenu()->ChangeFont(keepOriginalSize);
94b49b93
JS
1084 node = node->Next();
1085 }
1086#endif
1087}
1088
1089void wxMenu::SetFont(const wxFont& font)
1090{
1091 m_font = font;
1092 ChangeFont();
1093}
1094
1095void wxMenuBar::SetBackgroundColour(const wxColour& col)
1096{
2d120f83 1097
94b49b93
JS
1098 m_backgroundColour = col;
1099 if (m_mainWidget)
2d120f83 1100 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
94b49b93
JS
1101 int i;
1102 for (i = 0; i < m_menuCount; i++)
2d120f83 1103 m_menus[i]->SetBackgroundColour((wxColour&) col);
94b49b93
JS
1104}
1105
1106void wxMenuBar::SetForegroundColour(const wxColour& col)
1107{
1108 m_foregroundColour = col;
1109 if (m_mainWidget)
2d120f83
JS
1110 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
1111
94b49b93
JS
1112 int i;
1113 for (i = 0; i < m_menuCount; i++)
2d120f83 1114 m_menus[i]->SetForegroundColour((wxColour&) col);
94b49b93
JS
1115}
1116
1117void wxMenuBar::ChangeFont(bool keepOriginalSize)
1118{
2d120f83 1119 // Nothing to do for menubar, fonts are kept in wxMenus
94b49b93
JS
1120}
1121
1122void wxMenuBar::SetFont(const wxFont& font)
1123{
1124 m_font = font;
1125 ChangeFont();
2d120f83 1126
94b49b93
JS
1127 int i;
1128 for (i = 0; i < m_menuCount; i++)
2d120f83 1129 m_menus[i]->SetFont(font);
94b49b93
JS
1130}
1131