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