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