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