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