]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Fixed event handling for DialogEd
[wxWidgets.git] / src / gtk / menu.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
96fd301f 6// Id: $Id$
c801d85f 7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
96fd301f 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "menu.h"
14#endif
15
16#include "wx/menu.h"
96fd301f 17#include "wx/log.h"
30dea054 18#include "wx/intl.h"
c801d85f
KB
19
20//-----------------------------------------------------------------------------
21// wxMenuBar
22//-----------------------------------------------------------------------------
23
24IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
25
96fd301f 26wxMenuBar::wxMenuBar()
c801d85f
KB
27{
28 m_needParent = FALSE; // hmmm
96fd301f 29
c67daf87 30 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
c801d85f
KB
31
32 m_menus.DeleteContents( TRUE );
96fd301f 33
c801d85f 34 m_widget = gtk_handle_box_new();
96fd301f 35
c801d85f 36 m_menubar = gtk_menu_bar_new();
96fd301f 37
c801d85f 38 gtk_container_add( GTK_CONTAINER(m_widget), m_menubar );
96fd301f 39
c801d85f 40 gtk_widget_show( m_menubar );
96fd301f 41
c801d85f
KB
42 PostCreation();
43
44 Show( TRUE );
6de97a3b 45}
c801d85f
KB
46
47void wxMenuBar::Append( wxMenu *menu, const wxString &title )
48{
49 m_menus.Append( menu );
50 menu->m_title = title; // ??????
96fd301f 51
d355d3fe 52 int pos;
c801d85f
KB
53 do {
54 pos = menu->m_title.First( '&' );
d355d3fe
RR
55 if (pos != -1) menu->m_title.Remove( pos, 1 );
56 } while (pos != -1);
96fd301f 57
c801d85f
KB
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 );
96fd301f 62
c801d85f 63 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
6de97a3b 64}
96fd301f 65
716b7364 66static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f
KB
67{
68 if (menu->m_title == menuString)
69 {
70 int res = menu->FindItem( itemString );
71 if (res != -1) return res;
6de97a3b 72 }
c801d85f
KB
73 wxNode *node = menu->m_items.First();
74 while (node)
75 {
76 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
77 if (item->IsSubMenu())
78 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
c801d85f 79 node = node->Next();
6de97a3b 80 }
c801d85f 81 return -1;
6de97a3b 82}
c801d85f
KB
83
84int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
85{
86 wxNode *node = m_menus.First();
87 while (node)
88 {
89 wxMenu *menu = (wxMenu*)node->Data();
90 int res = FindMenuItemRecursive( menu, menuString, itemString);
91 if (res != -1) return res;
92 node = node->Next();
6de97a3b 93 }
c801d85f 94 return -1;
6de97a3b 95}
c801d85f 96
716b7364 97// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 98static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 99{
96fd301f 100 wxMenuItem* result = menu->FindItem(id);
716b7364
RR
101
102 wxNode *node = menu->m_items.First();
96fd301f 103 while ( node && result == NULL ) {
716b7364 104 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
105 if ( item->IsSubMenu() )
106 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
716b7364 107 node = node->Next();
6de97a3b 108 }
96fd301f 109
716b7364 110 return result;
6de97a3b 111}
716b7364
RR
112
113wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const
114{
115 wxMenuItem* result = 0;
116 wxNode *node = m_menus.First();
117 while (node && result == 0)
118 {
119 wxMenu *menu = (wxMenu*)node->Data();
120 result = FindMenuItemByIdRecursive( menu, id );
121 node = node->Next();
122 }
123 return result;
124}
125
54ff4a70
RR
126void wxMenuBar::Check( int id, bool check )
127{
128 wxMenuItem* item = FindMenuItemById( id );
129 if (item) item->Check(check);
6de97a3b 130}
54ff4a70
RR
131
132bool wxMenuBar::Checked( int id ) const
716b7364
RR
133{
134 wxMenuItem* item = FindMenuItemById( id );
135 if (item) return item->IsChecked();
136 return FALSE;
6de97a3b 137}
716b7364 138
54ff4a70
RR
139void wxMenuBar::Enable( int id, bool enable )
140{
141 wxMenuItem* item = FindMenuItemById( id );
142 if (item) item->Enable(enable);
6de97a3b 143}
54ff4a70
RR
144
145bool wxMenuBar::Enabled( int id ) const
716b7364
RR
146{
147 wxMenuItem* item = FindMenuItemById( id );
148 if (item) return item->IsEnabled();
149 return FALSE;
6de97a3b 150}
716b7364 151
c801d85f
KB
152//-----------------------------------------------------------------------------
153// wxMenu
154//-----------------------------------------------------------------------------
155
6de97a3b 156static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 157{
c801d85f 158 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f
VZ
159
160 wxASSERT( id != -1 ); // should find it!
161
6de97a3b 162 if (!menu->IsEnabled(id)) return;
96fd301f 163
c801d85f
KB
164 wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id );
165 event.SetEventObject( menu );
166 event.SetInt(id );
6de97a3b
RR
167
168 if (menu->m_callback)
169 {
170 (void) (*(menu->m_callback)) (*menu, event);
171 return;
172 }
173
174 if (menu->GetEventHandler()->ProcessEvent(event)) return;
175
c801d85f 176 wxWindow *win = menu->GetInvokingWindow();
cf4219e7 177 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 178}
c801d85f
KB
179
180IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
181
182wxMenuItem::wxMenuItem()
c801d85f 183{
96fd301f 184 m_id = ID_SEPARATOR;
c801d85f 185 m_isCheckMenu = FALSE;
96fd301f
VZ
186 m_isChecked = FALSE;
187 m_isEnabled = TRUE;
c67daf87
UR
188 m_subMenu = (wxMenu *) NULL;
189 m_menuItem = (GtkWidget *) NULL;
6de97a3b 190}
c801d85f 191
96fd301f 192void wxMenuItem::SetText(const wxString& str)
716b7364 193{
30dea054 194 m_text = "";
96fd301f
VZ
195 for ( const char *pc = str; *pc != '\0'; pc++ ) {
196 if ( *pc == '&' )
197 pc++; // skip it
198
199 m_text << *pc;
716b7364
RR
200 }
201}
202
96fd301f 203void wxMenuItem::Check( bool check )
716b7364 204{
30f82ea4 205 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
96fd301f
VZ
206
207 m_isChecked = check;
208 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
209}
210
a9c96bcc
RR
211void wxMenuItem::Enable( bool enable )
212{
213 gtk_widget_set_sensitive( m_menuItem, enable );
214 m_isEnabled = enable;
215}
216
96fd301f 217bool wxMenuItem::IsChecked() const
716b7364 218{
96fd301f
VZ
219 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
220
221 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
222
223 wxASSERT( bIsChecked == m_isChecked ); // consistency check
224
225 return bIsChecked;
716b7364
RR
226}
227
c801d85f
KB
228IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
229
6de97a3b 230wxMenu::wxMenu( const wxString& title, const wxFunction func )
c801d85f
KB
231{
232 m_title = title;
233 m_items.DeleteContents( TRUE );
c67daf87 234 m_invokingWindow = (wxWindow *) NULL;
c801d85f 235 m_menu = gtk_menu_new(); // Do not show!
6de97a3b
RR
236 m_callback = func;
237 m_eventHandler = this;
238 if (m_title.IsNull()) m_title = "";
239 if (m_title != "")
240 {
241 Append(-2, m_title);
242 AppendSeparator();
243 }
244}
c801d85f 245
c2dd8380
GL
246void wxMenu::SetTitle( const wxString& title )
247{
248 // Waiting for something better.
249 m_title = title;
250}
251
252const wxString wxMenu::GetTitle() const
253{
254 return m_title;
255}
256
96fd301f 257void wxMenu::AppendSeparator()
c801d85f
KB
258{
259 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
260 mitem->SetId(ID_SEPARATOR);
261
262 GtkWidget *menuItem = gtk_menu_item_new();
263 gtk_menu_append( GTK_MENU(m_menu), menuItem );
264 gtk_widget_show( menuItem );
265 mitem->SetMenuItem(menuItem);
c801d85f 266 m_items.Append( mitem );
6de97a3b 267}
c801d85f 268
debe6624 269void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f
KB
270{
271 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
272 mitem->SetId(id);
273 mitem->SetText(item);
c33c4050 274 mitem->SetHelp(helpStr);
96fd301f
VZ
275 mitem->SetCheckable(checkable);
276 const char *text = mitem->GetText();
277 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
278 : gtk_menu_item_new_with_label(text);
a9c96bcc 279
96fd301f
VZ
280 mitem->SetMenuItem(menuItem);
281
282 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
283 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
284 (gpointer*)this );
285
286 gtk_menu_append( GTK_MENU(m_menu), menuItem );
287 gtk_widget_show( menuItem );
c801d85f 288 m_items.Append( mitem );
6de97a3b 289}
c801d85f 290
96fd301f 291void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
c801d85f
KB
292{
293 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
294 mitem->SetId(id);
295 mitem->SetText(text);
296
297 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
c33c4050 298 mitem->SetHelp(helpStr);
96fd301f
VZ
299 mitem->SetMenuItem(menuItem);
300 mitem->SetSubMenu(subMenu);
301
302 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
303 gtk_menu_append( GTK_MENU(m_menu), menuItem );
304 gtk_widget_show( menuItem );
c801d85f 305 m_items.Append( mitem );
6de97a3b 306}
c801d85f
KB
307
308int wxMenu::FindItem( const wxString itemString ) const
309{
310 wxString s( itemString );
96fd301f 311
d355d3fe 312 int pos;
c801d85f
KB
313 do {
314 pos = s.First( '&' );
d355d3fe
RR
315 if (pos != -1) s.Remove( pos, 1 );
316 } while (pos != -1);
96fd301f 317
c801d85f
KB
318 wxNode *node = m_items.First();
319 while (node)
320 {
321 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
322 if (item->GetText() == s)
323 return item->GetId();
c801d85f 324 node = node->Next();
6de97a3b 325 }
96fd301f 326
c801d85f 327 return -1;
6de97a3b 328}
c801d85f 329
96fd301f 330void wxMenu::Enable( int id, bool enable )
716b7364 331{
96fd301f
VZ
332 wxMenuItem *item = FindItem(id);
333 if ( item )
334 item->Enable(enable);
6de97a3b 335}
716b7364 336
96fd301f 337bool wxMenu::IsEnabled( int id ) const
e2414cbe 338{
96fd301f
VZ
339 wxMenuItem *item = FindItem(id);
340 if ( item )
341 return item->IsEnabled();
342 else
343 return FALSE;
6de97a3b 344}
e2414cbe 345
96fd301f 346void wxMenu::Check( int id, bool enable )
c801d85f 347{
96fd301f
VZ
348 wxMenuItem *item = FindItem(id);
349 if ( item )
350 item->Check(enable);
6de97a3b 351}
c801d85f 352
96fd301f 353bool wxMenu::IsChecked( int id ) const
c801d85f 354{
96fd301f
VZ
355 wxMenuItem *item = FindItem(id);
356 if ( item )
357 return item->IsChecked();
358 else
359 return FALSE;
6de97a3b 360}
c801d85f 361
debe6624 362void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 363{
96fd301f 364 wxMenuItem *item = FindItem(id);
c33c4050 365 if (item)
96fd301f 366 item->SetText(label);
6de97a3b 367}
96fd301f 368
c33c4050
RR
369wxString wxMenu::GetLabel( int id ) const
370{
371 wxMenuItem *item = FindItem(id);
372 if (item) return item->GetText();
373 return "";
374}
375
376void wxMenu::SetHelpString( int id, const wxString& helpString )
377{
378 wxMenuItem *item = FindItem(id);
379 if (item) item->SetHelp( helpString );
380}
381
382wxString wxMenu::GetHelpString( int id ) const
383{
384 wxMenuItem *item = FindItem(id);
385 if (item) return item->GetHelp();
386 return "";
387}
388
96fd301f
VZ
389int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
390{
c801d85f
KB
391 wxNode *node = m_items.First();
392 while (node)
393 {
394 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
395 if (item->GetMenuItem() == menuItem)
396 return item->GetId();
c801d85f 397 node = node->Next();
6de97a3b 398 }
96fd301f
VZ
399
400 return -1;
6de97a3b 401}
c801d85f 402
96fd301f 403wxMenuItem *wxMenu::FindItem(int id) const
c801d85f
KB
404{
405 wxNode *node = m_items.First();
96fd301f 406 while (node) {
c801d85f 407 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
408 if ( item->GetId() == id )
409 return item;
c801d85f 410 node = node->Next();
6de97a3b 411 }
96fd301f 412
30f82ea4 413 wxLogDebug( "wxMenu::FindItem: item %d not found.", id);
96fd301f 414
c67daf87 415 return (wxMenuItem *) NULL;
96fd301f 416}
c801d85f
KB
417
418void wxMenu::SetInvokingWindow( wxWindow *win )
419{
420 m_invokingWindow = win;
6de97a3b 421}
c801d85f 422
96fd301f 423wxWindow *wxMenu::GetInvokingWindow()
c801d85f
KB
424{
425 return m_invokingWindow;
6de97a3b 426}
c801d85f
KB
427
428