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