]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/menu.cpp
corrected system condition for panther
[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
14f355c2
VS
10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
c801d85f
KB
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
96fd301f 18#include "wx/log.h"
30dea054 19#include "wx/intl.h"
06cfab17 20#include "wx/app.h"
37d403aa 21#include "wx/bitmap.h"
3dfac970 22#include "wx/menu.h"
c801d85f 23
974e8d94
VZ
24#if wxUSE_ACCEL
25 #include "wx/accel.h"
26#endif // wxUSE_ACCEL
27
9e691f46
VZ
28#include "wx/gtk/private.h"
29
cc47eed9 30#include <gdk/gdkkeysyms.h>
9e691f46
VZ
31
32// FIXME: is this right? somehow I don't think so (VZ)
33#ifdef __WXGTK20__
34 #include <glib-object.h>
35
36 #define gtk_accel_group_attach(g, o) _gtk_accel_group_attach((g), (o))
37 #define gtk_accel_group_detach(g, o) _gtk_accel_group_detach((g), (o))
38 #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
39
40 #define ACCEL_OBJECT GObject
41 #define ACCEL_OBJECTS(a) (a)->acceleratables
42 #define ACCEL_OBJ_CAST(obj) G_OBJECT(obj)
43#else // GTK+ 1.x
44 #define ACCEL_OBJECT GtkObject
45 #define ACCEL_OBJECTS(a) (a)->attach_objects
46 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
47#endif
83624f79 48
acfd422a
RR
49//-----------------------------------------------------------------------------
50// idle system
51//-----------------------------------------------------------------------------
52
53extern void wxapp_install_idle_handler();
54extern bool g_isIdle;
55
9e691f46
VZ
56#if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL
57 static wxString GetHotKey( const wxMenuItem& item );
717a57c2
VZ
58#endif
59
cc47eed9
RR
60//-----------------------------------------------------------------------------
61// substitute for missing GtkPixmapMenuItem
62//-----------------------------------------------------------------------------
37d403aa 63
9e691f46
VZ
64// FIXME: I can't make this compile with GTK+ 2.0, disabling for now (VZ)
65#ifndef __WXGTK20__
66 #define USE_MENU_BITMAPS
67#endif
68
69#ifdef USE_MENU_BITMAPS
70
cc47eed9
RR
71#define GTK_TYPE_PIXMAP_MENU_ITEM (gtk_pixmap_menu_item_get_type ())
72#define GTK_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItem))
37d403aa 73#define GTK_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_PIXMAP_MENU_ITEM, GtkPixmapMenuItemClass))
cc47eed9 74#define GTK_IS_PIXMAP_MENU_ITEM(obj) (GTK_CHECK_TYPE ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
37d403aa
JS
75#define GTK_IS_PIXMAP_MENU_ITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PIXMAP_MENU_ITEM))
76//#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_PIXMAP_MENU_ITEM))
cc47eed9 77#define GTK_PIXMAP_MENU_ITEM_GET_CLASS(obj) (GTK_PIXMAP_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
37d403aa
JS
78
79#ifndef GTK_MENU_ITEM_GET_CLASS
80#define GTK_MENU_ITEM_GET_CLASS(obj) (GTK_MENU_ITEM_CLASS( GTK_OBJECT_GET_CLASS(obj)))
81#endif
82
83typedef struct _GtkPixmapMenuItem GtkPixmapMenuItem;
84typedef struct _GtkPixmapMenuItemClass GtkPixmapMenuItemClass;
85
86struct _GtkPixmapMenuItem
87{
cc47eed9 88 GtkMenuItem menu_item;
37d403aa 89
cc47eed9 90 GtkWidget *pixmap;
37d403aa
JS
91};
92
93struct _GtkPixmapMenuItemClass
94{
cc47eed9 95 GtkMenuItemClass parent_class;
37d403aa 96
cc47eed9
RR
97 guint orig_toggle_size;
98 guint have_pixmap_count;
37d403aa
JS
99};
100
101
8f262dc5 102GtkType gtk_pixmap_menu_item_get_type (void);
cc47eed9
RR
103GtkWidget* gtk_pixmap_menu_item_new (void);
104void gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
8f262dc5 105 GtkWidget *pixmap);
37d403aa 106
9e691f46
VZ
107#endif // USE_MENU_BITMAPS
108
f6bcfd97
BP
109//-----------------------------------------------------------------------------
110// idle system
111//-----------------------------------------------------------------------------
112
113static wxString wxReplaceUnderscore( const wxString& title )
114{
115 const wxChar *pc;
2368dcda 116
f6bcfd97
BP
117 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
118 wxString str;
2b5f62a0
VZ
119 pc = title;
120 while (*pc != wxT('\0'))
f6bcfd97 121 {
2b5f62a0
VZ
122 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
123 {
124 // "&" is doubled to indicate "&" instead of accelerator
125 ++pc;
126 str << wxT('&');
127 }
128 else if (*pc == wxT('&'))
f6bcfd97 129 {
4e9cbd33 130#if GTK_CHECK_VERSION(1, 2, 0)
f6bcfd97 131 str << wxT('_');
4e9cbd33 132#endif
f6bcfd97 133 }
4e9cbd33
VS
134#if GTK_CHECK_VERSION(2, 0, 0)
135 else if (*pc == wxT('/'))
136 {
137 str << wxT("\\/");
138 }
139 else if (*pc == wxT('\\'))
140 {
141 str << wxT("\\\\");
142 }
143#elif GTK_CHECK_VERSION(1, 2, 0)
f6bcfd97
BP
144 else if (*pc == wxT('/'))
145 {
146 str << wxT('\\');
f6bcfd97 147 }
4e9cbd33 148#endif
f6bcfd97
BP
149 else
150 {
d9e403cc 151#ifdef __WXGTK12__
f6bcfd97
BP
152 if ( *pc == wxT('_') )
153 {
154 // underscores must be doubled to prevent them from being
155 // interpreted as accelerator character prefix by GTK
156 str << *pc;
157 }
158#endif // GTK+ 1.2
159
160 str << *pc;
161 }
2b5f62a0 162 ++pc;
f6bcfd97
BP
163 }
164 return str;
165}
166
e6396ed4
JS
167//-----------------------------------------------------------------------------
168// activate message from GTK
169//-----------------------------------------------------------------------------
170
171static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
172{
173 if (g_isIdle) wxapp_install_idle_handler();
174
92f1a59c 175 wxMenuEvent event( wxEVT_MENU_OPEN, -1, menu );
e6396ed4
JS
176 event.SetEventObject( menu );
177
a01fe3d6
RD
178 wxEvtHandler* handler = menu->GetEventHandler();
179 if (handler && handler->ProcessEvent(event))
e6396ed4
JS
180 return;
181
182 wxWindow *win = menu->GetInvokingWindow();
183 if (win) win->GetEventHandler()->ProcessEvent( event );
184}
185
c801d85f
KB
186//-----------------------------------------------------------------------------
187// wxMenuBar
188//-----------------------------------------------------------------------------
189
190IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
191
3502e687
RR
192wxMenuBar::wxMenuBar( long style )
193{
1e133b7d 194 /* the parent window is known after wxFrame::SetMenu() */
23280650 195 m_needParent = FALSE;
ae53c98c 196 m_style = style;
9c884972 197 m_invokingWindow = (wxWindow*) NULL;
23280650 198
4dcaf11a 199 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 200 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 201 {
223d09f6 202 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 203 return;
4dcaf11a 204 }
3502e687 205
1e133b7d 206 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
9e691f46 207#if GTK_CHECK_VERSION(1, 2, 1)
1e133b7d
RR
208 m_accel = gtk_accel_group_new();
209 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
210 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 211#else
3502e687 212 m_menubar = gtk_menu_bar_new();
1e133b7d 213#endif
3502e687
RR
214
215 if (style & wxMB_DOCKABLE)
216 {
217 m_widget = gtk_handle_box_new();
c626a8b7
VZ
218 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
219 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
220 }
221 else
222 {
223 m_widget = GTK_WIDGET(m_menubar);
224 }
225
226 PostCreation();
c4608a8a 227
db434467 228 ApplyWidgetStyle();
3502e687
RR
229}
230
96fd301f 231wxMenuBar::wxMenuBar()
c801d85f 232{
1e133b7d
RR
233 /* the parent window is known after wxFrame::SetMenu() */
234 m_needParent = FALSE;
ae53c98c 235 m_style = 0;
9c884972 236 m_invokingWindow = (wxWindow*) NULL;
23280650 237
4dcaf11a 238 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 239 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 240 {
223d09f6 241 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 242 return;
4dcaf11a 243 }
974e8d94 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
222ed1d6 277 wxMenuItemList::compatibility_iterator 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
222ed1d6 302 wxMenuItemList::compatibility_iterator 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
222ed1d6 326 wxMenuList::compatibility_iterator 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
222ed1d6 347 wxMenuList::compatibility_iterator 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
a01fe3d6 378 char cbuf[400];
fab591c5 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 388 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
a01fe3d6 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 ();
222ed1d6 476 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
75c116ab
JS
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));
a01fe3d6 496
75c116ab
JS
497 node = node->GetNext();
498 }
a01fe3d6 499
75c116ab
JS
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
222ed1d6 554 wxMenuItemList::compatibility_iterator 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{
222ed1d6 569 wxMenuList::compatibility_iterator 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
222ed1d6 587 wxMenuItemList::compatibility_iterator 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;
222ed1d6 604 wxMenuList::compatibility_iterator 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{
222ed1d6 622 wxMenuList::compatibility_iterator 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{
222ed1d6 634 wxMenuList::compatibility_iterator 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{
222ed1d6 661 wxMenuList::compatibility_iterator 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
4e6beae5
RD
703 wxMenuItem* item = menu->FindChildItem( id );
704 wxCHECK_RET( item, wxT("error in menu item callback") );
705
706 if (item->IsCheckable())
707 {
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) )
719 {
720 return;
721 }
722 }
723
724
02822c8c
RD
725 // Is this menu on a menubar? (possibly nested)
726 wxFrame* frame = NULL;
727 wxMenu* pm = menu;
728 while ( pm && !frame )
729 {
730 if ( pm->IsAttached() )
731 frame = pm->GetMenuBar()->GetFrame();
4e6beae5 732 pm = pm->GetParent();
02822c8c
RD
733 }
734
da0b5338
VZ
735 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
736 // normally wxMenu::SendEvent() should be enough, if it doesn't work
737 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
738 // should be fixed instead of working around it here...
02822c8c 739 if (frame)
2d17d68f 740 {
4e6beae5
RD
741 // If it is attached then let the frame send the event.
742 // Don't call frame->ProcessCommand(id) because it toggles
743 // checkable items and we've already done that above.
744 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
745 commandEvent.SetEventObject(frame);
746 if (item->IsCheckable())
747 commandEvent.SetInt(item->IsChecked());
da0b5338 748 commandEvent.SetEventObject(menu);
4e6beae5
RD
749
750 frame->GetEventHandler()->ProcessEvent(commandEvent);
a01fe3d6
RD
751 }
752 else
753 {
4e6beae5 754 // otherwise let the menu have it
a01fe3d6 755 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
2d17d68f 756 }
cf7a7e13
RR
757}
758
759//-----------------------------------------------------------------------------
760// "select"
761//-----------------------------------------------------------------------------
762
763static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
764{
acfd422a
RR
765 if (g_isIdle) wxapp_install_idle_handler();
766
83624f79
RR
767 int id = menu->FindMenuIdByMenuItem(widget);
768
769 wxASSERT( id != -1 ); // should find it!
cf7a7e13 770
c626a8b7
VZ
771 if (!menu->IsEnabled(id))
772 return;
cf7a7e13 773
342b6a2f 774 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 775 event.SetEventObject( menu );
cf7a7e13 776
a01fe3d6
RD
777 wxEvtHandler* handler = menu->GetEventHandler();
778 if (handler && handler->ProcessEvent(event))
c626a8b7 779 return;
6de97a3b 780
83624f79
RR
781 wxWindow *win = menu->GetInvokingWindow();
782 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 783}
c801d85f 784
cd743a6f
RR
785//-----------------------------------------------------------------------------
786// "deselect"
787//-----------------------------------------------------------------------------
788
789static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
790{
acfd422a
RR
791 if (g_isIdle) wxapp_install_idle_handler();
792
cd743a6f
RR
793 int id = menu->FindMenuIdByMenuItem(widget);
794
795 wxASSERT( id != -1 ); // should find it!
796
c626a8b7
VZ
797 if (!menu->IsEnabled(id))
798 return;
cd743a6f
RR
799
800 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
801 event.SetEventObject( menu );
802
a01fe3d6
RD
803 wxEvtHandler* handler = menu->GetEventHandler();
804 if (handler && handler->ProcessEvent(event))
c626a8b7 805 return;
cd743a6f
RR
806
807 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
808 if (win)
809 win->GetEventHandler()->ProcessEvent( event );
cd743a6f
RR
810}
811
cf7a7e13 812//-----------------------------------------------------------------------------
db1b4961 813// wxMenuItem
cf7a7e13
RR
814//-----------------------------------------------------------------------------
815
7dbf5360 816IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
974e8d94
VZ
817
818wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
819 int id,
820 const wxString& name,
821 const wxString& help,
d65c269b 822 wxItemKind kind,
974e8d94
VZ
823 wxMenu *subMenu)
824{
d65c269b 825 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 826}
96fd301f 827
974e8d94
VZ
828wxMenuItem::wxMenuItem(wxMenu *parentMenu,
829 int id,
830 const wxString& text,
831 const wxString& help,
d65c269b 832 wxItemKind kind,
974e8d94 833 wxMenu *subMenu)
d65c269b 834 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
2368dcda 835{
092f7536 836 Init(text);
2368dcda
VZ
837}
838
839wxMenuItem::wxMenuItem(wxMenu *parentMenu,
840 int id,
841 const wxString& text,
842 const wxString& help,
843 bool isCheckable,
844 wxMenu *subMenu)
845 : wxMenuItemBase(parentMenu, id, text, help,
846 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
847{
092f7536 848 Init(text);
2368dcda
VZ
849}
850
092f7536 851void wxMenuItem::Init(const wxString& text)
c801d85f 852{
37d403aa 853 m_labelWidget = (GtkWidget *) NULL;
83624f79 854 m_menuItem = (GtkWidget *) NULL;
974e8d94 855
092f7536 856 DoSetText(text);
6de97a3b 857}
c801d85f 858
d1b15f03
RR
859wxMenuItem::~wxMenuItem()
860{
861 // don't delete menu items, the menus take care of that
862}
863
717a57c2 864// return the menu item text without any menu accels
3b59cdbf
VZ
865/* static */
866wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2
VZ
867{
868 wxString label;
2368dcda 869
3b59cdbf 870 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
717a57c2 871 {
7cf4f7e2 872 if ( *pc == wxT('_') )
717a57c2 873 {
2b5f62a0 874 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
d76419bd
RR
875 pc++;
876 label += *pc;
877 continue;
878 }
2368dcda 879
2b5f62a0
VZ
880#if GTK_CHECK_VERSION(2, 0, 0)
881 if ( *pc == wxT('\\') )
d76419bd 882 {
2b5f62a0
VZ
883 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
884 pc++;
885 label += *pc;
717a57c2
VZ
886 continue;
887 }
2b5f62a0 888#endif
717a57c2 889
7cf4f7e2
GD
890 if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) )
891 {
892 // wxMSW escapes "&"
893 // "&" is doubled to indicate "&" instead of accelerator
894 continue;
895 }
a01fe3d6 896
717a57c2
VZ
897 label += *pc;
898 }
a01fe3d6 899
d9e403cc
RR
900 // wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() );
901
717a57c2
VZ
902 return label;
903}
904
905void wxMenuItem::SetText( const wxString& str )
906{
5869f93f
JS
907 // Some optimization to avoid flicker
908 wxString oldLabel = m_text;
909 oldLabel = wxStripMenuCodes(oldLabel.BeforeFirst('\t'));
910 oldLabel.Replace(wxT("_"), wxT(""));
911 wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t'));
912 if (oldLabel == label1)
913 return;
a01fe3d6 914
717a57c2 915 DoSetText(str);
354aa1e3
RR
916
917 if (m_menuItem)
918 {
37d403aa
JS
919 GtkLabel *label;
920 if (m_labelWidget)
2b5f62a0 921 label = (GtkLabel*) m_labelWidget;
37d403aa 922 else
2b5f62a0 923 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2 924
2b5f62a0
VZ
925#if GTK_CHECK_VERSION(2, 0, 0)
926 // We have to imitate item_factory_unescape_label here
927 wxString tmp;
928 for (size_t n = 0; n < m_text.Len(); n++)
929 {
930 if (m_text[n] != wxT('\\'))
931 tmp += m_text[n];
932 }
a01fe3d6 933
2b5f62a0
VZ
934 gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) );
935#else
936 // set new text
fab591c5 937 gtk_label_set( label, wxGTK_CONV( m_text ) );
717a57c2 938
2b5f62a0
VZ
939 // reparse key accel
940 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
354aa1e3 941 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
2b5f62a0 942#endif
354aa1e3
RR
943 }
944}
945
c626a8b7 946// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 947void wxMenuItem::DoSetText( const wxString& str )
716b7364 948{
2b5f62a0 949 // '\t' is the deliminator indicating a hot key
974e8d94 950 m_text.Empty();
ab46dc18 951 const wxChar *pc = str;
2b5f62a0 952 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
83624f79 953 {
2b5f62a0 954 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
23280650 955 {
2b5f62a0
VZ
956 // "&" is doubled to indicate "&" instead of accelerator
957 ++pc;
958 m_text << wxT('&');
572d7461 959 }
2b5f62a0 960 else if (*pc == wxT('&'))
572d7461 961 {
2b5f62a0 962 m_text << wxT('_');
572d7461 963 }
2b5f62a0
VZ
964#if GTK_CHECK_VERSION(2, 0, 0)
965 else if ( *pc == wxT('_') ) // escape underscores
4e9cbd33 966 {
2b5f62a0 967 // m_text << wxT("__"); doesn't work
d9e403cc 968 m_text << wxT("__");
4e9cbd33 969 }
4e9cbd33
VS
970 else if (*pc == wxT('/')) // we have to escape slashes
971 {
972 m_text << wxT("\\/");
973 }
974 else if (*pc == wxT('\\')) // we have to double backslashes
975 {
976 m_text << wxT("\\\\");
977 }
2b5f62a0
VZ
978#else
979 else if ( *pc == wxT('_') ) // escape underscores
980 {
981 m_text << wxT("__");
982 }
223d09f6 983 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
23280650 984 {
223d09f6 985 m_text << wxT('\\'); /* ... and replace them with back slashes */
034be888 986 }
4e9cbd33 987#endif
2b5f62a0 988 else {
354aa1e3 989 m_text << *pc;
2b5f62a0
VZ
990 }
991 ++pc;
83624f79 992 }
a01fe3d6 993
d9e403cc 994 // wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() );
a01fe3d6 995
223d09f6 996 m_hotKey = wxT("");
9e691f46 997
223d09f6 998 if(*pc == wxT('\t'))
d7dbc98a
KB
999 {
1000 pc++;
1001 m_hotKey = pc;
1002 }
716b7364
RR
1003}
1004
717a57c2
VZ
1005#if wxUSE_ACCEL
1006
1007wxAcceleratorEntry *wxMenuItem::GetAccel() const
1008{
1987af7e 1009 if ( !GetHotKey() )
717a57c2
VZ
1010 {
1011 // nothing
1012 return (wxAcceleratorEntry *)NULL;
1013 }
1014
1015 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
1016 wxString label;
1987af7e 1017 label << wxT('\t') << GetHotKey();
717a57c2
VZ
1018
1019 return wxGetAccelFromString(label);
1020}
1021
1022#endif // wxUSE_ACCEL
1023
96fd301f 1024void wxMenuItem::Check( bool check )
716b7364 1025{
223d09f6 1026 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 1027
974e8d94
VZ
1028 if (check == m_isChecked)
1029 return;
2d17d68f 1030
974e8d94 1031 wxMenuItemBase::Check( check );
0472ece7 1032
24bcaec3 1033 switch ( GetKind() )
0472ece7 1034 {
24bcaec3
VZ
1035 case wxITEM_CHECK:
1036 case wxITEM_RADIO:
1037 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
1038 break;
1039
1040 default:
1041 wxFAIL_MSG( _T("can't check this item") );
0472ece7 1042 }
716b7364
RR
1043}
1044
8bbe427f
VZ
1045void wxMenuItem::Enable( bool enable )
1046{
223d09f6 1047 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 1048
83624f79 1049 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 1050 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
1051}
1052
96fd301f 1053bool wxMenuItem::IsChecked() const
716b7364 1054{
223d09f6 1055 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
db1b4961 1056
974e8d94
VZ
1057 wxCHECK_MSG( IsCheckable(), FALSE,
1058 wxT("can't get state of uncheckable item!") );
96fd301f 1059
974e8d94 1060 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
1061}
1062
354aa1e3
RR
1063wxString wxMenuItem::GetFactoryPath() const
1064{
d9e403cc
RR
1065 // In order to get the pointer to the item we need the item
1066 // text _without_ underscores in GTK 1.2
354aa1e3 1067 wxString path( wxT("<main>/") );
717a57c2 1068
d76419bd
RR
1069 for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
1070 {
2b5f62a0 1071 if ( *pc == wxT('_') )
d76419bd 1072 {
d9e403cc
RR
1073#ifdef __WXGTK20__
1074 pc++;
1075#else
2b5f62a0 1076 // remove '_' unconditionally
d76419bd 1077 continue;
d9e403cc 1078#endif
d76419bd 1079 }
2368dcda 1080
7cf4f7e2
GD
1081 // don't remove ampersands '&' since if we have them in the menu item title
1082 // it means that they were doubled to indicate "&" instead of accelerator
1083
d76419bd
RR
1084 path += *pc;
1085 }
2368dcda 1086
354aa1e3
RR
1087 return path;
1088}
1089
db1b4961 1090//-----------------------------------------------------------------------------
83624f79 1091// wxMenu
db1b4961
RR
1092//-----------------------------------------------------------------------------
1093
c801d85f
KB
1094IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
1095
717a57c2 1096void wxMenu::Init()
c801d85f 1097{
034be888
RR
1098 m_accel = gtk_accel_group_new();
1099 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
1100 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
8bbe427f 1101
2b1c162e 1102 m_owner = (GtkWidget*) NULL;
2b2edbed 1103
d9e403cc
RR
1104 // Tearoffs are entries, just like separators. So if we want this
1105 // menu to be a tear-off one, we just append a tearoff entry
1106 // immediately.
2b2edbed
KB
1107 if(m_style & wxMENU_TEAROFF)
1108 {
1109 GtkItemFactoryEntry entry;
90350682 1110 entry.path = (char *)"/tearoff";
2b2edbed
KB
1111 entry.callback = (GtkItemFactoryCallback) NULL;
1112 entry.callback_action = 0;
90350682 1113 entry.item_type = (char *)"<Tearoff>";
2b2edbed 1114 entry.accelerator = (gchar*) NULL;
d9e403cc 1115 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
23280650 1116 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed 1117 }
c801d85f 1118
717a57c2
VZ
1119 // append the title as the very first entry if we have it
1120 if ( !!m_title )
d1b15f03 1121 {
717a57c2
VZ
1122 Append(-2, m_title);
1123 AppendSeparator();
d1b15f03 1124 }
717a57c2 1125}
15a2076a 1126
717a57c2
VZ
1127wxMenu::~wxMenu()
1128{
222ed1d6 1129 WX_CLEAR_LIST(wxMenuItemList, m_items);
f03ec224 1130
368a4a47
RD
1131 if ( GTK_IS_WIDGET( m_menu ))
1132 gtk_widget_destroy( m_menu );
974e8d94 1133
d1b15f03 1134 gtk_object_unref( GTK_OBJECT(m_factory) );
c2dd8380
GL
1135}
1136
32db328c 1137bool wxMenu::GtkAppend(wxMenuItem *mitem)
c2dd8380 1138{
717a57c2 1139 GtkWidget *menuItem;
96fd301f 1140
9e691f46 1141#if defined(USE_MENU_BITMAPS) || !GTK_CHECK_VERSION(1, 2, 0)
37d403aa 1142 bool appended = FALSE;
9e691f46 1143#endif
37d403aa 1144
24bcaec3
VZ
1145 // does this item terminate the current radio group?
1146 bool endOfRadioGroup = TRUE;
d65c269b 1147
717a57c2
VZ
1148 if ( mitem->IsSeparator() )
1149 {
717a57c2 1150 GtkItemFactoryEntry entry;
90350682 1151 entry.path = (char *)"/sep";
717a57c2
VZ
1152 entry.callback = (GtkItemFactoryCallback) NULL;
1153 entry.callback_action = 0;
90350682 1154 entry.item_type = (char *)"<Separator>";
717a57c2
VZ
1155 entry.accelerator = (gchar*) NULL;
1156
d9e403cc 1157 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
717a57c2 1158
d9e403cc 1159 // this will be wrong for more than one separator. do we care?
717a57c2 1160 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
24bcaec3
VZ
1161
1162 // we might have a separator inside a radio group
1163 endOfRadioGroup = FALSE;
717a57c2
VZ
1164 }
1165 else if ( mitem->IsSubMenu() )
837904f2 1166 {
d9e403cc 1167 // text has "_" instead of "&" after mitem->SetText()
717a57c2 1168 wxString text( mitem->GetText() );
974e8d94 1169
d9e403cc 1170 // local buffer in multibyte form
717a57c2
VZ
1171 char buf[200];
1172 strcpy( buf, "/" );
fab591c5 1173 strcat( buf, wxGTK_CONV( text ) );
50592885 1174
717a57c2
VZ
1175 GtkItemFactoryEntry entry;
1176 entry.path = buf;
1177 entry.callback = (GtkItemFactoryCallback) 0;
1178 entry.callback_action = 0;
90350682 1179 entry.item_type = (char *)"<Branch>";
a583e623 1180 entry.accelerator = (gchar*) NULL;
50592885 1181
d9e403cc 1182 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
50592885 1183
717a57c2 1184 wxString path( mitem->GetFactoryPath() );
fab591c5 1185 menuItem = gtk_item_factory_get_item( m_factory, wxGTK_CONV( path ) );
50592885 1186
1987af7e 1187 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
e7200790
VZ
1188
1189 // if adding a submenu to a menu already existing in the menu bar, we
1190 // must set invoking window to allow processing events from this
1191 // submenu
1192 if ( m_invokingWindow )
1193 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
837904f2 1194 }
9e691f46 1195#ifdef USE_MENU_BITMAPS
37d403aa
JS
1196 else if (mitem->GetBitmap().Ok()) // An item with bitmap
1197 {
cc47eed9
RR
1198 wxString text( mitem->GetText() );
1199 const wxBitmap *bitmap = &mitem->GetBitmap();
2368dcda 1200
cc47eed9 1201 menuItem = gtk_pixmap_menu_item_new ();
fab591c5 1202 GtkWidget *label = gtk_accel_label_new ( wxGTK_CONV( text ) );
cc47eed9
RR
1203 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1204 gtk_container_add (GTK_CONTAINER (menuItem), label);
cc47eed9 1205 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuItem);
33caa994
VZ
1206 guint accel_key;
1207 GdkModifierType accel_mods;
1208
1209 // accelerator for the item, as specified by its label
1210 // (ex. Ctrl+O for open)
1211 gtk_accelerator_parse(GetHotKey(*mitem).c_str(),
1212 &accel_key, &accel_mods);
cc47eed9 1213 if (accel_key != GDK_VoidSymbol)
2b5f62a0 1214 {
cc47eed9 1215 gtk_widget_add_accelerator (menuItem,
8f262dc5 1216 "activate_item",
33caa994
VZ
1217 gtk_menu_get_accel_group(
1218 GTK_MENU(m_menu)),
1219 accel_key, accel_mods,
1220 GTK_ACCEL_VISIBLE);
1221 }
1222
1223 // accelerator for the underlined char (ex ALT+F for the File menu)
1224 accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) );
1225 if (accel_key != GDK_VoidSymbol)
1226 {
1227 gtk_widget_add_accelerator (menuItem,
1228 "activate_item",
1229 gtk_menu_ensure_uline_accel_group (
1230 GTK_MENU (m_menu)),
8f262dc5
VZ
1231 accel_key, 0,
1232 GTK_ACCEL_LOCKED);
cc47eed9 1233 }
33caa994 1234
cc47eed9 1235 gtk_widget_show (label);
37d403aa 1236
cc47eed9 1237 mitem->SetLabelWidget(label);
37d403aa 1238
cc47eed9
RR
1239 GtkWidget* pixmap = gtk_pixmap_new( bitmap->GetPixmap(), bitmap->GetMask() ? bitmap->GetMask()->GetBitmap() : (GdkBitmap* )NULL);
1240 gtk_widget_show(pixmap);
1241 gtk_pixmap_menu_item_set_pixmap(GTK_PIXMAP_MENU_ITEM( menuItem ), pixmap);
37d403aa
JS
1242
1243 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
1244 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1245 (gpointer)this );
a01fe3d6 1246
37d403aa 1247 gtk_menu_append( GTK_MENU(m_menu), menuItem );
33caa994 1248
37d403aa
JS
1249 gtk_widget_show( menuItem );
1250
37d403aa
JS
1251 appended = TRUE; // We've done this, don't do it again
1252 }
9e691f46 1253#endif // USE_MENU_BITMAPS
717a57c2
VZ
1254 else // a normal item
1255 {
982f23fd 1256 // text has "_" instead of "&" after mitem->SetText() so don't use it
717a57c2
VZ
1257 wxString text( mitem->GetText() );
1258
8f262dc5
VZ
1259 // buffers containing the menu item path and type in multibyte form
1260 char bufPath[256],
1261 bufType[256];
1262
1263 strcpy( bufPath, "/" );
1264 strncat( bufPath, wxGTK_CONV(text), WXSIZEOF(bufPath) - 2 );
1265 bufPath[WXSIZEOF(bufPath) - 1] = '\0';
717a57c2
VZ
1266
1267 GtkItemFactoryEntry entry;
8f262dc5 1268 entry.path = bufPath;
717a57c2
VZ
1269 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
1270 entry.callback_action = 0;
d65c269b
VZ
1271
1272 wxString pathRadio;
1273 const char *item_type;
1274 switch ( mitem->GetKind() )
1275 {
546bfbea 1276 case wxITEM_CHECK:
d65c269b
VZ
1277 item_type = "<CheckItem>";
1278 break;
1279
546bfbea 1280 case wxITEM_RADIO:
d65c269b
VZ
1281 if ( m_pathLastRadio.empty() )
1282 {
1283 // start of a new radio group
1284 item_type = "<RadioItem>";
2b5f62a0
VZ
1285 wxString tmp( wxGTK_CONV_BACK( bufPath ) );
1286 tmp.Remove(0,1);
1287 m_pathLastRadio = tmp;
d65c269b
VZ
1288 }
1289 else // continue the radio group
1290 {
1291 pathRadio = m_pathLastRadio;
fab591c5
RR
1292 pathRadio.Replace(wxT("_"), wxT(""));
1293 pathRadio.Prepend(wxT("<main>/"));
982f23fd 1294
8f262dc5
VZ
1295 strncpy(bufType, wxGTK_CONV(pathRadio), WXSIZEOF(bufType));
1296 bufType[WXSIZEOF(bufType) - 1] = '\0';
1297 item_type = bufType;
d65c269b
VZ
1298 }
1299
24bcaec3
VZ
1300 // continue the existing radio group, if any
1301 endOfRadioGroup = FALSE;
d65c269b
VZ
1302 break;
1303
1304 default:
1305 wxFAIL_MSG( _T("unexpected menu item kind") );
1306 // fall through
1307
546bfbea 1308 case wxITEM_NORMAL:
d65c269b
VZ
1309 item_type = "<Item>";
1310 break;
1311 }
1312
1313 entry.item_type = (char *)item_type; // cast needed for GTK+
a583e623 1314 entry.accelerator = (gchar*) NULL;
23280650 1315
974e8d94 1316#if wxUSE_ACCEL
717a57c2
VZ
1317 // due to an apparent bug in GTK+, we have to use a static buffer here -
1318 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
1319 // somehow! (VZ)
982f23fd 1320 char s_accel[50]; // should be big enough, we check for overruns
3ca6a5f0 1321 wxString tmp( GetHotKey(*mitem) );
fab591c5 1322 strncpy(s_accel, wxGTK_CONV( tmp ), WXSIZEOF(s_accel));
982f23fd 1323 s_accel[WXSIZEOF(s_accel) - 1] = '\0';
717a57c2
VZ
1324 entry.accelerator = s_accel;
1325#else // !wxUSE_ACCEL
1326 entry.accelerator = (char*) NULL;
1327#endif // wxUSE_ACCEL/!wxUSE_ACCEL
96fd301f 1328
717a57c2 1329 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
cf7a7e13 1330
717a57c2 1331 wxString path( mitem->GetFactoryPath() );
fab591c5 1332 menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) );
a01fe3d6 1333
2b5f62a0
VZ
1334 if (!menuItem)
1335 wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() );
717a57c2 1336 }
23280650 1337
717a57c2
VZ
1338 if ( !mitem->IsSeparator() )
1339 {
2b5f62a0 1340 wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
a01fe3d6 1341
717a57c2
VZ
1342 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1343 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1344 (gpointer)this );
837904f2 1345
717a57c2
VZ
1346 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1347 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1348 (gpointer)this );
1349 }
23280650 1350
837904f2 1351 mitem->SetMenuItem(menuItem);
837904f2 1352
24bcaec3 1353 if ( endOfRadioGroup )
d65c269b
VZ
1354 {
1355 m_pathLastRadio.clear();
1356 }
d65c269b 1357
32db328c 1358 return TRUE;
6de97a3b 1359}
c801d85f 1360
9add9367 1361wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
828f655f 1362{
9add9367
RD
1363 if (!GtkAppend(mitem))
1364 return NULL;
1365 return wxMenuBase::DoAppend(mitem);
c33c4050
RR
1366}
1367
9add9367 1368wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 1369{
717a57c2 1370 if ( !wxMenuBase::DoInsert(pos, item) )
9add9367 1371 return NULL;
c626a8b7 1372
32db328c
VZ
1373 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
1374 // of version 1.2.6), so we first append the item and then change its
1375 // index
1376 if ( !GtkAppend(item) )
9add9367 1377 return NULL;
32db328c 1378
f6bcfd97
BP
1379 if ( m_style & wxMENU_TEAROFF )
1380 {
1381 // change the position as the first item is the tear-off marker
1382 pos++;
1383 }
1384
32db328c
VZ
1385 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
1386 gpointer data = g_list_last(menu_shell->children)->data;
1387 menu_shell->children = g_list_remove(menu_shell->children, data);
1388 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
1389
9add9367 1390 return item;
c33c4050
RR
1391}
1392
717a57c2 1393wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 1394{
717a57c2
VZ
1395 if ( !wxMenuBase::DoRemove(item) )
1396 return (wxMenuItem *)NULL;
c626a8b7 1397
717a57c2
VZ
1398 // TODO: this code doesn't delete the item factory item and this seems
1399 // impossible as of GTK 1.2.6.
1400 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 1401
717a57c2 1402 return item;
c33c4050
RR
1403}
1404
96fd301f
VZ
1405int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1406{
222ed1d6 1407 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
83624f79
RR
1408 while (node)
1409 {
b1d4dd7a 1410 wxMenuItem *item = node->GetData();
83624f79
RR
1411 if (item->GetMenuItem() == menuItem)
1412 return item->GetId();
b1d4dd7a 1413 node = node->GetNext();
83624f79 1414 }
96fd301f 1415
c626a8b7 1416 return wxNOT_FOUND;
6de97a3b 1417}
c801d85f 1418
717a57c2
VZ
1419// ----------------------------------------------------------------------------
1420// helpers
1421// ----------------------------------------------------------------------------
1422
9e691f46 1423#if GTK_CHECK_VERSION(1, 2, 0) && wxUSE_ACCEL
a070d8ce 1424
717a57c2 1425static wxString GetHotKey( const wxMenuItem& item )
c801d85f 1426{
717a57c2
VZ
1427 wxString hotkey;
1428
1429 wxAcceleratorEntry *accel = item.GetAccel();
1430 if ( accel )
83624f79 1431 {
717a57c2
VZ
1432 int flags = accel->GetFlags();
1433 if ( flags & wxACCEL_ALT )
1434 hotkey += wxT("<alt>");
1435 if ( flags & wxACCEL_CTRL )
1436 hotkey += wxT("<control>");
1437 if ( flags & wxACCEL_SHIFT )
1438 hotkey += wxT("<shift>");
1439
1440 int code = accel->GetKeyCode();
1441 switch ( code )
c626a8b7 1442 {
717a57c2
VZ
1443 case WXK_F1:
1444 case WXK_F2:
1445 case WXK_F3:
1446 case WXK_F4:
1447 case WXK_F5:
1448 case WXK_F6:
1449 case WXK_F7:
1450 case WXK_F8:
1451 case WXK_F9:
1452 case WXK_F10:
1453 case WXK_F11:
1454 case WXK_F12:
1455 hotkey << wxT('F') << code - WXK_F1 + 1;
1456 break;
2368dcda 1457
a070d8ce
VZ
1458 // TODO: we should use gdk_keyval_name() (a.k.a.
1459 // XKeysymToString) here as well as hardcoding the keysym
1460 // names this might be not portable
3ca6a5f0
BP
1461 case WXK_NUMPAD_INSERT:
1462 hotkey << wxT("KP_Insert" );
1463 break;
1464 case WXK_NUMPAD_DELETE:
1465 hotkey << wxT("KP_Delete" );
1466 break;
1467 case WXK_INSERT:
1468 hotkey << wxT("Insert" );
1469 break;
1470 case WXK_DELETE:
1471 hotkey << wxT("Delete" );
1472 break;
2b5f62a0
VZ
1473 case WXK_UP:
1474 hotkey << wxT("Up" );
1475 break;
1476 case WXK_DOWN:
1477 hotkey << wxT("Down" );
1478 break;
1479 case WXK_PAGEUP:
dac3065d 1480 case WXK_PRIOR:
2b5f62a0
VZ
1481 hotkey << wxT("Prior" );
1482 break;
1483 case WXK_PAGEDOWN:
dac3065d 1484 case WXK_NEXT:
2b5f62a0
VZ
1485 hotkey << wxT("Next" );
1486 break;
1487 case WXK_LEFT:
1488 hotkey << wxT("Left" );
1489 break;
1490 case WXK_RIGHT:
1491 hotkey << wxT("Right" );
1492 break;
1493 case WXK_HOME:
1494 hotkey << wxT("Home" );
1495 break;
1496 case WXK_END:
1497 hotkey << wxT("End" );
1498 break;
1499 case WXK_RETURN:
1500 hotkey << wxT("Return" );
1501 break;
96fd301f 1502
a070d8ce
VZ
1503 // if there are any other keys wxGetAccelFromString() may
1504 // return, we should process them here
8bbe427f 1505
717a57c2 1506 default:
a070d8ce 1507 if ( code < 127 )
717a57c2 1508 {
2b5f62a0 1509 wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) );
a070d8ce
VZ
1510 if ( name )
1511 {
1512 hotkey << name;
1513 break;
1514 }
717a57c2 1515 }
c801d85f 1516
717a57c2
VZ
1517 wxFAIL_MSG( wxT("unknown keyboard accel") );
1518 }
c801d85f 1519
717a57c2 1520 delete accel;
631f1bfe 1521 }
717a57c2
VZ
1522
1523 return hotkey;
631f1bfe 1524}
a070d8ce 1525
717a57c2 1526#endif // wxUSE_ACCEL
631f1bfe 1527
37d403aa 1528
cc47eed9
RR
1529//-----------------------------------------------------------------------------
1530// substitute for missing GtkPixmapMenuItem
1531//-----------------------------------------------------------------------------
37d403aa 1532
9e691f46
VZ
1533#ifdef USE_MENU_BITMAPS
1534
37d403aa
JS
1535/*
1536 * Copyright (C) 1998, 1999, 2000 Free Software Foundation
1537 * All rights reserved.
1538 *
1539 * This file is part of the Gnome Library.
1540 *
1541 * The Gnome Library is free software; you can redistribute it and/or
1542 * modify it under the terms of the GNU Library General Public License as
1543 * published by the Free Software Foundation; either version 2 of the
1544 * License, or (at your option) any later version.
1545 *
1546 * The Gnome Library is distributed in the hope that it will be useful,
1547 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1548 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1549 * Library General Public License for more details.
1550 *
1551 * You should have received a copy of the GNU Library General Public
1552 * License along with the Gnome Library; see the file COPYING.LIB. If not,
1553 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1554 * Boston, MA 02111-1307, USA.
1555 */
1556/*
1557 @NOTATION@
1558 */
1559
1560/* Author: Dietmar Maurer <dm@vlsivie.tuwien.ac.at> */
1561
37d403aa
JS
1562#include <gtk/gtkaccellabel.h>
1563#include <gtk/gtksignal.h>
1564#include <gtk/gtkmenuitem.h>
1565#include <gtk/gtkmenu.h>
1566#include <gtk/gtkcontainer.h>
1567
90350682
VZ
1568extern "C"
1569{
1570
37d403aa
JS
1571static void gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass);
1572static void gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item);
1573static void gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1574 GdkRectangle *area);
37d403aa 1575static gint gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1576 GdkEventExpose *event);
37d403aa
JS
1577
1578/* we must override the following functions */
1579
1580static void gtk_pixmap_menu_item_map (GtkWidget *widget);
1581static void gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1582 GtkAllocation *allocation);
37d403aa 1583static void gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1584 gboolean include_internals,
1585 GtkCallback callback,
1586 gpointer callback_data);
37d403aa 1587static void gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1588 GtkRequisition *requisition);
37d403aa 1589static void gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1590 GtkWidget *child);
37d403aa
JS
1591
1592static void changed_have_pixmap_status (GtkPixmapMenuItem *menu_item);
1593
1594static GtkMenuItemClass *parent_class = NULL;
1595
90350682
VZ
1596}
1597
37d403aa
JS
1598#define BORDER_SPACING 3
1599#define PMAP_WIDTH 20
1600
1601GtkType
1602gtk_pixmap_menu_item_get_type (void)
1603{
1604 static GtkType pixmap_menu_item_type = 0;
1605
1606 if (!pixmap_menu_item_type)
1607 {
1608 GtkTypeInfo pixmap_menu_item_info =
1609 {
90350682 1610 (char *)"GtkPixmapMenuItem",
37d403aa
JS
1611 sizeof (GtkPixmapMenuItem),
1612 sizeof (GtkPixmapMenuItemClass),
1613 (GtkClassInitFunc) gtk_pixmap_menu_item_class_init,
1614 (GtkObjectInitFunc) gtk_pixmap_menu_item_init,
1615 /* reserved_1 */ NULL,
1616 /* reserved_2 */ NULL,
1617 (GtkClassInitFunc) NULL,
1618 };
1619
2368dcda 1620 pixmap_menu_item_type = gtk_type_unique (gtk_menu_item_get_type (),
8f262dc5 1621 &pixmap_menu_item_info);
37d403aa
JS
1622 }
1623
1624 return pixmap_menu_item_type;
1625}
1626
1627/**
1628 * gtk_pixmap_menu_item_new
1629 *
2368dcda 1630 * Creates a new pixmap menu item. Use gtk_pixmap_menu_item_set_pixmap()
37d403aa
JS
1631 * to set the pixmap wich is displayed at the left side.
1632 *
1633 * Returns:
1634 * &GtkWidget pointer to new menu item
1635 **/
1636
1637GtkWidget*
1638gtk_pixmap_menu_item_new (void)
1639{
1640 return GTK_WIDGET (gtk_type_new (gtk_pixmap_menu_item_get_type ()));
1641}
1642
1643static void
1644gtk_pixmap_menu_item_class_init (GtkPixmapMenuItemClass *klass)
1645{
1646 GtkObjectClass *object_class;
1647 GtkWidgetClass *widget_class;
1648 GtkMenuItemClass *menu_item_class;
1649 GtkContainerClass *container_class;
1650
1651 object_class = (GtkObjectClass*) klass;
1652 widget_class = (GtkWidgetClass*) klass;
1653 menu_item_class = (GtkMenuItemClass*) klass;
1654 container_class = (GtkContainerClass*) klass;
1655
1656 parent_class = (GtkMenuItemClass*) gtk_type_class (gtk_menu_item_get_type ());
1657
1658 widget_class->draw = gtk_pixmap_menu_item_draw;
1659 widget_class->expose_event = gtk_pixmap_menu_item_expose;
1660 widget_class->map = gtk_pixmap_menu_item_map;
1661 widget_class->size_allocate = gtk_pixmap_menu_item_size_allocate;
1662 widget_class->size_request = gtk_pixmap_menu_item_size_request;
1663
1664 container_class->forall = gtk_pixmap_menu_item_forall;
1665 container_class->remove = gtk_pixmap_menu_item_remove;
1666
1667 klass->orig_toggle_size = menu_item_class->toggle_size;
1668 klass->have_pixmap_count = 0;
1669}
1670
1671static void
1672gtk_pixmap_menu_item_init (GtkPixmapMenuItem *menu_item)
1673{
1674 GtkMenuItem *mi;
1675
1676 mi = GTK_MENU_ITEM (menu_item);
1677
1678 menu_item->pixmap = NULL;
1679}
1680
1681static void
1682gtk_pixmap_menu_item_draw (GtkWidget *widget,
8f262dc5 1683 GdkRectangle *area)
37d403aa
JS
1684{
1685 g_return_if_fail (widget != NULL);
1686 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1687 g_return_if_fail (area != NULL);
1688
1689 if (GTK_WIDGET_CLASS (parent_class)->draw)
1690 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
1691
2368dcda 1692 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1693 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1694 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1695 }
1696}
1697
1698static gint
1699gtk_pixmap_menu_item_expose (GtkWidget *widget,
8f262dc5 1700 GdkEventExpose *event)
37d403aa
JS
1701{
1702 g_return_val_if_fail (widget != NULL, FALSE);
1703 g_return_val_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget), FALSE);
1704 g_return_val_if_fail (event != NULL, FALSE);
1705
1706 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
1707 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1708
2368dcda 1709 if (GTK_WIDGET_DRAWABLE (widget) &&
37d403aa
JS
1710 GTK_PIXMAP_MENU_ITEM(widget)->pixmap) {
1711 gtk_widget_draw(GTK_WIDGET(GTK_PIXMAP_MENU_ITEM(widget)->pixmap),NULL);
1712 }
1713
1714 return FALSE;
1715}
1716
1717/**
1718 * gtk_pixmap_menu_item_set_pixmap
1719 * @menu_item: Pointer to the pixmap menu item
1720 * @pixmap: Pointer to a pixmap widget
1721 *
1722 * Set the pixmap of the menu item.
1723 *
1724 **/
1725
1726void
1727gtk_pixmap_menu_item_set_pixmap (GtkPixmapMenuItem *menu_item,
8f262dc5 1728 GtkWidget *pixmap)
37d403aa
JS
1729{
1730 g_return_if_fail (menu_item != NULL);
1731 g_return_if_fail (pixmap != NULL);
1732 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (menu_item));
1733 g_return_if_fail (GTK_IS_WIDGET (pixmap));
1734 g_return_if_fail (menu_item->pixmap == NULL);
1735
1736 gtk_widget_set_parent (pixmap, GTK_WIDGET (menu_item));
1737 menu_item->pixmap = pixmap;
1738
1739 if (GTK_WIDGET_REALIZED (pixmap->parent) &&
1740 !GTK_WIDGET_REALIZED (pixmap))
1741 gtk_widget_realize (pixmap);
2368dcda
VZ
1742
1743 if (GTK_WIDGET_VISIBLE (pixmap->parent)) {
37d403aa 1744 if (GTK_WIDGET_MAPPED (pixmap->parent) &&
8f262dc5 1745 GTK_WIDGET_VISIBLE(pixmap) &&
37d403aa
JS
1746 !GTK_WIDGET_MAPPED (pixmap))
1747 gtk_widget_map (pixmap);
1748 }
1749
1750 changed_have_pixmap_status(menu_item);
2368dcda 1751
37d403aa
JS
1752 if (GTK_WIDGET_VISIBLE (pixmap) && GTK_WIDGET_VISIBLE (menu_item))
1753 gtk_widget_queue_resize (pixmap);
1754}
1755
1756static void
1757gtk_pixmap_menu_item_map (GtkWidget *widget)
1758{
1759 GtkPixmapMenuItem *menu_item;
1760
1761 g_return_if_fail (widget != NULL);
1762 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (widget));
1763
1764 menu_item = GTK_PIXMAP_MENU_ITEM(widget);
1765
1766 GTK_WIDGET_CLASS(parent_class)->map(widget);
1767
1768 if (menu_item->pixmap &&
1769 GTK_WIDGET_VISIBLE (menu_item->pixmap) &&
1770 !GTK_WIDGET_MAPPED (menu_item->pixmap))
1771 gtk_widget_map (menu_item->pixmap);
1772}
1773
1774static void
1775gtk_pixmap_menu_item_size_allocate (GtkWidget *widget,
8f262dc5 1776 GtkAllocation *allocation)
37d403aa
JS
1777{
1778 GtkPixmapMenuItem *pmenu_item;
1779
1780 pmenu_item = GTK_PIXMAP_MENU_ITEM(widget);
1781
1782 if (pmenu_item->pixmap && GTK_WIDGET_VISIBLE(pmenu_item))
1783 {
1784 GtkAllocation child_allocation;
1785 int border_width;
1786
1787 border_width = GTK_CONTAINER (widget)->border_width;
1788
1789 child_allocation.width = pmenu_item->pixmap->requisition.width;
1790 child_allocation.height = pmenu_item->pixmap->requisition.height;
1791 child_allocation.x = border_width + BORDER_SPACING;
1792 child_allocation.y = (border_width + BORDER_SPACING
8f262dc5
VZ
1793 + (((allocation->height - child_allocation.height) - child_allocation.x)
1794 / 2)); /* center pixmaps vertically */
37d403aa
JS
1795 gtk_widget_size_allocate (pmenu_item->pixmap, &child_allocation);
1796 }
1797
1798 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
1799 GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation);
1800}
1801
1802static void
1803gtk_pixmap_menu_item_forall (GtkContainer *container,
8f262dc5
VZ
1804 gboolean include_internals,
1805 GtkCallback callback,
1806 gpointer callback_data)
37d403aa
JS
1807{
1808 GtkPixmapMenuItem *menu_item;
1809
1810 g_return_if_fail (container != NULL);
1811 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1812 g_return_if_fail (callback != NULL);
1813
1814 menu_item = GTK_PIXMAP_MENU_ITEM (container);
1815
1816 if (menu_item->pixmap)
1817 (* callback) (menu_item->pixmap, callback_data);
1818
1819 GTK_CONTAINER_CLASS(parent_class)->forall(container,include_internals,
8f262dc5 1820 callback,callback_data);
37d403aa
JS
1821}
1822
1823static void
1824gtk_pixmap_menu_item_size_request (GtkWidget *widget,
8f262dc5 1825 GtkRequisition *requisition)
37d403aa
JS
1826{
1827 GtkPixmapMenuItem *menu_item;
1828 GtkRequisition req = {0, 0};
1829
1830 g_return_if_fail (widget != NULL);
1831 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1832 g_return_if_fail (requisition != NULL);
1833
1834 GTK_WIDGET_CLASS(parent_class)->size_request(widget,requisition);
1835
1836 menu_item = GTK_PIXMAP_MENU_ITEM (widget);
2368dcda 1837
37d403aa
JS
1838 if (menu_item->pixmap)
1839 gtk_widget_size_request(menu_item->pixmap, &req);
1840
1841 requisition->height = MAX(req.height + GTK_CONTAINER(widget)->border_width + BORDER_SPACING, (unsigned int) requisition->height);
1842 requisition->width += (req.width + GTK_CONTAINER(widget)->border_width + BORDER_SPACING);
1843}
1844
1845static void
1846gtk_pixmap_menu_item_remove (GtkContainer *container,
8f262dc5 1847 GtkWidget *child)
37d403aa
JS
1848{
1849 GtkBin *bin;
1850 gboolean widget_was_visible;
1851
1852 g_return_if_fail (container != NULL);
1853 g_return_if_fail (GTK_IS_PIXMAP_MENU_ITEM (container));
1854 g_return_if_fail (child != NULL);
1855 g_return_if_fail (GTK_IS_WIDGET (child));
1856
1857 bin = GTK_BIN (container);
2368dcda 1858 g_return_if_fail ((bin->child == child ||
8f262dc5 1859 (GTK_PIXMAP_MENU_ITEM(container)->pixmap == child)));
37d403aa
JS
1860
1861 widget_was_visible = GTK_WIDGET_VISIBLE (child);
2368dcda 1862
37d403aa
JS
1863 gtk_widget_unparent (child);
1864 if (bin->child == child)
2368dcda 1865 bin->child = NULL;
37d403aa
JS
1866 else {
1867 GTK_PIXMAP_MENU_ITEM(container)->pixmap = NULL;
1868 changed_have_pixmap_status(GTK_PIXMAP_MENU_ITEM(container));
1869 }
2368dcda 1870
37d403aa
JS
1871 if (widget_was_visible)
1872 gtk_widget_queue_resize (GTK_WIDGET (container));
1873}
1874
1875
1876/* important to only call this if there was actually a _change_ in pixmap == NULL */
1877static void
1878changed_have_pixmap_status (GtkPixmapMenuItem *menu_item)
1879{
1880 if (menu_item->pixmap != NULL) {
1881 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count += 1;
1882
1883 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 1) {
1884 /* Install pixmap toggle size */
1885 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = MAX(GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size, PMAP_WIDTH);
1886 }
1887 } else {
1888 GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count -= 1;
1889
1890 if (GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->have_pixmap_count == 0) {
1891 /* Install normal toggle size */
2368dcda 1892 GTK_MENU_ITEM_GET_CLASS(menu_item)->toggle_size = GTK_PIXMAP_MENU_ITEM_GET_CLASS(menu_item)->orig_toggle_size;
37d403aa
JS
1893 }
1894 }
1895
1896 /* Note that we actually need to do this for _all_ GtkPixmapMenuItem
1897 whenever the klass->toggle_size changes; but by doing it anytime
1898 this function is called, we get the same effect, just because of
1899 how the preferences option to show pixmaps works. Bogus, broken.
1900 */
2368dcda 1901 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(menu_item)))
37d403aa
JS
1902 gtk_widget_queue_resize(GTK_WIDGET(menu_item));
1903}
1904
9e691f46 1905#endif // USE_MENU_BITMAPS
37d403aa 1906