]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Better disabling of toolbars and menubars
[wxWidgets.git] / src / gtk / menu.cpp
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 // wxMenuBar
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
28
29 wxMenuBar::wxMenuBar()
30 {
31 m_needParent = FALSE; // hmmm
32
33 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
34
35 m_menus.DeleteContents( TRUE );
36
37 m_menubar = gtk_menu_bar_new();
38
39 m_widget = GTK_WIDGET(m_menubar);
40
41 PostCreation();
42
43 Show( TRUE );
44 }
45
46 void wxMenuBar::Append( wxMenu *menu, const wxString &title )
47 {
48 m_menus.Append( menu );
49 menu->m_title = title;
50
51 int pos;
52 do
53 {
54 pos = menu->m_title.First( '&' );
55 if (pos != -1) menu->m_title.Remove( pos, 1 );
56 } while (pos != -1);
57
58 menu->m_owner = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
59 gtk_widget_show( menu->m_owner );
60 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
61
62 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
63 }
64
65 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
66 {
67 if (menu->m_title == menuString)
68 {
69 int res = menu->FindItem( itemString );
70 if (res != -1) return res;
71 }
72
73 wxNode *node = menu->m_items.First();
74 while (node)
75 {
76 wxMenuItem *item = (wxMenuItem*)node->Data();
77 if (item->IsSubMenu())
78 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
79
80 node = node->Next();
81 }
82
83 return -1;
84 }
85
86 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
87 {
88 wxNode *node = m_menus.First();
89 while (node)
90 {
91 wxMenu *menu = (wxMenu*)node->Data();
92 int res = FindMenuItemRecursive( menu, menuString, itemString);
93 if (res != -1) return res;
94 node = node->Next();
95 }
96 return -1;
97 }
98
99 /* Find a wxMenuItem using its id. Recurses down into sub-menus */
100 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
101 {
102 wxMenuItem* result = menu->FindItem(id);
103
104 wxNode *node = menu->m_items.First();
105 while ( node && result == NULL )
106 {
107 wxMenuItem *item = (wxMenuItem*)node->Data();
108 if (item->IsSubMenu())
109 {
110 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
111 }
112 node = node->Next();
113 }
114
115 return result;
116 }
117
118 wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const
119 {
120 wxMenuItem* result = 0;
121 wxNode *node = m_menus.First();
122 while (node && result == 0)
123 {
124 wxMenu *menu = (wxMenu*)node->Data();
125 result = FindMenuItemByIdRecursive( menu, id );
126 node = node->Next();
127 }
128
129 return result;
130 }
131
132 void wxMenuBar::Check( int id, bool check )
133 {
134 wxMenuItem* item = FindMenuItemById( id );
135 if (item) item->Check(check);
136 }
137
138 bool wxMenuBar::Checked( int id ) const
139 {
140 wxMenuItem* item = FindMenuItemById( id );
141 if (item) return item->IsChecked();
142 return FALSE;
143 }
144
145 void wxMenuBar::Enable( int id, bool enable )
146 {
147 wxMenuItem* item = FindMenuItemById( id );
148 if (item) item->Enable(enable);
149 }
150
151 bool wxMenuBar::Enabled( int id ) const
152 {
153 wxMenuItem* item = FindMenuItemById( id );
154 if (item) return item->IsEnabled();
155 return FALSE;
156 }
157
158 wxString wxMenuBar::GetLabel( int id ) const
159 {
160 wxMenuItem* item = FindMenuItemById( id );
161
162 if (item) return item->GetText();
163
164 return "";
165 }
166
167 void wxMenuBar::SetLabel( int id, const wxString &label )
168 {
169 wxMenuItem* item = FindMenuItemById( id );
170
171 if (item) return item->SetText( label );
172 }
173
174 void wxMenuBar::EnableTop( int pos, bool flag )
175 {
176 wxNode *node = m_menus.Nth( pos );
177
178 wxCHECK_RET( node, "menu not found" );
179
180 wxMenu* menu = (wxMenu*)node->Data();
181
182 if (menu->m_owner) gtk_widget_set_sensitive( menu->m_owner, flag );
183 }
184
185 wxString wxMenuBar::GetLabelTop( int pos ) const
186 {
187 wxNode *node = m_menus.Nth( pos );
188
189 wxCHECK_MSG( node, "invalid", "menu not found" );
190
191 wxMenu* menu = (wxMenu*)node->Data();
192
193 return menu->GetTitle();
194 }
195
196 void wxMenuBar::SetLabelTop( int pos, const wxString& label )
197 {
198 wxNode *node = m_menus.Nth( pos );
199
200 wxCHECK_RET( node, "menu not found" );
201
202 wxMenu* menu = (wxMenu*)node->Data();
203
204 menu->SetTitle( label );
205 }
206
207 //-----------------------------------------------------------------------------
208 // "activate"
209 //-----------------------------------------------------------------------------
210
211 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
212 {
213 int id = menu->FindMenuIdByMenuItem(widget);
214
215 /* should find it for normal (not popup) menu */
216 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
217
218 if (!menu->IsEnabled(id)) return;
219
220 wxMenuItem* item = menu->FindItem( id );
221 wxCHECK_RET( item, "error in menu item callback" );
222
223 if (item->m_isCheckMenu)
224 {
225 if (item->m_isChecked == item->IsChecked())
226 {
227 /* the menu item has been checked by calling wxMenuItem->Check() */
228 return;
229 }
230 else
231 {
232 /* the user pressed on the menu item -> report */
233 item->m_isChecked = item->IsChecked(); /* make consistent again */
234 }
235 }
236
237 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
238 event.SetEventObject( menu );
239 event.SetInt(id );
240
241 if (menu->m_callback)
242 {
243 (void) (*(menu->m_callback)) (*menu, event);
244 return;
245 }
246
247 if (menu->GetEventHandler()->ProcessEvent(event)) return;
248
249 wxWindow *win = menu->GetInvokingWindow();
250 if (win) win->GetEventHandler()->ProcessEvent( event );
251 }
252
253 //-----------------------------------------------------------------------------
254 // "select"
255 //-----------------------------------------------------------------------------
256
257 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
258 {
259 int id = menu->FindMenuIdByMenuItem(widget);
260
261 wxASSERT( id != -1 ); // should find it!
262
263 if (!menu->IsEnabled(id)) return;
264
265 wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
266 event.SetEventObject( menu );
267 event.SetInt(id );
268
269 /* wxMSW doesn't call callback here either
270
271 if (menu->m_callback)
272 {
273 (void) (*(menu->m_callback)) (*menu, event);
274 return;
275 }
276 */
277
278 if (menu->GetEventHandler()->ProcessEvent(event)) return;
279
280 wxWindow *win = menu->GetInvokingWindow();
281 if (win) win->GetEventHandler()->ProcessEvent( event );
282 }
283
284 //-----------------------------------------------------------------------------
285 // wxMenuItem
286 //-----------------------------------------------------------------------------
287
288 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
289
290 wxMenuItem::wxMenuItem()
291 {
292 m_id = ID_SEPARATOR;
293 m_isCheckMenu = FALSE;
294 m_isChecked = FALSE;
295 m_isEnabled = TRUE;
296 m_subMenu = (wxMenu *) NULL;
297 m_menuItem = (GtkWidget *) NULL;
298 }
299
300 /* it's valid for this function to be called even if m_menuItem == NULL */
301 void wxMenuItem::SetName( const wxString& str )
302 {
303 m_text = "";
304 for ( const char *pc = str; *pc != '\0'; pc++ )
305 {
306 if (*pc == '&') pc++; /* skip it */
307 m_text << *pc;
308 }
309
310 if (m_menuItem)
311 {
312 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
313 gtk_label_set( label, m_text.c_str());
314 }
315 }
316
317 void wxMenuItem::Check( bool check )
318 {
319 wxCHECK_RET( m_menuItem, "invalid menu item" );
320
321 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
322
323 if (check == m_isChecked) return;
324
325 m_isChecked = check;
326 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
327 }
328
329 void wxMenuItem::Enable( bool enable )
330 {
331 wxCHECK_RET( m_menuItem, "invalid menu item" );
332
333 gtk_widget_set_sensitive( m_menuItem, enable );
334 m_isEnabled = enable;
335 }
336
337 bool wxMenuItem::IsChecked() const
338 {
339 wxCHECK_MSG( m_menuItem, FALSE, "invalid menu item" );
340
341 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
342
343 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
344
345 return bIsChecked;
346 }
347
348 //-----------------------------------------------------------------------------
349 // wxMenu
350 //-----------------------------------------------------------------------------
351
352 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
353
354 wxMenu::wxMenu( const wxString& title, const wxFunction func )
355 {
356 m_title = title;
357 m_items.DeleteContents( TRUE );
358 m_invokingWindow = (wxWindow *) NULL;
359 m_menu = gtk_menu_new(); // Do not show!
360
361 m_callback = func;
362 m_eventHandler = this;
363 m_clientData = (void*) NULL;
364
365 if (m_title.IsNull()) m_title = "";
366 if (m_title != "")
367 {
368 Append(-2, m_title);
369 AppendSeparator();
370 }
371
372 m_owner = (GtkWidget*) NULL;
373 }
374
375 void wxMenu::SetTitle( const wxString& title )
376 {
377 /* Waiting for something better. */
378 m_title = title;
379 }
380
381 const wxString wxMenu::GetTitle() const
382 {
383 return m_title;
384 }
385
386 void wxMenu::AppendSeparator()
387 {
388 wxMenuItem *mitem = new wxMenuItem();
389 mitem->SetId(ID_SEPARATOR);
390
391 GtkWidget *menuItem = gtk_menu_item_new();
392 gtk_menu_append( GTK_MENU(m_menu), menuItem );
393 gtk_widget_show( menuItem );
394 mitem->SetMenuItem(menuItem);
395 m_items.Append( mitem );
396 }
397
398 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
399 {
400 wxMenuItem *mitem = new wxMenuItem();
401 mitem->SetId(id);
402 mitem->SetText(item);
403 mitem->SetHelp(helpStr);
404 mitem->SetCheckable(checkable);
405 const char *text = mitem->GetText();
406 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
407 : gtk_menu_item_new_with_label(text);
408
409 mitem->SetMenuItem(menuItem);
410
411 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
412 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
413 (gpointer*)this );
414
415 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
416 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
417 (gpointer*)this );
418
419 gtk_menu_append( GTK_MENU(m_menu), menuItem );
420 gtk_widget_show( menuItem );
421 m_items.Append( mitem );
422 }
423
424 void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
425 {
426 wxMenuItem *mitem = new wxMenuItem();
427 mitem->SetId(id);
428 mitem->SetText(text);
429
430 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
431 mitem->SetHelp(helpStr);
432 mitem->SetMenuItem(menuItem);
433 mitem->SetSubMenu(subMenu);
434
435 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
436 gtk_menu_append( GTK_MENU(m_menu), menuItem );
437 gtk_widget_show( menuItem );
438 m_items.Append( mitem );
439 }
440
441 int wxMenu::FindItem( const wxString itemString ) const
442 {
443 wxString s( itemString );
444
445 int pos;
446 do
447 {
448 pos = s.First( '&' );
449 if (pos != -1) s.Remove( pos, 1 );
450 } while (pos != -1);
451
452 wxNode *node = m_items.First();
453 while (node)
454 {
455 wxMenuItem *item = (wxMenuItem*)node->Data();
456 if (item->GetText() == s)
457 {
458 return item->GetId();
459 }
460 node = node->Next();
461 }
462
463 return -1;
464 }
465
466 void wxMenu::Enable( int id, bool enable )
467 {
468 wxMenuItem *item = FindItem(id);
469 if (item)
470 {
471 item->Enable(enable);
472 }
473 }
474
475 bool wxMenu::IsEnabled( int id ) const
476 {
477 wxMenuItem *item = FindItem(id);
478 if (item)
479 {
480 return item->IsEnabled();
481 }
482 else
483 {
484 return FALSE;
485 }
486 }
487
488 void wxMenu::Check( int id, bool enable )
489 {
490 wxMenuItem *item = FindItem(id);
491 if (item)
492 {
493 item->Check(enable);
494 }
495 }
496
497 bool wxMenu::IsChecked( int id ) const
498 {
499 wxMenuItem *item = FindItem(id);
500 if (item)
501 {
502 return item->IsChecked();
503 }
504 else
505 {
506 return FALSE;
507 }
508 }
509
510 void wxMenu::SetLabel( int id, const wxString &label )
511 {
512 wxMenuItem *item = FindItem(id);
513 if (item)
514 {
515 item->SetText(label);
516 }
517 }
518
519 wxString wxMenu::GetLabel( int id ) const
520 {
521 wxMenuItem *item = FindItem(id);
522 if (item)
523 {
524 return item->GetText();
525 }
526 else
527 {
528 return "";
529 }
530 }
531
532 void wxMenu::SetHelpString( int id, const wxString& helpString )
533 {
534 wxMenuItem *item = FindItem(id);
535 if (item) item->SetHelp( helpString );
536 }
537
538 wxString wxMenu::GetHelpString( int id ) const
539 {
540 wxMenuItem *item = FindItem(id);
541 if (item)
542 {
543 return item->GetHelp();
544 }
545 else
546 {
547 return "";
548 }
549 }
550
551 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
552 {
553 wxNode *node = m_items.First();
554 while (node)
555 {
556 wxMenuItem *item = (wxMenuItem*)node->Data();
557 if (item->GetMenuItem() == menuItem)
558 return item->GetId();
559 node = node->Next();
560 }
561
562 return -1;
563 }
564
565 wxMenuItem *wxMenu::FindItem(int id) const
566 {
567 wxNode *node = m_items.First();
568 while (node)
569 {
570 wxMenuItem *item = (wxMenuItem*)node->Data();
571 if (item->GetId() == id)
572 {
573 return item;
574 }
575 node = node->Next();
576 }
577
578 /* Not finding anything here can be correct
579 * when search the entire menu system for
580 * an entry -> no error message. */
581
582 return (wxMenuItem *) NULL;
583 }
584
585 void wxMenu::SetInvokingWindow( wxWindow *win )
586 {
587 m_invokingWindow = win;
588 }
589
590 wxWindow *wxMenu::GetInvokingWindow()
591 {
592 return m_invokingWindow;
593 }
594
595