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