]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
75f11ad7 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
75f11ad7 | 6 | // Created: 10/10/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
75f11ad7 DW |
8 | // Copyright: (c) David Webster |
9 | // Licence: wxWindows licence | |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
75f11ad7 DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
0e320a79 | 14 | |
75f11ad7 DW |
15 | #ifndef WX_PRECOMP |
16 | #include "wx/frame.h" | |
17 | #include "wx/menu.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/intl.h" | |
c5fb56c0 | 20 | #include "wx/log.h" |
75f11ad7 | 21 | #endif |
0e320a79 | 22 | |
75f11ad7 DW |
23 | #if wxUSE_OWNER_DRAWN |
24 | #include "wx/ownerdrw.h" | |
0e320a79 DW |
25 | #endif |
26 | ||
75f11ad7 | 27 | #include "wx/os2/private.h" |
0e320a79 DW |
28 | |
29 | // other standard headers | |
0e320a79 DW |
30 | #include <string.h> |
31 | ||
75f11ad7 DW |
32 | // ---------------------------------------------------------------------------- |
33 | // global variables | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | extern wxMenu *wxCurrentPopupMenu; | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // constants | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // the (popup) menu title has this special id | |
43 | static const int idMenuTitle = -2; | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // macros | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
75f11ad7 DW |
49 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
50 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
0e320a79 DW |
51 | |
52 | // ============================================================================ | |
53 | // implementation | |
54 | // ============================================================================ | |
55 | ||
75f11ad7 DW |
56 | // --------------------------------------------------------------------------- |
57 | // wxMenu construction, adding and removing menu items | |
58 | // --------------------------------------------------------------------------- | |
0e320a79 DW |
59 | |
60 | // Construct a menu with optional title (then use append) | |
c5fb56c0 | 61 | void wxMenu::Init() |
0e320a79 | 62 | { |
c5fb56c0 | 63 | m_doBreak = FALSE; |
75f11ad7 | 64 | |
c5fb56c0 DW |
65 | // create the menu |
66 | m_hMenu = (WXHMENU)0; // CreatePopupMenu(); | |
67 | if ( !m_hMenu ) | |
0e320a79 | 68 | { |
c5fb56c0 | 69 | wxLogLastError("CreatePopupMenu"); |
0e320a79 DW |
70 | } |
71 | ||
c5fb56c0 DW |
72 | // if we have a title, insert it in the beginning of the menu |
73 | if ( !!m_title ) | |
74 | { | |
75 | Append(idMenuTitle, m_title); | |
76 | AppendSeparator(); | |
77 | } | |
0e320a79 DW |
78 | } |
79 | ||
80 | // The wxWindow destructor will take care of deleting the submenus. | |
81 | wxMenu::~wxMenu() | |
82 | { | |
c5fb56c0 DW |
83 | // we should free Windows resources only if Windows doesn't do it for us |
84 | // which happens if we're attached to a menubar or a submenu of another | |
85 | // menu | |
86 | if ( !IsAttached() && !GetParent() ) | |
75f11ad7 | 87 | { |
c5fb56c0 DW |
88 | /* |
89 | if ( !::DestroyMenu(GetHmenu()) ) | |
90 | { | |
91 | wxLogLastError("DestroyMenu"); | |
92 | } | |
93 | */ | |
75f11ad7 | 94 | } |
0e320a79 | 95 | |
c5fb56c0 DW |
96 | #if wxUSE_ACCEL |
97 | // delete accels | |
98 | WX_CLEAR_ARRAY(m_accels); | |
99 | #endif // wxUSE_ACCEL | |
0e320a79 DW |
100 | } |
101 | ||
102 | void wxMenu::Break() | |
103 | { | |
c5fb56c0 | 104 | // this will take effect during the next call to Append() |
75f11ad7 | 105 | m_doBreak = TRUE; |
0e320a79 DW |
106 | } |
107 | ||
c5fb56c0 DW |
108 | #if wxUSE_ACCEL |
109 | ||
110 | int wxMenu::FindAccel(int id) const | |
0e320a79 | 111 | { |
c5fb56c0 DW |
112 | size_t n, count = m_accels.GetCount(); |
113 | for ( n = 0; n < count; n++ ) | |
114 | { | |
115 | if ( m_accels[n]->m_command == id ) | |
116 | return n; | |
117 | } | |
75f11ad7 | 118 | |
c5fb56c0 DW |
119 | return wxNOT_FOUND; |
120 | } | |
75f11ad7 | 121 | |
c5fb56c0 DW |
122 | void wxMenu::UpdateAccel(wxMenuItem *item) |
123 | { | |
124 | // find the (new) accel for this item | |
125 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
126 | if ( accel ) | |
127 | accel->m_command = item->GetId(); | |
75f11ad7 | 128 | |
c5fb56c0 DW |
129 | // find the old one |
130 | int n = FindAccel(item->GetId()); | |
131 | if ( n == wxNOT_FOUND ) | |
132 | { | |
133 | // no old, add new if any | |
134 | if ( accel ) | |
135 | m_accels.Add(accel); | |
136 | else | |
137 | return; // skipping RebuildAccelTable() below | |
138 | } | |
139 | else | |
140 | { | |
141 | // replace old with new or just remove the old one if no new | |
142 | delete m_accels[n]; | |
143 | if ( accel ) | |
144 | m_accels[n] = accel; | |
145 | else | |
146 | m_accels.Remove(n); | |
75f11ad7 | 147 | } |
c5fb56c0 DW |
148 | |
149 | if ( IsAttached() ) | |
150 | { | |
151 | m_menuBar->RebuildAccelTable(); | |
152 | } | |
153 | } | |
154 | ||
75f11ad7 | 155 | #endif // wxUSE_ACCEL |
0e320a79 | 156 | |
c5fb56c0 DW |
157 | // append a new item or submenu to the menu |
158 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
159 | { | |
160 | #if wxUSE_ACCEL | |
161 | UpdateAccel(pItem); | |
162 | #endif // wxUSE_ACCEL | |
0e320a79 | 163 | |
c5fb56c0 DW |
164 | UINT flags = 0; |
165 | // TODO: | |
75f11ad7 DW |
166 | /* |
167 | // if "Break" has just been called, insert a menu break before this item | |
168 | // (and don't forget to reset the flag) | |
169 | if ( m_doBreak ) { | |
170 | flags |= MF_MENUBREAK; | |
171 | m_doBreak = FALSE; | |
172 | } | |
0e320a79 | 173 | |
75f11ad7 DW |
174 | if ( pItem->IsSeparator() ) { |
175 | flags |= MF_SEPARATOR; | |
176 | } | |
177 | ||
178 | // id is the numeric id for normal menu items and HMENU for submenus as | |
179 | // required by ::AppendMenu() API | |
180 | UINT id; | |
181 | wxMenu *submenu = pItem->GetSubMenu(); | |
182 | if ( submenu != NULL ) { | |
c5fb56c0 DW |
183 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); |
184 | ||
185 | submenu->SetParent(this); | |
75f11ad7 DW |
186 | |
187 | id = (UINT)submenu->GetHMenu(); | |
75f11ad7 DW |
188 | |
189 | flags |= MF_POPUP; | |
190 | } | |
191 | else { | |
192 | id = pItem->GetId(); | |
193 | } | |
194 | ||
c5fb56c0 | 195 | LPCTSTR pData; |
75f11ad7 DW |
196 | |
197 | #if wxUSE_OWNER_DRAWN | |
198 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? | |
199 | // item draws itself, pass pointer to it in data parameter | |
200 | flags |= MF_OWNERDRAW; | |
c5fb56c0 | 201 | pData = (LPCTSTR)pItem; |
75f11ad7 DW |
202 | } |
203 | else | |
204 | #endif | |
205 | { | |
206 | // menu is just a normal string (passed in data parameter) | |
207 | flags |= MF_STRING; | |
c5fb56c0 DW |
208 | |
209 | pData = (char*)pItem->GetText().c_str(); | |
75f11ad7 | 210 | } |
c5fb56c0 DW |
211 | |
212 | BOOL ok; | |
213 | if ( pos == (size_t)-1 ) | |
75f11ad7 | 214 | { |
c5fb56c0 | 215 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); |
75f11ad7 DW |
216 | } |
217 | else | |
218 | { | |
c5fb56c0 DW |
219 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); |
220 | } | |
221 | ||
222 | if ( !ok ) | |
223 | { | |
224 | wxLogLastError("Insert or AppendMenu"); | |
225 | ||
226 | return FALSE; | |
227 | } | |
228 | else | |
229 | { | |
230 | // if we just appended the title, highlight it | |
231 | #ifdef __WIN32__ | |
232 | if ( (int)id == idMenuTitle ) | |
75f11ad7 DW |
233 | { |
234 | // visually select the menu title | |
235 | MENUITEMINFO mii; | |
236 | mii.cbSize = sizeof(mii); | |
237 | mii.fMask = MIIM_STATE; | |
238 | mii.fState = MFS_DEFAULT; | |
239 | ||
240 | if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) | |
241 | { | |
242 | wxLogLastError(wxT("SetMenuItemInfo")); | |
243 | } | |
244 | } | |
c5fb56c0 DW |
245 | #endif // __WIN32__ |
246 | ||
247 | // if we're already attached to the menubar, we must update it | |
248 | if ( IsAttached() ) | |
249 | { | |
250 | m_menuBar->Refresh(); | |
251 | } | |
252 | ||
253 | return TRUE; | |
75f11ad7 DW |
254 | } |
255 | */ | |
c5fb56c0 | 256 | return FALSE; |
0e320a79 DW |
257 | } |
258 | ||
c5fb56c0 | 259 | bool wxMenu::DoAppend(wxMenuItem *item) |
0e320a79 | 260 | { |
c5fb56c0 | 261 | return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item); |
0e320a79 DW |
262 | } |
263 | ||
c5fb56c0 | 264 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
0e320a79 | 265 | { |
c5fb56c0 | 266 | return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos); |
0e320a79 DW |
267 | } |
268 | ||
c5fb56c0 | 269 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
0e320a79 | 270 | { |
c5fb56c0 DW |
271 | // we need to find the items position in the child list |
272 | size_t pos; | |
273 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
274 | for ( pos = 0; node; pos++ ) | |
75f11ad7 | 275 | { |
c5fb56c0 | 276 | if ( node->GetData() == item ) |
75f11ad7 | 277 | break; |
c5fb56c0 DW |
278 | |
279 | node = node->GetNext(); | |
0e320a79 DW |
280 | } |
281 | ||
c5fb56c0 DW |
282 | // DoRemove() (unlike Remove) can only be called for existing item! |
283 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
75f11ad7 | 284 | |
c5fb56c0 DW |
285 | #if wxUSE_ACCEL |
286 | // remove the corresponding accel from the accel table | |
287 | int n = FindAccel(item->GetId()); | |
288 | if ( n != wxNOT_FOUND ) | |
289 | { | |
290 | delete m_accels[n]; | |
75f11ad7 | 291 | |
c5fb56c0 DW |
292 | m_accels.Remove(n); |
293 | } | |
294 | //else: this item doesn't have an accel, nothing to do | |
295 | #endif // wxUSE_ACCEL | |
296 | /* | |
297 | // remove the item from the menu | |
298 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
75f11ad7 | 299 | { |
c5fb56c0 DW |
300 | wxLogLastError("RemoveMenu"); |
301 | } | |
302 | */ | |
303 | if ( IsAttached() ) | |
304 | { | |
305 | // otherwise, the chane won't be visible | |
306 | m_menuBar->Refresh(); | |
75f11ad7 | 307 | } |
0e320a79 | 308 | |
c5fb56c0 DW |
309 | // and from internal data structures |
310 | return wxMenuBase::DoRemove(item); | |
75f11ad7 | 311 | } |
0e320a79 | 312 | |
75f11ad7 DW |
313 | // --------------------------------------------------------------------------- |
314 | // accelerator helpers | |
315 | // --------------------------------------------------------------------------- | |
316 | ||
c5fb56c0 DW |
317 | #if wxUSE_ACCEL |
318 | ||
75f11ad7 DW |
319 | // create the wxAcceleratorEntries for our accels and put them into provided |
320 | // array - return the number of accels we have | |
321 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
322 | { | |
323 | size_t count = GetAccelCount(); | |
324 | for ( size_t n = 0; n < count; n++ ) | |
325 | { | |
c5fb56c0 | 326 | *accels++ = *m_accels[n]; |
75f11ad7 DW |
327 | } |
328 | ||
329 | return count; | |
0e320a79 DW |
330 | } |
331 | ||
75f11ad7 DW |
332 | #endif // wxUSE_ACCEL |
333 | ||
334 | // --------------------------------------------------------------------------- | |
c5fb56c0 | 335 | // set wxMenu title |
75f11ad7 DW |
336 | // --------------------------------------------------------------------------- |
337 | ||
338 | void wxMenu::SetTitle(const wxString& label) | |
0e320a79 | 339 | { |
75f11ad7 DW |
340 | bool hasNoTitle = m_title.IsEmpty(); |
341 | m_title = label; | |
0e320a79 | 342 | |
75f11ad7 | 343 | HMENU hMenu = GetHmenu(); |
c5fb56c0 | 344 | |
75f11ad7 | 345 | if ( hasNoTitle ) |
0e320a79 | 346 | { |
75f11ad7 DW |
347 | if ( !label.IsEmpty() ) |
348 | { | |
c5fb56c0 DW |
349 | /* |
350 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
351 | (unsigned)idMenuTitle, m_title) || | |
352 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
75f11ad7 | 353 | { |
c5fb56c0 | 354 | wxLogLastError("InsertMenu"); |
75f11ad7 | 355 | } |
c5fb56c0 | 356 | */ |
75f11ad7 | 357 | } |
0e320a79 | 358 | } |
75f11ad7 | 359 | else |
0e320a79 | 360 | { |
75f11ad7 | 361 | if ( label.IsEmpty() ) |
0e320a79 | 362 | { |
c5fb56c0 | 363 | /* |
75f11ad7 DW |
364 | // remove the title and the separator after it |
365 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
366 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
367 | { | |
368 | wxLogLastError("RemoveMenu"); | |
369 | } | |
c5fb56c0 | 370 | */ |
0e320a79 | 371 | } |
75f11ad7 | 372 | else |
0e320a79 | 373 | { |
c5fb56c0 | 374 | /* |
75f11ad7 DW |
375 | // modify the title |
376 | if ( !ModifyMenu(hMenu, 0u, | |
c5fb56c0 DW |
377 | MF_BYPOSITION | MF_STRING, |
378 | (unsigned)idMenuTitle, m_title) ) | |
75f11ad7 DW |
379 | { |
380 | wxLogLastError("ModifyMenu"); | |
381 | } | |
c5fb56c0 | 382 | */ |
0e320a79 DW |
383 | } |
384 | } | |
c5fb56c0 DW |
385 | /* |
386 | #ifdef __WIN32__ | |
75f11ad7 DW |
387 | // put the title string in bold face |
388 | if ( !m_title.IsEmpty() ) | |
389 | { | |
390 | MENUITEMINFO mii; | |
391 | mii.cbSize = sizeof(mii); | |
392 | mii.fMask = MIIM_STATE; | |
393 | mii.fState = MFS_DEFAULT; | |
394 | ||
395 | if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) | |
396 | { | |
397 | wxLogLastError("SetMenuItemInfo"); | |
398 | } | |
399 | } | |
c5fb56c0 | 400 | #endif // Win32 |
75f11ad7 | 401 | */ |
0e320a79 DW |
402 | } |
403 | ||
75f11ad7 DW |
404 | // --------------------------------------------------------------------------- |
405 | // event processing | |
406 | // --------------------------------------------------------------------------- | |
407 | ||
408 | bool wxMenu::OS2Command(WXUINT WXUNUSED(param), WXWORD id) | |
0e320a79 | 409 | { |
75f11ad7 DW |
410 | // ignore commands from the menu title |
411 | ||
c5fb56c0 | 412 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! |
75f11ad7 DW |
413 | if ( id != (WXWORD)idMenuTitle ) |
414 | { | |
415 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); | |
416 | event.SetEventObject( this ); | |
417 | event.SetId( id ); | |
418 | event.SetInt( id ); | |
419 | ProcessCommand(event); | |
420 | } | |
421 | ||
422 | return TRUE; | |
0e320a79 DW |
423 | } |
424 | ||
75f11ad7 | 425 | bool wxMenu::ProcessCommand(wxCommandEvent & event) |
0e320a79 DW |
426 | { |
427 | bool processed = FALSE; | |
428 | ||
c5fb56c0 | 429 | #if WXWIN_COMPATIBILITY |
0e320a79 DW |
430 | // Try a callback |
431 | if (m_callback) | |
432 | { | |
75f11ad7 DW |
433 | (void)(*(m_callback))(*this, event); |
434 | processed = TRUE; | |
0e320a79 | 435 | } |
c5fb56c0 | 436 | #endif // WXWIN_COMPATIBILITY |
0e320a79 DW |
437 | |
438 | // Try the menu's event handler | |
439 | if ( !processed && GetEventHandler()) | |
440 | { | |
75f11ad7 | 441 | processed = GetEventHandler()->ProcessEvent(event); |
0e320a79 | 442 | } |
75f11ad7 DW |
443 | |
444 | // Try the window the menu was popped up from (and up through the | |
445 | // hierarchy) | |
446 | wxWindow *win = GetInvokingWindow(); | |
447 | if ( !processed && win ) | |
448 | processed = win->GetEventHandler()->ProcessEvent(event); | |
449 | ||
450 | return processed; | |
0e320a79 DW |
451 | } |
452 | ||
75f11ad7 DW |
453 | // --------------------------------------------------------------------------- |
454 | // other | |
455 | // --------------------------------------------------------------------------- | |
456 | ||
457 | void wxMenu::Attach(wxMenuBar *menubar) | |
0e320a79 | 458 | { |
75f11ad7 DW |
459 | // menu can be in at most one menubar because otherwise they would both |
460 | // delete the menu pointer | |
461 | wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") ); | |
0e320a79 | 462 | |
75f11ad7 | 463 | m_menuBar = menubar; |
75f11ad7 DW |
464 | } |
465 | ||
466 | void wxMenu::Detach() | |
467 | { | |
468 | wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") ); | |
469 | ||
c5fb56c0 DW |
470 | m_menuBar = NULL; |
471 | } | |
472 | ||
473 | wxWindow *wxMenu::GetWindow() const | |
474 | { | |
475 | if ( m_invokingWindow != NULL ) | |
476 | return m_invokingWindow; | |
477 | else if ( m_menuBar != NULL) | |
478 | return m_menuBar->GetFrame(); | |
479 | ||
480 | return NULL; | |
0e320a79 DW |
481 | } |
482 | ||
75f11ad7 | 483 | // --------------------------------------------------------------------------- |
0e320a79 | 484 | // Menu Bar |
75f11ad7 DW |
485 | // --------------------------------------------------------------------------- |
486 | ||
487 | void wxMenuBar::Init() | |
0e320a79 DW |
488 | { |
489 | m_eventHandler = this; | |
0e320a79 | 490 | m_menuBarFrame = NULL; |
75f11ad7 DW |
491 | m_hMenu = 0; |
492 | } | |
0e320a79 | 493 | |
75f11ad7 DW |
494 | wxMenuBar::wxMenuBar() |
495 | { | |
496 | Init(); | |
0e320a79 DW |
497 | } |
498 | ||
75f11ad7 | 499 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) |
0e320a79 | 500 | { |
75f11ad7 DW |
501 | Init(); |
502 | } | |
503 | ||
504 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
505 | { | |
506 | Init(); | |
507 | ||
c5fb56c0 | 508 | m_titles.Alloc(count); |
75f11ad7 | 509 | |
c5fb56c0 DW |
510 | for ( int i = 0; i < count; i++ ) |
511 | { | |
512 | m_menus.Append(menus[i]); | |
513 | m_titles.Add(titles[i]); | |
0e320a79 | 514 | |
c5fb56c0 DW |
515 | menus[i]->Attach(this); |
516 | } | |
0e320a79 DW |
517 | } |
518 | ||
519 | wxMenuBar::~wxMenuBar() | |
520 | { | |
75f11ad7 | 521 | } |
0e320a79 | 522 | |
75f11ad7 DW |
523 | // --------------------------------------------------------------------------- |
524 | // wxMenuBar helpers | |
525 | // --------------------------------------------------------------------------- | |
526 | ||
c5fb56c0 DW |
527 | void wxMenuBar::Refresh( |
528 | bool WXUNUSED(bEraseBackground) | |
529 | , const wxRect* WXUNUSED(pRect) | |
530 | ) | |
75f11ad7 | 531 | { |
c5fb56c0 | 532 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); |
75f11ad7 | 533 | |
c5fb56c0 | 534 | // DrawMenuBar(GetHwndOf(m_menuBarFrame)); |
75f11ad7 DW |
535 | } |
536 | ||
537 | WXHMENU wxMenuBar::Create() | |
538 | { | |
539 | if (m_hMenu != 0 ) | |
c5fb56c0 | 540 | return m_hMenu; |
75f11ad7 DW |
541 | |
542 | wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") ); | |
543 | ||
c5fb56c0 DW |
544 | // TODO: |
545 | /* | |
546 | ||
547 | m_hMenu = (WXHMENU)::CreateMenu(); | |
75f11ad7 DW |
548 | |
549 | if ( !m_hMenu ) | |
550 | { | |
551 | wxLogLastError("CreateMenu"); | |
552 | } | |
553 | else | |
554 | { | |
c5fb56c0 DW |
555 | size_t count = GetMenuCount(); |
556 | for ( size_t i = 0; i < count; i++ ) | |
75f11ad7 | 557 | { |
c5fb56c0 DW |
558 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, |
559 | (UINT)m_menus[i]->GetHMenu(), | |
560 | m_titles[i]) ) | |
561 | { | |
562 | wxLogLastError("AppendMenu"); | |
563 | } | |
75f11ad7 DW |
564 | } |
565 | } | |
566 | ||
567 | return m_hMenu; | |
c5fb56c0 DW |
568 | */ |
569 | return (WXHMENU)0; | |
0e320a79 DW |
570 | } |
571 | ||
75f11ad7 | 572 | // --------------------------------------------------------------------------- |
c5fb56c0 | 573 | // wxMenuBar functions to work with the top level submenus |
75f11ad7 DW |
574 | // --------------------------------------------------------------------------- |
575 | ||
c5fb56c0 DW |
576 | // NB: we don't support owner drawn top level items for now, if we do these |
577 | // functions would have to be changed to use wxMenuItem as well | |
0e320a79 | 578 | |
c5fb56c0 | 579 | void wxMenuBar::EnableTop(size_t pos, bool enable) |
0e320a79 | 580 | { |
c5fb56c0 | 581 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); |
0e320a79 | 582 | |
c5fb56c0 | 583 | // int flag = enable ? MF_ENABLED : MF_GRAYED; |
75f11ad7 | 584 | |
c5fb56c0 | 585 | // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); |
75f11ad7 | 586 | |
c5fb56c0 | 587 | Refresh(); |
75f11ad7 DW |
588 | } |
589 | ||
c5fb56c0 | 590 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) |
75f11ad7 | 591 | { |
c5fb56c0 | 592 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
75f11ad7 | 593 | |
c5fb56c0 | 594 | m_titles[pos] = label; |
75f11ad7 | 595 | |
c5fb56c0 DW |
596 | if ( !IsAttached() ) |
597 | { | |
598 | return; | |
599 | } | |
600 | //else: have to modify the existing menu | |
75f11ad7 | 601 | |
c5fb56c0 DW |
602 | // TODO: |
603 | /* | |
75f11ad7 | 604 | UINT id; |
c5fb56c0 | 605 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); |
75f11ad7 DW |
606 | if ( flagsOld == 0xFFFFFFFF ) |
607 | { | |
608 | wxLogLastError(wxT("GetMenuState")); | |
609 | ||
610 | return; | |
611 | } | |
612 | ||
c5fb56c0 | 613 | if ( flagsOld & MF_POPUP ) |
75f11ad7 | 614 | { |
c5fb56c0 DW |
615 | // HIBYTE contains the number of items in the submenu in this case |
616 | flagsOld &= 0xff; | |
617 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos); | |
75f11ad7 DW |
618 | } |
619 | else | |
620 | { | |
c5fb56c0 DW |
621 | id = pos; |
622 | } | |
623 | ||
624 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, | |
625 | id, label) == (int)0xFFFFFFFF ) | |
626 | { | |
627 | wxLogLastError("ModifyMenu"); | |
75f11ad7 DW |
628 | } |
629 | */ | |
c5fb56c0 | 630 | Refresh(); |
0e320a79 DW |
631 | } |
632 | ||
c5fb56c0 | 633 | wxString wxMenuBar::GetLabelTop(size_t pos) const |
0e320a79 | 634 | { |
c5fb56c0 DW |
635 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, |
636 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
75f11ad7 | 637 | |
c5fb56c0 | 638 | return m_titles[pos]; |
0e320a79 DW |
639 | } |
640 | ||
75f11ad7 DW |
641 | int wxMenuBar::FindMenu(const wxString& title) |
642 | { | |
643 | wxString menuTitle = wxStripMenuCodes(title); | |
c5fb56c0 DW |
644 | |
645 | size_t count = GetMenuCount(); | |
646 | for ( size_t i = 0; i < count; i++ ) | |
75f11ad7 DW |
647 | { |
648 | wxString title = wxStripMenuCodes(m_titles[i]); | |
649 | if ( menuTitle == title ) | |
650 | return i; | |
651 | } | |
652 | ||
653 | return wxNOT_FOUND; | |
654 | ||
655 | } | |
656 | ||
c5fb56c0 DW |
657 | // --------------------------------------------------------------------------- |
658 | // wxMenuBar construction | |
659 | // --------------------------------------------------------------------------- | |
75f11ad7 | 660 | |
c5fb56c0 | 661 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) |
75f11ad7 | 662 | { |
c5fb56c0 DW |
663 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); |
664 | if ( !menuOld ) | |
665 | return FALSE; | |
666 | m_titles[pos] = title; | |
667 | // TODO: | |
668 | /* | |
669 | if ( IsAttached() ) | |
75f11ad7 | 670 | { |
c5fb56c0 DW |
671 | // can't use ModifyMenu() because it deletes the submenu it replaces |
672 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
673 | { | |
674 | wxLogLastError("RemoveMenu"); | |
675 | } | |
75f11ad7 | 676 | |
c5fb56c0 DW |
677 | if ( !::InsertMenu(GetHmenu(), (UINT)pos, |
678 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
679 | (UINT)GetHmenuOf(menu), title) ) | |
680 | { | |
681 | wxLogLastError("InsertMenu"); | |
682 | } | |
683 | ||
684 | #if wxUSE_ACCEL | |
685 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
686 | { | |
687 | // need to rebuild accell table | |
688 | RebuildAccelTable(); | |
689 | } | |
690 | #endif // wxUSE_ACCEL | |
75f11ad7 | 691 | |
c5fb56c0 DW |
692 | Refresh(); |
693 | } | |
694 | */ | |
695 | return menuOld; | |
696 | } | |
75f11ad7 | 697 | |
c5fb56c0 | 698 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
75f11ad7 | 699 | { |
c5fb56c0 DW |
700 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
701 | return FALSE; | |
75f11ad7 | 702 | |
c5fb56c0 | 703 | m_titles.Insert(title, pos); |
75f11ad7 | 704 | |
c5fb56c0 DW |
705 | menu->Attach(this); |
706 | // TODO: | |
707 | /* | |
708 | if ( IsAttached() ) | |
75f11ad7 | 709 | { |
c5fb56c0 DW |
710 | if ( !::InsertMenu(GetHmenu(), pos, |
711 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
712 | (UINT)GetHmenuOf(menu), title) ) | |
713 | { | |
714 | wxLogLastError("InsertMenu"); | |
715 | } | |
75f11ad7 | 716 | |
c5fb56c0 DW |
717 | #if wxUSE_ACCEL |
718 | if ( menu->HasAccels() ) | |
719 | { | |
720 | // need to rebuild accell table | |
721 | RebuildAccelTable(); | |
722 | } | |
723 | #endif // wxUSE_ACCEL | |
75f11ad7 | 724 | |
c5fb56c0 | 725 | Refresh(); |
75f11ad7 | 726 | } |
c5fb56c0 DW |
727 | */ |
728 | return TRUE; | |
75f11ad7 DW |
729 | } |
730 | ||
c5fb56c0 | 731 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) |
0e320a79 | 732 | { |
c5fb56c0 DW |
733 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; |
734 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
0e320a79 | 735 | |
c5fb56c0 DW |
736 | if ( !wxMenuBarBase::Append(menu, title) ) |
737 | return FALSE; | |
0e320a79 | 738 | |
c5fb56c0 DW |
739 | menu->Attach(this); |
740 | ||
741 | m_titles.Add(title); | |
742 | // TODO: | |
743 | /* | |
744 | if ( IsAttached() ) | |
0e320a79 | 745 | { |
c5fb56c0 DW |
746 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, |
747 | (UINT)submenu, title) ) | |
748 | { | |
749 | wxLogLastError(wxT("AppendMenu")); | |
750 | } | |
0e320a79 | 751 | |
c5fb56c0 DW |
752 | #if wxUSE_ACCEL |
753 | if ( menu->HasAccels() ) | |
754 | { | |
755 | // need to rebuild accell table | |
756 | RebuildAccelTable(); | |
757 | } | |
758 | #endif // wxUSE_ACCEL | |
0e320a79 | 759 | |
c5fb56c0 DW |
760 | Refresh(); |
761 | } | |
762 | */ | |
763 | return TRUE; | |
0e320a79 DW |
764 | } |
765 | ||
c5fb56c0 | 766 | wxMenu *wxMenuBar::Remove(size_t pos) |
0e320a79 | 767 | { |
c5fb56c0 DW |
768 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
769 | if ( !menu ) | |
770 | return NULL; | |
771 | // TODO: | |
772 | /* | |
773 | if ( IsAttached() ) | |
774 | { | |
775 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
776 | { | |
777 | wxLogLastError("RemoveMenu"); | |
75f11ad7 | 778 | } |
0e320a79 | 779 | |
c5fb56c0 | 780 | menu->Detach(); |
0e320a79 | 781 | |
c5fb56c0 DW |
782 | #if wxUSE_ACCEL |
783 | if ( menu->HasAccels() ) | |
784 | { | |
785 | // need to rebuild accell table | |
786 | RebuildAccelTable(); | |
787 | } | |
788 | #endif // wxUSE_ACCEL | |
0e320a79 | 789 | |
c5fb56c0 | 790 | Refresh(); |
0e320a79 | 791 | } |
75f11ad7 | 792 | |
c5fb56c0 DW |
793 | m_titles.Remove(pos); |
794 | */ | |
795 | return menu; | |
796 | } | |
75f11ad7 DW |
797 | |
798 | #if wxUSE_ACCEL | |
c5fb56c0 DW |
799 | |
800 | void wxMenuBar::RebuildAccelTable() | |
801 | { | |
802 | // merge the accelerators of all menus into one accel table | |
75f11ad7 | 803 | size_t nAccelCount = 0; |
c5fb56c0 DW |
804 | size_t i, count = GetMenuCount(); |
805 | for ( i = 0; i < count; i++ ) | |
75f11ad7 DW |
806 | { |
807 | nAccelCount += m_menus[i]->GetAccelCount(); | |
808 | } | |
809 | ||
810 | if ( nAccelCount ) | |
0e320a79 | 811 | { |
75f11ad7 DW |
812 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; |
813 | ||
814 | nAccelCount = 0; | |
c5fb56c0 | 815 | for ( i = 0; i < count; i++ ) |
75f11ad7 DW |
816 | { |
817 | nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); | |
818 | } | |
819 | ||
820 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
821 | ||
822 | delete [] accelEntries; | |
0e320a79 | 823 | } |
c5fb56c0 DW |
824 | } |
825 | ||
826 | #endif // wxUSE_ACCEL | |
827 | ||
828 | void wxMenuBar::Attach(wxFrame *frame) | |
829 | { | |
830 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
831 | ||
832 | m_menuBarFrame = frame; | |
833 | ||
834 | #if wxUSE_ACCEL | |
835 | RebuildAccelTable(); | |
75f11ad7 | 836 | #endif // wxUSE_ACCEL |
0e320a79 DW |
837 | } |
838 | ||
75f11ad7 | 839 | void wxMenuBar::Detach() |
0e320a79 | 840 | { |
75f11ad7 | 841 | // ::DestroyMenu((HMENU)m_hMenu); |
c5fb56c0 | 842 | m_hMenu = (WXHMENU)NULL; |
75f11ad7 DW |
843 | m_menuBarFrame = NULL; |
844 | } | |
845 | ||
846 | ||
847 | // --------------------------------------------------------------------------- | |
848 | // wxMenuBar searching for menu items | |
849 | // --------------------------------------------------------------------------- | |
850 | ||
851 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
852 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
853 | const wxString& itemString) const | |
854 | { | |
855 | wxString menuLabel = wxStripMenuCodes(menuString); | |
c5fb56c0 DW |
856 | size_t count = GetMenuCount(); |
857 | for ( size_t i = 0; i < count; i++ ) | |
75f11ad7 DW |
858 | { |
859 | wxString title = wxStripMenuCodes(m_titles[i]); | |
860 | if ( menuString == title ) | |
861 | return m_menus[i]->FindItem(itemString); | |
862 | } | |
863 | ||
864 | return wxNOT_FOUND; | |
865 | } | |
866 | ||
c5fb56c0 | 867 | wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const |
75f11ad7 DW |
868 | { |
869 | if ( itemMenu ) | |
0e320a79 DW |
870 | *itemMenu = NULL; |
871 | ||
872 | wxMenuItem *item = NULL; | |
c5fb56c0 DW |
873 | size_t count = GetMenuCount(); |
874 | for ( size_t i = 0; !item && (i < count); i++ ) | |
75f11ad7 | 875 | { |
c5fb56c0 | 876 | item = m_menus[i]->FindItem(id, itemMenu); |
75f11ad7 DW |
877 | } |
878 | ||
879 | return item; | |
880 | } | |
881 |