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