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