]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Dialog Editor corrections for latest wxWin changes; removed wxProp files
[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
VZ
154
155 wxASSERT( id != -1 ); // should find it!
156
6de97a3b 157 if (!menu->IsEnabled(id)) return;
96fd301f 158
cf7a7e13
RR
159 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
160 event.SetEventObject( menu );
161 event.SetInt(id );
8bbe427f 162
cf7a7e13
RR
163 if (menu->m_callback)
164 {
165 (void) (*(menu->m_callback)) (*menu, event);
166 return;
167 }
168
169 if (menu->GetEventHandler()->ProcessEvent(event)) return;
170
171 wxWindow *win = menu->GetInvokingWindow();
172 if (win) win->GetEventHandler()->ProcessEvent( event );
173}
174
175//-----------------------------------------------------------------------------
176// "select"
177//-----------------------------------------------------------------------------
178
179static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
180{
181 int id = menu->FindMenuIdByMenuItem(widget);
182
183 wxASSERT( id != -1 ); // should find it!
184
185 if (!menu->IsEnabled(id)) return;
186
187 wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
c801d85f
KB
188 event.SetEventObject( menu );
189 event.SetInt(id );
8bbe427f 190
13439807 191/* wxMSW doesn't call callback here either
6de97a3b
RR
192 if (menu->m_callback)
193 {
194 (void) (*(menu->m_callback)) (*menu, event);
195 return;
196 }
13439807 197*/
6de97a3b
RR
198
199 if (menu->GetEventHandler()->ProcessEvent(event)) return;
200
c801d85f 201 wxWindow *win = menu->GetInvokingWindow();
cf4219e7 202 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 203}
c801d85f 204
cf7a7e13 205//-----------------------------------------------------------------------------
db1b4961 206// wxMenuItem
cf7a7e13
RR
207//-----------------------------------------------------------------------------
208
c801d85f 209IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
210
211wxMenuItem::wxMenuItem()
c801d85f 212{
96fd301f 213 m_id = ID_SEPARATOR;
c801d85f 214 m_isCheckMenu = FALSE;
96fd301f
VZ
215 m_isChecked = FALSE;
216 m_isEnabled = TRUE;
c67daf87
UR
217 m_subMenu = (wxMenu *) NULL;
218 m_menuItem = (GtkWidget *) NULL;
6de97a3b 219}
c801d85f 220
8bbe427f 221// it's valid for this function to be called even if m_menuItem == NULL
db1b4961 222void wxMenuItem::SetName(const wxString& str)
716b7364 223{
30dea054 224 m_text = "";
8bbe427f 225 for ( const char *pc = str; *pc != '\0'; pc++ )
db1b4961 226 {
96fd301f
VZ
227 if ( *pc == '&' )
228 pc++; // skip it
229
230 m_text << *pc;
716b7364 231 }
8bbe427f
VZ
232
233 if ( m_menuItem )
234 {
235 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
236
237 gtk_label_set( label, m_text.c_str());
238 }
716b7364
RR
239}
240
96fd301f 241void wxMenuItem::Check( bool check )
716b7364 242{
db1b4961
RR
243 wxCHECK_RET( m_menuItem, "invalid menu item" );
244
30f82ea4 245 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
96fd301f
VZ
246
247 m_isChecked = check;
248 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
249}
250
8bbe427f
VZ
251void wxMenuItem::Enable( bool enable )
252{
db1b4961
RR
253 wxCHECK_RET( m_menuItem, "invalid menu item" );
254
a9c96bcc 255 gtk_widget_set_sensitive( m_menuItem, enable );
8bbe427f 256 m_isEnabled = enable;
a9c96bcc
RR
257}
258
96fd301f 259bool wxMenuItem::IsChecked() const
716b7364 260{
db1b4961
RR
261 wxCHECK_MSG( m_menuItem, FALSE, "invalid menu item" );
262
96fd301f
VZ
263 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
264
265 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
266
267 wxASSERT( bIsChecked == m_isChecked ); // consistency check
268
269 return bIsChecked;
716b7364
RR
270}
271
db1b4961
RR
272//-----------------------------------------------------------------------------
273// wxMenuItem
274//-----------------------------------------------------------------------------
275
c801d85f
KB
276IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
277
6de97a3b 278wxMenu::wxMenu( const wxString& title, const wxFunction func )
c801d85f
KB
279{
280 m_title = title;
281 m_items.DeleteContents( TRUE );
c67daf87 282 m_invokingWindow = (wxWindow *) NULL;
c801d85f 283 m_menu = gtk_menu_new(); // Do not show!
8bbe427f 284
6de97a3b
RR
285 m_callback = func;
286 m_eventHandler = this;
41dee9d0 287 m_clientData = (void*) NULL;
8bbe427f 288
6de97a3b
RR
289 if (m_title.IsNull()) m_title = "";
290 if (m_title != "")
291 {
292 Append(-2, m_title);
293 AppendSeparator();
294 }
295}
c801d85f 296
c2dd8380
GL
297void wxMenu::SetTitle( const wxString& title )
298{
299 // Waiting for something better.
300 m_title = title;
301}
302
303const wxString wxMenu::GetTitle() const
304{
305 return m_title;
306}
307
96fd301f 308void wxMenu::AppendSeparator()
c801d85f
KB
309{
310 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
311 mitem->SetId(ID_SEPARATOR);
312
313 GtkWidget *menuItem = gtk_menu_item_new();
314 gtk_menu_append( GTK_MENU(m_menu), menuItem );
315 gtk_widget_show( menuItem );
316 mitem->SetMenuItem(menuItem);
c801d85f 317 m_items.Append( mitem );
6de97a3b 318}
c801d85f 319
debe6624 320void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f
KB
321{
322 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
323 mitem->SetId(id);
324 mitem->SetText(item);
c33c4050 325 mitem->SetHelp(helpStr);
96fd301f
VZ
326 mitem->SetCheckable(checkable);
327 const char *text = mitem->GetText();
328 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
329 : gtk_menu_item_new_with_label(text);
8bbe427f 330
96fd301f
VZ
331 mitem->SetMenuItem(menuItem);
332
333 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
334 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
335 (gpointer*)this );
336
cf7a7e13
RR
337 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
338 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
339 (gpointer*)this );
340
96fd301f
VZ
341 gtk_menu_append( GTK_MENU(m_menu), menuItem );
342 gtk_widget_show( menuItem );
c801d85f 343 m_items.Append( mitem );
6de97a3b 344}
c801d85f 345
96fd301f 346void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
c801d85f
KB
347{
348 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
349 mitem->SetId(id);
350 mitem->SetText(text);
351
352 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
c33c4050 353 mitem->SetHelp(helpStr);
96fd301f
VZ
354 mitem->SetMenuItem(menuItem);
355 mitem->SetSubMenu(subMenu);
356
357 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
358 gtk_menu_append( GTK_MENU(m_menu), menuItem );
359 gtk_widget_show( menuItem );
c801d85f 360 m_items.Append( mitem );
6de97a3b 361}
c801d85f
KB
362
363int wxMenu::FindItem( const wxString itemString ) const
364{
365 wxString s( itemString );
96fd301f 366
d355d3fe 367 int pos;
c801d85f
KB
368 do {
369 pos = s.First( '&' );
d355d3fe
RR
370 if (pos != -1) s.Remove( pos, 1 );
371 } while (pos != -1);
96fd301f 372
c801d85f
KB
373 wxNode *node = m_items.First();
374 while (node)
375 {
376 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
377 if (item->GetText() == s)
378 return item->GetId();
c801d85f 379 node = node->Next();
6de97a3b 380 }
96fd301f 381
c801d85f 382 return -1;
6de97a3b 383}
c801d85f 384
96fd301f 385void wxMenu::Enable( int id, bool enable )
716b7364 386{
96fd301f
VZ
387 wxMenuItem *item = FindItem(id);
388 if ( item )
389 item->Enable(enable);
6de97a3b 390}
716b7364 391
96fd301f 392bool wxMenu::IsEnabled( int id ) const
e2414cbe 393{
96fd301f
VZ
394 wxMenuItem *item = FindItem(id);
395 if ( item )
396 return item->IsEnabled();
397 else
398 return FALSE;
6de97a3b 399}
e2414cbe 400
96fd301f 401void wxMenu::Check( int id, bool enable )
c801d85f 402{
96fd301f
VZ
403 wxMenuItem *item = FindItem(id);
404 if ( item )
405 item->Check(enable);
6de97a3b 406}
c801d85f 407
96fd301f 408bool wxMenu::IsChecked( int id ) const
c801d85f 409{
96fd301f
VZ
410 wxMenuItem *item = FindItem(id);
411 if ( item )
412 return item->IsChecked();
413 else
414 return FALSE;
6de97a3b 415}
c801d85f 416
debe6624 417void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 418{
96fd301f 419 wxMenuItem *item = FindItem(id);
c33c4050 420 if (item)
96fd301f 421 item->SetText(label);
6de97a3b 422}
96fd301f 423
c33c4050
RR
424wxString wxMenu::GetLabel( int id ) const
425{
426 wxMenuItem *item = FindItem(id);
427 if (item) return item->GetText();
428 return "";
429}
430
431void wxMenu::SetHelpString( int id, const wxString& helpString )
432{
433 wxMenuItem *item = FindItem(id);
434 if (item) item->SetHelp( helpString );
435}
436
437wxString wxMenu::GetHelpString( int id ) const
438{
439 wxMenuItem *item = FindItem(id);
440 if (item) return item->GetHelp();
441 return "";
442}
443
96fd301f
VZ
444int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
445{
c801d85f
KB
446 wxNode *node = m_items.First();
447 while (node)
448 {
449 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
450 if (item->GetMenuItem() == menuItem)
451 return item->GetId();
c801d85f 452 node = node->Next();
6de97a3b 453 }
96fd301f
VZ
454
455 return -1;
6de97a3b 456}
c801d85f 457
96fd301f 458wxMenuItem *wxMenu::FindItem(int id) const
c801d85f
KB
459{
460 wxNode *node = m_items.First();
96fd301f 461 while (node) {
c801d85f 462 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
463 if ( item->GetId() == id )
464 return item;
c801d85f 465 node = node->Next();
6de97a3b 466 }
96fd301f 467
dc86cb34
RR
468 // Not finding anything here can be correct
469 // when search the entire menu system for
470 // an entry -> no error message.
8bbe427f 471
c67daf87 472 return (wxMenuItem *) NULL;
96fd301f 473}
c801d85f
KB
474
475void wxMenu::SetInvokingWindow( wxWindow *win )
476{
477 m_invokingWindow = win;
6de97a3b 478}
c801d85f 479
96fd301f 480wxWindow *wxMenu::GetInvokingWindow()
c801d85f
KB
481{
482 return m_invokingWindow;
6de97a3b 483}
c801d85f
KB
484
485