]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/menu.cpp
added an extra pixel to the margin in CalcSizeFromPage() (patch 1498847)
[wxWidgets.git] / src / motif / menu.cpp
... / ...
CommitLineData
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
57IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
58IMPLEMENT_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)
69void 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 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
87 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
88 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
89}
90
91// The wxWindow destructor will take care of deleting the submenus.
92wxMenu::~wxMenu()
93{
94 if (m_menuWidget)
95 {
96 if (m_menuParent)
97 DestroyMenu(true);
98 else
99 DestroyMenu(false);
100 }
101
102 // Not sure if this is right
103 if (m_menuParent && m_menuBar)
104 {
105 m_menuParent = NULL;
106 // m_menuBar = NULL;
107 }
108}
109
110void wxMenu::Break()
111{
112 m_numColumns++;
113}
114
115// function appends a new item or submenu to the menu
116wxMenuItem* wxMenu::DoAppend(wxMenuItem *pItem)
117{
118 return DoInsert(GetMenuItemCount(), pItem);
119}
120
121wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
122{
123 item->DestroyItem(true);
124
125 return wxMenuBase::DoRemove(item);
126}
127
128wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
129{
130 if (m_menuWidget)
131 {
132 // this is a dynamic Append
133#ifndef XmNpositionIndex
134 wxCHECK_MSG( pos == GetMenuItemCount(), -1, wxT("insert not implemented"));
135#endif
136 item->CreateItem(m_menuWidget, GetMenuBar(), m_topLevelMenu, pos);
137 }
138
139 if ( item->IsSubMenu() )
140 {
141 item->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
142 }
143
144 return pos == GetMenuItemCount() ? wxMenuBase::DoAppend(item) :
145 wxMenuBase::DoInsert(pos, item);
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 return Insert(GetMenuCount(), menu, title);
271}
272
273bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
274{
275 wxCHECK_MSG( pos <= GetMenuCount(), false, wxT("invalid position") );
276 wxCHECK_MSG( menu, false, wxT("invalid menu") );
277 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), false,
278 wxT("menu already appended") );
279
280 if ( m_menuBarFrame )
281 {
282 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu,
283 pos, title, true);
284 wxCHECK_MSG( w, false, wxT("failed to create menu") );
285 menu->SetButtonWidget(w);
286 }
287
288 m_titles.Insert(title, pos);
289
290 return wxMenuBarBase::Insert(pos, menu, title);
291}
292
293wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
294{
295 if ( !wxMenuBarBase::Replace(pos, menu, title) )
296 return NULL;
297
298 wxFAIL_MSG(wxT("TODO"));
299
300 return NULL;
301}
302
303wxMenu *wxMenuBar::Remove(size_t pos)
304{
305 wxMenu *menu = wxMenuBarBase::Remove(pos);
306 if ( !menu )
307 return NULL;
308
309 if ( m_menuBarFrame )
310 menu->DestroyMenu(true);
311
312 menu->SetMenuBar(NULL);
313
314 m_titles.RemoveAt(pos);
315
316 return menu;
317}
318
319// Find the menu menuString, item itemString, and return the item id.
320// Returns -1 if none found.
321int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
322{
323 char buf1[200];
324 char buf2[200];
325 wxStripMenuCodes (wxConstCast(menuString.c_str(), char), buf1);
326
327 size_t menuCount = GetMenuCount();
328 for (size_t i = 0; i < menuCount; i++)
329 {
330 wxStripMenuCodes (wxConstCast(m_titles[i].c_str(), char), buf2);
331 if (strcmp (buf1, buf2) == 0)
332 return m_menus.Item(i)->GetData()->FindItem (itemString);
333 }
334 return -1;
335}
336
337wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
338{
339 if (itemMenu)
340 *itemMenu = NULL;
341
342 size_t menuCount = GetMenuCount();
343 for (size_t i = 0; i < menuCount; i++)
344 {
345 wxMenuItem *item = m_menus.Item(i)->GetData()->FindItem(id, itemMenu);
346 if (item) return item;
347 }
348
349 return NULL;
350}
351
352// Create menubar
353bool wxMenuBar::CreateMenuBar(wxFrame* parent)
354{
355 if (m_mainWidget)
356 {
357 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
358 /*
359 if (!XtIsManaged((Widget) m_mainWidget))
360 XtManageChild((Widget) m_mainWidget);
361 */
362 XtMapWidget((Widget) m_mainWidget);
363 return true;
364 }
365
366 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWidget(),
367 wxMOTIF_STR("MenuBar"), NULL, 0);
368 m_mainWidget = (WXWidget) menuBarW;
369
370 size_t menuCount = GetMenuCount();
371 for (size_t i = 0; i < menuCount; i++)
372 {
373 wxMenu *menu = GetMenu(i);
374 wxString title(m_titles[i]);
375 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, i, title, true));
376
377 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
378 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
379
380 // tear off menu support
381#if (XmVersion >= 1002)
382 if ( menu->IsTearOff() )
383 {
384 XtVaSetValues(GetWidget(menu),
385 XmNtearOffModel, XmTEAR_OFF_ENABLED,
386 NULL);
387 Widget tearOff = XmGetTearOffControl(GetWidget(menu));
388 wxDoChangeForegroundColour((Widget) tearOff, m_foregroundColour);
389 wxDoChangeBackgroundColour((Widget) tearOff, m_backgroundColour, true);
390#endif
391 }
392 }
393
394 SetBackgroundColour(m_backgroundColour);
395 SetForegroundColour(m_foregroundColour);
396 SetFont(m_font);
397
398 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
399 XtRealizeWidget ((Widget) menuBarW);
400 XtManageChild ((Widget) menuBarW);
401 SetMenuBarFrame(parent);
402
403 return true;
404}
405
406// Destroy menubar, but keep data structures intact so we can recreate it.
407bool wxMenuBar::DestroyMenuBar()
408{
409 if (!m_mainWidget)
410 {
411 SetMenuBarFrame((wxFrame*) NULL);
412 return false;
413 }
414
415 XtUnmanageChild ((Widget) m_mainWidget);
416 XtUnrealizeWidget ((Widget) m_mainWidget);
417
418 size_t menuCount = GetMenuCount();
419 for (size_t i = 0; i < menuCount; i++)
420 {
421 wxMenu *menu = GetMenu(i);
422 menu->DestroyMenu(true);
423
424 }
425 XtDestroyWidget((Widget) m_mainWidget);
426 m_mainWidget = (WXWidget) 0;
427
428 SetMenuBarFrame((wxFrame*) NULL);
429
430 return true;
431}
432
433// Since PopupMenu under Motif stills grab right mouse button events
434// after it was closed, we need to delete the associated widgets to
435// allow next PopUpMenu to appear...
436void wxMenu::DestroyWidgetAndDetach()
437{
438 if (GetMainWidget())
439 {
440 wxMenu *menuParent = GetParent();
441 if ( menuParent )
442 {
443 wxMenuItemList::compatibility_iterator node = menuParent->GetMenuItems().GetFirst();
444 while ( node )
445 {
446 if ( node->GetData()->GetSubMenu() == this )
447 {
448 delete node->GetData();
449 menuParent->GetMenuItems().Erase(node);
450
451 break;
452 }
453
454 node = node->GetNext();
455 }
456 }
457
458 DestroyMenu(true);
459 }
460
461 // Mark as no longer popped up
462 m_menuId = -1;
463}
464
465/*
466* Create a popup or pulldown menu.
467* Submenus of a popup will be pulldown.
468*
469*/
470
471WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar,
472 WXWidget parent,
473 wxMenu * topMenu,
474 size_t WXUNUSED(index),
475 const wxString& title,
476 bool pullDown)
477{
478 Widget menu = (Widget) 0;
479 Widget buttonWidget = (Widget) 0;
480 Arg args[5];
481 XtSetArg (args[0], XmNnumColumns, m_numColumns);
482 XtSetArg (args[1], XmNpacking, (m_numColumns > 1) ? XmPACK_COLUMN : XmPACK_TIGHT);
483
484 if (!pullDown)
485 {
486 menu = XmCreatePopupMenu ((Widget) parent, wxMOTIF_STR("popup"), args, 2);
487#if 0
488 XtAddCallback(menu,
489 XmNunmapCallback,
490 (XtCallbackProc)wxMenuPopdownCallback,
491 (XtPointer)this);
492#endif
493 }
494 else
495 {
496 char mnem = wxFindMnemonic (title);
497 menu = XmCreatePulldownMenu ((Widget) parent, wxMOTIF_STR("pulldown"), args, 2);
498
499 wxString title2(wxStripMenuCodes(title));
500 wxXmString label_str(title2);
501 buttonWidget = XtVaCreateManagedWidget(title2,
502#if wxUSE_GADGETS
503 xmCascadeButtonGadgetClass, (Widget) parent,
504#else
505 xmCascadeButtonWidgetClass, (Widget) parent,
506#endif
507 XmNlabelString, label_str(),
508 XmNsubMenuId, menu,
509 NULL);
510
511 if (mnem != 0)
512 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
513 }
514
515 m_menuWidget = (WXWidget) menu;
516
517 m_topLevelMenu = topMenu;
518
519 size_t i = 0;
520 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
521 node;
522 node = node->GetNext(), ++i )
523 {
524 wxMenuItem *item = node->GetData();
525
526 item->CreateItem(menu, menuBar, topMenu, i);
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#else
683 wxUnusedVar(keepOriginalSize);
684#endif
685}
686
687void wxMenu::SetFont(const wxFont& font)
688{
689 m_font = font;
690 ChangeFont();
691}
692
693bool wxMenuBar::SetBackgroundColour(const wxColour& col)
694{
695 m_backgroundColour = col;
696 if (m_mainWidget)
697 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
698
699 size_t menuCount = GetMenuCount();
700 for (size_t i = 0; i < menuCount; i++)
701 m_menus.Item(i)->GetData()->SetBackgroundColour((wxColour&) col);
702
703 return true;
704}
705
706bool wxMenuBar::SetForegroundColour(const wxColour& col)
707{
708 m_foregroundColour = col;
709 if (m_mainWidget)
710 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
711
712 size_t menuCount = GetMenuCount();
713 for (size_t i = 0; i < menuCount; i++)
714 m_menus.Item(i)->GetData()->SetForegroundColour((wxColour&) col);
715
716 return true;
717}
718
719void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
720{
721 // Nothing to do for menubar, fonts are kept in wxMenus
722}
723
724bool wxMenuBar::SetFont(const wxFont& font)
725{
726 m_font = font;
727 ChangeFont();
728
729 size_t menuCount = GetMenuCount();
730 for (size_t i = 0; i < menuCount; i++)
731 m_menus.Item(i)->GetData()->SetFont(font);
732
733 return true;
734}