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