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