]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Changed back inline/const things
[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 {
d9e403cc 148#ifdef __WXGTK12__
f6bcfd97
BP
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 324
d9e403cc 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 349
d9e403cc 350 // This doesn't have much effect right now.
1e133b7d 351 menu->SetTitle( str );
23280650 352
d9e403cc 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
d9e403cc 359 // local buffer in multibyte form
fab591c5
RR
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
d9e403cc
RR
370 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
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
d9e403cc 407 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0
VZ
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
75c116ab
JS
451static wxMenu *CopyMenu (wxMenu *menu)
452{
453 wxMenu *menucopy = new wxMenu ();
454 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
455 while (node)
456 {
457 wxMenuItem *item = node->GetData();
458 int itemid = item->GetId();
459 wxString text = item->GetText();
460 text.Replace(wxT("_"), wxT("&"));
461 wxMenu *submenu = item->GetSubMenu();
462 if (!submenu)
463 {
464 wxMenuItem* itemcopy = new wxMenuItem(menucopy,
465 itemid, text,
466 menu->GetHelpString(itemid));
467 itemcopy->SetBitmap(item->GetBitmap());
468 itemcopy->SetCheckable(item->IsCheckable());
469 menucopy->Append(itemcopy);
470 }
471 else
472 menucopy->Append (itemid, text, CopyMenu(submenu),
473 menu->GetHelpString(itemid));
474
475 node = node->GetNext();
476 }
477
478 return menucopy;
479}
480
3dfac970
VZ
481wxMenu *wxMenuBar::Remove(size_t pos)
482{
f03ec224
VZ
483 wxMenu *menu = wxMenuBarBase::Remove(pos);
484 if ( !menu )
1d62a8b4 485 return (wxMenu*) NULL;
f03ec224 486
589c9fff 487/*
c4608a8a 488 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
589c9fff 489
1d62a8b4
RR
490 printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
491 printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
589c9fff 492*/
c4608a8a 493
75c116ab
JS
494 wxMenu *menucopy = CopyMenu( menu );
495
1d62a8b4
RR
496 // unparent calls unref() and that would delete the widget so we raise
497 // the ref count to 2 artificially before invoking unparent.
498 gtk_widget_ref( menu->m_menu );
499 gtk_widget_unparent( menu->m_menu );
c4608a8a 500
1d62a8b4 501 gtk_widget_destroy( menu->m_owner );
75c116ab 502 delete menu;
c4608a8a 503
75c116ab 504 menu = menucopy;
589c9fff 505/*
1d62a8b4
RR
506 printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
507 printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
589c9fff 508*/
c4608a8a 509
2b5f62a0
VZ
510 if (m_invokingWindow)
511 {
512 // OPTIMISE ME: see comment in GtkAppend
513
d9e403cc 514 wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame );
2b5f62a0 515
d9e403cc 516 if( frame )
2b5f62a0
VZ
517 frame->UpdateMenuBarSize();
518 }
519
1d62a8b4 520 return menu;
6de97a3b 521}
96fd301f 522
716b7364 523static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 524{
f6bcfd97 525 if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
83624f79
RR
526 {
527 int res = menu->FindItem( itemString );
c626a8b7
VZ
528 if (res != wxNOT_FOUND)
529 return res;
83624f79 530 }
c626a8b7 531
1987af7e 532 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
83624f79
RR
533 while (node)
534 {
1987af7e 535 wxMenuItem *item = node->GetData();
83624f79
RR
536 if (item->IsSubMenu())
537 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 538
1987af7e 539 node = node->GetNext();
83624f79
RR
540 }
541
c626a8b7
VZ
542 return wxNOT_FOUND;
543}
544
c801d85f
KB
545int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
546{
1987af7e 547 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
548 while (node)
549 {
1987af7e 550 wxMenu *menu = node->GetData();
83624f79 551 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
552 if (res != -1)
553 return res;
554 node = node->GetNext();
83624f79 555 }
1987af7e
VZ
556
557 return wxNOT_FOUND;
6de97a3b 558}
c801d85f 559
c626a8b7 560// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 561static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 562{
717a57c2 563 wxMenuItem* result = menu->FindChildItem(id);
716b7364 564
1987af7e 565 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
c626a8b7 566 while ( node && result == NULL )
83624f79 567 {
1987af7e 568 wxMenuItem *item = node->GetData();
83624f79 569 if (item->IsSubMenu())
c626a8b7 570 {
83624f79 571 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 572 }
1987af7e 573 node = node->GetNext();
83624f79 574 }
96fd301f 575
83624f79 576 return result;
6de97a3b 577}
716b7364 578
3dfac970 579wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 580{
83624f79 581 wxMenuItem* result = 0;
1987af7e 582 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
583 while (node && result == 0)
584 {
1987af7e 585 wxMenu *menu = node->GetData();
83624f79 586 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 587 node = node->GetNext();
83624f79 588 }
c626a8b7 589
3dfac970
VZ
590 if ( menuForItem )
591 {
592 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
593 }
c626a8b7 594
3dfac970 595 return result;
bbe0af5b
RR
596}
597
3dfac970 598void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 599{
3dfac970 600 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 601
223d09f6 602 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 603
3dfac970 604 wxMenu* menu = node->GetData();
c626a8b7
VZ
605
606 if (menu->m_owner)
607 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
608}
609
3dfac970 610wxString wxMenuBar::GetLabelTop( size_t pos ) const
bbe0af5b 611{
3dfac970 612 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 613
223d09f6 614 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 615
3dfac970 616 wxMenu* menu = node->GetData();
c626a8b7 617
f6bcfd97
BP
618 wxString label;
619 wxString text( menu->GetTitle() );
f6bcfd97
BP
620 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
621 {
2b5f62a0 622 if ( *pc == wxT('_') )
f6bcfd97 623 {
2b5f62a0 624 // '_' is the escape character for GTK+
f6bcfd97
BP
625 continue;
626 }
627
7cf4f7e2
GD
628 // don't remove ampersands '&' since if we have them in the menu title
629 // it means that they were doubled to indicate "&" instead of accelerator
630
f6bcfd97
BP
631 label += *pc;
632 }
f6bcfd97
BP
633
634 return label;
bbe0af5b
RR
635}
636
3dfac970 637void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
bbe0af5b 638{
3dfac970 639 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 640
223d09f6 641 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 642
3dfac970 643 wxMenu* menu = node->GetData();
c626a8b7 644
f6bcfd97
BP
645 wxString str( wxReplaceUnderscore( label ) );
646
647 menu->SetTitle( str );
648
649 if (menu->m_owner)
650 {
651 GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
652
653 /* set new text */
fab591c5 654 gtk_label_set( label, wxGTK_CONV( str ) );
f6bcfd97
BP
655
656 /* reparse key accel */
fab591c5 657 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) );
f6bcfd97
BP
658 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
659 }
660
bbe0af5b
RR
661}
662
c801d85f 663//-----------------------------------------------------------------------------
cf7a7e13 664// "activate"
c801d85f
KB
665//-----------------------------------------------------------------------------
666
6de97a3b 667static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 668{
1e6feb95
VZ
669 if (g_isIdle)
670 wxapp_install_idle_handler();
c4608a8a 671
83624f79 672 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 673
83624f79 674 /* should find it for normal (not popup) menu */
1e6feb95
VZ
675 wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
676 _T("menu item not found in gtk_menu_clicked_callback") );
96fd301f 677
c626a8b7
VZ
678 if (!menu->IsEnabled(id))
679 return;
96fd301f 680
717a57c2 681 wxMenuItem* item = menu->FindChildItem( id );
223d09f6 682 wxCHECK_RET( item, wxT("error in menu item callback") );
c626a8b7
VZ
683
684 if (item->IsCheckable())
2d17d68f 685 {
705dd80a
VZ
686 bool isReallyChecked = item->IsChecked(),
687 isInternallyChecked = item->wxMenuItemBase::IsChecked();
688
689 // ensure that the internal state is always consistent with what is
690 // shown on the screen
691 item->wxMenuItemBase::Check(isReallyChecked);
692
693 // we must not report the events for the radio button going up nor the
694 // events resulting from the calls to wxMenuItem::Check()
695 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
696 (isInternallyChecked == isReallyChecked) )
2d17d68f 697 {
2d17d68f 698 return;
c626a8b7 699 }
705dd80a
VZ
700
701 // the user pressed on the menu item: report the event below
2d17d68f
RR
702 }
703
705dd80a 704 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
cf7a7e13
RR
705}
706
707//-----------------------------------------------------------------------------
708// "select"
709//-----------------------------------------------------------------------------
710
711static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
712{
acfd422a
RR
713 if (g_isIdle) wxapp_install_idle_handler();
714
83624f79
RR
715 int id = menu->FindMenuIdByMenuItem(widget);
716
717 wxASSERT( id != -1 ); // should find it!
cf7a7e13 718
c626a8b7
VZ
719 if (!menu->IsEnabled(id))
720 return;
cf7a7e13 721
342b6a2f 722 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 723 event.SetEventObject( menu );
cf7a7e13 724
c626a8b7
VZ
725 if (menu->GetEventHandler()->ProcessEvent(event))
726 return;
6de97a3b 727
83624f79
RR
728 wxWindow *win = menu->GetInvokingWindow();
729 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 730}
c801d85f 731
cd743a6f
RR
732//-----------------------------------------------------------------------------
733// "deselect"
734//-----------------------------------------------------------------------------
735
736static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
737{
acfd422a
RR
738 if (g_isIdle) wxapp_install_idle_handler();
739
cd743a6f
RR
740 int id = menu->FindMenuIdByMenuItem(widget);
741
742 wxASSERT( id != -1 ); // should find it!
743
c626a8b7
VZ
744 if (!menu->IsEnabled(id))
745 return;
cd743a6f
RR
746
747 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
748 event.SetEventObject( menu );
749
c626a8b7
VZ
750 if (menu->GetEventHandler()->ProcessEvent(event))
751 return;
cd743a6f
RR
752
753 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
754 if (win)
755 win->GetEventHandler()->ProcessEvent( event );
cd743a6f
RR
756}
757
cf7a7e13 758//-----------------------------------------------------------------------------
db1b4961 759// wxMenuItem
cf7a7e13
RR
760//-----------------------------------------------------------------------------
761
7dbf5360 762IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
974e8d94
VZ
763
764wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
765 int id,
766 const wxString& name,
767 const wxString& help,
d65c269b 768 wxItemKind kind,
974e8d94
VZ
769 wxMenu *subMenu)
770{
d65c269b 771 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 772}
96fd301f 773
974e8d94
VZ
774wxMenuItem::wxMenuItem(wxMenu *parentMenu,
775 int id,
776 const wxString& text,
777 const wxString& help,
d65c269b 778 wxItemKind kind,
974e8d94 779 wxMenu *subMenu)
d65c269b 780 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
2368dcda 781{
092f7536 782 Init(text);
2368dcda
VZ
783}
784
785wxMenuItem::wxMenuItem(wxMenu *parentMenu,
786 int id,
787 const wxString& text,
788 const wxString& help,
789 bool isCheckable,
790 wxMenu *subMenu)
791 : wxMenuItemBase(parentMenu, id, text, help,
792 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
793{
092f7536 794 Init(text);
2368dcda
VZ
795}
796
092f7536 797void wxMenuItem::Init(const wxString& text)
c801d85f 798{
37d403aa 799 m_labelWidget = (GtkWidget *) NULL;
83624f79 800 m_menuItem = (GtkWidget *) NULL;
974e8d94 801
092f7536 802 DoSetText(text);
6de97a3b 803}
c801d85f 804
d1b15f03
RR
805wxMenuItem::~wxMenuItem()
806{
807 // don't delete menu items, the menus take care of that
808}
809
717a57c2 810// return the menu item text without any menu accels
3b59cdbf
VZ
811/* static */
812wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2
VZ
813{
814 wxString label;
2368dcda 815
3b59cdbf 816 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
717a57c2 817 {
7cf4f7e2 818 if ( *pc == wxT('_') )
717a57c2 819 {
2b5f62a0 820 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
d76419bd
RR
821 pc++;
822 label += *pc;
823 continue;
824 }
2368dcda 825
2b5f62a0
VZ
826#if GTK_CHECK_VERSION(2, 0, 0)
827 if ( *pc == wxT('\\') )
d76419bd 828 {
2b5f62a0
VZ
829 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
830 pc++;
831 label += *pc;
717a57c2
VZ
832 continue;
833 }
2b5f62a0 834#endif
717a57c2 835
7cf4f7e2
GD
836 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
837 {
838 // wxMSW escapes "&"
839 // "&" is doubled to indicate "&" instead of accelerator
840 continue;
841 }
842
717a57c2
VZ
843 label += *pc;
844 }
d9e403cc
RR
845
846 // wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() );
847
717a57c2
VZ
848 return label;
849}
850
851void wxMenuItem::SetText( const wxString& str )
852{
5869f93f
JS
853 // Some optimization to avoid flicker
854 wxString oldLabel = m_text;
855 oldLabel = wxStripMenuCodes(oldLabel.BeforeFirst('\t'));
856 oldLabel.Replace(wxT("_"), wxT(""));
857 wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t'));
858 if (oldLabel == label1)
859 return;
860
717a57c2 861 DoSetText(str);
354aa1e3
RR
862
863 if (m_menuItem)
864 {
37d403aa
JS
865 GtkLabel *label;
866 if (m_labelWidget)
2b5f62a0 867 label = (GtkLabel*) m_labelWidget;
37d403aa 868 else
2b5f62a0 869 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2 870
2b5f62a0
VZ
871#if GTK_CHECK_VERSION(2, 0, 0)
872 // We have to imitate item_factory_unescape_label here
873 wxString tmp;
874 for (size_t n = 0; n < m_text.Len(); n++)
875 {
876 if (m_text[n] != wxT('\\'))
877 tmp += m_text[n];
878 }
879
880 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) );
881#else
882 // set new text
fab591c5 883 gtk_label_set( label, wxGTK_CONV( m_text ) );
717a57c2 884
2b5f62a0
VZ
885 // reparse key accel
886 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
354aa1e3 887 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
2b5f62a0 888#endif
354aa1e3
RR
889 }
890}
891
c626a8b7 892// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 893void wxMenuItem::DoSetText( const wxString& str )
716b7364 894{
2b5f62a0 895 // '\t' is the deliminator indicating a hot key
974e8d94 896 m_text.Empty();
ab46dc18 897 const wxChar *pc = str;
2b5f62a0 898 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
83624f79 899 {
2b5f62a0 900 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
23280650 901 {
2b5f62a0
VZ
902 // "&" is doubled to indicate "&" instead of accelerator
903 ++pc;
904 m_text << wxT('&');
572d7461 905 }
2b5f62a0 906 else if (*pc == wxT('&'))
572d7461 907 {
2b5f62a0 908 m_text << wxT('_');
572d7461 909 }
2b5f62a0
VZ
910#if GTK_CHECK_VERSION(2, 0, 0)
911 else if ( *pc == wxT('_') ) // escape underscores
4e9cbd33 912 {
2b5f62a0 913 // m_text << wxT("__"); doesn't work
d9e403cc 914 m_text << wxT("__");
4e9cbd33 915 }
4e9cbd33
VS
916 else if (*pc == wxT('/')) // we have to escape slashes
917 {
918 m_text << wxT("\\/");
919 }
920 else if (*pc == wxT('\\')) // we have to double backslashes
921 {
922 m_text << wxT("\\\\");
923 }
2b5f62a0
VZ
924#else
925 else if ( *pc == wxT('_') ) // escape underscores
926 {
927 m_text << wxT("__");
928 }
223d09f6 929 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
23280650 930 {
223d09f6 931 m_text << wxT('\\'); /* ... and replace them with back slashes */
034be888 932 }
4e9cbd33 933#endif
2b5f62a0 934 else {
354aa1e3 935 m_text << *pc;
2b5f62a0
VZ
936 }
937 ++pc;
83624f79 938 }
2b5f62a0 939
d9e403cc
RR
940 // wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() );
941
223d09f6 942 m_hotKey = wxT("");
9e691f46 943
223d09f6 944 if(*pc == wxT('\t'))
d7dbc98a
KB
945 {
946 pc++;
947 m_hotKey = pc;
948 }
716b7364
RR
949}
950
717a57c2
VZ
951#if wxUSE_ACCEL
952
953wxAcceleratorEntry *wxMenuItem::GetAccel() const
954{
1987af7e 955 if ( !GetHotKey() )
717a57c2
VZ
956 {
957 // nothing
958 return (wxAcceleratorEntry *)NULL;
959 }
960
961 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
962 wxString label;
1987af7e 963 label << wxT('\t') << GetHotKey();
717a57c2
VZ
964
965 return wxGetAccelFromString(label);
966}
967
968#endif // wxUSE_ACCEL
969
96fd301f 970void wxMenuItem::Check( bool check )
716b7364 971{
223d09f6 972 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 973
974e8d94
VZ
974 if (check == m_isChecked)
975 return;
2d17d68f 976
974e8d94 977 wxMenuItemBase::Check( check );
0472ece7 978
24bcaec3 979 switch ( GetKind() )
0472ece7 980 {
24bcaec3
VZ
981 case wxITEM_CHECK:
982 case wxITEM_RADIO:
983 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
984 break;
985
986 default:
987 wxFAIL_MSG( _T("can't check this item") );
0472ece7 988 }
716b7364
RR
989}
990
8bbe427f
VZ
991void wxMenuItem::Enable( bool enable )
992{
223d09f6 993 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 994
83624f79 995 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 996 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
997}
998
96fd301f 999bool wxMenuItem::IsChecked() const
716b7364 1000{
223d09f6 1001 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
db1b4961 1002
974e8d94
VZ
1003 wxCHECK_MSG( IsCheckable(), FALSE,
1004 wxT("can't get state of uncheckable item!") );
96fd301f 1005
974e8d94 1006 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
1007}
1008
354aa1e3
RR
1009wxString wxMenuItem::GetFactoryPath() const
1010{
d9e403cc
RR
1011 // In order to get the pointer to the item we need the item
1012 // text _without_ underscores in GTK 1.2
354aa1e3 1013 wxString path( wxT("<main>/") );
717a57c2 1014
d76419bd
RR
1015 for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
1016 {
2b5f62a0 1017 if ( *pc == wxT('_') )
d76419bd 1018 {
d9e403cc
RR
1019#ifdef __WXGTK20__
1020 pc++;
1021#else
2b5f62a0 1022 // remove '_' unconditionally
d76419bd 1023 continue;
d9e403cc 1024#endif
d76419bd 1025 }
2368dcda 1026
7cf4f7e2
GD
1027 // don't remove ampersands '&' since if we have them in the menu item title
1028 // it means that they were doubled to indicate "&" instead of accelerator
1029
d76419bd
RR
1030 path += *pc;
1031 }
2368dcda 1032
354aa1e3
RR
1033 return path;
1034}
1035
db1b4961 1036//-----------------------------------------------------------------------------
83624f79 1037// wxMenu
db1b4961
RR
1038//-----------------------------------------------------------------------------
1039
c801d85f
KB
1040IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
1041
717a57c2 1042void wxMenu::Init()
c801d85f 1043{
034be888
RR
1044 m_accel = gtk_accel_group_new();
1045 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
1046 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
8bbe427f 1047
2b1c162e 1048 m_owner = (GtkWidget*) NULL;
2b2edbed 1049
d9e403cc
RR
1050 // Tearoffs are entries, just like separators. So if we want this
1051 // menu to be a tear-off one, we just append a tearoff entry
1052 // immediately.
2b2edbed
KB
1053 if(m_style & wxMENU_TEAROFF)
1054 {
1055 GtkItemFactoryEntry entry;
90350682 1056 entry.path = (char *)"/tearoff";
2b2edbed
KB
1057 entry.callback = (GtkItemFactoryCallback) NULL;
1058 entry.callback_action = 0;
90350682 1059 entry.item_type = (char *)"<Tearoff>";
2b2edbed 1060 entry.accelerator = (gchar*) NULL;
d9e403cc 1061 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
23280650 1062 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed 1063 }
c801d85f 1064
717a57c2
VZ
1065 // append the title as the very first entry if we have it
1066 if ( !!m_title )
d1b15f03 1067 {
717a57c2
VZ
1068 Append(-2, m_title);
1069 AppendSeparator();
d1b15f03 1070 }
717a57c2 1071}
15a2076a 1072
717a57c2
VZ
1073wxMenu::~wxMenu()
1074{
a583e623 1075 m_items.Clear();
f03ec224 1076
d1b15f03 1077 gtk_widget_destroy( m_menu );
974e8d94 1078
d1b15f03 1079 gtk_object_unref( GTK_OBJECT(m_factory) );
c2dd8380
GL
1080}
1081
32db328c 1082bool wxMenu::GtkAppend(wxMenuItem *mitem)
c2dd8380 1083{
717a57c2 1084 GtkWidget *menuItem;
96fd301f 1085
9e691f46 1086#if defined(USE_MENU_BITMAPS) || !GTK_CHECK_VERSION(1, 2, 0)
37d403aa 1087 bool appended = FALSE;
9e691f46 1088#endif
37d403aa 1089
24bcaec3
VZ
1090 // does this item terminate the current radio group?
1091 bool endOfRadioGroup = TRUE;
d65c269b 1092
717a57c2
VZ
1093 if ( mitem->IsSeparator() )
1094 {
717a57c2 1095 GtkItemFactoryEntry entry;
90350682 1096 entry.path = (char *)"/sep";
717a57c2
VZ
1097 entry.callback = (GtkItemFactoryCallback) NULL;
1098 entry.callback_action = 0;
90350682 1099 entry.item_type = (char *)"<Separator>";
717a57c2
VZ
1100 entry.accelerator = (gchar*) NULL;
1101
d9e403cc 1102 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
717a57c2 1103
d9e403cc 1104 // this will be wrong for more than one separator. do we care?
717a57c2 1105 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
24bcaec3
VZ
1106
1107 // we might have a separator inside a radio group
1108 endOfRadioGroup = FALSE;
717a57c2
VZ
1109 }
1110 else if ( mitem->IsSubMenu() )
837904f2 1111 {
d9e403cc 1112 // text has "_" instead of "&" after mitem->SetText()
717a57c2 1113 wxString text( mitem->GetText() );
974e8d94 1114
d9e403cc 1115 // local buffer in multibyte form
717a57c2
VZ
1116 char buf[200];
1117 strcpy( buf, "/" );
fab591c5 1118 strcat( buf, wxGTK_CONV( text ) );
50592885 1119
717a57c2
VZ
1120 GtkItemFactoryEntry entry;
1121 entry.path = buf;
1122 entry.callback = (GtkItemFactoryCallback) 0;
1123 entry.callback_action = 0;
90350682 1124 entry.item_type = (char *)"<Branch>";
a583e623 1125 entry.accelerator = (gchar*) NULL;
50592885 1126
d9e403cc 1127 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
50592885 1128
717a57c2 1129 wxString path( mitem->GetFactoryPath() );
fab591c5 1130 menuItem = gtk_item_factory_get_item( m_factory, wxGTK_CONV( path ) );
50592885 1131
1987af7e 1132 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
e7200790
VZ
1133
1134 // if adding a submenu to a menu already existing in the menu bar, we
1135 // must set invoking window to allow processing events from this
1136 // submenu
1137 if ( m_invokingWindow )
1138 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
837904f2 1139 }
9e691f46 1140#ifdef USE_MENU_BITMAPS
37d403aa
JS
1141 else if (mitem->GetBitmap().Ok()) // An item with bitmap
1142 {
cc47eed9
RR
1143 wxString text( mitem->GetText() );
1144 const wxBitmap *bitmap = &mitem->GetBitmap();
2368dcda 1145
cc47eed9 1146 menuItem = gtk_pixmap_menu_item_new ();
fab591c5 1147 GtkWidget *label = gtk_accel_label_new ( wxGTK_CONV( text ) );
cc47eed9
RR
1148 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1149 gtk_container_add (GTK_CONTAINER (menuItem), label);
fab591c5 1150 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
cc47eed9
RR
1151 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuItem);
1152 if (accel_key != GDK_VoidSymbol)
2b5f62a0 1153 {
cc47eed9 1154 gtk_widget_add_accelerator (menuItem,
8f262dc5
VZ
1155 "activate_item",
1156 gtk_menu_ensure_uline_accel_group (GTK_MENU (m_menu)),
1157 accel_key, 0,
1158 GTK_ACCEL_LOCKED);
cc47eed9
RR
1159 }
1160 gtk_widget_show (label);
37d403aa 1161
cc47eed9 1162 mitem->SetLabelWidget(label);
37d403aa 1163
cc47eed9
RR
1164 GtkWidget* pixmap = gtk_pixmap_new( bitmap->GetPixmap(), bitmap->GetMask() ? bitmap->GetMask()->GetBitmap() : (GdkBitmap* )NULL);
1165 gtk_widget_show(pixmap);
1166 gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem ), pixmap);
37d403aa
JS
1167
1168 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
1169 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1170 (gpointer)this );
2b5f62a0 1171
37d403aa
JS
1172 gtk_menu_append( GTK_MENU(m_menu), menuItem );
1173 gtk_widget_show( menuItem );
1174
37d403aa
JS
1175 appended = TRUE; // We've done this, don't do it again
1176 }
9e691f46 1177#endif // USE_MENU_BITMAPS
717a57c2
VZ
1178 else // a normal item
1179 {
982f23fd 1180 // text has "_" instead of "&" after mitem->SetText() so don't use it
717a57c2
VZ
1181 wxString text( mitem->GetText() );
1182
8f262dc5
VZ
1183 // buffers containing the menu item path and type in multibyte form
1184 char bufPath[256],
1185 bufType[256];
1186
1187 strcpy( bufPath, "/" );
1188 strncat( bufPath, wxGTK_CONV(text), WXSIZEOF(bufPath) - 2 );
1189 bufPath[WXSIZEOF(bufPath) - 1] = '\0';
717a57c2
VZ
1190
1191 GtkItemFactoryEntry entry;
8f262dc5 1192 entry.path = bufPath;
717a57c2
VZ
1193 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
1194 entry.callback_action = 0;
d65c269b
VZ
1195
1196 wxString pathRadio;
1197 const char *item_type;
1198 switch ( mitem->GetKind() )
1199 {
546bfbea 1200 case wxITEM_CHECK:
d65c269b
VZ
1201 item_type = "<CheckItem>";
1202 break;
1203
546bfbea 1204 case wxITEM_RADIO:
d65c269b
VZ
1205 if ( m_pathLastRadio.empty() )
1206 {
1207 // start of a new radio group
1208 item_type = "<RadioItem>";
2b5f62a0
VZ
1209 wxString tmp( wxGTK_CONV_BACK( bufPath ) );
1210 tmp.Remove(0,1);
1211 m_pathLastRadio = tmp;
d65c269b
VZ
1212 }
1213 else // continue the radio group
1214 {
1215 pathRadio = m_pathLastRadio;
fab591c5
RR
1216 pathRadio.Replace(wxT("_"), wxT(""));
1217 pathRadio.Prepend(wxT("<main>/"));
982f23fd 1218
8f262dc5
VZ
1219 strncpy(bufType, wxGTK_CONV(pathRadio), WXSIZEOF(bufType));
1220 bufType[WXSIZEOF(bufType) - 1] = '\0';
1221 item_type = bufType;
d65c269b
VZ
1222 }
1223
24bcaec3
VZ
1224 // continue the existing radio group, if any
1225 endOfRadioGroup = FALSE;
d65c269b
VZ
1226 break;
1227
1228 default:
1229 wxFAIL_MSG( _T("unexpected menu item kind") );
1230 // fall through
1231
546bfbea 1232 case wxITEM_NORMAL:
d65c269b
VZ
1233 item_type = "<Item>";
1234 break;
1235 }
1236
1237 entry.item_type = (char *)item_type; // cast needed for GTK+
a583e623 1238 entry.accelerator = (gchar*) NULL;
23280650 1239
974e8d94 1240#if wxUSE_ACCEL
717a57c2
VZ
1241 // due to an apparent bug in GTK+, we have to use a static buffer here -
1242 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
1243 // somehow! (VZ)
982f23fd 1244 char s_accel[50]; // should be big enough, we check for overruns
3ca6a5f0 1245 wxString tmp( GetHotKey(*mitem) );
fab591c5 1246 strncpy(s_accel, wxGTK_CONV( tmp ), WXSIZEOF(s_accel));
982f23fd 1247 s_accel[WXSIZEOF(s_accel) - 1] = '\0';
717a57c2
VZ
1248 entry.accelerator = s_accel;
1249#else // !wxUSE_ACCEL
1250 entry.accelerator = (char*) NULL;
1251#endif // wxUSE_ACCEL/!wxUSE_ACCEL
96fd301f 1252
717a57c2 1253 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
cf7a7e13 1254
717a57c2 1255 wxString path( mitem->GetFactoryPath() );
fab591c5 1256 menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) );
2b5f62a0
VZ
1257
1258 if (!menuItem)
1259 wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() );
717a57c2 1260 }
23280650 1261
717a57c2
VZ
1262 if ( !mitem->IsSeparator() )
1263 {
2b5f62a0
VZ
1264 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
1265
717a57c2
VZ
1266 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1267 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1268 (gpointer)this );
837904f2 1269
717a57c2
VZ
1270 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1271 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1272 (gpointer)this );
1273 }
23280650 1274
837904f2 1275 mitem->SetMenuItem(menuItem);
837904f2 1276
24bcaec3 1277 if ( endOfRadioGroup )
d65c269b
VZ
1278 {
1279 m_pathLastRadio.clear();
1280 }
d65c269b 1281
32db328c 1282 return TRUE;
6de97a3b 1283}
c801d85f 1284
32db328c 1285bool wxMenu::DoAppend(wxMenuItem *mitem)
828f655f 1286{
32db328c 1287 return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem);
c33c4050
RR
1288}
1289
717a57c2 1290bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 1291{
717a57c2
VZ
1292 if ( !wxMenuBase::DoInsert(pos, item) )
1293 return FALSE;
c626a8b7 1294
32db328c
VZ
1295 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
1296 // of version 1.2.6), so we first append the item and then change its
1297 // index
1298 if ( !GtkAppend(item) )
1299 return FALSE;
1300
f6bcfd97
BP
1301 if ( m_style & wxMENU_TEAROFF )
1302 {
1303 // change the position as the first item is the tear-off marker
1304 pos++;
1305 }
1306
32db328c
VZ
1307 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
1308 gpointer data = g_list_last(menu_shell->children)->data;
1309 menu_shell->children = g_list_remove(menu_shell->children, data);
1310 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
1311
1312 return TRUE;
c33c4050
RR
1313}
1314
717a57c2 1315wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 1316{
717a57c2
VZ
1317 if ( !wxMenuBase::DoRemove(item) )
1318 return (wxMenuItem *)NULL;
c626a8b7 1319
717a57c2
VZ
1320 // TODO: this code doesn't delete the item factory item and this seems
1321 // impossible as of GTK 1.2.6.
1322 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 1323
717a57c2 1324 return item;
c33c4050
RR
1325}
1326
96fd301f
VZ
1327int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1328{
b1d4dd7a 1329 wxMenuItemList::Node *node = m_items.GetFirst();
83624f79
RR
1330 while (node)
1331 {
b1d4dd7a 1332 wxMenuItem *item = node->GetData();
83624f79
RR
1333 if (item->GetMenuItem() == menuItem)
1334 return item->GetId();
b1d4dd7a 1335 node = node->GetNext();
83624f79 1336 }
96fd301f 1337
c626a8b7 1338 return wxNOT_FOUND;
6de97a3b 1339}
c801d85f 1340
717a57c2
VZ
1341// ----------------------------------------------------------------------------
1342// helpers
1343// ----------------------------------------------------------------------------
1344
9e691f46 1345#if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL
a070d8ce 1346
717a57c2 1347static wxString GetHotKey( const wxMenuItem& item )
c801d85f 1348{
717a57c2
VZ
1349 wxString hotkey;
1350
1351 wxAcceleratorEntry *accel = item.GetAccel();
1352 if ( accel )
83624f79 1353 {
717a57c2
VZ
1354 int flags = accel->GetFlags();
1355 if ( flags & wxACCEL_ALT )
1356 hotkey += wxT("<alt>");
1357 if ( flags & wxACCEL_CTRL )
1358 hotkey += wxT("<control>");
1359 if ( flags & wxACCEL_SHIFT )
1360 hotkey += wxT("<shift>");
1361
1362 int code = accel->GetKeyCode();
1363 switch ( code )
c626a8b7 1364 {
717a57c2
VZ
1365 case WXK_F1:
1366 case WXK_F2:
1367 case WXK_F3:
1368 case WXK_F4:
1369 case WXK_F5:
1370 case WXK_F6:
1371 case WXK_F7:
1372 case WXK_F8:
1373 case WXK_F9:
1374 case WXK_F10:
1375 case WXK_F11:
1376 case WXK_F12:
1377 hotkey << wxT('F') << code - WXK_F1 + 1;
1378 break;
2368dcda 1379
a070d8ce
VZ
1380 // TODO: we should use gdk_keyval_name() (a.k.a.
1381 // XKeysymToString) here as well as hardcoding the keysym
1382 // names this might be not portable
3ca6a5f0
BP
1383 case WXK_NUMPAD_INSERT:
1384 hotkey << wxT("KP_Insert" );
1385 break;
1386 case WXK_NUMPAD_DELETE:
1387 hotkey << wxT("KP_Delete" );
1388 break;
1389 case WXK_INSERT:
1390 hotkey << wxT("Insert" );
1391 break;
1392 case WXK_DELETE:
1393 hotkey << wxT("Delete" );
1394 break;
2b5f62a0
VZ
1395 case WXK_UP:
1396 hotkey << wxT("Up" );
1397 break;
1398 case WXK_DOWN:
1399 hotkey << wxT("Down" );
1400 break;
1401 case WXK_PAGEUP:
1402 hotkey << wxT("Prior" );
1403 break;
1404 case WXK_PAGEDOWN:
1405 hotkey << wxT("Next" );
1406 break;
1407 case WXK_LEFT:
1408 hotkey << wxT("Left" );
1409 break;
1410 case WXK_RIGHT:
1411 hotkey << wxT("Right" );
1412 break;
1413 case WXK_HOME:
1414 hotkey << wxT("Home" );
1415 break;
1416 case WXK_END:
1417 hotkey << wxT("End" );
1418 break;
1419 case WXK_RETURN:
1420 hotkey << wxT("Return" );
1421 break;
96fd301f 1422
a070d8ce
VZ
1423 // if there are any other keys wxGetAccelFromString() may
1424 // return, we should process them here
8bbe427f 1425
717a57c2 1426 default:
a070d8ce 1427 if ( code < 127 )
717a57c2 1428 {
2b5f62a0 1429 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
a070d8ce
VZ
1430 if ( name )
1431 {
1432 hotkey << name;
1433 break;
1434 }
717a57c2 1435 }
c801d85f 1436
717a57c2
VZ
1437 wxFAIL_MSG( wxT("unknown keyboard accel") );
1438 }
c801d85f 1439
717a57c2 1440 delete accel;
631f1bfe 1441 }
717a57c2
VZ
1442
1443 return hotkey;
631f1bfe 1444}
a070d8ce 1445
717a57c2 1446#endif // wxUSE_ACCEL
631f1bfe 1447
37d403aa 1448
cc47eed9
RR
1449//-----------------------------------------------------------------------------
1450// substitute for missing GtkPixmapMenuItem
1451//-----------------------------------------------------------------------------
37d403aa 1452
9e691f46
VZ
1453#ifdef USE_MENU_BITMAPS
1454
37d403aa
JS
1455/*
1456 * Copyright (C) 1998, 1999, 2000 Free Software Foundation
1457 * All rights reserved.
1458 *
1459 * This file is part of the Gnome Library.
1460 *
1461 * The Gnome Library is free software; you can redistribute it and/or
1462 * modify it under the terms of the GNU Library General Public License as
1463 * published by the Free Software Foundation; either version 2 of the
1464 * License, or (at your option) any later version.
1465 *
1466 * The Gnome Library is distributed in the hope that it will be useful,
1467 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1468 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1469 * Library General Public License for more details.
1470 *
1471 * You should have received a copy of the GNU Library General Public
1472 * License along with the Gnome Library; see the file COPYING.LIB. If not,
1473 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1474 * Boston, MA 02111-1307, USA.
1475 */
1476/*
1477 @NOTATION@
1478 */
1479
1480/* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */
1481
37d403aa
JS
1482#include <gtk/gtkaccellabel.h>
1483#include <gtk/gtksignal.h>
1484#include <gtk/gtkmenuitem.h>
1485#include <gtk/gtkmenu.h>
1486#include <gtk/gtkcontainer.h>
1487
90350682
VZ
1488extern "C"
1489{
1490
37d403aa
JS
1491static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass);
1492static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item);
1493static void gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1494 GdkRectangle *area);
37d403aa 1495static gint gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1496 GdkEventExpose *event);
37d403aa
JS
1497
1498/* we must override the following functions */
1499
1500static void gtk_pixmap_menu_item_map (GtkWidget *widget);
1501static void gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1502 GtkAllocation *allocation);
37d403aa 1503static void gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1504 gboolean include_internals,
1505 GtkCallback callback,
1506 gpointer callback_data);
37d403aa 1507static void gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1508 GtkRequisition *requisition);
37d403aa 1509static void gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1510 GtkWidget *child);
37d403aa
JS
1511
1512static void changed_have_pixmap_status (GtkPixmapMenuItem *menu_item);
1513
1514static GtkMenuItemClass *parent_class = NULL;
1515
90350682
VZ
1516}
1517
37d403aa
JS
1518#define BORDER_SPACING 3
1519#define PMAP_WIDTH 20
1520
1521GtkType
1522gtk_pixmap_menu_item_get_type (void)
1523{
1524 static GtkType pixmap_menu_item_type = 0;
1525
1526 if (!pixmap_menu_item_type)
1527 {
1528 GtkTypeInfo pixmap_menu_item_info =
1529 {
90350682 1530 (char *)"GtkPixmapMenuItem",
37d403aa
JS
1531 sizeof (GtkPixmapMenuItem),
1532 sizeof (GtkPixmapMenuItemClass),
1533 (GtkClassInitFunc) gtk_pixmap_menu_item_class_init,
1534 (GtkObjectInitFunc) gtk_pixmap_menu_item_init,
1535 /* reserved_1 */ NULL,
1536 /* reserved_2 */ NULL,
1537 (GtkClassInitFunc) NULL,
1538 };
1539
2368dcda 1540 pixmap_menu_item_type = gtk_type_unique (gtk_menu_item_get_type (),
8f262dc5 1541 &pixmap_menu_item_info);
37d403aa
JS
1542 }
1543
1544 return pixmap_menu_item_type;
1545}
1546
1547/**
1548 * gtk_pixmap_menu_item_new
1549 *
2368dcda 1550 * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap()
37d403aa
JS
1551 * to set the pixmap wich is displayed at the left side.
1552 *
1553 * Returns:
1554 * &GtkWidget pointer to new menu item
1555 **/
1556
1557GtkWidget*
1558gtk_pixmap_menu_item_new (void)
1559{
1560 return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ()));
1561}
1562
1563static void
1564gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass)
1565{
1566 GtkObjectClass *object_class;
1567 GtkWidgetClass *widget_class;
1568 GtkMenuItemClass *menu_item_class;
1569 GtkContainerClass *container_class;
1570
1571 object_class = (GtkObjectClass*) klass;
1572 widget_class = (GtkWidgetClass*) klass;
1573 menu_item_class = (GtkMenuItemClass*) klass;
1574 container_class = (GtkContainerClass*) klass;
1575
1576 parent_class = (GtkMenuItemClass*) gtk_type_class (gtk_menu_item_get_type ());
1577
1578 widget_class->draw = gtk_pixmap_menu_item_draw;
1579 widget_class->expose_event = gtk_pixmap_menu_item_expose;
1580 widget_class->map = gtk_pixmap_menu_item_map;
1581 widget_class->size_allocate = gtk_pixmap_menu_item_size_allocate;
1582 widget_class->size_request = gtk_pixmap_menu_item_size_request;
1583
1584 container_class->forall = gtk_pixmap_menu_item_forall;
1585 container_class->remove = gtk_pixmap_menu_item_remove;
1586
1587 klass->orig_toggle_size = menu_item_class->toggle_size;
1588 klass->have_pixmap_count = 0;
1589}
1590
1591static void
1592gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item)
1593{
1594 GtkMenuItem *mi;
1595
1596 mi = GTK_MENU_ITEM (menu_item);
1597
1598 menu_item->pixmap = NULL;
1599}
1600
1601static void
1602gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1603 GdkRectangle *area)
37d403aa
JS
1604{
1605 g_return_if_fail (widget != NULL);
1606 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1607 g_return_if_fail (area != NULL);
1608
1609 if (GTK_WIDGET_CLASS (parent_class)->draw)
1610 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
1611
2368dcda 1612 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1613 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1614 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1615 }
1616}
1617
1618static gint
1619gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1620 GdkEventExpose *event)
37d403aa
JS
1621{
1622 g_return_val_if_fail (widget != NULL, FALSE);
1623 g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget), FALSE);
1624 g_return_val_if_fail (event != NULL, FALSE);
1625
1626 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
1627 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1628
2368dcda 1629 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1630 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1631 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1632 }
1633
1634 return FALSE;
1635}
1636
1637/**
1638 * gtk_pixmap_menu_item_set_pixmap
1639 * @menu_item: Pointer to the pixmap menu item
1640 * @pixmap: Pointer to a pixmap widget
1641 *
1642 * Set the pixmap of the menu item.
1643 *
1644 **/
1645
1646void
1647gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
8f262dc5 1648 GtkWidget *pixmap)
37d403aa
JS
1649{
1650 g_return_if_fail (menu_item != NULL);
1651 g_return_if_fail (pixmap != NULL);
1652 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item));
1653 g_return_if_fail (GTK_IS_WIDGET (pixmap));
1654 g_return_if_fail (menu_item->pixmap == NULL);
1655
1656 gtk_widget_set_parent (pixmap, GTK_WIDGET (menu_item));
1657 menu_item->pixmap = pixmap;
1658
1659 if (GTK_WIDGET_REALIZED (pixmap->parent) &&
1660 !GTK_WIDGET_REALIZED (pixmap))
1661 gtk_widget_realize (pixmap);
2368dcda
VZ
1662
1663 if (GTK_WIDGET_VISIBLE (pixmap->parent)) {
37d403aa 1664 if (GTK_WIDGET_MAPPED (pixmap->parent) &&
8f262dc5 1665 GTK_WIDGET_VISIBLE(pixmap) &&
37d403aa
JS
1666 !GTK_WIDGET_MAPPED (pixmap))
1667 gtk_widget_map (pixmap);
1668 }
1669
1670 changed_have_pixmap_status(menu_item);
2368dcda 1671
37d403aa
JS
1672 if (GTK_WIDGET_VISIBLE (pixmap) && GTK_WIDGET_VISIBLE (menu_item))
1673 gtk_widget_queue_resize (pixmap);
1674}
1675
1676static void
1677gtk_pixmap_menu_item_map (GtkWidget *widget)
1678{
1679 GtkPixmapMenuItem *menu_item;
1680
1681 g_return_if_fail (widget != NULL);
1682 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1683
1684 menu_item = GTK_PIXMAP_MENU_ITEM(widget);
1685
1686 GTK_WIDGET_CLASS(parent_class)->map(widget);
1687
1688 if (menu_item->pixmap &&
1689 GTK_WIDGET_VISIBLE (menu_item->pixmap) &&
1690 !GTK_WIDGET_MAPPED (menu_item->pixmap))
1691 gtk_widget_map (menu_item->pixmap);
1692}
1693
1694static void
1695gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1696 GtkAllocation *allocation)
37d403aa
JS
1697{
1698 GtkPixmapMenuItem *pmenu_item;
1699
1700 pmenu_item = GTK_PIXMAP_MENU_ITEM(widget);
1701
1702 if (pmenu_item->pixmap && GTK_WIDGET_VISIBLE(pmenu_item))
1703 {
1704 GtkAllocation child_allocation;
1705 int border_width;
1706
1707 border_width = GTK_CONTAINER (widget)->border_width;
1708
1709 child_allocation.width = pmenu_item->pixmap->requisition.width;
1710 child_allocation.height = pmenu_item->pixmap->requisition.height;
1711 child_allocation.x = border_width + BORDER_SPACING;
1712 child_allocation.y = (border_width + BORDER_SPACING
8f262dc5
VZ
1713 + (((allocation->height - child_allocation.height) - child_allocation.x)
1714 / 2)); /* center pixmaps vertically */
37d403aa
JS
1715 gtk_widget_size_allocate (pmenu_item->pixmap, &child_allocation);
1716 }
1717
1718 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
1719 GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation);
1720}
1721
1722static void
1723gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1724 gboolean include_internals,
1725 GtkCallback callback,
1726 gpointer callback_data)
37d403aa
JS
1727{
1728 GtkPixmapMenuItem *menu_item;
1729
1730 g_return_if_fail (container != NULL);
1731 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1732 g_return_if_fail (callback != NULL);
1733
1734 menu_item = GTK_PIXMAP_MENU_ITEM (container);
1735
1736 if (menu_item->pixmap)
1737 (* callback) (menu_item->pixmap, callback_data);
1738
1739 GTK_CONTAINER_CLASS(parent_class)->forall(container,include_internals,
8f262dc5 1740 callback,callback_data);
37d403aa
JS
1741}
1742
1743static void
1744gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1745 GtkRequisition *requisition)
37d403aa
JS
1746{
1747 GtkPixmapMenuItem *menu_item;
1748 GtkRequisition req = {0, 0};
1749
1750 g_return_if_fail (widget != NULL);
1751 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1752 g_return_if_fail (requisition != NULL);
1753
1754 GTK_WIDGET_CLASS(parent_class)->size_request(widget,requisition);
1755
1756 menu_item = GTK_PIXMAP_MENU_ITEM (widget);
2368dcda 1757
37d403aa
JS
1758 if (menu_item->pixmap)
1759 gtk_widget_size_request(menu_item->pixmap, &req);
1760
1761 requisition->height = MAX(req.height + GTK_CONTAINER(widget)->border_width + BORDER_SPACING, (unsigned int) requisition->height);
1762 requisition->width += (req.width + GTK_CONTAINER(widget)->border_width + BORDER_SPACING);
1763}
1764
1765static void
1766gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1767 GtkWidget *child)
37d403aa
JS
1768{
1769 GtkBin *bin;
1770 gboolean widget_was_visible;
1771
1772 g_return_if_fail (container != NULL);
1773 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1774 g_return_if_fail (child != NULL);
1775 g_return_if_fail (GTK_IS_WIDGET (child));
1776
1777 bin = GTK_BIN (container);
2368dcda 1778 g_return_if_fail ((bin->child == child ||
8f262dc5 1779 (GTK_PIXMAP_MENU_ITEM(container)->pixmap == child)));
37d403aa
JS
1780
1781 widget_was_visible = GTK_WIDGET_VISIBLE (child);
2368dcda 1782
37d403aa
JS
1783 gtk_widget_unparent (child);
1784 if (bin->child == child)
2368dcda 1785 bin->child = NULL;
37d403aa
JS
1786 else {
1787 GTK_PIXMAP_MENU_ITEM(container)->pixmap = NULL;
1788 changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container));
1789 }
2368dcda 1790
37d403aa
JS
1791 if (widget_was_visible)
1792 gtk_widget_queue_resize (GTK_WIDGET (container));
1793}
1794
1795
1796/* important to only call this if there was actually a _change_ in pixmap == NULL */
1797static void
1798changed_have_pixmap_status (GtkPixmapMenuItem *menu_item)
1799{
1800 if (menu_item->pixmap != NULL) {
1801 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count += 1;
1802
1803 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 1) {
1804 /* Install pixmap toggle size */
1805 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size, PMAP_WIDTH);
1806 }
1807 } else {
1808 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count -= 1;
1809
1810 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 0) {
1811 /* Install normal toggle size */
2368dcda 1812 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size;
37d403aa
JS
1813 }
1814 }
1815
1816 /* Note that we actually need to do this for _all_ GtkPixmapMenuItem
1817 whenever the klass->toggle_size changes; but by doing it anytime
1818 this function is called, we get the same effect, just because of
1819 how the preferences option to show pixmaps works. Bogus, broken.
1820 */
2368dcda 1821 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item)))
37d403aa
JS
1822 gtk_widget_queue_resize(GTK_WIDGET(menu_item));
1823}
1824
9e691f46 1825#endif // USE_MENU_BITMAPS
37d403aa 1826