]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
Added some tentative wxMotif clipboard code; did some file formatting
[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 wxMenu::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
69 //// Motif-specific members
70 m_numColumns = 1;
71 m_menuWidget = (WXWidget) NULL;
72 m_popupShell = (WXWidget) NULL;
73 m_buttonWidget = (WXWidget) NULL;
74 m_menuId = 0;
75 m_topLevelMenu = (wxMenu*) NULL;
76 m_ownedByMenuBar = FALSE;
77 m_menuParent = (wxMenu*) NULL;
78 m_clientData = (void*) NULL;
79
80 if (m_title != "")
81 {
82 Append(ID_SEPARATOR, m_title) ;
83 AppendSeparator() ;
84 }
85 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
86 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
87 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
88
89 Callback(func);
90 }
91
92 // The wxWindow destructor will take care of deleting the submenus.
93 wxMenu::~wxMenu()
94 {
95 if (m_menuWidget)
96 {
97 if (m_menuParent)
98 DestroyMenu(TRUE);
99 else
100 DestroyMenu(FALSE);
101 }
102
103 // Not sure if this is right
104 if (m_menuParent && m_menuBar)
105 {
106 m_menuParent = NULL;
107 // m_menuBar = NULL;
108 }
109
110 wxNode *node = m_menuItems.First();
111 while (node)
112 {
113 wxMenuItem *item = (wxMenuItem *)node->Data();
114
115 /*
116 if (item->GetSubMenu())
117 item->DeleteSubMenu();
118 */
119
120 wxNode *next = node->Next();
121 delete item;
122 delete node;
123 node = next;
124 }
125 }
126
127 void wxMenu::Break()
128 {
129 m_numColumns ++;
130 }
131
132 // function appends a new item or submenu to the menu
133 void wxMenu::Append(wxMenuItem *pItem)
134 {
135 wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
136
137 m_menuItems.Append(pItem);
138
139 if (m_menuWidget)
140 pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append
141
142 m_noItems++;
143 }
144
145 void wxMenu::AppendSeparator()
146 {
147 Append(new wxMenuItem(this, ID_SEPARATOR));
148 }
149
150 // Pullright item
151 // N.B.: difference between old and new code.
152 // Old code stores subMenu in 'children' for later deletion,
153 // as well as in m_menuItems, whereas we only store it in
154 // m_menuItems here. What implications does this have?
155
156 void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu,
157 const wxString& helpString)
158 {
159 Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu));
160
161 subMenu->m_topLevelMenu = m_topLevelMenu;
162 }
163
164 // Ordinary menu item
165 void wxMenu::Append(int id, const wxString& label,
166 const wxString& helpString, bool checkable)
167 {
168 // 'checkable' parameter is useless for Windows.
169 Append(new wxMenuItem(this, id, label, helpString, checkable));
170 }
171
172 void wxMenu::Delete(int id)
173 {
174 wxNode *node;
175 wxMenuItem *item;
176 int pos;
177
178 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
179 {
180 item = (wxMenuItem *)node->Data();
181 if (item->GetId() == id)
182 break;
183 }
184
185 if (!node)
186 return;
187
188 item->DestroyItem(TRUE);
189
190 // See also old code - don't know if this is needed (seems redundant).
191 /*
192 if (item->GetSubMenu()) {
193 item->subMenu->top_level_menu = item->GetSubMenu();
194 item->subMenu->window_parent = NULL;
195 children->DeleteObject(item->GetSubMenu());
196 }
197 */
198
199 m_menuItems.DeleteNode(node);
200 delete item;
201 }
202
203 void wxMenu::Enable(int id, bool flag)
204 {
205 wxMenuItem *item = FindItemForId(id);
206 wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
207
208 item->Enable(flag);
209 }
210
211 bool wxMenu::Enabled(int Id) const
212 {
213 wxMenuItem *item = FindItemForId(Id);
214 wxCHECK( item != NULL, FALSE );
215
216 return item->IsEnabled();
217 }
218
219 void wxMenu::Check(int Id, bool Flag)
220 {
221 wxMenuItem *item = FindItemForId(Id);
222 wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" );
223
224 item->Check(Flag);
225 }
226
227 bool wxMenu::Checked(int id) const
228 {
229 wxMenuItem *item = FindItemForId(id);
230 wxCHECK( item != NULL, FALSE );
231
232 return item->IsChecked();
233 }
234
235 void wxMenu::SetTitle(const wxString& label)
236 {
237 m_title = label ;
238
239 wxNode *node = m_menuItems.First ();
240 if (!node)
241 return;
242
243 wxMenuItem *item = (wxMenuItem *) node->Data ();
244 Widget widget = (Widget) item->GetButtonWidget();
245 if (!widget)
246 return;
247
248 XmString title_str = XmStringCreateSimple ((char*) (const char*) label);
249 XtVaSetValues (widget,
250 XmNlabelString, title_str,
251 NULL);
252 // TODO: should we delete title_str now?
253 }
254
255 const wxString wxMenu::GetTitle() const
256 {
257 return m_title;
258 }
259
260 void wxMenu::SetLabel(int id, const wxString& label)
261 {
262 wxMenuItem *item = FindItemForId(id);
263 if (item == (wxMenuItem*) NULL)
264 return;
265
266 item->SetLabel(label);
267 }
268
269 wxString wxMenu::GetLabel(int id) const
270 {
271 wxMenuItem *it = NULL;
272 WXWidget w = FindMenuItem (id, &it);
273 if (w)
274 {
275 XmString text;
276 char *s;
277 XtVaGetValues ((Widget) w,
278 XmNlabelString, &text,
279 NULL);
280
281 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
282 {
283 wxString str(s);
284 XtFree (s);
285 return str;
286 }
287 else
288 {
289 XmStringFree (text);
290 return wxEmptyString;
291 }
292 }
293 else
294 return wxEmptyString;
295 }
296
297 // Finds the item id matching the given string, -1 if not found.
298 int wxMenu::FindItem (const wxString& itemString) const
299 {
300 char buf1[200];
301 char buf2[200];
302 wxStripMenuCodes ((char *)(const char *)itemString, buf1);
303
304 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
305 {
306 wxMenuItem *item = (wxMenuItem *) node->Data ();
307 if (item->GetSubMenu())
308 {
309 int ans = item->GetSubMenu()->FindItem(itemString);
310 if (ans > -1)
311 return ans;
312 }
313 if ( !item->IsSeparator() )
314 {
315 wxStripMenuCodes((char *)item->GetName().c_str(), buf2);
316 if (strcmp(buf1, buf2) == 0)
317 return item->GetId();
318 }
319 }
320
321 return -1;
322 }
323
324 wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
325 {
326 if (itemMenu)
327 *itemMenu = NULL;
328 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
329 {
330 wxMenuItem *item = (wxMenuItem *) node->Data ();
331
332 if (item->GetId() == itemId)
333 {
334 if (itemMenu)
335 *itemMenu = (wxMenu *) this;
336 return item;
337 }
338
339 if (item->GetSubMenu())
340 {
341 wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
342 if (ans)
343 return ans;
344 }
345 }
346
347 if (itemMenu)
348 *itemMenu = NULL;
349 return NULL;
350 }
351
352 void wxMenu::SetHelpString(int itemId, const wxString& helpString)
353 {
354 wxMenuItem *item = FindItemForId (itemId);
355 if (item)
356 item->SetHelp(helpString);
357 }
358
359 wxString wxMenu::GetHelpString (int itemId) const
360 {
361 wxMenuItem *item = FindItemForId (itemId);
362 wxString str("");
363 return (item == NULL) ? str : item->GetHelp();
364 }
365
366 void wxMenu::ProcessCommand(wxCommandEvent & event)
367 {
368 bool processed = FALSE;
369
370 // Try a callback
371 if (m_callback)
372 {
373 (void) (*(m_callback)) (*this, event);
374 processed = TRUE;
375 }
376
377 // Try the menu's event handler
378 if ( !processed && GetEventHandler())
379 {
380 processed = GetEventHandler()->ProcessEvent(event);
381 }
382 /* TODO
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
390 bool wxWindow::PopupMenu(wxMenu *menu, int x, int y)
391 {
392 Widget widget = (Widget) GetMainWidget();
393
394 /* The menuId field seems to be usused, so we'll use it to
395 indicate whether a menu is popped up or not:
396 0: Not currently created as a popup
397 -1: Created as a popup, but not active
398 1: Active popup.
399 */
400
401 if (menu->GetParent() && (menu->GetId() != -1))
402 return FALSE;
403
404 if (menu->GetMainWidget()) {
405 menu->DestroyMenu(TRUE);
406 }
407
408 wxWindow *parent = this;
409
410 menu->SetId(1); /* Mark as popped-up */
411 menu->CreateMenu(NULL, widget, menu);
412 // menu->SetParent(parent);
413 // parent->children->Append(menu); // Store menu for later deletion
414
415 Widget menuWidget = (Widget) menu->GetMainWidget();
416
417 int rootX = 0;
418 int rootY = 0;
419
420 int deviceX = x;
421 int deviceY = y;
422 /*
423 if (this->IsKindOf(CLASSINFO(wxCanvas)))
424 {
425 wxCanvas *canvas = (wxCanvas *) this;
426 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
427 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
428 }
429 */
430
431 Display *display = XtDisplay (widget);
432 Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget));
433 Window thisWindow = XtWindow (widget);
434 Window childWindow;
435 XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY,
436 &rootX, &rootY, &childWindow);
437
438 XButtonPressedEvent event;
439 event.type = ButtonPress;
440 event.button = 1;
441
442 event.x = deviceX;
443 event.y = deviceY;
444
445 event.x_root = rootX;
446 event.y_root = rootY;
447
448 XmMenuPosition (menuWidget, &event);
449 XtManageChild (menuWidget);
450
451 return TRUE;
452 }
453
454 // Menu Bar
455 wxMenuBar::wxMenuBar()
456 {
457 m_eventHandler = this;
458 m_menuCount = 0;
459 m_menus = NULL;
460 m_titles = NULL;
461 m_menuBarFrame = NULL;
462 m_mainWidget = (WXWidget) NULL;
463 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
464 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
465 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
466 }
467
468 wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
469 {
470 m_eventHandler = this;
471 m_menuCount = n;
472 m_menus = menus;
473 m_titles = new wxString[n];
474 int i;
475 for ( i = 0; i < n; i++ )
476 m_titles[i] = titles[i];
477 m_menuBarFrame = NULL;
478 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
479 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
480 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
481 }
482
483 wxMenuBar::~wxMenuBar()
484 {
485 int i;
486 for (i = 0; i < m_menuCount; i++)
487 {
488 delete m_menus[i];
489 }
490 delete[] m_menus;
491 delete[] m_titles;
492 }
493
494 // Must only be used AFTER menu has been attached to frame,
495 // otherwise use individual menus to enable/disable items
496 void wxMenuBar::Enable(int id, bool flag)
497 {
498 wxMenu *itemMenu = NULL;
499 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
500 if (!item)
501 return;
502 item->Enable(flag);
503 }
504
505 void wxMenuBar::EnableTop(int pos, bool flag)
506 {
507 // TODO
508 }
509
510 // Must only be used AFTER menu has been attached to frame,
511 // otherwise use individual menus
512 void wxMenuBar::Check(int id, bool flag)
513 {
514 wxMenu *itemMenu = NULL;
515 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
516 if (!item)
517 return;
518
519 if (!item->IsCheckable())
520 return ;
521
522 item->Check(flag);
523 }
524
525 bool wxMenuBar::Checked(int id) const
526 {
527 wxMenu *itemMenu = NULL;
528 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
529 if (!item)
530 return FALSE;
531
532 return item->IsChecked();
533 }
534
535 bool wxMenuBar::Enabled(int id) const
536 {
537 wxMenu *itemMenu = NULL;
538 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
539 if (!item)
540 return FALSE;
541
542 return item->IsEnabled();
543 }
544
545 void wxMenuBar::SetLabel(int id, const wxString& label)
546 {
547 wxMenu *itemMenu = NULL;
548 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
549
550 if (!item)
551 return;
552
553 item->SetLabel(label);
554 }
555
556 wxString wxMenuBar::GetLabel(int id) const
557 {
558 wxMenu *itemMenu = NULL;
559 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
560
561 if (!item)
562 return wxString("");
563
564 return item->GetLabel();
565 }
566
567 void wxMenuBar::SetLabelTop(int pos, const wxString& label)
568 {
569 wxASSERT( (pos < m_menuCount) );
570
571 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
572 if (w)
573 {
574 XmString label_str = XmStringCreateSimple ((char*) (const char*) label);
575 XtVaSetValues (w,
576 XmNlabelString, label_str,
577 NULL);
578 XmStringFree (label_str);
579 }
580 }
581
582 wxString wxMenuBar::GetLabelTop(int pos) const
583 {
584 wxASSERT( (pos < m_menuCount) );
585
586 Widget w = (Widget) m_menus[pos]->GetButtonWidget();
587 if (w)
588 {
589 XmString text;
590 char *s;
591 XtVaGetValues (w,
592 XmNlabelString, &text,
593 NULL);
594
595 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
596 {
597 wxString str(s);
598 XtFree (s);
599 return str;
600 }
601 else
602 {
603 return wxEmptyString;
604 }
605 }
606 else
607 return wxEmptyString;
608
609 }
610
611 bool wxMenuBar::OnDelete(wxMenu *menu, int pos)
612 {
613 // Only applies to dynamic deletion (when set in frame)
614 if (!m_menuBarFrame)
615 return TRUE;
616
617 menu->DestroyMenu(TRUE);
618 return TRUE;
619 }
620
621 bool wxMenuBar::OnAppend(wxMenu *menu, const char *title)
622 {
623 // Only applies to dynamic append (when set in frame)
624 if (!m_menuBarFrame)
625 return TRUE;
626
627 // Probably should be an assert here
628 if (menu->GetParent())
629 return FALSE;
630
631 // Has already been appended
632 if (menu->GetButtonWidget())
633 return FALSE;
634
635 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
636 menu->SetButtonWidget(w);
637
638 return TRUE;
639 }
640
641 void wxMenuBar::Append (wxMenu * menu, const wxString& title)
642 {
643 if (!OnAppend(menu, title))
644 return;
645
646 m_menuCount ++;
647 wxMenu **new_menus = new wxMenu *[m_menuCount];
648 wxString *new_titles = new wxString[m_menuCount];
649 int i;
650
651 for (i = 0; i < m_menuCount - 1; i++)
652 {
653 new_menus[i] = m_menus[i];
654 m_menus[i] = NULL;
655 new_titles[i] = m_titles[i];
656 m_titles[i] = "";
657 }
658 if (m_menus)
659 {
660 delete[]m_menus;
661 delete[]m_titles;
662 }
663 m_menus = new_menus;
664 m_titles = new_titles;
665
666 m_menus[m_menuCount - 1] = (wxMenu *)menu;
667 m_titles[m_menuCount - 1] = title;
668
669 menu->SetMenuBar(this);
670 menu->SetParent(this);
671 }
672
673 void wxMenuBar::Delete(wxMenu * menu, int i)
674 {
675 int j;
676 int ii = (int) i;
677
678 if (menu != 0)
679 {
680 for (ii = 0; ii < m_menuCount; ii++)
681 {
682 if (m_menus[ii] == menu)
683 break;
684 }
685 if (ii >= m_menuCount)
686 return;
687 } else
688 {
689 if (ii < 0 || ii >= m_menuCount)
690 return;
691 menu = m_menus[ii];
692 }
693
694 if (!OnDelete(menu, ii))
695 return;
696
697 menu->SetParent((wxEvtHandler*) NULL);
698
699 -- m_menuCount;
700 for (j = ii; j < m_menuCount; j++)
701 {
702 m_menus[j] = m_menus[j + 1];
703 m_titles[j] = m_titles[j + 1];
704 }
705 }
706
707 // Find the menu menuString, item itemString, and return the item id.
708 // Returns -1 if none found.
709 int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
710 {
711 char buf1[200];
712 char buf2[200];
713 wxStripMenuCodes ((char *)(const char *)menuString, buf1);
714 int i;
715 for (i = 0; i < m_menuCount; i++)
716 {
717 wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
718 if (strcmp (buf1, buf2) == 0)
719 return m_menus[i]->FindItem (itemString);
720 }
721 return -1;
722 }
723
724 wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const
725 {
726 if (itemMenu)
727 *itemMenu = NULL;
728
729 wxMenuItem *item = NULL;
730 int i;
731 for (i = 0; i < m_menuCount; i++)
732 if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
733 return item;
734 return NULL;
735 }
736
737 void wxMenuBar::SetHelpString (int id, const wxString& helpString)
738 {
739 int i;
740 for (i = 0; i < m_menuCount; i++)
741 {
742 if (m_menus[i]->FindItemForId (id))
743 {
744 m_menus[i]->SetHelpString (id, helpString);
745 return;
746 }
747 }
748 }
749
750 wxString wxMenuBar::GetHelpString (int id) const
751 {
752 int i;
753 for (i = 0; i < m_menuCount; i++)
754 {
755 if (m_menus[i]->FindItemForId (id))
756 return wxString(m_menus[i]->GetHelpString (id));
757 }
758 return wxString("");
759 }
760
761 // Create menubar
762 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
763 {
764 if (m_mainWidget)
765 {
766 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
767 /*
768 if (!XtIsManaged((Widget) m_mainWidget))
769 XtManageChild((Widget) m_mainWidget);
770 */
771 XtMapWidget((Widget) m_mainWidget);
772 return TRUE;
773 }
774
775 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
776 m_mainWidget = (WXWidget) menuBarW;
777
778 int i;
779 for (i = 0; i < GetMenuCount(); i++)
780 {
781 wxMenu *menu = GetMenu(i);
782 wxString title(m_titles[i]);
783 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
784
785 /*
786 * COMMENT THIS OUT IF YOU DON'T LIKE A RIGHT-JUSTIFIED HELP MENU
787 */
788 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
789
790 if (strcmp (wxBuffer, "Help") == 0)
791 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
792 }
793
794 SetBackgroundColour(m_backgroundColour);
795 SetForegroundColour(m_foregroundColour);
796 SetFont(m_font);
797
798 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
799 XtRealizeWidget ((Widget) menuBarW);
800 XtManageChild ((Widget) menuBarW);
801 SetMenuBarFrame(parent);
802
803 return TRUE;
804 }
805
806 // Destroy menubar, but keep data structures intact so we can recreate it.
807 bool wxMenuBar::DestroyMenuBar()
808 {
809 if (!m_mainWidget)
810 {
811 SetMenuBarFrame((wxFrame*) NULL);
812 return FALSE;
813 }
814
815 XtUnmanageChild ((Widget) m_mainWidget);
816 XtUnrealizeWidget ((Widget) m_mainWidget);
817
818 int i;
819 for (i = 0; i < GetMenuCount(); i++)
820 {
821 wxMenu *menu = GetMenu(i);
822 menu->DestroyMenu(TRUE);
823
824 }
825 XtDestroyWidget((Widget) m_mainWidget);
826 m_mainWidget = (WXWidget) 0;
827
828 SetMenuBarFrame((wxFrame*) NULL);
829
830 return TRUE;
831 }
832
833 //// Motif-specific
834
835 extern wxApp *wxTheApp;
836 static XtWorkProcId WorkProcMenuId;
837
838 /* Since PopupMenu under Motif stills grab right mouse button events
839 * after it was closed, we need to delete the associated widgets to
840 * allow next PopUpMenu to appear...
841 */
842
843 int PostDeletionOfMenu( XtPointer* clientData )
844 {
845 XtRemoveWorkProc(WorkProcMenuId);
846 wxMenu *menu = (wxMenu *)clientData;
847
848 if (menu->GetMainWidget()) {
849 if (menu->GetParent())
850 {
851 wxList& list = menu->GetParent()->GetItems();
852 list.DeleteObject(menu);
853 }
854 menu->DestroyMenu(TRUE);
855 }
856 /* Mark as no longer popped up */
857 menu->m_menuId = -1;
858 return TRUE;
859 }
860
861 void
862 wxMenuPopdownCallback(Widget w, XtPointer clientData,
863 XtPointer ptr)
864 {
865 wxMenu *menu = (wxMenu *)clientData;
866
867 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
868 /* Since Callbacks of MenuItems are not yet processed, we put a
869 * background job which will be done when system will be idle.
870 * What awful hack!! :(
871 */
872
873 WorkProcMenuId = XtAppAddWorkProc(
874 (XtAppContext) wxTheApp->GetAppContext(),
875 (XtWorkProc) PostDeletionOfMenu,
876 (XtPointer) menu );
877 // Apparently not found in Motif headers
878 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
879 }
880
881 /*
882 * Create a popup or pulldown menu.
883 * Submenus of a popup will be pulldown.
884 *
885 */
886
887 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
888 {
889 Widget menu = (Widget) 0;
890 Widget buttonWidget = (Widget) 0;
891 Arg args[5];
892 XtSetArg (args[0], XmNnumColumns, m_numColumns);
893 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
894
895 if (!pullDown)
896 {
897 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
898 XtAddCallback(menu,
899 XmNunmapCallback,
900 (XtCallbackProc)wxMenuPopdownCallback,
901 (XtPointer)this);
902 }
903 else
904 {
905 char mnem = wxFindMnemonic (title);
906 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
907
908 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
909
910 XmString label_str = XmStringCreateSimple (wxBuffer);
911 buttonWidget = XtVaCreateManagedWidget (wxBuffer,
912 #if wxUSE_GADGETS
913 xmCascadeButtonGadgetClass, (Widget) parent,
914 #else
915 xmCascadeButtonWidgetClass, (Widget) parent,
916 #endif
917 XmNlabelString, label_str,
918 XmNsubMenuId, menu,
919 NULL);
920
921 if (mnem != 0)
922 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
923
924 XmStringFree (label_str);
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