]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/gtk/menu.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
96fd301f | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
e1bf3ad3 | 13 | #include "wx/menu.h" |
88a7a4e1 WS |
14 | |
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/intl.h" | |
e4db172a | 17 | #include "wx/log.h" |
d281df50 | 18 | #include "wx/frame.h" |
0bca0373 | 19 | #include "wx/bitmap.h" |
7d02e0d6 | 20 | #include "wx/app.h" |
88a7a4e1 WS |
21 | #endif |
22 | ||
d281df50 | 23 | #include "wx/accel.h" |
ab73fe8d | 24 | #include "wx/stockitem.h" |
9e691f46 VZ |
25 | #include "wx/gtk/private.h" |
26 | ||
bcf881ef JS |
27 | #ifdef __WXGTK20__ |
28 | #include <gdk/gdktypes.h> | |
29 | #endif | |
30 | ||
9e691f46 | 31 | // FIXME: is this right? somehow I don't think so (VZ) |
0d2c4443 PC |
32 | |
33 | #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g)) | |
34 | //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g)) | |
35 | //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m) | |
36 | ||
37 | #define ACCEL_OBJECT GtkWindow | |
38 | #define ACCEL_OBJECTS(a) (a)->acceleratables | |
39 | #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj) | |
83624f79 | 40 | |
9959e2b6 VZ |
41 | // we use normal item but with a special id for the menu title |
42 | static const int wxGTK_TITLE_ID = -3; | |
43 | ||
acfd422a RR |
44 | //----------------------------------------------------------------------------- |
45 | // idle system | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
6d971354 | 48 | #if wxUSE_ACCEL |
98f29783 | 49 | static wxString GetGtkHotKey( const wxMenuItem& item ); |
717a57c2 VZ |
50 | #endif |
51 | ||
f6bcfd97 BP |
52 | //----------------------------------------------------------------------------- |
53 | // idle system | |
54 | //----------------------------------------------------------------------------- | |
55 | ||
56 | static wxString wxReplaceUnderscore( const wxString& title ) | |
57 | { | |
6d971354 | 58 | // GTK 1.2 wants to have "_" instead of "&" for accelerators |
f6bcfd97 | 59 | wxString str; |
86501081 VS |
60 | |
61 | for ( wxString::const_iterator pc = title.begin(); pc != title.end(); ++pc ) | |
f6bcfd97 | 62 | { |
86501081 | 63 | if ((*pc == wxT('&')) && (pc+1 != title.end()) && (*(pc+1) == wxT('&'))) |
2b5f62a0 VZ |
64 | { |
65 | // "&" is doubled to indicate "&" instead of accelerator | |
66 | ++pc; | |
67 | str << wxT('&'); | |
68 | } | |
69 | else if (*pc == wxT('&')) | |
f6bcfd97 | 70 | { |
f6bcfd97 | 71 | str << wxT('_'); |
4e9cbd33 | 72 | } |
f6bcfd97 BP |
73 | else |
74 | { | |
f6bcfd97 BP |
75 | if ( *pc == wxT('_') ) |
76 | { | |
77 | // underscores must be doubled to prevent them from being | |
78 | // interpreted as accelerator character prefix by GTK | |
79 | str << *pc; | |
80 | } | |
f6bcfd97 BP |
81 | |
82 | str << *pc; | |
83 | } | |
84 | } | |
9959e2b6 | 85 | |
6d971354 | 86 | // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() ); |
9959e2b6 | 87 | |
f6bcfd97 BP |
88 | return str; |
89 | } | |
90 | ||
e6396ed4 JS |
91 | //----------------------------------------------------------------------------- |
92 | // activate message from GTK | |
93 | //----------------------------------------------------------------------------- | |
94 | ||
cdf003d4 | 95 | static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event) |
e6396ed4 | 96 | { |
e6396ed4 JS |
97 | event.SetEventObject( menu ); |
98 | ||
a01fe3d6 RD |
99 | wxEvtHandler* handler = menu->GetEventHandler(); |
100 | if (handler && handler->ProcessEvent(event)) | |
e6396ed4 JS |
101 | return; |
102 | ||
103 | wxWindow *win = menu->GetInvokingWindow(); | |
cdf003d4 VZ |
104 | if (win) |
105 | win->GetEventHandler()->ProcessEvent( event ); | |
106 | } | |
107 | ||
108 | extern "C" { | |
109 | ||
110 | static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu ) | |
111 | { | |
112 | wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu); | |
113 | ||
114 | DoCommonMenuCallbackCode(menu, event); | |
115 | } | |
116 | ||
13d35112 | 117 | static void gtk_menu_close_callback( GtkWidget *widget, wxMenuBar *menubar ) |
cdf003d4 | 118 | { |
13d35112 VZ |
119 | if ( !menubar->GetMenuCount() ) |
120 | { | |
121 | // if menubar is empty we can't call GetMenu(0) below | |
122 | return; | |
123 | } | |
cdf003d4 | 124 | |
13d35112 VZ |
125 | wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL ); |
126 | ||
127 | DoCommonMenuCallbackCode(menubar->GetMenu(0), event); | |
e6396ed4 | 128 | } |
cdf003d4 | 129 | |
865bb325 | 130 | } |
e6396ed4 | 131 | |
c801d85f KB |
132 | //----------------------------------------------------------------------------- |
133 | // wxMenuBar | |
134 | //----------------------------------------------------------------------------- | |
135 | ||
136 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
137 | ||
294ea16d | 138 | void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style) |
3502e687 | 139 | { |
ae53c98c | 140 | m_style = style; |
e8375af8 | 141 | m_invokingWindow = NULL; |
23280650 | 142 | |
e8375af8 VZ |
143 | if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) || |
144 | !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) | |
4dcaf11a | 145 | { |
223d09f6 | 146 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 147 | return; |
4dcaf11a | 148 | } |
3502e687 | 149 | |
3502e687 RR |
150 | m_menubar = gtk_menu_bar_new(); |
151 | ||
152 | if (style & wxMB_DOCKABLE) | |
153 | { | |
154 | m_widget = gtk_handle_box_new(); | |
10bd1f7d PC |
155 | gtk_container_add(GTK_CONTAINER(m_widget), m_menubar); |
156 | gtk_widget_show(m_menubar); | |
3502e687 RR |
157 | } |
158 | else | |
159 | { | |
10bd1f7d | 160 | m_widget = m_menubar; |
3502e687 RR |
161 | } |
162 | ||
163 | PostCreation(); | |
c4608a8a | 164 | |
db434467 | 165 | ApplyWidgetStyle(); |
294ea16d VZ |
166 | |
167 | for (size_t i = 0; i < n; ++i ) | |
168 | Append(menus[i], titles[i]); | |
13d35112 VZ |
169 | |
170 | // VZ: for some reason connecting to menus "deactivate" doesn't work (we | |
171 | // don't get it when the menu is dismissed by clicking outside the | |
172 | // toolbar) so we connect to the global one, even if it means that we | |
173 | // can't pass the menu which was closed in wxMenuEvent object | |
9fa72bd2 MR |
174 | g_signal_connect (m_menubar, "deactivate", |
175 | G_CALLBACK (gtk_menu_close_callback), this); | |
13d35112 | 176 | |
3502e687 RR |
177 | } |
178 | ||
294ea16d | 179 | wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style) |
c801d85f | 180 | { |
294ea16d VZ |
181 | Init(n, menus, titles, style); |
182 | } | |
96fd301f | 183 | |
294ea16d VZ |
184 | wxMenuBar::wxMenuBar(long style) |
185 | { | |
186 | Init(0, NULL, NULL, style); | |
187 | } | |
c4608a8a | 188 | |
294ea16d VZ |
189 | wxMenuBar::wxMenuBar() |
190 | { | |
191 | Init(0, NULL, NULL, 0); | |
6de97a3b | 192 | } |
c801d85f | 193 | |
1e133b7d RR |
194 | wxMenuBar::~wxMenuBar() |
195 | { | |
1e133b7d RR |
196 | } |
197 | ||
5bd9e519 RR |
198 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
199 | { | |
200 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
201 | ||
5bd9e519 | 202 | wxWindow *top_frame = win; |
8487f887 | 203 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 204 | top_frame = top_frame->GetParent(); |
5bd9e519 | 205 | |
222ed1d6 | 206 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
207 | while (node) |
208 | { | |
1987af7e | 209 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 RR |
210 | if (menuitem->IsSubMenu()) |
211 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
1987af7e | 212 | node = node->GetNext(); |
5bd9e519 RR |
213 | } |
214 | } | |
215 | ||
216 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
217 | { | |
218 | menu->SetInvokingWindow( win ); | |
219 | ||
5bd9e519 | 220 | wxWindow *top_frame = win; |
8487f887 | 221 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 222 | top_frame = top_frame->GetParent(); |
5bd9e519 | 223 | |
6d971354 | 224 | // support for native hot keys |
9e691f46 VZ |
225 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); |
226 | if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) ) | |
b4da05a6 | 227 | gtk_accel_group_attach( menu->m_accel, obj ); |
5bd9e519 | 228 | |
222ed1d6 | 229 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
230 | while (node) |
231 | { | |
1987af7e | 232 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 RR |
233 | if (menuitem->IsSubMenu()) |
234 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
1987af7e | 235 | node = node->GetNext(); |
5bd9e519 RR |
236 | } |
237 | } | |
238 | ||
239 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
240 | { | |
9c884972 | 241 | m_invokingWindow = win; |
5bd9e519 | 242 | wxWindow *top_frame = win; |
8487f887 | 243 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 244 | top_frame = top_frame->GetParent(); |
5bd9e519 | 245 | |
222ed1d6 | 246 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
5bd9e519 RR |
247 | while (node) |
248 | { | |
1987af7e | 249 | wxMenu *menu = node->GetData(); |
5bd9e519 | 250 | wxMenubarSetInvokingWindow( menu, win ); |
1987af7e | 251 | node = node->GetNext(); |
5bd9e519 RR |
252 | } |
253 | } | |
254 | ||
978af864 VZ |
255 | void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir) |
256 | { | |
257 | if ( dir == wxLayout_Default ) | |
258 | { | |
259 | const wxWindow *const frame = GetFrame(); | |
260 | if ( frame ) | |
261 | { | |
262 | // inherit layout from frame. | |
263 | dir = frame->GetLayoutDirection(); | |
264 | } | |
265 | else // use global layout | |
266 | { | |
267 | dir = wxTheApp->GetLayoutDirection(); | |
268 | } | |
269 | } | |
270 | ||
271 | if ( dir == wxLayout_Default ) | |
272 | return; | |
273 | ||
274 | GTKSetLayout(m_menubar, dir); | |
275 | ||
276 | // also set the layout of all menus we already have (new ones will inherit | |
277 | // the current layout) | |
278 | for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
279 | node; | |
280 | node = node->GetNext() ) | |
281 | { | |
282 | wxMenu *const menu = node->GetData(); | |
283 | menu->SetLayoutDirection(dir); | |
284 | } | |
285 | } | |
286 | ||
287 | wxLayoutDirection wxMenuBar::GetLayoutDirection() const | |
288 | { | |
289 | return GTKGetLayout(m_menubar); | |
290 | } | |
291 | ||
292 | void wxMenuBar::Attach(wxFrame *frame) | |
293 | { | |
294 | wxMenuBarBase::Attach(frame); | |
295 | ||
296 | SetLayoutDirection(wxLayout_Default); | |
297 | } | |
298 | ||
5bd9e519 RR |
299 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) |
300 | { | |
9c884972 | 301 | m_invokingWindow = (wxWindow*) NULL; |
5bd9e519 | 302 | wxWindow *top_frame = win; |
8487f887 | 303 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
bd77da97 | 304 | top_frame = top_frame->GetParent(); |
9959e2b6 | 305 | |
222ed1d6 | 306 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
5bd9e519 RR |
307 | while (node) |
308 | { | |
1987af7e | 309 | wxMenu *menu = node->GetData(); |
5bd9e519 | 310 | wxMenubarUnsetInvokingWindow( menu, win ); |
1987af7e | 311 | node = node->GetNext(); |
5bd9e519 RR |
312 | } |
313 | } | |
314 | ||
3dfac970 | 315 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
c801d85f | 316 | { |
f03ec224 | 317 | if ( !wxMenuBarBase::Append( menu, title ) ) |
670f9935 | 318 | return false; |
f03ec224 VZ |
319 | |
320 | return GtkAppend(menu, title); | |
321 | } | |
23280650 | 322 | |
49826dab | 323 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) |
f03ec224 | 324 | { |
f6bcfd97 | 325 | wxString str( wxReplaceUnderscore( title ) ); |
1e133b7d | 326 | |
d9e403cc | 327 | // This doesn't have much effect right now. |
1e133b7d | 328 | menu->SetTitle( str ); |
23280650 | 329 | |
6d971354 | 330 | // The "m_owner" is the "menu item" |
6d971354 | 331 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); |
978af864 | 332 | menu->SetLayoutDirection(GetLayoutDirection()); |
96fd301f | 333 | |
2b1c162e | 334 | gtk_widget_show( menu->m_owner ); |
9959e2b6 | 335 | |
2b1c162e | 336 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
9959e2b6 | 337 | |
49826dab RD |
338 | if (pos == -1) |
339 | gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner ); | |
340 | else | |
341 | gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos ); | |
9959e2b6 | 342 | |
9fa72bd2 MR |
343 | g_signal_connect (menu->m_owner, "activate", |
344 | G_CALLBACK (gtk_menu_open_callback), | |
345 | menu); | |
e6396ed4 | 346 | |
9c884972 | 347 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables |
2b5f62a0 | 348 | // addings menu later on. |
9c884972 | 349 | if (m_invokingWindow) |
2b5f62a0 | 350 | { |
9c884972 | 351 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); |
3dfac970 | 352 | |
2b5f62a0 VZ |
353 | // OPTIMISE ME: we should probably cache this, or pass it |
354 | // directly, but for now this is a minimal | |
355 | // change to validate the new dynamic sizing. | |
356 | // see (and refactor :) similar code in Remove | |
357 | // below. | |
358 | ||
d9e403cc | 359 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); |
2b5f62a0 VZ |
360 | |
361 | if( frame ) | |
362 | frame->UpdateMenuBarSize(); | |
363 | } | |
364 | ||
670f9935 | 365 | return true; |
3dfac970 VZ |
366 | } |
367 | ||
368 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
369 | { | |
370 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
670f9935 | 371 | return false; |
3dfac970 | 372 | |
6d971354 | 373 | // TODO |
9959e2b6 | 374 | |
49826dab | 375 | if ( !GtkAppend(menu, title, (int)pos) ) |
670f9935 | 376 | return false; |
f03ec224 | 377 | |
670f9935 | 378 | return true; |
3dfac970 VZ |
379 | } |
380 | ||
381 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
382 | { | |
f03ec224 VZ |
383 | // remove the old item and insert a new one |
384 | wxMenu *menuOld = Remove(pos); | |
385 | if ( menuOld && !Insert(pos, menu, title) ) | |
386 | { | |
1d62a8b4 | 387 | return (wxMenu*) NULL; |
f03ec224 | 388 | } |
3dfac970 | 389 | |
f03ec224 VZ |
390 | // either Insert() succeeded or Remove() failed and menuOld is NULL |
391 | return menuOld; | |
3dfac970 VZ |
392 | } |
393 | ||
394 | wxMenu *wxMenuBar::Remove(size_t pos) | |
395 | { | |
f03ec224 VZ |
396 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
397 | if ( !menu ) | |
1d62a8b4 | 398 | return (wxMenu*) NULL; |
f03ec224 | 399 | |
defc0789 VS |
400 | gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) ); |
401 | gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner); | |
c4608a8a | 402 | |
1d62a8b4 | 403 | gtk_widget_destroy( menu->m_owner ); |
defc0789 | 404 | menu->m_owner = NULL; |
c4608a8a | 405 | |
2b5f62a0 VZ |
406 | if (m_invokingWindow) |
407 | { | |
6d971354 RR |
408 | // OPTIMISE ME: see comment in GtkAppend |
409 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); | |
2b5f62a0 | 410 | |
6d971354 | 411 | if( frame ) |
2b5f62a0 VZ |
412 | frame->UpdateMenuBarSize(); |
413 | } | |
414 | ||
1d62a8b4 | 415 | return menu; |
6de97a3b | 416 | } |
96fd301f | 417 | |
716b7364 | 418 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 419 | { |
f6bcfd97 | 420 | if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString)) |
83624f79 RR |
421 | { |
422 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
423 | if (res != wxNOT_FOUND) |
424 | return res; | |
83624f79 | 425 | } |
c626a8b7 | 426 | |
222ed1d6 | 427 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
83624f79 RR |
428 | while (node) |
429 | { | |
1987af7e | 430 | wxMenuItem *item = node->GetData(); |
83624f79 RR |
431 | if (item->IsSubMenu()) |
432 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 433 | |
1987af7e | 434 | node = node->GetNext(); |
83624f79 RR |
435 | } |
436 | ||
c626a8b7 VZ |
437 | return wxNOT_FOUND; |
438 | } | |
439 | ||
c801d85f KB |
440 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const |
441 | { | |
222ed1d6 | 442 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
83624f79 RR |
443 | while (node) |
444 | { | |
1987af7e | 445 | wxMenu *menu = node->GetData(); |
83624f79 | 446 | int res = FindMenuItemRecursive( menu, menuString, itemString); |
1987af7e VZ |
447 | if (res != -1) |
448 | return res; | |
449 | node = node->GetNext(); | |
83624f79 | 450 | } |
1987af7e VZ |
451 | |
452 | return wxNOT_FOUND; | |
6de97a3b | 453 | } |
c801d85f | 454 | |
c626a8b7 | 455 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 456 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 457 | { |
717a57c2 | 458 | wxMenuItem* result = menu->FindChildItem(id); |
716b7364 | 459 | |
222ed1d6 | 460 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
c626a8b7 | 461 | while ( node && result == NULL ) |
83624f79 | 462 | { |
1987af7e | 463 | wxMenuItem *item = node->GetData(); |
83624f79 | 464 | if (item->IsSubMenu()) |
c626a8b7 | 465 | { |
83624f79 | 466 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 467 | } |
1987af7e | 468 | node = node->GetNext(); |
83624f79 | 469 | } |
96fd301f | 470 | |
83624f79 | 471 | return result; |
6de97a3b | 472 | } |
716b7364 | 473 | |
3dfac970 | 474 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const |
716b7364 | 475 | { |
83624f79 | 476 | wxMenuItem* result = 0; |
222ed1d6 | 477 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
83624f79 RR |
478 | while (node && result == 0) |
479 | { | |
1987af7e | 480 | wxMenu *menu = node->GetData(); |
83624f79 | 481 | result = FindMenuItemByIdRecursive( menu, id ); |
1987af7e | 482 | node = node->GetNext(); |
83624f79 | 483 | } |
c626a8b7 | 484 | |
3dfac970 VZ |
485 | if ( menuForItem ) |
486 | { | |
487 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; | |
488 | } | |
c626a8b7 | 489 | |
3dfac970 | 490 | return result; |
bbe0af5b RR |
491 | } |
492 | ||
3dfac970 | 493 | void wxMenuBar::EnableTop( size_t pos, bool flag ) |
bbe0af5b | 494 | { |
222ed1d6 | 495 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 496 | |
223d09f6 | 497 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 498 | |
3dfac970 | 499 | wxMenu* menu = node->GetData(); |
c626a8b7 VZ |
500 | |
501 | if (menu->m_owner) | |
502 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
503 | } |
504 | ||
3dfac970 | 505 | wxString wxMenuBar::GetLabelTop( size_t pos ) const |
bbe0af5b | 506 | { |
222ed1d6 | 507 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 508 | |
223d09f6 | 509 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
c626a8b7 | 510 | |
3dfac970 | 511 | wxMenu* menu = node->GetData(); |
c626a8b7 | 512 | |
f6bcfd97 BP |
513 | wxString label; |
514 | wxString text( menu->GetTitle() ); | |
f6bcfd97 BP |
515 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
516 | { | |
2b5f62a0 | 517 | if ( *pc == wxT('_') ) |
f6bcfd97 | 518 | { |
2b5f62a0 | 519 | // '_' is the escape character for GTK+ |
f6bcfd97 BP |
520 | continue; |
521 | } | |
522 | ||
7cf4f7e2 GD |
523 | // don't remove ampersands '&' since if we have them in the menu title |
524 | // it means that they were doubled to indicate "&" instead of accelerator | |
525 | ||
f6bcfd97 BP |
526 | label += *pc; |
527 | } | |
f6bcfd97 BP |
528 | |
529 | return label; | |
bbe0af5b RR |
530 | } |
531 | ||
3dfac970 | 532 | void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) |
bbe0af5b | 533 | { |
222ed1d6 | 534 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 535 | |
223d09f6 | 536 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 537 | |
3dfac970 | 538 | wxMenu* menu = node->GetData(); |
c626a8b7 | 539 | |
4e115ed2 | 540 | const wxString str( wxReplaceUnderscore( label ) ); |
f6bcfd97 BP |
541 | |
542 | menu->SetTitle( str ); | |
543 | ||
544 | if (menu->m_owner) | |
8394218c | 545 | gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) ); |
bbe0af5b RR |
546 | } |
547 | ||
c801d85f | 548 | //----------------------------------------------------------------------------- |
cf7a7e13 | 549 | // "activate" |
c801d85f KB |
550 | //----------------------------------------------------------------------------- |
551 | ||
865bb325 | 552 | extern "C" { |
6de97a3b | 553 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 554 | { |
83624f79 | 555 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 556 | |
83624f79 | 557 | /* should find it for normal (not popup) menu */ |
1e6feb95 VZ |
558 | wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL), |
559 | _T("menu item not found in gtk_menu_clicked_callback") ); | |
96fd301f | 560 | |
c626a8b7 VZ |
561 | if (!menu->IsEnabled(id)) |
562 | return; | |
96fd301f | 563 | |
4e6beae5 RD |
564 | wxMenuItem* item = menu->FindChildItem( id ); |
565 | wxCHECK_RET( item, wxT("error in menu item callback") ); | |
566 | ||
9959e2b6 VZ |
567 | if ( item->GetId() == wxGTK_TITLE_ID ) |
568 | { | |
569 | // ignore events from the menu title | |
570 | return; | |
571 | } | |
572 | ||
4e6beae5 RD |
573 | if (item->IsCheckable()) |
574 | { | |
575 | bool isReallyChecked = item->IsChecked(), | |
576 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); | |
577 | ||
578 | // ensure that the internal state is always consistent with what is | |
579 | // shown on the screen | |
580 | item->wxMenuItemBase::Check(isReallyChecked); | |
581 | ||
582 | // we must not report the events for the radio button going up nor the | |
583 | // events resulting from the calls to wxMenuItem::Check() | |
584 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || | |
585 | (isInternallyChecked == isReallyChecked) ) | |
586 | { | |
587 | return; | |
588 | } | |
589 | } | |
590 | ||
591 | ||
02822c8c RD |
592 | // Is this menu on a menubar? (possibly nested) |
593 | wxFrame* frame = NULL; | |
6edf1107 DE |
594 | if(menu->IsAttached()) |
595 | frame = menu->GetMenuBar()->GetFrame(); | |
02822c8c | 596 | |
da0b5338 VZ |
597 | // FIXME: why do we have to call wxFrame::GetEventHandler() directly here? |
598 | // normally wxMenu::SendEvent() should be enough, if it doesn't work | |
599 | // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which | |
600 | // should be fixed instead of working around it here... | |
02822c8c | 601 | if (frame) |
2d17d68f | 602 | { |
4e6beae5 RD |
603 | // If it is attached then let the frame send the event. |
604 | // Don't call frame->ProcessCommand(id) because it toggles | |
605 | // checkable items and we've already done that above. | |
606 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); | |
607 | commandEvent.SetEventObject(frame); | |
608 | if (item->IsCheckable()) | |
609 | commandEvent.SetInt(item->IsChecked()); | |
da0b5338 | 610 | commandEvent.SetEventObject(menu); |
4e6beae5 RD |
611 | |
612 | frame->GetEventHandler()->ProcessEvent(commandEvent); | |
a01fe3d6 RD |
613 | } |
614 | else | |
615 | { | |
4e6beae5 | 616 | // otherwise let the menu have it |
a01fe3d6 | 617 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); |
2d17d68f | 618 | } |
cf7a7e13 | 619 | } |
865bb325 | 620 | } |
cf7a7e13 RR |
621 | |
622 | //----------------------------------------------------------------------------- | |
623 | // "select" | |
624 | //----------------------------------------------------------------------------- | |
625 | ||
865bb325 | 626 | extern "C" { |
cf7a7e13 RR |
627 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) |
628 | { | |
83624f79 RR |
629 | int id = menu->FindMenuIdByMenuItem(widget); |
630 | ||
631 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 632 | |
c626a8b7 VZ |
633 | if (!menu->IsEnabled(id)) |
634 | return; | |
cf7a7e13 | 635 | |
342b6a2f | 636 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 637 | event.SetEventObject( menu ); |
cf7a7e13 | 638 | |
a01fe3d6 RD |
639 | wxEvtHandler* handler = menu->GetEventHandler(); |
640 | if (handler && handler->ProcessEvent(event)) | |
c626a8b7 | 641 | return; |
6de97a3b | 642 | |
83624f79 RR |
643 | wxWindow *win = menu->GetInvokingWindow(); |
644 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 645 | } |
865bb325 | 646 | } |
c801d85f | 647 | |
cd743a6f RR |
648 | //----------------------------------------------------------------------------- |
649 | // "deselect" | |
650 | //----------------------------------------------------------------------------- | |
651 | ||
865bb325 | 652 | extern "C" { |
cd743a6f RR |
653 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) |
654 | { | |
655 | int id = menu->FindMenuIdByMenuItem(widget); | |
656 | ||
657 | wxASSERT( id != -1 ); // should find it! | |
658 | ||
c626a8b7 VZ |
659 | if (!menu->IsEnabled(id)) |
660 | return; | |
cd743a6f RR |
661 | |
662 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
663 | event.SetEventObject( menu ); | |
664 | ||
a01fe3d6 RD |
665 | wxEvtHandler* handler = menu->GetEventHandler(); |
666 | if (handler && handler->ProcessEvent(event)) | |
c626a8b7 | 667 | return; |
cd743a6f RR |
668 | |
669 | wxWindow *win = menu->GetInvokingWindow(); | |
c626a8b7 VZ |
670 | if (win) |
671 | win->GetEventHandler()->ProcessEvent( event ); | |
cd743a6f | 672 | } |
865bb325 | 673 | } |
cd743a6f | 674 | |
cf7a7e13 | 675 | //----------------------------------------------------------------------------- |
db1b4961 | 676 | // wxMenuItem |
cf7a7e13 RR |
677 | //----------------------------------------------------------------------------- |
678 | ||
7dbf5360 | 679 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) |
974e8d94 VZ |
680 | |
681 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
682 | int id, | |
683 | const wxString& name, | |
684 | const wxString& help, | |
d65c269b | 685 | wxItemKind kind, |
974e8d94 VZ |
686 | wxMenu *subMenu) |
687 | { | |
d65c269b | 688 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
974e8d94 | 689 | } |
96fd301f | 690 | |
974e8d94 VZ |
691 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
692 | int id, | |
693 | const wxString& text, | |
694 | const wxString& help, | |
d65c269b | 695 | wxItemKind kind, |
974e8d94 | 696 | wxMenu *subMenu) |
d65c269b | 697 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) |
2368dcda | 698 | { |
092f7536 | 699 | Init(text); |
2368dcda VZ |
700 | } |
701 | ||
702 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
703 | int id, | |
704 | const wxString& text, | |
705 | const wxString& help, | |
706 | bool isCheckable, | |
707 | wxMenu *subMenu) | |
708 | : wxMenuItemBase(parentMenu, id, text, help, | |
709 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
710 | { | |
092f7536 | 711 | Init(text); |
2368dcda VZ |
712 | } |
713 | ||
092f7536 | 714 | void wxMenuItem::Init(const wxString& text) |
c801d85f | 715 | { |
37d403aa | 716 | m_labelWidget = (GtkWidget *) NULL; |
83624f79 | 717 | m_menuItem = (GtkWidget *) NULL; |
974e8d94 | 718 | |
092f7536 | 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; | |
2368dcda | 732 | |
3b59cdbf | 733 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
717a57c2 | 734 | { |
f0b72e0d RR |
735 | if ( *pc == wxT('\t')) |
736 | break; | |
9959e2b6 | 737 | |
7cf4f7e2 | 738 | if ( *pc == wxT('_') ) |
717a57c2 | 739 | { |
2b5f62a0 | 740 | // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx" |
d76419bd RR |
741 | pc++; |
742 | label += *pc; | |
743 | continue; | |
744 | } | |
2368dcda | 745 | |
2b5f62a0 | 746 | if ( *pc == wxT('\\') ) |
d76419bd | 747 | { |
2b5f62a0 VZ |
748 | // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx" |
749 | pc++; | |
750 | label += *pc; | |
717a57c2 VZ |
751 | continue; |
752 | } | |
753 | ||
7cf4f7e2 GD |
754 | if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) ) |
755 | { | |
756 | // wxMSW escapes "&" | |
757 | // "&" is doubled to indicate "&" instead of accelerator | |
758 | continue; | |
759 | } | |
a01fe3d6 | 760 | |
717a57c2 VZ |
761 | label += *pc; |
762 | } | |
a01fe3d6 | 763 | |
6d971354 | 764 | // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() ); |
d9e403cc | 765 | |
717a57c2 VZ |
766 | return label; |
767 | } | |
768 | ||
769 | void wxMenuItem::SetText( const wxString& str ) | |
770 | { | |
ee0a94cf RR |
771 | // cache some data which must be used later |
772 | bool isstock = wxIsStockID(GetId()); | |
773 | const char *stockid = NULL; | |
774 | if (isstock) | |
775 | stockid = wxGetStockGtkID(GetId()); | |
776 | ||
5869f93f JS |
777 | // Some optimization to avoid flicker |
778 | wxString oldLabel = m_text; | |
98f29783 | 779 | oldLabel = wxStripMenuCodes(oldLabel); |
5869f93f | 780 | oldLabel.Replace(wxT("_"), wxT("")); |
98f29783 | 781 | wxString label1 = wxStripMenuCodes(str); |
a8e607d9 | 782 | wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format |
5f11fef5 | 783 | wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo |
a01fe3d6 | 784 | |
717a57c2 | 785 | DoSetText(str); |
354aa1e3 | 786 | |
88d19775 | 787 | if (oldLabel == label1 && |
ee0a94cf | 788 | oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered |
98f29783 RR |
789 | return; |
790 | ||
354aa1e3 RR |
791 | if (m_menuItem) |
792 | { | |
37d403aa JS |
793 | GtkLabel *label; |
794 | if (m_labelWidget) | |
2b5f62a0 | 795 | label = (GtkLabel*) m_labelWidget; |
37d403aa | 796 | else |
2b5f62a0 | 797 | label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); |
717a57c2 | 798 | |
ee0a94cf RR |
799 | // stock menu items can have empty labels: |
800 | wxString text = m_text; | |
801 | if (text.IsEmpty() && !IsSeparator()) | |
802 | { | |
803 | wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?")); | |
804 | text = wxGetStockLabel(GetId()); | |
805 | ||
806 | // need & => _ conversion | |
807 | text = GTKProcessMenuItemLabel(text, NULL); | |
808 | } | |
809 | ||
810 | gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV_SYS(text) ); | |
354aa1e3 | 811 | } |
98f29783 | 812 | |
ee0a94cf | 813 | // remove old accelerator from our parent's accelerator group, if present |
98f29783 RR |
814 | guint accel_key; |
815 | GdkModifierType accel_mods; | |
ee0a94cf | 816 | if (oldbuf[(size_t)0] != '\0') |
98f29783 | 817 | { |
ee0a94cf RR |
818 | gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); |
819 | if (accel_key != 0) | |
820 | { | |
10bd1f7d | 821 | gtk_widget_remove_accelerator(m_menuItem, |
ee0a94cf RR |
822 | m_parentMenu->m_accel, |
823 | accel_key, | |
824 | accel_mods ); | |
825 | } | |
826 | } | |
827 | else if (isstock) | |
828 | { | |
829 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock | |
830 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
10bd1f7d | 831 | gtk_widget_remove_accelerator( m_menuItem, |
ee0a94cf RR |
832 | m_parentMenu->m_accel, |
833 | accel_key, | |
834 | accel_mods ); | |
98f29783 RR |
835 | } |
836 | ||
ee0a94cf | 837 | // add new accelerator to our parent's accelerator group |
5f11fef5 | 838 | wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); |
ee0a94cf | 839 | if (buf[(size_t)0] != '\0') |
98f29783 | 840 | { |
ee0a94cf RR |
841 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); |
842 | if (accel_key != 0) | |
843 | { | |
10bd1f7d | 844 | gtk_widget_add_accelerator( m_menuItem, |
ee0a94cf RR |
845 | "activate", |
846 | m_parentMenu->m_accel, | |
847 | accel_key, | |
848 | accel_mods, | |
849 | GTK_ACCEL_VISIBLE); | |
850 | } | |
851 | } | |
852 | else if (isstock) | |
853 | { | |
854 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock | |
855 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
10bd1f7d | 856 | gtk_widget_remove_accelerator( m_menuItem, |
ee0a94cf RR |
857 | m_parentMenu->m_accel, |
858 | accel_key, | |
859 | accel_mods ); | |
98f29783 | 860 | } |
354aa1e3 RR |
861 | } |
862 | ||
ee0a94cf RR |
863 | // NOTE: this function is different from the similar functions GTKProcessMnemonics() |
864 | // implemented in control.cpp and from wxMenuItemBase::GetLabelFromText... | |
865 | // so there's no real code duplication | |
866 | wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey) | |
716b7364 | 867 | { |
ee0a94cf RR |
868 | wxString text; |
869 | ||
2b5f62a0 | 870 | // '\t' is the deliminator indicating a hot key |
e0a050e3 VS |
871 | wxString::const_iterator pc = str.begin(); |
872 | while ( pc != str.end() && *pc != wxT('\t') ) | |
83624f79 | 873 | { |
e0a050e3 | 874 | if (*pc == wxT('&')) |
23280650 | 875 | { |
e0a050e3 VS |
876 | wxString::const_iterator next = pc + 1; |
877 | if (next != str.end() && *next == wxT('&')) | |
878 | { | |
879 | // "&" is doubled to indicate "&" instead of accelerator | |
880 | ++pc; | |
881 | text << wxT('&'); | |
882 | } | |
883 | else | |
884 | { | |
885 | text << wxT('_'); | |
886 | } | |
572d7461 | 887 | } |
2b5f62a0 VZ |
888 | else if ( *pc == wxT('_') ) // escape underscores |
889 | { | |
ee0a94cf | 890 | text << wxT("__"); |
2b5f62a0 | 891 | } |
9959e2b6 | 892 | else |
23280650 | 893 | { |
ee0a94cf | 894 | text << *pc; |
2b5f62a0 VZ |
895 | } |
896 | ++pc; | |
83624f79 | 897 | } |
a01fe3d6 | 898 | |
ee0a94cf | 899 | if (hotKey) |
d7dbc98a | 900 | { |
ee0a94cf RR |
901 | hotKey->Empty(); |
902 | if(*pc == wxT('\t')) | |
903 | { | |
904 | pc++; | |
e0a050e3 | 905 | hotKey->assign(pc, str.end()); |
ee0a94cf | 906 | } |
d7dbc98a | 907 | } |
88d19775 | 908 | |
ee0a94cf RR |
909 | return text; |
910 | } | |
911 | ||
912 | // it's valid for this function to be called even if m_menuItem == NULL | |
913 | void wxMenuItem::DoSetText( const wxString& str ) | |
914 | { | |
915 | m_text.Empty(); | |
916 | m_text = GTKProcessMenuItemLabel(str, &m_hotKey); | |
716b7364 RR |
917 | } |
918 | ||
717a57c2 VZ |
919 | #if wxUSE_ACCEL |
920 | ||
921 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
922 | { | |
1987af7e | 923 | if ( !GetHotKey() ) |
717a57c2 VZ |
924 | { |
925 | // nothing | |
90527a50 | 926 | return NULL; |
717a57c2 VZ |
927 | } |
928 | ||
90527a50 VZ |
929 | // accelerator parsing code looks for them after a TAB, so insert a dummy |
930 | // one here | |
717a57c2 | 931 | wxString label; |
1987af7e | 932 | label << wxT('\t') << GetHotKey(); |
717a57c2 | 933 | |
90527a50 | 934 | return wxAcceleratorEntry::Create(label); |
717a57c2 VZ |
935 | } |
936 | ||
937 | #endif // wxUSE_ACCEL | |
938 | ||
96fd301f | 939 | void wxMenuItem::Check( bool check ) |
716b7364 | 940 | { |
223d09f6 | 941 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 942 | |
974e8d94 VZ |
943 | if (check == m_isChecked) |
944 | return; | |
2d17d68f | 945 | |
974e8d94 | 946 | wxMenuItemBase::Check( check ); |
0472ece7 | 947 | |
24bcaec3 | 948 | switch ( GetKind() ) |
0472ece7 | 949 | { |
24bcaec3 VZ |
950 | case wxITEM_CHECK: |
951 | case wxITEM_RADIO: | |
8394218c | 952 | gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check ); |
24bcaec3 VZ |
953 | break; |
954 | ||
955 | default: | |
956 | wxFAIL_MSG( _T("can't check this item") ); | |
0472ece7 | 957 | } |
716b7364 RR |
958 | } |
959 | ||
8bbe427f VZ |
960 | void wxMenuItem::Enable( bool enable ) |
961 | { | |
223d09f6 | 962 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 963 | |
83624f79 | 964 | gtk_widget_set_sensitive( m_menuItem, enable ); |
974e8d94 | 965 | wxMenuItemBase::Enable( enable ); |
a9c96bcc RR |
966 | } |
967 | ||
96fd301f | 968 | bool wxMenuItem::IsChecked() const |
716b7364 | 969 | { |
670f9935 | 970 | wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") ); |
db1b4961 | 971 | |
670f9935 | 972 | wxCHECK_MSG( IsCheckable(), false, |
974e8d94 | 973 | wxT("can't get state of uncheckable item!") ); |
96fd301f | 974 | |
974e8d94 | 975 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
716b7364 RR |
976 | } |
977 | ||
db1b4961 | 978 | //----------------------------------------------------------------------------- |
83624f79 | 979 | // wxMenu |
db1b4961 RR |
980 | //----------------------------------------------------------------------------- |
981 | ||
c801d85f KB |
982 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
983 | ||
717a57c2 | 984 | void wxMenu::Init() |
c801d85f | 985 | { |
034be888 | 986 | m_accel = gtk_accel_group_new(); |
6d971354 | 987 | m_menu = gtk_menu_new(); |
defc0789 VS |
988 | // NB: keep reference to the menu so that it is not destroyed behind |
989 | // our back by GTK+ e.g. when it is removed from menubar: | |
990 | gtk_widget_ref(m_menu); | |
8bbe427f | 991 | |
2b1c162e | 992 | m_owner = (GtkWidget*) NULL; |
2b2edbed | 993 | |
d9e403cc RR |
994 | // Tearoffs are entries, just like separators. So if we want this |
995 | // menu to be a tear-off one, we just append a tearoff entry | |
996 | // immediately. | |
9959e2b6 | 997 | if ( m_style & wxMENU_TEAROFF ) |
2b2edbed | 998 | { |
9959e2b6 | 999 | GtkWidget *tearoff = gtk_tearoff_menu_item_new(); |
6d971354 | 1000 | |
1ab9e06d | 1001 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff); |
9959e2b6 | 1002 | } |
6d971354 | 1003 | |
9959e2b6 | 1004 | m_prevRadio = NULL; |
c801d85f | 1005 | |
717a57c2 | 1006 | // append the title as the very first entry if we have it |
9959e2b6 | 1007 | if ( !m_title.empty() ) |
d1b15f03 | 1008 | { |
9959e2b6 | 1009 | Append(wxGTK_TITLE_ID, m_title); |
717a57c2 | 1010 | AppendSeparator(); |
d1b15f03 | 1011 | } |
717a57c2 | 1012 | } |
15a2076a | 1013 | |
717a57c2 VZ |
1014 | wxMenu::~wxMenu() |
1015 | { | |
222ed1d6 | 1016 | WX_CLEAR_LIST(wxMenuItemList, m_items); |
f03ec224 | 1017 | |
368a4a47 | 1018 | if ( GTK_IS_WIDGET( m_menu )) |
defc0789 | 1019 | { |
a761df69 | 1020 | // see wxMenu::Init |
88d19775 | 1021 | gtk_widget_unref( m_menu ); |
b8a922d0 RR |
1022 | g_object_unref( m_accel ); |
1023 | ||
a761df69 VS |
1024 | // if the menu is inserted in another menu at this time, there was |
1025 | // one more reference to it: | |
1026 | if ( m_owner ) | |
1027 | gtk_widget_destroy( m_menu ); | |
defc0789 | 1028 | } |
c2dd8380 GL |
1029 | } |
1030 | ||
978af864 VZ |
1031 | void wxMenu::SetLayoutDirection(const wxLayoutDirection dir) |
1032 | { | |
1033 | if ( m_owner ) | |
1034 | wxWindow::GTKSetLayout(m_owner, dir); | |
1035 | //else: will be called later by wxMenuBar again | |
1036 | } | |
1037 | ||
1038 | wxLayoutDirection wxMenu::GetLayoutDirection() const | |
1039 | { | |
1040 | return wxWindow::GTKGetLayout(m_owner); | |
1041 | } | |
1042 | ||
49826dab | 1043 | bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) |
c2dd8380 | 1044 | { |
717a57c2 | 1045 | GtkWidget *menuItem; |
96fd301f | 1046 | |
ee0a94cf RR |
1047 | // cache some data used later |
1048 | wxString text = mitem->GetText(); | |
1049 | int id = mitem->GetId(); | |
1050 | bool isstock = wxIsStockID(id); | |
1051 | const char *stockid = NULL; | |
1052 | if (isstock) | |
1053 | stockid = wxGetStockGtkID(mitem->GetId()); | |
1054 | ||
1055 | // stock menu items can have an empty label | |
1056 | if (text.IsEmpty() && !mitem->IsSeparator()) | |
1057 | { | |
1058 | wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?")); | |
1059 | text = wxGetStockLabel(id); | |
1060 | ||
1061 | // need & => _ conversion | |
1062 | text = wxMenuItem::GTKProcessMenuItemLabel(text, NULL); | |
1063 | } | |
e31126cb | 1064 | |
717a57c2 VZ |
1065 | if ( mitem->IsSeparator() ) |
1066 | { | |
6d971354 | 1067 | menuItem = gtk_separator_menu_item_new(); |
837904f2 | 1068 | } |
ab73fe8d | 1069 | else if ( mitem->GetBitmap().Ok() || |
ee0a94cf | 1070 | (mitem->GetKind() == wxITEM_NORMAL && isstock) ) |
37d403aa | 1071 | { |
ab73fe8d | 1072 | wxBitmap bitmap(mitem->GetBitmap()); |
9959e2b6 | 1073 | |
5f11fef5 | 1074 | menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); |
9959e2b6 | 1075 | |
b1ad1424 | 1076 | GtkWidget *image; |
ab73fe8d | 1077 | if ( !bitmap.Ok() ) |
b1ad1424 | 1078 | { |
ab73fe8d VZ |
1079 | // use stock bitmap for this item if available on the assumption |
1080 | // that it never hurts to follow GTK+ conventions more closely | |
ee0a94cf RR |
1081 | image = stockid ? gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU) |
1082 | : NULL; | |
b1ad1424 | 1083 | } |
ab73fe8d | 1084 | else // we have a custom bitmap |
b1ad1424 | 1085 | { |
ab73fe8d VZ |
1086 | wxASSERT_MSG( mitem->GetKind() == wxITEM_NORMAL, |
1087 | _T("only normal menu items can have bitmaps") ); | |
1088 | ||
1089 | if ( bitmap.HasPixbuf() ) | |
1090 | { | |
1091 | image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf()); | |
1092 | } | |
1093 | else | |
1094 | { | |
1095 | GdkPixmap *gdk_pixmap = bitmap.GetPixmap(); | |
1096 | GdkBitmap *gdk_bitmap = bitmap.GetMask() ? | |
1097 | bitmap.GetMask()->GetBitmap() : | |
1098 | (GdkBitmap*) NULL; | |
1099 | image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap ); | |
1100 | } | |
b1ad1424 | 1101 | } |
88d19775 | 1102 | |
ab73fe8d VZ |
1103 | if ( image ) |
1104 | { | |
1105 | gtk_widget_show(image); | |
9959e2b6 | 1106 | |
ab73fe8d VZ |
1107 | gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image ); |
1108 | } | |
9959e2b6 | 1109 | |
6d971354 RR |
1110 | m_prevRadio = NULL; |
1111 | } | |
717a57c2 VZ |
1112 | else // a normal item |
1113 | { | |
ee0a94cf RR |
1114 | // NB: 'text' variable has "_" instead of "&" after mitem->SetText() |
1115 | // so don't use it | |
717a57c2 | 1116 | |
d65c269b VZ |
1117 | switch ( mitem->GetKind() ) |
1118 | { | |
546bfbea | 1119 | case wxITEM_CHECK: |
6d971354 | 1120 | { |
5f11fef5 | 1121 | menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); |
6d971354 | 1122 | m_prevRadio = NULL; |
d65c269b | 1123 | break; |
6d971354 | 1124 | } |
d65c269b | 1125 | |
546bfbea | 1126 | case wxITEM_RADIO: |
6d971354 RR |
1127 | { |
1128 | GSList *group = NULL; | |
1129 | if ( m_prevRadio == NULL ) | |
d65c269b VZ |
1130 | { |
1131 | // start of a new radio group | |
5f11fef5 VZ |
1132 | m_prevRadio = menuItem = |
1133 | gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) ); | |
d65c269b VZ |
1134 | } |
1135 | else // continue the radio group | |
1136 | { | |
6d971354 | 1137 | group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio)); |
5f11fef5 VZ |
1138 | m_prevRadio = menuItem = |
1139 | gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) ); | |
d65c269b | 1140 | } |
c0d0a552 | 1141 | break; |
6d971354 | 1142 | } |
d65c269b VZ |
1143 | |
1144 | default: | |
1145 | wxFAIL_MSG( _T("unexpected menu item kind") ); | |
1146 | // fall through | |
1147 | ||
546bfbea | 1148 | case wxITEM_NORMAL: |
6d971354 | 1149 | { |
5f11fef5 | 1150 | menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); |
6d971354 | 1151 | m_prevRadio = NULL; |
d65c269b | 1152 | break; |
6d971354 | 1153 | } |
d65c269b VZ |
1154 | } |
1155 | ||
6d971354 | 1156 | } |
9959e2b6 | 1157 | |
6d971354 RR |
1158 | guint accel_key; |
1159 | GdkModifierType accel_mods; | |
5f11fef5 | 1160 | wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*mitem) ); |
9959e2b6 | 1161 | |
98f29783 | 1162 | // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() ); |
ee0a94cf RR |
1163 | if (buf[(size_t)0] != '\0') |
1164 | { | |
1165 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); | |
1166 | if (accel_key != 0) | |
1167 | { | |
10bd1f7d | 1168 | gtk_widget_add_accelerator (menuItem, |
ee0a94cf RR |
1169 | "activate", |
1170 | m_accel, | |
1171 | accel_key, | |
1172 | accel_mods, | |
1173 | GTK_ACCEL_VISIBLE); | |
1174 | } | |
1175 | } | |
1176 | else if (isstock) | |
6d971354 | 1177 | { |
ee0a94cf RR |
1178 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock |
1179 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
10bd1f7d | 1180 | gtk_widget_add_accelerator( menuItem, |
ee0a94cf RR |
1181 | "activate", |
1182 | m_accel, | |
1183 | accel_key, | |
1184 | accel_mods, | |
1185 | GTK_ACCEL_VISIBLE); | |
717a57c2 | 1186 | } |
9959e2b6 | 1187 | |
e31126cb RR |
1188 | if (pos == -1) |
1189 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem); | |
1190 | else | |
1191 | gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos); | |
1192 | ||
6d971354 | 1193 | gtk_widget_show( menuItem ); |
23280650 | 1194 | |
717a57c2 VZ |
1195 | if ( !mitem->IsSeparator() ) |
1196 | { | |
2b5f62a0 | 1197 | wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); |
a01fe3d6 | 1198 | |
9fa72bd2 MR |
1199 | g_signal_connect (menuItem, "select", |
1200 | G_CALLBACK (gtk_menu_hilight_callback), this); | |
1201 | g_signal_connect (menuItem, "deselect", | |
1202 | G_CALLBACK (gtk_menu_nolight_callback), this); | |
e31126cb | 1203 | |
88d19775 MR |
1204 | if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) |
1205 | { | |
e31126cb RR |
1206 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); |
1207 | ||
1208 | gtk_widget_show( mitem->GetSubMenu()->m_menu ); | |
1209 | ||
1210 | // if adding a submenu to a menu already existing in the menu bar, we | |
1211 | // must set invoking window to allow processing events from this | |
1212 | // submenu | |
1213 | if ( m_invokingWindow ) | |
1214 | wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); | |
88d19775 MR |
1215 | } |
1216 | else | |
1217 | { | |
9fa72bd2 MR |
1218 | g_signal_connect (menuItem, "activate", |
1219 | G_CALLBACK (gtk_menu_clicked_callback), | |
1220 | this); | |
88d19775 | 1221 | } |
717a57c2 | 1222 | } |
23280650 | 1223 | |
837904f2 | 1224 | mitem->SetMenuItem(menuItem); |
837904f2 | 1225 | |
6d971354 | 1226 | if (ms_locked) |
d65c269b | 1227 | { |
6d971354 RR |
1228 | // This doesn't even exist! |
1229 | // gtk_widget_lock_accelerators(mitem->GetMenuItem()); | |
d65c269b | 1230 | } |
d65c269b | 1231 | |
670f9935 | 1232 | return true; |
6de97a3b | 1233 | } |
c801d85f | 1234 | |
9add9367 | 1235 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) |
828f655f | 1236 | { |
9add9367 RD |
1237 | if (!GtkAppend(mitem)) |
1238 | return NULL; | |
9959e2b6 | 1239 | |
9add9367 | 1240 | return wxMenuBase::DoAppend(mitem); |
c33c4050 RR |
1241 | } |
1242 | ||
9add9367 | 1243 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
c33c4050 | 1244 | { |
717a57c2 | 1245 | if ( !wxMenuBase::DoInsert(pos, item) ) |
9add9367 | 1246 | return NULL; |
c626a8b7 | 1247 | |
6d971354 | 1248 | // TODO |
49826dab | 1249 | if ( !GtkAppend(item, (int)pos) ) |
9add9367 | 1250 | return NULL; |
32db328c | 1251 | |
9add9367 | 1252 | return item; |
c33c4050 RR |
1253 | } |
1254 | ||
717a57c2 | 1255 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
c33c4050 | 1256 | { |
717a57c2 VZ |
1257 | if ( !wxMenuBase::DoRemove(item) ) |
1258 | return (wxMenuItem *)NULL; | |
c626a8b7 | 1259 | |
717a57c2 VZ |
1260 | // TODO: this code doesn't delete the item factory item and this seems |
1261 | // impossible as of GTK 1.2.6. | |
1262 | gtk_widget_destroy( item->GetMenuItem() ); | |
c626a8b7 | 1263 | |
717a57c2 | 1264 | return item; |
c33c4050 RR |
1265 | } |
1266 | ||
96fd301f VZ |
1267 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
1268 | { | |
222ed1d6 | 1269 | wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
83624f79 RR |
1270 | while (node) |
1271 | { | |
b1d4dd7a | 1272 | wxMenuItem *item = node->GetData(); |
83624f79 | 1273 | if (item->GetMenuItem() == menuItem) |
88d19775 | 1274 | return item->GetId(); |
b1d4dd7a | 1275 | node = node->GetNext(); |
83624f79 | 1276 | } |
96fd301f | 1277 | |
c626a8b7 | 1278 | return wxNOT_FOUND; |
6de97a3b | 1279 | } |
c801d85f | 1280 | |
978af864 VZ |
1281 | void wxMenu::Attach(wxMenuBarBase *menubar) |
1282 | { | |
1283 | wxMenuBase::Attach(menubar); | |
1284 | ||
1285 | // inherit layout direction from menubar. | |
1286 | SetLayoutDirection(menubar->GetLayoutDirection()); | |
1287 | } | |
1288 | ||
717a57c2 VZ |
1289 | // ---------------------------------------------------------------------------- |
1290 | // helpers | |
1291 | // ---------------------------------------------------------------------------- | |
1292 | ||
6d971354 | 1293 | #if wxUSE_ACCEL |
a070d8ce | 1294 | |
98f29783 | 1295 | static wxString GetGtkHotKey( const wxMenuItem& item ) |
c801d85f | 1296 | { |
717a57c2 VZ |
1297 | wxString hotkey; |
1298 | ||
1299 | wxAcceleratorEntry *accel = item.GetAccel(); | |
1300 | if ( accel ) | |
83624f79 | 1301 | { |
717a57c2 VZ |
1302 | int flags = accel->GetFlags(); |
1303 | if ( flags & wxACCEL_ALT ) | |
1304 | hotkey += wxT("<alt>"); | |
1305 | if ( flags & wxACCEL_CTRL ) | |
1306 | hotkey += wxT("<control>"); | |
1307 | if ( flags & wxACCEL_SHIFT ) | |
1308 | hotkey += wxT("<shift>"); | |
1309 | ||
1310 | int code = accel->GetKeyCode(); | |
1311 | switch ( code ) | |
c626a8b7 | 1312 | { |
717a57c2 VZ |
1313 | case WXK_F1: |
1314 | case WXK_F2: | |
1315 | case WXK_F3: | |
1316 | case WXK_F4: | |
1317 | case WXK_F5: | |
1318 | case WXK_F6: | |
1319 | case WXK_F7: | |
1320 | case WXK_F8: | |
1321 | case WXK_F9: | |
1322 | case WXK_F10: | |
1323 | case WXK_F11: | |
1324 | case WXK_F12: | |
bbcd4085 RR |
1325 | case WXK_F13: |
1326 | case WXK_F14: | |
1327 | case WXK_F15: | |
1328 | case WXK_F16: | |
1329 | case WXK_F17: | |
1330 | case WXK_F18: | |
1331 | case WXK_F19: | |
1332 | case WXK_F20: | |
1333 | case WXK_F21: | |
1334 | case WXK_F22: | |
1335 | case WXK_F23: | |
1336 | case WXK_F24: | |
04cc1e93 | 1337 | hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1); |
717a57c2 | 1338 | break; |
2368dcda | 1339 | |
a070d8ce VZ |
1340 | // TODO: we should use gdk_keyval_name() (a.k.a. |
1341 | // XKeysymToString) here as well as hardcoding the keysym | |
1342 | // names this might be not portable | |
bbcd4085 | 1343 | case WXK_INSERT: |
3ca6a5f0 BP |
1344 | hotkey << wxT("Insert" ); |
1345 | break; | |
1346 | case WXK_DELETE: | |
1347 | hotkey << wxT("Delete" ); | |
1348 | break; | |
2b5f62a0 VZ |
1349 | case WXK_UP: |
1350 | hotkey << wxT("Up" ); | |
1351 | break; | |
1352 | case WXK_DOWN: | |
1353 | hotkey << wxT("Down" ); | |
1354 | break; | |
1355 | case WXK_PAGEUP: | |
f005ea42 | 1356 | hotkey << wxT("Page_Up" ); |
2b5f62a0 VZ |
1357 | break; |
1358 | case WXK_PAGEDOWN: | |
f005ea42 | 1359 | hotkey << wxT("Page_Down" ); |
2b5f62a0 VZ |
1360 | break; |
1361 | case WXK_LEFT: | |
1362 | hotkey << wxT("Left" ); | |
1363 | break; | |
1364 | case WXK_RIGHT: | |
1365 | hotkey << wxT("Right" ); | |
1366 | break; | |
1367 | case WXK_HOME: | |
1368 | hotkey << wxT("Home" ); | |
1369 | break; | |
1370 | case WXK_END: | |
1371 | hotkey << wxT("End" ); | |
1372 | break; | |
1373 | case WXK_RETURN: | |
1374 | hotkey << wxT("Return" ); | |
1375 | break; | |
bbcd4085 RR |
1376 | case WXK_BACK: |
1377 | hotkey << wxT("BackSpace" ); | |
1378 | break; | |
1379 | case WXK_TAB: | |
1380 | hotkey << wxT("Tab" ); | |
1381 | break; | |
1382 | case WXK_ESCAPE: | |
1383 | hotkey << wxT("Esc" ); | |
1384 | break; | |
1385 | case WXK_SPACE: | |
1386 | hotkey << wxT("space" ); | |
1387 | break; | |
1388 | case WXK_MULTIPLY: | |
1389 | hotkey << wxT("Multiply" ); | |
1390 | break; | |
1391 | case WXK_ADD: | |
1392 | hotkey << wxT("Add" ); | |
1393 | break; | |
1394 | case WXK_SEPARATOR: | |
1395 | hotkey << wxT("Separator" ); | |
1396 | break; | |
1397 | case WXK_SUBTRACT: | |
1398 | hotkey << wxT("Subtract" ); | |
1399 | break; | |
1400 | case WXK_DECIMAL: | |
1401 | hotkey << wxT("Decimal" ); | |
1402 | break; | |
1403 | case WXK_DIVIDE: | |
1404 | hotkey << wxT("Divide" ); | |
1405 | break; | |
1406 | case WXK_CANCEL: | |
1407 | hotkey << wxT("Cancel" ); | |
1408 | break; | |
1409 | case WXK_CLEAR: | |
1410 | hotkey << wxT("Clear" ); | |
1411 | break; | |
1412 | case WXK_MENU: | |
1413 | hotkey << wxT("Menu" ); | |
1414 | break; | |
1415 | case WXK_PAUSE: | |
1416 | hotkey << wxT("Pause" ); | |
1417 | break; | |
1418 | case WXK_CAPITAL: | |
1419 | hotkey << wxT("Capital" ); | |
1420 | break; | |
1421 | case WXK_SELECT: | |
1422 | hotkey << wxT("Select" ); | |
1423 | break; | |
1424 | case WXK_PRINT: | |
1425 | hotkey << wxT("Print" ); | |
1426 | break; | |
1427 | case WXK_EXECUTE: | |
1428 | hotkey << wxT("Execute" ); | |
1429 | break; | |
1430 | case WXK_SNAPSHOT: | |
1431 | hotkey << wxT("Snapshot" ); | |
1432 | break; | |
1433 | case WXK_HELP: | |
1434 | hotkey << wxT("Help" ); | |
1435 | break; | |
1436 | case WXK_NUMLOCK: | |
1437 | hotkey << wxT("Num_Lock" ); | |
1438 | break; | |
1439 | case WXK_SCROLL: | |
1440 | hotkey << wxT("Scroll_Lock" ); | |
1441 | break; | |
1442 | case WXK_NUMPAD_INSERT: | |
1443 | hotkey << wxT("KP_Insert" ); | |
1444 | break; | |
1445 | case WXK_NUMPAD_DELETE: | |
1446 | hotkey << wxT("KP_Delete" ); | |
1447 | break; | |
1448 | case WXK_NUMPAD_SPACE: | |
1449 | hotkey << wxT("KP_Space" ); | |
1450 | break; | |
1451 | case WXK_NUMPAD_TAB: | |
1452 | hotkey << wxT("KP_Tab" ); | |
1453 | break; | |
1454 | case WXK_NUMPAD_ENTER: | |
1455 | hotkey << wxT("KP_Enter" ); | |
1456 | break; | |
1457 | case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3: | |
1458 | case WXK_NUMPAD_F4: | |
1459 | hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1); | |
1460 | break; | |
1461 | case WXK_NUMPAD_HOME: | |
1462 | hotkey << wxT("KP_Home" ); | |
1463 | break; | |
1464 | case WXK_NUMPAD_LEFT: | |
1465 | hotkey << wxT("KP_Left" ); | |
1466 | break; | |
1467 | case WXK_NUMPAD_UP: | |
1468 | hotkey << wxT("KP_Up" ); | |
1469 | break; | |
1470 | case WXK_NUMPAD_RIGHT: | |
1471 | hotkey << wxT("KP_Right" ); | |
1472 | break; | |
1473 | case WXK_NUMPAD_DOWN: | |
1474 | hotkey << wxT("KP_Down" ); | |
1475 | break; | |
5bd24f72 | 1476 | case WXK_NUMPAD_PAGEUP: |
f005ea42 | 1477 | hotkey << wxT("KP_Page_Up" ); |
bbcd4085 | 1478 | break; |
5bd24f72 | 1479 | case WXK_NUMPAD_PAGEDOWN: |
f005ea42 | 1480 | hotkey << wxT("KP_Page_Down" ); |
bbcd4085 RR |
1481 | break; |
1482 | case WXK_NUMPAD_END: | |
1483 | hotkey << wxT("KP_End" ); | |
1484 | break; | |
1485 | case WXK_NUMPAD_BEGIN: | |
1486 | hotkey << wxT("KP_Begin" ); | |
1487 | break; | |
1488 | case WXK_NUMPAD_EQUAL: | |
1489 | hotkey << wxT("KP_Equal" ); | |
1490 | break; | |
1491 | case WXK_NUMPAD_MULTIPLY: | |
1492 | hotkey << wxT("KP_Multiply" ); | |
1493 | break; | |
1494 | case WXK_NUMPAD_ADD: | |
1495 | hotkey << wxT("KP_Add" ); | |
1496 | break; | |
1497 | case WXK_NUMPAD_SEPARATOR: | |
1498 | hotkey << wxT("KP_Separator" ); | |
1499 | break; | |
1500 | case WXK_NUMPAD_SUBTRACT: | |
1501 | hotkey << wxT("KP_Subtract" ); | |
1502 | break; | |
1503 | case WXK_NUMPAD_DECIMAL: | |
1504 | hotkey << wxT("KP_Decimal" ); | |
1505 | break; | |
1506 | case WXK_NUMPAD_DIVIDE: | |
1507 | hotkey << wxT("KP_Divide" ); | |
1508 | break; | |
1509 | case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2: | |
1510 | case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5: | |
1511 | case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9: | |
1512 | hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0); | |
1513 | break; | |
1514 | case WXK_WINDOWS_LEFT: | |
1515 | hotkey << wxT("Super_L" ); | |
1516 | break; | |
1517 | case WXK_WINDOWS_RIGHT: | |
1518 | hotkey << wxT("Super_R" ); | |
1519 | break; | |
1520 | case WXK_WINDOWS_MENU: | |
1521 | hotkey << wxT("Menu" ); | |
1522 | break; | |
1523 | case WXK_COMMAND: | |
1524 | hotkey << wxT("Command" ); | |
1525 | break; | |
1526 | /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt | |
88d19775 MR |
1527 | case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4: |
1528 | case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8: | |
1529 | case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12: | |
1530 | case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16: | |
bbcd4085 RR |
1531 | case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20: |
1532 | hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1); | |
1533 | break; | |
1534 | */ | |
90527a50 | 1535 | // if there are any other keys wxAcceleratorEntry::Create() may |
a070d8ce | 1536 | // return, we should process them here |
8bbe427f | 1537 | |
717a57c2 | 1538 | default: |
a070d8ce | 1539 | if ( code < 127 ) |
717a57c2 | 1540 | { |
30083ad8 VZ |
1541 | const wxString |
1542 | name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code)); | |
1543 | if ( !name.empty() ) | |
a070d8ce VZ |
1544 | { |
1545 | hotkey << name; | |
1546 | break; | |
1547 | } | |
717a57c2 | 1548 | } |
c801d85f | 1549 | |
717a57c2 VZ |
1550 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
1551 | } | |
c801d85f | 1552 | |
717a57c2 | 1553 | delete accel; |
631f1bfe | 1554 | } |
717a57c2 VZ |
1555 | |
1556 | return hotkey; | |
631f1bfe | 1557 | } |
a070d8ce | 1558 | |
717a57c2 | 1559 | #endif // wxUSE_ACCEL |
43a11e2a VZ |
1560 | |
1561 | // ---------------------------------------------------------------------------- | |
1562 | // Pop-up menu stuff | |
1563 | // ---------------------------------------------------------------------------- | |
1564 | ||
1565 | #if wxUSE_MENUS_NATIVE | |
1566 | ||
81939780 | 1567 | extern "C" WXDLLIMPEXP_CORE |
43a11e2a VZ |
1568 | void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting ) |
1569 | { | |
670f9935 | 1570 | *is_waiting = false; |
43a11e2a VZ |
1571 | } |
1572 | ||
81939780 | 1573 | WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win ) |
43a11e2a VZ |
1574 | { |
1575 | menu->SetInvokingWindow( win ); | |
1576 | ||
1577 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
1578 | while (node) | |
1579 | { | |
1580 | wxMenuItem *menuitem = node->GetData(); | |
1581 | if (menuitem->IsSubMenu()) | |
1582 | { | |
1583 | SetInvokingWindow( menuitem->GetSubMenu(), win ); | |
1584 | } | |
1585 | ||
1586 | node = node->GetNext(); | |
1587 | } | |
1588 | } | |
1589 | ||
81939780 | 1590 | extern "C" WXDLLIMPEXP_CORE |
43a11e2a VZ |
1591 | void wxPopupMenuPositionCallback( GtkMenu *menu, |
1592 | gint *x, gint *y, | |
43a11e2a | 1593 | gboolean * WXUNUSED(whatever), |
43a11e2a VZ |
1594 | gpointer user_data ) |
1595 | { | |
1596 | // ensure that the menu appears entirely on screen | |
1597 | GtkRequisition req; | |
1598 | gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req); | |
1599 | ||
1600 | wxSize sizeScreen = wxGetDisplaySize(); | |
1601 | wxPoint *pos = (wxPoint*)user_data; | |
1602 | ||
1603 | gint xmax = sizeScreen.x - req.width, | |
1604 | ymax = sizeScreen.y - req.height; | |
1605 | ||
1606 | *x = pos->x < xmax ? pos->x : xmax; | |
1607 | *y = pos->y < ymax ? pos->y : ymax; | |
1608 | } | |
1609 | ||
1610 | bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y ) | |
1611 | { | |
1612 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") ); | |
1613 | ||
1614 | wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") ); | |
1615 | ||
1616 | // NOTE: if you change this code, you need to update | |
1617 | // the same code in taskbar.cpp as well. This | |
1618 | // is ugly code duplication, I know. | |
1619 | ||
1620 | SetInvokingWindow( menu, this ); | |
1621 | ||
1622 | menu->UpdateUI(); | |
1623 | ||
1624 | bool is_waiting = true; | |
1625 | ||
9fa72bd2 MR |
1626 | gulong handler = g_signal_connect (menu->m_menu, "hide", |
1627 | G_CALLBACK (gtk_pop_hide_callback), | |
1628 | &is_waiting); | |
43a11e2a VZ |
1629 | |
1630 | wxPoint pos; | |
1631 | gpointer userdata; | |
1632 | GtkMenuPositionFunc posfunc; | |
1633 | if ( x == -1 && y == -1 ) | |
1634 | { | |
1635 | // use GTK's default positioning algorithm | |
1636 | userdata = NULL; | |
1637 | posfunc = NULL; | |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | pos = ClientToScreen(wxPoint(x, y)); | |
1642 | userdata = &pos; | |
1643 | posfunc = wxPopupMenuPositionCallback; | |
1644 | } | |
1645 | ||
1646 | wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu); | |
1647 | DoCommonMenuCallbackCode(menu, eventOpen); | |
1648 | ||
1649 | gtk_menu_popup( | |
1650 | GTK_MENU(menu->m_menu), | |
1651 | (GtkWidget *) NULL, // parent menu shell | |
1652 | (GtkWidget *) NULL, // parent menu item | |
1653 | posfunc, // function to position it | |
1654 | userdata, // client data | |
1655 | 0, // button used to activate it | |
43a11e2a | 1656 | gtk_get_current_event_time() |
43a11e2a VZ |
1657 | ); |
1658 | ||
1659 | while (is_waiting) | |
1660 | { | |
1661 | gtk_main_iteration(); | |
1662 | } | |
1663 | ||
9fa72bd2 | 1664 | g_signal_handler_disconnect (menu->m_menu, handler); |
43a11e2a VZ |
1665 | |
1666 | wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu); | |
1667 | DoCommonMenuCallbackCode(menu, eventClose); | |
1668 | ||
1669 | return true; | |
1670 | } | |
1671 | ||
1672 | #endif // wxUSE_MENUS_NATIVE | |
bcf881ef JS |
1673 | |
1674 | #ifdef __WXGTK20__ | |
1675 | ||
1676 | #include <gtk/gtk.h> | |
1677 | ||
1678 | const char *wxGetStockGtkID(wxWindowID id) | |
1679 | { | |
1680 | #define STOCKITEM(wx,gtk) \ | |
1681 | case wx: \ | |
1682 | return gtk; | |
1683 | ||
1684 | #define STOCKITEM_MISSING(wx) \ | |
1685 | case wx: \ | |
1686 | return NULL; | |
1687 | ||
1688 | #if GTK_CHECK_VERSION(2,4,0) | |
1689 | #define STOCKITEM_24(wx,gtk) STOCKITEM(wx,gtk) | |
1690 | #else | |
1691 | #define STOCKITEM_24(wx,gtk) STOCKITEM_MISSING(wx) | |
1692 | #endif | |
1693 | ||
1694 | #if GTK_CHECK_VERSION(2,6,0) | |
1695 | #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk) | |
1696 | #else | |
1697 | #define STOCKITEM_26(wx,gtk) STOCKITEM_MISSING(wx) | |
1698 | #endif | |
1699 | ||
1700 | #if GTK_CHECK_VERSION(2,10,0) | |
1701 | #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk) | |
1702 | #else | |
1703 | #define STOCKITEM_210(wx,gtk) STOCKITEM_MISSING(wx) | |
1704 | #endif | |
1705 | ||
1706 | ||
1707 | switch (id) | |
1708 | { | |
1709 | STOCKITEM_26(wxID_ABOUT, GTK_STOCK_ABOUT) | |
1710 | STOCKITEM(wxID_ADD, GTK_STOCK_ADD) | |
1711 | STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY) | |
1712 | STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD) | |
1713 | STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL) | |
1714 | STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR) | |
1715 | STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE) | |
1716 | STOCKITEM(wxID_COPY, GTK_STOCK_COPY) | |
1717 | STOCKITEM(wxID_CUT, GTK_STOCK_CUT) | |
1718 | STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE) | |
1719 | STOCKITEM_26(wxID_EDIT, GTK_STOCK_EDIT) | |
1720 | STOCKITEM(wxID_FIND, GTK_STOCK_FIND) | |
1721 | STOCKITEM_26(wxID_FILE, GTK_STOCK_FILE) | |
1722 | STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE) | |
1723 | STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK) | |
1724 | STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN) | |
1725 | STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD) | |
1726 | STOCKITEM(wxID_UP, GTK_STOCK_GO_UP) | |
1727 | STOCKITEM(wxID_HELP, GTK_STOCK_HELP) | |
1728 | STOCKITEM(wxID_HOME, GTK_STOCK_HOME) | |
1729 | STOCKITEM_24(wxID_INDENT, GTK_STOCK_INDENT) | |
1730 | STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX) | |
1731 | STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC) | |
1732 | STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER) | |
1733 | STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL) | |
1734 | STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT) | |
1735 | STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT) | |
1736 | STOCKITEM(wxID_NEW, GTK_STOCK_NEW) | |
1737 | STOCKITEM(wxID_NO, GTK_STOCK_NO) | |
1738 | STOCKITEM(wxID_OK, GTK_STOCK_OK) | |
1739 | STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN) | |
1740 | STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE) | |
1741 | STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES) | |
1742 | STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT) | |
1743 | STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW) | |
1744 | STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES) | |
1745 | STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT) | |
1746 | STOCKITEM(wxID_REDO, GTK_STOCK_REDO) | |
1747 | STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH) | |
1748 | STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE) | |
1749 | STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED) | |
1750 | STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE) | |
1751 | STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS) | |
1752 | STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL) | |
1753 | STOCKITEM(wxID_STOP, GTK_STOCK_STOP) | |
1754 | STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE) | |
1755 | STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE) | |
1756 | STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO) | |
1757 | STOCKITEM_24(wxID_UNINDENT, GTK_STOCK_UNINDENT) | |
1758 | STOCKITEM(wxID_YES, GTK_STOCK_YES) | |
1759 | STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100) | |
1760 | STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT) | |
1761 | STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN) | |
1762 | STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT) | |
1763 | ||
1764 | default: | |
1765 | wxFAIL_MSG( _T("invalid stock item ID") ); | |
1766 | break; | |
1767 | }; | |
1768 | ||
1769 | #undef STOCKITEM | |
1770 | ||
1771 | return NULL; | |
1772 | } | |
1773 | ||
1774 | bool wxGetStockGtkAccelerator(const char *id, GdkModifierType *mod, guint *key) | |
1775 | { | |
1776 | if (!id) | |
1777 | return false; | |
1778 | ||
1779 | GtkStockItem stock_item; | |
1780 | if (gtk_stock_lookup (id, &stock_item)) | |
1781 | { | |
1782 | if (key) *key = stock_item.keyval; | |
1783 | if (mod) *mod = stock_item.modifier; | |
1784 | ||
1785 | // some GTK stock items have zero values for the keyval; | |
1786 | // it means that they do not have an accelerator... | |
1787 | if (stock_item.keyval) | |
1788 | return true; | |
1789 | } | |
1790 | ||
1791 | return false; | |
1792 | } | |
1793 | ||
1794 | #endif // __WXGTK20__ |