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