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