]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
attempt to work around SDK specific problems
[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 wxMenuItem* 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 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
141 {
142 if ( wxMenuBase::DoInsert(pos, item) )
143 return item;
144
145 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
146
147 return NULL;
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 wxArrayString& titles)
210 {
211 wxASSERT( size_t(n) == titles.GetCount() );
212
213 Init();
214
215 m_titles = titles;
216 for ( int i = 0; i < n; i++ )
217 m_menus.Append(menus[i]);
218 }
219
220 wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
221 {
222 Init();
223
224 for ( int i = 0; i < n; i++ )
225 {
226 m_menus.Append(menus[i]);
227 m_titles.Add(titles[i]);
228 }
229 }
230
231 wxMenuBar::~wxMenuBar()
232 {
233 // nothing to do: wxMenuBarBase will delete the menus
234 }
235
236 void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
237 {
238 // wxFAIL_MSG("TODO");
239 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
240 }
241
242 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
243 {
244 wxMenu *menu = GetMenu(pos);
245 if ( !menu )
246 return;
247
248 Widget w = (Widget)menu->GetButtonWidget();
249 if (w)
250 {
251 wxXmString label_str(label);
252
253 XtVaSetValues(w,
254 XmNlabelString, label_str(),
255 NULL);
256 }
257 }
258
259 wxString wxMenuBar::GetLabelTop(size_t pos) const
260 {
261 wxMenu *menu = GetMenu(pos);
262 if ( menu )
263 {
264 Widget w = (Widget)menu->GetButtonWidget();
265 if (w)
266 {
267 XmString text;
268 XtVaGetValues(w,
269 XmNlabelString, &text,
270 NULL);
271
272 return wxXmStringToString( text );
273 }
274 }
275
276 return wxEmptyString;
277 }
278
279 bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
280 {
281 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
282 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
283 wxT("menu already appended") );
284
285 if ( m_menuBarFrame )
286 {
287 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
288 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
289 menu->SetButtonWidget(w);
290 }
291
292 m_titles.Add(title);
293
294 return wxMenuBarBase::Append(menu, title);
295 }
296
297 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
298 {
299 if ( !wxMenuBarBase::Insert(pos, menu, title) )
300 return FALSE;
301
302 wxFAIL_MSG(wxT("TODO"));
303
304 return FALSE;
305 }
306
307 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
308 {
309 if ( !wxMenuBarBase::Replace(pos, menu, title) )
310 return NULL;
311
312 wxFAIL_MSG(wxT("TODO"));
313
314 return NULL;
315 }
316
317 wxMenu *wxMenuBar::Remove(size_t pos)
318 {
319 wxMenu *menu = wxMenuBarBase::Remove(pos);
320 if ( !menu )
321 return NULL;
322
323 if ( m_menuBarFrame )
324 menu->DestroyMenu(TRUE);
325
326 menu->SetMenuBar(NULL);
327
328 m_titles.RemoveAt(pos);
329
330 return menu;
331 }
332
333 // Find the menu menuString, item itemString, and return the item id.
334 // Returns -1 if none found.
335 int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
336 {
337 char buf1[200];
338 char buf2[200];
339 wxStripMenuCodes (wxConstCast(menuString.c_str(), char), buf1);
340
341 size_t menuCount = GetMenuCount();
342 for (size_t i = 0; i < menuCount; i++)
343 {
344 wxStripMenuCodes (wxConstCast(m_titles[i].c_str(), char), buf2);
345 if (strcmp (buf1, buf2) == 0)
346 return m_menus.Item(i)->GetData()->FindItem (itemString);
347 }
348 return -1;
349 }
350
351 wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
352 {
353 if (itemMenu)
354 *itemMenu = NULL;
355
356 wxMenuItem *item = NULL;
357 size_t menuCount = GetMenuCount();
358 for (size_t i = 0; i < menuCount; i++)
359 if ((item = m_menus.Item(i)->GetData()->FindItem(id, itemMenu)))
360 return item;
361 return NULL;
362 }
363
364 // Create menubar
365 bool wxMenuBar::CreateMenuBar(wxFrame* parent)
366 {
367 if (m_mainWidget)
368 {
369 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
370 /*
371 if (!XtIsManaged((Widget) m_mainWidget))
372 XtManageChild((Widget) m_mainWidget);
373 */
374 XtMapWidget((Widget) m_mainWidget);
375 return TRUE;
376 }
377
378 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWidget(), "MenuBar", NULL, 0);
379 m_mainWidget = (WXWidget) menuBarW;
380
381 size_t menuCount = GetMenuCount();
382 for (size_t i = 0; i < menuCount; i++)
383 {
384 wxMenu *menu = GetMenu(i);
385 wxString title(m_titles[i]);
386 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
387
388 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
389 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
390
391 // tear off menu support
392 #if (XmVersion >= 1002)
393 if ( menu->IsTearOff() )
394 {
395 XtVaSetValues(GetWidget(menu),
396 XmNtearOffModel, XmTEAR_OFF_ENABLED,
397 NULL);
398 Widget tearOff = XmGetTearOffControl(GetWidget(menu));
399 wxDoChangeForegroundColour((Widget) tearOff, m_foregroundColour);
400 wxDoChangeBackgroundColour((Widget) tearOff, m_backgroundColour, TRUE);
401 #endif
402 }
403 }
404
405 SetBackgroundColour(m_backgroundColour);
406 SetForegroundColour(m_foregroundColour);
407 SetFont(m_font);
408
409 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
410 XtRealizeWidget ((Widget) menuBarW);
411 XtManageChild ((Widget) menuBarW);
412 SetMenuBarFrame(parent);
413
414 return TRUE;
415 }
416
417 // Destroy menubar, but keep data structures intact so we can recreate it.
418 bool wxMenuBar::DestroyMenuBar()
419 {
420 if (!m_mainWidget)
421 {
422 SetMenuBarFrame((wxFrame*) NULL);
423 return FALSE;
424 }
425
426 XtUnmanageChild ((Widget) m_mainWidget);
427 XtUnrealizeWidget ((Widget) m_mainWidget);
428
429 size_t menuCount = GetMenuCount();
430 for (size_t i = 0; i < menuCount; i++)
431 {
432 wxMenu *menu = GetMenu(i);
433 menu->DestroyMenu(TRUE);
434
435 }
436 XtDestroyWidget((Widget) m_mainWidget);
437 m_mainWidget = (WXWidget) 0;
438
439 SetMenuBarFrame((wxFrame*) NULL);
440
441 return TRUE;
442 }
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 void wxMenu::DestroyWidgetAndDetach()
448 {
449 if (GetMainWidget())
450 {
451 wxMenu *menuParent = GetParent();
452 if ( menuParent )
453 {
454 wxMenuItemList::compatibility_iterator node = menuParent->GetMenuItems().GetFirst();
455 while ( node )
456 {
457 if ( node->GetData()->GetSubMenu() == this )
458 {
459 delete node->GetData();
460 menuParent->GetMenuItems().Erase(node);
461
462 break;
463 }
464
465 node = node->GetNext();
466 }
467 }
468
469 DestroyMenu(TRUE);
470 }
471
472 // Mark as no longer popped up
473 m_menuId = -1;
474 }
475
476 /*
477 * Create a popup or pulldown menu.
478 * Submenus of a popup will be pulldown.
479 *
480 */
481
482 WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
483 {
484 Widget menu = (Widget) 0;
485 Widget buttonWidget = (Widget) 0;
486 Arg args[5];
487 XtSetArg (args[0], XmNnumColumns, m_numColumns);
488 XtSetArg (args[1], XmNpacking, (m_numColumns > 1) ? XmPACK_COLUMN : XmPACK_TIGHT);
489
490 if (!pullDown)
491 {
492 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
493 #if 0
494 XtAddCallback(menu,
495 XmNunmapCallback,
496 (XtCallbackProc)wxMenuPopdownCallback,
497 (XtPointer)this);
498 #endif
499 }
500 else
501 {
502 char mnem = wxFindMnemonic (title);
503 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
504
505 wxString title2(wxStripMenuCodes(title));
506 wxXmString label_str(title2);
507 buttonWidget = XtVaCreateManagedWidget(title2,
508 #if wxUSE_GADGETS
509 xmCascadeButtonGadgetClass, (Widget) parent,
510 #else
511 xmCascadeButtonWidgetClass, (Widget) parent,
512 #endif
513 XmNlabelString, label_str(),
514 XmNsubMenuId, menu,
515 NULL);
516
517 if (mnem != 0)
518 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
519 }
520
521 m_menuWidget = (WXWidget) menu;
522
523 m_topLevelMenu = topMenu;
524
525 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
526 node;
527 node = node->GetNext() )
528 {
529 wxMenuItem *item = node->GetData();
530
531 item->CreateItem(menu, menuBar, topMenu);
532 }
533
534 SetBackgroundColour(m_backgroundColour);
535 SetForegroundColour(m_foregroundColour);
536 SetFont(m_font);
537
538 return buttonWidget;
539 }
540
541 // Destroys the Motif implementation of the menu,
542 // but maintains the wxWindows data structures so we can
543 // do a CreateMenu again.
544 void wxMenu::DestroyMenu (bool full)
545 {
546 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
547 node;
548 node = node->GetNext() )
549 {
550 wxMenuItem *item = node->GetData();
551 item->SetMenuBar((wxMenuBar*) NULL);
552
553 item->DestroyItem(full);
554 }
555
556 if (m_buttonWidget)
557 {
558 if (full)
559 {
560 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
561 XtDestroyWidget ((Widget) m_buttonWidget);
562 m_buttonWidget = (WXWidget) 0;
563 }
564 }
565 if (m_menuWidget && full)
566 {
567 XtDestroyWidget((Widget) m_menuWidget);
568 m_menuWidget = (WXWidget) NULL;
569 }
570 }
571
572 WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
573 {
574 if (id == m_menuId)
575 {
576 if (it)
577 *it = (wxMenuItem*) NULL;
578 return m_buttonWidget;
579 }
580
581 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
582 node;
583 node = node->GetNext() )
584 {
585 wxMenuItem *item = node->GetData ();
586 if (item->GetId() == id)
587 {
588 if (it)
589 *it = item;
590 return item->GetButtonWidget();
591 }
592
593 if (item->GetSubMenu())
594 {
595 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
596 if (w)
597 {
598 return w;
599 }
600 }
601 }
602
603 if (it)
604 *it = (wxMenuItem*) NULL;
605 return (WXWidget) NULL;
606 }
607
608 void wxMenu::SetBackgroundColour(const wxColour& col)
609 {
610 m_backgroundColour = col;
611 if (m_menuWidget)
612 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
613 if (m_buttonWidget)
614 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
615
616 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
617 node;
618 node = node->GetNext() )
619 {
620 wxMenuItem* item = node->GetData();
621 if (item->GetButtonWidget())
622 {
623 // This crashes because it uses gadgets
624 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
625 }
626 if (item->GetSubMenu())
627 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
628 }
629 }
630
631 void wxMenu::SetForegroundColour(const wxColour& col)
632 {
633 m_foregroundColour = col;
634 if (m_menuWidget)
635 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
636 if (m_buttonWidget)
637 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
638
639 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
640 node;
641 node = node->GetNext() )
642 {
643 wxMenuItem* item = node->GetData();
644 if (item->GetButtonWidget())
645 {
646 // This crashes because it uses gadgets
647 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
648 }
649 if (item->GetSubMenu())
650 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
651 }
652 }
653
654 void wxMenu::ChangeFont(bool keepOriginalSize)
655 {
656 // Lesstif 0.87 hangs here, but 0.93 does not
657 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
658 if (!m_font.Ok() || !m_menuWidget)
659 return;
660
661 WXFontType fontType = m_font.GetFontType(XtDisplay((Widget) m_menuWidget));
662
663 XtVaSetValues ((Widget) m_menuWidget,
664 wxFont::GetFontTag(), fontType,
665 NULL);
666 if (m_buttonWidget)
667 {
668 XtVaSetValues ((Widget) m_buttonWidget,
669 wxFont::GetFontTag(), fontType,
670 NULL);
671 }
672
673 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
674 node;
675 node = node->GetNext() )
676 {
677 wxMenuItem* item = node->GetData();
678 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
679 {
680 XtVaSetValues ((Widget) item->GetButtonWidget(),
681 wxFont::GetFontTag(), fontType,
682 NULL);
683 }
684 if (item->GetSubMenu())
685 item->GetSubMenu()->ChangeFont(keepOriginalSize);
686 }
687 #endif
688 }
689
690 void wxMenu::SetFont(const wxFont& font)
691 {
692 m_font = font;
693 ChangeFont();
694 }
695
696 bool wxMenuBar::SetBackgroundColour(const wxColour& col)
697 {
698 m_backgroundColour = col;
699 if (m_mainWidget)
700 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
701
702 size_t menuCount = GetMenuCount();
703 for (size_t i = 0; i < menuCount; i++)
704 m_menus.Item(i)->GetData()->SetBackgroundColour((wxColour&) col);
705
706 return TRUE;
707 }
708
709 bool wxMenuBar::SetForegroundColour(const wxColour& col)
710 {
711 m_foregroundColour = col;
712 if (m_mainWidget)
713 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
714
715 size_t menuCount = GetMenuCount();
716 for (size_t i = 0; i < menuCount; i++)
717 m_menus.Item(i)->GetData()->SetForegroundColour((wxColour&) col);
718
719 return TRUE;
720 }
721
722 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
723 {
724 // Nothing to do for menubar, fonts are kept in wxMenus
725 }
726
727 bool wxMenuBar::SetFont(const wxFont& font)
728 {
729 m_font = font;
730 ChangeFont();
731
732 size_t menuCount = GetMenuCount();
733 for (size_t i = 0; i < menuCount; i++)
734 m_menus.Item(i)->GetData()->SetFont(font);
735
736 return TRUE;
737 }
738