]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/menu.cpp
Some minor fixes to satisfy Latex. Tex2rtf works ok.
[wxWidgets.git] / src / gtk1 / 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 mitem->SetHelp(helpStr);
498
499 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
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
517void wxMenu::Append( wxMenuItem *item )
518{
519 m_items.Append( item );
520
521 GtkWidget *menuItem = (GtkWidget*) NULL;
522
523 if (item->IsSeparator())
524 menuItem = gtk_menu_item_new();
525 else if (item->IsSubMenu())
526 menuItem = gtk_menu_item_new_with_label(item->GetText());
527 else
528 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText())
529 : gtk_menu_item_new_with_label(item->GetText());
530
531 if (!item->IsSeparator())
532 {
533 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
534 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
535 (gpointer*)this );
536
537 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
538 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
539 (gpointer*)this );
540
541 if (!item->IsSubMenu())
542 {
543 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
544 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
545 (gpointer*)this );
546 }
547 }
548
549 gtk_menu_append( GTK_MENU(m_menu), menuItem );
550 gtk_widget_show( menuItem );
551 item->SetMenuItem(menuItem);
552}
553
554int wxMenu::FindItem( const wxString itemString ) const
555{
556 wxString s( itemString );
557
558 int pos;
559 do
560 {
561 pos = s.First( '&' );
562 if (pos != -1) s.Remove( pos, 1 );
563 } while (pos != -1);
564
565 wxNode *node = m_items.First();
566 while (node)
567 {
568 wxMenuItem *item = (wxMenuItem*)node->Data();
569 if (item->GetText() == s)
570 {
571 return item->GetId();
572 }
573 node = node->Next();
574 }
575
576 return -1;
577}
578
579void wxMenu::Enable( int id, bool enable )
580{
581 wxMenuItem *item = FindItem(id);
582 if (item)
583 {
584 item->Enable(enable);
585 }
586}
587
588bool wxMenu::IsEnabled( int id ) const
589{
590 wxMenuItem *item = FindItem(id);
591 if (item)
592 {
593 return item->IsEnabled();
594 }
595 else
596 {
597 return FALSE;
598 }
599}
600
601void wxMenu::Check( int id, bool enable )
602{
603 wxMenuItem *item = FindItem(id);
604 if (item)
605 {
606 item->Check(enable);
607 }
608}
609
610bool wxMenu::IsChecked( int id ) const
611{
612 wxMenuItem *item = FindItem(id);
613 if (item)
614 {
615 return item->IsChecked();
616 }
617 else
618 {
619 return FALSE;
620 }
621}
622
623void wxMenu::SetLabel( int id, const wxString &label )
624{
625 wxMenuItem *item = FindItem(id);
626 if (item)
627 {
628 item->SetText(label);
629 }
630}
631
632wxString wxMenu::GetLabel( int id ) const
633{
634 wxMenuItem *item = FindItem(id);
635 if (item)
636 {
637 return item->GetText();
638 }
639 else
640 {
641 return "";
642 }
643}
644
645void wxMenu::SetHelpString( int id, const wxString& helpString )
646{
647 wxMenuItem *item = FindItem(id);
648 if (item) item->SetHelp( helpString );
649}
650
651wxString wxMenu::GetHelpString( int id ) const
652{
653 wxMenuItem *item = FindItem(id);
654 if (item)
655 {
656 return item->GetHelp();
657 }
658 else
659 {
660 return "";
661 }
662}
663
664int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
665{
666 wxNode *node = m_items.First();
667 while (node)
668 {
669 wxMenuItem *item = (wxMenuItem*)node->Data();
670 if (item->GetMenuItem() == menuItem)
671 return item->GetId();
672 node = node->Next();
673 }
674
675 return -1;
676}
677
678wxMenuItem *wxMenu::FindItem(int id) const
679{
680 wxNode *node = m_items.First();
681 while (node)
682 {
683 wxMenuItem *item = (wxMenuItem*)node->Data();
684 if (item->GetId() == id)
685 {
686 return item;
687 }
688 node = node->Next();
689 }
690
691 /* Not finding anything here can be correct
692 * when search the entire menu system for
693 * an entry -> no error message. */
694
695 return (wxMenuItem *) NULL;
696}
697
698void wxMenu::SetInvokingWindow( wxWindow *win )
699{
700 m_invokingWindow = win;
701}
702
703wxWindow *wxMenu::GetInvokingWindow()
704{
705 return m_invokingWindow;
706}
707
708// Update a menu and all submenus recursively.
709// source is the object that has the update event handlers
710// defined for it. If NULL, the menu or associated window
711// will be used.
712void wxMenu::UpdateUI(wxEvtHandler* source)
713{
714 if (!source && GetInvokingWindow())
715 source = GetInvokingWindow()->GetEventHandler();
716 if (!source)
717 source = GetEventHandler();
718 if (!source)
719 source = this;
720
721 wxNode* node = GetItems().First();
722 while (node)
723 {
724 wxMenuItem* item = (wxMenuItem*) node->Data();
725 if ( !item->IsSeparator() )
726 {
727 wxWindowID id = item->GetId();
728 wxUpdateUIEvent event(id);
729 event.SetEventObject( source );
730
731 if (source->ProcessEvent(event))
732 {
733 if (event.GetSetText())
734 SetLabel(id, event.GetText());
735 if (event.GetSetChecked())
736 Check(id, event.GetChecked());
737 if (event.GetSetEnabled())
738 Enable(id, event.GetEnabled());
739 }
740
741 if (item->GetSubMenu())
742 item->GetSubMenu()->UpdateUI(source);
743 }
744 node = node->Next();
745 }
746}
747
748