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