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