]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
e40298d5 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
eb22f2a6 GD |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "menu.h" | |
14 | #pragma implementation "menuitem.h" | |
15 | #endif | |
16 | ||
e9576ca5 SC |
17 | // ============================================================================ |
18 | // headers & declarations | |
19 | // ============================================================================ | |
20 | ||
21 | // wxWindows headers | |
22 | // ----------------- | |
23 | ||
03e11df5 | 24 | #include "wx/app.h" |
e9576ca5 SC |
25 | #include "wx/menu.h" |
26 | #include "wx/menuitem.h" | |
03e11df5 | 27 | #include "wx/window.h" |
e9576ca5 SC |
28 | #include "wx/log.h" |
29 | #include "wx/utils.h" | |
2b5f62a0 | 30 | #include "wx/frame.h" |
e9576ca5 | 31 | |
519cb848 SC |
32 | #include "wx/mac/uma.h" |
33 | ||
e9576ca5 SC |
34 | // other standard headers |
35 | // ---------------------- | |
36 | #include <string.h> | |
37 | ||
2f1ae414 | 38 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
39 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
40 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
2f1ae414 | 41 | #endif |
e9576ca5 | 42 | |
519cb848 SC |
43 | // the (popup) menu title has this special id |
44 | static const int idMenuTitle = -2; | |
b03e4fcd | 45 | static MenuItemIndex firstUserHelpMenuItem = 0 ; |
519cb848 SC |
46 | |
47 | const short kwxMacMenuBarResource = 1 ; | |
48 | const short kwxMacAppleMenuId = 1 ; | |
49 | ||
e9576ca5 SC |
50 | // ============================================================================ |
51 | // implementation | |
52 | // ============================================================================ | |
7c15086c SC |
53 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu ) ; |
54 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ); | |
519cb848 | 55 | |
e9576ca5 SC |
56 | // Menus |
57 | ||
58 | // Construct a menu with optional title (then use append) | |
519cb848 | 59 | |
f5c6eb5c | 60 | #ifdef __DARWIN__ |
82ca6dbc GD |
61 | short wxMenu::s_macNextMenuId = 3 ; |
62 | #else | |
519cb848 | 63 | short wxMenu::s_macNextMenuId = 2 ; |
82ca6dbc | 64 | #endif |
519cb848 | 65 | |
e7549107 | 66 | void wxMenu::Init() |
e9576ca5 | 67 | { |
e7549107 | 68 | m_doBreak = FALSE; |
7c15086c | 69 | m_startRadioGroup = -1; |
e7549107 SC |
70 | |
71 | // create the menu | |
58751a86 | 72 | m_macMenuId = s_macNextMenuId++; |
a9412f8f | 73 | m_hMenu = UMANewMenu(m_macMenuId, m_title, wxFont::GetDefaultEncoding() ); |
e7549107 SC |
74 | |
75 | if ( !m_hMenu ) | |
76 | { | |
22026088 | 77 | wxLogLastError(wxT("UMANewMenu failed")); |
e7549107 SC |
78 | } |
79 | ||
80 | // if we have a title, insert it in the beginning of the menu | |
81 | if ( !!m_title ) | |
e9576ca5 | 82 | { |
519cb848 | 83 | Append(idMenuTitle, m_title) ; |
e9576ca5 SC |
84 | AppendSeparator() ; |
85 | } | |
e7549107 | 86 | } |
e9576ca5 | 87 | |
e7549107 SC |
88 | wxMenu::~wxMenu() |
89 | { | |
e40298d5 JS |
90 | if (MAC_WXHMENU(m_hMenu)) |
91 | ::DisposeMenu(MAC_WXHMENU(m_hMenu)); | |
e9576ca5 SC |
92 | } |
93 | ||
e7549107 | 94 | void wxMenu::Break() |
e9576ca5 | 95 | { |
e40298d5 | 96 | // not available on the mac platform |
e7549107 | 97 | } |
e9576ca5 | 98 | |
7c15086c | 99 | void wxMenu::Attach(wxMenuBarBase *menubar) |
e7549107 | 100 | { |
7c15086c | 101 | wxMenuBase::Attach(menubar); |
e7549107 | 102 | |
7c15086c | 103 | EndRadioGroup(); |
e9576ca5 SC |
104 | } |
105 | ||
e9576ca5 | 106 | // function appends a new item or submenu to the menu |
e7549107 SC |
107 | // append a new item or submenu to the menu |
108 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
e9576ca5 | 109 | { |
427ff662 | 110 | wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") ); |
e9576ca5 | 111 | |
e40298d5 JS |
112 | if ( pItem->IsSeparator() ) |
113 | { | |
114 | if ( pos == (size_t)-1 ) | |
115 | MacAppendMenu(MAC_WXHMENU(m_hMenu), "\p-"); | |
116 | else | |
117 | MacInsertMenuItem(MAC_WXHMENU(m_hMenu), "\p-" , pos); | |
118 | } | |
58751a86 | 119 | else |
e40298d5 JS |
120 | { |
121 | wxMenu *pSubMenu = pItem->GetSubMenu() ; | |
122 | if ( pSubMenu != NULL ) | |
123 | { | |
427ff662 | 124 | wxASSERT_MSG( pSubMenu->m_hMenu != NULL , wxT("invalid submenu added")); |
e40298d5 | 125 | pSubMenu->m_menuParent = this ; |
58751a86 | 126 | |
4224f059 | 127 | if (wxMenuBar::MacGetInstalledMenuBar() == GetMenuBar()) |
d46342af SC |
128 | { |
129 | pSubMenu->MacBeforeDisplay( true ) ; | |
130 | } | |
58751a86 | 131 | |
e40298d5 | 132 | if ( pos == (size_t)-1 ) |
a9412f8f | 133 | UMAAppendSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pSubMenu->m_macMenuId); |
e40298d5 | 134 | else |
a9412f8f | 135 | UMAInsertSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pos, pSubMenu->m_macMenuId); |
e40298d5 JS |
136 | pItem->UpdateItemBitmap() ; |
137 | pItem->UpdateItemStatus() ; | |
138 | } | |
139 | else | |
140 | { | |
141 | if ( pos == (size_t)-1 ) | |
142 | { | |
a9412f8f | 143 | UMAAppendMenuItem(MAC_WXHMENU(m_hMenu), wxT("a") , wxFont::GetDefaultEncoding() ); |
e40298d5 JS |
144 | pos = CountMenuItems(MAC_WXHMENU(m_hMenu)) ; |
145 | } | |
146 | else | |
147 | { | |
4ab107d7 SC |
148 | // MacOS counts menu items from 1 and inserts after, therefore having the |
149 | // same effect as wx 0 based and inserting before, we must correct pos | |
150 | // after however for updates to be correct | |
a9412f8f | 151 | UMAInsertMenuItem(MAC_WXHMENU(m_hMenu), wxT("a"), wxFont::GetDefaultEncoding(), pos); |
4ab107d7 | 152 | pos += 1 ; |
e40298d5 JS |
153 | } |
154 | ||
4ab107d7 | 155 | SetMenuItemCommandID( MAC_WXHMENU(m_hMenu) , pos , pItem->GetId() ) ; |
e40298d5 JS |
156 | pItem->UpdateItemText() ; |
157 | pItem->UpdateItemBitmap() ; | |
158 | pItem->UpdateItemStatus() ; | |
159 | ||
58751a86 | 160 | if ( pItem->GetId() == idMenuTitle ) |
e40298d5 JS |
161 | { |
162 | UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , pos , false ) ; | |
163 | } | |
164 | } | |
165 | } | |
e7549107 SC |
166 | // if we're already attached to the menubar, we must update it |
167 | if ( IsAttached() ) | |
168 | { | |
4224f059 | 169 | GetMenuBar()->Refresh(); |
e7549107 | 170 | } |
e40298d5 | 171 | return TRUE ; |
e9576ca5 SC |
172 | } |
173 | ||
7c15086c SC |
174 | void wxMenu::EndRadioGroup() |
175 | { | |
176 | // we're not inside a radio group any longer | |
177 | m_startRadioGroup = -1; | |
178 | } | |
179 | ||
9add9367 | 180 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) |
e9576ca5 | 181 | { |
9add9367 | 182 | wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") ); |
7c15086c SC |
183 | |
184 | bool check = FALSE; | |
185 | ||
186 | if ( item->GetKind() == wxITEM_RADIO ) | |
187 | { | |
188 | int count = GetMenuItemCount(); | |
189 | ||
190 | if ( m_startRadioGroup == -1 ) | |
191 | { | |
192 | // start a new radio group | |
193 | m_startRadioGroup = count; | |
194 | ||
195 | // for now it has just one element | |
196 | item->SetAsRadioGroupStart(); | |
197 | item->SetRadioGroupEnd(m_startRadioGroup); | |
198 | ||
199 | // ensure that we have a checked item in the radio group | |
200 | check = TRUE; | |
201 | } | |
202 | else // extend the current radio group | |
203 | { | |
204 | // we need to update its end item | |
205 | item->SetRadioGroupStart(m_startRadioGroup); | |
206 | wxMenuItemList::Node *node = GetMenuItems().Item(m_startRadioGroup); | |
207 | ||
208 | if ( node ) | |
209 | { | |
210 | node->GetData()->SetRadioGroupEnd(count); | |
211 | } | |
212 | else | |
213 | { | |
214 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
215 | } | |
216 | } | |
217 | } | |
218 | else // not a radio item | |
219 | { | |
220 | EndRadioGroup(); | |
221 | } | |
222 | ||
223 | if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) | |
224 | { | |
9add9367 | 225 | return NULL; |
7c15086c SC |
226 | } |
227 | ||
228 | if ( check ) | |
229 | { | |
230 | // check the item initially | |
231 | item->Check(TRUE); | |
232 | } | |
233 | ||
9add9367 | 234 | return item; |
e9576ca5 SC |
235 | } |
236 | ||
9add9367 | 237 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
e9576ca5 | 238 | { |
9add9367 RD |
239 | if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos)) |
240 | return item; | |
241 | else | |
242 | return NULL; | |
e9576ca5 SC |
243 | } |
244 | ||
e7549107 | 245 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
e9576ca5 | 246 | { |
e7549107 SC |
247 | // we need to find the items position in the child list |
248 | size_t pos; | |
249 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
250 | for ( pos = 0; node; pos++ ) | |
251 | { | |
252 | if ( node->GetData() == item ) | |
253 | break; | |
e9576ca5 | 254 | |
e7549107 | 255 | node = node->GetNext(); |
e9576ca5 SC |
256 | } |
257 | ||
e7549107 SC |
258 | // DoRemove() (unlike Remove) can only be called for existing item! |
259 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
519cb848 | 260 | |
e40298d5 | 261 | ::DeleteMenuItem(MAC_WXHMENU(m_hMenu) , pos + 1); |
e9576ca5 | 262 | |
e7549107 SC |
263 | if ( IsAttached() ) |
264 | { | |
7c15086c | 265 | // otherwise, the change won't be visible |
4224f059 | 266 | GetMenuBar()->Refresh(); |
e7549107 | 267 | } |
e9576ca5 | 268 | |
e7549107 SC |
269 | // and from internal data structures |
270 | return wxMenuBase::DoRemove(item); | |
e9576ca5 SC |
271 | } |
272 | ||
e9576ca5 SC |
273 | void wxMenu::SetTitle(const wxString& label) |
274 | { | |
e40298d5 | 275 | m_title = label ; |
a9412f8f | 276 | UMASetMenuTitle(MAC_WXHMENU(m_hMenu) , label , wxFont::GetDefaultEncoding() ) ; |
e9576ca5 | 277 | } |
e7549107 | 278 | bool wxMenu::ProcessCommand(wxCommandEvent & event) |
e9576ca5 SC |
279 | { |
280 | bool processed = FALSE; | |
281 | ||
e9576ca5 SC |
282 | // Try the menu's event handler |
283 | if ( !processed && GetEventHandler()) | |
284 | { | |
e7549107 | 285 | processed = GetEventHandler()->ProcessEvent(event); |
e9576ca5 | 286 | } |
519cb848 | 287 | |
e7549107 SC |
288 | // Try the window the menu was popped up from (and up through the |
289 | // hierarchy) | |
290 | wxWindow *win = GetInvokingWindow(); | |
291 | if ( !processed && win ) | |
292 | processed = win->GetEventHandler()->ProcessEvent(event); | |
293 | ||
294 | return processed; | |
295 | } | |
296 | ||
297 | ||
298 | // --------------------------------------------------------------------------- | |
299 | // other | |
300 | // --------------------------------------------------------------------------- | |
301 | ||
e7549107 SC |
302 | wxWindow *wxMenu::GetWindow() const |
303 | { | |
304 | if ( m_invokingWindow != NULL ) | |
305 | return m_invokingWindow; | |
4224f059 DE |
306 | else if ( GetMenuBar() != NULL) |
307 | return (wxWindow *) GetMenuBar()->GetFrame(); | |
e7549107 SC |
308 | |
309 | return NULL; | |
310 | } | |
519cb848 | 311 | |
58751a86 | 312 | // helper functions returning the mac menu position for a certain item, note that this is |
519cb848 SC |
313 | // mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0 |
314 | ||
58751a86 | 315 | int wxMenu::MacGetIndexFromId( int id ) |
519cb848 | 316 | { |
e7549107 SC |
317 | size_t pos; |
318 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
319 | for ( pos = 0; node; pos++ ) | |
519cb848 | 320 | { |
e7549107 SC |
321 | if ( node->GetData()->GetId() == id ) |
322 | break; | |
519cb848 | 323 | |
e7549107 SC |
324 | node = node->GetNext(); |
325 | } | |
58751a86 | 326 | |
519cb848 | 327 | if (!node) |
e40298d5 | 328 | return 0; |
58751a86 | 329 | |
e40298d5 | 330 | return pos + 1 ; |
519cb848 SC |
331 | } |
332 | ||
58751a86 | 333 | int wxMenu::MacGetIndexFromItem( wxMenuItem *pItem ) |
519cb848 | 334 | { |
e7549107 SC |
335 | size_t pos; |
336 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
337 | for ( pos = 0; node; pos++ ) | |
519cb848 | 338 | { |
e7549107 SC |
339 | if ( node->GetData() == pItem ) |
340 | break; | |
341 | ||
342 | node = node->GetNext(); | |
519cb848 SC |
343 | } |
344 | ||
345 | if (!node) | |
e40298d5 | 346 | return 0; |
58751a86 | 347 | |
e40298d5 | 348 | return pos + 1 ; |
519cb848 SC |
349 | } |
350 | ||
58751a86 | 351 | void wxMenu::MacEnableMenu( bool bDoEnable ) |
519cb848 | 352 | { |
e40298d5 | 353 | UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , 0 , bDoEnable ) ; |
58751a86 | 354 | |
e40298d5 | 355 | ::DrawMenuBar() ; |
519cb848 SC |
356 | } |
357 | ||
d46342af SC |
358 | // MacOS needs to know about submenus somewhere within this menu |
359 | // before it can be displayed , also hide special menu items like preferences | |
360 | // that are handled by the OS | |
58751a86 | 361 | void wxMenu::MacBeforeDisplay( bool isSubMenu ) |
d46342af SC |
362 | { |
363 | wxMenuItem* previousItem = NULL ; | |
5be55d56 | 364 | size_t pos ; |
d46342af SC |
365 | wxMenuItemList::Node *node; |
366 | wxMenuItem *item; | |
58751a86 | 367 | for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) |
d46342af SC |
368 | { |
369 | item = (wxMenuItem *)node->GetData(); | |
370 | wxMenu* subMenu = item->GetSubMenu() ; | |
58751a86 | 371 | if (subMenu) |
d46342af SC |
372 | { |
373 | subMenu->MacBeforeDisplay( true ) ; | |
374 | } | |
375 | else | |
376 | { | |
377 | #if TARGET_CARBON | |
378 | if ( UMAGetSystemVersion() >= 0x1000 ) | |
379 | { | |
380 | if ( item->GetId() == wxApp::s_macPreferencesMenuItemId || item->GetId() == wxApp::s_macExitMenuItemId) | |
381 | { | |
382 | ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos + 1, kMenuItemAttrHidden, 0 ); | |
5be55d56 VZ |
383 | if ( GetMenuItems().GetCount() == pos + 1 && |
384 | previousItem != NULL && | |
385 | previousItem->IsSeparator() ) | |
d46342af SC |
386 | { |
387 | ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos , kMenuItemAttrHidden, 0 ); | |
388 | } | |
389 | } | |
390 | } | |
58751a86 | 391 | #endif |
d46342af SC |
392 | } |
393 | previousItem = item ; | |
394 | } | |
395 | ||
396 | if ( isSubMenu ) | |
397 | ::InsertMenu(MAC_WXHMENU( GetHMenu()), -1); | |
398 | ||
399 | } | |
400 | // undo all changes from the MacBeforeDisplay call | |
58751a86 | 401 | void wxMenu::MacAfterDisplay( bool isSubMenu ) |
d46342af SC |
402 | { |
403 | if ( isSubMenu ) | |
404 | ::DeleteMenu(MacGetMenuId()); | |
405 | ||
406 | wxMenuItem* previousItem = NULL ; | |
407 | int pos ; | |
408 | wxMenuItemList::Node *node; | |
409 | wxMenuItem *item; | |
58751a86 | 410 | for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) |
d46342af SC |
411 | { |
412 | item = (wxMenuItem *)node->GetData(); | |
413 | wxMenu* subMenu = item->GetSubMenu() ; | |
58751a86 | 414 | if (subMenu) |
d46342af SC |
415 | { |
416 | subMenu->MacAfterDisplay( true ) ; | |
417 | } | |
418 | else | |
419 | { | |
420 | // no need to undo hidings | |
421 | } | |
422 | previousItem = item ; | |
423 | } | |
424 | } | |
425 | ||
e9576ca5 | 426 | // Menu Bar |
519cb848 | 427 | |
58751a86 | 428 | /* |
519cb848 SC |
429 | |
430 | Mac Implementation note : | |
431 | ||
432 | The Mac has only one global menubar, so we attempt to install the currently | |
433 | active menubar from a frame, we currently don't take into account mdi-frames | |
434 | which would ask for menu-merging | |
435 | ||
58751a86 | 436 | Secondly there is no mac api for changing a menubar that is not the current |
519cb848 SC |
437 | menubar, so we have to wait for preparing the actual menubar until the |
438 | wxMenubar is to be used | |
439 | ||
58751a86 | 440 | We can in subsequent versions use MacInstallMenuBar to provide some sort of |
519cb848 SC |
441 | auto-merge for MDI in case this will be necessary |
442 | ||
443 | */ | |
444 | ||
445 | wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ; | |
1b1d2207 | 446 | wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ; |
519cb848 | 447 | |
e7549107 | 448 | void wxMenuBar::Init() |
e9576ca5 SC |
449 | { |
450 | m_eventHandler = this; | |
e9576ca5 | 451 | m_menuBarFrame = NULL; |
7c15086c | 452 | m_invokingWindow = (wxWindow*) NULL; |
e9576ca5 SC |
453 | } |
454 | ||
51abe921 SC |
455 | wxMenuBar::wxMenuBar() |
456 | { | |
457 | Init(); | |
458 | } | |
459 | ||
460 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
461 | { | |
462 | Init(); | |
463 | } | |
464 | ||
e7549107 SC |
465 | |
466 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
e9576ca5 | 467 | { |
e7549107 SC |
468 | Init(); |
469 | ||
470 | m_titles.Alloc(count); | |
471 | ||
472 | for ( int i = 0; i < count; i++ ) | |
473 | { | |
474 | m_menus.Append(menus[i]); | |
475 | m_titles.Add(titles[i]); | |
476 | ||
477 | menus[i]->Attach(this); | |
478 | } | |
e9576ca5 SC |
479 | } |
480 | ||
481 | wxMenuBar::~wxMenuBar() | |
482 | { | |
1b1d2207 DE |
483 | if (s_macCommonMenuBar == this) |
484 | s_macCommonMenuBar = NULL; | |
e40298d5 JS |
485 | if (s_macInstalledMenuBar == this) |
486 | { | |
487 | ::ClearMenuBar(); | |
488 | s_macInstalledMenuBar = NULL; | |
489 | } | |
519cb848 | 490 | |
e7549107 SC |
491 | } |
492 | ||
6d6da89c | 493 | void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect)) |
e7549107 SC |
494 | { |
495 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
e9576ca5 | 496 | |
e7549107 | 497 | DrawMenuBar(); |
e9576ca5 SC |
498 | } |
499 | ||
58751a86 | 500 | void wxMenuBar::MacInstallMenuBar() |
519cb848 | 501 | { |
2b8a6962 GD |
502 | if ( s_macInstalledMenuBar == this ) |
503 | return ; | |
58751a86 | 504 | |
d9e054bc | 505 | wxStAppResource resload ; |
58751a86 | 506 | |
2b8a6962 GD |
507 | Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ; |
508 | wxString message ; | |
427ff662 | 509 | wxCHECK_RET( menubar != NULL, wxT("can't read MBAR resource") ); |
2b8a6962 | 510 | ::SetMenuBar( menubar ) ; |
a3590de1 SC |
511 | #if TARGET_API_MAC_CARBON |
512 | ::DisposeMenuBar( menubar ) ; | |
513 | #else | |
2b8a6962 | 514 | ::DisposeHandle( menubar ) ; |
a3590de1 | 515 | #endif |
519cb848 | 516 | |
a3590de1 | 517 | #if TARGET_API_MAC_OS8 |
2b8a6962 | 518 | MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ; |
d9e054bc SC |
519 | if ( CountMenuItems( menu ) == 2 ) |
520 | { | |
2b8a6962 GD |
521 | ::AppendResMenu(menu, 'DRVR'); |
522 | } | |
a3590de1 | 523 | #endif |
58751a86 | 524 | |
7c15086c | 525 | // clean-up the help menu before adding new items |
e40298d5 JS |
526 | MenuHandle mh = NULL ; |
527 | if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) == noErr ) | |
528 | { | |
529 | for ( int i = CountMenuItems( mh ) ; i >= firstUserHelpMenuItem ; --i ) | |
530 | { | |
531 | DeleteMenuItem( mh , i ) ; | |
532 | } | |
533 | } | |
534 | else | |
535 | { | |
536 | mh = NULL ; | |
537 | } | |
7c15086c | 538 | #if TARGET_CARBON |
e40298d5 JS |
539 | if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId) |
540 | { | |
541 | wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ; | |
542 | if ( item == NULL || !(item->IsEnabled()) ) | |
543 | DisableMenuCommand( NULL , kHICommandPreferences ) ; | |
544 | else | |
545 | EnableMenuCommand( NULL , kHICommandPreferences ) ; | |
546 | } | |
7c15086c | 547 | #endif |
e40298d5 JS |
548 | for (size_t i = 0; i < m_menus.GetCount(); i++) |
549 | { | |
f823cacb | 550 | wxMenuItemList::Node *node; |
2b5f62a0 VZ |
551 | wxMenuItem *item; |
552 | int pos ; | |
e40298d5 JS |
553 | wxMenu* menu = m_menus[i] , *subMenu = NULL ; |
554 | ||
427ff662 | 555 | if( m_titles[i] == wxT("?") || m_titles[i] == wxT("&?") || m_titles[i] == wxApp::s_macHelpMenuTitleName ) |
e40298d5 JS |
556 | { |
557 | if ( mh == NULL ) | |
558 | { | |
559 | continue ; | |
560 | } | |
58751a86 RD |
561 | |
562 | for (pos = 0 , node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) | |
e40298d5 JS |
563 | { |
564 | item = (wxMenuItem *)node->GetData(); | |
565 | subMenu = item->GetSubMenu() ; | |
58751a86 | 566 | if (subMenu) |
e40298d5 JS |
567 | { |
568 | // we don't support hierarchical menus in the help menu yet | |
569 | } | |
58751a86 | 570 | else |
e40298d5 JS |
571 | { |
572 | if ( item->IsSeparator() ) | |
573 | { | |
574 | if ( mh ) | |
575 | MacAppendMenu(mh, "\p-" ); | |
576 | } | |
577 | else | |
578 | { | |
579 | wxAcceleratorEntry* entry = wxGetAccelFromString( item->GetText() ) ; | |
580 | ||
581 | if ( item->GetId() == wxApp::s_macAboutMenuItemId ) | |
58751a86 | 582 | { |
a9412f8f | 583 | UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetText() , wxFont::GetDefaultEncoding() ); |
e40298d5 JS |
584 | UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true ); |
585 | SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetId() ) ; | |
586 | UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ; | |
587 | } | |
588 | else | |
589 | { | |
590 | if ( mh ) | |
591 | { | |
a9412f8f | 592 | UMAAppendMenuItem(mh, item->GetText() , wxFont::GetDefaultEncoding(), entry); |
e40298d5 JS |
593 | SetMenuItemCommandID( mh , CountMenuItems(mh) , item->GetId() ) ; |
594 | } | |
595 | } | |
58751a86 | 596 | |
e40298d5 JS |
597 | delete entry ; |
598 | } | |
599 | } | |
600 | } | |
601 | } | |
602 | else | |
603 | { | |
a9412f8f | 604 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ; |
d46342af | 605 | m_menus[i]->MacBeforeDisplay(false) ; |
e40298d5 | 606 | ::InsertMenu(MAC_WXHMENU(m_menus[i]->GetHMenu()), 0); |
e40298d5 JS |
607 | } |
608 | } | |
609 | ::DrawMenuBar() ; | |
610 | s_macInstalledMenuBar = this; | |
519cb848 SC |
611 | } |
612 | ||
e7549107 | 613 | void wxMenuBar::EnableTop(size_t pos, bool enable) |
e9576ca5 | 614 | { |
e7549107 | 615 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); |
e40298d5 | 616 | m_menus[pos]->MacEnableMenu( enable ) ; |
e7549107 | 617 | Refresh(); |
e9576ca5 SC |
618 | } |
619 | ||
c393c740 JS |
620 | bool wxMenuBar::Enable( bool enable) |
621 | { | |
5059f192 | 622 | wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") ); |
c393c740 JS |
623 | size_t i; |
624 | for (i = 0; i < GetMenuCount(); i++) | |
625 | { | |
626 | EnableTop(i, enable); | |
627 | } | |
628 | return true; | |
629 | } | |
630 | ||
e7549107 | 631 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) |
e9576ca5 | 632 | { |
e7549107 | 633 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
e9576ca5 | 634 | |
e7549107 | 635 | m_titles[pos] = label; |
e9576ca5 | 636 | |
e7549107 SC |
637 | if ( !IsAttached() ) |
638 | { | |
e9576ca5 | 639 | return; |
e7549107 | 640 | } |
e9576ca5 | 641 | |
519cb848 | 642 | m_menus[pos]->SetTitle( label ) ; |
e40298d5 JS |
643 | if (wxMenuBar::s_macInstalledMenuBar == this) // are we currently installed ? |
644 | { | |
645 | ::SetMenuBar( GetMenuBar() ) ; | |
646 | ::InvalMenuBar() ; | |
647 | } | |
e9576ca5 SC |
648 | } |
649 | ||
e7549107 | 650 | wxString wxMenuBar::GetLabelTop(size_t pos) const |
e9576ca5 | 651 | { |
e7549107 SC |
652 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, |
653 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
519cb848 | 654 | |
e7549107 | 655 | return m_titles[pos]; |
e9576ca5 SC |
656 | } |
657 | ||
e7549107 | 658 | int wxMenuBar::FindMenu(const wxString& title) |
e9576ca5 | 659 | { |
e7549107 | 660 | wxString menuTitle = wxStripMenuCodes(title); |
e9576ca5 | 661 | |
e7549107 SC |
662 | size_t count = GetMenuCount(); |
663 | for ( size_t i = 0; i < count; i++ ) | |
e9576ca5 | 664 | { |
e7549107 SC |
665 | wxString title = wxStripMenuCodes(m_titles[i]); |
666 | if ( menuTitle == title ) | |
58751a86 | 667 | return i; |
e9576ca5 | 668 | } |
e9576ca5 | 669 | |
e7549107 | 670 | return wxNOT_FOUND; |
e9576ca5 | 671 | |
e9576ca5 SC |
672 | } |
673 | ||
e7549107 SC |
674 | |
675 | // --------------------------------------------------------------------------- | |
676 | // wxMenuBar construction | |
677 | // --------------------------------------------------------------------------- | |
678 | ||
679 | // --------------------------------------------------------------------------- | |
680 | // wxMenuBar construction | |
681 | // --------------------------------------------------------------------------- | |
682 | ||
683 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
e9576ca5 | 684 | { |
e7549107 SC |
685 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); |
686 | if ( !menuOld ) | |
687 | return FALSE; | |
688 | m_titles[pos] = title; | |
e9576ca5 | 689 | |
e7549107 | 690 | if ( IsAttached() ) |
e9576ca5 | 691 | { |
e40298d5 JS |
692 | if (s_macInstalledMenuBar == this) |
693 | { | |
d46342af | 694 | menuOld->MacAfterDisplay( false ) ; |
e40298d5 JS |
695 | ::DeleteMenu( menuOld->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ; |
696 | { | |
d46342af | 697 | menu->MacBeforeDisplay( false ) ; |
a9412f8f | 698 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
e40298d5 JS |
699 | if ( pos == m_menus.GetCount() - 1) |
700 | { | |
701 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
702 | } | |
703 | else | |
704 | { | |
705 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ; | |
706 | } | |
707 | } | |
708 | } | |
e9576ca5 | 709 | |
e7549107 | 710 | Refresh(); |
e9576ca5 | 711 | } |
e9576ca5 | 712 | |
e7549107 | 713 | return menuOld; |
e9576ca5 SC |
714 | } |
715 | ||
e7549107 | 716 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
e9576ca5 | 717 | { |
e7549107 SC |
718 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
719 | return FALSE; | |
e9576ca5 | 720 | |
e7549107 | 721 | m_titles.Insert(title, pos); |
e9576ca5 | 722 | |
a9412f8f | 723 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
e7549107 | 724 | |
d46342af | 725 | if ( IsAttached() && s_macInstalledMenuBar == this ) |
e9576ca5 | 726 | { |
d46342af | 727 | if (s_macInstalledMenuBar == this) |
e40298d5 | 728 | { |
d46342af SC |
729 | menu->MacBeforeDisplay( false ) ; |
730 | if ( pos == (size_t) -1 || pos + 1 == m_menus.GetCount() ) | |
731 | { | |
732 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
733 | } | |
734 | else | |
735 | { | |
736 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ; | |
737 | } | |
e40298d5 | 738 | } |
e7549107 | 739 | Refresh(); |
e9576ca5 | 740 | } |
e7549107 SC |
741 | |
742 | return TRUE; | |
e9576ca5 SC |
743 | } |
744 | ||
51abe921 SC |
745 | wxMenu *wxMenuBar::Remove(size_t pos) |
746 | { | |
747 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
748 | if ( !menu ) | |
749 | return NULL; | |
750 | ||
751 | if ( IsAttached() ) | |
752 | { | |
e40298d5 JS |
753 | if (s_macInstalledMenuBar == this) |
754 | { | |
755 | ::DeleteMenu( menu->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ; | |
756 | } | |
51abe921 | 757 | |
51abe921 SC |
758 | Refresh(); |
759 | } | |
760 | ||
5fe38474 | 761 | m_titles.RemoveAt(pos); |
51abe921 SC |
762 | |
763 | return menu; | |
764 | } | |
765 | ||
766 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
767 | { | |
768 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
769 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
770 | ||
771 | if ( !wxMenuBarBase::Append(menu, title) ) | |
772 | return FALSE; | |
773 | ||
51abe921 | 774 | m_titles.Add(title); |
58751a86 | 775 | |
a9412f8f | 776 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
51abe921 SC |
777 | |
778 | if ( IsAttached() ) | |
779 | { | |
e40298d5 JS |
780 | if (s_macInstalledMenuBar == this) |
781 | { | |
782 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
783 | } | |
51abe921 SC |
784 | |
785 | Refresh(); | |
786 | } | |
787 | ||
7c15086c SC |
788 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables |
789 | // adding menu later on. | |
790 | if (m_invokingWindow) | |
791 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
792 | ||
51abe921 SC |
793 | return TRUE; |
794 | } | |
795 | ||
2b5f62a0 VZ |
796 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu ) |
797 | { | |
798 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
799 | ||
800 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); | |
801 | while (node) | |
802 | { | |
803 | wxMenuItem *menuitem = node->GetData(); | |
804 | if (menuitem->IsSubMenu()) | |
805 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu() ); | |
806 | node = node->GetNext(); | |
807 | } | |
808 | } | |
809 | ||
810 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
811 | { | |
812 | menu->SetInvokingWindow( win ); | |
813 | ||
814 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); | |
815 | while (node) | |
816 | { | |
817 | wxMenuItem *menuitem = node->GetData(); | |
818 | if (menuitem->IsSubMenu()) | |
819 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu() , win ); | |
820 | node = node->GetNext(); | |
821 | } | |
822 | } | |
823 | ||
824 | void wxMenuBar::UnsetInvokingWindow() | |
825 | { | |
7c15086c | 826 | m_invokingWindow = (wxWindow*) NULL; |
2b5f62a0 VZ |
827 | wxMenuList::Node *node = m_menus.GetFirst(); |
828 | while (node) | |
829 | { | |
830 | wxMenu *menu = node->GetData(); | |
831 | wxMenubarUnsetInvokingWindow( menu ); | |
832 | node = node->GetNext(); | |
833 | } | |
834 | } | |
835 | ||
836 | void wxMenuBar::SetInvokingWindow(wxFrame *frame) | |
837 | { | |
7c15086c | 838 | m_invokingWindow = frame; |
2b5f62a0 VZ |
839 | wxMenuList::Node *node = m_menus.GetFirst(); |
840 | while (node) | |
841 | { | |
842 | wxMenu *menu = node->GetData(); | |
843 | wxMenubarSetInvokingWindow( menu, frame ); | |
844 | node = node->GetNext(); | |
845 | } | |
846 | } | |
847 | ||
90b959ae | 848 | void wxMenuBar::Detach() |
2f1ae414 | 849 | { |
90b959ae SC |
850 | wxMenuBarBase::Detach() ; |
851 | } | |
2f1ae414 | 852 | |
90b959ae SC |
853 | void wxMenuBar::Attach(wxFrame *frame) |
854 | { | |
855 | wxMenuBarBase::Attach( frame ) ; | |
2f1ae414 | 856 | } |
51abe921 SC |
857 | // --------------------------------------------------------------------------- |
858 | // wxMenuBar searching for menu items | |
859 | // --------------------------------------------------------------------------- | |
860 | ||
861 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
862 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
863 | const wxString& itemString) const | |
864 | { | |
865 | wxString menuLabel = wxStripMenuCodes(menuString); | |
866 | size_t count = GetMenuCount(); | |
867 | for ( size_t i = 0; i < count; i++ ) | |
868 | { | |
869 | wxString title = wxStripMenuCodes(m_titles[i]); | |
ef089805 | 870 | if ( menuLabel == title ) |
51abe921 SC |
871 | return m_menus[i]->FindItem(itemString); |
872 | } | |
873 | ||
874 | return wxNOT_FOUND; | |
875 | } | |
876 | ||
877 | wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const | |
878 | { | |
879 | if ( itemMenu ) | |
880 | *itemMenu = NULL; | |
881 | ||
882 | wxMenuItem *item = NULL; | |
883 | size_t count = GetMenuCount(); | |
884 | for ( size_t i = 0; !item && (i < count); i++ ) | |
885 | { | |
886 | item = m_menus[i]->FindItem(id, itemMenu); | |
887 | } | |
888 | ||
889 | return item; | |
890 | } | |
891 | ||
892 |