]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "menu.h" | |
12 | #pragma implementation "menuitem.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/menu.h" | |
16 | #include "wx/log.h" | |
17 | #include "wx/intl.h" | |
18 | #include "wx/app.h" | |
19 | ||
20 | #include "gdk/gdk.h" | |
21 | #include "gtk/gtk.h" | |
22 | ||
23 | //----------------------------------------------------------------------------- | |
24 | // idle system | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | extern void wxapp_install_idle_handler(); | |
28 | extern bool g_isIdle; | |
29 | ||
30 | //----------------------------------------------------------------------------- | |
31 | // wxMenuBar | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
35 | ||
36 | wxMenuBar::wxMenuBar( long style ) | |
37 | { | |
38 | /* the parent window is known after wxFrame::SetMenu() */ | |
39 | m_needParent = FALSE; | |
40 | m_style = style; | |
41 | m_invokingWindow = (wxWindow*) NULL; | |
42 | ||
43 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || | |
44 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, _T("menubar") )) | |
45 | { | |
46 | wxFAIL_MSG( _T("wxMenuBar creation failed") ); | |
47 | return; | |
48 | } | |
49 | ||
50 | m_menus.DeleteContents( TRUE ); | |
51 | ||
52 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ | |
53 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
54 | m_accel = gtk_accel_group_new(); | |
55 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
56 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
57 | #else | |
58 | m_menubar = gtk_menu_bar_new(); | |
59 | #endif | |
60 | ||
61 | if (style & wxMB_DOCKABLE) | |
62 | { | |
63 | m_widget = gtk_handle_box_new(); | |
64 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); | |
65 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
66 | } | |
67 | else | |
68 | { | |
69 | m_widget = GTK_WIDGET(m_menubar); | |
70 | } | |
71 | ||
72 | PostCreation(); | |
73 | } | |
74 | ||
75 | wxMenuBar::wxMenuBar() | |
76 | { | |
77 | /* the parent window is known after wxFrame::SetMenu() */ | |
78 | m_needParent = FALSE; | |
79 | m_style = 0; | |
80 | m_invokingWindow = (wxWindow*) NULL; | |
81 | ||
82 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || | |
83 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("menubar") )) | |
84 | { | |
85 | wxFAIL_MSG( _T("wxMenuBar creation failed") ); | |
86 | return; | |
87 | } | |
88 | ||
89 | m_menus.DeleteContents( TRUE ); | |
90 | ||
91 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ | |
92 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
93 | m_accel = gtk_accel_group_new(); | |
94 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
95 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
96 | #else | |
97 | m_menubar = gtk_menu_bar_new(); | |
98 | #endif | |
99 | ||
100 | m_widget = GTK_WIDGET(m_menubar); | |
101 | ||
102 | PostCreation(); | |
103 | } | |
104 | ||
105 | wxMenuBar::~wxMenuBar() | |
106 | { | |
107 | // how to destroy a GtkItemFactory ? | |
108 | } | |
109 | ||
110 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
111 | { | |
112 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
113 | ||
114 | #if (GTK_MINOR_VERSION > 0) | |
115 | wxWindow *top_frame = win; | |
116 | while (top_frame->GetParent()) top_frame = top_frame->GetParent(); | |
117 | ||
118 | /* support for native hot keys */ | |
119 | gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
120 | #endif | |
121 | ||
122 | wxNode *node = menu->GetItems().First(); | |
123 | while (node) | |
124 | { | |
125 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
126 | if (menuitem->IsSubMenu()) | |
127 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
128 | node = node->Next(); | |
129 | } | |
130 | } | |
131 | ||
132 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
133 | { | |
134 | menu->SetInvokingWindow( win ); | |
135 | ||
136 | #if (GTK_MINOR_VERSION > 0) | |
137 | wxWindow *top_frame = win; | |
138 | while (top_frame->GetParent()) | |
139 | top_frame = top_frame->GetParent(); | |
140 | ||
141 | /* support for native hot keys */ | |
142 | gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
143 | #endif | |
144 | ||
145 | wxNode *node = menu->GetItems().First(); | |
146 | while (node) | |
147 | { | |
148 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
149 | if (menuitem->IsSubMenu()) | |
150 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
151 | node = node->Next(); | |
152 | } | |
153 | } | |
154 | ||
155 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
156 | { | |
157 | m_invokingWindow = win; | |
158 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
159 | wxWindow *top_frame = win; | |
160 | while (top_frame->GetParent()) | |
161 | top_frame = top_frame->GetParent(); | |
162 | ||
163 | /* support for native key accelerators indicated by underscroes */ | |
164 | gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
165 | #endif | |
166 | ||
167 | wxNode *node = m_menus.First(); | |
168 | while (node) | |
169 | { | |
170 | wxMenu *menu = (wxMenu*)node->Data(); | |
171 | wxMenubarSetInvokingWindow( menu, win ); | |
172 | node = node->Next(); | |
173 | } | |
174 | } | |
175 | ||
176 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
177 | { | |
178 | m_invokingWindow = (wxWindow*) NULL; | |
179 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
180 | wxWindow *top_frame = win; | |
181 | while (top_frame->GetParent()) | |
182 | top_frame = top_frame->GetParent(); | |
183 | ||
184 | /* support for native key accelerators indicated by underscroes */ | |
185 | gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
186 | #endif | |
187 | ||
188 | wxNode *node = m_menus.First(); | |
189 | while (node) | |
190 | { | |
191 | wxMenu *menu = (wxMenu*)node->Data(); | |
192 | wxMenubarUnsetInvokingWindow( menu, win ); | |
193 | node = node->Next(); | |
194 | } | |
195 | } | |
196 | ||
197 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
198 | { | |
199 | m_menus.Append( menu ); | |
200 | ||
201 | const wxChar *pc; | |
202 | ||
203 | /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ | |
204 | wxString str; | |
205 | for ( pc = title; *pc != _T('\0'); pc++ ) | |
206 | { | |
207 | if (*pc == _T('&')) | |
208 | { | |
209 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
210 | str << _T('_'); | |
211 | } | |
212 | else if (*pc == _T('/')) | |
213 | { | |
214 | str << _T('\\'); | |
215 | #endif | |
216 | } | |
217 | else | |
218 | { | |
219 | #if __WXGTK12__ | |
220 | if ( *pc == _T('_') ) | |
221 | { | |
222 | // underscores must be doubled to prevent them from being | |
223 | // interpreted as accelerator character prefix by GTK | |
224 | str << *pc; | |
225 | } | |
226 | #endif // GTK+ 1.2 | |
227 | ||
228 | str << *pc; | |
229 | } | |
230 | } | |
231 | ||
232 | /* this doesn't have much effect right now */ | |
233 | menu->SetTitle( str ); | |
234 | ||
235 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ | |
236 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
237 | ||
238 | /* local buffer in multibyte form */ | |
239 | wxString buf; | |
240 | buf << _T('/') << str.c_str(); | |
241 | ||
242 | char *cbuf = new char[buf.Length()+1]; | |
243 | strcpy(cbuf, buf.mbc_str()); | |
244 | ||
245 | GtkItemFactoryEntry entry; | |
246 | entry.path = (gchar *)cbuf; // const_cast | |
247 | entry.accelerator = (gchar*) NULL; | |
248 | entry.callback = (GtkItemFactoryCallback) NULL; | |
249 | entry.callback_action = 0; | |
250 | entry.item_type = "<Branch>"; | |
251 | ||
252 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
253 | /* in order to get the pointer to the item we need the item text _without_ underscores */ | |
254 | wxString tmp = _T("<main>/"); | |
255 | for ( pc = str; *pc != _T('\0'); pc++ ) | |
256 | { | |
257 | // contrary to the common sense, we must throw out _all_ underscores, | |
258 | // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we | |
259 | // might naively think). IMHO it's a bug in GTK+ (VZ) | |
260 | while (*pc == _T('_')) | |
261 | pc++; | |
262 | tmp << *pc; | |
263 | } | |
264 | menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() ); | |
265 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
266 | delete [] cbuf; | |
267 | #else | |
268 | ||
269 | menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() ); | |
270 | gtk_widget_show( menu->m_owner ); | |
271 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
272 | ||
273 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); | |
274 | ||
275 | #endif | |
276 | ||
277 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables | |
278 | // adding menu later on. | |
279 | if (m_invokingWindow) | |
280 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
281 | } | |
282 | ||
283 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
284 | { | |
285 | if (menu->GetTitle() == menuString) | |
286 | { | |
287 | int res = menu->FindItem( itemString ); | |
288 | if (res != wxNOT_FOUND) | |
289 | return res; | |
290 | } | |
291 | ||
292 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast | |
293 | while (node) | |
294 | { | |
295 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
296 | if (item->IsSubMenu()) | |
297 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
298 | ||
299 | node = node->Next(); | |
300 | } | |
301 | ||
302 | return wxNOT_FOUND; | |
303 | } | |
304 | ||
305 | wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem ) const | |
306 | { | |
307 | if ( menuForItem ) | |
308 | { | |
309 | // TODO return the pointer to the menu | |
310 | ||
311 | *menuForItem = NULL; | |
312 | } | |
313 | ||
314 | return FindItem(itemId); | |
315 | } | |
316 | ||
317 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
318 | { | |
319 | wxNode *node = m_menus.First(); | |
320 | while (node) | |
321 | { | |
322 | wxMenu *menu = (wxMenu*)node->Data(); | |
323 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
324 | if (res != -1) return res; | |
325 | node = node->Next(); | |
326 | } | |
327 | return -1; | |
328 | } | |
329 | ||
330 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
331 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
332 | { | |
333 | wxMenuItem* result = menu->FindItem(id); | |
334 | ||
335 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast | |
336 | while ( node && result == NULL ) | |
337 | { | |
338 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
339 | if (item->IsSubMenu()) | |
340 | { | |
341 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
342 | } | |
343 | node = node->Next(); | |
344 | } | |
345 | ||
346 | return result; | |
347 | } | |
348 | ||
349 | wxMenuItem* wxMenuBar::FindItem( int id ) const | |
350 | { | |
351 | wxMenuItem* result = 0; | |
352 | wxNode *node = m_menus.First(); | |
353 | while (node && result == 0) | |
354 | { | |
355 | wxMenu *menu = (wxMenu*)node->Data(); | |
356 | result = FindMenuItemByIdRecursive( menu, id ); | |
357 | node = node->Next(); | |
358 | } | |
359 | ||
360 | return result; | |
361 | } | |
362 | ||
363 | void wxMenuBar::Check( int id, bool check ) | |
364 | { | |
365 | wxMenuItem* item = FindMenuItemById( id ); | |
366 | ||
367 | wxCHECK_RET( item, _T("wxMenuBar::Check: no such item") ); | |
368 | ||
369 | item->Check(check); | |
370 | } | |
371 | ||
372 | bool wxMenuBar::IsChecked( int id ) const | |
373 | { | |
374 | wxMenuItem* item = FindMenuItemById( id ); | |
375 | ||
376 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked: no such item") ); | |
377 | ||
378 | return item->IsChecked(); | |
379 | } | |
380 | ||
381 | void wxMenuBar::Enable( int id, bool enable ) | |
382 | { | |
383 | wxMenuItem* item = FindMenuItemById( id ); | |
384 | ||
385 | wxCHECK_RET( item, _T("wxMenuBar::Enable: no such item") ); | |
386 | ||
387 | item->Enable(enable); | |
388 | } | |
389 | ||
390 | bool wxMenuBar::IsEnabled( int id ) const | |
391 | { | |
392 | wxMenuItem* item = FindMenuItemById( id ); | |
393 | ||
394 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled: no such item") ); | |
395 | ||
396 | return item->IsEnabled(); | |
397 | } | |
398 | ||
399 | wxString wxMenuBar::GetLabel( int id ) const | |
400 | { | |
401 | wxMenuItem* item = FindMenuItemById( id ); | |
402 | ||
403 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel: no such item") ); | |
404 | ||
405 | return item->GetText(); | |
406 | } | |
407 | ||
408 | void wxMenuBar::SetLabel( int id, const wxString &label ) | |
409 | { | |
410 | wxMenuItem* item = FindMenuItemById( id ); | |
411 | ||
412 | wxCHECK_RET( item, _T("wxMenuBar::SetLabel: no such item") ); | |
413 | ||
414 | item->SetText( label ); | |
415 | } | |
416 | ||
417 | void wxMenuBar::EnableTop( int pos, bool flag ) | |
418 | { | |
419 | wxNode *node = m_menus.Nth( pos ); | |
420 | ||
421 | wxCHECK_RET( node, _T("menu not found") ); | |
422 | ||
423 | wxMenu* menu = (wxMenu*)node->Data(); | |
424 | ||
425 | if (menu->m_owner) | |
426 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
427 | } | |
428 | ||
429 | wxString wxMenuBar::GetLabelTop( int pos ) const | |
430 | { | |
431 | wxNode *node = m_menus.Nth( pos ); | |
432 | ||
433 | wxCHECK_MSG( node, _T("invalid"), _T("menu not found") ); | |
434 | ||
435 | wxMenu* menu = (wxMenu*)node->Data(); | |
436 | ||
437 | return menu->GetTitle(); | |
438 | } | |
439 | ||
440 | void wxMenuBar::SetLabelTop( int pos, const wxString& label ) | |
441 | { | |
442 | wxNode *node = m_menus.Nth( pos ); | |
443 | ||
444 | wxCHECK_RET( node, _T("menu not found") ); | |
445 | ||
446 | wxMenu* menu = (wxMenu*)node->Data(); | |
447 | ||
448 | menu->SetTitle( label ); | |
449 | } | |
450 | ||
451 | void wxMenuBar::SetHelpString( int id, const wxString& helpString ) | |
452 | { | |
453 | wxMenuItem* item = FindMenuItemById( id ); | |
454 | ||
455 | wxCHECK_RET( item, _T("wxMenuBar::SetHelpString: no such item") ); | |
456 | ||
457 | item->SetHelp( helpString ); | |
458 | } | |
459 | ||
460 | wxString wxMenuBar::GetHelpString( int id ) const | |
461 | { | |
462 | wxMenuItem* item = FindMenuItemById( id ); | |
463 | ||
464 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString: no such item") ); | |
465 | ||
466 | return item->GetHelp(); | |
467 | } | |
468 | ||
469 | //----------------------------------------------------------------------------- | |
470 | // "activate" | |
471 | //----------------------------------------------------------------------------- | |
472 | ||
473 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) | |
474 | { | |
475 | if (g_isIdle) wxapp_install_idle_handler(); | |
476 | ||
477 | int id = menu->FindMenuIdByMenuItem(widget); | |
478 | ||
479 | /* should find it for normal (not popup) menu */ | |
480 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); | |
481 | ||
482 | if (!menu->IsEnabled(id)) | |
483 | return; | |
484 | ||
485 | wxMenuItem* item = menu->FindItem( id ); | |
486 | wxCHECK_RET( item, _T("error in menu item callback") ); | |
487 | ||
488 | if (item->IsCheckable()) | |
489 | { | |
490 | if (item->GetCheckedFlag() == item->IsChecked()) | |
491 | { | |
492 | /* the menu item has been checked by calling wxMenuItem->Check() */ | |
493 | return; | |
494 | } | |
495 | else | |
496 | { | |
497 | /* the user pressed on the menu item -> report */ | |
498 | item->SetCheckedFlag(item->IsChecked()); /* make consistent again */ | |
499 | } | |
500 | } | |
501 | ||
502 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); | |
503 | event.SetEventObject( menu ); | |
504 | event.SetInt(id ); | |
505 | ||
506 | if (menu->GetCallback()) | |
507 | { | |
508 | (void) (*(menu->GetCallback())) (*menu, event); | |
509 | return; | |
510 | } | |
511 | ||
512 | if (menu->GetEventHandler()->ProcessEvent(event)) | |
513 | return; | |
514 | ||
515 | wxWindow *win = menu->GetInvokingWindow(); | |
516 | if (win) | |
517 | win->GetEventHandler()->ProcessEvent( event ); | |
518 | } | |
519 | ||
520 | //----------------------------------------------------------------------------- | |
521 | // "select" | |
522 | //----------------------------------------------------------------------------- | |
523 | ||
524 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
525 | { | |
526 | if (g_isIdle) wxapp_install_idle_handler(); | |
527 | ||
528 | int id = menu->FindMenuIdByMenuItem(widget); | |
529 | ||
530 | wxASSERT( id != -1 ); // should find it! | |
531 | ||
532 | if (!menu->IsEnabled(id)) | |
533 | return; | |
534 | ||
535 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); | |
536 | event.SetEventObject( menu ); | |
537 | ||
538 | if (menu->GetEventHandler()->ProcessEvent(event)) | |
539 | return; | |
540 | ||
541 | wxWindow *win = menu->GetInvokingWindow(); | |
542 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
543 | } | |
544 | ||
545 | //----------------------------------------------------------------------------- | |
546 | // "deselect" | |
547 | //----------------------------------------------------------------------------- | |
548 | ||
549 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
550 | { | |
551 | if (g_isIdle) wxapp_install_idle_handler(); | |
552 | ||
553 | int id = menu->FindMenuIdByMenuItem(widget); | |
554 | ||
555 | wxASSERT( id != -1 ); // should find it! | |
556 | ||
557 | if (!menu->IsEnabled(id)) | |
558 | return; | |
559 | ||
560 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
561 | event.SetEventObject( menu ); | |
562 | ||
563 | if (menu->GetEventHandler()->ProcessEvent(event)) | |
564 | return; | |
565 | ||
566 | wxWindow *win = menu->GetInvokingWindow(); | |
567 | if (win) | |
568 | win->GetEventHandler()->ProcessEvent( event ); | |
569 | } | |
570 | ||
571 | //----------------------------------------------------------------------------- | |
572 | // wxMenuItem | |
573 | //----------------------------------------------------------------------------- | |
574 | ||
575 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) | |
576 | ||
577 | wxMenuItem::wxMenuItem() | |
578 | { | |
579 | m_id = ID_SEPARATOR; | |
580 | m_isCheckMenu = FALSE; | |
581 | m_isChecked = FALSE; | |
582 | m_isEnabled = TRUE; | |
583 | m_subMenu = (wxMenu *) NULL; | |
584 | m_menuItem = (GtkWidget *) NULL; | |
585 | } | |
586 | ||
587 | // it's valid for this function to be called even if m_menuItem == NULL | |
588 | void wxMenuItem::SetName( const wxString& str ) | |
589 | { | |
590 | /* '\t' is the deliminator indicating a hot key */ | |
591 | m_text = _T(""); | |
592 | const wxChar *pc = str; | |
593 | for (; (*pc != _T('\0')) && (*pc != _T('\t')); pc++ ) | |
594 | { | |
595 | if (*pc == _T('&')) | |
596 | { | |
597 | #if (GTK_MINOR_VERSION > 0) | |
598 | m_text << _T('_'); | |
599 | } | |
600 | else if ( *pc == _T('_') ) // escape underscores | |
601 | { | |
602 | m_text << _T("__"); | |
603 | } | |
604 | else if (*pc == _T('/')) /* we have to filter out slashes ... */ | |
605 | { | |
606 | m_text << _T('\\'); /* ... and replace them with back slashes */ | |
607 | #endif | |
608 | } | |
609 | else | |
610 | m_text << *pc; | |
611 | } | |
612 | ||
613 | /* only GTK 1.2 knows about hot keys */ | |
614 | m_hotKey = _T(""); | |
615 | #if (GTK_MINOR_VERSION > 0) | |
616 | if(*pc == _T('\t')) | |
617 | { | |
618 | pc++; | |
619 | m_hotKey = pc; | |
620 | } | |
621 | #endif | |
622 | ||
623 | if (m_menuItem) | |
624 | { | |
625 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
626 | gtk_label_set( label, m_text.mb_str()); | |
627 | } | |
628 | } | |
629 | ||
630 | void wxMenuItem::Check( bool check ) | |
631 | { | |
632 | wxCHECK_RET( m_menuItem, _T("invalid menu item") ); | |
633 | ||
634 | wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") ) | |
635 | ||
636 | if (check == m_isChecked) return; | |
637 | ||
638 | m_isChecked = check; | |
639 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
640 | } | |
641 | ||
642 | void wxMenuItem::Enable( bool enable ) | |
643 | { | |
644 | wxCHECK_RET( m_menuItem, _T("invalid menu item") ); | |
645 | ||
646 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
647 | m_isEnabled = enable; | |
648 | } | |
649 | ||
650 | bool wxMenuItem::IsChecked() const | |
651 | { | |
652 | wxCHECK_MSG( m_menuItem, FALSE, _T("invalid menu item") ); | |
653 | ||
654 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! | |
655 | ||
656 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
657 | ||
658 | return bIsChecked; | |
659 | } | |
660 | ||
661 | //----------------------------------------------------------------------------- | |
662 | // wxMenu | |
663 | //----------------------------------------------------------------------------- | |
664 | ||
665 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
666 | ||
667 | void | |
668 | wxMenu::Init( const wxString& title, | |
669 | long style, | |
670 | const wxFunction func | |
671 | ) | |
672 | { | |
673 | m_title = title; | |
674 | m_items.DeleteContents( TRUE ); | |
675 | m_invokingWindow = (wxWindow *) NULL; | |
676 | m_style = style; | |
677 | ||
678 | #if (GTK_MINOR_VERSION > 0) | |
679 | m_accel = gtk_accel_group_new(); | |
680 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); | |
681 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
682 | #else | |
683 | m_menu = gtk_menu_new(); // Do not show! | |
684 | #endif | |
685 | ||
686 | m_callback = func; | |
687 | ||
688 | m_eventHandler = this; | |
689 | m_clientData = (void*) NULL; | |
690 | ||
691 | if (m_title.IsNull()) m_title = _T(""); | |
692 | if (m_title != _T("")) | |
693 | { | |
694 | Append(-2, m_title); | |
695 | AppendSeparator(); | |
696 | } | |
697 | ||
698 | m_owner = (GtkWidget*) NULL; | |
699 | ||
700 | #if (GTK_MINOR_VERSION > 0) | |
701 | /* Tearoffs are entries, just like separators. So if we want this | |
702 | menu to be a tear-off one, we just append a tearoff entry | |
703 | immediately. */ | |
704 | if(m_style & wxMENU_TEAROFF) | |
705 | { | |
706 | GtkItemFactoryEntry entry; | |
707 | entry.path = "/tearoff"; | |
708 | entry.callback = (GtkItemFactoryCallback) NULL; | |
709 | entry.callback_action = 0; | |
710 | entry.item_type = "<Tearoff>"; | |
711 | entry.accelerator = (gchar*) NULL; | |
712 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
713 | //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" ); | |
714 | } | |
715 | #endif | |
716 | } | |
717 | ||
718 | wxMenu::~wxMenu() | |
719 | { | |
720 | /* how do we delete an item-factory ? */ | |
721 | gtk_widget_destroy( m_menu ); | |
722 | ||
723 | } | |
724 | ||
725 | void wxMenu::SetTitle( const wxString& title ) | |
726 | { | |
727 | // TODO Waiting for something better | |
728 | m_title = title; | |
729 | } | |
730 | ||
731 | const wxString wxMenu::GetTitle() const | |
732 | { | |
733 | return m_title; | |
734 | } | |
735 | ||
736 | void wxMenu::AppendSeparator() | |
737 | { | |
738 | wxMenuItem *mitem = new wxMenuItem(); | |
739 | mitem->SetId(ID_SEPARATOR); | |
740 | ||
741 | #if (GTK_MINOR_VERSION > 0) | |
742 | GtkItemFactoryEntry entry; | |
743 | entry.path = "/sep"; | |
744 | entry.callback = (GtkItemFactoryCallback) NULL; | |
745 | entry.callback_action = 0; | |
746 | entry.item_type = "<Separator>"; | |
747 | entry.accelerator = (gchar*) NULL; | |
748 | ||
749 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
750 | ||
751 | /* this will be wrong for more than one separator. do we care? */ | |
752 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" ); | |
753 | #else | |
754 | GtkWidget *menuItem = gtk_menu_item_new(); | |
755 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
756 | gtk_widget_show( menuItem ); | |
757 | #endif | |
758 | ||
759 | mitem->SetMenuItem(menuItem); | |
760 | m_items.Append( mitem ); | |
761 | } | |
762 | ||
763 | #if (GTK_MINOR_VERSION > 0) | |
764 | static char* GetHotKey( const wxString &hotkey, char *hotbuf ) | |
765 | { | |
766 | if (hotkey.IsEmpty()) return (char*) NULL; | |
767 | ||
768 | switch (hotkey[0]) | |
769 | { | |
770 | case _T('a'): /* Alt */ | |
771 | case _T('A'): | |
772 | case _T('m'): /* Meta */ | |
773 | case _T('M'): | |
774 | { | |
775 | strcpy( hotbuf, "<alt>" ); | |
776 | wxString last = hotkey.Right(1); | |
777 | strcat( hotbuf, last.mb_str() ); | |
778 | return hotbuf; | |
779 | } | |
780 | case _T('c'): /* Ctrl */ | |
781 | case _T('C'): | |
782 | case _T('s'): /* Strg, yeah man, I'm German */ | |
783 | case _T('S'): | |
784 | { | |
785 | strcpy( hotbuf, "<control>" ); | |
786 | wxString last = hotkey.Right(1); | |
787 | strcat( hotbuf, last.mb_str() ); | |
788 | return hotbuf; | |
789 | } | |
790 | case _T('F'): /* function keys */ | |
791 | { | |
792 | strcpy( hotbuf, hotkey.mb_str() ); | |
793 | return hotbuf; | |
794 | } | |
795 | default: | |
796 | { | |
797 | } | |
798 | } | |
799 | return (char*) NULL; | |
800 | } | |
801 | #endif | |
802 | ||
803 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) | |
804 | { | |
805 | wxMenuItem *mitem = new wxMenuItem(); | |
806 | mitem->SetId(id); | |
807 | mitem->SetText(item); | |
808 | mitem->SetHelp(helpStr); | |
809 | mitem->SetCheckable(checkable); | |
810 | ||
811 | #if (GTK_MINOR_VERSION > 0) | |
812 | /* text has "_" instead of "&" after mitem->SetText() */ | |
813 | wxString text( mitem->GetText() ); | |
814 | ||
815 | /* local buffer in multibyte form */ | |
816 | char buf[200]; | |
817 | strcpy( buf, "/" ); | |
818 | strcat( buf, text.mb_str() ); | |
819 | ||
820 | GtkItemFactoryEntry entry; | |
821 | entry.path = buf; | |
822 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; | |
823 | entry.callback_action = 0; | |
824 | if (checkable) | |
825 | entry.item_type = "<CheckItem>"; | |
826 | else | |
827 | entry.item_type = "<Item>"; | |
828 | ||
829 | char hotbuf[50]; | |
830 | entry.accelerator = GetHotKey( mitem->GetHotKey(), hotbuf ); | |
831 | ||
832 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
833 | ||
834 | /* in order to get the pointer to the item we need the item text _without_ underscores */ | |
835 | wxString s = _T("<main>/"); | |
836 | for ( const wxChar *pc = text; *pc != _T('\0'); pc++ ) | |
837 | { | |
838 | while (*pc == _T('_')) pc++; /* skip it */ | |
839 | s << *pc; | |
840 | } | |
841 | ||
842 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() ); | |
843 | ||
844 | #else | |
845 | ||
846 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() ) | |
847 | : gtk_menu_item_new_with_label( mitem->GetText().mb_str() ); | |
848 | ||
849 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
850 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
851 | (gpointer)this ); | |
852 | ||
853 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
854 | gtk_widget_show( menuItem ); | |
855 | ||
856 | #endif | |
857 | ||
858 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
859 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
860 | (gpointer)this ); | |
861 | ||
862 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
863 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
864 | (gpointer)this ); | |
865 | ||
866 | mitem->SetMenuItem(menuItem); | |
867 | ||
868 | m_items.Append( mitem ); | |
869 | } | |
870 | ||
871 | void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr ) | |
872 | { | |
873 | wxMenuItem *mitem = new wxMenuItem(); | |
874 | mitem->SetId(id); | |
875 | mitem->SetText(item); | |
876 | mitem->SetHelp(helpStr); | |
877 | ||
878 | #if (GTK_MINOR_VERSION > 0) | |
879 | /* text has "_" instead of "&" after mitem->SetText() */ | |
880 | wxString text( mitem->GetText() ); | |
881 | ||
882 | /* local buffer in multibyte form */ | |
883 | char buf[200]; | |
884 | strcpy( buf, "/" ); | |
885 | strcat( buf, text.mb_str() ); | |
886 | ||
887 | GtkItemFactoryEntry entry; | |
888 | entry.path = buf; | |
889 | entry.callback = (GtkItemFactoryCallback) 0; | |
890 | entry.callback_action = 0; | |
891 | entry.item_type = "<Branch>"; | |
892 | ||
893 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
894 | ||
895 | /* in order to get the pointer to the item we need the item text _without_ underscores */ | |
896 | wxString s = _T("<main>/"); | |
897 | for ( const wxChar *pc = text; *pc != _T('\0'); pc++ ) | |
898 | { | |
899 | if (*pc == _T('_')) pc++; /* skip it */ | |
900 | s << *pc; | |
901 | } | |
902 | ||
903 | GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() ); | |
904 | ||
905 | #else | |
906 | ||
907 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str()); | |
908 | ||
909 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
910 | gtk_widget_show( menuItem ); | |
911 | ||
912 | #endif | |
913 | ||
914 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
915 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
916 | (gpointer*)this ); | |
917 | ||
918 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
919 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
920 | (gpointer*)this ); | |
921 | ||
922 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); | |
923 | ||
924 | mitem->SetMenuItem(menuItem); | |
925 | mitem->SetSubMenu(subMenu); | |
926 | ||
927 | m_items.Append( mitem ); | |
928 | } | |
929 | ||
930 | void wxMenu::Append( wxMenuItem *item ) | |
931 | { | |
932 | m_items.Append( item ); | |
933 | ||
934 | GtkWidget *menuItem = (GtkWidget*) NULL; | |
935 | ||
936 | if (item->IsSeparator()) | |
937 | menuItem = gtk_menu_item_new(); | |
938 | else if (item->IsSubMenu()) | |
939 | menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str()); | |
940 | else | |
941 | menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str()) | |
942 | : gtk_menu_item_new_with_label(item->GetText().mbc_str()); | |
943 | ||
944 | if (!item->IsSeparator()) | |
945 | { | |
946 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
947 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
948 | (gpointer*)this ); | |
949 | ||
950 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
951 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
952 | (gpointer*)this ); | |
953 | ||
954 | if (!item->IsSubMenu()) | |
955 | { | |
956 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
957 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
958 | (gpointer*)this ); | |
959 | } | |
960 | } | |
961 | ||
962 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
963 | gtk_widget_show( menuItem ); | |
964 | item->SetMenuItem(menuItem); | |
965 | } | |
966 | ||
967 | int wxMenu::FindItem( const wxString itemString ) const | |
968 | { | |
969 | wxString s = _T(""); | |
970 | for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ ) | |
971 | { | |
972 | if (*pc == _T('&')) | |
973 | { | |
974 | pc++; /* skip it */ | |
975 | #if (GTK_MINOR_VERSION > 0) | |
976 | s << _T('_'); | |
977 | #endif | |
978 | } | |
979 | s << *pc; | |
980 | } | |
981 | ||
982 | wxNode *node = m_items.First(); | |
983 | while (node) | |
984 | { | |
985 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
986 | if (item->GetText() == s) | |
987 | { | |
988 | return item->GetId(); | |
989 | } | |
990 | node = node->Next(); | |
991 | } | |
992 | ||
993 | return wxNOT_FOUND; | |
994 | } | |
995 | ||
996 | void wxMenu::Enable( int id, bool enable ) | |
997 | { | |
998 | wxMenuItem *item = FindItem(id); | |
999 | ||
1000 | wxCHECK_RET( item, _T("wxMenu::Enable: no such item") ); | |
1001 | ||
1002 | item->Enable(enable); | |
1003 | } | |
1004 | ||
1005 | bool wxMenu::IsEnabled( int id ) const | |
1006 | { | |
1007 | wxMenuItem *item = FindItem(id); | |
1008 | ||
1009 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") ); | |
1010 | ||
1011 | return item->IsEnabled(); | |
1012 | } | |
1013 | ||
1014 | void wxMenu::Check( int id, bool enable ) | |
1015 | { | |
1016 | wxMenuItem *item = FindItem(id); | |
1017 | ||
1018 | wxCHECK_RET( item, _T("wxMenu::Check: no such item") ); | |
1019 | ||
1020 | item->Check(enable); | |
1021 | } | |
1022 | ||
1023 | bool wxMenu::IsChecked( int id ) const | |
1024 | { | |
1025 | wxMenuItem *item = FindItem(id); | |
1026 | ||
1027 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") ); | |
1028 | ||
1029 | return item->IsChecked(); | |
1030 | } | |
1031 | ||
1032 | void wxMenu::SetLabel( int id, const wxString &label ) | |
1033 | { | |
1034 | wxMenuItem *item = FindItem(id); | |
1035 | ||
1036 | wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") ); | |
1037 | ||
1038 | item->SetText(label); | |
1039 | } | |
1040 | ||
1041 | wxString wxMenu::GetLabel( int id ) const | |
1042 | { | |
1043 | wxMenuItem *item = FindItem(id); | |
1044 | ||
1045 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") ); | |
1046 | ||
1047 | return item->GetText(); | |
1048 | } | |
1049 | ||
1050 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
1051 | { | |
1052 | wxMenuItem *item = FindItem(id); | |
1053 | ||
1054 | wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") ); | |
1055 | ||
1056 | item->SetHelp( helpString ); | |
1057 | } | |
1058 | ||
1059 | wxString wxMenu::GetHelpString( int id ) const | |
1060 | { | |
1061 | wxMenuItem *item = FindItem(id); | |
1062 | ||
1063 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") ); | |
1064 | ||
1065 | return item->GetHelp(); | |
1066 | } | |
1067 | ||
1068 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
1069 | { | |
1070 | wxNode *node = m_items.First(); | |
1071 | while (node) | |
1072 | { | |
1073 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1074 | if (item->GetMenuItem() == menuItem) | |
1075 | return item->GetId(); | |
1076 | node = node->Next(); | |
1077 | } | |
1078 | ||
1079 | return wxNOT_FOUND; | |
1080 | } | |
1081 | ||
1082 | wxMenuItem *wxMenu::FindItem(int id) const | |
1083 | { | |
1084 | wxNode *node = m_items.First(); | |
1085 | while (node) | |
1086 | { | |
1087 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1088 | if (item->GetId() == id) | |
1089 | { | |
1090 | return item; | |
1091 | } | |
1092 | node = node->Next(); | |
1093 | } | |
1094 | ||
1095 | /* Not finding anything here can be correct | |
1096 | * when search the entire menu system for | |
1097 | * an entry -> no error message. */ | |
1098 | ||
1099 | return (wxMenuItem *) NULL; | |
1100 | } | |
1101 | ||
1102 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
1103 | { | |
1104 | m_invokingWindow = win; | |
1105 | } | |
1106 | ||
1107 | wxWindow *wxMenu::GetInvokingWindow() | |
1108 | { | |
1109 | return m_invokingWindow; | |
1110 | } | |
1111 | ||
1112 | // Update a menu and all submenus recursively. source is the object that has | |
1113 | // the update event handlers defined for it. If NULL, the menu or associated | |
1114 | // window will be used. | |
1115 | void wxMenu::UpdateUI(wxEvtHandler* source) | |
1116 | { | |
1117 | if (!source && GetInvokingWindow()) | |
1118 | source = GetInvokingWindow()->GetEventHandler(); | |
1119 | if (!source) | |
1120 | source = GetEventHandler(); | |
1121 | if (!source) | |
1122 | source = this; | |
1123 | ||
1124 | wxNode* node = GetItems().First(); | |
1125 | while (node) | |
1126 | { | |
1127 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
1128 | if ( !item->IsSeparator() ) | |
1129 | { | |
1130 | wxWindowID id = item->GetId(); | |
1131 | wxUpdateUIEvent event(id); | |
1132 | event.SetEventObject( source ); | |
1133 | ||
1134 | if (source->ProcessEvent(event)) | |
1135 | { | |
1136 | if (event.GetSetText()) | |
1137 | SetLabel(id, event.GetText()); | |
1138 | if (event.GetSetChecked()) | |
1139 | Check(id, event.GetChecked()); | |
1140 | if (event.GetSetEnabled()) | |
1141 | Enable(id, event.GetEnabled()); | |
1142 | } | |
1143 | ||
1144 | if (item->GetSubMenu()) | |
1145 | item->GetSubMenu()->UpdateUI(source); | |
1146 | } | |
1147 | node = node->Next(); | |
1148 | } | |
1149 | } | |
1150 |