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