]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
1. wxMenu{Item|Bar} modifications for wxMotif
[wxWidgets.git] / src / motif / menu.cpp
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 // declarations
15 // ============================================================================
16
17 #ifdef __GNUG__
18 #pragma implementation "menu.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/menu.h"
26 #include "wx/menuitem.h"
27 #include "wx/log.h"
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #include "wx/frame.h"
31 #include "wx/settings.h"
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
43 #include "wx/motif/private.h"
44
45 // other standard headers
46 #include <string.h>
47
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
51 #endif
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // Menus
59 // ----------------------------------------------------------------------------
60
61 // Construct a menu with optional title (then use append)
62 void wxMenu::Init(const wxString& title,
63 long style
64 #ifdef WXWIN_COMPATIBILITY
65 , const wxFunction func
66 #endif
67 )
68 {
69 m_title = title;
70 m_eventHandler = this;
71 m_noItems = 0;
72 m_menuBar = NULL;
73 m_pInvokingWindow = NULL;
74 m_style = style;
75
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;
82 m_topLevelMenu = (wxMenu*) NULL;
83 m_ownedByMenuBar = FALSE;
84 m_menuParent = (wxMenu*) NULL;
85 m_clientData = (void*) NULL;
86
87 if (m_title != "")
88 {
89 Append(ID_SEPARATOR, m_title) ;
90 AppendSeparator() ;
91 }
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);
95
96 #ifdef WXWIN_COMPATIBILITY
97 Callback(func);
98 #endif
99 }
100
101 // The wxWindow destructor will take care of deleting the submenus.
102 wxMenu::~wxMenu()
103 {
104 if (m_menuWidget)
105 {
106 if (m_menuParent)
107 DestroyMenu(TRUE);
108 else
109 DestroyMenu(FALSE);
110 }
111
112 // Not sure if this is right
113 if (m_menuParent && m_menuBar)
114 {
115 m_menuParent = NULL;
116 // m_menuBar = NULL;
117 }
118
119 wxNode *node = m_menuItems.First();
120 while (node)
121 {
122 wxMenuItem *item = (wxMenuItem *)node->Data();
123
124 /*
125 if (item->GetSubMenu())
126 item->DeleteSubMenu();
127 */
128
129 wxNode *next = node->Next();
130 delete item;
131 delete node;
132 node = next;
133 }
134 }
135
136 void wxMenu::Break()
137 {
138 m_numColumns ++;
139 }
140
141 // function appends a new item or submenu to the menu
142 void wxMenu::Append(wxMenuItem *pItem)
143 {
144 wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
145
146 m_menuItems.Append(pItem);
147
148 if (m_menuWidget)
149 pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append
150
151 m_noItems++;
152 }
153
154 void wxMenu::AppendSeparator()
155 {
156 Append(new wxMenuItem(this, ID_SEPARATOR));
157 }
158
159 // Pullright item
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
165 void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu,
166 const wxString& helpString)
167 {
168 Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu));
169
170 subMenu->m_topLevelMenu = m_topLevelMenu;
171 }
172
173 // Ordinary menu item
174 void wxMenu::Append(int id, const wxString& label,
175 const wxString& helpString, bool checkable)
176 {
177 // 'checkable' parameter is useless for Windows.
178 Append(new wxMenuItem(this, id, label, helpString, checkable));
179 }
180
181 void wxMenu::Delete(int id)
182 {
183 wxNode *node;
184 wxMenuItem *item;
185 int pos;
186
187 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
188 {
189 item = (wxMenuItem *)node->Data();
190 if (item->GetId() == id)
191 break;
192 }
193
194 if (!node)
195 return;
196
197 item->DestroyItem(TRUE);
198
199 // See also old code - don't know if this is needed (seems redundant).
200 /*
201 if (item->GetSubMenu()) {
202 item->subMenu->top_level_menu = item->GetSubMenu();
203 item->subMenu->window_parent = NULL;
204 children->DeleteObject(item->GetSubMenu());
205 }
206 */
207
208 m_menuItems.DeleteNode(node);
209 delete item;
210 }
211
212 void wxMenu::Enable(int id, bool flag)
213 {
214 wxMenuItem *item = FindItemForId(id);
215 wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
216
217 item->Enable(flag);
218 }
219
220 bool wxMenu::Enabled(int Id) const
221 {
222 wxMenuItem *item = FindItemForId(Id);
223 wxCHECK( item != NULL, FALSE );
224
225 return item->IsEnabled();
226 }
227
228 void 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" );
232
233 item->Check(Flag);
234 }
235
236 bool wxMenu::Checked(int id) const
237 {
238 wxMenuItem *item = FindItemForId(id);
239 wxCHECK( item != NULL, FALSE );
240
241 return item->IsChecked();
242 }
243
244 void wxMenu::SetTitle(const wxString& label)
245 {
246 m_title = label ;
247
248 wxNode *node = m_menuItems.First ();
249 if (!node)
250 return;
251
252 wxMenuItem *item = (wxMenuItem *) node->Data ();
253 Widget widget = (Widget) item->GetButtonWidget();
254 if (!widget)
255 return;
256
257 XmString title_str = XmStringCreateSimple ((char*) (const char*) label);
258 XtVaSetValues (widget,
259 XmNlabelString, title_str,
260 NULL);
261 // TODO: should we delete title_str now?
262 }
263
264 const wxString wxMenu::GetTitle() const
265 {
266 return m_title;
267 }
268
269 void wxMenu::SetLabel(int id, const wxString& label)
270 {
271 wxMenuItem *item = FindItemForId(id);
272 if (item == (wxMenuItem*) NULL)
273 return;
274
275 item->SetText(label);
276 }
277
278 wxString wxMenu::GetLabel(int id) const
279 {
280 wxMenuItem *it = NULL;
281 WXWidget w = FindMenuItem (id, &it);
282 if (w)
283 {
284 XmString text;
285 char *s;
286 XtVaGetValues ((Widget) w,
287 XmNlabelString, &text,
288 NULL);
289
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 }
301 }
302 else
303 return wxEmptyString;
304 }
305
306 // Finds the item id matching the given string, -1 if not found.
307 int wxMenu::FindItem (const wxString& itemString) const
308 {
309 char buf1[200];
310 char buf2[200];
311 wxStripMenuCodes ((char *)(const char *)itemString, buf1);
312
313 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
314 {
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 }
328 }
329
330 return -1;
331 }
332
333 wxMenuItem *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 ();
340
341 if (item->GetId() == itemId)
342 {
343 if (itemMenu)
344 *itemMenu = (wxMenu *) this;
345 return item;
346 }
347
348 if (item->GetSubMenu())
349 {
350 wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
351 if (ans)
352 return ans;
353 }
354 }
355
356 if (itemMenu)
357 *itemMenu = NULL;
358 return NULL;
359 }
360
361 void wxMenu::SetHelpString(int itemId, const wxString& helpString)
362 {
363 wxMenuItem *item = FindItemForId (itemId);
364 if (item)
365 item->SetHelp(helpString);
366 }
367
368 wxString wxMenu::GetHelpString (int itemId) const
369 {
370 wxMenuItem *item = FindItemForId (itemId);
371 wxString str("");
372 return (item == NULL) ? str : item->GetHelp();
373 }
374
375 void wxMenu::ProcessCommand(wxCommandEvent & event)
376 {
377 bool processed = FALSE;
378
379 // Try a callback
380 if (m_callback)
381 {
382 (void) (*(m_callback)) (*this, event);
383 processed = TRUE;
384 }
385
386 // Try the menu's event handler
387 if ( !processed && GetEventHandler())
388 {
389 processed = GetEventHandler()->ProcessEvent(event);
390 }
391 // Try the window the menu was popped up from (and up
392 // through the hierarchy)
393 if ( !processed && GetInvokingWindow())
394 processed = GetInvokingWindow()->ProcessEvent(event);
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.
401 void 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 }
435 }
436
437 // ----------------------------------------------------------------------------
438 // Menu Bar
439 // ----------------------------------------------------------------------------
440
441 void wxMenuBar::Init()
442 {
443 m_eventHandler = this;
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
451 wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
452 {
453 Init();
454
455 for ( int i = 0; i < n; i++ )
456 {
457 m_menus.Append(menus[i]);
458 m_titles.Add(titles[i]);
459 }
460 }
461
462 wxMenuBar::~wxMenuBar()
463 {
464 // nothing to do: wxMenuBarBase will delete the menus
465 }
466
467 void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
468 {
469 wxFAIL_MSG("TODO");
470 }
471
472 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
473 {
474 wxMenu *menu = GetMenu(pos);
475 if ( !menu )
476 return;
477
478 Widget w = (Widget)menu->GetButtonWidget();
479 if (w)
480 {
481 wxXmString label_str(label);
482
483 XtVaSetValues(w,
484 XmNlabelString, label_str(),
485 NULL);
486 }
487 }
488
489 wxString wxMenuBar::GetLabelTop(size_t pos) const
490 {
491 wxString str;
492
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);
503
504 char *s;
505 if ( XmStringGetLtoR(text, XmSTRING_DEFAULT_CHARSET, &s) )
506 {
507 str = s;
508
509 XtFree(s);
510 }
511 }
512 }
513
514 return str;
515 }
516
517 bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
518 {
519 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
520 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
521 wxT("menu already appended") );
522
523 if ( m_menuBarFrame )
524 {
525 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
526 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
527 menu->SetButtonWidget(w);
528 }
529
530 menu->SetMenuBar(this);
531
532 m_titles.Add(title);
533
534 return wxMenuBarBase::Append(menu, title);
535 }
536
537 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
538 {
539 if ( !wxMenuBarBase::Insert(pos, menu, title) )
540 return FALSE;
541
542 wxFAIL_MSG(wxT("TODO"));
543
544 return FALSE;
545 }
546
547 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
548 {
549 if ( !wxMenuBarBase::Replace(pos, menu, title) )
550 return FALSE;
551
552 wxFAIL_MSG(wxT("TODO"));
553
554 return NULL;
555 }
556
557 wxMenu *wxMenuBar::Remove(size_t pos)
558 {
559 wxMenu *menu = wxMenuBarBase::Remove(pos);
560 if ( !menu )
561 return NULL;
562
563 if ( m_menuBarFrame )
564 menu->DestroyMenu(TRUE);
565
566 menu->SetMenuBar(NULL);
567
568 m_titles.Remove(pos);
569
570 return menu;
571 }
572
573 // Find the menu menuString, item itemString, and return the item id.
574 // Returns -1 if none found.
575 int 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);
580
581 size_t menuCount = GetMenuCount();
582 for (size_t i = 0; i < menuCount; i++)
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
591 wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
592 {
593 if (itemMenu)
594 *itemMenu = NULL;
595
596 wxMenuItem *item = NULL;
597 size_t menuCount = GetMenuCount();
598 for (size_t i = 0; i < menuCount; i++)
599 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
600 return item;
601 return NULL;
602 }
603
604 // Create menubar
605 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
606 {
607 if (m_mainWidget)
608 {
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;
616 }
617
618 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
619 m_mainWidget = (WXWidget) menuBarW;
620
621 size_t menuCount = GetMenuCount();
622 for (size_t i = 0; i < menuCount; i++)
623 {
624 wxMenu *menu = GetMenu(i);
625 wxString title(m_titles[i]);
626 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
627
628 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
629 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
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 }
640 }
641
642 SetBackgroundColour(m_backgroundColour);
643 SetForegroundColour(m_foregroundColour);
644 SetFont(m_font);
645
646 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
647 XtRealizeWidget ((Widget) menuBarW);
648 XtManageChild ((Widget) menuBarW);
649 SetMenuBarFrame(parent);
650
651 return TRUE;
652 }
653
654 // Destroy menubar, but keep data structures intact so we can recreate it.
655 bool wxMenuBar::DestroyMenuBar()
656 {
657 if (!m_mainWidget)
658 {
659 SetMenuBarFrame((wxFrame*) NULL);
660 return FALSE;
661 }
662
663 XtUnmanageChild ((Widget) m_mainWidget);
664 XtUnrealizeWidget ((Widget) m_mainWidget);
665
666 size_t menuCount = GetMenuCount();
667 for (size_t i = 0; i < menuCount; i++)
668 {
669 wxMenu *menu = GetMenu(i);
670 menu->DestroyMenu(TRUE);
671
672 }
673 XtDestroyWidget((Widget) m_mainWidget);
674 m_mainWidget = (WXWidget) 0;
675
676 SetMenuBarFrame((wxFrame*) NULL);
677
678 return TRUE;
679 }
680
681 //// Motif-specific
682 static XtWorkProcId WorkProcMenuId;
683
684 /* Since PopupMenu under Motif stills grab right mouse button events
685 * after it was closed, we need to delete the associated widgets to
686 * allow next PopUpMenu to appear...
687 */
688
689 int PostDeletionOfMenu( XtPointer* clientData )
690 {
691 XtRemoveWorkProc(WorkProcMenuId);
692 wxMenu *menu = (wxMenu *)clientData;
693
694 if (menu->GetMainWidget()) {
695 if (menu->GetParent())
696 {
697 wxList& list = menu->GetParent()->GetItems();
698 list.DeleteObject(menu);
699 }
700 menu->DestroyMenu(TRUE);
701 }
702 /* Mark as no longer popped up */
703 menu->m_menuId = -1;
704 return TRUE;
705 }
706
707 void
708 wxMenuPopdownCallback(Widget WXUNUSED(w), XtPointer clientData,
709 XtPointer WXUNUSED(ptr))
710 {
711 wxMenu *menu = (wxMenu *)clientData;
712
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 */
718
719 WorkProcMenuId = XtAppAddWorkProc(
720 (XtAppContext) wxTheApp->GetAppContext(),
721 (XtWorkProc) PostDeletionOfMenu,
722 (XtPointer) menu );
723 // Apparently not found in Motif headers
724 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
725 }
726
727 /*
728 * Create a popup or pulldown menu.
729 * Submenus of a popup will be pulldown.
730 *
731 */
732
733 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
734 {
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);
740
741 if (!pullDown)
742 {
743 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
744 XtAddCallback(menu,
745 XmNunmapCallback,
746 (XtCallbackProc)wxMenuPopdownCallback,
747 (XtPointer)this);
748 }
749 else
750 {
751 char mnem = wxFindMnemonic (title);
752 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
753
754 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
755
756 wxString title2(wxStripMenuCodes(title));
757 wxXmString label_str(title2);
758 buttonWidget = XtVaCreateManagedWidget(title2,
759 #if wxUSE_GADGETS
760 xmCascadeButtonGadgetClass, (Widget) parent,
761 #else
762 xmCascadeButtonWidgetClass, (Widget) parent,
763 #endif
764 XmNlabelString, label_str(),
765 XmNsubMenuId, menu,
766 NULL);
767
768 if (mnem != 0)
769 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
770 }
771
772 m_menuWidget = (WXWidget) menu;
773
774 m_menuBar = menuBar;
775 m_topLevelMenu = topMenu;
776
777 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
778 {
779 wxMenuItem *item = (wxMenuItem *) node->Data ();
780 item->CreateItem (menu, menuBar, topMenu);
781 }
782
783 SetBackgroundColour(m_backgroundColour);
784 SetForegroundColour(m_foregroundColour);
785 SetFont(m_font);
786
787 return buttonWidget;
788 }
789
790 // Destroys the Motif implementation of the menu,
791 // but maintains the wxWindows data structures so we can
792 // do a CreateMenu again.
793 void wxMenu::DestroyMenu (bool full)
794 {
795 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
796 {
797 wxMenuItem *item = (wxMenuItem *) node->Data ();
798 item->SetMenuBar((wxMenuBar*) NULL);
799
800 item->DestroyItem(full);
801 }// for()
802
803 if (m_buttonWidget)
804 {
805 if (full)
806 {
807 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
808 XtDestroyWidget ((Widget) m_buttonWidget);
809 m_buttonWidget = (WXWidget) 0;
810 }
811 }
812 if (m_menuWidget && full)
813 {
814 XtDestroyWidget((Widget) m_menuWidget);
815 m_menuWidget = (WXWidget) NULL;
816 }
817 }
818
819 WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
820 {
821 if (id == m_menuId)
822 {
823 if (it)
824 *it = (wxMenuItem*) NULL;
825 return m_buttonWidget;
826 }
827
828 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
829 {
830 wxMenuItem *item = (wxMenuItem *) node->Data ();
831 if (item->GetId() == id)
832 {
833 if (it)
834 *it = item;
835 return item->GetButtonWidget();
836 }
837
838 if (item->GetSubMenu())
839 {
840 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
841 if (w)
842 {
843 return w;
844 }
845 }
846 }// for()
847
848 if (it)
849 *it = (wxMenuItem*) NULL;
850 return (WXWidget) NULL;
851 }
852
853 void wxMenu::SetBackgroundColour(const wxColour& col)
854 {
855 m_backgroundColour = col;
856 if (m_menuWidget)
857 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
858 if (m_buttonWidget)
859 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
860
861 wxNode* node = m_menuItems.First();
862 while (node)
863 {
864 wxMenuItem* item = (wxMenuItem*) node->Data();
865 if (item->GetButtonWidget())
866 {
867 // This crashes because it uses gadgets
868 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
869 }
870 if (item->GetSubMenu())
871 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
872 node = node->Next();
873 }
874 }
875
876 void wxMenu::SetForegroundColour(const wxColour& col)
877 {
878 m_foregroundColour = col;
879 if (m_menuWidget)
880 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
881 if (m_buttonWidget)
882 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
883
884 wxNode* node = m_menuItems.First();
885 while (node)
886 {
887 wxMenuItem* item = (wxMenuItem*) node->Data();
888 if (item->GetButtonWidget())
889 {
890 // This crashes because it uses gadgets
891 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
892 }
893 if (item->GetSubMenu())
894 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
895 node = node->Next();
896 }
897 }
898
899 void wxMenu::ChangeFont(bool keepOriginalSize)
900 {
901 // lesstif 0.87 hangs when setting XmNfontList
902 #ifndef LESSTIF_VERSION
903 if (!m_font.Ok() || !m_menuWidget)
904 return;
905
906 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
907
908 XtVaSetValues ((Widget) m_menuWidget,
909 XmNfontList, fontList,
910 NULL);
911 if (m_buttonWidget)
912 {
913 XtVaSetValues ((Widget) m_buttonWidget,
914 XmNfontList, fontList,
915 NULL);
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 {
923 XtVaSetValues ((Widget) item->GetButtonWidget(),
924 XmNfontList, fontList,
925 NULL);
926 }
927 if (item->GetSubMenu())
928 item->GetSubMenu()->ChangeFont(keepOriginalSize);
929 node = node->Next();
930 }
931 #endif
932 }
933
934 void wxMenu::SetFont(const wxFont& font)
935 {
936 m_font = font;
937 ChangeFont();
938 }
939
940 bool wxMenuBar::SetBackgroundColour(const wxColour& col)
941 {
942 m_backgroundColour = col;
943 if (m_mainWidget)
944 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
945
946 size_t menuCount = GetMenuCount();
947 for (size_t i = 0; i < menuCount; i++)
948 m_menus[i]->SetBackgroundColour((wxColour&) col);
949
950 return TRUE;
951 }
952
953 bool wxMenuBar::SetForegroundColour(const wxColour& col)
954 {
955 m_foregroundColour = col;
956 if (m_mainWidget)
957 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
958
959 size_t menuCount = GetMenuCount();
960 for (size_t i = 0; i < menuCount; i++)
961 m_menus[i]->SetForegroundColour((wxColour&) col);
962
963 return TRUE;
964 }
965
966 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
967 {
968 // Nothing to do for menubar, fonts are kept in wxMenus
969 }
970
971 bool wxMenuBar::SetFont(const wxFont& font)
972 {
973 m_font = font;
974 ChangeFont();
975
976 size_t menuCount = GetMenuCount();
977 for (size_t i = 0; i < menuCount; i++)
978 m_menus[i]->SetFont(font);
979
980 return TRUE;
981 }
982