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