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