]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/menu.cpp
Trace module initialization and cleanup.
[wxWidgets.git] / src / motif / menu.cpp
... / ...
CommitLineData
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// 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/defs.h"
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#define XtDisplay XTDISPLAY
36#define XtWindow XTWINDOW
37#endif
38#include <Xm/Label.h>
39#include <Xm/LabelG.h>
40#include <Xm/CascadeBG.h>
41#include <Xm/CascadeB.h>
42#include <Xm/SeparatoG.h>
43#include <Xm/PushBG.h>
44#include <Xm/ToggleB.h>
45#include <Xm/ToggleBG.h>
46#include <Xm/RowColumn.h>
47#ifdef __VMS__
48#pragma message enable nosimpint
49#endif
50
51#include "wx/motif/private.h"
52
53// other standard headers
54#include <string.h>
55
56IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
57IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
58
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// Menus
65// ----------------------------------------------------------------------------
66
67// Construct a menu with optional title (then use append)
68void 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.empty() )
80 {
81 Append(-3, m_title) ;
82 AppendSeparator() ;
83 }
84
85 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
86 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
87 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
88}
89
90// The wxWindow destructor will take care of deleting the submenus.
91wxMenu::~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
109void wxMenu::Break()
110{
111 m_numColumns++;
112}
113
114// function appends a new item or submenu to the menu
115wxMenuItem* wxMenu::DoAppend(wxMenuItem *pItem)
116{
117 if (m_menuWidget)
118 {
119 // this is a dynamic Append
120 pItem->CreateItem(m_menuWidget, GetMenuBar(), 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
131wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
132{
133 item->DestroyItem(true);
134
135 return wxMenuBase::DoRemove(item);
136}
137
138wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
139{
140 if ( wxMenuBase::DoInsert(pos, item) )
141 return item;
142
143 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
144
145 return NULL;
146}
147
148void wxMenu::SetTitle(const wxString& label)
149{
150 m_title = label;
151
152 wxMenuItemList::compatibility_iterator 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
167bool wxMenu::ProcessCommand(wxCommandEvent & event)
168{
169 bool processed = false;
170
171 // Try the menu's event handler
172 if ( !processed && GetEventHandler())
173 {
174 processed = GetEventHandler()->ProcessEvent(event);
175 }
176 // Try the window the menu was popped up from (and up
177 // through the hierarchy)
178 if ( !processed && GetInvokingWindow())
179 processed = GetInvokingWindow()->ProcessEvent(event);
180
181 return processed;
182}
183
184// ----------------------------------------------------------------------------
185// Menu Bar
186// ----------------------------------------------------------------------------
187
188void wxMenuBar::Init()
189{
190 m_eventHandler = this;
191 m_menuBarFrame = NULL;
192 m_mainWidget = (WXWidget) NULL;
193 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
194 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
195 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
196}
197
198wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxArrayString& titles, long WXUNUSED(style))
199{
200 wxASSERT( n == titles.GetCount() );
201
202 Init();
203
204 m_titles = titles;
205 for ( size_t i = 0; i < n; i++ )
206 m_menus.Append(menus[i]);
207}
208
209wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
210{
211 Init();
212
213 for ( size_t i = 0; i < n; i++ )
214 {
215 m_menus.Append(menus[i]);
216 m_titles.Add(titles[i]);
217 }
218}
219
220wxMenuBar::~wxMenuBar()
221{
222 // nothing to do: wxMenuBarBase will delete the menus
223}
224
225void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
226{
227 // wxFAIL_MSG("TODO");
228// wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
229}
230
231void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
232{
233 wxMenu *menu = GetMenu(pos);
234 if ( !menu )
235 return;
236
237 Widget w = (Widget)menu->GetButtonWidget();
238 if (w)
239 {
240 wxXmString label_str(label);
241
242 XtVaSetValues(w,
243 XmNlabelString, label_str(),
244 NULL);
245 }
246}
247
248wxString wxMenuBar::GetLabelTop(size_t pos) const
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 return wxXmStringToString( text );
262 }
263 }
264
265 return wxEmptyString;
266}
267
268bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
269{
270 wxCHECK_MSG( menu, false, wxT("invalid menu") );
271 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), false,
272 wxT("menu already appended") );
273
274 if ( m_menuBarFrame )
275 {
276 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, true);
277 wxCHECK_MSG( w, false, wxT("failed to create menu") );
278 menu->SetButtonWidget(w);
279 }
280
281 m_titles.Add(title);
282
283 return wxMenuBarBase::Append(menu, title);
284}
285
286bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
287{
288 if ( !wxMenuBarBase::Insert(pos, menu, title) )
289 return false;
290
291 wxFAIL_MSG(wxT("TODO"));
292
293 return false;
294}
295
296wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
297{
298 if ( !wxMenuBarBase::Replace(pos, menu, title) )
299 return NULL;
300
301 wxFAIL_MSG(wxT("TODO"));
302
303 return NULL;
304}
305
306wxMenu *wxMenuBar::Remove(size_t pos)
307{
308 wxMenu *menu = wxMenuBarBase::Remove(pos);
309 if ( !menu )
310 return NULL;
311
312 if ( m_menuBarFrame )
313 menu->DestroyMenu(true);
314
315 menu->SetMenuBar(NULL);
316
317 m_titles.RemoveAt(pos);
318
319 return menu;
320}
321
322// Find the menu menuString, item itemString, and return the item id.
323// Returns -1 if none found.
324int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
325{
326 char buf1[200];
327 char buf2[200];
328 wxStripMenuCodes (wxConstCast(menuString.c_str(), char), buf1);
329
330 size_t menuCount = GetMenuCount();
331 for (size_t i = 0; i < menuCount; i++)
332 {
333 wxStripMenuCodes (wxConstCast(m_titles[i].c_str(), char), buf2);
334 if (strcmp (buf1, buf2) == 0)
335 return m_menus.Item(i)->GetData()->FindItem (itemString);
336 }
337 return -1;
338}
339
340wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
341{
342 if (itemMenu)
343 *itemMenu = NULL;
344
345 wxMenuItem *item = NULL;
346 size_t menuCount = GetMenuCount();
347 for (size_t i = 0; i < menuCount; i++)
348 if ((item = m_menus.Item(i)->GetData()->FindItem(id, itemMenu)))
349 return item;
350 return NULL;
351}
352
353// Create menubar
354bool 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, 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.
408bool 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...
437void 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
472WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
473{
474 Widget menu = (Widget) 0;
475 Widget buttonWidget = (Widget) 0;
476 Arg args[5];
477 XtSetArg (args[0], XmNnumColumns, m_numColumns);
478 XtSetArg (args[1], XmNpacking, (m_numColumns > 1) ? XmPACK_COLUMN : XmPACK_TIGHT);
479
480 if (!pullDown)
481 {
482 menu = XmCreatePopupMenu ((Widget) parent, wxMOTIF_STR("popup"), args, 2);
483#if 0
484 XtAddCallback(menu,
485 XmNunmapCallback,
486 (XtCallbackProc)wxMenuPopdownCallback,
487 (XtPointer)this);
488#endif
489 }
490 else
491 {
492 char mnem = wxFindMnemonic (title);
493 menu = XmCreatePulldownMenu ((Widget) parent, wxMOTIF_STR("pulldown"), args, 2);
494
495 wxString title2(wxStripMenuCodes(title));
496 wxXmString label_str(title2);
497 buttonWidget = XtVaCreateManagedWidget(title2,
498#if wxUSE_GADGETS
499 xmCascadeButtonGadgetClass, (Widget) parent,
500#else
501 xmCascadeButtonWidgetClass, (Widget) parent,
502#endif
503 XmNlabelString, label_str(),
504 XmNsubMenuId, menu,
505 NULL);
506
507 if (mnem != 0)
508 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
509 }
510
511 m_menuWidget = (WXWidget) menu;
512
513 m_topLevelMenu = topMenu;
514
515 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
516 node;
517 node = node->GetNext() )
518 {
519 wxMenuItem *item = node->GetData();
520
521 item->CreateItem(menu, menuBar, topMenu);
522 }
523
524 SetBackgroundColour(m_backgroundColour);
525 SetForegroundColour(m_foregroundColour);
526 SetFont(m_font);
527
528 return buttonWidget;
529}
530
531// Destroys the Motif implementation of the menu,
532// but maintains the wxWidgets data structures so we can
533// do a CreateMenu again.
534void wxMenu::DestroyMenu (bool full)
535{
536 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
537 node;
538 node = node->GetNext() )
539 {
540 wxMenuItem *item = node->GetData();
541 item->SetMenuBar((wxMenuBar*) NULL);
542
543 item->DestroyItem(full);
544 }
545
546 if (m_buttonWidget)
547 {
548 if (full)
549 {
550 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
551 XtDestroyWidget ((Widget) m_buttonWidget);
552 m_buttonWidget = (WXWidget) 0;
553 }
554 }
555 if (m_menuWidget && full)
556 {
557 XtDestroyWidget((Widget) m_menuWidget);
558 m_menuWidget = (WXWidget) NULL;
559 }
560}
561
562WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
563{
564 if (id == m_menuId)
565 {
566 if (it)
567 *it = (wxMenuItem*) NULL;
568 return m_buttonWidget;
569 }
570
571 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
572 node;
573 node = node->GetNext() )
574 {
575 wxMenuItem *item = node->GetData ();
576 if (item->GetId() == id)
577 {
578 if (it)
579 *it = item;
580 return item->GetButtonWidget();
581 }
582
583 if (item->GetSubMenu())
584 {
585 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
586 if (w)
587 {
588 return w;
589 }
590 }
591 }
592
593 if (it)
594 *it = (wxMenuItem*) NULL;
595 return (WXWidget) NULL;
596}
597
598void wxMenu::SetBackgroundColour(const wxColour& col)
599{
600 m_backgroundColour = col;
601 if (m_menuWidget)
602 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
603 if (m_buttonWidget)
604 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, true);
605
606 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
607 node;
608 node = node->GetNext() )
609 {
610 wxMenuItem* item = node->GetData();
611 if (item->GetButtonWidget())
612 {
613 // This crashes because it uses gadgets
614 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
615 }
616 if (item->GetSubMenu())
617 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
618 }
619}
620
621void wxMenu::SetForegroundColour(const wxColour& col)
622{
623 m_foregroundColour = col;
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
644void 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#endif
678}
679
680void wxMenu::SetFont(const wxFont& font)
681{
682 m_font = font;
683 ChangeFont();
684}
685
686bool wxMenuBar::SetBackgroundColour(const wxColour& col)
687{
688 m_backgroundColour = col;
689 if (m_mainWidget)
690 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
691
692 size_t menuCount = GetMenuCount();
693 for (size_t i = 0; i < menuCount; i++)
694 m_menus.Item(i)->GetData()->SetBackgroundColour((wxColour&) col);
695
696 return true;
697}
698
699bool wxMenuBar::SetForegroundColour(const wxColour& col)
700{
701 m_foregroundColour = col;
702 if (m_mainWidget)
703 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
704
705 size_t menuCount = GetMenuCount();
706 for (size_t i = 0; i < menuCount; i++)
707 m_menus.Item(i)->GetData()->SetForegroundColour((wxColour&) col);
708
709 return true;
710}
711
712void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
713{
714 // Nothing to do for menubar, fonts are kept in wxMenus
715}
716
717bool wxMenuBar::SetFont(const wxFont& font)
718{
719 m_font = font;
720 ChangeFont();
721
722 size_t menuCount = GetMenuCount();
723 for (size_t i = 0; i < menuCount; i++)
724 m_menus.Item(i)->GetData()->SetFont(font);
725
726 return true;
727}
728