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