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