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