]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[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()
63 {
64 // Motif-specific members
65 m_numColumns = 1;
66 m_menuWidget = (WXWidget) NULL;
67 m_popupShell = (WXWidget) NULL;
68 m_buttonWidget = (WXWidget) NULL;
69 m_menuId = 0;
70 m_topLevelMenu = (wxMenu*) NULL;
71 m_ownedByMenuBar = FALSE;
72
73 if ( !!m_title )
74 {
75 Append(wxID_SEPARATOR, m_title) ;
76 AppendSeparator() ;
77 }
78
79 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
80 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
81 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
82 }
83
84 // The wxWindow destructor will take care of deleting the submenus.
85 wxMenu::~wxMenu()
86 {
87 if (m_menuWidget)
88 {
89 if (m_menuParent)
90 DestroyMenu(TRUE);
91 else
92 DestroyMenu(FALSE);
93 }
94
95 // Not sure if this is right
96 if (m_menuParent && m_menuBar)
97 {
98 m_menuParent = NULL;
99 // m_menuBar = NULL;
100 }
101 }
102
103 void wxMenu::Break()
104 {
105 m_numColumns++;
106 }
107
108 // function appends a new item or submenu to the menu
109 bool wxMenu::DoAppend(wxMenuItem *pItem)
110 {
111 if (m_menuWidget)
112 {
113 // this is a dynamic Append
114 pItem->CreateItem(m_menuWidget, m_menuBar, m_topLevelMenu);
115 }
116
117 if ( pItem->IsSubMenu() )
118 {
119 pItem->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
120 }
121
122 return wxMenuBase::DoAppend(pItem);
123 }
124
125 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
126 {
127 item->DestroyItem(TRUE);
128
129 return wxMenuBase::DoRemove(item);
130 }
131
132 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
133 {
134 if ( !wxMenuBase::DoInsert(pos, item) )
135 return FALSE;
136
137 wxFAIL_MSG(wxT("not implemented"));
138
139 return FALSE;
140 }
141
142 void wxMenu::SetTitle(const wxString& label)
143 {
144 m_title = label;
145
146 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
147 if ( !node )
148 return;
149
150 wxMenuItem *item = node->GetData ();
151 Widget widget = (Widget) item->GetButtonWidget();
152 if ( !widget )
153 return;
154
155 wxXmString title_str(label);
156 XtVaSetValues(widget,
157 XmNlabelString, title_str(),
158 NULL);
159 }
160
161 bool wxMenu::ProcessCommand(wxCommandEvent & event)
162 {
163 bool processed = FALSE;
164
165 #if wxUSE_MENU_CALLBACK
166 // Try a callback
167 if (m_callback)
168 {
169 (void) (*(m_callback)) (*this, event);
170 processed = TRUE;
171 }
172 #endif // wxUSE_MENU_CALLBACK
173
174 // Try the menu's event handler
175 if ( !processed && GetEventHandler())
176 {
177 processed = GetEventHandler()->ProcessEvent(event);
178 }
179 // Try the window the menu was popped up from (and up
180 // through the hierarchy)
181 if ( !processed && GetInvokingWindow())
182 processed = GetInvokingWindow()->ProcessEvent(event);
183
184 return processed;
185 }
186
187 // ----------------------------------------------------------------------------
188 // Menu Bar
189 // ----------------------------------------------------------------------------
190
191 void wxMenuBar::Init()
192 {
193 m_eventHandler = this;
194 m_menuBarFrame = NULL;
195 m_mainWidget = (WXWidget) NULL;
196 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
197 m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
198 m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
199 }
200
201 wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
202 {
203 Init();
204
205 for ( int i = 0; i < n; i++ )
206 {
207 m_menus.Append(menus[i]);
208 m_titles.Add(titles[i]);
209 }
210 }
211
212 wxMenuBar::~wxMenuBar()
213 {
214 // nothing to do: wxMenuBarBase will delete the menus
215 }
216
217 void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
218 {
219 // wxFAIL_MSG("TODO");
220 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
221 }
222
223 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
224 {
225 wxMenu *menu = GetMenu(pos);
226 if ( !menu )
227 return;
228
229 Widget w = (Widget)menu->GetButtonWidget();
230 if (w)
231 {
232 wxXmString label_str(label);
233
234 XtVaSetValues(w,
235 XmNlabelString, label_str(),
236 NULL);
237 }
238 }
239
240 wxString wxMenuBar::GetLabelTop(size_t pos) const
241 {
242 wxString str;
243
244 wxMenu *menu = GetMenu(pos);
245 if ( menu )
246 {
247 Widget w = (Widget)menu->GetButtonWidget();
248 if (w)
249 {
250 XmString text;
251 XtVaGetValues(w,
252 XmNlabelString, &text,
253 NULL);
254
255 char *s;
256 if ( XmStringGetLtoR(text, XmSTRING_DEFAULT_CHARSET, &s) )
257 {
258 str = s;
259
260 XtFree(s);
261 }
262 }
263 }
264
265 return str;
266 }
267
268 bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
269 {
270 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
271 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
272 wxT("menu already appended") );
273
274 if ( m_menuBarFrame )
275 {
276 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
277 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
278 menu->SetButtonWidget(w);
279 }
280
281 menu->SetMenuBar(this);
282
283 m_titles.Add(title);
284
285 return wxMenuBarBase::Append(menu, title);
286 }
287
288 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
289 {
290 if ( !wxMenuBarBase::Insert(pos, menu, title) )
291 return FALSE;
292
293 wxFAIL_MSG(wxT("TODO"));
294
295 return FALSE;
296 }
297
298 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
299 {
300 if ( !wxMenuBarBase::Replace(pos, menu, title) )
301 return FALSE;
302
303 wxFAIL_MSG(wxT("TODO"));
304
305 return NULL;
306 }
307
308 wxMenu *wxMenuBar::Remove(size_t pos)
309 {
310 wxMenu *menu = wxMenuBarBase::Remove(pos);
311 if ( !menu )
312 return NULL;
313
314 if ( m_menuBarFrame )
315 menu->DestroyMenu(TRUE);
316
317 menu->SetMenuBar(NULL);
318
319 m_titles.Remove(pos);
320
321 return menu;
322 }
323
324 // Find the menu menuString, item itemString, and return the item id.
325 // Returns -1 if none found.
326 int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
327 {
328 char buf1[200];
329 char buf2[200];
330 wxStripMenuCodes ((char *)(const char *)menuString, buf1);
331
332 size_t menuCount = GetMenuCount();
333 for (size_t i = 0; i < menuCount; i++)
334 {
335 wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
336 if (strcmp (buf1, buf2) == 0)
337 return m_menus[i]->FindItem (itemString);
338 }
339 return -1;
340 }
341
342 wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
343 {
344 if (itemMenu)
345 *itemMenu = NULL;
346
347 wxMenuItem *item = NULL;
348 size_t menuCount = GetMenuCount();
349 for (size_t i = 0; i < menuCount; i++)
350 if ((item = m_menus[i]->FindItem(id, itemMenu)))
351 return item;
352 return NULL;
353 }
354
355 // Create menubar
356 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
357 {
358 if (m_mainWidget)
359 {
360 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
361 /*
362 if (!XtIsManaged((Widget) m_mainWidget))
363 XtManageChild((Widget) m_mainWidget);
364 */
365 XtMapWidget((Widget) m_mainWidget);
366 return TRUE;
367 }
368
369 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0);
370 m_mainWidget = (WXWidget) menuBarW;
371
372 size_t menuCount = GetMenuCount();
373 for (size_t i = 0; i < menuCount; i++)
374 {
375 wxMenu *menu = GetMenu(i);
376 wxString title(m_titles[i]);
377 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
378
379 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
380 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
381
382 // tear off menu support
383 #if (XmVersion >= 1002)
384 if ( menu->IsTearOff() )
385 {
386 XtVaSetValues(GetWidget(menu),
387 XmNtearOffModel, XmTEAR_OFF_ENABLED,
388 NULL);
389 Widget tearOff = XmGetTearOffControl(GetWidget(menu));
390 wxDoChangeForegroundColour((Widget) tearOff, m_foregroundColour);
391 wxDoChangeBackgroundColour((Widget) tearOff, m_backgroundColour, TRUE);
392 #endif
393 }
394 }
395
396 SetBackgroundColour(m_backgroundColour);
397 SetForegroundColour(m_foregroundColour);
398 SetFont(m_font);
399
400 XtVaSetValues((Widget) parent->GetMainWindowWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
401 XtRealizeWidget ((Widget) menuBarW);
402 XtManageChild ((Widget) menuBarW);
403 SetMenuBarFrame(parent);
404
405 return TRUE;
406 }
407
408 // Destroy menubar, but keep data structures intact so we can recreate it.
409 bool wxMenuBar::DestroyMenuBar()
410 {
411 if (!m_mainWidget)
412 {
413 SetMenuBarFrame((wxFrame*) NULL);
414 return FALSE;
415 }
416
417 XtUnmanageChild ((Widget) m_mainWidget);
418 XtUnrealizeWidget ((Widget) m_mainWidget);
419
420 size_t menuCount = GetMenuCount();
421 for (size_t i = 0; i < menuCount; i++)
422 {
423 wxMenu *menu = GetMenu(i);
424 menu->DestroyMenu(TRUE);
425
426 }
427 XtDestroyWidget((Widget) m_mainWidget);
428 m_mainWidget = (WXWidget) 0;
429
430 SetMenuBarFrame((wxFrame*) NULL);
431
432 return TRUE;
433 }
434
435 //// Motif-specific
436 static XtWorkProcId WorkProcMenuId;
437
438 /* Since PopupMenu under Motif stills grab right mouse button events
439 * after it was closed, we need to delete the associated widgets to
440 * allow next PopUpMenu to appear...
441 */
442
443 int PostDeletionOfMenu( XtPointer* clientData )
444 {
445 XtRemoveWorkProc(WorkProcMenuId);
446 wxMenu *menu = (wxMenu *)clientData;
447
448 if (menu->GetMainWidget())
449 {
450 wxMenu *menuParent = menu->GetParent();
451 if ( menuParent )
452 {
453 wxMenuItemList::Node *node = menuParent->GetMenuItems().GetFirst();
454 while ( node )
455 {
456 if ( node->GetData()->GetSubMenu() == menu )
457 {
458 menuParent->GetMenuItems().DeleteNode(node);
459
460 break;
461 }
462
463 node = node->GetNext();
464 }
465 }
466
467 menu->DestroyMenu(TRUE);
468 }
469
470 // Mark as no longer popped up
471 menu->m_menuId = -1;
472
473 return TRUE;
474 }
475
476 void
477 wxMenuPopdownCallback(Widget WXUNUSED(w), XtPointer clientData,
478 XtPointer WXUNUSED(ptr))
479 {
480 wxMenu *menu = (wxMenu *)clientData;
481
482 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
483 /* Since Callbacks of MenuItems are not yet processed, we put a
484 * background job which will be done when system will be idle.
485 * What awful hack!! :(
486 */
487
488 WorkProcMenuId = XtAppAddWorkProc(
489 (XtAppContext) wxTheApp->GetAppContext(),
490 (XtWorkProc) PostDeletionOfMenu,
491 (XtPointer) menu );
492 // Apparently not found in Motif headers
493 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
494 }
495
496 /*
497 * Create a popup or pulldown menu.
498 * Submenus of a popup will be pulldown.
499 *
500 */
501
502 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
503 {
504 Widget menu = (Widget) 0;
505 Widget buttonWidget = (Widget) 0;
506 Arg args[5];
507 XtSetArg (args[0], XmNnumColumns, m_numColumns);
508 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
509
510 if (!pullDown)
511 {
512 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
513 XtAddCallback(menu,
514 XmNunmapCallback,
515 (XtCallbackProc)wxMenuPopdownCallback,
516 (XtPointer)this);
517 }
518 else
519 {
520 char mnem = wxFindMnemonic (title);
521 wxStripMenuCodes ((char*) (const char*) title, wxBuffer);
522
523 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
524
525 wxString title2(wxStripMenuCodes(title));
526 wxXmString label_str(title2);
527 buttonWidget = XtVaCreateManagedWidget(title2,
528 #if wxUSE_GADGETS
529 xmCascadeButtonGadgetClass, (Widget) parent,
530 #else
531 xmCascadeButtonWidgetClass, (Widget) parent,
532 #endif
533 XmNlabelString, label_str(),
534 XmNsubMenuId, menu,
535 NULL);
536
537 if (mnem != 0)
538 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
539 }
540
541 m_menuWidget = (WXWidget) menu;
542
543 m_menuBar = menuBar;
544 m_topLevelMenu = topMenu;
545
546 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
547 node;
548 node = node->GetNext() )
549 {
550 wxMenuItem *item = node->GetData();
551
552 item->CreateItem(menu, menuBar, topMenu);
553 }
554
555 SetBackgroundColour(m_backgroundColour);
556 SetForegroundColour(m_foregroundColour);
557 SetFont(m_font);
558
559 return buttonWidget;
560 }
561
562 // Destroys the Motif implementation of the menu,
563 // but maintains the wxWindows data structures so we can
564 // do a CreateMenu again.
565 void wxMenu::DestroyMenu (bool full)
566 {
567 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
568 node;
569 node = node->GetNext() )
570 {
571 wxMenuItem *item = node->GetData();
572 item->SetMenuBar((wxMenuBar*) NULL);
573
574 item->DestroyItem(full);
575 }
576
577 if (m_buttonWidget)
578 {
579 if (full)
580 {
581 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
582 XtDestroyWidget ((Widget) m_buttonWidget);
583 m_buttonWidget = (WXWidget) 0;
584 }
585 }
586 if (m_menuWidget && full)
587 {
588 XtDestroyWidget((Widget) m_menuWidget);
589 m_menuWidget = (WXWidget) NULL;
590 }
591 }
592
593 WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
594 {
595 if (id == m_menuId)
596 {
597 if (it)
598 *it = (wxMenuItem*) NULL;
599 return m_buttonWidget;
600 }
601
602 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
603 node;
604 node = node->GetNext() )
605 {
606 wxMenuItem *item = node->GetData ();
607 if (item->GetId() == id)
608 {
609 if (it)
610 *it = item;
611 return item->GetButtonWidget();
612 }
613
614 if (item->GetSubMenu())
615 {
616 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
617 if (w)
618 {
619 return w;
620 }
621 }
622 }
623
624 if (it)
625 *it = (wxMenuItem*) NULL;
626 return (WXWidget) NULL;
627 }
628
629 void wxMenu::SetBackgroundColour(const wxColour& col)
630 {
631 m_backgroundColour = col;
632 if (m_menuWidget)
633 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
634 if (m_buttonWidget)
635 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
636
637 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
638 node;
639 node = node->GetNext() )
640 {
641 wxMenuItem* item = node->GetData();
642 if (item->GetButtonWidget())
643 {
644 // This crashes because it uses gadgets
645 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
646 }
647 if (item->GetSubMenu())
648 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
649 }
650 }
651
652 void wxMenu::SetForegroundColour(const wxColour& col)
653 {
654 m_foregroundColour = col;
655 if (m_menuWidget)
656 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
657 if (m_buttonWidget)
658 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
659
660 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
661 node;
662 node = node->GetNext() )
663 {
664 wxMenuItem* item = node->GetData();
665 if (item->GetButtonWidget())
666 {
667 // This crashes because it uses gadgets
668 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
669 }
670 if (item->GetSubMenu())
671 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
672 }
673 }
674
675 void wxMenu::ChangeFont(bool keepOriginalSize)
676 {
677 // lesstif 0.87 hangs when setting XmNfontList
678 #ifndef LESSTIF_VERSION
679 if (!m_font.Ok() || !m_menuWidget)
680 return;
681
682 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
683
684 XtVaSetValues ((Widget) m_menuWidget,
685 XmNfontList, fontList,
686 NULL);
687 if (m_buttonWidget)
688 {
689 XtVaSetValues ((Widget) m_buttonWidget,
690 XmNfontList, fontList,
691 NULL);
692 }
693
694 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
695 node;
696 node = node->GetNext() )
697 {
698 wxMenuItem* item = node->GetData();
699 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
700 {
701 XtVaSetValues ((Widget) item->GetButtonWidget(),
702 XmNfontList, fontList,
703 NULL);
704 }
705 if (item->GetSubMenu())
706 item->GetSubMenu()->ChangeFont(keepOriginalSize);
707 }
708 #endif
709 }
710
711 void wxMenu::SetFont(const wxFont& font)
712 {
713 m_font = font;
714 ChangeFont();
715 }
716
717 bool wxMenuBar::SetBackgroundColour(const wxColour& col)
718 {
719 m_backgroundColour = col;
720 if (m_mainWidget)
721 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
722
723 size_t menuCount = GetMenuCount();
724 for (size_t i = 0; i < menuCount; i++)
725 m_menus[i]->SetBackgroundColour((wxColour&) col);
726
727 return TRUE;
728 }
729
730 bool wxMenuBar::SetForegroundColour(const wxColour& col)
731 {
732 m_foregroundColour = col;
733 if (m_mainWidget)
734 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
735
736 size_t menuCount = GetMenuCount();
737 for (size_t i = 0; i < menuCount; i++)
738 m_menus[i]->SetForegroundColour((wxColour&) col);
739
740 return TRUE;
741 }
742
743 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
744 {
745 // Nothing to do for menubar, fonts are kept in wxMenus
746 }
747
748 bool wxMenuBar::SetFont(const wxFont& font)
749 {
750 m_font = font;
751 ChangeFont();
752
753 size_t menuCount = GetMenuCount();
754 for (size_t i = 0; i < menuCount; i++)
755 m_menus[i]->SetFont(font);
756
757 return TRUE;
758 }
759