]>
Commit | Line | Data |
---|---|---|
93cf77c0 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | ||
13 | // ============================================================================ | |
14 | // headers & declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // wxWindows headers | |
18 | // ----------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menu.h" | |
22 | #pragma implementation "menuitem.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/menu.h" | |
26 | #include "wx/menuitem.h" | |
27 | #include "wx/log.h" | |
34138703 | 28 | #include "wx/utils.h" |
93cf77c0 JS |
29 | |
30 | // other standard headers | |
31 | // ---------------------- | |
32 | #include <string.h> | |
33 | ||
34 | #if !USE_SHARED_LIBRARY | |
35 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
36 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
37 | #endif | |
38 | ||
39 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | // Menus | |
44 | ||
45 | // Construct a menu with optional title (then use append) | |
46 | wxMenu::wxMenu(const wxString& title, const wxFunction func) | |
47 | { | |
48 | m_title = title; | |
49 | m_parent = NULL; | |
50 | m_eventHandler = this; | |
51 | m_noItems = 0; | |
52 | m_menuBar = NULL; | |
53 | if (m_title != "") | |
54 | { | |
55 | Append(-2, m_title) ; | |
56 | AppendSeparator() ; | |
57 | } | |
58 | ||
59 | Callback(func); | |
60 | ||
61 | // TODO create menu | |
62 | } | |
63 | ||
64 | // The wxWindow destructor will take care of deleting the submenus. | |
65 | wxMenu::~wxMenu() | |
66 | { | |
67 | // TODO destroy menu and children | |
68 | ||
69 | wxNode *node = m_menuItems.First(); | |
70 | while (node) | |
71 | { | |
72 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
73 | ||
74 | // Delete child menus. | |
75 | // Beware: they must not be appended to children list!!! | |
76 | // (because order of delete is significant) | |
77 | if (item->GetSubMenu()) | |
78 | item->DeleteSubMenu(); | |
79 | ||
80 | wxNode *next = node->Next(); | |
81 | delete item; | |
82 | delete node; | |
83 | node = next; | |
84 | } | |
85 | } | |
86 | ||
87 | void wxMenu::Break() | |
88 | { | |
89 | // TODO | |
90 | } | |
91 | ||
92 | // function appends a new item or submenu to the menu | |
93 | void wxMenu::Append(wxMenuItem *pItem) | |
94 | { | |
95 | // TODO | |
96 | ||
97 | wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" ); | |
98 | ||
99 | m_menuItems.Append(pItem); | |
100 | ||
101 | m_noItems++; | |
102 | } | |
103 | ||
104 | void wxMenu::AppendSeparator() | |
105 | { | |
106 | // TODO | |
107 | Append(new wxMenuItem(this, ID_SEPARATOR)); | |
108 | } | |
109 | ||
110 | // Pullright item | |
111 | void wxMenu::Append(int Id, const wxString& label, wxMenu *SubMenu, | |
112 | const wxString& helpString) | |
113 | { | |
114 | Append(new wxMenuItem(this, Id, label, helpString, FALSE, SubMenu)); | |
115 | } | |
116 | ||
117 | // Ordinary menu item | |
118 | void wxMenu::Append(int Id, const wxString& label, | |
119 | const wxString& helpString, bool checkable) | |
120 | { | |
121 | // 'checkable' parameter is useless for Windows. | |
122 | Append(new wxMenuItem(this, Id, label, helpString, checkable)); | |
123 | } | |
124 | ||
125 | void wxMenu::Delete(int id) | |
126 | { | |
127 | wxNode *node; | |
128 | wxMenuItem *item; | |
129 | int pos; | |
130 | ||
131 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) { | |
132 | item = (wxMenuItem *)node->Data(); | |
133 | if (item->GetId() == id) | |
134 | break; | |
135 | } | |
136 | ||
137 | if (!node) | |
138 | return; | |
139 | ||
140 | m_menuItems.DeleteNode(node); | |
141 | delete item; | |
142 | ||
143 | // TODO | |
144 | } | |
145 | ||
146 | void wxMenu::Enable(int Id, bool Flag) | |
147 | { | |
148 | wxMenuItem *item = FindItemForId(Id); | |
149 | wxCHECK_RET( item != NULL, "can't enable non-existing menu item" ); | |
150 | ||
151 | item->Enable(Flag); | |
152 | } | |
153 | ||
154 | bool wxMenu::Enabled(int Id) const | |
155 | { | |
156 | wxMenuItem *item = FindItemForId(Id); | |
157 | wxCHECK( item != NULL, FALSE ); | |
158 | ||
159 | return item->IsEnabled(); | |
160 | } | |
161 | ||
162 | void wxMenu::Check(int Id, bool Flag) | |
163 | { | |
164 | wxMenuItem *item = FindItemForId(Id); | |
165 | wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" ); | |
166 | ||
167 | item->Check(Flag); | |
168 | } | |
169 | ||
170 | bool wxMenu::Checked(int Id) const | |
171 | { | |
172 | wxMenuItem *item = FindItemForId(Id); | |
173 | wxCHECK( item != NULL, FALSE ); | |
174 | ||
175 | return item->IsChecked(); | |
176 | } | |
177 | ||
178 | void wxMenu::SetTitle(const wxString& label) | |
179 | { | |
180 | m_title = label ; | |
181 | // TODO | |
182 | } | |
183 | ||
f7387de5 | 184 | const wxString wxMenu::GetTitle() const |
93cf77c0 JS |
185 | { |
186 | return m_title; | |
187 | } | |
188 | ||
189 | void wxMenu::SetLabel(int id, const wxString& label) | |
190 | { | |
191 | wxMenuItem *item = FindItemForId(id) ; | |
192 | if (item==NULL) | |
193 | return; | |
194 | ||
195 | if (item->GetSubMenu()==NULL) | |
196 | { | |
197 | // TODO | |
198 | } | |
199 | else | |
200 | { | |
201 | // TODO | |
202 | } | |
203 | item->SetName(label); | |
204 | } | |
205 | ||
206 | wxString wxMenu::GetLabel(int Id) const | |
207 | { | |
208 | // TODO | |
209 | return wxString("") ; | |
210 | } | |
211 | ||
212 | // Finds the item id matching the given string, -1 if not found. | |
213 | int wxMenu::FindItem (const wxString& itemString) const | |
214 | { | |
215 | char buf1[200]; | |
216 | char buf2[200]; | |
217 | wxStripMenuCodes ((char *)(const char *)itemString, buf1); | |
218 | ||
219 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
220 | { | |
221 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
222 | if (item->GetSubMenu()) | |
223 | { | |
224 | int ans = item->GetSubMenu()->FindItem(itemString); | |
225 | if (ans > -1) | |
226 | return ans; | |
227 | } | |
228 | if ( !item->IsSeparator() ) | |
229 | { | |
230 | wxStripMenuCodes((char *)item->GetName().c_str(), buf2); | |
231 | if (strcmp(buf1, buf2) == 0) | |
232 | return item->GetId(); | |
233 | } | |
234 | } | |
235 | ||
236 | return -1; | |
237 | } | |
238 | ||
239 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const | |
240 | { | |
241 | if (itemMenu) | |
242 | *itemMenu = NULL; | |
243 | for (wxNode * node = m_menuItems.First (); node; node = node->Next ()) | |
244 | { | |
245 | wxMenuItem *item = (wxMenuItem *) node->Data (); | |
246 | ||
247 | if (item->GetId() == itemId) | |
248 | { | |
249 | if (itemMenu) | |
250 | *itemMenu = (wxMenu *) this; | |
251 | return item; | |
252 | } | |
253 | ||
254 | if (item->GetSubMenu()) | |
255 | { | |
256 | wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu); | |
257 | if (ans) | |
258 | return ans; | |
259 | } | |
260 | } | |
261 | ||
262 | if (itemMenu) | |
263 | *itemMenu = NULL; | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) | |
268 | { | |
269 | wxMenuItem *item = FindItemForId (itemId); | |
270 | if (item) | |
271 | item->SetHelp(helpString); | |
272 | } | |
273 | ||
274 | wxString wxMenu::GetHelpString (int itemId) const | |
275 | { | |
276 | wxMenuItem *item = FindItemForId (itemId); | |
277 | wxString str(""); | |
278 | return (item == NULL) ? str : item->GetHelp(); | |
279 | } | |
280 | ||
281 | void wxMenu::ProcessCommand(wxCommandEvent & event) | |
282 | { | |
283 | bool processed = FALSE; | |
284 | ||
285 | // Try a callback | |
286 | if (m_callback) | |
287 | { | |
288 | (void) (*(m_callback)) (*this, event); | |
289 | processed = TRUE; | |
290 | } | |
291 | ||
292 | // Try the menu's event handler | |
293 | if ( !processed && GetEventHandler()) | |
294 | { | |
295 | processed = GetEventHandler()->ProcessEvent(event); | |
296 | } | |
34138703 | 297 | /* TODO |
93cf77c0 JS |
298 | // Try the window the menu was popped up from (and up |
299 | // through the hierarchy) | |
300 | if ( !processed && GetInvokingWindow()) | |
301 | processed = GetInvokingWindow()->ProcessEvent(event); | |
34138703 | 302 | */ |
93cf77c0 JS |
303 | } |
304 | ||
305 | bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) | |
306 | { | |
307 | // TODO | |
308 | return FALSE; | |
309 | } | |
310 | ||
311 | // Menu Bar | |
312 | wxMenuBar::wxMenuBar() | |
313 | { | |
314 | m_eventHandler = this; | |
315 | m_menuCount = 0; | |
316 | m_menus = NULL; | |
317 | m_titles = NULL; | |
318 | m_menuBarFrame = NULL; | |
319 | ||
320 | // TODO | |
321 | } | |
322 | ||
34138703 | 323 | wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[]) |
93cf77c0 JS |
324 | { |
325 | m_eventHandler = this; | |
326 | m_menuCount = n; | |
327 | m_menus = menus; | |
328 | m_titles = new wxString[n]; | |
329 | int i; | |
330 | for ( i = 0; i < n; i++ ) | |
331 | m_titles[i] = titles[i]; | |
332 | m_menuBarFrame = NULL; | |
333 | ||
334 | // TODO | |
335 | } | |
336 | ||
337 | wxMenuBar::~wxMenuBar() | |
338 | { | |
339 | int i; | |
340 | for (i = 0; i < m_menuCount; i++) | |
341 | { | |
342 | delete m_menus[i]; | |
343 | } | |
344 | delete[] m_menus; | |
345 | delete[] m_titles; | |
346 | ||
347 | // TODO | |
348 | } | |
349 | ||
350 | // Must only be used AFTER menu has been attached to frame, | |
351 | // otherwise use individual menus to enable/disable items | |
352 | void wxMenuBar::Enable(int id, bool flag) | |
353 | { | |
354 | wxMenu *itemMenu = NULL; | |
355 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
356 | if (!item) | |
357 | return; | |
358 | ||
359 | // TODO | |
360 | } | |
361 | ||
362 | void wxMenuBar::EnableTop(int pos, bool flag) | |
363 | { | |
364 | // TODO | |
365 | } | |
366 | ||
367 | // Must only be used AFTER menu has been attached to frame, | |
368 | // otherwise use individual menus | |
369 | void wxMenuBar::Check(int id, bool flag) | |
370 | { | |
371 | wxMenu *itemMenu = NULL; | |
372 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
373 | if (!item) | |
374 | return; | |
375 | ||
376 | if (!item->IsCheckable()) | |
377 | return ; | |
378 | ||
379 | // TODO | |
380 | } | |
381 | ||
382 | bool wxMenuBar::Checked(int id) const | |
383 | { | |
384 | wxMenu *itemMenu = NULL; | |
385 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
386 | if (!item) | |
387 | return FALSE; | |
388 | ||
389 | // TODO | |
390 | return FALSE; | |
391 | } | |
392 | ||
393 | bool wxMenuBar::Enabled(int id) const | |
394 | { | |
395 | wxMenu *itemMenu = NULL; | |
396 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
397 | if (!item) | |
398 | return FALSE; | |
399 | ||
400 | // TODO | |
401 | return FALSE ; | |
402 | } | |
403 | ||
404 | ||
405 | void wxMenuBar::SetLabel(int id, const wxString& label) | |
406 | { | |
407 | wxMenu *itemMenu = NULL; | |
408 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
409 | ||
410 | if (!item) | |
411 | return; | |
412 | ||
413 | // TODO | |
414 | } | |
415 | ||
416 | wxString wxMenuBar::GetLabel(int id) const | |
417 | { | |
418 | wxMenu *itemMenu = NULL; | |
419 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
420 | ||
421 | if (!item) | |
422 | return wxString(""); | |
423 | ||
424 | // TODO | |
425 | return wxString("") ; | |
426 | } | |
427 | ||
428 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) | |
429 | { | |
430 | // TODO | |
431 | } | |
432 | ||
433 | wxString wxMenuBar::GetLabelTop(int pos) const | |
434 | { | |
435 | // TODO | |
436 | return wxString(""); | |
437 | } | |
438 | ||
439 | bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) | |
440 | { | |
441 | // TODO | |
442 | return FALSE; | |
443 | } | |
444 | ||
445 | bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title) | |
446 | { | |
447 | // TODO | |
448 | return FALSE; | |
449 | } | |
450 | ||
451 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) | |
452 | { | |
453 | if (!OnAppend(menu, title)) | |
454 | return; | |
455 | ||
456 | m_menuCount ++; | |
457 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
458 | wxString *new_titles = new wxString[m_menuCount]; | |
459 | int i; | |
460 | ||
461 | for (i = 0; i < m_menuCount - 1; i++) | |
462 | { | |
463 | new_menus[i] = m_menus[i]; | |
464 | m_menus[i] = NULL; | |
465 | new_titles[i] = m_titles[i]; | |
466 | m_titles[i] = ""; | |
467 | } | |
468 | if (m_menus) | |
469 | { | |
470 | delete[]m_menus; | |
471 | delete[]m_titles; | |
472 | } | |
473 | m_menus = new_menus; | |
474 | m_titles = new_titles; | |
475 | ||
476 | m_menus[m_menuCount - 1] = (wxMenu *)menu; | |
477 | m_titles[m_menuCount - 1] = title; | |
478 | ||
479 | // TODO | |
480 | } | |
481 | ||
482 | void wxMenuBar::Delete(wxMenu * menu, int i) | |
483 | { | |
484 | int j; | |
485 | int ii = (int) i; | |
486 | ||
487 | if (menu != 0) | |
488 | { | |
489 | for (ii = 0; ii < m_menuCount; ii++) | |
490 | { | |
491 | if (m_menus[ii] == menu) | |
492 | break; | |
493 | } | |
494 | if (ii >= m_menuCount) | |
495 | return; | |
496 | } else | |
497 | { | |
498 | if (ii < 0 || ii >= m_menuCount) | |
499 | return; | |
500 | menu = m_menus[ii]; | |
501 | } | |
502 | ||
503 | if (!OnDelete(menu, ii)) | |
504 | return; | |
505 | ||
506 | menu->SetParent(NULL); | |
507 | ||
508 | -- m_menuCount; | |
509 | for (j = ii; j < m_menuCount; j++) | |
510 | { | |
511 | m_menus[j] = m_menus[j + 1]; | |
512 | m_titles[j] = m_titles[j + 1]; | |
513 | } | |
514 | } | |
515 | ||
516 | // Find the menu menuString, item itemString, and return the item id. | |
517 | // Returns -1 if none found. | |
518 | int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const | |
519 | { | |
520 | char buf1[200]; | |
521 | char buf2[200]; | |
522 | wxStripMenuCodes ((char *)(const char *)menuString, buf1); | |
523 | int i; | |
524 | for (i = 0; i < m_menuCount; i++) | |
525 | { | |
526 | wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); | |
527 | if (strcmp (buf1, buf2) == 0) | |
528 | return m_menus[i]->FindItem (itemString); | |
529 | } | |
530 | return -1; | |
531 | } | |
532 | ||
533 | wxMenuItem *wxMenuBar::FindItemForId (int Id, wxMenu ** itemMenu) const | |
534 | { | |
535 | if (itemMenu) | |
536 | *itemMenu = NULL; | |
537 | ||
538 | wxMenuItem *item = NULL; | |
539 | int i; | |
540 | for (i = 0; i < m_menuCount; i++) | |
541 | if ((item = m_menus[i]->FindItemForId (Id, itemMenu))) | |
542 | return item; | |
543 | return NULL; | |
544 | } | |
545 | ||
546 | void wxMenuBar::SetHelpString (int Id, const wxString& helpString) | |
547 | { | |
548 | int i; | |
549 | for (i = 0; i < m_menuCount; i++) | |
550 | { | |
551 | if (m_menus[i]->FindItemForId (Id)) | |
552 | { | |
553 | m_menus[i]->SetHelpString (Id, helpString); | |
554 | return; | |
555 | } | |
556 | } | |
557 | } | |
558 | ||
559 | wxString wxMenuBar::GetHelpString (int Id) const | |
560 | { | |
561 | int i; | |
562 | for (i = 0; i < m_menuCount; i++) | |
563 | { | |
564 | if (m_menus[i]->FindItemForId (Id)) | |
34138703 | 565 | return wxString(m_menus[i]->GetHelpString (Id)); |
93cf77c0 JS |
566 | } |
567 | return wxString(""); | |
568 | } | |
569 | ||
570 |