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