]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/menu.cpp
fixed compilation in Unicode build
[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#ifdef __GNUG__
18 #pragma implementation "menu.h"
19#endif
20
21// ----------------------------------------------------------------------------
22// headers
23// ----------------------------------------------------------------------------
24
25#include "wx/defs.h"
26
27#include "wx/menu.h"
28#include "wx/menuitem.h"
29#include "wx/log.h"
30#include "wx/utils.h"
31#include "wx/app.h"
32#include "wx/frame.h"
33#include "wx/settings.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
58IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
59IMPLEMENT_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)
70void 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 )
82 {
83 Append(wxID_SEPARATOR, 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.
93wxMenu::~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
111void wxMenu::Break()
112{
113 m_numColumns++;
114}
115
116// function appends a new item or submenu to the menu
117bool wxMenu::DoAppend(wxMenuItem *pItem)
118{
119 if (m_menuWidget)
120 {
121 // this is a dynamic Append
122 pItem->CreateItem(m_menuWidget, m_menuBar, m_topLevelMenu);
123 }
124
125 if ( pItem->IsSubMenu() )
126 {
127 pItem->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
128 }
129
130 return wxMenuBase::DoAppend(pItem);
131}
132
133wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
134{
135 item->DestroyItem(TRUE);
136
137 return wxMenuBase::DoRemove(item);
138}
139
140bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
141{
142 if ( !wxMenuBase::DoInsert(pos, item) )
143 return FALSE;
144
145 wxFAIL_MSG(wxT("not implemented"));
146
147 return FALSE;
148}
149
150void wxMenu::SetTitle(const wxString& label)
151{
152 m_title = label;
153
154 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
155 if ( !node )
156 return;
157
158 wxMenuItem *item = node->GetData ();
159 Widget widget = (Widget) item->GetButtonWidget();
160 if ( !widget )
161 return;
162
163 wxXmString title_str(label);
164 XtVaSetValues(widget,
165 XmNlabelString, title_str(),
166 NULL);
167}
168
169bool wxMenu::ProcessCommand(wxCommandEvent & event)
170{
171 bool processed = FALSE;
172
173#if wxUSE_MENU_CALLBACK
174 // Try a callback
175 if (m_callback)
176 {
177 (void) (*(m_callback)) (*this, event);
178 processed = TRUE;
179 }
180#endif // wxUSE_MENU_CALLBACK
181
182 // Try the menu's event handler
183 if ( !processed && GetEventHandler())
184 {
185 processed = GetEventHandler()->ProcessEvent(event);
186 }
187 // Try the window the menu was popped up from (and up
188 // through the hierarchy)
189 if ( !processed && GetInvokingWindow())
190 processed = GetInvokingWindow()->ProcessEvent(event);
191
192 return processed;
193}
194
195// ----------------------------------------------------------------------------
196// Menu Bar
197// ----------------------------------------------------------------------------
198
199void wxMenuBar::Init()
200{
201 m_eventHandler = this;
202 m_menuBarFrame = NULL;
203 m_mainWidget = (WXWidget) NULL;
204 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENU);
205 m_foregroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
206 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
207}
208
209wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
210{
211 Init();
212
213 for ( int 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 wxString str;
251
252 wxMenu *menu = GetMenu(pos);
253 if ( menu )
254 {
255 Widget w = (Widget)menu->GetButtonWidget();
256 if (w)
257 {
258 XmString text;
259 XtVaGetValues(w,
260 XmNlabelString, &text,
261 NULL);
262
263 char *s;
264 if ( XmStringGetLtoR(text, XmSTRING_DEFAULT_CHARSET, &s) )
265 {
266 str = s;
267
268 XtFree(s);
269 }
270 }
271 }
272
273 return str;
274}
275
276bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
277{
278 wxCHECK_MSG( menu, FALSE, wxT("invalid menu") );
279 wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE,
280 wxT("menu already appended") );
281
282 if ( m_menuBarFrame )
283 {
284 WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE);
285 wxCHECK_MSG( w, FALSE, wxT("failed to create menu") );
286 menu->SetButtonWidget(w);
287 }
288
289 //menu->SetMenuBar(this);
290
291 m_titles.Add(title);
292
293 return wxMenuBarBase::Append(menu, title);
294}
295
296bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
297{
298 if ( !wxMenuBarBase::Insert(pos, menu, title) )
299 return FALSE;
300
301 wxFAIL_MSG(wxT("TODO"));
302
303 return FALSE;
304}
305
306wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
307{
308 if ( !wxMenuBarBase::Replace(pos, menu, title) )
309 return FALSE;
310
311 wxFAIL_MSG(wxT("TODO"));
312
313 return NULL;
314}
315
316wxMenu *wxMenuBar::Remove(size_t pos)
317{
318 wxMenu *menu = wxMenuBarBase::Remove(pos);
319 if ( !menu )
320 return NULL;
321
322 if ( m_menuBarFrame )
323 menu->DestroyMenu(TRUE);
324
325 menu->SetMenuBar(NULL);
326
327 m_titles.Remove(pos);
328
329 return menu;
330}
331
332// Find the menu menuString, item itemString, and return the item id.
333// Returns -1 if none found.
334int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
335{
336 char buf1[200];
337 char buf2[200];
338 wxStripMenuCodes ((char *)(const char *)menuString, buf1);
339
340 size_t menuCount = GetMenuCount();
341 for (size_t i = 0; i < menuCount; i++)
342 {
343 wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
344 if (strcmp (buf1, buf2) == 0)
345 return m_menus[i]->FindItem (itemString);
346 }
347 return -1;
348}
349
350wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const
351{
352 if (itemMenu)
353 *itemMenu = NULL;
354
355 wxMenuItem *item = NULL;
356 size_t menuCount = GetMenuCount();
357 for (size_t i = 0; i < menuCount; i++)
358 if ((item = m_menus[i]->FindItem(id, itemMenu)))
359 return item;
360 return NULL;
361}
362
363// Create menubar
364bool wxMenuBar::CreateMenuBar(wxFrame* parent)
365{
366 if (m_mainWidget)
367 {
368 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
369 /*
370 if (!XtIsManaged((Widget) m_mainWidget))
371 XtManageChild((Widget) m_mainWidget);
372 */
373 XtMapWidget((Widget) m_mainWidget);
374 return TRUE;
375 }
376
377 Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWidget(), "MenuBar", NULL, 0);
378 m_mainWidget = (WXWidget) menuBarW;
379
380 size_t menuCount = GetMenuCount();
381 for (size_t i = 0; i < menuCount; i++)
382 {
383 wxMenu *menu = GetMenu(i);
384 wxString title(m_titles[i]);
385 menu->SetButtonWidget(menu->CreateMenu (this, menuBarW, menu, title, TRUE));
386
387 if (strcmp (wxStripMenuCodes(title), "Help") == 0)
388 XtVaSetValues ((Widget) menuBarW, XmNmenuHelpWidget, (Widget) menu->GetButtonWidget(), NULL);
389
390 // tear off menu support
391#if (XmVersion >= 1002)
392 if ( menu->IsTearOff() )
393 {
394 XtVaSetValues(GetWidget(menu),
395 XmNtearOffModel, XmTEAR_OFF_ENABLED,
396 NULL);
397 Widget tearOff = XmGetTearOffControl(GetWidget(menu));
398 wxDoChangeForegroundColour((Widget) tearOff, m_foregroundColour);
399 wxDoChangeBackgroundColour((Widget) tearOff, m_backgroundColour, TRUE);
400#endif
401 }
402 }
403
404 SetBackgroundColour(m_backgroundColour);
405 SetForegroundColour(m_foregroundColour);
406 SetFont(m_font);
407
408 XtVaSetValues((Widget) parent->GetMainWidget(), XmNmenuBar, (Widget) m_mainWidget, NULL);
409 XtRealizeWidget ((Widget) menuBarW);
410 XtManageChild ((Widget) menuBarW);
411 SetMenuBarFrame(parent);
412
413 return TRUE;
414}
415
416// Destroy menubar, but keep data structures intact so we can recreate it.
417bool wxMenuBar::DestroyMenuBar()
418{
419 if (!m_mainWidget)
420 {
421 SetMenuBarFrame((wxFrame*) NULL);
422 return FALSE;
423 }
424
425 XtUnmanageChild ((Widget) m_mainWidget);
426 XtUnrealizeWidget ((Widget) m_mainWidget);
427
428 size_t menuCount = GetMenuCount();
429 for (size_t i = 0; i < menuCount; i++)
430 {
431 wxMenu *menu = GetMenu(i);
432 menu->DestroyMenu(TRUE);
433
434 }
435 XtDestroyWidget((Widget) m_mainWidget);
436 m_mainWidget = (WXWidget) 0;
437
438 SetMenuBarFrame((wxFrame*) NULL);
439
440 return TRUE;
441}
442
443//// Motif-specific
444static XtWorkProcId WorkProcMenuId;
445
446/* Since PopupMenu under Motif stills grab right mouse button events
447* after it was closed, we need to delete the associated widgets to
448* allow next PopUpMenu to appear...
449*/
450
451int PostDeletionOfMenu( XtPointer* clientData )
452{
453 XtRemoveWorkProc(WorkProcMenuId);
454 wxMenu *menu = (wxMenu *)clientData;
455
456 if (menu->GetMainWidget())
457 {
458 wxMenu *menuParent = menu->GetParent();
459 if ( menuParent )
460 {
461 wxMenuItemList::Node *node = menuParent->GetMenuItems().GetFirst();
462 while ( node )
463 {
464 if ( node->GetData()->GetSubMenu() == menu )
465 {
466 menuParent->GetMenuItems().DeleteNode(node);
467
468 break;
469 }
470
471 node = node->GetNext();
472 }
473 }
474
475 menu->DestroyMenu(TRUE);
476 }
477
478 // Mark as no longer popped up
479 menu->m_menuId = -1;
480
481 return TRUE;
482}
483
484void
485wxMenuPopdownCallback(Widget WXUNUSED(w), XtPointer clientData,
486 XtPointer WXUNUSED(ptr))
487{
488 wxMenu *menu = (wxMenu *)clientData;
489
490 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
491 /* Since Callbacks of MenuItems are not yet processed, we put a
492 * background job which will be done when system will be idle.
493 * What awful hack!! :(
494 */
495
496 WorkProcMenuId = XtAppAddWorkProc(
497 (XtAppContext) wxTheApp->GetAppContext(),
498 (XtWorkProc) PostDeletionOfMenu,
499 (XtPointer) menu );
500 // Apparently not found in Motif headers
501 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
502}
503
504/*
505* Create a popup or pulldown menu.
506* Submenus of a popup will be pulldown.
507*
508*/
509
510WXWidget wxMenu::CreateMenu (wxMenuBar * menuBar, WXWidget parent, wxMenu * topMenu, const wxString& title, bool pullDown)
511{
512 Widget menu = (Widget) 0;
513 Widget buttonWidget = (Widget) 0;
514 Arg args[5];
515 XtSetArg (args[0], XmNnumColumns, m_numColumns);
516 XtSetArg (args[1], XmNpacking, XmPACK_COLUMN);
517
518 if (!pullDown)
519 {
520 menu = XmCreatePopupMenu ((Widget) parent, "popup", args, 2);
521 XtAddCallback(menu,
522 XmNunmapCallback,
523 (XtCallbackProc)wxMenuPopdownCallback,
524 (XtPointer)this);
525 }
526 else
527 {
528 char mnem = wxFindMnemonic (title);
529 menu = XmCreatePulldownMenu ((Widget) parent, "pulldown", args, 2);
530
531 wxString title2(wxStripMenuCodes(title));
532 wxXmString label_str(title2);
533 buttonWidget = XtVaCreateManagedWidget(title2,
534#if wxUSE_GADGETS
535 xmCascadeButtonGadgetClass, (Widget) parent,
536#else
537 xmCascadeButtonWidgetClass, (Widget) parent,
538#endif
539 XmNlabelString, label_str(),
540 XmNsubMenuId, menu,
541 NULL);
542
543 if (mnem != 0)
544 XtVaSetValues (buttonWidget, XmNmnemonic, mnem, NULL);
545 }
546
547 m_menuWidget = (WXWidget) menu;
548
549 m_menuBar = menuBar;
550 m_topLevelMenu = topMenu;
551
552 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
553 node;
554 node = node->GetNext() )
555 {
556 wxMenuItem *item = node->GetData();
557
558 item->CreateItem(menu, menuBar, topMenu);
559 }
560
561 SetBackgroundColour(m_backgroundColour);
562 SetForegroundColour(m_foregroundColour);
563 SetFont(m_font);
564
565 return buttonWidget;
566}
567
568// Destroys the Motif implementation of the menu,
569// but maintains the wxWindows data structures so we can
570// do a CreateMenu again.
571void wxMenu::DestroyMenu (bool full)
572{
573 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
574 node;
575 node = node->GetNext() )
576 {
577 wxMenuItem *item = node->GetData();
578 item->SetMenuBar((wxMenuBar*) NULL);
579
580 item->DestroyItem(full);
581 }
582
583 if (m_buttonWidget)
584 {
585 if (full)
586 {
587 XtVaSetValues((Widget) m_buttonWidget, XmNsubMenuId, NULL, NULL);
588 XtDestroyWidget ((Widget) m_buttonWidget);
589 m_buttonWidget = (WXWidget) 0;
590 }
591 }
592 if (m_menuWidget && full)
593 {
594 XtDestroyWidget((Widget) m_menuWidget);
595 m_menuWidget = (WXWidget) NULL;
596 }
597}
598
599WXWidget wxMenu::FindMenuItem (int id, wxMenuItem ** it) const
600{
601 if (id == m_menuId)
602 {
603 if (it)
604 *it = (wxMenuItem*) NULL;
605 return m_buttonWidget;
606 }
607
608 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
609 node;
610 node = node->GetNext() )
611 {
612 wxMenuItem *item = node->GetData ();
613 if (item->GetId() == id)
614 {
615 if (it)
616 *it = item;
617 return item->GetButtonWidget();
618 }
619
620 if (item->GetSubMenu())
621 {
622 WXWidget w = item->GetSubMenu()->FindMenuItem (id, it);
623 if (w)
624 {
625 return w;
626 }
627 }
628 }
629
630 if (it)
631 *it = (wxMenuItem*) NULL;
632 return (WXWidget) NULL;
633}
634
635void wxMenu::SetBackgroundColour(const wxColour& col)
636{
637 m_backgroundColour = col;
638 if (m_menuWidget)
639 wxDoChangeBackgroundColour(m_menuWidget, (wxColour&) col);
640 if (m_buttonWidget)
641 wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
642
643 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
644 node;
645 node = node->GetNext() )
646 {
647 wxMenuItem* item = node->GetData();
648 if (item->GetButtonWidget())
649 {
650 // This crashes because it uses gadgets
651 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
652 }
653 if (item->GetSubMenu())
654 item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
655 }
656}
657
658void wxMenu::SetForegroundColour(const wxColour& col)
659{
660 m_foregroundColour = col;
661 if (m_menuWidget)
662 wxDoChangeForegroundColour(m_menuWidget, (wxColour&) col);
663 if (m_buttonWidget)
664 wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
665
666 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
667 node;
668 node = node->GetNext() )
669 {
670 wxMenuItem* item = node->GetData();
671 if (item->GetButtonWidget())
672 {
673 // This crashes because it uses gadgets
674 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
675 }
676 if (item->GetSubMenu())
677 item->GetSubMenu()->SetForegroundColour((wxColour&) col);
678 }
679}
680
681void wxMenu::ChangeFont(bool keepOriginalSize)
682{
683 // lesstif 0.87 hangs when setting XmNfontList
684#ifndef LESSTIF_VERSION
685 if (!m_font.Ok() || !m_menuWidget)
686 return;
687
688 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_menuWidget));
689
690 XtVaSetValues ((Widget) m_menuWidget,
691 XmNfontList, fontList,
692 NULL);
693 if (m_buttonWidget)
694 {
695 XtVaSetValues ((Widget) m_buttonWidget,
696 XmNfontList, fontList,
697 NULL);
698 }
699
700 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
701 node;
702 node = node->GetNext() )
703 {
704 wxMenuItem* item = node->GetData();
705 if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
706 {
707 XtVaSetValues ((Widget) item->GetButtonWidget(),
708 XmNfontList, fontList,
709 NULL);
710 }
711 if (item->GetSubMenu())
712 item->GetSubMenu()->ChangeFont(keepOriginalSize);
713 }
714#endif
715}
716
717void wxMenu::SetFont(const wxFont& font)
718{
719 m_font = font;
720 ChangeFont();
721}
722
723bool wxMenuBar::SetBackgroundColour(const wxColour& col)
724{
725 m_backgroundColour = col;
726 if (m_mainWidget)
727 wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col);
728
729 size_t menuCount = GetMenuCount();
730 for (size_t i = 0; i < menuCount; i++)
731 m_menus[i]->SetBackgroundColour((wxColour&) col);
732
733 return TRUE;
734}
735
736bool wxMenuBar::SetForegroundColour(const wxColour& col)
737{
738 m_foregroundColour = col;
739 if (m_mainWidget)
740 wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col);
741
742 size_t menuCount = GetMenuCount();
743 for (size_t i = 0; i < menuCount; i++)
744 m_menus[i]->SetForegroundColour((wxColour&) col);
745
746 return TRUE;
747}
748
749void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
750{
751 // Nothing to do for menubar, fonts are kept in wxMenus
752}
753
754bool wxMenuBar::SetFont(const wxFont& font)
755{
756 m_font = font;
757 ChangeFont();
758
759 size_t menuCount = GetMenuCount();
760 for (size_t i = 0; i < menuCount; i++)
761 m_menus[i]->SetFont(font);
762
763 return TRUE;
764}
765