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