]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
use a smaller tip font for MSW
[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
96fd301f 15#include "wx/log.h"
30dea054 16#include "wx/intl.h"
06cfab17 17#include "wx/app.h"
3dfac970 18#include "wx/menu.h"
c801d85f 19
974e8d94
VZ
20#if wxUSE_ACCEL
21 #include "wx/accel.h"
22#endif // wxUSE_ACCEL
23
83624f79
RR
24#include "gdk/gdk.h"
25#include "gtk/gtk.h"
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
717a57c2
VZ
34#if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
35static wxString GetHotKey( const wxMenuItem& item );
36#endif
37
c801d85f
KB
38//-----------------------------------------------------------------------------
39// wxMenuBar
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
43
3502e687
RR
44wxMenuBar::wxMenuBar( long style )
45{
1e133b7d 46 /* the parent window is known after wxFrame::SetMenu() */
23280650 47 m_needParent = FALSE;
ae53c98c 48 m_style = style;
9c884972 49 m_invokingWindow = (wxWindow*) NULL;
23280650 50
4dcaf11a 51 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 52 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 53 {
223d09f6 54 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 55 return;
4dcaf11a 56 }
3502e687
RR
57
58 m_menus.DeleteContents( TRUE );
59
1e133b7d
RR
60 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
61#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
62 m_accel = gtk_accel_group_new();
63 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
64 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 65#else
3502e687 66 m_menubar = gtk_menu_bar_new();
1e133b7d 67#endif
3502e687
RR
68
69 if (style & wxMB_DOCKABLE)
70 {
71 m_widget = gtk_handle_box_new();
c626a8b7
VZ
72 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
73 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
74 }
75 else
76 {
77 m_widget = GTK_WIDGET(m_menubar);
78 }
79
80 PostCreation();
3502e687
RR
81}
82
96fd301f 83wxMenuBar::wxMenuBar()
c801d85f 84{
1e133b7d
RR
85 /* the parent window is known after wxFrame::SetMenu() */
86 m_needParent = FALSE;
ae53c98c 87 m_style = 0;
9c884972 88 m_invokingWindow = (wxWindow*) NULL;
23280650 89
4dcaf11a 90 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 91 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 92 {
223d09f6 93 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 94 return;
4dcaf11a 95 }
974e8d94 96
83624f79 97 m_menus.DeleteContents( TRUE );
96fd301f 98
1e133b7d
RR
99 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
100#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
101 m_accel = gtk_accel_group_new();
102 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
103 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 104#else
83624f79 105 m_menubar = gtk_menu_bar_new();
1e133b7d 106#endif
8bbe427f 107
828f655f 108 m_widget = GTK_WIDGET(m_menubar);
96fd301f 109
83624f79 110 PostCreation();
6de97a3b 111}
c801d85f 112
1e133b7d
RR
113wxMenuBar::~wxMenuBar()
114{
d1b15f03 115// gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
1e133b7d
RR
116}
117
5bd9e519
RR
118static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
119{
120 menu->SetInvokingWindow( (wxWindow*) NULL );
121
122#if (GTK_MINOR_VERSION > 0)
123 wxWindow *top_frame = win;
8487f887 124 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 125 top_frame = top_frame->GetParent();
5bd9e519
RR
126
127 /* support for native hot keys */
128 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
129#endif
130
1987af7e 131 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
132 while (node)
133 {
1987af7e 134 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
135 if (menuitem->IsSubMenu())
136 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 137 node = node->GetNext();
5bd9e519
RR
138 }
139}
140
141static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
142{
143 menu->SetInvokingWindow( win );
144
145#if (GTK_MINOR_VERSION > 0)
146 wxWindow *top_frame = win;
8487f887 147 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 148 top_frame = top_frame->GetParent();
5bd9e519
RR
149
150 /* support for native hot keys */
151 gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
152#endif
153
1987af7e 154 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
155 while (node)
156 {
1987af7e 157 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
158 if (menuitem->IsSubMenu())
159 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 160 node = node->GetNext();
5bd9e519
RR
161 }
162}
163
164void wxMenuBar::SetInvokingWindow( wxWindow *win )
165{
9c884972 166 m_invokingWindow = win;
5bd9e519
RR
167#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
168 wxWindow *top_frame = win;
8487f887 169 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 170 top_frame = top_frame->GetParent();
5bd9e519
RR
171
172 /* support for native key accelerators indicated by underscroes */
173 gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
174#endif
175
1987af7e 176 wxMenuList::Node *node = m_menus.GetFirst();
5bd9e519
RR
177 while (node)
178 {
1987af7e 179 wxMenu *menu = node->GetData();
5bd9e519 180 wxMenubarSetInvokingWindow( menu, win );
1987af7e 181 node = node->GetNext();
5bd9e519
RR
182 }
183}
184
185void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
186{
9c884972 187 m_invokingWindow = (wxWindow*) NULL;
5bd9e519
RR
188#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
189 wxWindow *top_frame = win;
8487f887 190 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 191 top_frame = top_frame->GetParent();
5bd9e519
RR
192
193 /* support for native key accelerators indicated by underscroes */
194 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
195#endif
196
1987af7e 197 wxMenuList::Node *node = m_menus.GetFirst();
5bd9e519
RR
198 while (node)
199 {
1987af7e 200 wxMenu *menu = node->GetData();
5bd9e519 201 wxMenubarUnsetInvokingWindow( menu, win );
1987af7e 202 node = node->GetNext();
5bd9e519
RR
203 }
204}
205
3dfac970 206bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
c801d85f 207{
83624f79 208 m_menus.Append( menu );
23280650 209
a533f5c1 210 const wxChar *pc;
23280650 211
1e133b7d
RR
212 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
213 wxString str;
223d09f6 214 for ( pc = title; *pc != wxT('\0'); pc++ )
83624f79 215 {
223d09f6 216 if (*pc == wxT('&'))
23280650 217 {
1e133b7d 218#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
223d09f6 219 str << wxT('_');
455fadaa 220 }
223d09f6 221 else if (*pc == wxT('/'))
23280650 222 {
223d09f6 223 str << wxT('\\');
034be888 224#endif
23280650 225 }
d38ceae8 226 else
455fadaa 227 {
90a53a3a 228#if __WXGTK12__
223d09f6 229 if ( *pc == wxT('_') )
455fadaa
VZ
230 {
231 // underscores must be doubled to prevent them from being
232 // interpreted as accelerator character prefix by GTK
233 str << *pc;
234 }
90a53a3a 235#endif // GTK+ 1.2
455fadaa
VZ
236
237 str << *pc;
238 }
1e133b7d
RR
239 }
240
241 /* this doesn't have much effect right now */
242 menu->SetTitle( str );
23280650 243
1e133b7d
RR
244 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
245#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
246
247 /* local buffer in multibyte form */
2b2edbed 248 wxString buf;
223d09f6 249 buf << wxT('/') << str.c_str();
c980c992 250
dc6c62a9 251 char *cbuf = new char[buf.Length()+1];
c980c992
GL
252 strcpy(cbuf, buf.mbc_str());
253
1e133b7d 254 GtkItemFactoryEntry entry;
c980c992 255 entry.path = (gchar *)cbuf; // const_cast
1e133b7d
RR
256 entry.accelerator = (gchar*) NULL;
257 entry.callback = (GtkItemFactoryCallback) NULL;
258 entry.callback_action = 0;
2b2edbed
KB
259 entry.item_type = "<Branch>";
260
1e133b7d 261 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
1e133b7d 262 /* in order to get the pointer to the item we need the item text _without_ underscores */
223d09f6
KB
263 wxString tmp = wxT("<main>/");
264 for ( pc = str; *pc != wxT('\0'); pc++ )
1e133b7d 265 {
455fadaa
VZ
266 // contrary to the common sense, we must throw out _all_ underscores,
267 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
974e8d94 268 // might naively think). IMHO it's a bug in GTK+ (VZ)
223d09f6 269 while (*pc == wxT('_'))
455fadaa 270 pc++;
2b2edbed 271 tmp << *pc;
034be888 272 }
1e133b7d 273 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
1e133b7d 274 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
2b2edbed 275 delete [] cbuf;
1e133b7d 276#else
96fd301f 277
1e133b7d 278 menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() );
2b1c162e
RR
279 gtk_widget_show( menu->m_owner );
280 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
96fd301f 281
2b1c162e 282 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
23280650 283
1e133b7d 284#endif
9c884972
RR
285
286 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
287 // adding menu later on.
288 if (m_invokingWindow)
289 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
3dfac970
VZ
290
291 return TRUE;
292}
293
294bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
295{
296 if ( !wxMenuBarBase::Insert(pos, menu, title) )
297 return FALSE;
298
299 wxFAIL_MSG(wxT("TODO"));
300
301 return FALSE;
302}
303
304wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
305{
306 if ( !wxMenuBarBase::Replace(pos, menu, title) )
307 return FALSE;
308
309 wxFAIL_MSG(wxT("TODO"));
310
311 return NULL;
312}
313
314wxMenu *wxMenuBar::Remove(size_t pos)
315{
316 if ( !wxMenuBarBase::Remove(pos) )
317 return FALSE;
318
319 wxFAIL_MSG(wxT("TODO"));
320
321 return NULL;
6de97a3b 322}
96fd301f 323
716b7364 324static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 325{
c626a8b7 326 if (menu->GetTitle() == menuString)
83624f79
RR
327 {
328 int res = menu->FindItem( itemString );
c626a8b7
VZ
329 if (res != wxNOT_FOUND)
330 return res;
83624f79 331 }
c626a8b7 332
1987af7e 333 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
83624f79
RR
334 while (node)
335 {
1987af7e 336 wxMenuItem *item = node->GetData();
83624f79
RR
337 if (item->IsSubMenu())
338 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 339
1987af7e 340 node = node->GetNext();
83624f79
RR
341 }
342
c626a8b7
VZ
343 return wxNOT_FOUND;
344}
345
c801d85f
KB
346int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
347{
1987af7e 348 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
349 while (node)
350 {
1987af7e 351 wxMenu *menu = node->GetData();
83624f79 352 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
353 if (res != -1)
354 return res;
355 node = node->GetNext();
83624f79 356 }
1987af7e
VZ
357
358 return wxNOT_FOUND;
6de97a3b 359}
c801d85f 360
c626a8b7 361// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 362static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 363{
717a57c2 364 wxMenuItem* result = menu->FindChildItem(id);
716b7364 365
1987af7e 366 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
c626a8b7 367 while ( node && result == NULL )
83624f79 368 {
1987af7e 369 wxMenuItem *item = node->GetData();
83624f79 370 if (item->IsSubMenu())
c626a8b7 371 {
83624f79 372 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 373 }
1987af7e 374 node = node->GetNext();
83624f79 375 }
96fd301f 376
83624f79 377 return result;
6de97a3b 378}
716b7364 379
3dfac970 380wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 381{
83624f79 382 wxMenuItem* result = 0;
1987af7e 383 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
384 while (node && result == 0)
385 {
1987af7e 386 wxMenu *menu = node->GetData();
83624f79 387 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 388 node = node->GetNext();
83624f79 389 }
c626a8b7 390
3dfac970
VZ
391 if ( menuForItem )
392 {
393 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
394 }
c626a8b7 395
3dfac970 396 return result;
bbe0af5b
RR
397}
398
3dfac970 399void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 400{
3dfac970 401 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 402
223d09f6 403 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 404
3dfac970 405 wxMenu* menu = node->GetData();
c626a8b7
VZ
406
407 if (menu->m_owner)
408 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
409}
410
3dfac970 411wxString wxMenuBar::GetLabelTop( size_t pos ) const
bbe0af5b 412{
3dfac970 413 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 414
223d09f6 415 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 416
3dfac970 417 wxMenu* menu = node->GetData();
c626a8b7 418
2b1c162e 419 return menu->GetTitle();
bbe0af5b
RR
420}
421
3dfac970 422void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
bbe0af5b 423{
3dfac970 424 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 425
223d09f6 426 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 427
3dfac970 428 wxMenu* menu = node->GetData();
c626a8b7 429
2b1c162e 430 menu->SetTitle( label );
bbe0af5b
RR
431}
432
c801d85f 433//-----------------------------------------------------------------------------
cf7a7e13 434// "activate"
c801d85f
KB
435//-----------------------------------------------------------------------------
436
6de97a3b 437static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 438{
acfd422a
RR
439 if (g_isIdle) wxapp_install_idle_handler();
440
83624f79 441 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 442
83624f79 443 /* should find it for normal (not popup) menu */
c626a8b7 444 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
96fd301f 445
c626a8b7
VZ
446 if (!menu->IsEnabled(id))
447 return;
96fd301f 448
717a57c2 449 wxMenuItem* item = menu->FindChildItem( id );
223d09f6 450 wxCHECK_RET( item, wxT("error in menu item callback") );
c626a8b7
VZ
451
452 if (item->IsCheckable())
2d17d68f 453 {
974e8d94
VZ
454 bool isReallyChecked = item->IsChecked();
455 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
2d17d68f 456 {
c626a8b7 457 /* the menu item has been checked by calling wxMenuItem->Check() */
2d17d68f 458 return;
c626a8b7
VZ
459 }
460 else
461 {
974e8d94
VZ
462 /* the user pressed on the menu item -> report and make consistent
463 * again */
464 item->wxMenuItemBase::Check(isReallyChecked);
c626a8b7 465 }
2d17d68f
RR
466 }
467
83624f79
RR
468 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
469 event.SetEventObject( menu );
470 event.SetInt(id );
8bbe427f 471
9739d9ee 472#if wxUSE_MENU_CALLBACK
c626a8b7 473 if (menu->GetCallback())
83624f79 474 {
c626a8b7 475 (void) (*(menu->GetCallback())) (*menu, event);
83624f79
RR
476 return;
477 }
9739d9ee 478#endif // wxUSE_MENU_CALLBACK
cf7a7e13 479
c626a8b7
VZ
480 if (menu->GetEventHandler()->ProcessEvent(event))
481 return;
cf7a7e13 482
83624f79 483 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
484 if (win)
485 win->GetEventHandler()->ProcessEvent( event );
cf7a7e13
RR
486}
487
488//-----------------------------------------------------------------------------
489// "select"
490//-----------------------------------------------------------------------------
491
492static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
493{
acfd422a
RR
494 if (g_isIdle) wxapp_install_idle_handler();
495
83624f79
RR
496 int id = menu->FindMenuIdByMenuItem(widget);
497
498 wxASSERT( id != -1 ); // should find it!
cf7a7e13 499
c626a8b7
VZ
500 if (!menu->IsEnabled(id))
501 return;
cf7a7e13 502
342b6a2f 503 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 504 event.SetEventObject( menu );
cf7a7e13 505
c626a8b7
VZ
506 if (menu->GetEventHandler()->ProcessEvent(event))
507 return;
6de97a3b 508
83624f79
RR
509 wxWindow *win = menu->GetInvokingWindow();
510 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 511}
c801d85f 512
cd743a6f
RR
513//-----------------------------------------------------------------------------
514// "deselect"
515//-----------------------------------------------------------------------------
516
517static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
518{
acfd422a
RR
519 if (g_isIdle) wxapp_install_idle_handler();
520
cd743a6f
RR
521 int id = menu->FindMenuIdByMenuItem(widget);
522
523 wxASSERT( id != -1 ); // should find it!
524
c626a8b7
VZ
525 if (!menu->IsEnabled(id))
526 return;
cd743a6f
RR
527
528 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
529 event.SetEventObject( menu );
530
c626a8b7
VZ
531 if (menu->GetEventHandler()->ProcessEvent(event))
532 return;
cd743a6f
RR
533
534 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
535 if (win)
536 win->GetEventHandler()->ProcessEvent( event );
cd743a6f
RR
537}
538
cf7a7e13 539//-----------------------------------------------------------------------------
db1b4961 540// wxMenuItem
cf7a7e13
RR
541//-----------------------------------------------------------------------------
542
974e8d94
VZ
543IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
544
545wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
546 int id,
547 const wxString& name,
548 const wxString& help,
549 bool isCheckable,
550 wxMenu *subMenu)
551{
552 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
553}
96fd301f 554
974e8d94
VZ
555wxMenuItem::wxMenuItem(wxMenu *parentMenu,
556 int id,
557 const wxString& text,
558 const wxString& help,
559 bool isCheckable,
560 wxMenu *subMenu)
c801d85f 561{
974e8d94
VZ
562 m_id = id;
563 m_isCheckable = isCheckable;
83624f79
RR
564 m_isChecked = FALSE;
565 m_isEnabled = TRUE;
974e8d94
VZ
566 m_subMenu = subMenu;
567 m_parentMenu = parentMenu;
568 m_help = help;
569
83624f79 570 m_menuItem = (GtkWidget *) NULL;
974e8d94 571
974e8d94 572 DoSetText(text);
6de97a3b 573}
c801d85f 574
d1b15f03
RR
575wxMenuItem::~wxMenuItem()
576{
577 // don't delete menu items, the menus take care of that
578}
579
717a57c2
VZ
580// return the menu item text without any menu accels
581wxString wxMenuItem::GetLabel() const
582{
583 wxString label;
584#if (GTK_MINOR_VERSION > 0)
585 for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
586 {
587 if ( *pc == wxT('_') )
588 {
589 // this is the escape character for GTK+ - skip it
590 continue;
591 }
592
593 label += *pc;
594 }
595#else // GTK+ 1.0
596 label = m_text;
597#endif // GTK+ 1.2/1.0
598
599 return label;
600}
601
602void wxMenuItem::SetText( const wxString& str )
603{
604 DoSetText(str);
354aa1e3
RR
605
606 if (m_menuItem)
607 {
608 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2
VZ
609
610 /* set new text */
354aa1e3 611 gtk_label_set( label, m_text.mb_str());
717a57c2
VZ
612
613 /* reparse key accel */
1987af7e 614 (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
354aa1e3
RR
615 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
616 }
617}
618
c626a8b7 619// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 620void wxMenuItem::DoSetText( const wxString& str )
716b7364 621{
ab46dc18 622 /* '\t' is the deliminator indicating a hot key */
974e8d94 623 m_text.Empty();
ab46dc18 624 const wxChar *pc = str;
223d09f6 625 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
83624f79 626 {
223d09f6 627 if (*pc == wxT('&'))
23280650 628 {
034be888 629#if (GTK_MINOR_VERSION > 0)
223d09f6 630 m_text << wxT('_');
572d7461 631 }
223d09f6 632 else if ( *pc == wxT('_') ) // escape underscores
572d7461 633 {
223d09f6 634 m_text << wxT("__");
572d7461 635 }
223d09f6 636 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
23280650 637 {
223d09f6 638 m_text << wxT('\\'); /* ... and replace them with back slashes */
034be888
RR
639#endif
640 }
d38ceae8 641 else
354aa1e3 642 m_text << *pc;
83624f79 643 }
23280650 644
837904f2 645 /* only GTK 1.2 knows about hot keys */
223d09f6 646 m_hotKey = wxT("");
ab46dc18 647#if (GTK_MINOR_VERSION > 0)
223d09f6 648 if(*pc == wxT('\t'))
d7dbc98a
KB
649 {
650 pc++;
651 m_hotKey = pc;
652 }
ab46dc18 653#endif
716b7364
RR
654}
655
717a57c2
VZ
656#if wxUSE_ACCEL
657
658wxAcceleratorEntry *wxMenuItem::GetAccel() const
659{
1987af7e 660 if ( !GetHotKey() )
717a57c2
VZ
661 {
662 // nothing
663 return (wxAcceleratorEntry *)NULL;
664 }
665
666 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
667 wxString label;
1987af7e 668 label << wxT('\t') << GetHotKey();
717a57c2
VZ
669
670 return wxGetAccelFromString(label);
671}
672
673#endif // wxUSE_ACCEL
674
96fd301f 675void wxMenuItem::Check( bool check )
716b7364 676{
223d09f6 677 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 678
223d09f6 679 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
96fd301f 680
974e8d94
VZ
681 if (check == m_isChecked)
682 return;
2d17d68f 683
974e8d94 684 wxMenuItemBase::Check( check );
83624f79 685 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
686}
687
8bbe427f
VZ
688void wxMenuItem::Enable( bool enable )
689{
223d09f6 690 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 691
83624f79 692 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 693 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
694}
695
96fd301f 696bool wxMenuItem::IsChecked() const
716b7364 697{
223d09f6 698 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
db1b4961 699
974e8d94
VZ
700 wxCHECK_MSG( IsCheckable(), FALSE,
701 wxT("can't get state of uncheckable item!") );
96fd301f 702
974e8d94 703 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
704}
705
354aa1e3
RR
706wxString wxMenuItem::GetFactoryPath() const
707{
708 /* in order to get the pointer to the item we need the item text _without_ underscores */
709 wxString path( wxT("<main>/") );
717a57c2
VZ
710 path += GetLabel();
711
354aa1e3
RR
712 return path;
713}
714
db1b4961 715//-----------------------------------------------------------------------------
83624f79 716// wxMenu
db1b4961
RR
717//-----------------------------------------------------------------------------
718
c801d85f
KB
719IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
720
717a57c2 721void wxMenu::Init()
c801d85f 722{
034be888
RR
723#if (GTK_MINOR_VERSION > 0)
724 m_accel = gtk_accel_group_new();
725 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
726 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 727#else
83624f79 728 m_menu = gtk_menu_new(); // Do not show!
034be888 729#endif
8bbe427f 730
2b1c162e 731 m_owner = (GtkWidget*) NULL;
2b2edbed
KB
732
733#if (GTK_MINOR_VERSION > 0)
734 /* Tearoffs are entries, just like separators. So if we want this
735 menu to be a tear-off one, we just append a tearoff entry
736 immediately. */
737 if(m_style & wxMENU_TEAROFF)
738 {
739 GtkItemFactoryEntry entry;
740 entry.path = "/tearoff";
741 entry.callback = (GtkItemFactoryCallback) NULL;
742 entry.callback_action = 0;
743 entry.item_type = "<Tearoff>";
744 entry.accelerator = (gchar*) NULL;
745 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 746 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed
KB
747 }
748#endif
c801d85f 749
717a57c2
VZ
750 // append the title as the very first entry if we have it
751 if ( !!m_title )
d1b15f03 752 {
717a57c2
VZ
753 Append(-2, m_title);
754 AppendSeparator();
d1b15f03 755 }
717a57c2 756}
15a2076a 757
717a57c2
VZ
758wxMenu::~wxMenu()
759{
a583e623
RR
760 m_items.Clear();
761
d1b15f03 762 gtk_widget_destroy( m_menu );
974e8d94 763
d1b15f03 764 gtk_object_unref( GTK_OBJECT(m_factory) );
c2dd8380
GL
765}
766
1987af7e 767bool wxMenu::DoAppend(wxMenuItem *mitem)
c2dd8380 768{
717a57c2 769 GtkWidget *menuItem;
96fd301f 770
717a57c2
VZ
771 if ( mitem->IsSeparator() )
772 {
08fc1744 773#if (GTK_MINOR_VERSION > 0)
717a57c2
VZ
774 GtkItemFactoryEntry entry;
775 entry.path = "/sep";
776 entry.callback = (GtkItemFactoryCallback) NULL;
777 entry.callback_action = 0;
778 entry.item_type = "<Separator>";
779 entry.accelerator = (gchar*) NULL;
780
781 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
782
783 /* this will be wrong for more than one separator. do we care? */
784 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
785#else // GTK+ 1.0
786 menuItem = gtk_menu_item_new();
787#endif // GTK 1.2/1.0
788 }
789 else if ( mitem->IsSubMenu() )
837904f2 790 {
717a57c2
VZ
791#if (GTK_MINOR_VERSION > 0)
792 /* text has "_" instead of "&" after mitem->SetText() */
793 wxString text( mitem->GetText() );
974e8d94 794
717a57c2
VZ
795 /* local buffer in multibyte form */
796 char buf[200];
797 strcpy( buf, "/" );
798 strcat( buf, text.mb_str() );
50592885 799
717a57c2
VZ
800 GtkItemFactoryEntry entry;
801 entry.path = buf;
802 entry.callback = (GtkItemFactoryCallback) 0;
803 entry.callback_action = 0;
804 entry.item_type = "<Branch>";
a583e623 805 entry.accelerator = (gchar*) NULL;
50592885 806
717a57c2 807 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
50592885 808
717a57c2 809 wxString path( mitem->GetFactoryPath() );
1987af7e 810 menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
717a57c2 811#else // GTK+ 1.0
1987af7e 812 menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
717a57c2 813#endif // GTK 1.2/1.0
50592885 814
1987af7e 815 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
837904f2 816 }
717a57c2
VZ
817 else // a normal item
818 {
034be888 819#if (GTK_MINOR_VERSION > 0)
717a57c2
VZ
820 /* text has "_" instead of "&" after mitem->SetText() */
821 wxString text( mitem->GetText() );
822
823 /* local buffer in multibyte form */
824 char buf[200];
825 strcpy( buf, "/" );
826 strcat( buf, text.mb_str() );
827
828 GtkItemFactoryEntry entry;
829 entry.path = buf;
830 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
831 entry.callback_action = 0;
1987af7e 832 if ( mitem->IsCheckable() )
717a57c2
VZ
833 entry.item_type = "<CheckItem>";
834 else
835 entry.item_type = "<Item>";
a583e623 836 entry.accelerator = (gchar*) NULL;
23280650 837
974e8d94 838#if wxUSE_ACCEL
717a57c2
VZ
839 // due to an apparent bug in GTK+, we have to use a static buffer here -
840 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
841 // somehow! (VZ)
842 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
843 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
844 entry.accelerator = s_accel;
845#else // !wxUSE_ACCEL
846 entry.accelerator = (char*) NULL;
847#endif // wxUSE_ACCEL/!wxUSE_ACCEL
96fd301f 848
717a57c2 849 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
cf7a7e13 850
717a57c2 851 wxString path( mitem->GetFactoryPath() );
1987af7e 852 menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
717a57c2 853#else // GTK+ 1.0
1987af7e
VZ
854 menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
855 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
034be888 856
717a57c2
VZ
857 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
858 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
859 (gpointer)this );
860#endif // GTK+ 1.2/1.0
861 }
23280650 862
717a57c2
VZ
863 if ( !mitem->IsSeparator() )
864 {
865 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
866 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
867 (gpointer)this );
837904f2 868
717a57c2
VZ
869 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
870 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
871 (gpointer)this );
872 }
23280650 873
717a57c2 874#if GTK_MINOR_VERSION == 0
837904f2
RR
875 gtk_menu_append( GTK_MENU(m_menu), menuItem );
876 gtk_widget_show( menuItem );
717a57c2 877#endif // GTK+ 1.0
23280650 878
837904f2 879 mitem->SetMenuItem(menuItem);
837904f2 880
717a57c2 881 return wxMenuBase::DoAppend(mitem);
6de97a3b 882}
c801d85f 883
717a57c2
VZ
884// VZ: this seems to be GTK+ 1.0 only code, I don't understand why there were
885// both specialized versions of Append() and this one before my changes,
886// but it seems that the others are better...
887#if 0
828f655f
RR
888void wxMenu::Append( wxMenuItem *item )
889{
828f655f
RR
890 GtkWidget *menuItem = (GtkWidget*) NULL;
891
c626a8b7 892 if (item->IsSeparator())
828f655f 893 menuItem = gtk_menu_item_new();
c626a8b7 894 else if (item->IsSubMenu())
b019151f 895 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
c626a8b7 896 else
b019151f
OK
897 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
898 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
828f655f
RR
899
900 if (!item->IsSeparator())
901 {
902 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
903 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
904 (gpointer*)this );
905
906 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
907 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
908 (gpointer*)this );
c626a8b7
VZ
909
910 if (!item->IsSubMenu())
911 {
828f655f
RR
912 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
913 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
914 (gpointer*)this );
c626a8b7 915 }
828f655f 916 }
c626a8b7 917
828f655f
RR
918 gtk_menu_append( GTK_MENU(m_menu), menuItem );
919 gtk_widget_show( menuItem );
96fd301f 920
717a57c2 921 item->SetMenuItem(menuItem);
c33c4050 922}
717a57c2 923#endif // 0
c33c4050 924
717a57c2 925bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 926{
717a57c2
VZ
927 if ( !wxMenuBase::DoInsert(pos, item) )
928 return FALSE;
c626a8b7 929
717a57c2 930 wxFAIL_MSG(wxT("not implemented"));
c626a8b7 931
717a57c2 932 return FALSE;
c33c4050
RR
933}
934
717a57c2 935wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 936{
717a57c2
VZ
937 if ( !wxMenuBase::DoRemove(item) )
938 return (wxMenuItem *)NULL;
c626a8b7 939
717a57c2
VZ
940 // TODO: this code doesn't delete the item factory item and this seems
941 // impossible as of GTK 1.2.6.
942 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 943
717a57c2 944 return item;
c33c4050
RR
945}
946
96fd301f
VZ
947int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
948{
83624f79
RR
949 wxNode *node = m_items.First();
950 while (node)
951 {
952 wxMenuItem *item = (wxMenuItem*)node->Data();
953 if (item->GetMenuItem() == menuItem)
954 return item->GetId();
955 node = node->Next();
956 }
96fd301f 957
c626a8b7 958 return wxNOT_FOUND;
6de97a3b 959}
c801d85f 960
717a57c2
VZ
961// ----------------------------------------------------------------------------
962// helpers
963// ----------------------------------------------------------------------------
964
965#if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
966static wxString GetHotKey( const wxMenuItem& item )
c801d85f 967{
717a57c2
VZ
968 wxString hotkey;
969
970 wxAcceleratorEntry *accel = item.GetAccel();
971 if ( accel )
83624f79 972 {
717a57c2
VZ
973 int flags = accel->GetFlags();
974 if ( flags & wxACCEL_ALT )
975 hotkey += wxT("<alt>");
976 if ( flags & wxACCEL_CTRL )
977 hotkey += wxT("<control>");
978 if ( flags & wxACCEL_SHIFT )
979 hotkey += wxT("<shift>");
980
981 int code = accel->GetKeyCode();
982 switch ( code )
c626a8b7 983 {
717a57c2
VZ
984 case WXK_F1:
985 case WXK_F2:
986 case WXK_F3:
987 case WXK_F4:
988 case WXK_F5:
989 case WXK_F6:
990 case WXK_F7:
991 case WXK_F8:
992 case WXK_F9:
993 case WXK_F10:
994 case WXK_F11:
995 case WXK_F12:
996 hotkey << wxT('F') << code - WXK_F1 + 1;
997 break;
96fd301f 998
717a57c2
VZ
999 // if there are any other keys wxGetAccelFromString() may return,
1000 // we should process them here
8bbe427f 1001
717a57c2
VZ
1002 default:
1003 if ( wxIsalnum(code) )
1004 {
1005 hotkey << (wxChar)code;
c801d85f 1006
717a57c2
VZ
1007 break;
1008 }
c801d85f 1009
717a57c2
VZ
1010 wxFAIL_MSG( wxT("unknown keyboard accel") );
1011 }
c801d85f 1012
717a57c2 1013 delete accel;
631f1bfe 1014 }
717a57c2
VZ
1015
1016 return hotkey;
631f1bfe 1017}
717a57c2 1018#endif // wxUSE_ACCEL
631f1bfe 1019