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