]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
The usual amount of trash over my telephone fixed
[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 GtkWidget *root_menu;
59 root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
60 gtk_widget_show( root_menu );
61 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu );
62
63 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
64 }
65
66 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
67 {
68 if (menu->m_title == menuString)
69 {
70 int res = menu->FindItem( itemString );
71 if (res != -1) return res;
72 }
73
74 wxNode *node = menu->m_items.First();
75 while (node)
76 {
77 wxMenuItem *item = (wxMenuItem*)node->Data();
78 if (item->IsSubMenu())
79 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
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 if (item) return item->GetText();
162 return "";
163 }
164
165 void wxMenuBar::SetLabel( int id, const wxString &label )
166 {
167 wxMenuItem* item = FindMenuItemById( id );
168 if (item) return item->SetText( label );
169 }
170
171 void wxMenuBar::EnableTop( int WXUNUSED(pos), bool WXUNUSED(flag) )
172 {
173 }
174
175 wxString wxMenuBar::GetLabelTop( int WXUNUSED(pos) ) const
176 {
177 return "menu";
178 }
179
180 void wxMenuBar::SetLabelTop( int WXUNUSED(pos), const wxString& WXUNUSED(label) )
181 {
182 }
183
184 //-----------------------------------------------------------------------------
185 // "activate"
186 //-----------------------------------------------------------------------------
187
188 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
189 {
190 int id = menu->FindMenuIdByMenuItem(widget);
191
192 /* should find it for normal (not popup) menu */
193 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
194
195 if (!menu->IsEnabled(id)) return;
196
197 wxMenuItem* item = menu->FindItem( id );
198 wxCHECK_RET( item, "error in menu item callback" );
199
200 if (item->m_isCheckMenu)
201 {
202 if (item->m_isChecked == item->IsChecked())
203 {
204 /* the menu item has been checked by calling wxMenuItem->Check() */
205 return;
206 }
207 else
208 {
209 /* the user pressed on the menu item -> report */
210 item->m_isChecked = item->IsChecked(); /* make consistent again */
211 }
212 }
213
214 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
215 event.SetEventObject( menu );
216 event.SetInt(id );
217
218 if (menu->m_callback)
219 {
220 (void) (*(menu->m_callback)) (*menu, event);
221 return;
222 }
223
224 if (menu->GetEventHandler()->ProcessEvent(event)) return;
225
226 wxWindow *win = menu->GetInvokingWindow();
227 if (win) win->GetEventHandler()->ProcessEvent( event );
228 }
229
230 //-----------------------------------------------------------------------------
231 // "select"
232 //-----------------------------------------------------------------------------
233
234 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
235 {
236 int id = menu->FindMenuIdByMenuItem(widget);
237
238 wxASSERT( id != -1 ); // should find it!
239
240 if (!menu->IsEnabled(id)) return;
241
242 wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
243 event.SetEventObject( menu );
244 event.SetInt(id );
245
246 /* wxMSW doesn't call callback here either
247
248 if (menu->m_callback)
249 {
250 (void) (*(menu->m_callback)) (*menu, event);
251 return;
252 }
253 */
254
255 if (menu->GetEventHandler()->ProcessEvent(event)) return;
256
257 wxWindow *win = menu->GetInvokingWindow();
258 if (win) win->GetEventHandler()->ProcessEvent( event );
259 }
260
261 //-----------------------------------------------------------------------------
262 // wxMenuItem
263 //-----------------------------------------------------------------------------
264
265 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
266
267 wxMenuItem::wxMenuItem()
268 {
269 m_id = ID_SEPARATOR;
270 m_isCheckMenu = FALSE;
271 m_isChecked = FALSE;
272 m_isEnabled = TRUE;
273 m_subMenu = (wxMenu *) NULL;
274 m_menuItem = (GtkWidget *) NULL;
275 }
276
277 /* it's valid for this function to be called even if m_menuItem == NULL */
278 void wxMenuItem::SetName( const wxString& str )
279 {
280 m_text = "";
281 for ( const char *pc = str; *pc != '\0'; pc++ )
282 {
283 if (*pc == '&') pc++; /* skip it */
284 m_text << *pc;
285 }
286
287 if (m_menuItem)
288 {
289 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
290 gtk_label_set( label, m_text.c_str());
291 }
292 }
293
294 void wxMenuItem::Check( bool check )
295 {
296 wxCHECK_RET( m_menuItem, "invalid menu item" );
297
298 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
299
300 if (check == m_isChecked) return;
301
302 m_isChecked = check;
303 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
304 }
305
306 void wxMenuItem::Enable( bool enable )
307 {
308 wxCHECK_RET( m_menuItem, "invalid menu item" );
309
310 gtk_widget_set_sensitive( m_menuItem, enable );
311 m_isEnabled = enable;
312 }
313
314 bool wxMenuItem::IsChecked() const
315 {
316 wxCHECK_MSG( m_menuItem, FALSE, "invalid menu item" );
317
318 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
319
320 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
321
322 return bIsChecked;
323 }
324
325 //-----------------------------------------------------------------------------
326 // wxMenu
327 //-----------------------------------------------------------------------------
328
329 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
330
331 wxMenu::wxMenu( const wxString& title, const wxFunction func )
332 {
333 m_title = title;
334 m_items.DeleteContents( TRUE );
335 m_invokingWindow = (wxWindow *) NULL;
336 m_menu = gtk_menu_new(); // Do not show!
337
338 m_callback = func;
339 m_eventHandler = this;
340 m_clientData = (void*) NULL;
341
342 if (m_title.IsNull()) m_title = "";
343 if (m_title != "")
344 {
345 Append(-2, m_title);
346 AppendSeparator();
347 }
348 }
349
350 void wxMenu::SetTitle( const wxString& title )
351 {
352 /* Waiting for something better. */
353 m_title = title;
354 }
355
356 const wxString wxMenu::GetTitle() const
357 {
358 return m_title;
359 }
360
361 void wxMenu::AppendSeparator()
362 {
363 wxMenuItem *mitem = new wxMenuItem();
364 mitem->SetId(ID_SEPARATOR);
365
366 GtkWidget *menuItem = gtk_menu_item_new();
367 gtk_menu_append( GTK_MENU(m_menu), menuItem );
368 gtk_widget_show( menuItem );
369 mitem->SetMenuItem(menuItem);
370 m_items.Append( mitem );
371 }
372
373 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
374 {
375 wxMenuItem *mitem = new wxMenuItem();
376 mitem->SetId(id);
377 mitem->SetText(item);
378 mitem->SetHelp(helpStr);
379 mitem->SetCheckable(checkable);
380 const char *text = mitem->GetText();
381 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
382 : gtk_menu_item_new_with_label(text);
383
384 mitem->SetMenuItem(menuItem);
385
386 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
387 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
388 (gpointer*)this );
389
390 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
391 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
392 (gpointer*)this );
393
394 gtk_menu_append( GTK_MENU(m_menu), menuItem );
395 gtk_widget_show( menuItem );
396 m_items.Append( mitem );
397 }
398
399 void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
400 {
401 wxMenuItem *mitem = new wxMenuItem();
402 mitem->SetId(id);
403 mitem->SetText(text);
404
405 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
406 mitem->SetHelp(helpStr);
407 mitem->SetMenuItem(menuItem);
408 mitem->SetSubMenu(subMenu);
409
410 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
411 gtk_menu_append( GTK_MENU(m_menu), menuItem );
412 gtk_widget_show( menuItem );
413 m_items.Append( mitem );
414 }
415
416 int wxMenu::FindItem( const wxString itemString ) const
417 {
418 wxString s( itemString );
419
420 int pos;
421 do
422 {
423 pos = s.First( '&' );
424 if (pos != -1) s.Remove( pos, 1 );
425 } while (pos != -1);
426
427 wxNode *node = m_items.First();
428 while (node)
429 {
430 wxMenuItem *item = (wxMenuItem*)node->Data();
431 if (item->GetText() == s)
432 {
433 return item->GetId();
434 }
435 node = node->Next();
436 }
437
438 return -1;
439 }
440
441 void wxMenu::Enable( int id, bool enable )
442 {
443 wxMenuItem *item = FindItem(id);
444 if (item)
445 {
446 item->Enable(enable);
447 }
448 }
449
450 bool wxMenu::IsEnabled( int id ) const
451 {
452 wxMenuItem *item = FindItem(id);
453 if (item)
454 {
455 return item->IsEnabled();
456 }
457 else
458 {
459 return FALSE;
460 }
461 }
462
463 void wxMenu::Check( int id, bool enable )
464 {
465 wxMenuItem *item = FindItem(id);
466 if (item)
467 {
468 item->Check(enable);
469 }
470 }
471
472 bool wxMenu::IsChecked( int id ) const
473 {
474 wxMenuItem *item = FindItem(id);
475 if (item)
476 {
477 return item->IsChecked();
478 }
479 else
480 {
481 return FALSE;
482 }
483 }
484
485 void wxMenu::SetLabel( int id, const wxString &label )
486 {
487 wxMenuItem *item = FindItem(id);
488 if (item)
489 {
490 item->SetText(label);
491 }
492 }
493
494 wxString wxMenu::GetLabel( int id ) const
495 {
496 wxMenuItem *item = FindItem(id);
497 if (item)
498 {
499 return item->GetText();
500 }
501 else
502 {
503 return "";
504 }
505 }
506
507 void wxMenu::SetHelpString( int id, const wxString& helpString )
508 {
509 wxMenuItem *item = FindItem(id);
510 if (item) item->SetHelp( helpString );
511 }
512
513 wxString wxMenu::GetHelpString( int id ) const
514 {
515 wxMenuItem *item = FindItem(id);
516 if (item)
517 {
518 return item->GetHelp();
519 }
520 else
521 {
522 return "";
523 }
524 }
525
526 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
527 {
528 wxNode *node = m_items.First();
529 while (node)
530 {
531 wxMenuItem *item = (wxMenuItem*)node->Data();
532 if (item->GetMenuItem() == menuItem)
533 return item->GetId();
534 node = node->Next();
535 }
536
537 return -1;
538 }
539
540 wxMenuItem *wxMenu::FindItem(int id) const
541 {
542 wxNode *node = m_items.First();
543 while (node)
544 {
545 wxMenuItem *item = (wxMenuItem*)node->Data();
546 if (item->GetId() == id)
547 {
548 return item;
549 }
550 node = node->Next();
551 }
552
553 /* Not finding anything here can be correct
554 * when search the entire menu system for
555 * an entry -> no error message. */
556
557 return (wxMenuItem *) NULL;
558 }
559
560 void wxMenu::SetInvokingWindow( wxWindow *win )
561 {
562 m_invokingWindow = win;
563 }
564
565 wxWindow *wxMenu::GetInvokingWindow()
566 {
567 return m_invokingWindow;
568 }
569
570