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