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