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