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