]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
Corrected Motif build,
[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 // 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"
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 // ----------------------
47 #include <string.h>
48
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
51 IMPLEMENT_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)
61 void wxMenu::Init(const wxString& title,
62 long style
63 #ifdef WXWIN_COMPATIBILITY
64 , const wxFunction func
65 #endif
66 )
67 {
68 m_title = title;
69 m_parent = (wxEvtHandler*) NULL;
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->SetLabel(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 // Menu Bar
438 wxMenuBar::wxMenuBar()
439 {
440 m_eventHandler = this;
441 m_menuCount = 0;
442 m_menus = NULL;
443 m_titles = NULL;
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(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
464 wxMenuBar::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++ )
472 m_titles[i] = titles[i];
473 m_menuBarFrame = NULL;
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);
477 }
478
479 wxMenuBar::~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;
488 }
489
490 // Must only be used AFTER menu has been attached to frame,
491 // otherwise use individual menus to enable/disable items
492 void wxMenuBar::Enable(int id, bool flag)
493 {
494 wxMenu *itemMenu = NULL;
495 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
496 if (!item)
497 return;
498 item->Enable(flag);
499 }
500
501 void 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
508 void wxMenuBar::Check(int id, bool flag)
509 {
510 wxMenu *itemMenu = NULL;
511 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
512 if (!item)
513 return;
514
515 if (!item->IsCheckable())
516 return ;
517
518 item->Check(flag);
519 }
520
521 bool wxMenuBar::Checked(int id) const
522 {
523 wxMenu *itemMenu = NULL;
524 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
525 if (!item)
526 return FALSE;
527
528 return item->IsChecked();
529 }
530
531 bool wxMenuBar::Enabled(int id) const
532 {
533 wxMenu *itemMenu = NULL;
534 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
535 if (!item)
536 return FALSE;
537
538 return item->IsEnabled();
539 }
540
541 void wxMenuBar::SetLabel(int id, const wxString& label)
542 {
543 wxMenu *itemMenu = NULL;
544 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
545
546 if (!item)
547 return;
548
549 item->SetLabel(label);
550 }
551
552 wxString wxMenuBar::GetLabel(int id) const
553 {
554 wxMenu *itemMenu = NULL;
555 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
556
557 if (!item)
558 return wxString("");
559
560 return item->GetLabel();
561 }
562
563 void wxMenuBar::SetLabelTop(int pos, const wxString& label)
564 {
565 wxASSERT( (pos < m_menuCount) );
566
567 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
568 if (w)
569 {
570 XmString label_str = XmStringCreateSimple ((char*) (const char*) label);
571 XtVaSetValues (w,
572 XmNlabelString, label_str,
573 NULL);
574 XmStringFree (label_str);
575 }
576 }
577
578 wxString wxMenuBar::GetLabelTop(int pos) const
579 {
580 wxASSERT( (pos < m_menuCount) );
581
582 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
583 if (w)
584 {
585 XmString text;
586 char *s;
587 XtVaGetValues (w,
588 XmNlabelString, &text,
589 NULL);
590
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 }
601 }
602 else
603 return wxEmptyString;
604
605 }
606
607 bool wxMenuBar::OnDelete(wxMenu *menu, int pos)
608 {
609 // Only applies to dynamic deletion (when set in frame)
610 if (!m_menuBarFrame)
611 return TRUE;
612
613 menu->DestroyMenu(TRUE);
614 return TRUE;
615 }
616
617 bool wxMenuBar::OnAppend(wxMenu *menu, const char *title)
618 {
619 // Only applies to dynamic append (when set in frame)
620 if (!m_menuBarFrame)
621 return TRUE;
622
623 // Probably should be an assert here
624 if (menu->GetParent())
625 return FALSE;
626
627 // Has already been appended
628 if (menu->GetButtonWidget())
629 return FALSE;
630
631 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
632 menu->SetButtonWidget(w);
633
634 return TRUE;
635 }
636
637 void wxMenuBar::Append (wxMenu * menu, const wxString& title)
638 {
639 if (!OnAppend(menu, title))
640 return;
641
642 m_menuCount ++;
643 wxMenu **new_menus = new wxMenu *[m_menuCount];
644 wxString *new_titles = new wxString[m_menuCount];
645 int i;
646
647 for (i = 0; i < m_menuCount - 1; i++)
648 {
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;
661
662 m_menus[m_menuCount - 1] = (wxMenu *)menu;
663 m_titles[m_menuCount - 1] = title;
664
665 menu->SetMenuBar(this);
666 menu->SetParent(this);
667 }
668
669 void wxMenuBar::Delete(wxMenu * menu, int i)
670 {
671 int j;
672 int ii = (int) i;
673
674 if (menu != 0)
675 {
676 for (ii = 0; ii < m_menuCount; ii++)
677 {
678 if (m_menus[ii] == menu)
679 break;
680 }
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 }
689
690 if (!OnDelete(menu, ii))
691 return;
692
693 menu->SetParent((wxEvtHandler*) NULL);
694
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.
705 int 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
720 wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const
721 {
722 if (itemMenu)
723 *itemMenu = NULL;
724
725 wxMenuItem *item = NULL;
726 int i;
727 for (i = 0; i < m_menuCount; i++)
728 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
729 return item;
730 return NULL;
731 }
732
733 void wxMenuBar::SetHelpString (int id, const wxString& helpString)
734 {
735 int i;
736 for (i = 0; i < m_menuCount; i++)
737 {
738 if (m_menus[i]->FindItemForId (id))
739 {
740 m_menus[i]->SetHelpString (id, helpString);
741 return;
742 }
743 }
744 }
745
746 wxString wxMenuBar::GetHelpString (int id) const
747 {
748 int i;
749 for (i = 0; i < m_menuCount; i++)
750 {
751 if (m_menus[i]->FindItemForId (id))
752 return wxString(m_menus[i]->GetHelpString (id));
753 }
754 return wxString("");
755 }
756
757 // Create menubar
758 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
759 {
760 if (m_mainWidget)
761 {
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;
769 }
770
771 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
772 m_mainWidget = (WXWidget) menuBarW;
773
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));
780
781 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
782 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
783
784 // tear off menu support
785 #if (XmVersion >= 1002)
786 if ( menu->IsTearOff() )
787 {
788 XtVaSetValues(GetWidget(menu),
789 XmNtearOffModel, XmTEAR_OFF_ENABLED,
790 NULL);
791 #endif
792 }
793 }
794
795 SetBackgroundColour(m_backgroundColour);
796 SetForegroundColour(m_foregroundColour);
797 SetFont(m_font);
798
799 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
800 XtRealizeWidget ((Widget) menuBarW);
801 XtManageChild ((Widget) menuBarW);
802 SetMenuBarFrame(parent);
803
804 return TRUE;
805 }
806
807 // Destroy menubar, but keep data structures intact so we can recreate it.
808 bool wxMenuBar::DestroyMenuBar()
809 {
810 if (!m_mainWidget)
811 {
812 SetMenuBarFrame((wxFrame*) NULL);
813 return FALSE;
814 }
815
816 XtUnmanageChild ((Widget) m_mainWidget);
817 XtUnrealizeWidget ((Widget) m_mainWidget);
818
819 int i;
820 for (i = 0; i < GetMenuCount(); i++)
821 {
822 wxMenu *menu = GetMenu(i);
823 menu->DestroyMenu(TRUE);
824
825 }
826 XtDestroyWidget((Widget) m_mainWidget);
827 m_mainWidget = (WXWidget) 0;
828
829 SetMenuBarFrame((wxFrame*) NULL);
830
831 return TRUE;
832 }
833
834 //// Motif-specific
835
836 extern wxApp *wxTheApp;
837 static XtWorkProcId WorkProcMenuId;
838
839 /* Since PopupMenu under Motif stills grab right mouse button events
840 * after it was closed, we need to delete the associated widgets to
841 * allow next PopUpMenu to appear...
842 */
843
844 int PostDeletionOfMenu( XtPointer* clientData )
845 {
846 XtRemoveWorkProc(WorkProcMenuId);
847 wxMenu *menu = (wxMenu *)clientData;
848
849 if (menu->GetMainWidget()) {
850 if (menu->GetParent())
851 {
852 wxList& list = menu->GetParent()->GetItems();
853 list.DeleteObject(menu);
854 }
855 menu->DestroyMenu(TRUE);
856 }
857 /* Mark as no longer popped up */
858 menu->m_menuId = -1;
859 return TRUE;
860 }
861
862 void
863 wxMenuPopdownCallback(Widget w, XtPointer clientData,
864 XtPointer ptr)
865 {
866 wxMenu *menu = (wxMenu *)clientData;
867
868 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
869 /* Since Callbacks of MenuItems are not yet processed, we put a
870 * background job which will be done when system will be idle.
871 * What awful hack!! :(
872 */
873
874 WorkProcMenuId = XtAppAddWorkProc(
875 (XtAppContext) wxTheApp->GetAppContext(),
876 (XtWorkProc) PostDeletionOfMenu,
877 (XtPointer) menu );
878 // Apparently not found in Motif headers
879 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
880 }
881
882 /*
883 * Create a popup or pulldown menu.
884 * Submenus of a popup will be pulldown.
885 *
886 */
887
888 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
889 {
890 Widget menu = (Widget) 0;
891 Widget buttonWidget = (Widget) 0;
892 Arg args[5];
893 XtSetArg (args[0], XmNnumColumns, m_numColumns);
894 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
895
896 if (!pullDown)
897 {
898 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
899 XtAddCallback(menu,
900 XmNunmapCallback,
901 (XtCallbackProc)wxMenuPopdownCallback,
902 (XtPointer)this);
903 }
904 else
905 {
906 char mnem = wxFindMnemonic (title);
907 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
908
909 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
910
911 wxString title2(wxStripMenuCodes(title));
912 wxXmString label_str(title2);
913 buttonWidget = XtVaCreateManagedWidget(title2,
914 #if wxUSE_GADGETS
915 xmCascadeButtonGadgetClass, (Widget) parent,
916 #else
917 xmCascadeButtonWidgetClass, (Widget) parent,
918 #endif
919 XmNlabelString, label_str(),
920 XmNsubMenuId, menu,
921 NULL);
922
923 if (mnem != 0)
924 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
925 }
926
927 m_menuWidget = (WXWidget) menu;
928
929 m_menuBar = menuBar;
930 m_topLevelMenu = topMenu;
931
932 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
933 {
934 wxMenuItem *item = (wxMenuItem *) node->Data ();
935 item->CreateItem (menu, menuBar, topMenu);
936 }
937
938 SetBackgroundColour(m_backgroundColour);
939 SetForegroundColour(m_foregroundColour);
940 SetFont(m_font);
941
942 return buttonWidget;
943 }
944
945 // Destroys the Motif implementation of the menu,
946 // but maintains the wxWindows data structures so we can
947 // do a CreateMenu again.
948 void wxMenu::DestroyMenu (bool full)
949 {
950 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
951 {
952 wxMenuItem *item = (wxMenuItem *) node->Data ();
953 item->SetMenuBar((wxMenuBar*) NULL);
954
955 item->DestroyItem(full);
956 }// for()
957
958 if (m_buttonWidget)
959 {
960 if (full)
961 {
962 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
963 XtDestroyWidget ((Widget) m_buttonWidget);
964 m_buttonWidget = (WXWidget) 0;
965 }
966 }
967 if (m_menuWidget && full)
968 {
969 XtDestroyWidget((Widget) m_menuWidget);
970 m_menuWidget = (WXWidget) NULL;
971 }
972 }
973
974 WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
975 {
976 if (id == m_menuId)
977 {
978 if (it)
979 *it = (wxMenuItem*) NULL;
980 return m_buttonWidget;
981 }
982
983 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
984 {
985 wxMenuItem *item = (wxMenuItem *) node->Data ();
986 if (item->GetId() == id)
987 {
988 if (it)
989 *it = item;
990 return item->GetButtonWidget();
991 }
992
993 if (item->GetSubMenu())
994 {
995 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
996 if (w)
997 {
998 return w;
999 }
1000 }
1001 }// for()
1002
1003 if (it)
1004 *it = (wxMenuItem*) NULL;
1005 return (WXWidget) NULL;
1006 }
1007
1008 void wxMenu::SetBackgroundColour(const wxColour& col)
1009 {
1010 m_backgroundColour = col;
1011 if (m_menuWidget)
1012 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
1013 if (m_buttonWidget)
1014 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
1015
1016 wxNode* node = m_menuItems.First();
1017 while (node)
1018 {
1019 wxMenuItem* item = (wxMenuItem*) node->Data();
1020 if (item->GetButtonWidget())
1021 {
1022 // This crashes because it uses gadgets
1023 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
1024 }
1025 if (item->GetSubMenu())
1026 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
1027 node = node->Next();
1028 }
1029 }
1030
1031 void wxMenu::SetForegroundColour(const wxColour& col)
1032 {
1033 m_foregroundColour = col;
1034 if (m_menuWidget)
1035 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
1036 if (m_buttonWidget)
1037 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
1038
1039 wxNode* node = m_menuItems.First();
1040 while (node)
1041 {
1042 wxMenuItem* item = (wxMenuItem*) node->Data();
1043 if (item->GetButtonWidget())
1044 {
1045 // This crashes because it uses gadgets
1046 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
1047 }
1048 if (item->GetSubMenu())
1049 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
1050 node = node->Next();
1051 }
1052 }
1053
1054 void wxMenu::ChangeFont(bool keepOriginalSize)
1055 {
1056 // lesstif 0.87 hangs when setting XmNfontList
1057 #ifndef LESSTIF_VERSION
1058 if (!m_font.Ok() || !m_menuWidget)
1059 return;
1060
1061 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
1062
1063 XtVaSetValues ((Widget) m_menuWidget,
1064 XmNfontList, fontList,
1065 NULL);
1066 if (m_buttonWidget)
1067 {
1068 XtVaSetValues ((Widget) m_buttonWidget,
1069 XmNfontList, fontList,
1070 NULL);
1071 }
1072 wxNode* node = m_menuItems.First();
1073 while (node)
1074 {
1075 wxMenuItem* item = (wxMenuItem*) node->Data();
1076 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
1077 {
1078 XtVaSetValues ((Widget) item->GetButtonWidget(),
1079 XmNfontList, fontList,
1080 NULL);
1081 }
1082 if (item->GetSubMenu())
1083 item->GetSubMenu()->ChangeFont(keepOriginalSize);
1084 node = node->Next();
1085 }
1086 #endif
1087 }
1088
1089 void wxMenu::SetFont(const wxFont& font)
1090 {
1091 m_font = font;
1092 ChangeFont();
1093 }
1094
1095 void wxMenuBar::SetBackgroundColour(const wxColour& col)
1096 {
1097
1098 m_backgroundColour = col;
1099 if (m_mainWidget)
1100 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
1101 int i;
1102 for (i = 0; i < m_menuCount; i++)
1103 m_menus[i]->SetBackgroundColour((wxColour&) col);
1104 }
1105
1106 void wxMenuBar::SetForegroundColour(const wxColour& col)
1107 {
1108 m_foregroundColour = col;
1109 if (m_mainWidget)
1110 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
1111
1112 int i;
1113 for (i = 0; i < m_menuCount; i++)
1114 m_menus[i]->SetForegroundColour((wxColour&) col);
1115 }
1116
1117 void wxMenuBar::ChangeFont(bool keepOriginalSize)
1118 {
1119 // Nothing to do for menubar, fonts are kept in wxMenus
1120 }
1121
1122 void wxMenuBar::SetFont(const wxFont& font)
1123 {
1124 m_font = font;
1125 ChangeFont();
1126
1127 int i;
1128 for (i = 0; i < m_menuCount; i++)
1129 m_menus[i]->SetFont(font);
1130 }
1131