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