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