]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/menu.cpp
use unsgined int instead of int for 1 bit bitfields (SGI CC warning fix)
[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// ============================================================================
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// For compilers that support precompilation, includes "wx.h".
26#include "wx/wxprec.h"
27
28#include "wx/defs.h"
29
30#include "wx/menu.h"
31#include "wx/menuitem.h"
32#include "wx/log.h"
33#include "wx/utils.h"
34#include "wx/app.h"
35#include "wx/frame.h"
36#include "wx/settings.h"
37
38#ifdef __VMS__
39#pragma message disable nosimpint
40#define XtDisplay XTDISPLAY
41#define XtWindow XTWINDOW
42#endif
43#include <Xm/Label.h>
44#include <Xm/LabelG.h>
45#include <Xm/CascadeBG.h>
46#include <Xm/CascadeB.h>
47#include <Xm/SeparatoG.h>
48#include <Xm/PushBG.h>
49#include <Xm/ToggleB.h>
50#include <Xm/ToggleBG.h>
51#include <Xm/RowColumn.h>
52#ifdef __VMS__
53#pragma message enable nosimpint
54#endif
55
56#include "wx/motif/private.h"
57
58// other standard headers
59#include <string.h>
60
61IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
62IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// Menus
70// ----------------------------------------------------------------------------
71
72// Construct a menu with optional title (then use append)
73void wxMenu::Init()
74{
75 // Motif-specific members
76 m_numColumns = 1;
77 m_menuWidget = (WXWidget) NULL;
78 m_popupShell = (WXWidget) NULL;
79 m_buttonWidget = (WXWidget) NULL;
80 m_menuId = 0;
81 m_topLevelMenu = (wxMenu*) NULL;
82 m_ownedByMenuBar = false;
83
84 if ( !m_title.empty() )
85 {
86 Append(-3, m_title) ;
87 AppendSeparator() ;
88 }
89
90 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
91 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
92 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
93}
94
95// The wxWindow destructor will take care of deleting the submenus.
96wxMenu::~wxMenu()
97{
98 if (m_menuWidget)
99 {
100 if (m_menuParent)
101 DestroyMenu(true);
102 else
103 DestroyMenu(false);
104 }
105
106 // Not sure if this is right
107 if (m_menuParent && m_menuBar)
108 {
109 m_menuParent = NULL;
110 // m_menuBar = NULL;
111 }
112}
113
114void wxMenu::Break()
115{
116 m_numColumns++;
117}
118
119// function appends a new item or submenu to the menu
120wxMenuItem* wxMenu::DoAppend(wxMenuItem *pItem)
121{
122 if (m_menuWidget)
123 {
124 // this is a dynamic Append
125 pItem->CreateItem(m_menuWidget, GetMenuBar(), m_topLevelMenu);
126 }
127
128 if ( pItem->IsSubMenu() )
129 {
130 pItem->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
131 }
132
133 return wxMenuBase::DoAppend(pItem);
134}
135
136wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
137{
138 item->DestroyItem(true);
139
140 return wxMenuBase::DoRemove(item);
141}
142
143wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
144{
145 if ( wxMenuBase::DoInsert(pos, item) )
146 return item;
147
148 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
149
150 return NULL;
151}
152
153void wxMenu::SetTitle(const wxString& label)
154{
155 m_title = label;
156
157 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
158 if ( !node )
159 return;
160
161 wxMenuItem *item = node->GetData ();
162 Widget widget = (Widget) item->GetButtonWidget();
163 if ( !widget )
164 return;
165
166 wxXmString title_str(label);
167 XtVaSetValues(widget,
168 XmNlabelString, title_str(),
169 NULL);
170}
171
172bool wxMenu::ProcessCommand(wxCommandEvent & event)
173{
174 bool processed = false;
175
176 // Try the menu's event handler
177 if ( !processed && GetEventHandler())
178 {
179 processed = GetEventHandler()->ProcessEvent(event);
180 }
181 // Try the window the menu was popped up from (and up
182 // through the hierarchy)
183 if ( !processed && GetInvokingWindow())
184 processed = GetInvokingWindow()->ProcessEvent(event);
185
186 return processed;
187}
188
189// ----------------------------------------------------------------------------
190// Menu Bar
191// ----------------------------------------------------------------------------
192
193void wxMenuBar::Init()
194{
195 m_eventHandler = this;
196 m_menuBarFrame = NULL;
197 m_mainWidget = (WXWidget) NULL;
198 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
199 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
200 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
201}
202
203wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxArrayString& titles, long WXUNUSED(style))
204{
205 wxASSERT( n == titles.GetCount() );
206
207 Init();
208
209 m_titles = titles;
210 for ( size_t i = 0; i < n; i++ )
211 m_menus.Append(menus[i]);
212}
213
214wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
215{
216 Init();
217
218 for ( size_t i = 0; i < n; i++ )
219 {
220 m_menus.Append(menus[i]);
221 m_titles.Add(titles[i]);
222 }
223}
224
225wxMenuBar::~wxMenuBar()
226{
227 // nothing to do: wxMenuBarBase will delete the menus
228}
229
230void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag))
231{
232 // wxFAIL_MSG("TODO");
233// wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
234}
235
236void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
237{
238 wxMenu *menu = GetMenu(pos);
239 if ( !menu )
240 return;
241
242 Widget w = (Widget)menu->GetButtonWidget();
243 if (w)
244 {
245 wxXmString label_str(label);
246
247 XtVaSetValues(w,
248 XmNlabelString, label_str(),
249 NULL);
250 }
251}
252
253wxString wxMenuBar::GetLabelTop(size_t pos) const
254{
255 wxMenu *menu = GetMenu(pos);
256 if ( menu )
257 {
258 Widget w = (Widget)menu->GetButtonWidget();
259 if (w)
260 {
261 XmString text;
262 XtVaGetValues(w,
263 XmNlabelString, &text,
264 NULL);
265
266 return wxXmStringToString( text );
267 }
268 }
269
270 return wxEmptyString;
271}
272
273bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
274{
275 wxCHECK_MSG( menu, false, wxT("invalid menu") );
276 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), false,
277 wxT("menu already appended") );
278
279 if ( m_menuBarFrame )
280 {
281 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, true);
282 wxCHECK_MSG( w, false, wxT("failed to create menu") );
283 menu->SetButtonWidget(w);
284 }
285
286 m_titles.Add(title);
287
288 return wxMenuBarBase::Append(menu, title);
289}
290
291bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
292{
293 if ( !wxMenuBarBase::Insert(pos, menu, title) )
294 return false;
295
296 wxFAIL_MSG(wxT("TODO"));
297
298 return false;
299}
300
301wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
302{
303 if ( !wxMenuBarBase::Replace(pos, menu, title) )
304 return NULL;
305
306 wxFAIL_MSG(wxT("TODO"));
307
308 return NULL;
309}
310
311wxMenu *wxMenuBar::Remove(size_t pos)
312{
313 wxMenu *menu = wxMenuBarBase::Remove(pos);
314 if ( !menu )
315 return NULL;
316
317 if ( m_menuBarFrame )
318 menu->DestroyMenu(true);
319
320 menu->SetMenuBar(NULL);
321
322 m_titles.RemoveAt(pos);
323
324 return menu;
325}
326
327// Find the menu menuString, item itemString, and return the item id.
328// Returns -1 if none found.
329int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
330{
331 char buf1[200];
332 char buf2[200];
333 wxStripMenuCodes (wxConstCast(menuString.c_str(), char), buf1);
334
335 size_t menuCount = GetMenuCount();
336 for (size_t i = 0; i < menuCount; i++)
337 {
338 wxStripMenuCodes (wxConstCast(m_titles[i].c_str(), char), buf2);
339 if (strcmp (buf1, buf2) == 0)
340 return m_menus.Item(i)->GetData()->FindItem (itemString);
341 }
342 return -1;
343}
344
345wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
346{
347 if (itemMenu)
348 *itemMenu = NULL;
349
350 wxMenuItem *item = NULL;
351 size_t menuCount = GetMenuCount();
352 for (size_t i = 0; i < menuCount; i++)
353 if ((item = m_menus.Item(i)->GetData()->FindItem(id, itemMenu)))
354 return item;
355 return NULL;
356}
357
358// Create menubar
359bool wxMenuBar::CreateMenuBar(wxFrame* parent)
360{
361 if (m_mainWidget)
362 {
363 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
364 /*
365 if (!XtIsManaged((Widget) m_mainWidget))
366 XtManageChild((Widget) m_mainWidget);
367 */
368 XtMapWidget((Widget) m_mainWidget);
369 return true;
370 }
371
372 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWidget(),
373 wxMOTIF_STR("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.
413bool 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// Since PopupMenu under Motif stills grab right mouse button events
440// after it was closed, we need to delete the associated widgets to
441// allow next PopUpMenu to appear...
442void wxMenu::DestroyWidgetAndDetach()
443{
444 if (GetMainWidget())
445 {
446 wxMenu *menuParent = GetParent();
447 if ( menuParent )
448 {
449 wxMenuItemList::compatibility_iterator node = menuParent->GetMenuItems().GetFirst();
450 while ( node )
451 {
452 if ( node->GetData()->GetSubMenu() == this )
453 {
454 delete node->GetData();
455 menuParent->GetMenuItems().Erase(node);
456
457 break;
458 }
459
460 node = node->GetNext();
461 }
462 }
463
464 DestroyMenu(true);
465 }
466
467 // Mark as no longer popped up
468 m_menuId = -1;
469}
470
471/*
472* Create a popup or pulldown menu.
473* Submenus of a popup will be pulldown.
474*
475*/
476
477WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, 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 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
521 node;
522 node = node->GetNext() )
523 {
524 wxMenuItem *item = node->GetData();
525
526 item->CreateItem(menu, menuBar, topMenu);
527 }
528
529 SetBackgroundColour(m_backgroundColour);
530 SetForegroundColour(m_foregroundColour);
531 SetFont(m_font);
532
533 return buttonWidget;
534}
535
536// Destroys the Motif implementation of the menu,
537// but maintains the wxWidgets data structures so we can
538// do a CreateMenu again.
539void wxMenu::DestroyMenu (bool full)
540{
541 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
542 node;
543 node = node->GetNext() )
544 {
545 wxMenuItem *item = node->GetData();
546 item->SetMenuBar((wxMenuBar*) NULL);
547
548 item->DestroyItem(full);
549 }
550
551 if (m_buttonWidget)
552 {
553 if (full)
554 {
555 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
556 XtDestroyWidget ((Widget) m_buttonWidget);
557 m_buttonWidget = (WXWidget) 0;
558 }
559 }
560 if (m_menuWidget && full)
561 {
562 XtDestroyWidget((Widget) m_menuWidget);
563 m_menuWidget = (WXWidget) NULL;
564 }
565}
566
567WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
568{
569 if (id == m_menuId)
570 {
571 if (it)
572 *it = (wxMenuItem*) NULL;
573 return m_buttonWidget;
574 }
575
576 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
577 node;
578 node = node->GetNext() )
579 {
580 wxMenuItem *item = node->GetData ();
581 if (item->GetId() == id)
582 {
583 if (it)
584 *it = item;
585 return item->GetButtonWidget();
586 }
587
588 if (item->GetSubMenu())
589 {
590 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
591 if (w)
592 {
593 return w;
594 }
595 }
596 }
597
598 if (it)
599 *it = (wxMenuItem*) NULL;
600 return (WXWidget) NULL;
601}
602
603void wxMenu::SetBackgroundColour(const wxColour& col)
604{
605 m_backgroundColour = col;
606 if (m_menuWidget)
607 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
608 if (m_buttonWidget)
609 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, true);
610
611 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
612 node;
613 node = node->GetNext() )
614 {
615 wxMenuItem* item = node->GetData();
616 if (item->GetButtonWidget())
617 {
618 // This crashes because it uses gadgets
619 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
620 }
621 if (item->GetSubMenu())
622 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
623 }
624}
625
626void wxMenu::SetForegroundColour(const wxColour& col)
627{
628 m_foregroundColour = col;
629 if (m_menuWidget)
630 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
631 if (m_buttonWidget)
632 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
633
634 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
635 node;
636 node = node->GetNext() )
637 {
638 wxMenuItem* item = node->GetData();
639 if (item->GetButtonWidget())
640 {
641 // This crashes because it uses gadgets
642 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
643 }
644 if (item->GetSubMenu())
645 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
646 }
647}
648
649void wxMenu::ChangeFont(bool keepOriginalSize)
650{
651 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
652#if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
653 if (!m_font.Ok() || !m_menuWidget)
654 return;
655
656 Display* dpy = XtDisplay((Widget) m_menuWidget);
657
658 XtVaSetValues ((Widget) m_menuWidget,
659 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
660 NULL);
661 if (m_buttonWidget)
662 {
663 XtVaSetValues ((Widget) m_buttonWidget,
664 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
665 NULL);
666 }
667
668 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
669 node;
670 node = node->GetNext() )
671 {
672 wxMenuItem* item = node->GetData();
673 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
674 {
675 XtVaSetValues ((Widget) item->GetButtonWidget(),
676 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
677 NULL);
678 }
679 if (item->GetSubMenu())
680 item->GetSubMenu()->ChangeFont(keepOriginalSize);
681 }
682#endif
683}
684
685void wxMenu::SetFont(const wxFont& font)
686{
687 m_font = font;
688 ChangeFont();
689}
690
691bool wxMenuBar::SetBackgroundColour(const wxColour& col)
692{
693 m_backgroundColour = col;
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
704bool wxMenuBar::SetForegroundColour(const wxColour& col)
705{
706 m_foregroundColour = col;
707 if (m_mainWidget)
708 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
709
710 size_t menuCount = GetMenuCount();
711 for (size_t i = 0; i < menuCount; i++)
712 m_menus.Item(i)->GetData()->SetForegroundColour((wxColour&) col);
713
714 return true;
715}
716
717void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
718{
719 // Nothing to do for menubar, fonts are kept in wxMenus
720}
721
722bool wxMenuBar::SetFont(const wxFont& font)
723{
724 m_font = font;
725 ChangeFont();
726
727 size_t menuCount = GetMenuCount();
728 for (size_t i = 0; i < menuCount; i++)
729 m_menus.Item(i)->GetData()->SetFont(font);
730
731 return true;
732}
733