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