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