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