end label edit patch from Ricky Gonzales <gonzales@pyramid3.net>
[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// ============================================================================
9874b4ee 14// declarations
4bb6408c
JS
15// ============================================================================
16
4bb6408c 17#ifdef __GNUG__
9874b4ee 18 #pragma implementation "menu.h"
4bb6408c
JS
19#endif
20
9874b4ee
VZ
21// ----------------------------------------------------------------------------
22// headers
23// ----------------------------------------------------------------------------
24
4bb6408c
JS
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 45// other standard headers
4bb6408c
JS
46#include <string.h>
47
4bb6408c
JS
48#if !USE_SHARED_LIBRARY
49IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
50IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
51#endif
52
53// ============================================================================
54// implementation
55// ============================================================================
56
9874b4ee 57// ----------------------------------------------------------------------------
4bb6408c 58// Menus
9874b4ee 59// ----------------------------------------------------------------------------
4bb6408c
JS
60
61// Construct a menu with optional title (then use append)
ee31c392
VZ
62void wxMenu::Init(const wxString& title,
63 long style
64#ifdef WXWIN_COMPATIBILITY
65 , const wxFunction func
66#endif
67 )
4bb6408c
JS
68{
69 m_title = title;
4bb6408c
JS
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
9874b4ee 275 item->SetText(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
9874b4ee 437// ----------------------------------------------------------------------------
4bb6408c 438// Menu Bar
9874b4ee 439// ----------------------------------------------------------------------------
4bb6408c 440
9874b4ee 441void wxMenuBar::Init()
cba2db0c
JS
442{
443 m_eventHandler = this;
cba2db0c
JS
444 m_menuBarFrame = NULL;
445 m_mainWidget = (WXWidget) NULL;
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);
449}
450
4bb6408c
JS
451wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
452{
9874b4ee 453 Init();
4bb6408c 454
9874b4ee 455 for ( int i = 0; i < n; i++ )
4bb6408c 456 {
9874b4ee
VZ
457 m_menus.Append(menus[i]);
458 m_titles.Add(titles[i]);
4bb6408c 459 }
4bb6408c
JS
460}
461
9874b4ee 462wxMenuBar::~wxMenuBar()
4bb6408c 463{
9874b4ee 464 // nothing to do: wxMenuBarBase will delete the menus
4bb6408c
JS
465}
466
9874b4ee 467void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
4bb6408c 468{
9874b4ee 469 wxFAIL_MSG("TODO");
4bb6408c
JS
470}
471
9874b4ee 472void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
4bb6408c 473{
9874b4ee
VZ
474 wxMenu *menu = GetMenu(pos);
475 if ( !menu )
4bb6408c 476 return;
bf6c2b35 477
9874b4ee
VZ
478 Widget w = (Widget)menu->GetButtonWidget();
479 if (w)
480 {
481 wxXmString label_str(label);
bf6c2b35 482
9874b4ee
VZ
483 XtVaSetValues(w,
484 XmNlabelString, label_str(),
485 NULL);
486 }
4bb6408c
JS
487}
488
9874b4ee 489wxString wxMenuBar::GetLabelTop(size_t pos) const
4bb6408c 490{
9874b4ee 491 wxString str;
bf6c2b35 492
9874b4ee
VZ
493 wxMenu *menu = GetMenu(pos);
494 if ( menu )
495 {
496 Widget w = (Widget)menu->GetButtonWidget();
497 if (w)
498 {
499 XmString text;
500 XtVaGetValues(w,
501 XmNlabelString, &text,
502 NULL);
4bb6408c 503
9874b4ee
VZ
504 char *s;
505 if ( XmStringGetLtoR(text, XmSTRING_DEFAULT_CHARSET, &s) )
506 {
507 str = s;
bf6c2b35 508
9874b4ee
VZ
509 XtFree(s);
510 }
511 }
512 }
bf6c2b35 513
9874b4ee 514 return str;
4bb6408c
JS
515}
516
9874b4ee 517bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
4bb6408c 518{
9874b4ee
VZ
519 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
520 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
521 wxT("menu already appended") );
bf6c2b35 522
9874b4ee 523 if ( m_menuBarFrame )
50414e24 524 {
9874b4ee
VZ
525 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
526 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
527 menu->SetButtonWidget(w);
50414e24 528 }
bf6c2b35 529
9874b4ee 530 menu->SetMenuBar(this);
4bb6408c 531
9874b4ee 532 m_titles.Add(title);
bf6c2b35 533
9874b4ee 534 return wxMenuBarBase::Append(menu, title);
4bb6408c
JS
535}
536
9874b4ee 537bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
4bb6408c 538{
9874b4ee 539 if ( !wxMenuBarBase::Insert(pos, menu, title) )
50414e24 540 return FALSE;
bf6c2b35 541
9874b4ee 542 wxFAIL_MSG(wxT("TODO"));
bf6c2b35 543
9874b4ee 544 return FALSE;
4bb6408c
JS
545}
546
9874b4ee 547wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
4bb6408c 548{
9874b4ee
VZ
549 if ( !wxMenuBarBase::Replace(pos, menu, title) )
550 return FALSE;
bf6c2b35 551
9874b4ee 552 wxFAIL_MSG(wxT("TODO"));
bf6c2b35 553
9874b4ee 554 return NULL;
4bb6408c
JS
555}
556
9874b4ee 557wxMenu *wxMenuBar::Remove(size_t pos)
4bb6408c 558{
9874b4ee
VZ
559 wxMenu *menu = wxMenuBarBase::Remove(pos);
560 if ( !menu )
561 return NULL;
bf6c2b35 562
9874b4ee
VZ
563 if ( m_menuBarFrame )
564 menu->DestroyMenu(TRUE);
bf6c2b35 565
9874b4ee 566 menu->SetMenuBar(NULL);
bf6c2b35 567
9874b4ee 568 m_titles.Remove(pos);
bf6c2b35 569
9874b4ee 570 return menu;
4bb6408c
JS
571}
572
573// Find the menu menuString, item itemString, and return the item id.
574// Returns -1 if none found.
575int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
576{
577 char buf1[200];
578 char buf2[200];
579 wxStripMenuCodes ((char *)(const char *)menuString, buf1);
9874b4ee
VZ
580
581 size_t menuCount = GetMenuCount();
582 for (size_t i = 0; i < menuCount; i++)
4bb6408c
JS
583 {
584 wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
585 if (strcmp (buf1, buf2) == 0)
586 return m_menus[i]->FindItem (itemString);
587 }
588 return -1;
589}
590
9874b4ee 591wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
4bb6408c
JS
592{
593 if (itemMenu)
594 *itemMenu = NULL;
bf6c2b35 595
4bb6408c 596 wxMenuItem *item = NULL;
9874b4ee
VZ
597 size_t menuCount = GetMenuCount();
598 for (size_t i = 0; i < menuCount; i++)
50414e24 599 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
4bb6408c 600 return item;
2d120f83 601 return NULL;
4bb6408c
JS
602}
603
621793f4
JS
604// Create menubar
605bool wxMenuBar::CreateMenuBar(wxFrame* parent)
606{
2d120f83 607 if (m_mainWidget)
621793f4 608 {
2d120f83
JS
609 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
610 /*
611 if (!XtIsManaged((Widget) m_mainWidget))
612 XtManageChild((Widget) m_mainWidget);
613 */
614 XtMapWidget((Widget) m_mainWidget);
615 return TRUE;
621793f4 616 }
bf6c2b35 617
2d120f83
JS
618 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
619 m_mainWidget = (WXWidget) menuBarW;
bf6c2b35 620
9874b4ee
VZ
621 size_t menuCount = GetMenuCount();
622 for (size_t i = 0; i < menuCount; i++)
2d120f83
JS
623 {
624 wxMenu *menu = GetMenu(i);
625 wxString title(m_titles[i]);
626 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
bf6c2b35 627
31528cd3 628 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
2d120f83 629 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
ee31c392
VZ
630
631 // tear off menu support
632#if (XmVersion >= 1002)
633 if ( menu->IsTearOff() )
634 {
635 XtVaSetValues(GetWidget(menu),
636 XmNtearOffModel, XmTEAR_OFF_ENABLED,
637 NULL);
638#endif
639 }
2d120f83 640 }
bf6c2b35 641
2d120f83
JS
642 SetBackgroundColour(m_backgroundColour);
643 SetForegroundColour(m_foregroundColour);
644 SetFont(m_font);
bf6c2b35 645
2d120f83
JS
646 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
647 XtRealizeWidget ((Widget) menuBarW);
648 XtManageChild ((Widget) menuBarW);
649 SetMenuBarFrame(parent);
bf6c2b35 650
2d120f83 651 return TRUE;
621793f4
JS
652}
653
654// Destroy menubar, but keep data structures intact so we can recreate it.
655bool wxMenuBar::DestroyMenuBar()
656{
2d120f83 657 if (!m_mainWidget)
621793f4 658 {
2d120f83
JS
659 SetMenuBarFrame((wxFrame*) NULL);
660 return FALSE;
621793f4 661 }
bf6c2b35 662
2d120f83
JS
663 XtUnmanageChild ((Widget) m_mainWidget);
664 XtUnrealizeWidget ((Widget) m_mainWidget);
bf6c2b35 665
9874b4ee
VZ
666 size_t menuCount = GetMenuCount();
667 for (size_t i = 0; i < menuCount; i++)
2d120f83
JS
668 {
669 wxMenu *menu = GetMenu(i);
670 menu->DestroyMenu(TRUE);
bf6c2b35 671
2d120f83
JS
672 }
673 XtDestroyWidget((Widget) m_mainWidget);
674 m_mainWidget = (WXWidget) 0;
bf6c2b35 675
2d120f83 676 SetMenuBarFrame((wxFrame*) NULL);
bf6c2b35 677
2d120f83 678 return TRUE;
621793f4
JS
679}
680
50414e24 681//// Motif-specific
50414e24
JS
682static XtWorkProcId WorkProcMenuId;
683
684/* Since PopupMenu under Motif stills grab right mouse button events
2d120f83
JS
685* after it was closed, we need to delete the associated widgets to
686* allow next PopUpMenu to appear...
687*/
50414e24
JS
688
689int PostDeletionOfMenu( XtPointer* clientData )
690{
2d120f83
JS
691 XtRemoveWorkProc(WorkProcMenuId);
692 wxMenu *menu = (wxMenu *)clientData;
bf6c2b35 693
2d120f83
JS
694 if (menu->GetMainWidget()) {
695 if (menu->GetParent())
696 {
697 wxList& list = menu->GetParent()->GetItems();
698 list.DeleteObject(menu);
699 }
700 menu->DestroyMenu(TRUE);
7fe7d506 701 }
2d120f83
JS
702 /* Mark as no longer popped up */
703 menu->m_menuId = -1;
704 return TRUE;
50414e24
JS
705}
706
bf6c2b35 707void
af111fc3
JS
708wxMenuPopdownCallback(Widget WXUNUSED(w), XtPointer clientData,
709 XtPointer WXUNUSED(ptr))
50414e24 710{
2d120f83 711 wxMenu *menu = (wxMenu *)clientData;
bf6c2b35 712
2d120f83
JS
713 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
714 /* Since Callbacks of MenuItems are not yet processed, we put a
715 * background job which will be done when system will be idle.
716 * What awful hack!! :(
717 */
bf6c2b35
VZ
718
719 WorkProcMenuId = XtAppAddWorkProc(
720 (XtAppContext) wxTheApp->GetAppContext(),
2d120f83
JS
721 (XtWorkProc) PostDeletionOfMenu,
722 (XtPointer) menu );
723 // Apparently not found in Motif headers
724 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
50414e24
JS
725}
726
727/*
2d120f83
JS
728* Create a popup or pulldown menu.
729* Submenus of a popup will be pulldown.
730*
731*/
50414e24
JS
732
733WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
734{
2d120f83
JS
735 Widget menu = (Widget) 0;
736 Widget buttonWidget = (Widget) 0;
737 Arg args[5];
738 XtSetArg (args[0], XmNnumColumns, m_numColumns);
739 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
bf6c2b35 740
2d120f83 741 if (!pullDown)
50414e24 742 {
2d120f83
JS
743 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
744 XtAddCallback(menu,
bf6c2b35 745 XmNunmapCallback,
2d120f83
JS
746 (XtCallbackProc)wxMenuPopdownCallback,
747 (XtPointer)this);
50414e24 748 }
2d120f83 749 else
50414e24 750 {
2d120f83
JS
751 char mnem = wxFindMnemonic (title);
752 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
bf6c2b35 753
2d120f83 754 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
bf6c2b35 755
31528cd3
VZ
756 wxString title2(wxStripMenuCodes(title));
757 wxXmString label_str(title2);
758 buttonWidget = XtVaCreateManagedWidget(title2,
47d67540 759#if wxUSE_GADGETS
2d120f83 760 xmCascadeButtonGadgetClass, (Widget) parent,
50414e24 761#else
2d120f83 762 xmCascadeButtonWidgetClass, (Widget) parent,
50414e24 763#endif
193fe989 764 XmNlabelString, label_str(),
2d120f83
JS
765 XmNsubMenuId, menu,
766 NULL);
bf6c2b35 767
2d120f83
JS
768 if (mnem != 0)
769 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
50414e24 770 }
bf6c2b35 771
2d120f83 772 m_menuWidget = (WXWidget) menu;
bf6c2b35 773
2d120f83
JS
774 m_menuBar = menuBar;
775 m_topLevelMenu = topMenu;
bf6c2b35 776
2d120f83 777 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 778 {
2d120f83
JS
779 wxMenuItem *item = (wxMenuItem *) node->Data ();
780 item->CreateItem (menu, menuBar, topMenu);
50414e24 781 }
bf6c2b35 782
2d120f83
JS
783 SetBackgroundColour(m_backgroundColour);
784 SetForegroundColour(m_foregroundColour);
785 SetFont(m_font);
bf6c2b35 786
2d120f83 787 return buttonWidget;
50414e24
JS
788}
789
790// Destroys the Motif implementation of the menu,
791// but maintains the wxWindows data structures so we can
bf6c2b35 792// do a CreateMenu again.
50414e24
JS
793void wxMenu::DestroyMenu (bool full)
794{
2d120f83 795 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 796 {
2d120f83
JS
797 wxMenuItem *item = (wxMenuItem *) node->Data ();
798 item->SetMenuBar((wxMenuBar*) NULL);
bf6c2b35 799
2d120f83 800 item->DestroyItem(full);
bf6c2b35
VZ
801 }// for()
802
2d120f83 803 if (m_buttonWidget)
50414e24 804 {
2d120f83
JS
805 if (full)
806 {
807 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
808 XtDestroyWidget ((Widget) m_buttonWidget);
809 m_buttonWidget = (WXWidget) 0;
810 }
50414e24 811 }
2d120f83 812 if (m_menuWidget && full)
50414e24 813 {
2d120f83
JS
814 XtDestroyWidget((Widget) m_menuWidget);
815 m_menuWidget = (WXWidget) NULL;
50414e24
JS
816 }
817}
818
819WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
820{
2d120f83 821 if (id == m_menuId)
50414e24 822 {
2d120f83
JS
823 if (it)
824 *it = (wxMenuItem*) NULL;
825 return m_buttonWidget;
50414e24 826 }
bf6c2b35 827
2d120f83 828 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
50414e24 829 {
2d120f83
JS
830 wxMenuItem *item = (wxMenuItem *) node->Data ();
831 if (item->GetId() == id)
832 {
833 if (it)
834 *it = item;
835 return item->GetButtonWidget();
836 }
bf6c2b35 837
2d120f83
JS
838 if (item->GetSubMenu())
839 {
840 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
841 if (w)
842 {
843 return w;
844 }
845 }
bf6c2b35
VZ
846 }// for()
847
2d120f83
JS
848 if (it)
849 *it = (wxMenuItem*) NULL;
850 return (WXWidget) NULL;
50414e24 851}
94b49b93
JS
852
853void wxMenu::SetBackgroundColour(const wxColour& col)
854{
855 m_backgroundColour = col;
856 if (m_menuWidget)
2d120f83 857 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
94b49b93 858 if (m_buttonWidget)
2d120f83 859 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
bf6c2b35 860
94b49b93
JS
861 wxNode* node = m_menuItems.First();
862 while (node)
863 {
864 wxMenuItem* item = (wxMenuItem*) node->Data();
865 if (item->GetButtonWidget())
866 {
2d120f83
JS
867 // This crashes because it uses gadgets
868 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
94b49b93
JS
869 }
870 if (item->GetSubMenu())
2d120f83 871 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
94b49b93
JS
872 node = node->Next();
873 }
874}
875
876void wxMenu::SetForegroundColour(const wxColour& col)
877{
878 m_foregroundColour = col;
879 if (m_menuWidget)
2d120f83 880 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
94b49b93 881 if (m_buttonWidget)
2d120f83 882 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
bf6c2b35 883
94b49b93
JS
884 wxNode* node = m_menuItems.First();
885 while (node)
886 {
887 wxMenuItem* item = (wxMenuItem*) node->Data();
888 if (item->GetButtonWidget())
889 {
2d120f83
JS
890 // This crashes because it uses gadgets
891 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
94b49b93
JS
892 }
893 if (item->GetSubMenu())
2d120f83 894 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
94b49b93
JS
895 node = node->Next();
896 }
897}
898
899void wxMenu::ChangeFont(bool keepOriginalSize)
900{
2d120f83 901 // lesstif 0.87 hangs when setting XmNfontList
bf6c2b35 902#ifndef LESSTIF_VERSION
94b49b93
JS
903 if (!m_font.Ok() || !m_menuWidget)
904 return;
bf6c2b35 905
94b49b93 906 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
bf6c2b35 907
94b49b93 908 XtVaSetValues ((Widget) m_menuWidget,
2d120f83
JS
909 XmNfontList, fontList,
910 NULL);
94b49b93
JS
911 if (m_buttonWidget)
912 {
2d120f83
JS
913 XtVaSetValues ((Widget) m_buttonWidget,
914 XmNfontList, fontList,
915 NULL);
94b49b93
JS
916 }
917 wxNode* node = m_menuItems.First();
918 while (node)
919 {
920 wxMenuItem* item = (wxMenuItem*) node->Data();
921 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
922 {
2d120f83
JS
923 XtVaSetValues ((Widget) item->GetButtonWidget(),
924 XmNfontList, fontList,
925 NULL);
94b49b93
JS
926 }
927 if (item->GetSubMenu())
2d120f83 928 item->GetSubMenu()->ChangeFont(keepOriginalSize);
94b49b93
JS
929 node = node->Next();
930 }
931#endif
932}
933
934void wxMenu::SetFont(const wxFont& font)
935{
936 m_font = font;
937 ChangeFont();
938}
939
9874b4ee 940bool wxMenuBar::SetBackgroundColour(const wxColour& col)
94b49b93 941{
94b49b93
JS
942 m_backgroundColour = col;
943 if (m_mainWidget)
2d120f83 944 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
9874b4ee
VZ
945
946 size_t menuCount = GetMenuCount();
947 for (size_t i = 0; i < menuCount; i++)
2d120f83 948 m_menus[i]->SetBackgroundColour((wxColour&) col);
9874b4ee
VZ
949
950 return TRUE;
94b49b93
JS
951}
952
9874b4ee 953bool wxMenuBar::SetForegroundColour(const wxColour& col)
94b49b93
JS
954{
955 m_foregroundColour = col;
956 if (m_mainWidget)
2d120f83 957 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
bf6c2b35 958
9874b4ee
VZ
959 size_t menuCount = GetMenuCount();
960 for (size_t i = 0; i < menuCount; i++)
2d120f83 961 m_menus[i]->SetForegroundColour((wxColour&) col);
9874b4ee
VZ
962
963 return TRUE;
94b49b93
JS
964}
965
af111fc3 966void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
94b49b93 967{
2d120f83 968 // Nothing to do for menubar, fonts are kept in wxMenus
94b49b93
JS
969}
970
9874b4ee 971bool wxMenuBar::SetFont(const wxFont& font)
94b49b93
JS
972{
973 m_font = font;
974 ChangeFont();
bf6c2b35 975
9874b4ee
VZ
976 size_t menuCount = GetMenuCount();
977 for (size_t i = 0; i < menuCount; i++)
2d120f83 978 m_menus[i]->SetFont(font);
9874b4ee
VZ
979
980 return TRUE;
94b49b93
JS
981}
982