]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
Moved wxToolBarSimple to deprecated library
[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 #include "wx/defs.h"
26
27 #include "wx/menu.h"
28 #include "wx/menuitem.h"
29 #include "wx/log.h"
30 #include "wx/utils.h"
31 #include "wx/app.h"
32 #include "wx/frame.h"
33 #include "wx/settings.h"
34
35 #ifdef __VMS__
36 #pragma message disable nosimpint
37 #define XtDisplay XTDISPLAY
38 #define XtWindow XTWINDOW
39 #endif
40 #include <Xm/Label.h>
41 #include <Xm/LabelG.h>
42 #include <Xm/CascadeBG.h>
43 #include <Xm/CascadeB.h>
44 #include <Xm/SeparatoG.h>
45 #include <Xm/PushBG.h>
46 #include <Xm/ToggleB.h>
47 #include <Xm/ToggleBG.h>
48 #include <Xm/RowColumn.h>
49 #ifdef __VMS__
50 #pragma message enable nosimpint
51 #endif
52
53 #include "wx/motif/private.h"
54
55 // other standard headers
56 #include <string.h>
57
58 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
59 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // Menus
67 // ----------------------------------------------------------------------------
68
69 // Construct a menu with optional title (then use append)
70 void wxMenu::Init()
71 {
72 // Motif-specific members
73 m_numColumns = 1;
74 m_menuWidget = (WXWidget) NULL;
75 m_popupShell = (WXWidget) NULL;
76 m_buttonWidget = (WXWidget) NULL;
77 m_menuId = 0;
78 m_topLevelMenu = (wxMenu*) NULL;
79 m_ownedByMenuBar = FALSE;
80
81 if ( !!m_title )
82 {
83 Append(wxID_SEPARATOR, m_title) ;
84 AppendSeparator() ;
85 }
86
87 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
88 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
89 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
90 }
91
92 // The wxWindow destructor will take care of deleting the submenus.
93 wxMenu::~wxMenu()
94 {
95 if (m_menuWidget)
96 {
97 if (m_menuParent)
98 DestroyMenu(TRUE);
99 else
100 DestroyMenu(FALSE);
101 }
102
103 // Not sure if this is right
104 if (m_menuParent && m_menuBar)
105 {
106 m_menuParent = NULL;
107 // m_menuBar = NULL;
108 }
109 }
110
111 void wxMenu::Break()
112 {
113 m_numColumns++;
114 }
115
116 // function appends a new item or submenu to the menu
117 bool wxMenu::DoAppend(wxMenuItem *pItem)
118 {
119 if (m_menuWidget)
120 {
121 // this is a dynamic Append
122 pItem->CreateItem(m_menuWidget, m_menuBar, m_topLevelMenu);
123 }
124
125 if ( pItem->IsSubMenu() )
126 {
127 pItem->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
128 }
129
130 return wxMenuBase::DoAppend(pItem);
131 }
132
133 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
134 {
135 item->DestroyItem(TRUE);
136
137 return wxMenuBase::DoRemove(item);
138 }
139
140 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
141 {
142 if ( wxMenuBase::DoInsert(pos, item) )
143 return TRUE;
144
145 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
146
147 return FALSE;
148 }
149
150 void wxMenu::SetTitle(const wxString& label)
151 {
152 m_title = label;
153
154 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
155 if ( !node )
156 return;
157
158 wxMenuItem *item = node->GetData ();
159 Widget widget = (Widget) item->GetButtonWidget();
160 if ( !widget )
161 return;
162
163 wxXmString title_str(label);
164 XtVaSetValues(widget,
165 XmNlabelString, title_str(),
166 NULL);
167 }
168
169 bool wxMenu::ProcessCommand(wxCommandEvent & event)
170 {
171 bool processed = FALSE;
172
173 #if wxUSE_MENU_CALLBACK
174 // Try a callback
175 if (m_callback)
176 {
177 (void) (*(m_callback)) (*this, event);
178 processed = TRUE;
179 }
180 #endif // wxUSE_MENU_CALLBACK
181
182 // Try the menu's event handler
183 if ( !processed && GetEventHandler())
184 {
185 processed = GetEventHandler()->ProcessEvent(event);
186 }
187 // Try the window the menu was popped up from (and up
188 // through the hierarchy)
189 if ( !processed && GetInvokingWindow())
190 processed = GetInvokingWindow()->ProcessEvent(event);
191
192 return processed;
193 }
194
195 // ----------------------------------------------------------------------------
196 // Menu Bar
197 // ----------------------------------------------------------------------------
198
199 void wxMenuBar::Init()
200 {
201 m_eventHandler = this;
202 m_menuBarFrame = NULL;
203 m_mainWidget = (WXWidget) NULL;
204 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
205 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
206 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
207 }
208
209 wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
210 {
211 Init();
212
213 for ( int 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 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
271 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
272 wxT("menu already appended") );
273
274 if ( m_menuBarFrame )
275 {
276 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
277 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
278 menu->SetButtonWidget(w);
279 }
280
281 //menu->SetMenuBar(this);
282
283 m_titles.Add(title);
284
285 return wxMenuBarBase::Append(menu, title);
286 }
287
288 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
289 {
290 if ( !wxMenuBarBase::Insert(pos, menu, title) )
291 return FALSE;
292
293 wxFAIL_MSG(wxT("TODO"));
294
295 return FALSE;
296 }
297
298 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
299 {
300 if ( !wxMenuBarBase::Replace(pos, menu, title) )
301 return NULL;
302
303 wxFAIL_MSG(wxT("TODO"));
304
305 return NULL;
306 }
307
308 wxMenu *wxMenuBar::Remove(size_t pos)
309 {
310 wxMenu *menu = wxMenuBarBase::Remove(pos);
311 if ( !menu )
312 return NULL;
313
314 if ( m_menuBarFrame )
315 menu->DestroyMenu(TRUE);
316
317 menu->SetMenuBar(NULL);
318
319 m_titles.RemoveAt(pos);
320
321 return menu;
322 }
323
324 // Find the menu menuString, item itemString, and return the item id.
325 // Returns -1 if none found.
326 int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
327 {
328 char buf1[200];
329 char buf2[200];
330 wxStripMenuCodes (wxConstCast(menuString.c_str(), char), buf1);
331
332 size_t menuCount = GetMenuCount();
333 for (size_t i = 0; i < menuCount; i++)
334 {
335 wxStripMenuCodes (wxConstCast(m_titles[i].c_str(), char), buf2);
336 if (strcmp (buf1, buf2) == 0)
337 return m_menus.Item(i)->GetData()->FindItem (itemString);
338 }
339 return -1;
340 }
341
342 wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
343 {
344 if (itemMenu)
345 *itemMenu = NULL;
346
347 wxMenuItem *item = NULL;
348 size_t menuCount = GetMenuCount();
349 for (size_t i = 0; i < menuCount; i++)
350 if ((item = m_menus.Item(i)->GetData()->FindItem(id, itemMenu)))
351 return item;
352 return NULL;
353 }
354
355 // Create menubar
356 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
357 {
358 if (m_mainWidget)
359 {
360 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
361 /*
362 if (!XtIsManaged((Widget) m_mainWidget))
363 XtManageChild((Widget) m_mainWidget);
364 */
365 XtMapWidget((Widget) m_mainWidget);
366 return TRUE;
367 }
368
369 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWidget(), "MenuBar", NULL, 0);
370 m_mainWidget = (WXWidget) menuBarW;
371
372 size_t menuCount = GetMenuCount();
373 for (size_t i = 0; i < menuCount; i++)
374 {
375 wxMenu *menu = GetMenu(i);
376 wxString title(m_titles[i]);
377 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
378
379 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
380 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
381
382 // tear off menu support
383 #if (XmVersion >= 1002)
384 if ( menu->IsTearOff() )
385 {
386 XtVaSetValues(GetWidget(menu),
387 XmNtearOffModel, XmTEAR_OFF_ENABLED,
388 NULL);
389 Widget tearOff = XmGetTearOffControl(GetWidget(menu));
390 wxDoChangeForegroundColour((Widget) tearOff, m_foregroundColour);
391 wxDoChangeBackgroundColour((Widget) tearOff, m_backgroundColour, TRUE);
392 #endif
393 }
394 }
395
396 SetBackgroundColour(m_backgroundColour);
397 SetForegroundColour(m_foregroundColour);
398 SetFont(m_font);
399
400 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
401 XtRealizeWidget ((Widget) menuBarW);
402 XtManageChild ((Widget) menuBarW);
403 SetMenuBarFrame(parent);
404
405 return TRUE;
406 }
407
408 // Destroy menubar, but keep data structures intact so we can recreate it.
409 bool wxMenuBar::DestroyMenuBar()
410 {
411 if (!m_mainWidget)
412 {
413 SetMenuBarFrame((wxFrame*) NULL);
414 return FALSE;
415 }
416
417 XtUnmanageChild ((Widget) m_mainWidget);
418 XtUnrealizeWidget ((Widget) m_mainWidget);
419
420 size_t menuCount = GetMenuCount();
421 for (size_t i = 0; i < menuCount; i++)
422 {
423 wxMenu *menu = GetMenu(i);
424 menu->DestroyMenu(TRUE);
425
426 }
427 XtDestroyWidget((Widget) m_mainWidget);
428 m_mainWidget = (WXWidget) 0;
429
430 SetMenuBarFrame((wxFrame*) NULL);
431
432 return TRUE;
433 }
434
435 // Since PopupMenu under Motif stills grab right mouse button events
436 // after it was closed, we need to delete the associated widgets to
437 // allow next PopUpMenu to appear...
438 void wxMenu::DestroyWidgetAndDetach()
439 {
440 if (GetMainWidget())
441 {
442 wxMenu *menuParent = GetParent();
443 if ( menuParent )
444 {
445 wxMenuItemList::compatibility_iterator node = menuParent->GetMenuItems().GetFirst();
446 while ( node )
447 {
448 if ( node->GetData()->GetSubMenu() == this )
449 {
450 delete node->GetData();
451 menuParent->GetMenuItems().Erase(node);
452
453 break;
454 }
455
456 node = node->GetNext();
457 }
458 }
459
460 DestroyMenu(TRUE);
461 }
462
463 // Mark as no longer popped up
464 m_menuId = -1;
465 }
466
467 /*
468 * Create a popup or pulldown menu.
469 * Submenus of a popup will be pulldown.
470 *
471 */
472
473 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, 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, "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, "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_menuBar = menuBar;
515 m_topLevelMenu = topMenu;
516
517 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
518 node;
519 node = node->GetNext() )
520 {
521 wxMenuItem *item = node->GetData();
522
523 item->CreateItem(menu, menuBar, topMenu);
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 wxWindows 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
649 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
650 if (!m_font.Ok() || !m_menuWidget)
651 return;
652
653 WXFontType fontType = m_font.GetFontType(XtDisplay((Widget) m_menuWidget));
654
655 XtVaSetValues ((Widget) m_menuWidget,
656 wxFont::GetFontTag(), fontType,
657 NULL);
658 if (m_buttonWidget)
659 {
660 XtVaSetValues ((Widget) m_buttonWidget,
661 wxFont::GetFontTag(), fontType,
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(), fontType,
674 NULL);
675 }
676 if (item->GetSubMenu())
677 item->GetSubMenu()->ChangeFont(keepOriginalSize);
678 }
679 #endif
680 }
681
682 void wxMenu::SetFont(const wxFont& font)
683 {
684 m_font = font;
685 ChangeFont();
686 }
687
688 bool wxMenuBar::SetBackgroundColour(const wxColour& col)
689 {
690 m_backgroundColour = col;
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 m_foregroundColour = col;
704 if (m_mainWidget)
705 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
706
707 size_t menuCount = GetMenuCount();
708 for (size_t i = 0; i < menuCount; i++)
709 m_menus.Item(i)->GetData()->SetForegroundColour((wxColour&) col);
710
711 return TRUE;
712 }
713
714 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
715 {
716 // Nothing to do for menubar, fonts are kept in wxMenus
717 }
718
719 bool wxMenuBar::SetFont(const wxFont& font)
720 {
721 m_font = font;
722 ChangeFont();
723
724 size_t menuCount = GetMenuCount();
725 for (size_t i = 0; i < menuCount; i++)
726 m_menus.Item(i)->GetData()->SetFont(font);
727
728 return TRUE;
729 }
730