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