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