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