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