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