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