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