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