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