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