]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/menu.cpp
Border corrections
[wxWidgets.git] / src / gtk1 / menu.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
44014bcd 2// Name: src/gtk1/menu.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
96fd301f 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
e1bf3ad3 13#include "wx/menu.h"
ee0a94cf 14#include "wx/stockitem.h"
88a7a4e1
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
e4db172a 18 #include "wx/log.h"
670f9935 19 #include "wx/app.h"
0bca0373 20 #include "wx/bitmap.h"
88a7a4e1
WS
21#endif
22
974e8d94
VZ
23#if wxUSE_ACCEL
24 #include "wx/accel.h"
25#endif // wxUSE_ACCEL
26
3cbab641 27#include "wx/gtk1/private.h"
9e691f46 28
cc47eed9 29#include <gdk/gdkkeysyms.h>
9e691f46 30
3cbab641
MR
31#define ACCEL_OBJECT GtkObject
32#define ACCEL_OBJECTS(a) (a)->attach_objects
33#define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
83624f79 34
9959e2b6
VZ
35// we use normal item but with a special id for the menu title
36static const int wxGTK_TITLE_ID = -3;
37
defdd888 38// defined in window.cpp
3cbab641 39extern guint32 wxGtkTimeLastClick;
defdd888 40
acfd422a
RR
41//-----------------------------------------------------------------------------
42// idle system
43//-----------------------------------------------------------------------------
44
45extern void wxapp_install_idle_handler();
46extern bool g_isIdle;
47
6d971354 48#if wxUSE_ACCEL
98f29783 49static wxString GetGtkHotKey( const wxMenuItem& item );
717a57c2
VZ
50#endif
51
f6bcfd97
BP
52//-----------------------------------------------------------------------------
53// idle system
54//-----------------------------------------------------------------------------
55
56static wxString wxReplaceUnderscore( const wxString& title )
57{
58 const wxChar *pc;
2368dcda 59
6d971354 60 // GTK 1.2 wants to have "_" instead of "&" for accelerators
f6bcfd97 61 wxString str;
2b5f62a0
VZ
62 pc = title;
63 while (*pc != wxT('\0'))
f6bcfd97 64 {
2b5f62a0
VZ
65 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
66 {
67 // "&" is doubled to indicate "&" instead of accelerator
68 ++pc;
69 str << wxT('&');
70 }
71 else if (*pc == wxT('&'))
f6bcfd97 72 {
f6bcfd97 73 str << wxT('_');
4e9cbd33 74 }
f6bcfd97
BP
75 else
76 {
f6bcfd97
BP
77 if ( *pc == wxT('_') )
78 {
79 // underscores must be doubled to prevent them from being
80 // interpreted as accelerator character prefix by GTK
81 str << *pc;
82 }
f6bcfd97
BP
83
84 str << *pc;
85 }
2b5f62a0 86 ++pc;
f6bcfd97 87 }
9959e2b6 88
6d971354 89 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
9959e2b6 90
f6bcfd97
BP
91 return str;
92}
93
52af3158
JS
94static wxString wxConvertFromGTKToWXLabel(const wxString& gtkLabel)
95{
96 wxString label;
97 for ( const wxChar *pc = gtkLabel.c_str(); *pc; pc++ )
98 {
99 // '_' is the escape character for GTK+.
100
101 if ( *pc == wxT('_') && *(pc+1) == wxT('_'))
102 {
103 // An underscore was escaped.
104 label += wxT('_');
105 pc++;
106 }
107 else if ( *pc == wxT('_') )
108 {
109 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
110 label += wxT('&');
111 }
112 else if ( *pc == wxT('&') )
113 {
114 // Double the ampersand to escape it as far as wxWidgets is concerned
115 label += wxT("&&");
116 }
117 else
118 {
119 // don't remove ampersands '&' since if we have them in the menu title
120 // it means that they were doubled to indicate "&" instead of accelerator
121 label += *pc;
122 }
123 }
124
125 return label;
126}
127
128
e6396ed4
JS
129//-----------------------------------------------------------------------------
130// activate message from GTK
131//-----------------------------------------------------------------------------
132
cdf003d4 133static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
e6396ed4 134{
cdf003d4
VZ
135 if (g_isIdle)
136 wxapp_install_idle_handler();
e6396ed4 137
e6396ed4
JS
138 event.SetEventObject( menu );
139
a01fe3d6
RD
140 wxEvtHandler* handler = menu->GetEventHandler();
141 if (handler && handler->ProcessEvent(event))
e6396ed4
JS
142 return;
143
144 wxWindow *win = menu->GetInvokingWindow();
cdf003d4
VZ
145 if (win)
146 win->GetEventHandler()->ProcessEvent( event );
147}
148
149extern "C" {
150
89954433 151static void gtk_menu_open_callback( GtkWidget *WXUNUSED(widget), wxMenu *menu )
cdf003d4
VZ
152{
153 wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu);
154
155 DoCommonMenuCallbackCode(menu, event);
156}
157
89954433 158static void gtk_menu_close_callback( GtkWidget *WXUNUSED(widget), wxMenuBar *menubar )
cdf003d4 159{
13d35112
VZ
160 if ( !menubar->GetMenuCount() )
161 {
162 // if menubar is empty we can't call GetMenu(0) below
163 return;
164 }
cdf003d4 165
13d35112
VZ
166 wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL );
167
168 DoCommonMenuCallbackCode(menubar->GetMenu(0), event);
e6396ed4 169}
cdf003d4 170
865bb325 171}
e6396ed4 172
c801d85f
KB
173//-----------------------------------------------------------------------------
174// wxMenuBar
175//-----------------------------------------------------------------------------
176
177IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
178
294ea16d 179void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
3502e687 180{
6d971354 181 // the parent window is known after wxFrame::SetMenu()
44014bcd 182 m_needParent = false;
ae53c98c 183 m_style = style;
9c884972 184 m_invokingWindow = (wxWindow*) NULL;
23280650 185
4dcaf11a 186 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 187 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 188 {
223d09f6 189 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 190 return;
4dcaf11a 191 }
3502e687 192
3502e687 193 m_menubar = gtk_menu_bar_new();
6d971354 194 m_accel = gtk_accel_group_new();
3502e687
RR
195
196 if (style & wxMB_DOCKABLE)
197 {
198 m_widget = gtk_handle_box_new();
c626a8b7
VZ
199 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
200 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
201 }
202 else
203 {
204 m_widget = GTK_WIDGET(m_menubar);
205 }
206
207 PostCreation();
c4608a8a 208
db434467 209 ApplyWidgetStyle();
294ea16d
VZ
210
211 for (size_t i = 0; i < n; ++i )
212 Append(menus[i], titles[i]);
13d35112
VZ
213
214 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
215 // don't get it when the menu is dismissed by clicking outside the
216 // toolbar) so we connect to the global one, even if it means that we
217 // can't pass the menu which was closed in wxMenuEvent object
218 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar)),
219 "deactivate",
220 GTK_SIGNAL_FUNC(gtk_menu_close_callback),
221 (gpointer)this );
222
3502e687
RR
223}
224
294ea16d 225wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
c801d85f 226{
294ea16d
VZ
227 Init(n, menus, titles, style);
228}
96fd301f 229
294ea16d
VZ
230wxMenuBar::wxMenuBar(long style)
231{
232 Init(0, NULL, NULL, style);
233}
c4608a8a 234
294ea16d
VZ
235wxMenuBar::wxMenuBar()
236{
237 Init(0, NULL, NULL, 0);
6de97a3b 238}
c801d85f 239
1e133b7d
RR
240wxMenuBar::~wxMenuBar()
241{
1e133b7d
RR
242}
243
5bd9e519
RR
244static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
245{
246 menu->SetInvokingWindow( (wxWindow*) NULL );
247
5bd9e519 248 wxWindow *top_frame = win;
8487f887 249 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 250 top_frame = top_frame->GetParent();
5bd9e519 251
6d971354 252 // support for native hot keys
9e691f46 253 gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
5bd9e519 254
222ed1d6 255 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
256 while (node)
257 {
1987af7e 258 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
259 if (menuitem->IsSubMenu())
260 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 261 node = node->GetNext();
5bd9e519
RR
262 }
263}
264
265static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
266{
267 menu->SetInvokingWindow( win );
268
5bd9e519 269 wxWindow *top_frame = win;
8487f887 270 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 271 top_frame = top_frame->GetParent();
5bd9e519 272
6d971354 273 // support for native hot keys
9e691f46
VZ
274 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
275 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
b4da05a6 276 gtk_accel_group_attach( menu->m_accel, obj );
5bd9e519 277
222ed1d6 278 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
279 while (node)
280 {
1987af7e 281 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
282 if (menuitem->IsSubMenu())
283 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 284 node = node->GetNext();
5bd9e519
RR
285 }
286}
287
288void wxMenuBar::SetInvokingWindow( wxWindow *win )
289{
9c884972 290 m_invokingWindow = win;
5bd9e519 291 wxWindow *top_frame = win;
8487f887 292 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 293 top_frame = top_frame->GetParent();
5bd9e519 294
6d971354 295 // support for native key accelerators indicated by underscroes
9e691f46
VZ
296 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
297 if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) )
b4da05a6 298 gtk_accel_group_attach( m_accel, obj );
5bd9e519 299
222ed1d6 300 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
301 while (node)
302 {
1987af7e 303 wxMenu *menu = node->GetData();
5bd9e519 304 wxMenubarSetInvokingWindow( menu, win );
1987af7e 305 node = node->GetNext();
5bd9e519
RR
306 }
307}
308
309void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
310{
9c884972 311 m_invokingWindow = (wxWindow*) NULL;
5bd9e519 312 wxWindow *top_frame = win;
8487f887 313 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 314 top_frame = top_frame->GetParent();
9959e2b6 315
d9e403cc 316 // support for native key accelerators indicated by underscroes
9e691f46 317 gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
5bd9e519 318
222ed1d6 319 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
320 while (node)
321 {
1987af7e 322 wxMenu *menu = node->GetData();
5bd9e519 323 wxMenubarUnsetInvokingWindow( menu, win );
1987af7e 324 node = node->GetNext();
5bd9e519
RR
325 }
326}
327
3dfac970 328bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
c801d85f 329{
f03ec224 330 if ( !wxMenuBarBase::Append( menu, title ) )
44014bcd 331 return false;
f03ec224
VZ
332
333 return GtkAppend(menu, title);
334}
23280650 335
49826dab 336bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
f03ec224 337{
f6bcfd97 338 wxString str( wxReplaceUnderscore( title ) );
1e133b7d 339
d9e403cc 340 // This doesn't have much effect right now.
1e133b7d 341 menu->SetTitle( str );
23280650 342
6d971354 343 // The "m_owner" is the "menu item"
6d971354
RR
344 menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) );
345 GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
346 // set new text
347 gtk_label_set_text( label, wxGTK_CONV( str ) );
348 // reparse key accel
349 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) );
350 if (accel_key != GDK_VoidSymbol)
1e133b7d 351 {
6d971354 352 gtk_widget_add_accelerator (menu->m_owner,
88d19775
MR
353 "activate_item",
354 m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
355 accel_key,
356 GDK_MOD1_MASK,
357 GTK_ACCEL_LOCKED);
034be888 358 }
96fd301f 359
2b1c162e 360 gtk_widget_show( menu->m_owner );
9959e2b6 361
2b1c162e 362 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
9959e2b6 363
49826dab
RD
364 if (pos == -1)
365 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
366 else
367 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
9959e2b6 368
e6396ed4
JS
369 gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate",
370 GTK_SIGNAL_FUNC(gtk_menu_open_callback),
371 (gpointer)menu );
372
9c884972 373 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
2b5f62a0 374 // addings menu later on.
9c884972 375 if (m_invokingWindow)
2b5f62a0 376 {
9c884972 377 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
3dfac970 378
2b5f62a0
VZ
379 // OPTIMISE ME: we should probably cache this, or pass it
380 // directly, but for now this is a minimal
381 // change to validate the new dynamic sizing.
382 // see (and refactor :) similar code in Remove
383 // below.
384
d9e403cc 385 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0
VZ
386
387 if( frame )
388 frame->UpdateMenuBarSize();
389 }
390
44014bcd 391 return true;
3dfac970
VZ
392}
393
394bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
395{
396 if ( !wxMenuBarBase::Insert(pos, menu, title) )
44014bcd 397 return false;
3dfac970 398
6d971354 399 // TODO
9959e2b6 400
49826dab 401 if ( !GtkAppend(menu, title, (int)pos) )
44014bcd 402 return false;
f03ec224 403
44014bcd 404 return true;
3dfac970
VZ
405}
406
407wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
408{
f03ec224
VZ
409 // remove the old item and insert a new one
410 wxMenu *menuOld = Remove(pos);
411 if ( menuOld && !Insert(pos, menu, title) )
412 {
1d62a8b4 413 return (wxMenu*) NULL;
f03ec224 414 }
3dfac970 415
f03ec224
VZ
416 // either Insert() succeeded or Remove() failed and menuOld is NULL
417 return menuOld;
3dfac970
VZ
418}
419
420wxMenu *wxMenuBar::Remove(size_t pos)
421{
f03ec224
VZ
422 wxMenu *menu = wxMenuBarBase::Remove(pos);
423 if ( !menu )
1d62a8b4 424 return (wxMenu*) NULL;
f03ec224 425
defc0789
VS
426 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
427 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
c4608a8a 428
1d62a8b4 429 gtk_widget_destroy( menu->m_owner );
defc0789 430 menu->m_owner = NULL;
c4608a8a 431
2b5f62a0
VZ
432 if (m_invokingWindow)
433 {
6d971354
RR
434 // OPTIMISE ME: see comment in GtkAppend
435 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0 436
6d971354 437 if( frame )
2b5f62a0
VZ
438 frame->UpdateMenuBarSize();
439 }
440
1d62a8b4 441 return menu;
6de97a3b 442}
96fd301f 443
716b7364 444static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 445{
52af3158 446 if (wxMenuItem::GetLabelText(wxConvertFromGTKToWXLabel(menu->GetTitle())) == wxMenuItem::GetLabelText(menuString))
83624f79
RR
447 {
448 int res = menu->FindItem( itemString );
c626a8b7
VZ
449 if (res != wxNOT_FOUND)
450 return res;
83624f79 451 }
c626a8b7 452
222ed1d6 453 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
83624f79
RR
454 while (node)
455 {
1987af7e 456 wxMenuItem *item = node->GetData();
83624f79
RR
457 if (item->IsSubMenu())
458 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 459
1987af7e 460 node = node->GetNext();
83624f79
RR
461 }
462
c626a8b7
VZ
463 return wxNOT_FOUND;
464}
465
c801d85f
KB
466int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
467{
222ed1d6 468 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
469 while (node)
470 {
1987af7e 471 wxMenu *menu = node->GetData();
83624f79 472 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
473 if (res != -1)
474 return res;
475 node = node->GetNext();
83624f79 476 }
1987af7e
VZ
477
478 return wxNOT_FOUND;
6de97a3b 479}
c801d85f 480
c626a8b7 481// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 482static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 483{
717a57c2 484 wxMenuItem* result = menu->FindChildItem(id);
716b7364 485
222ed1d6 486 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
c626a8b7 487 while ( node && result == NULL )
83624f79 488 {
1987af7e 489 wxMenuItem *item = node->GetData();
83624f79 490 if (item->IsSubMenu())
c626a8b7 491 {
83624f79 492 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 493 }
1987af7e 494 node = node->GetNext();
83624f79 495 }
96fd301f 496
83624f79 497 return result;
6de97a3b 498}
716b7364 499
3dfac970 500wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 501{
83624f79 502 wxMenuItem* result = 0;
222ed1d6 503 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
504 while (node && result == 0)
505 {
1987af7e 506 wxMenu *menu = node->GetData();
83624f79 507 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 508 node = node->GetNext();
83624f79 509 }
c626a8b7 510
3dfac970
VZ
511 if ( menuForItem )
512 {
513 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
514 }
c626a8b7 515
3dfac970 516 return result;
bbe0af5b
RR
517}
518
3dfac970 519void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 520{
222ed1d6 521 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 522
223d09f6 523 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 524
3dfac970 525 wxMenu* menu = node->GetData();
c626a8b7
VZ
526
527 if (menu->m_owner)
528 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
529}
530
52af3158 531wxString wxMenuBar::GetMenuLabel( size_t pos ) const
bbe0af5b 532{
222ed1d6 533 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 534
223d09f6 535 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 536
3dfac970 537 wxMenu* menu = node->GetData();
c626a8b7 538
52af3158 539 return wxConvertFromGTKToWXLabel(menu->GetTitle());
bbe0af5b
RR
540}
541
52af3158 542void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
bbe0af5b 543{
222ed1d6 544 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 545
223d09f6 546 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 547
3dfac970 548 wxMenu* menu = node->GetData();
c626a8b7 549
4e115ed2 550 const wxString str( wxReplaceUnderscore( label ) );
f6bcfd97
BP
551
552 menu->SetTitle( str );
553
554 if (menu->m_owner)
555 {
4e115ed2 556 GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
f6bcfd97
BP
557
558 /* set new text */
4e115ed2 559 gtk_label_set( glabel, wxGTK_CONV( str ) );
f6bcfd97
BP
560
561 /* reparse key accel */
4e115ed2
VZ
562 (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) );
563 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) );
f6bcfd97
BP
564 }
565
bbe0af5b
RR
566}
567
c801d85f 568//-----------------------------------------------------------------------------
cf7a7e13 569// "activate"
c801d85f
KB
570//-----------------------------------------------------------------------------
571
865bb325 572extern "C" {
6de97a3b 573static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 574{
1e6feb95
VZ
575 if (g_isIdle)
576 wxapp_install_idle_handler();
c4608a8a 577
83624f79 578 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 579
83624f79 580 /* should find it for normal (not popup) menu */
1e6feb95
VZ
581 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
582 _T("menu item not found in gtk_menu_clicked_callback") );
96fd301f 583
c626a8b7
VZ
584 if (!menu->IsEnabled(id))
585 return;
96fd301f 586
4e6beae5
RD
587 wxMenuItem* item = menu->FindChildItem( id );
588 wxCHECK_RET( item, wxT("error in menu item callback") );
589
9959e2b6
VZ
590 if ( item->GetId() == wxGTK_TITLE_ID )
591 {
592 // ignore events from the menu title
593 return;
594 }
595
4e6beae5
RD
596 if (item->IsCheckable())
597 {
598 bool isReallyChecked = item->IsChecked(),
599 isInternallyChecked = item->wxMenuItemBase::IsChecked();
600
601 // ensure that the internal state is always consistent with what is
602 // shown on the screen
603 item->wxMenuItemBase::Check(isReallyChecked);
604
605 // we must not report the events for the radio button going up nor the
606 // events resulting from the calls to wxMenuItem::Check()
607 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
608 (isInternallyChecked == isReallyChecked) )
609 {
610 return;
611 }
612 }
613
614
02822c8c
RD
615 // Is this menu on a menubar? (possibly nested)
616 wxFrame* frame = NULL;
6edf1107
DE
617 if(menu->IsAttached())
618 frame = menu->GetMenuBar()->GetFrame();
02822c8c 619
da0b5338
VZ
620 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
621 // normally wxMenu::SendEvent() should be enough, if it doesn't work
622 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
623 // should be fixed instead of working around it here...
02822c8c 624 if (frame)
2d17d68f 625 {
4e6beae5
RD
626 // If it is attached then let the frame send the event.
627 // Don't call frame->ProcessCommand(id) because it toggles
628 // checkable items and we've already done that above.
629 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
630 commandEvent.SetEventObject(frame);
631 if (item->IsCheckable())
632 commandEvent.SetInt(item->IsChecked());
da0b5338 633 commandEvent.SetEventObject(menu);
4e6beae5
RD
634
635 frame->GetEventHandler()->ProcessEvent(commandEvent);
a01fe3d6
RD
636 }
637 else
638 {
4e6beae5 639 // otherwise let the menu have it
a01fe3d6 640 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
2d17d68f 641 }
cf7a7e13 642}
865bb325 643}
cf7a7e13
RR
644
645//-----------------------------------------------------------------------------
646// "select"
647//-----------------------------------------------------------------------------
648
865bb325 649extern "C" {
cf7a7e13
RR
650static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
651{
acfd422a
RR
652 if (g_isIdle) wxapp_install_idle_handler();
653
83624f79
RR
654 int id = menu->FindMenuIdByMenuItem(widget);
655
656 wxASSERT( id != -1 ); // should find it!
cf7a7e13 657
c626a8b7
VZ
658 if (!menu->IsEnabled(id))
659 return;
cf7a7e13 660
342b6a2f 661 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 662 event.SetEventObject( menu );
cf7a7e13 663
a01fe3d6
RD
664 wxEvtHandler* handler = menu->GetEventHandler();
665 if (handler && handler->ProcessEvent(event))
c626a8b7 666 return;
6de97a3b 667
83624f79
RR
668 wxWindow *win = menu->GetInvokingWindow();
669 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 670}
865bb325 671}
c801d85f 672
cd743a6f
RR
673//-----------------------------------------------------------------------------
674// "deselect"
675//-----------------------------------------------------------------------------
676
865bb325 677extern "C" {
cd743a6f
RR
678static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
679{
acfd422a
RR
680 if (g_isIdle) wxapp_install_idle_handler();
681
cd743a6f
RR
682 int id = menu->FindMenuIdByMenuItem(widget);
683
684 wxASSERT( id != -1 ); // should find it!
685
c626a8b7
VZ
686 if (!menu->IsEnabled(id))
687 return;
cd743a6f
RR
688
689 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
690 event.SetEventObject( menu );
691
a01fe3d6
RD
692 wxEvtHandler* handler = menu->GetEventHandler();
693 if (handler && handler->ProcessEvent(event))
c626a8b7 694 return;
cd743a6f
RR
695
696 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
697 if (win)
698 win->GetEventHandler()->ProcessEvent( event );
cd743a6f 699}
865bb325 700}
cd743a6f 701
cf7a7e13 702//-----------------------------------------------------------------------------
db1b4961 703// wxMenuItem
cf7a7e13
RR
704//-----------------------------------------------------------------------------
705
7dbf5360 706IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
974e8d94
VZ
707
708wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
709 int id,
710 const wxString& name,
711 const wxString& help,
d65c269b 712 wxItemKind kind,
974e8d94
VZ
713 wxMenu *subMenu)
714{
d65c269b 715 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 716}
96fd301f 717
974e8d94
VZ
718wxMenuItem::wxMenuItem(wxMenu *parentMenu,
719 int id,
720 const wxString& text,
721 const wxString& help,
d65c269b 722 wxItemKind kind,
974e8d94 723 wxMenu *subMenu)
d65c269b 724 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
2368dcda 725{
010eb424 726 Init();
2368dcda
VZ
727}
728
729wxMenuItem::wxMenuItem(wxMenu *parentMenu,
730 int id,
731 const wxString& text,
732 const wxString& help,
733 bool isCheckable,
734 wxMenu *subMenu)
735 : wxMenuItemBase(parentMenu, id, text, help,
736 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
737{
010eb424 738 Init();
2368dcda
VZ
739}
740
010eb424 741void wxMenuItem::Init()
c801d85f 742{
37d403aa 743 m_labelWidget = (GtkWidget *) NULL;
83624f79 744 m_menuItem = (GtkWidget *) NULL;
974e8d94 745
010eb424 746 DoSetText(m_text);
6de97a3b 747}
c801d85f 748
d1b15f03
RR
749wxMenuItem::~wxMenuItem()
750{
751 // don't delete menu items, the menus take care of that
752}
753
717a57c2 754// return the menu item text without any menu accels
3b59cdbf 755/* static */
52af3158 756wxString wxMenuItemBase::GetLabelText(const wxString& text)
717a57c2 757{
52af3158
JS
758 // The argument to this function will now always be in wxWidgets standard label
759 // format, not GTK+ format, so we do what the other ports do.
760
761 return wxStripMenuCodes(text);
762
763#if 0
717a57c2 764 wxString label;
2368dcda 765
3b59cdbf 766 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
717a57c2 767 {
f0b72e0d
RR
768 if ( *pc == wxT('\t'))
769 break;
9959e2b6 770
7cf4f7e2 771 if ( *pc == wxT('_') )
717a57c2 772 {
2b5f62a0 773 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
d76419bd
RR
774 pc++;
775 label += *pc;
776 continue;
777 }
2368dcda 778
7cf4f7e2
GD
779 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
780 {
781 // wxMSW escapes "&"
782 // "&" is doubled to indicate "&" instead of accelerator
783 continue;
784 }
a01fe3d6 785
717a57c2
VZ
786 label += *pc;
787 }
a01fe3d6 788
52af3158 789 // wxPrintf( wxT("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() );
d9e403cc 790
717a57c2 791 return label;
52af3158
JS
792#endif
793}
794
795wxString wxMenuItem::GetItemLabel() const
796{
a738f87c
JS
797 wxString label = wxConvertFromGTKToWXLabel(m_text);
798 if (!m_hotKey.IsEmpty())
799 label = label + wxT("\t") + m_hotKey;
800 return label;
717a57c2
VZ
801}
802
52af3158 803void wxMenuItem::SetItemLabel( const wxString& string )
717a57c2 804{
ee0a94cf 805 wxString str = string;
345319d6 806 if ( str.empty() && !IsSeparator() )
ee0a94cf 807 {
4fc3eebc 808 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
345319d6
VZ
809 str = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
810 wxSTOCK_WITH_MNEMONIC);
ee0a94cf
RR
811 }
812
5869f93f
JS
813 // Some optimization to avoid flicker
814 wxString oldLabel = m_text;
98f29783 815 oldLabel = wxStripMenuCodes(oldLabel);
44014bcd 816 oldLabel.Replace(wxT("_"), wxEmptyString);
98f29783 817 wxString label1 = wxStripMenuCodes(str);
a8e607d9
RR
818 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
819 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
a01fe3d6 820
717a57c2 821 DoSetText(str);
354aa1e3 822
88d19775 823 if (oldLabel == label1 &&
a8e607d9 824 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
98f29783
RR
825 return;
826
354aa1e3
RR
827 if (m_menuItem)
828 {
37d403aa
JS
829 GtkLabel *label;
830 if (m_labelWidget)
2b5f62a0 831 label = (GtkLabel*) m_labelWidget;
37d403aa 832 else
2b5f62a0 833 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2 834
2b5f62a0 835 // set new text
fab591c5 836 gtk_label_set( label, wxGTK_CONV( m_text ) );
717a57c2 837
2b5f62a0
VZ
838 // reparse key accel
839 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
354aa1e3
RR
840 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
841 }
98f29783 842
98f29783
RR
843 guint accel_key;
844 GdkModifierType accel_mods;
98f29783
RR
845 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
846 if (accel_key != 0)
847 {
88d19775 848 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
98f29783
RR
849 m_parentMenu->m_accel,
850 accel_key,
851 accel_mods );
852 }
853
854 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
855 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
856 if (accel_key != 0)
857 {
858 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
859 "activate",
860 m_parentMenu->m_accel,
861 accel_key,
862 accel_mods,
863 GTK_ACCEL_VISIBLE);
864 }
354aa1e3
RR
865}
866
c626a8b7 867// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 868void wxMenuItem::DoSetText( const wxString& str )
716b7364 869{
2b5f62a0 870 // '\t' is the deliminator indicating a hot key
010eb424
VZ
871 wxString text;
872 text.reserve(str.length());
873
ab46dc18 874 const wxChar *pc = str;
2b5f62a0 875 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
83624f79 876 {
2b5f62a0 877 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
23280650 878 {
2b5f62a0
VZ
879 // "&" is doubled to indicate "&" instead of accelerator
880 ++pc;
010eb424 881 text << wxT('&');
572d7461 882 }
2b5f62a0 883 else if (*pc == wxT('&'))
572d7461 884 {
010eb424 885 text << wxT('_');
572d7461 886 }
2b5f62a0
VZ
887 else if ( *pc == wxT('_') ) // escape underscores
888 {
010eb424 889 text << wxT("__");
2b5f62a0 890 }
9959e2b6 891 else
23280650 892 {
010eb424 893 text << *pc;
2b5f62a0
VZ
894 }
895 ++pc;
83624f79 896 }
a01fe3d6 897
44014bcd 898 m_hotKey = wxEmptyString;
9e691f46 899
010eb424 900 if ( *pc == wxT('\t') )
d7dbc98a
KB
901 {
902 pc++;
903 m_hotKey = pc;
904 }
010eb424
VZ
905
906 m_text = text;
716b7364
RR
907}
908
717a57c2
VZ
909#if wxUSE_ACCEL
910
911wxAcceleratorEntry *wxMenuItem::GetAccel() const
912{
1987af7e 913 if ( !GetHotKey() )
717a57c2
VZ
914 {
915 // nothing
916 return (wxAcceleratorEntry *)NULL;
917 }
918
90527a50
VZ
919 // accelerator parsing code looks for them after a TAB, so insert a dummy
920 // one here
717a57c2 921 wxString label;
1987af7e 922 label << wxT('\t') << GetHotKey();
717a57c2 923
90527a50 924 return wxAcceleratorEntry::Create(label);
717a57c2
VZ
925}
926
927#endif // wxUSE_ACCEL
928
96fd301f 929void wxMenuItem::Check( bool check )
716b7364 930{
223d09f6 931 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 932
974e8d94
VZ
933 if (check == m_isChecked)
934 return;
2d17d68f 935
974e8d94 936 wxMenuItemBase::Check( check );
0472ece7 937
24bcaec3 938 switch ( GetKind() )
0472ece7 939 {
24bcaec3
VZ
940 case wxITEM_CHECK:
941 case wxITEM_RADIO:
942 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
943 break;
944
945 default:
946 wxFAIL_MSG( _T("can't check this item") );
0472ece7 947 }
716b7364
RR
948}
949
8bbe427f
VZ
950void wxMenuItem::Enable( bool enable )
951{
223d09f6 952 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 953
83624f79 954 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 955 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
956}
957
96fd301f 958bool wxMenuItem::IsChecked() const
716b7364 959{
44014bcd 960 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
db1b4961 961
44014bcd 962 wxCHECK_MSG( IsCheckable(), false,
974e8d94 963 wxT("can't get state of uncheckable item!") );
96fd301f 964
974e8d94 965 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
966}
967
db1b4961 968//-----------------------------------------------------------------------------
83624f79 969// wxMenu
db1b4961
RR
970//-----------------------------------------------------------------------------
971
c801d85f
KB
972IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
973
717a57c2 974void wxMenu::Init()
c801d85f 975{
034be888 976 m_accel = gtk_accel_group_new();
6d971354 977 m_menu = gtk_menu_new();
defc0789
VS
978 // NB: keep reference to the menu so that it is not destroyed behind
979 // our back by GTK+ e.g. when it is removed from menubar:
980 gtk_widget_ref(m_menu);
8bbe427f 981
2b1c162e 982 m_owner = (GtkWidget*) NULL;
2b2edbed 983
d9e403cc
RR
984 // Tearoffs are entries, just like separators. So if we want this
985 // menu to be a tear-off one, we just append a tearoff entry
986 // immediately.
9959e2b6 987 if ( m_style & wxMENU_TEAROFF )
2b2edbed 988 {
9959e2b6 989 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
6d971354 990
9959e2b6
VZ
991 gtk_menu_append(GTK_MENU(m_menu), tearoff);
992 }
6d971354 993
9959e2b6 994 m_prevRadio = NULL;
c801d85f 995
717a57c2 996 // append the title as the very first entry if we have it
9959e2b6 997 if ( !m_title.empty() )
d1b15f03 998 {
9959e2b6 999 Append(wxGTK_TITLE_ID, m_title);
717a57c2 1000 AppendSeparator();
d1b15f03 1001 }
717a57c2 1002}
15a2076a 1003
717a57c2
VZ
1004wxMenu::~wxMenu()
1005{
222ed1d6 1006 WX_CLEAR_LIST(wxMenuItemList, m_items);
f03ec224 1007
368a4a47 1008 if ( GTK_IS_WIDGET( m_menu ))
defc0789 1009 {
a761df69 1010 // see wxMenu::Init
88d19775 1011 gtk_widget_unref( m_menu );
a761df69
VS
1012 // if the menu is inserted in another menu at this time, there was
1013 // one more reference to it:
1014 if ( m_owner )
1015 gtk_widget_destroy( m_menu );
defc0789 1016 }
c2dd8380
GL
1017}
1018
49826dab 1019bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
c2dd8380 1020{
717a57c2 1021 GtkWidget *menuItem;
96fd301f 1022
e31126cb 1023 wxString text;
44014bcd 1024 GtkLabel* label = NULL;
e31126cb 1025
717a57c2
VZ
1026 if ( mitem->IsSeparator() )
1027 {
6d971354
RR
1028 // TODO
1029 menuItem = gtk_menu_item_new();
837904f2 1030 }
71628a57 1031 else if (mitem->GetBitmap().Ok())
37d403aa 1032 {
9c895994 1033 text = mitem->wxMenuItemBase::GetItemLabel();
cc47eed9 1034 const wxBitmap *bitmap = &mitem->GetBitmap();
9959e2b6 1035
88a7a4e1 1036 // TODO
5fd420ed
JS
1037 wxUnusedVar(bitmap);
1038 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
1039 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1040
1041 m_prevRadio = NULL;
1042 }
717a57c2
VZ
1043 else // a normal item
1044 {
52af3158 1045 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
84c51319 1046 text = mitem->wxMenuItemBase::GetItemLabel() ;
717a57c2 1047
d65c269b
VZ
1048 switch ( mitem->GetKind() )
1049 {
546bfbea 1050 case wxITEM_CHECK:
6d971354 1051 {
6d971354 1052 menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) );
e31126cb 1053 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1054 // set new text
1055 gtk_label_set_text( label, wxGTK_CONV( text ) );
6d971354 1056 m_prevRadio = NULL;
d65c269b 1057 break;
6d971354 1058 }
d65c269b 1059
546bfbea 1060 case wxITEM_RADIO:
6d971354
RR
1061 {
1062 GSList *group = NULL;
1063 if ( m_prevRadio == NULL )
d65c269b
VZ
1064 {
1065 // start of a new radio group
6d971354 1066 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
e31126cb 1067 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1068 // set new text
1069 gtk_label_set_text( label, wxGTK_CONV( text ) );
d65c269b
VZ
1070 }
1071 else // continue the radio group
1072 {
6d971354
RR
1073 group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1074 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
e31126cb 1075 label = GTK_LABEL( GTK_BIN(menuItem)->child );
d65c269b 1076 }
c0d0a552 1077 break;
6d971354 1078 }
d65c269b
VZ
1079
1080 default:
1081 wxFAIL_MSG( _T("unexpected menu item kind") );
1082 // fall through
1083
546bfbea 1084 case wxITEM_NORMAL:
6d971354 1085 {
6d971354 1086 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
e31126cb 1087 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354 1088 m_prevRadio = NULL;
d65c269b 1089 break;
6d971354 1090 }
d65c269b
VZ
1091 }
1092
6d971354 1093 }
9959e2b6 1094
6d971354
RR
1095 guint accel_key;
1096 GdkModifierType accel_mods;
98f29783 1097 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) );
9959e2b6 1098
52af3158 1099 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
6d971354
RR
1100 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1101 if (accel_key != 0)
1102 {
9959e2b6
VZ
1103 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1104 "activate",
6d971354 1105 m_accel,
9959e2b6 1106 accel_key,
6d971354
RR
1107 accel_mods,
1108 GTK_ACCEL_VISIBLE);
717a57c2 1109 }
9959e2b6 1110
e31126cb
RR
1111 if (pos == -1)
1112 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1113 else
1114 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1115
6d971354 1116 gtk_widget_show( menuItem );
23280650 1117
717a57c2
VZ
1118 if ( !mitem->IsSeparator() )
1119 {
2b5f62a0 1120 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
a01fe3d6 1121
717a57c2
VZ
1122 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1123 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1124 (gpointer)this );
837904f2 1125
717a57c2
VZ
1126 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1127 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1128 (gpointer)this );
e31126cb 1129
88d19775
MR
1130 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1131 {
e31126cb
RR
1132 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1133
1134 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1135
1136 // if adding a submenu to a menu already existing in the menu bar, we
1137 // must set invoking window to allow processing events from this
1138 // submenu
1139 if ( m_invokingWindow )
1140 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
88d19775
MR
1141 }
1142 else
1143 {
e31126cb 1144 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
88d19775
MR
1145 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1146 (gpointer)this );
1147 }
1148
e31126cb
RR
1149 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
1150 if (accel_key != GDK_VoidSymbol)
1151 {
1152 gtk_widget_add_accelerator (menuItem,
88d19775
MR
1153 "activate_item",
1154 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)),
1155 accel_key,
1156 GDK_MOD1_MASK,
1157 GTK_ACCEL_LOCKED);
1158 }
717a57c2 1159 }
23280650 1160
837904f2 1161 mitem->SetMenuItem(menuItem);
837904f2 1162
6d971354 1163 if (ms_locked)
d65c269b 1164 {
6d971354
RR
1165 // This doesn't even exist!
1166 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
d65c269b 1167 }
d65c269b 1168
44014bcd 1169 return true;
6de97a3b 1170}
c801d85f 1171
9add9367 1172wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
828f655f 1173{
9add9367
RD
1174 if (!GtkAppend(mitem))
1175 return NULL;
9959e2b6 1176
9add9367 1177 return wxMenuBase::DoAppend(mitem);
c33c4050
RR
1178}
1179
9add9367 1180wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 1181{
717a57c2 1182 if ( !wxMenuBase::DoInsert(pos, item) )
9add9367 1183 return NULL;
c626a8b7 1184
6d971354 1185 // TODO
49826dab 1186 if ( !GtkAppend(item, (int)pos) )
9add9367 1187 return NULL;
32db328c 1188
9add9367 1189 return item;
c33c4050
RR
1190}
1191
717a57c2 1192wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 1193{
717a57c2
VZ
1194 if ( !wxMenuBase::DoRemove(item) )
1195 return (wxMenuItem *)NULL;
c626a8b7 1196
717a57c2
VZ
1197 // TODO: this code doesn't delete the item factory item and this seems
1198 // impossible as of GTK 1.2.6.
1199 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 1200
717a57c2 1201 return item;
c33c4050
RR
1202}
1203
96fd301f
VZ
1204int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1205{
222ed1d6 1206 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
83624f79
RR
1207 while (node)
1208 {
b1d4dd7a 1209 wxMenuItem *item = node->GetData();
83624f79 1210 if (item->GetMenuItem() == menuItem)
88d19775 1211 return item->GetId();
b1d4dd7a 1212 node = node->GetNext();
83624f79 1213 }
96fd301f 1214
c626a8b7 1215 return wxNOT_FOUND;
6de97a3b 1216}
c801d85f 1217
717a57c2
VZ
1218// ----------------------------------------------------------------------------
1219// helpers
1220// ----------------------------------------------------------------------------
1221
6d971354 1222#if wxUSE_ACCEL
a070d8ce 1223
98f29783 1224static wxString GetGtkHotKey( const wxMenuItem& item )
c801d85f 1225{
717a57c2
VZ
1226 wxString hotkey;
1227
1228 wxAcceleratorEntry *accel = item.GetAccel();
1229 if ( accel )
83624f79 1230 {
717a57c2
VZ
1231 int flags = accel->GetFlags();
1232 if ( flags & wxACCEL_ALT )
1233 hotkey += wxT("<alt>");
1234 if ( flags & wxACCEL_CTRL )
1235 hotkey += wxT("<control>");
1236 if ( flags & wxACCEL_SHIFT )
1237 hotkey += wxT("<shift>");
1238
1239 int code = accel->GetKeyCode();
1240 switch ( code )
c626a8b7 1241 {
717a57c2
VZ
1242 case WXK_F1:
1243 case WXK_F2:
1244 case WXK_F3:
1245 case WXK_F4:
1246 case WXK_F5:
1247 case WXK_F6:
1248 case WXK_F7:
1249 case WXK_F8:
1250 case WXK_F9:
1251 case WXK_F10:
1252 case WXK_F11:
1253 case WXK_F12:
bbcd4085
RR
1254 case WXK_F13:
1255 case WXK_F14:
1256 case WXK_F15:
1257 case WXK_F16:
1258 case WXK_F17:
1259 case WXK_F18:
1260 case WXK_F19:
1261 case WXK_F20:
1262 case WXK_F21:
1263 case WXK_F22:
1264 case WXK_F23:
1265 case WXK_F24:
04cc1e93 1266 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
717a57c2 1267 break;
2368dcda 1268
a070d8ce
VZ
1269 // TODO: we should use gdk_keyval_name() (a.k.a.
1270 // XKeysymToString) here as well as hardcoding the keysym
1271 // names this might be not portable
bbcd4085 1272 case WXK_INSERT:
3ca6a5f0
BP
1273 hotkey << wxT("Insert" );
1274 break;
1275 case WXK_DELETE:
1276 hotkey << wxT("Delete" );
1277 break;
2b5f62a0
VZ
1278 case WXK_UP:
1279 hotkey << wxT("Up" );
1280 break;
1281 case WXK_DOWN:
1282 hotkey << wxT("Down" );
1283 break;
1284 case WXK_PAGEUP:
f005ea42 1285 hotkey << wxT("Page_Up" );
2b5f62a0
VZ
1286 break;
1287 case WXK_PAGEDOWN:
f005ea42 1288 hotkey << wxT("Page_Down" );
2b5f62a0
VZ
1289 break;
1290 case WXK_LEFT:
1291 hotkey << wxT("Left" );
1292 break;
1293 case WXK_RIGHT:
1294 hotkey << wxT("Right" );
1295 break;
1296 case WXK_HOME:
1297 hotkey << wxT("Home" );
1298 break;
1299 case WXK_END:
1300 hotkey << wxT("End" );
1301 break;
1302 case WXK_RETURN:
1303 hotkey << wxT("Return" );
1304 break;
bbcd4085
RR
1305 case WXK_BACK:
1306 hotkey << wxT("BackSpace" );
1307 break;
1308 case WXK_TAB:
1309 hotkey << wxT("Tab" );
1310 break;
1311 case WXK_ESCAPE:
1312 hotkey << wxT("Esc" );
1313 break;
1314 case WXK_SPACE:
1315 hotkey << wxT("space" );
1316 break;
1317 case WXK_MULTIPLY:
1318 hotkey << wxT("Multiply" );
1319 break;
1320 case WXK_ADD:
1321 hotkey << wxT("Add" );
1322 break;
1323 case WXK_SEPARATOR:
1324 hotkey << wxT("Separator" );
1325 break;
1326 case WXK_SUBTRACT:
1327 hotkey << wxT("Subtract" );
1328 break;
1329 case WXK_DECIMAL:
1330 hotkey << wxT("Decimal" );
1331 break;
1332 case WXK_DIVIDE:
1333 hotkey << wxT("Divide" );
1334 break;
1335 case WXK_CANCEL:
1336 hotkey << wxT("Cancel" );
1337 break;
1338 case WXK_CLEAR:
1339 hotkey << wxT("Clear" );
1340 break;
1341 case WXK_MENU:
1342 hotkey << wxT("Menu" );
1343 break;
1344 case WXK_PAUSE:
1345 hotkey << wxT("Pause" );
1346 break;
1347 case WXK_CAPITAL:
1348 hotkey << wxT("Capital" );
1349 break;
1350 case WXK_SELECT:
1351 hotkey << wxT("Select" );
1352 break;
1353 case WXK_PRINT:
1354 hotkey << wxT("Print" );
1355 break;
1356 case WXK_EXECUTE:
1357 hotkey << wxT("Execute" );
1358 break;
1359 case WXK_SNAPSHOT:
1360 hotkey << wxT("Snapshot" );
1361 break;
1362 case WXK_HELP:
1363 hotkey << wxT("Help" );
1364 break;
1365 case WXK_NUMLOCK:
1366 hotkey << wxT("Num_Lock" );
1367 break;
1368 case WXK_SCROLL:
1369 hotkey << wxT("Scroll_Lock" );
1370 break;
1371 case WXK_NUMPAD_INSERT:
1372 hotkey << wxT("KP_Insert" );
1373 break;
1374 case WXK_NUMPAD_DELETE:
1375 hotkey << wxT("KP_Delete" );
1376 break;
1377 case WXK_NUMPAD_SPACE:
1378 hotkey << wxT("KP_Space" );
1379 break;
1380 case WXK_NUMPAD_TAB:
1381 hotkey << wxT("KP_Tab" );
1382 break;
1383 case WXK_NUMPAD_ENTER:
1384 hotkey << wxT("KP_Enter" );
1385 break;
1386 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1387 case WXK_NUMPAD_F4:
1388 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1389 break;
1390 case WXK_NUMPAD_HOME:
1391 hotkey << wxT("KP_Home" );
1392 break;
1393 case WXK_NUMPAD_LEFT:
1394 hotkey << wxT("KP_Left" );
1395 break;
1396 case WXK_NUMPAD_UP:
1397 hotkey << wxT("KP_Up" );
1398 break;
1399 case WXK_NUMPAD_RIGHT:
1400 hotkey << wxT("KP_Right" );
1401 break;
1402 case WXK_NUMPAD_DOWN:
1403 hotkey << wxT("KP_Down" );
1404 break;
aee12bfb 1405 case WXK_NUMPAD_PAGEUP:
f005ea42 1406 hotkey << wxT("KP_Page_Up" );
bbcd4085 1407 break;
aee12bfb 1408 case WXK_NUMPAD_PAGEDOWN:
f005ea42 1409 hotkey << wxT("KP_Page_Down" );
bbcd4085
RR
1410 break;
1411 case WXK_NUMPAD_END:
1412 hotkey << wxT("KP_End" );
1413 break;
1414 case WXK_NUMPAD_BEGIN:
1415 hotkey << wxT("KP_Begin" );
1416 break;
1417 case WXK_NUMPAD_EQUAL:
1418 hotkey << wxT("KP_Equal" );
1419 break;
1420 case WXK_NUMPAD_MULTIPLY:
1421 hotkey << wxT("KP_Multiply" );
1422 break;
1423 case WXK_NUMPAD_ADD:
1424 hotkey << wxT("KP_Add" );
1425 break;
1426 case WXK_NUMPAD_SEPARATOR:
1427 hotkey << wxT("KP_Separator" );
1428 break;
1429 case WXK_NUMPAD_SUBTRACT:
1430 hotkey << wxT("KP_Subtract" );
1431 break;
1432 case WXK_NUMPAD_DECIMAL:
1433 hotkey << wxT("KP_Decimal" );
1434 break;
1435 case WXK_NUMPAD_DIVIDE:
1436 hotkey << wxT("KP_Divide" );
1437 break;
1438 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1439 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1440 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1441 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1442 break;
1443 case WXK_WINDOWS_LEFT:
1444 hotkey << wxT("Super_L" );
1445 break;
1446 case WXK_WINDOWS_RIGHT:
1447 hotkey << wxT("Super_R" );
1448 break;
1449 case WXK_WINDOWS_MENU:
1450 hotkey << wxT("Menu" );
1451 break;
1452 case WXK_COMMAND:
1453 hotkey << wxT("Command" );
1454 break;
1455 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
88d19775
MR
1456 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1457 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1458 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1459 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
bbcd4085
RR
1460 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1461 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1462 break;
1463 */
90527a50 1464 // if there are any other keys wxAcceleratorEntry::Create() may
a070d8ce 1465 // return, we should process them here
8bbe427f 1466
717a57c2 1467 default:
a070d8ce 1468 if ( code < 127 )
717a57c2 1469 {
2b5f62a0 1470 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
316ef03b 1471 if ( !name.empty() )
a070d8ce
VZ
1472 {
1473 hotkey << name;
1474 break;
1475 }
717a57c2 1476 }
c801d85f 1477
717a57c2
VZ
1478 wxFAIL_MSG( wxT("unknown keyboard accel") );
1479 }
c801d85f 1480
717a57c2 1481 delete accel;
631f1bfe 1482 }
717a57c2
VZ
1483
1484 return hotkey;
631f1bfe 1485}
a070d8ce 1486
717a57c2 1487#endif // wxUSE_ACCEL
43a11e2a
VZ
1488
1489// ----------------------------------------------------------------------------
1490// Pop-up menu stuff
1491// ----------------------------------------------------------------------------
1492
1493#if wxUSE_MENUS_NATIVE
1494
81939780 1495extern "C" WXDLLIMPEXP_CORE
43a11e2a
VZ
1496void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
1497{
670f9935 1498 *is_waiting = false;
43a11e2a
VZ
1499}
1500
81939780 1501WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win )
43a11e2a
VZ
1502{
1503 menu->SetInvokingWindow( win );
1504
1505 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
1506 while (node)
1507 {
1508 wxMenuItem *menuitem = node->GetData();
1509 if (menuitem->IsSubMenu())
1510 {
1511 SetInvokingWindow( menuitem->GetSubMenu(), win );
1512 }
1513
1514 node = node->GetNext();
1515 }
1516}
1517
81939780 1518extern "C" WXDLLIMPEXP_CORE
43a11e2a
VZ
1519void wxPopupMenuPositionCallback( GtkMenu *menu,
1520 gint *x, gint *y,
43a11e2a
VZ
1521 gpointer user_data )
1522{
1523 // ensure that the menu appears entirely on screen
1524 GtkRequisition req;
1525 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
1526
1527 wxSize sizeScreen = wxGetDisplaySize();
1528 wxPoint *pos = (wxPoint*)user_data;
1529
1530 gint xmax = sizeScreen.x - req.width,
1531 ymax = sizeScreen.y - req.height;
1532
1533 *x = pos->x < xmax ? pos->x : xmax;
1534 *y = pos->y < ymax ? pos->y : ymax;
1535}
1536
1537bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
1538{
1539 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
1540
1541 wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") );
1542
1543 // NOTE: if you change this code, you need to update
1544 // the same code in taskbar.cpp as well. This
1545 // is ugly code duplication, I know.
1546
1547 SetInvokingWindow( menu, this );
1548
1549 menu->UpdateUI();
1550
1551 bool is_waiting = true;
1552
1553 gulong handler = gtk_signal_connect( GTK_OBJECT(menu->m_menu),
1554 "hide",
1555 GTK_SIGNAL_FUNC(gtk_pop_hide_callback),
1556 (gpointer)&is_waiting );
1557
1558 wxPoint pos;
1559 gpointer userdata;
1560 GtkMenuPositionFunc posfunc;
1561 if ( x == -1 && y == -1 )
1562 {
1563 // use GTK's default positioning algorithm
1564 userdata = NULL;
1565 posfunc = NULL;
1566 }
1567 else
1568 {
1569 pos = ClientToScreen(wxPoint(x, y));
1570 userdata = &pos;
1571 posfunc = wxPopupMenuPositionCallback;
1572 }
1573
1574 wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu);
1575 DoCommonMenuCallbackCode(menu, eventOpen);
1576
1577 gtk_menu_popup(
1578 GTK_MENU(menu->m_menu),
1579 (GtkWidget *) NULL, // parent menu shell
1580 (GtkWidget *) NULL, // parent menu item
1581 posfunc, // function to position it
1582 userdata, // client data
1583 0, // button used to activate it
defdd888 1584 wxGtkTimeLastClick // the time of activation
43a11e2a
VZ
1585 );
1586
1587 while (is_waiting)
1588 {
1589 gtk_main_iteration();
1590 }
1591
1592 gtk_signal_disconnect(GTK_OBJECT(menu->m_menu), handler);
1593
1594 wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu);
1595 DoCommonMenuCallbackCode(menu, eventClose);
1596
1597 return true;
1598}
1599
1600#endif // wxUSE_MENUS_NATIVE