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