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