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