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