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