]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Use GetFont not m_font for GetCharHeight
[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
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"
96fd301f 14#include "wx/log.h"
30dea054 15#include "wx/intl.h"
06cfab17 16#include "wx/app.h"
37d403aa 17#include "wx/bitmap.h"
c801d85f 18
974e8d94
VZ
19#if wxUSE_ACCEL
20 #include "wx/accel.h"
21#endif // wxUSE_ACCEL
22
9e691f46
VZ
23#include "wx/gtk/private.h"
24
cc47eed9 25#include <gdk/gdkkeysyms.h>
9e691f46
VZ
26
27// FIXME: is this right? somehow I don't think so (VZ)
28#ifdef __WXGTK20__
29 #include <glib-object.h>
30
6d971354
RR
31 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
32 #define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
9e691f46
VZ
33 #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
34
6d971354 35 #define ACCEL_OBJECT GtkWindow
9e691f46 36 #define ACCEL_OBJECTS(a) (a)->acceleratables
6d971354 37 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
9e691f46
VZ
38#else // GTK+ 1.x
39 #define ACCEL_OBJECT GtkObject
40 #define ACCEL_OBJECTS(a) (a)->attach_objects
41 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
42#endif
83624f79 43
9959e2b6
VZ
44// we use normal item but with a special id for the menu title
45static const int wxGTK_TITLE_ID = -3;
46
acfd422a
RR
47//-----------------------------------------------------------------------------
48// idle system
49//-----------------------------------------------------------------------------
50
51extern void wxapp_install_idle_handler();
52extern bool g_isIdle;
53
6d971354 54#if wxUSE_ACCEL
98f29783 55static wxString GetGtkHotKey( const wxMenuItem& item );
717a57c2
VZ
56#endif
57
cc47eed9
RR
58//-----------------------------------------------------------------------------
59// substitute for missing GtkPixmapMenuItem
60//-----------------------------------------------------------------------------
37d403aa 61
9e691f46 62#ifndef __WXGTK20__
9e691f46 63
cc47eed9
RR
64#define GTK_TYPE_PIXMAP_MENU_ITEM (gtk_pixmap_menu_item_get_type ())
65#define GTK_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItem))
37d403aa 66#define GTK_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItemClass))
cc47eed9 67#define GTK_IS_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_TYPE ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
37d403aa
JS
68#define GTK_IS_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PIXMAP_MENU_ITEM))
69//#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
cc47eed9 70#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_PIXMAP_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
37d403aa
JS
71
72#ifndef GTK_MENU_ITEM_GET_CLASS
73#define GTK_MENU_ITEM_GET_CLASS(obj) (GTK_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
74#endif
75
76typedef struct _GtkPixmapMenuItem GtkPixmapMenuItem;
77typedef struct _GtkPixmapMenuItemClass GtkPixmapMenuItemClass;
78
79struct _GtkPixmapMenuItem
80{
cc47eed9 81 GtkMenuItem menu_item;
37d403aa 82
cc47eed9 83 GtkWidget *pixmap;
37d403aa
JS
84};
85
86struct _GtkPixmapMenuItemClass
87{
cc47eed9 88 GtkMenuItemClass parent_class;
37d403aa 89
cc47eed9
RR
90 guint orig_toggle_size;
91 guint have_pixmap_count;
37d403aa
JS
92};
93
865bb325 94extern "C" {
71628a57 95GtkType gtk_pixmap_menu_item_get_type (void);
cc47eed9
RR
96GtkWidget* gtk_pixmap_menu_item_new (void);
97void gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
865bb325
VZ
98 GtkWidget *pixmap);
99}
100
101#endif // !__WXGTK20__
9e691f46 102
f6bcfd97
BP
103//-----------------------------------------------------------------------------
104// idle system
105//-----------------------------------------------------------------------------
106
107static wxString wxReplaceUnderscore( const wxString& title )
108{
109 const wxChar *pc;
2368dcda 110
6d971354 111 // GTK 1.2 wants to have "_" instead of "&" for accelerators
f6bcfd97 112 wxString str;
2b5f62a0
VZ
113 pc = title;
114 while (*pc != wxT('\0'))
f6bcfd97 115 {
2b5f62a0
VZ
116 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
117 {
118 // "&" is doubled to indicate "&" instead of accelerator
119 ++pc;
120 str << wxT('&');
121 }
122 else if (*pc == wxT('&'))
f6bcfd97 123 {
f6bcfd97 124 str << wxT('_');
4e9cbd33 125 }
f6bcfd97
BP
126 else
127 {
f6bcfd97
BP
128 if ( *pc == wxT('_') )
129 {
130 // underscores must be doubled to prevent them from being
131 // interpreted as accelerator character prefix by GTK
132 str << *pc;
133 }
f6bcfd97
BP
134
135 str << *pc;
136 }
2b5f62a0 137 ++pc;
f6bcfd97 138 }
9959e2b6 139
6d971354 140 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
9959e2b6 141
f6bcfd97
BP
142 return str;
143}
144
e6396ed4
JS
145//-----------------------------------------------------------------------------
146// activate message from GTK
147//-----------------------------------------------------------------------------
148
865bb325 149extern "C" {
e6396ed4
JS
150static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
151{
152 if (g_isIdle) wxapp_install_idle_handler();
153
92f1a59c 154 wxMenuEvent event( wxEVT_MENU_OPEN, -1, menu );
e6396ed4
JS
155 event.SetEventObject( menu );
156
a01fe3d6
RD
157 wxEvtHandler* handler = menu->GetEventHandler();
158 if (handler && handler->ProcessEvent(event))
e6396ed4
JS
159 return;
160
161 wxWindow *win = menu->GetInvokingWindow();
162 if (win) win->GetEventHandler()->ProcessEvent( event );
163}
865bb325 164}
e6396ed4 165
c801d85f
KB
166//-----------------------------------------------------------------------------
167// wxMenuBar
168//-----------------------------------------------------------------------------
169
170IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
171
294ea16d 172void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
3502e687 173{
6d971354 174 // the parent window is known after wxFrame::SetMenu()
23280650 175 m_needParent = FALSE;
ae53c98c 176 m_style = style;
9c884972 177 m_invokingWindow = (wxWindow*) NULL;
23280650 178
4dcaf11a 179 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 180 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 181 {
223d09f6 182 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 183 return;
4dcaf11a 184 }
3502e687 185
3502e687 186 m_menubar = gtk_menu_bar_new();
6d971354
RR
187#ifndef __WXGTK20__
188 m_accel = gtk_accel_group_new();
1e133b7d 189#endif
3502e687
RR
190
191 if (style & wxMB_DOCKABLE)
192 {
193 m_widget = gtk_handle_box_new();
c626a8b7
VZ
194 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
195 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
196 }
197 else
198 {
199 m_widget = GTK_WIDGET(m_menubar);
200 }
201
202 PostCreation();
c4608a8a 203
db434467 204 ApplyWidgetStyle();
294ea16d
VZ
205
206 for (size_t i = 0; i < n; ++i )
207 Append(menus[i], titles[i]);
3502e687
RR
208}
209
294ea16d 210wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
c801d85f 211{
294ea16d
VZ
212 Init(n, menus, titles, style);
213}
96fd301f 214
294ea16d
VZ
215wxMenuBar::wxMenuBar(long style)
216{
217 Init(0, NULL, NULL, style);
218}
c4608a8a 219
294ea16d
VZ
220wxMenuBar::wxMenuBar()
221{
222 Init(0, NULL, NULL, 0);
6de97a3b 223}
c801d85f 224
1e133b7d
RR
225wxMenuBar::~wxMenuBar()
226{
1e133b7d
RR
227}
228
5bd9e519
RR
229static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
230{
231 menu->SetInvokingWindow( (wxWindow*) NULL );
232
5bd9e519 233 wxWindow *top_frame = win;
8487f887 234 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 235 top_frame = top_frame->GetParent();
5bd9e519 236
6d971354
RR
237#ifndef __WXGTK20__
238 // support for native hot keys
9e691f46 239 gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
6d971354 240#endif
5bd9e519 241
222ed1d6 242 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
243 while (node)
244 {
1987af7e 245 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
246 if (menuitem->IsSubMenu())
247 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 248 node = node->GetNext();
5bd9e519
RR
249 }
250}
251
252static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
253{
254 menu->SetInvokingWindow( win );
255
5bd9e519 256 wxWindow *top_frame = win;
8487f887 257 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 258 top_frame = top_frame->GetParent();
5bd9e519 259
6d971354 260 // support for native hot keys
9e691f46
VZ
261 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
262 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
b4da05a6 263 gtk_accel_group_attach( menu->m_accel, obj );
5bd9e519 264
222ed1d6 265 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
266 while (node)
267 {
1987af7e 268 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
269 if (menuitem->IsSubMenu())
270 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 271 node = node->GetNext();
5bd9e519
RR
272 }
273}
274
275void wxMenuBar::SetInvokingWindow( wxWindow *win )
276{
9c884972 277 m_invokingWindow = win;
5bd9e519 278 wxWindow *top_frame = win;
8487f887 279 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 280 top_frame = top_frame->GetParent();
5bd9e519 281
6d971354
RR
282#ifndef __WXGTK20__
283 // support for native key accelerators indicated by underscroes
9e691f46
VZ
284 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
285 if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) )
b4da05a6 286 gtk_accel_group_attach( m_accel, obj );
6d971354 287#endif
5bd9e519 288
222ed1d6 289 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
290 while (node)
291 {
1987af7e 292 wxMenu *menu = node->GetData();
5bd9e519 293 wxMenubarSetInvokingWindow( menu, win );
1987af7e 294 node = node->GetNext();
5bd9e519
RR
295 }
296}
297
298void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
299{
9c884972 300 m_invokingWindow = (wxWindow*) NULL;
5bd9e519 301 wxWindow *top_frame = win;
8487f887 302 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 303 top_frame = top_frame->GetParent();
9959e2b6 304
6d971354 305#ifndef __WXGTK20__
d9e403cc 306 // support for native key accelerators indicated by underscroes
9e691f46 307 gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
6d971354 308#endif
5bd9e519 309
222ed1d6 310 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
311 while (node)
312 {
1987af7e 313 wxMenu *menu = node->GetData();
5bd9e519 314 wxMenubarUnsetInvokingWindow( menu, win );
1987af7e 315 node = node->GetNext();
5bd9e519
RR
316 }
317}
318
3dfac970 319bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
c801d85f 320{
f03ec224
VZ
321 if ( !wxMenuBarBase::Append( menu, title ) )
322 return FALSE;
323
324 return GtkAppend(menu, title);
325}
23280650 326
49826dab 327bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
f03ec224 328{
f6bcfd97 329 wxString str( wxReplaceUnderscore( title ) );
1e133b7d 330
d9e403cc 331 // This doesn't have much effect right now.
1e133b7d 332 menu->SetTitle( str );
23280650 333
6d971354
RR
334 // The "m_owner" is the "menu item"
335#ifdef __WXGTK20__
336 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
337#else
338 menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) );
339 GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
340 // set new text
341 gtk_label_set_text( label, wxGTK_CONV( str ) );
342 // reparse key accel
343 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) );
344 if (accel_key != GDK_VoidSymbol)
1e133b7d 345 {
6d971354 346 gtk_widget_add_accelerator (menu->m_owner,
88d19775
MR
347 "activate_item",
348 m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
349 accel_key,
350 GDK_MOD1_MASK,
351 GTK_ACCEL_LOCKED);
034be888 352 }
6d971354 353#endif
96fd301f 354
2b1c162e 355 gtk_widget_show( menu->m_owner );
9959e2b6 356
2b1c162e 357 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
9959e2b6 358
49826dab
RD
359 if (pos == -1)
360 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
361 else
362 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
9959e2b6 363
e6396ed4
JS
364 gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate",
365 GTK_SIGNAL_FUNC(gtk_menu_open_callback),
366 (gpointer)menu );
367
9c884972 368 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
2b5f62a0 369 // addings menu later on.
9c884972 370 if (m_invokingWindow)
2b5f62a0 371 {
9c884972 372 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
3dfac970 373
2b5f62a0
VZ
374 // OPTIMISE ME: we should probably cache this, or pass it
375 // directly, but for now this is a minimal
376 // change to validate the new dynamic sizing.
377 // see (and refactor :) similar code in Remove
378 // below.
379
d9e403cc 380 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0
VZ
381
382 if( frame )
383 frame->UpdateMenuBarSize();
384 }
385
3dfac970
VZ
386 return TRUE;
387}
388
389bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
390{
391 if ( !wxMenuBarBase::Insert(pos, menu, title) )
392 return FALSE;
393
6d971354 394 // TODO
9959e2b6 395
49826dab 396 if ( !GtkAppend(menu, title, (int)pos) )
f03ec224
VZ
397 return FALSE;
398
f03ec224 399 return TRUE;
3dfac970
VZ
400}
401
402wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
403{
f03ec224
VZ
404 // remove the old item and insert a new one
405 wxMenu *menuOld = Remove(pos);
406 if ( menuOld && !Insert(pos, menu, title) )
407 {
1d62a8b4 408 return (wxMenu*) NULL;
f03ec224 409 }
3dfac970 410
f03ec224
VZ
411 // either Insert() succeeded or Remove() failed and menuOld is NULL
412 return menuOld;
3dfac970
VZ
413}
414
415wxMenu *wxMenuBar::Remove(size_t pos)
416{
f03ec224
VZ
417 wxMenu *menu = wxMenuBarBase::Remove(pos);
418 if ( !menu )
1d62a8b4 419 return (wxMenu*) NULL;
f03ec224 420
defc0789
VS
421 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
422 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
c4608a8a 423
1d62a8b4 424 gtk_widget_destroy( menu->m_owner );
defc0789 425 menu->m_owner = NULL;
c4608a8a 426
2b5f62a0
VZ
427 if (m_invokingWindow)
428 {
6d971354
RR
429 // OPTIMISE ME: see comment in GtkAppend
430 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0 431
6d971354 432 if( frame )
2b5f62a0
VZ
433 frame->UpdateMenuBarSize();
434 }
435
1d62a8b4 436 return menu;
6de97a3b 437}
96fd301f 438
716b7364 439static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 440{
f6bcfd97 441 if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
83624f79
RR
442 {
443 int res = menu->FindItem( itemString );
c626a8b7
VZ
444 if (res != wxNOT_FOUND)
445 return res;
83624f79 446 }
c626a8b7 447
222ed1d6 448 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
83624f79
RR
449 while (node)
450 {
1987af7e 451 wxMenuItem *item = node->GetData();
83624f79
RR
452 if (item->IsSubMenu())
453 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 454
1987af7e 455 node = node->GetNext();
83624f79
RR
456 }
457
c626a8b7
VZ
458 return wxNOT_FOUND;
459}
460
c801d85f
KB
461int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
462{
222ed1d6 463 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
464 while (node)
465 {
1987af7e 466 wxMenu *menu = node->GetData();
83624f79 467 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
468 if (res != -1)
469 return res;
470 node = node->GetNext();
83624f79 471 }
1987af7e
VZ
472
473 return wxNOT_FOUND;
6de97a3b 474}
c801d85f 475
c626a8b7 476// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 477static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 478{
717a57c2 479 wxMenuItem* result = menu->FindChildItem(id);
716b7364 480
222ed1d6 481 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
c626a8b7 482 while ( node && result == NULL )
83624f79 483 {
1987af7e 484 wxMenuItem *item = node->GetData();
83624f79 485 if (item->IsSubMenu())
c626a8b7 486 {
83624f79 487 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 488 }
1987af7e 489 node = node->GetNext();
83624f79 490 }
96fd301f 491
83624f79 492 return result;
6de97a3b 493}
716b7364 494
3dfac970 495wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 496{
83624f79 497 wxMenuItem* result = 0;
222ed1d6 498 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
499 while (node && result == 0)
500 {
1987af7e 501 wxMenu *menu = node->GetData();
83624f79 502 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 503 node = node->GetNext();
83624f79 504 }
c626a8b7 505
3dfac970
VZ
506 if ( menuForItem )
507 {
508 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
509 }
c626a8b7 510
3dfac970 511 return result;
bbe0af5b
RR
512}
513
3dfac970 514void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 515{
222ed1d6 516 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 517
223d09f6 518 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 519
3dfac970 520 wxMenu* menu = node->GetData();
c626a8b7
VZ
521
522 if (menu->m_owner)
523 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
524}
525
3dfac970 526wxString wxMenuBar::GetLabelTop( size_t pos ) const
bbe0af5b 527{
222ed1d6 528 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 529
223d09f6 530 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 531
3dfac970 532 wxMenu* menu = node->GetData();
c626a8b7 533
f6bcfd97
BP
534 wxString label;
535 wxString text( menu->GetTitle() );
f6bcfd97
BP
536 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
537 {
2b5f62a0 538 if ( *pc == wxT('_') )
f6bcfd97 539 {
2b5f62a0 540 // '_' is the escape character for GTK+
f6bcfd97
BP
541 continue;
542 }
543
7cf4f7e2
GD
544 // don't remove ampersands '&' since if we have them in the menu title
545 // it means that they were doubled to indicate "&" instead of accelerator
546
f6bcfd97
BP
547 label += *pc;
548 }
f6bcfd97
BP
549
550 return label;
bbe0af5b
RR
551}
552
3dfac970 553void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
bbe0af5b 554{
222ed1d6 555 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 556
223d09f6 557 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 558
3dfac970 559 wxMenu* menu = node->GetData();
c626a8b7 560
4e115ed2 561 const wxString str( wxReplaceUnderscore( label ) );
f6bcfd97
BP
562
563 menu->SetTitle( str );
564
565 if (menu->m_owner)
566 {
4e115ed2 567 GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
f6bcfd97
BP
568
569 /* set new text */
4e115ed2 570 gtk_label_set( glabel, wxGTK_CONV( str ) );
f6bcfd97
BP
571
572 /* reparse key accel */
4e115ed2
VZ
573 (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) );
574 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) );
f6bcfd97
BP
575 }
576
bbe0af5b
RR
577}
578
c801d85f 579//-----------------------------------------------------------------------------
cf7a7e13 580// "activate"
c801d85f
KB
581//-----------------------------------------------------------------------------
582
865bb325 583extern "C" {
6de97a3b 584static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 585{
1e6feb95
VZ
586 if (g_isIdle)
587 wxapp_install_idle_handler();
c4608a8a 588
83624f79 589 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 590
83624f79 591 /* should find it for normal (not popup) menu */
1e6feb95
VZ
592 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
593 _T("menu item not found in gtk_menu_clicked_callback") );
96fd301f 594
c626a8b7
VZ
595 if (!menu->IsEnabled(id))
596 return;
96fd301f 597
4e6beae5
RD
598 wxMenuItem* item = menu->FindChildItem( id );
599 wxCHECK_RET( item, wxT("error in menu item callback") );
600
9959e2b6
VZ
601 if ( item->GetId() == wxGTK_TITLE_ID )
602 {
603 // ignore events from the menu title
604 return;
605 }
606
4e6beae5
RD
607 if (item->IsCheckable())
608 {
609 bool isReallyChecked = item->IsChecked(),
610 isInternallyChecked = item->wxMenuItemBase::IsChecked();
611
612 // ensure that the internal state is always consistent with what is
613 // shown on the screen
614 item->wxMenuItemBase::Check(isReallyChecked);
615
616 // we must not report the events for the radio button going up nor the
617 // events resulting from the calls to wxMenuItem::Check()
618 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
619 (isInternallyChecked == isReallyChecked) )
620 {
621 return;
622 }
623 }
624
625
02822c8c
RD
626 // Is this menu on a menubar? (possibly nested)
627 wxFrame* frame = NULL;
6edf1107
DE
628 if(menu->IsAttached())
629 frame = menu->GetMenuBar()->GetFrame();
02822c8c 630
da0b5338
VZ
631 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
632 // normally wxMenu::SendEvent() should be enough, if it doesn't work
633 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
634 // should be fixed instead of working around it here...
02822c8c 635 if (frame)
2d17d68f 636 {
4e6beae5
RD
637 // If it is attached then let the frame send the event.
638 // Don't call frame->ProcessCommand(id) because it toggles
639 // checkable items and we've already done that above.
640 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
641 commandEvent.SetEventObject(frame);
642 if (item->IsCheckable())
643 commandEvent.SetInt(item->IsChecked());
da0b5338 644 commandEvent.SetEventObject(menu);
4e6beae5
RD
645
646 frame->GetEventHandler()->ProcessEvent(commandEvent);
a01fe3d6
RD
647 }
648 else
649 {
4e6beae5 650 // otherwise let the menu have it
a01fe3d6 651 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
2d17d68f 652 }
cf7a7e13 653}
865bb325 654}
cf7a7e13
RR
655
656//-----------------------------------------------------------------------------
657// "select"
658//-----------------------------------------------------------------------------
659
865bb325 660extern "C" {
cf7a7e13
RR
661static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
662{
acfd422a
RR
663 if (g_isIdle) wxapp_install_idle_handler();
664
83624f79
RR
665 int id = menu->FindMenuIdByMenuItem(widget);
666
667 wxASSERT( id != -1 ); // should find it!
cf7a7e13 668
c626a8b7
VZ
669 if (!menu->IsEnabled(id))
670 return;
cf7a7e13 671
342b6a2f 672 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 673 event.SetEventObject( menu );
cf7a7e13 674
a01fe3d6
RD
675 wxEvtHandler* handler = menu->GetEventHandler();
676 if (handler && handler->ProcessEvent(event))
c626a8b7 677 return;
6de97a3b 678
83624f79
RR
679 wxWindow *win = menu->GetInvokingWindow();
680 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 681}
865bb325 682}
c801d85f 683
cd743a6f
RR
684//-----------------------------------------------------------------------------
685// "deselect"
686//-----------------------------------------------------------------------------
687
865bb325 688extern "C" {
cd743a6f
RR
689static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
690{
acfd422a
RR
691 if (g_isIdle) wxapp_install_idle_handler();
692
cd743a6f
RR
693 int id = menu->FindMenuIdByMenuItem(widget);
694
695 wxASSERT( id != -1 ); // should find it!
696
c626a8b7
VZ
697 if (!menu->IsEnabled(id))
698 return;
cd743a6f
RR
699
700 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
701 event.SetEventObject( menu );
702
a01fe3d6
RD
703 wxEvtHandler* handler = menu->GetEventHandler();
704 if (handler && handler->ProcessEvent(event))
c626a8b7 705 return;
cd743a6f
RR
706
707 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
708 if (win)
709 win->GetEventHandler()->ProcessEvent( event );
cd743a6f 710}
865bb325 711}
cd743a6f 712
cf7a7e13 713//-----------------------------------------------------------------------------
db1b4961 714// wxMenuItem
cf7a7e13
RR
715//-----------------------------------------------------------------------------
716
7dbf5360 717IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
974e8d94
VZ
718
719wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
720 int id,
721 const wxString& name,
722 const wxString& help,
d65c269b 723 wxItemKind kind,
974e8d94
VZ
724 wxMenu *subMenu)
725{
d65c269b 726 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 727}
96fd301f 728
974e8d94
VZ
729wxMenuItem::wxMenuItem(wxMenu *parentMenu,
730 int id,
731 const wxString& text,
732 const wxString& help,
d65c269b 733 wxItemKind kind,
974e8d94 734 wxMenu *subMenu)
d65c269b 735 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
2368dcda 736{
092f7536 737 Init(text);
2368dcda
VZ
738}
739
740wxMenuItem::wxMenuItem(wxMenu *parentMenu,
741 int id,
742 const wxString& text,
743 const wxString& help,
744 bool isCheckable,
745 wxMenu *subMenu)
746 : wxMenuItemBase(parentMenu, id, text, help,
747 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
748{
092f7536 749 Init(text);
2368dcda
VZ
750}
751
092f7536 752void wxMenuItem::Init(const wxString& text)
c801d85f 753{
37d403aa 754 m_labelWidget = (GtkWidget *) NULL;
83624f79 755 m_menuItem = (GtkWidget *) NULL;
974e8d94 756
092f7536 757 DoSetText(text);
6de97a3b 758}
c801d85f 759
d1b15f03
RR
760wxMenuItem::~wxMenuItem()
761{
762 // don't delete menu items, the menus take care of that
763}
764
717a57c2 765// return the menu item text without any menu accels
3b59cdbf
VZ
766/* static */
767wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2
VZ
768{
769 wxString label;
2368dcda 770
3b59cdbf 771 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
717a57c2 772 {
f0b72e0d
RR
773 if ( *pc == wxT('\t'))
774 break;
9959e2b6 775
7cf4f7e2 776 if ( *pc == wxT('_') )
717a57c2 777 {
2b5f62a0 778 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
d76419bd
RR
779 pc++;
780 label += *pc;
781 continue;
782 }
2368dcda 783
6d971354 784#ifdef __WXGTK20__
2b5f62a0 785 if ( *pc == wxT('\\') )
d76419bd 786 {
2b5f62a0
VZ
787 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
788 pc++;
789 label += *pc;
717a57c2
VZ
790 continue;
791 }
2b5f62a0 792#endif
717a57c2 793
7cf4f7e2
GD
794 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
795 {
796 // wxMSW escapes "&"
797 // "&" is doubled to indicate "&" instead of accelerator
798 continue;
799 }
a01fe3d6 800
717a57c2
VZ
801 label += *pc;
802 }
a01fe3d6 803
6d971354 804 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
d9e403cc 805
717a57c2
VZ
806 return label;
807}
808
809void wxMenuItem::SetText( const wxString& str )
810{
5869f93f
JS
811 // Some optimization to avoid flicker
812 wxString oldLabel = m_text;
98f29783 813 oldLabel = wxStripMenuCodes(oldLabel);
5869f93f 814 oldLabel.Replace(wxT("_"), wxT(""));
98f29783 815 wxString label1 = wxStripMenuCodes(str);
a8e607d9
RR
816 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
817 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
a01fe3d6 818
717a57c2 819 DoSetText(str);
354aa1e3 820
88d19775 821 if (oldLabel == label1 &&
a8e607d9 822 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
98f29783
RR
823 return;
824
354aa1e3
RR
825 if (m_menuItem)
826 {
37d403aa
JS
827 GtkLabel *label;
828 if (m_labelWidget)
2b5f62a0 829 label = (GtkLabel*) m_labelWidget;
37d403aa 830 else
2b5f62a0 831 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2 832
6d971354
RR
833#ifdef __WXGTK20__
834 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(m_text) );
2b5f62a0
VZ
835#else
836 // set new text
fab591c5 837 gtk_label_set( label, wxGTK_CONV( m_text ) );
717a57c2 838
2b5f62a0
VZ
839 // reparse key accel
840 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
354aa1e3 841 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
2b5f62a0 842#endif
354aa1e3 843 }
98f29783 844
98f29783
RR
845 guint accel_key;
846 GdkModifierType accel_mods;
98f29783
RR
847 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
848 if (accel_key != 0)
849 {
88d19775 850 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
98f29783
RR
851 m_parentMenu->m_accel,
852 accel_key,
853 accel_mods );
854 }
855
856 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
857 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
858 if (accel_key != 0)
859 {
860 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
861 "activate",
862 m_parentMenu->m_accel,
863 accel_key,
864 accel_mods,
865 GTK_ACCEL_VISIBLE);
866 }
354aa1e3
RR
867}
868
c626a8b7 869// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 870void wxMenuItem::DoSetText( const wxString& str )
716b7364 871{
2b5f62a0 872 // '\t' is the deliminator indicating a hot key
974e8d94 873 m_text.Empty();
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;
881 m_text << wxT('&');
572d7461 882 }
2b5f62a0 883 else if (*pc == wxT('&'))
572d7461 884 {
2b5f62a0 885 m_text << wxT('_');
572d7461 886 }
2b5f62a0
VZ
887 else if ( *pc == wxT('_') ) // escape underscores
888 {
889 m_text << wxT("__");
890 }
9959e2b6 891 else
23280650 892 {
354aa1e3 893 m_text << *pc;
2b5f62a0
VZ
894 }
895 ++pc;
83624f79 896 }
a01fe3d6 897
223d09f6 898 m_hotKey = wxT("");
9e691f46 899
223d09f6 900 if(*pc == wxT('\t'))
d7dbc98a
KB
901 {
902 pc++;
903 m_hotKey = pc;
904 }
88d19775 905
98f29783 906 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
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
919 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
920 wxString label;
1987af7e 921 label << wxT('\t') << GetHotKey();
717a57c2
VZ
922
923 return wxGetAccelFromString(label);
924}
925
926#endif // wxUSE_ACCEL
927
96fd301f 928void wxMenuItem::Check( bool check )
716b7364 929{
223d09f6 930 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 931
974e8d94
VZ
932 if (check == m_isChecked)
933 return;
2d17d68f 934
974e8d94 935 wxMenuItemBase::Check( check );
0472ece7 936
24bcaec3 937 switch ( GetKind() )
0472ece7 938 {
24bcaec3
VZ
939 case wxITEM_CHECK:
940 case wxITEM_RADIO:
941 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
942 break;
943
944 default:
945 wxFAIL_MSG( _T("can't check this item") );
0472ece7 946 }
716b7364
RR
947}
948
8bbe427f
VZ
949void wxMenuItem::Enable( bool enable )
950{
223d09f6 951 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 952
83624f79 953 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 954 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
955}
956
96fd301f 957bool wxMenuItem::IsChecked() const
716b7364 958{
223d09f6 959 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
db1b4961 960
974e8d94
VZ
961 wxCHECK_MSG( IsCheckable(), FALSE,
962 wxT("can't get state of uncheckable item!") );
96fd301f 963
974e8d94 964 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
965}
966
db1b4961 967//-----------------------------------------------------------------------------
83624f79 968// wxMenu
db1b4961
RR
969//-----------------------------------------------------------------------------
970
c801d85f
KB
971IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
972
717a57c2 973void wxMenu::Init()
c801d85f 974{
034be888 975 m_accel = gtk_accel_group_new();
6d971354 976 m_menu = gtk_menu_new();
defc0789
VS
977 // NB: keep reference to the menu so that it is not destroyed behind
978 // our back by GTK+ e.g. when it is removed from menubar:
979 gtk_widget_ref(m_menu);
8bbe427f 980
2b1c162e 981 m_owner = (GtkWidget*) NULL;
2b2edbed 982
d9e403cc
RR
983 // Tearoffs are entries, just like separators. So if we want this
984 // menu to be a tear-off one, we just append a tearoff entry
985 // immediately.
9959e2b6 986 if ( m_style & wxMENU_TEAROFF )
2b2edbed 987 {
9959e2b6 988 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
6d971354 989
9959e2b6
VZ
990 gtk_menu_append(GTK_MENU(m_menu), tearoff);
991 }
6d971354 992
9959e2b6 993 m_prevRadio = NULL;
c801d85f 994
717a57c2 995 // append the title as the very first entry if we have it
9959e2b6 996 if ( !m_title.empty() )
d1b15f03 997 {
9959e2b6 998 Append(wxGTK_TITLE_ID, m_title);
717a57c2 999 AppendSeparator();
d1b15f03 1000 }
717a57c2 1001}
15a2076a 1002
717a57c2
VZ
1003wxMenu::~wxMenu()
1004{
222ed1d6 1005 WX_CLEAR_LIST(wxMenuItemList, m_items);
f03ec224 1006
368a4a47 1007 if ( GTK_IS_WIDGET( m_menu ))
defc0789 1008 {
a761df69 1009 // see wxMenu::Init
88d19775 1010 gtk_widget_unref( m_menu );
a761df69
VS
1011 // if the menu is inserted in another menu at this time, there was
1012 // one more reference to it:
1013 if ( m_owner )
1014 gtk_widget_destroy( m_menu );
defc0789 1015 }
c2dd8380
GL
1016}
1017
49826dab 1018bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
c2dd8380 1019{
717a57c2 1020 GtkWidget *menuItem;
96fd301f 1021
e31126cb
RR
1022 wxString text;
1023#ifndef __WXGTK20__
1024 GtkLabel* label;
1025#endif
1026
717a57c2
VZ
1027 if ( mitem->IsSeparator() )
1028 {
6d971354
RR
1029#ifdef __WXGTK20__
1030 menuItem = gtk_separator_menu_item_new();
1031#else
1032 // TODO
1033 menuItem = gtk_menu_item_new();
1034#endif
837904f2 1035 }
71628a57 1036 else if (mitem->GetBitmap().Ok())
37d403aa 1037 {
e31126cb 1038 text = mitem->GetText();
cc47eed9 1039 const wxBitmap *bitmap = &mitem->GetBitmap();
9959e2b6 1040
6d971354
RR
1041#ifdef __WXGTK20__
1042 menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
9959e2b6 1043
b1ad1424
VS
1044 GtkWidget *image;
1045 if (bitmap->HasPixbuf())
1046 {
1047 image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
1048 }
1049 else
1050 {
1051 GdkPixmap *gdk_pixmap = bitmap->GetPixmap();
88d19775 1052 GdkBitmap *gdk_bitmap = bitmap->GetMask() ?
b1ad1424
VS
1053 bitmap->GetMask()->GetBitmap() :
1054 (GdkBitmap*) NULL;
1055 image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap );
1056 }
88d19775 1057
6d971354 1058 gtk_widget_show(image);
9959e2b6 1059
6d971354 1060 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image );
9959e2b6 1061
6d971354 1062#else
b1ad1424
VS
1063 GdkPixmap *gdk_pixmap = bitmap->GetPixmap();
1064 GdkBitmap *gdk_bitmap = bitmap->GetMask() ? bitmap->GetMask()->GetBitmap() : (GdkBitmap*) NULL;
2368dcda 1065
cc47eed9 1066 menuItem = gtk_pixmap_menu_item_new ();
e31126cb 1067 label = GTK_LABEL(gtk_accel_label_new ( wxGTK_CONV( text ) ));
cc47eed9 1068 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
e31126cb 1069 gtk_container_add (GTK_CONTAINER (menuItem), GTK_WIDGET(label));
71628a57 1070
cc47eed9 1071 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuItem);
33caa994 1072
e31126cb 1073 gtk_widget_show (GTK_WIDGET(label));
37d403aa 1074
e31126cb 1075 mitem->SetLabelWidget(GTK_WIDGET(label));
37d403aa 1076
6d971354 1077 GtkWidget* pixmap = gtk_pixmap_new( gdk_pixmap, gdk_bitmap );
cc47eed9
RR
1078 gtk_widget_show(pixmap);
1079 gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem ), pixmap);
37d403aa 1080
71628a57 1081#endif
6d971354
RR
1082
1083 m_prevRadio = NULL;
1084 }
717a57c2
VZ
1085 else // a normal item
1086 {
982f23fd 1087 // text has "_" instead of "&" after mitem->SetText() so don't use it
e31126cb 1088 text = mitem->GetText() ;
717a57c2 1089
d65c269b
VZ
1090 switch ( mitem->GetKind() )
1091 {
546bfbea 1092 case wxITEM_CHECK:
6d971354
RR
1093 {
1094#ifdef __WXGTK20__
1095 menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1096#else
1097 menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) );
e31126cb 1098 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1099 // set new text
1100 gtk_label_set_text( label, wxGTK_CONV( text ) );
6d971354
RR
1101#endif
1102 m_prevRadio = NULL;
d65c269b 1103 break;
6d971354 1104 }
d65c269b 1105
546bfbea 1106 case wxITEM_RADIO:
6d971354
RR
1107 {
1108 GSList *group = NULL;
1109 if ( m_prevRadio == NULL )
d65c269b
VZ
1110 {
1111 // start of a new radio group
6d971354
RR
1112#ifdef __WXGTK20__
1113 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1114#else
1115 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
e31126cb 1116 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1117 // set new text
1118 gtk_label_set_text( label, wxGTK_CONV( text ) );
6d971354 1119#endif
d65c269b
VZ
1120 }
1121 else // continue the radio group
1122 {
6d971354
RR
1123#ifdef __WXGTK20__
1124 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1125 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) );
1126#else
1127 group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio));
1128 m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) );
e31126cb 1129 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354 1130#endif
d65c269b 1131 }
c0d0a552 1132 break;
6d971354 1133 }
d65c269b
VZ
1134
1135 default:
1136 wxFAIL_MSG( _T("unexpected menu item kind") );
1137 // fall through
1138
546bfbea 1139 case wxITEM_NORMAL:
6d971354
RR
1140 {
1141#ifdef __WXGTK20__
1142 menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text ) );
1143#else
1144 menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) );
e31126cb 1145 label = GTK_LABEL( GTK_BIN(menuItem)->child );
6d971354
RR
1146#endif
1147 m_prevRadio = NULL;
d65c269b 1148 break;
6d971354 1149 }
d65c269b
VZ
1150 }
1151
6d971354 1152 }
9959e2b6 1153
6d971354
RR
1154 guint accel_key;
1155 GdkModifierType accel_mods;
98f29783 1156 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) );
9959e2b6 1157
98f29783 1158 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
6d971354
RR
1159 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
1160 if (accel_key != 0)
1161 {
9959e2b6
VZ
1162 gtk_widget_add_accelerator (GTK_WIDGET(menuItem),
1163 "activate",
6d971354 1164 m_accel,
9959e2b6 1165 accel_key,
6d971354
RR
1166 accel_mods,
1167 GTK_ACCEL_VISIBLE);
717a57c2 1168 }
9959e2b6 1169
e31126cb
RR
1170 if (pos == -1)
1171 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem);
1172 else
1173 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
1174
6d971354 1175 gtk_widget_show( menuItem );
23280650 1176
717a57c2
VZ
1177 if ( !mitem->IsSeparator() )
1178 {
2b5f62a0 1179 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
a01fe3d6 1180
717a57c2
VZ
1181 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1182 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1183 (gpointer)this );
837904f2 1184
717a57c2
VZ
1185 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1186 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1187 (gpointer)this );
e31126cb 1188
88d19775
MR
1189 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
1190 {
e31126cb
RR
1191 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
1192
1193 gtk_widget_show( mitem->GetSubMenu()->m_menu );
1194
1195 // if adding a submenu to a menu already existing in the menu bar, we
1196 // must set invoking window to allow processing events from this
1197 // submenu
1198 if ( m_invokingWindow )
1199 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
88d19775
MR
1200 }
1201 else
1202 {
e31126cb 1203 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
88d19775
MR
1204 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1205 (gpointer)this );
1206 }
1207
e31126cb
RR
1208#ifndef __WXGTK20__
1209 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
1210 if (accel_key != GDK_VoidSymbol)
1211 {
1212 gtk_widget_add_accelerator (menuItem,
88d19775
MR
1213 "activate_item",
1214 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)),
1215 accel_key,
1216 GDK_MOD1_MASK,
1217 GTK_ACCEL_LOCKED);
1218 }
e31126cb 1219#endif
717a57c2 1220 }
23280650 1221
837904f2 1222 mitem->SetMenuItem(menuItem);
837904f2 1223
6d971354 1224 if (ms_locked)
d65c269b 1225 {
6d971354
RR
1226 // This doesn't even exist!
1227 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
d65c269b 1228 }
d65c269b 1229
32db328c 1230 return TRUE;
6de97a3b 1231}
c801d85f 1232
9add9367 1233wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
828f655f 1234{
9add9367
RD
1235 if (!GtkAppend(mitem))
1236 return NULL;
9959e2b6 1237
9add9367 1238 return wxMenuBase::DoAppend(mitem);
c33c4050
RR
1239}
1240
9add9367 1241wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 1242{
717a57c2 1243 if ( !wxMenuBase::DoInsert(pos, item) )
9add9367 1244 return NULL;
c626a8b7 1245
6d971354 1246 // TODO
49826dab 1247 if ( !GtkAppend(item, (int)pos) )
9add9367 1248 return NULL;
32db328c 1249
9add9367 1250 return item;
c33c4050
RR
1251}
1252
717a57c2 1253wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 1254{
717a57c2
VZ
1255 if ( !wxMenuBase::DoRemove(item) )
1256 return (wxMenuItem *)NULL;
c626a8b7 1257
717a57c2
VZ
1258 // TODO: this code doesn't delete the item factory item and this seems
1259 // impossible as of GTK 1.2.6.
1260 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 1261
717a57c2 1262 return item;
c33c4050
RR
1263}
1264
96fd301f
VZ
1265int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1266{
222ed1d6 1267 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
83624f79
RR
1268 while (node)
1269 {
b1d4dd7a 1270 wxMenuItem *item = node->GetData();
83624f79 1271 if (item->GetMenuItem() == menuItem)
88d19775 1272 return item->GetId();
b1d4dd7a 1273 node = node->GetNext();
83624f79 1274 }
96fd301f 1275
c626a8b7 1276 return wxNOT_FOUND;
6de97a3b 1277}
c801d85f 1278
717a57c2
VZ
1279// ----------------------------------------------------------------------------
1280// helpers
1281// ----------------------------------------------------------------------------
1282
6d971354 1283#if wxUSE_ACCEL
a070d8ce 1284
98f29783 1285static wxString GetGtkHotKey( const wxMenuItem& item )
c801d85f 1286{
717a57c2
VZ
1287 wxString hotkey;
1288
1289 wxAcceleratorEntry *accel = item.GetAccel();
1290 if ( accel )
83624f79 1291 {
717a57c2
VZ
1292 int flags = accel->GetFlags();
1293 if ( flags & wxACCEL_ALT )
1294 hotkey += wxT("<alt>");
1295 if ( flags & wxACCEL_CTRL )
1296 hotkey += wxT("<control>");
1297 if ( flags & wxACCEL_SHIFT )
1298 hotkey += wxT("<shift>");
1299
1300 int code = accel->GetKeyCode();
1301 switch ( code )
c626a8b7 1302 {
717a57c2
VZ
1303 case WXK_F1:
1304 case WXK_F2:
1305 case WXK_F3:
1306 case WXK_F4:
1307 case WXK_F5:
1308 case WXK_F6:
1309 case WXK_F7:
1310 case WXK_F8:
1311 case WXK_F9:
1312 case WXK_F10:
1313 case WXK_F11:
1314 case WXK_F12:
bbcd4085
RR
1315 case WXK_F13:
1316 case WXK_F14:
1317 case WXK_F15:
1318 case WXK_F16:
1319 case WXK_F17:
1320 case WXK_F18:
1321 case WXK_F19:
1322 case WXK_F20:
1323 case WXK_F21:
1324 case WXK_F22:
1325 case WXK_F23:
1326 case WXK_F24:
04cc1e93 1327 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
717a57c2 1328 break;
2368dcda 1329
a070d8ce
VZ
1330 // TODO: we should use gdk_keyval_name() (a.k.a.
1331 // XKeysymToString) here as well as hardcoding the keysym
1332 // names this might be not portable
bbcd4085 1333 case WXK_INSERT:
3ca6a5f0
BP
1334 hotkey << wxT("Insert" );
1335 break;
1336 case WXK_DELETE:
1337 hotkey << wxT("Delete" );
1338 break;
2b5f62a0
VZ
1339 case WXK_UP:
1340 hotkey << wxT("Up" );
1341 break;
1342 case WXK_DOWN:
1343 hotkey << wxT("Down" );
1344 break;
1345 case WXK_PAGEUP:
dac3065d 1346 case WXK_PRIOR:
2b5f62a0
VZ
1347 hotkey << wxT("Prior" );
1348 break;
1349 case WXK_PAGEDOWN:
dac3065d 1350 case WXK_NEXT:
2b5f62a0
VZ
1351 hotkey << wxT("Next" );
1352 break;
1353 case WXK_LEFT:
1354 hotkey << wxT("Left" );
1355 break;
1356 case WXK_RIGHT:
1357 hotkey << wxT("Right" );
1358 break;
1359 case WXK_HOME:
1360 hotkey << wxT("Home" );
1361 break;
1362 case WXK_END:
1363 hotkey << wxT("End" );
1364 break;
1365 case WXK_RETURN:
1366 hotkey << wxT("Return" );
1367 break;
bbcd4085
RR
1368 case WXK_BACK:
1369 hotkey << wxT("BackSpace" );
1370 break;
1371 case WXK_TAB:
1372 hotkey << wxT("Tab" );
1373 break;
1374 case WXK_ESCAPE:
1375 hotkey << wxT("Esc" );
1376 break;
1377 case WXK_SPACE:
1378 hotkey << wxT("space" );
1379 break;
1380 case WXK_MULTIPLY:
1381 hotkey << wxT("Multiply" );
1382 break;
1383 case WXK_ADD:
1384 hotkey << wxT("Add" );
1385 break;
1386 case WXK_SEPARATOR:
1387 hotkey << wxT("Separator" );
1388 break;
1389 case WXK_SUBTRACT:
1390 hotkey << wxT("Subtract" );
1391 break;
1392 case WXK_DECIMAL:
1393 hotkey << wxT("Decimal" );
1394 break;
1395 case WXK_DIVIDE:
1396 hotkey << wxT("Divide" );
1397 break;
1398 case WXK_CANCEL:
1399 hotkey << wxT("Cancel" );
1400 break;
1401 case WXK_CLEAR:
1402 hotkey << wxT("Clear" );
1403 break;
1404 case WXK_MENU:
1405 hotkey << wxT("Menu" );
1406 break;
1407 case WXK_PAUSE:
1408 hotkey << wxT("Pause" );
1409 break;
1410 case WXK_CAPITAL:
1411 hotkey << wxT("Capital" );
1412 break;
1413 case WXK_SELECT:
1414 hotkey << wxT("Select" );
1415 break;
1416 case WXK_PRINT:
1417 hotkey << wxT("Print" );
1418 break;
1419 case WXK_EXECUTE:
1420 hotkey << wxT("Execute" );
1421 break;
1422 case WXK_SNAPSHOT:
1423 hotkey << wxT("Snapshot" );
1424 break;
1425 case WXK_HELP:
1426 hotkey << wxT("Help" );
1427 break;
1428 case WXK_NUMLOCK:
1429 hotkey << wxT("Num_Lock" );
1430 break;
1431 case WXK_SCROLL:
1432 hotkey << wxT("Scroll_Lock" );
1433 break;
1434 case WXK_NUMPAD_INSERT:
1435 hotkey << wxT("KP_Insert" );
1436 break;
1437 case WXK_NUMPAD_DELETE:
1438 hotkey << wxT("KP_Delete" );
1439 break;
1440 case WXK_NUMPAD_SPACE:
1441 hotkey << wxT("KP_Space" );
1442 break;
1443 case WXK_NUMPAD_TAB:
1444 hotkey << wxT("KP_Tab" );
1445 break;
1446 case WXK_NUMPAD_ENTER:
1447 hotkey << wxT("KP_Enter" );
1448 break;
1449 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1450 case WXK_NUMPAD_F4:
1451 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1452 break;
1453 case WXK_NUMPAD_HOME:
1454 hotkey << wxT("KP_Home" );
1455 break;
1456 case WXK_NUMPAD_LEFT:
1457 hotkey << wxT("KP_Left" );
1458 break;
1459 case WXK_NUMPAD_UP:
1460 hotkey << wxT("KP_Up" );
1461 break;
1462 case WXK_NUMPAD_RIGHT:
1463 hotkey << wxT("KP_Right" );
1464 break;
1465 case WXK_NUMPAD_DOWN:
1466 hotkey << wxT("KP_Down" );
1467 break;
1468 case WXK_NUMPAD_PRIOR: case WXK_NUMPAD_PAGEUP:
1469 hotkey << wxT("KP_Prior" );
1470 break;
1471 case WXK_NUMPAD_NEXT: case WXK_NUMPAD_PAGEDOWN:
1472 hotkey << wxT("KP_Next" );
1473 break;
1474 case WXK_NUMPAD_END:
1475 hotkey << wxT("KP_End" );
1476 break;
1477 case WXK_NUMPAD_BEGIN:
1478 hotkey << wxT("KP_Begin" );
1479 break;
1480 case WXK_NUMPAD_EQUAL:
1481 hotkey << wxT("KP_Equal" );
1482 break;
1483 case WXK_NUMPAD_MULTIPLY:
1484 hotkey << wxT("KP_Multiply" );
1485 break;
1486 case WXK_NUMPAD_ADD:
1487 hotkey << wxT("KP_Add" );
1488 break;
1489 case WXK_NUMPAD_SEPARATOR:
1490 hotkey << wxT("KP_Separator" );
1491 break;
1492 case WXK_NUMPAD_SUBTRACT:
1493 hotkey << wxT("KP_Subtract" );
1494 break;
1495 case WXK_NUMPAD_DECIMAL:
1496 hotkey << wxT("KP_Decimal" );
1497 break;
1498 case WXK_NUMPAD_DIVIDE:
1499 hotkey << wxT("KP_Divide" );
1500 break;
1501 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1502 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1503 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9:
1504 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1505 break;
1506 case WXK_WINDOWS_LEFT:
1507 hotkey << wxT("Super_L" );
1508 break;
1509 case WXK_WINDOWS_RIGHT:
1510 hotkey << wxT("Super_R" );
1511 break;
1512 case WXK_WINDOWS_MENU:
1513 hotkey << wxT("Menu" );
1514 break;
1515 case WXK_COMMAND:
1516 hotkey << wxT("Command" );
1517 break;
1518 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
88d19775
MR
1519 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1520 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1521 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1522 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
bbcd4085
RR
1523 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1524 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1525 break;
1526 */
a070d8ce
VZ
1527 // if there are any other keys wxGetAccelFromString() may
1528 // return, we should process them here
8bbe427f 1529
717a57c2 1530 default:
a070d8ce 1531 if ( code < 127 )
717a57c2 1532 {
2b5f62a0 1533 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
a070d8ce
VZ
1534 if ( name )
1535 {
1536 hotkey << name;
1537 break;
1538 }
717a57c2 1539 }
c801d85f 1540
717a57c2
VZ
1541 wxFAIL_MSG( wxT("unknown keyboard accel") );
1542 }
c801d85f 1543
717a57c2 1544 delete accel;
631f1bfe 1545 }
717a57c2
VZ
1546
1547 return hotkey;
631f1bfe 1548}
a070d8ce 1549
717a57c2 1550#endif // wxUSE_ACCEL
631f1bfe 1551
37d403aa 1552
cc47eed9
RR
1553//-----------------------------------------------------------------------------
1554// substitute for missing GtkPixmapMenuItem
1555//-----------------------------------------------------------------------------
37d403aa 1556
71628a57 1557#ifndef __WXGTK20__
9e691f46 1558
37d403aa
JS
1559/*
1560 * Copyright (C) 1998, 1999, 2000 Free Software Foundation
1561 * All rights reserved.
1562 *
1563 * This file is part of the Gnome Library.
1564 *
1565 * The Gnome Library is free software; you can redistribute it and/or
1566 * modify it under the terms of the GNU Library General Public License as
1567 * published by the Free Software Foundation; either version 2 of the
1568 * License, or (at your option) any later version.
1569 *
1570 * The Gnome Library is distributed in the hope that it will be useful,
1571 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1572 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1573 * Library General Public License for more details.
1574 *
1575 * You should have received a copy of the GNU Library General Public
1576 * License along with the Gnome Library; see the file COPYING.LIB. If not,
1577 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1578 * Boston, MA 02111-1307, USA.
1579 */
1580/*
1581 @NOTATION@
1582 */
1583
1584/* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */
1585
37d403aa
JS
1586#include <gtk/gtkaccellabel.h>
1587#include <gtk/gtksignal.h>
1588#include <gtk/gtkmenuitem.h>
1589#include <gtk/gtkmenu.h>
1590#include <gtk/gtkcontainer.h>
1591
90350682
VZ
1592extern "C"
1593{
1594
37d403aa
JS
1595static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass);
1596static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item);
1597static void gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1598 GdkRectangle *area);
37d403aa 1599static gint gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1600 GdkEventExpose *event);
37d403aa
JS
1601
1602/* we must override the following functions */
1603
1604static void gtk_pixmap_menu_item_map (GtkWidget *widget);
1605static void gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1606 GtkAllocation *allocation);
37d403aa 1607static void gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1608 gboolean include_internals,
1609 GtkCallback callback,
1610 gpointer callback_data);
37d403aa 1611static void gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1612 GtkRequisition *requisition);
37d403aa 1613static void gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1614 GtkWidget *child);
37d403aa
JS
1615
1616static void changed_have_pixmap_status (GtkPixmapMenuItem *menu_item);
1617
1618static GtkMenuItemClass *parent_class = NULL;
1619
865bb325 1620} // extern "C"
90350682 1621
37d403aa
JS
1622#define BORDER_SPACING 3
1623#define PMAP_WIDTH 20
1624
1625GtkType
1626gtk_pixmap_menu_item_get_type (void)
1627{
1628 static GtkType pixmap_menu_item_type = 0;
1629
1630 if (!pixmap_menu_item_type)
1631 {
1632 GtkTypeInfo pixmap_menu_item_info =
1633 {
90350682 1634 (char *)"GtkPixmapMenuItem",
37d403aa
JS
1635 sizeof (GtkPixmapMenuItem),
1636 sizeof (GtkPixmapMenuItemClass),
1637 (GtkClassInitFunc) gtk_pixmap_menu_item_class_init,
1638 (GtkObjectInitFunc) gtk_pixmap_menu_item_init,
1639 /* reserved_1 */ NULL,
1640 /* reserved_2 */ NULL,
1641 (GtkClassInitFunc) NULL,
1642 };
1643
2368dcda 1644 pixmap_menu_item_type = gtk_type_unique (gtk_menu_item_get_type (),
8f262dc5 1645 &pixmap_menu_item_info);
37d403aa
JS
1646 }
1647
1648 return pixmap_menu_item_type;
1649}
1650
865bb325
VZ
1651extern "C" {
1652
37d403aa
JS
1653/**
1654 * gtk_pixmap_menu_item_new
1655 *
2368dcda 1656 * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap()
37d403aa
JS
1657 * to set the pixmap wich is displayed at the left side.
1658 *
1659 * Returns:
1660 * &GtkWidget pointer to new menu item
1661 **/
1662
1663GtkWidget*
1664gtk_pixmap_menu_item_new (void)
1665{
1666 return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ()));
1667}
1668
1669static void
1670gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass)
1671{
1672 GtkObjectClass *object_class;
1673 GtkWidgetClass *widget_class;
1674 GtkMenuItemClass *menu_item_class;
1675 GtkContainerClass *container_class;
1676
1677 object_class = (GtkObjectClass*) klass;
1678 widget_class = (GtkWidgetClass*) klass;
1679 menu_item_class = (GtkMenuItemClass*) klass;
1680 container_class = (GtkContainerClass*) klass;
1681
1682 parent_class = (GtkMenuItemClass*) gtk_type_class (gtk_menu_item_get_type ());
1683
1684 widget_class->draw = gtk_pixmap_menu_item_draw;
1685 widget_class->expose_event = gtk_pixmap_menu_item_expose;
1686 widget_class->map = gtk_pixmap_menu_item_map;
1687 widget_class->size_allocate = gtk_pixmap_menu_item_size_allocate;
1688 widget_class->size_request = gtk_pixmap_menu_item_size_request;
1689
1690 container_class->forall = gtk_pixmap_menu_item_forall;
1691 container_class->remove = gtk_pixmap_menu_item_remove;
1692
1693 klass->orig_toggle_size = menu_item_class->toggle_size;
1694 klass->have_pixmap_count = 0;
1695}
1696
1697static void
1698gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item)
1699{
1700 GtkMenuItem *mi;
1701
1702 mi = GTK_MENU_ITEM (menu_item);
1703
1704 menu_item->pixmap = NULL;
1705}
1706
1707static void
1708gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1709 GdkRectangle *area)
37d403aa
JS
1710{
1711 g_return_if_fail (widget != NULL);
1712 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1713 g_return_if_fail (area != NULL);
1714
1715 if (GTK_WIDGET_CLASS (parent_class)->draw)
1716 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
1717
2368dcda 1718 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1719 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1720 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1721 }
1722}
1723
1724static gint
1725gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1726 GdkEventExpose *event)
37d403aa
JS
1727{
1728 g_return_val_if_fail (widget != NULL, FALSE);
1729 g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget), FALSE);
1730 g_return_val_if_fail (event != NULL, FALSE);
1731
1732 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
1733 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1734
2368dcda 1735 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1736 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1737 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1738 }
1739
1740 return FALSE;
1741}
1742
1743/**
1744 * gtk_pixmap_menu_item_set_pixmap
1745 * @menu_item: Pointer to the pixmap menu item
1746 * @pixmap: Pointer to a pixmap widget
1747 *
1748 * Set the pixmap of the menu item.
1749 *
1750 **/
1751
1752void
1753gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
8f262dc5 1754 GtkWidget *pixmap)
37d403aa
JS
1755{
1756 g_return_if_fail (menu_item != NULL);
1757 g_return_if_fail (pixmap != NULL);
1758 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item));
1759 g_return_if_fail (GTK_IS_WIDGET (pixmap));
1760 g_return_if_fail (menu_item->pixmap == NULL);
1761
1762 gtk_widget_set_parent (pixmap, GTK_WIDGET (menu_item));
1763 menu_item->pixmap = pixmap;
1764
1765 if (GTK_WIDGET_REALIZED (pixmap->parent) &&
1766 !GTK_WIDGET_REALIZED (pixmap))
1767 gtk_widget_realize (pixmap);
2368dcda
VZ
1768
1769 if (GTK_WIDGET_VISIBLE (pixmap->parent)) {
37d403aa 1770 if (GTK_WIDGET_MAPPED (pixmap->parent) &&
8f262dc5 1771 GTK_WIDGET_VISIBLE(pixmap) &&
37d403aa
JS
1772 !GTK_WIDGET_MAPPED (pixmap))
1773 gtk_widget_map (pixmap);
1774 }
1775
1776 changed_have_pixmap_status(menu_item);
2368dcda 1777
37d403aa
JS
1778 if (GTK_WIDGET_VISIBLE (pixmap) && GTK_WIDGET_VISIBLE (menu_item))
1779 gtk_widget_queue_resize (pixmap);
1780}
1781
1782static void
1783gtk_pixmap_menu_item_map (GtkWidget *widget)
1784{
1785 GtkPixmapMenuItem *menu_item;
1786
1787 g_return_if_fail (widget != NULL);
1788 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1789
1790 menu_item = GTK_PIXMAP_MENU_ITEM(widget);
1791
1792 GTK_WIDGET_CLASS(parent_class)->map(widget);
1793
1794 if (menu_item->pixmap &&
1795 GTK_WIDGET_VISIBLE (menu_item->pixmap) &&
1796 !GTK_WIDGET_MAPPED (menu_item->pixmap))
1797 gtk_widget_map (menu_item->pixmap);
1798}
1799
1800static void
1801gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1802 GtkAllocation *allocation)
37d403aa
JS
1803{
1804 GtkPixmapMenuItem *pmenu_item;
1805
1806 pmenu_item = GTK_PIXMAP_MENU_ITEM(widget);
1807
1808 if (pmenu_item->pixmap && GTK_WIDGET_VISIBLE(pmenu_item))
1809 {
1810 GtkAllocation child_allocation;
1811 int border_width;
1812
1813 border_width = GTK_CONTAINER (widget)->border_width;
1814
1815 child_allocation.width = pmenu_item->pixmap->requisition.width;
1816 child_allocation.height = pmenu_item->pixmap->requisition.height;
1817 child_allocation.x = border_width + BORDER_SPACING;
1818 child_allocation.y = (border_width + BORDER_SPACING
8f262dc5
VZ
1819 + (((allocation->height - child_allocation.height) - child_allocation.x)
1820 / 2)); /* center pixmaps vertically */
37d403aa
JS
1821 gtk_widget_size_allocate (pmenu_item->pixmap, &child_allocation);
1822 }
1823
1824 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
1825 GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation);
1826}
1827
1828static void
1829gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1830 gboolean include_internals,
1831 GtkCallback callback,
1832 gpointer callback_data)
37d403aa
JS
1833{
1834 GtkPixmapMenuItem *menu_item;
1835
1836 g_return_if_fail (container != NULL);
1837 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1838 g_return_if_fail (callback != NULL);
1839
1840 menu_item = GTK_PIXMAP_MENU_ITEM (container);
1841
1842 if (menu_item->pixmap)
1843 (* callback) (menu_item->pixmap, callback_data);
1844
1845 GTK_CONTAINER_CLASS(parent_class)->forall(container,include_internals,
8f262dc5 1846 callback,callback_data);
37d403aa
JS
1847}
1848
1849static void
1850gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1851 GtkRequisition *requisition)
37d403aa
JS
1852{
1853 GtkPixmapMenuItem *menu_item;
1854 GtkRequisition req = {0, 0};
1855
1856 g_return_if_fail (widget != NULL);
1857 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1858 g_return_if_fail (requisition != NULL);
1859
1860 GTK_WIDGET_CLASS(parent_class)->size_request(widget,requisition);
1861
1862 menu_item = GTK_PIXMAP_MENU_ITEM (widget);
2368dcda 1863
37d403aa
JS
1864 if (menu_item->pixmap)
1865 gtk_widget_size_request(menu_item->pixmap, &req);
1866
1867 requisition->height = MAX(req.height + GTK_CONTAINER(widget)->border_width + BORDER_SPACING, (unsigned int) requisition->height);
1868 requisition->width += (req.width + GTK_CONTAINER(widget)->border_width + BORDER_SPACING);
1869}
1870
1871static void
1872gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1873 GtkWidget *child)
37d403aa
JS
1874{
1875 GtkBin *bin;
1876 gboolean widget_was_visible;
1877
1878 g_return_if_fail (container != NULL);
1879 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1880 g_return_if_fail (child != NULL);
1881 g_return_if_fail (GTK_IS_WIDGET (child));
1882
1883 bin = GTK_BIN (container);
2368dcda 1884 g_return_if_fail ((bin->child == child ||
8f262dc5 1885 (GTK_PIXMAP_MENU_ITEM(container)->pixmap == child)));
37d403aa
JS
1886
1887 widget_was_visible = GTK_WIDGET_VISIBLE (child);
2368dcda 1888
37d403aa
JS
1889 gtk_widget_unparent (child);
1890 if (bin->child == child)
2368dcda 1891 bin->child = NULL;
37d403aa
JS
1892 else {
1893 GTK_PIXMAP_MENU_ITEM(container)->pixmap = NULL;
1894 changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container));
1895 }
2368dcda 1896
37d403aa
JS
1897 if (widget_was_visible)
1898 gtk_widget_queue_resize (GTK_WIDGET (container));
1899}
1900
1901
1902/* important to only call this if there was actually a _change_ in pixmap == NULL */
1903static void
1904changed_have_pixmap_status (GtkPixmapMenuItem *menu_item)
1905{
1906 if (menu_item->pixmap != NULL) {
1907 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count += 1;
1908
1909 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 1) {
1910 /* Install pixmap toggle size */
1911 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size, PMAP_WIDTH);
1912 }
1913 } else {
1914 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count -= 1;
1915
1916 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 0) {
1917 /* Install normal toggle size */
2368dcda 1918 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size;
37d403aa
JS
1919 }
1920 }
1921
1922 /* Note that we actually need to do this for _all_ GtkPixmapMenuItem
1923 whenever the klass->toggle_size changes; but by doing it anytime
1924 this function is called, we get the same effect, just because of
1925 how the preferences option to show pixmaps works. Bogus, broken.
1926 */
2368dcda 1927 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item)))
37d403aa
JS
1928 gtk_widget_queue_resize(GTK_WIDGET(menu_item));
1929}
1930
865bb325
VZ
1931} // extern "C"
1932
1933#endif // !__WXGTK20__
37d403aa 1934