]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
96fd301f | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling |
96fd301f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
c801d85f KB |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "menu.h" | |
6ca41e57 | 12 | #pragma implementation "menuitem.h" |
c801d85f KB |
13 | #endif |
14 | ||
15 | #include "wx/menu.h" | |
96fd301f | 16 | #include "wx/log.h" |
30dea054 | 17 | #include "wx/intl.h" |
06cfab17 | 18 | #include "wx/app.h" |
c801d85f | 19 | |
83624f79 RR |
20 | #include "gdk/gdk.h" |
21 | #include "gtk/gtk.h" | |
22 | ||
acfd422a RR |
23 | //----------------------------------------------------------------------------- |
24 | // idle system | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | extern void wxapp_install_idle_handler(); | |
28 | extern bool g_isIdle; | |
29 | ||
c801d85f KB |
30 | //----------------------------------------------------------------------------- |
31 | // wxMenuBar | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
35 | ||
3502e687 RR |
36 | wxMenuBar::wxMenuBar( long style ) |
37 | { | |
1e133b7d | 38 | /* the parent window is known after wxFrame::SetMenu() */ |
23280650 | 39 | m_needParent = FALSE; |
ae53c98c | 40 | m_style = style; |
23280650 | 41 | |
16bcc879 | 42 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, style, "menu" ); |
3502e687 RR |
43 | |
44 | m_menus.DeleteContents( TRUE ); | |
45 | ||
1e133b7d RR |
46 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
47 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
48 | m_accel = gtk_accel_group_new(); | |
49 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
50 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 51 | #else |
3502e687 | 52 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 53 | #endif |
3502e687 RR |
54 | |
55 | if (style & wxMB_DOCKABLE) | |
56 | { | |
57 | m_widget = gtk_handle_box_new(); | |
c626a8b7 VZ |
58 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
59 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
3502e687 RR |
60 | } |
61 | else | |
62 | { | |
63 | m_widget = GTK_WIDGET(m_menubar); | |
64 | } | |
65 | ||
66 | PostCreation(); | |
3502e687 RR |
67 | } |
68 | ||
96fd301f | 69 | wxMenuBar::wxMenuBar() |
c801d85f | 70 | { |
1e133b7d RR |
71 | /* the parent window is known after wxFrame::SetMenu() */ |
72 | m_needParent = FALSE; | |
ae53c98c | 73 | m_style = 0; |
23280650 | 74 | |
83624f79 | 75 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
c801d85f | 76 | |
83624f79 | 77 | m_menus.DeleteContents( TRUE ); |
96fd301f | 78 | |
1e133b7d RR |
79 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
80 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
81 | m_accel = gtk_accel_group_new(); | |
82 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
83 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 84 | #else |
83624f79 | 85 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 86 | #endif |
8bbe427f | 87 | |
828f655f | 88 | m_widget = GTK_WIDGET(m_menubar); |
96fd301f | 89 | |
83624f79 | 90 | PostCreation(); |
6de97a3b | 91 | } |
c801d85f | 92 | |
1e133b7d RR |
93 | wxMenuBar::~wxMenuBar() |
94 | { | |
95 | // how to destroy a GtkItemFactory ? | |
96 | } | |
97 | ||
5bd9e519 RR |
98 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
99 | { | |
100 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
101 | ||
102 | #if (GTK_MINOR_VERSION > 0) | |
103 | wxWindow *top_frame = win; | |
104 | while (top_frame->GetParent()) top_frame = top_frame->GetParent(); | |
105 | ||
106 | /* support for native hot keys */ | |
107 | gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
108 | #endif | |
109 | ||
110 | wxNode *node = menu->GetItems().First(); | |
111 | while (node) | |
112 | { | |
113 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
114 | if (menuitem->IsSubMenu()) | |
115 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
116 | node = node->Next(); | |
117 | } | |
118 | } | |
119 | ||
120 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
121 | { | |
122 | menu->SetInvokingWindow( win ); | |
123 | ||
124 | #if (GTK_MINOR_VERSION > 0) | |
125 | wxWindow *top_frame = win; | |
126 | while (top_frame->GetParent()) | |
127 | top_frame = top_frame->GetParent(); | |
128 | ||
129 | /* support for native hot keys */ | |
130 | gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
131 | #endif | |
132 | ||
133 | wxNode *node = menu->GetItems().First(); | |
134 | while (node) | |
135 | { | |
136 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
137 | if (menuitem->IsSubMenu()) | |
138 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
139 | node = node->Next(); | |
140 | } | |
141 | } | |
142 | ||
143 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
144 | { | |
145 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
146 | wxWindow *top_frame = win; | |
147 | while (top_frame->GetParent()) | |
148 | top_frame = top_frame->GetParent(); | |
149 | ||
150 | /* support for native key accelerators indicated by underscroes */ | |
151 | gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
152 | #endif | |
153 | ||
154 | wxNode *node = m_menus.First(); | |
155 | while (node) | |
156 | { | |
157 | wxMenu *menu = (wxMenu*)node->Data(); | |
158 | wxMenubarSetInvokingWindow( menu, win ); | |
159 | node = node->Next(); | |
160 | } | |
161 | } | |
162 | ||
163 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
164 | { | |
165 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
166 | wxWindow *top_frame = win; | |
167 | while (top_frame->GetParent()) | |
168 | top_frame = top_frame->GetParent(); | |
169 | ||
170 | /* support for native key accelerators indicated by underscroes */ | |
171 | gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
172 | #endif | |
173 | ||
174 | wxNode *node = m_menus.First(); | |
175 | while (node) | |
176 | { | |
177 | wxMenu *menu = (wxMenu*)node->Data(); | |
178 | wxMenubarUnsetInvokingWindow( menu, win ); | |
179 | node = node->Next(); | |
180 | } | |
181 | } | |
182 | ||
c801d85f KB |
183 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
184 | { | |
83624f79 | 185 | m_menus.Append( menu ); |
23280650 | 186 | |
a533f5c1 | 187 | const wxChar *pc; |
23280650 | 188 | |
1e133b7d RR |
189 | /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ |
190 | wxString str; | |
a533f5c1 | 191 | for ( pc = title; *pc != _T('\0'); pc++ ) |
83624f79 | 192 | { |
b019151f | 193 | if (*pc == _T('&')) |
23280650 | 194 | { |
1e133b7d RR |
195 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
196 | str << _T('_'); | |
23280650 VZ |
197 | } else |
198 | if (*pc == _T('/')) | |
199 | { | |
fac4253c | 200 | str << _T('\\'); |
034be888 | 201 | #endif |
23280650 | 202 | } |
d38ceae8 KB |
203 | else |
204 | str << *pc; | |
1e133b7d RR |
205 | } |
206 | ||
207 | /* this doesn't have much effect right now */ | |
208 | menu->SetTitle( str ); | |
23280650 | 209 | |
1e133b7d RR |
210 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
211 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
212 | ||
213 | /* local buffer in multibyte form */ | |
2b2edbed | 214 | wxString buf; |
c980c992 GL |
215 | buf << _T('/') << str.c_str(); |
216 | ||
2b2edbed | 217 | char *cbuf = new char[buf.Length()]; |
c980c992 GL |
218 | strcpy(cbuf, buf.mbc_str()); |
219 | ||
1e133b7d | 220 | GtkItemFactoryEntry entry; |
c980c992 | 221 | entry.path = (gchar *)cbuf; // const_cast |
1e133b7d RR |
222 | entry.accelerator = (gchar*) NULL; |
223 | entry.callback = (GtkItemFactoryCallback) NULL; | |
224 | entry.callback_action = 0; | |
2b2edbed KB |
225 | entry.item_type = "<Branch>"; |
226 | ||
1e133b7d | 227 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
1e133b7d RR |
228 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
229 | wxString tmp = _T("<main>/"); | |
a533f5c1 | 230 | for ( pc = str; *pc != _T('\0'); pc++ ) |
1e133b7d | 231 | { |
2b2edbed KB |
232 | if (*pc == _T('_')) pc++; /* skip it */ |
233 | tmp << *pc; | |
034be888 | 234 | } |
1e133b7d | 235 | menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() ); |
1e133b7d | 236 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
2b2edbed | 237 | delete [] cbuf; |
1e133b7d | 238 | #else |
96fd301f | 239 | |
1e133b7d | 240 | menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() ); |
2b1c162e RR |
241 | gtk_widget_show( menu->m_owner ); |
242 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
96fd301f | 243 | |
2b1c162e | 244 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
23280650 | 245 | |
1e133b7d | 246 | #endif |
6de97a3b | 247 | } |
96fd301f | 248 | |
716b7364 | 249 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 250 | { |
c626a8b7 | 251 | if (menu->GetTitle() == menuString) |
83624f79 RR |
252 | { |
253 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
254 | if (res != wxNOT_FOUND) |
255 | return res; | |
83624f79 | 256 | } |
c626a8b7 VZ |
257 | |
258 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast | |
83624f79 RR |
259 | while (node) |
260 | { | |
261 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
262 | if (item->IsSubMenu()) | |
263 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 264 | |
83624f79 RR |
265 | node = node->Next(); |
266 | } | |
267 | ||
c626a8b7 VZ |
268 | return wxNOT_FOUND; |
269 | } | |
270 | ||
80ec928c | 271 | wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem ) const |
c626a8b7 VZ |
272 | { |
273 | if ( menuForItem ) | |
274 | { | |
275 | // TODO return the pointer to the menu | |
276 | ||
277 | *menuForItem = NULL; | |
278 | } | |
279 | ||
280 | return FindItem(itemId); | |
6de97a3b | 281 | } |
c801d85f KB |
282 | |
283 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
284 | { | |
83624f79 RR |
285 | wxNode *node = m_menus.First(); |
286 | while (node) | |
287 | { | |
288 | wxMenu *menu = (wxMenu*)node->Data(); | |
289 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
290 | if (res != -1) return res; | |
291 | node = node->Next(); | |
292 | } | |
293 | return -1; | |
6de97a3b | 294 | } |
c801d85f | 295 | |
c626a8b7 | 296 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 297 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 298 | { |
83624f79 | 299 | wxMenuItem* result = menu->FindItem(id); |
716b7364 | 300 | |
c626a8b7 VZ |
301 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast |
302 | while ( node && result == NULL ) | |
83624f79 RR |
303 | { |
304 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
305 | if (item->IsSubMenu()) | |
c626a8b7 | 306 | { |
83624f79 | 307 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 308 | } |
83624f79 RR |
309 | node = node->Next(); |
310 | } | |
96fd301f | 311 | |
83624f79 | 312 | return result; |
6de97a3b | 313 | } |
716b7364 | 314 | |
c626a8b7 | 315 | wxMenuItem* wxMenuBar::FindItem( int id ) const |
716b7364 | 316 | { |
83624f79 RR |
317 | wxMenuItem* result = 0; |
318 | wxNode *node = m_menus.First(); | |
319 | while (node && result == 0) | |
320 | { | |
321 | wxMenu *menu = (wxMenu*)node->Data(); | |
322 | result = FindMenuItemByIdRecursive( menu, id ); | |
323 | node = node->Next(); | |
324 | } | |
c626a8b7 | 325 | |
83624f79 | 326 | return result; |
716b7364 RR |
327 | } |
328 | ||
54ff4a70 RR |
329 | void wxMenuBar::Check( int id, bool check ) |
330 | { | |
83624f79 | 331 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 332 | |
b019151f | 333 | wxCHECK_RET( item, _T("wxMenuBar::Check: no such item") ); |
c626a8b7 VZ |
334 | |
335 | item->Check(check); | |
6de97a3b | 336 | } |
54ff4a70 | 337 | |
c626a8b7 | 338 | bool wxMenuBar::IsChecked( int id ) const |
716b7364 | 339 | { |
83624f79 | 340 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 341 | |
b019151f | 342 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked: no such item") ); |
c626a8b7 VZ |
343 | |
344 | return item->IsChecked(); | |
6de97a3b | 345 | } |
716b7364 | 346 | |
54ff4a70 RR |
347 | void wxMenuBar::Enable( int id, bool enable ) |
348 | { | |
83624f79 | 349 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 350 | |
b019151f | 351 | wxCHECK_RET( item, _T("wxMenuBar::Enable: no such item") ); |
c626a8b7 VZ |
352 | |
353 | item->Enable(enable); | |
6de97a3b | 354 | } |
54ff4a70 | 355 | |
c626a8b7 | 356 | bool wxMenuBar::IsEnabled( int id ) const |
716b7364 | 357 | { |
83624f79 | 358 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 359 | |
b019151f | 360 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled: no such item") ); |
c626a8b7 VZ |
361 | |
362 | return item->IsEnabled(); | |
6de97a3b | 363 | } |
716b7364 | 364 | |
bbe0af5b RR |
365 | wxString wxMenuBar::GetLabel( int id ) const |
366 | { | |
367 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 368 | |
b019151f | 369 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel: no such item") ); |
c626a8b7 VZ |
370 | |
371 | return item->GetText(); | |
bbe0af5b RR |
372 | } |
373 | ||
374 | void wxMenuBar::SetLabel( int id, const wxString &label ) | |
375 | { | |
376 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 377 | |
b019151f | 378 | wxCHECK_RET( item, _T("wxMenuBar::SetLabel: no such item") ); |
c626a8b7 VZ |
379 | |
380 | item->SetText( label ); | |
bbe0af5b RR |
381 | } |
382 | ||
2b1c162e | 383 | void wxMenuBar::EnableTop( int pos, bool flag ) |
bbe0af5b | 384 | { |
2b1c162e | 385 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 386 | |
b019151f | 387 | wxCHECK_RET( node, _T("menu not found") ); |
c626a8b7 | 388 | |
2b1c162e | 389 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 VZ |
390 | |
391 | if (menu->m_owner) | |
392 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
393 | } |
394 | ||
2b1c162e | 395 | wxString wxMenuBar::GetLabelTop( int pos ) const |
bbe0af5b | 396 | { |
2b1c162e | 397 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 398 | |
b019151f | 399 | wxCHECK_MSG( node, _T("invalid"), _T("menu not found") ); |
c626a8b7 | 400 | |
2b1c162e | 401 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 402 | |
2b1c162e | 403 | return menu->GetTitle(); |
bbe0af5b RR |
404 | } |
405 | ||
2b1c162e | 406 | void wxMenuBar::SetLabelTop( int pos, const wxString& label ) |
bbe0af5b | 407 | { |
2b1c162e | 408 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 409 | |
b019151f | 410 | wxCHECK_RET( node, _T("menu not found") ); |
c626a8b7 | 411 | |
2b1c162e | 412 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 413 | |
2b1c162e | 414 | menu->SetTitle( label ); |
bbe0af5b RR |
415 | } |
416 | ||
342b6a2f RR |
417 | void wxMenuBar::SetHelpString( int id, const wxString& helpString ) |
418 | { | |
419 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 420 | |
b019151f | 421 | wxCHECK_RET( item, _T("wxMenuBar::SetHelpString: no such item") ); |
c626a8b7 VZ |
422 | |
423 | item->SetHelp( helpString ); | |
342b6a2f RR |
424 | } |
425 | ||
426 | wxString wxMenuBar::GetHelpString( int id ) const | |
427 | { | |
428 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 429 | |
b019151f | 430 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString: no such item") ); |
c626a8b7 VZ |
431 | |
432 | return item->GetHelp(); | |
342b6a2f RR |
433 | } |
434 | ||
c801d85f | 435 | //----------------------------------------------------------------------------- |
cf7a7e13 | 436 | // "activate" |
c801d85f KB |
437 | //----------------------------------------------------------------------------- |
438 | ||
6de97a3b | 439 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 440 | { |
acfd422a RR |
441 | if (g_isIdle) wxapp_install_idle_handler(); |
442 | ||
83624f79 | 443 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 444 | |
83624f79 | 445 | /* should find it for normal (not popup) menu */ |
c626a8b7 | 446 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); |
96fd301f | 447 | |
c626a8b7 VZ |
448 | if (!menu->IsEnabled(id)) |
449 | return; | |
96fd301f | 450 | |
2d17d68f | 451 | wxMenuItem* item = menu->FindItem( id ); |
b019151f | 452 | wxCHECK_RET( item, _T("error in menu item callback") ); |
c626a8b7 VZ |
453 | |
454 | if (item->IsCheckable()) | |
2d17d68f | 455 | { |
c626a8b7 | 456 | if (item->GetCheckedFlag() == item->IsChecked()) |
2d17d68f | 457 | { |
c626a8b7 | 458 | /* the menu item has been checked by calling wxMenuItem->Check() */ |
2d17d68f | 459 | return; |
c626a8b7 VZ |
460 | } |
461 | else | |
462 | { | |
463 | /* the user pressed on the menu item -> report */ | |
464 | item->SetCheckedFlag(item->IsChecked()); /* make consistent again */ | |
465 | } | |
2d17d68f RR |
466 | } |
467 | ||
83624f79 RR |
468 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
469 | event.SetEventObject( menu ); | |
470 | event.SetInt(id ); | |
8bbe427f | 471 | |
c626a8b7 | 472 | if (menu->GetCallback()) |
83624f79 | 473 | { |
c626a8b7 | 474 | (void) (*(menu->GetCallback())) (*menu, event); |
83624f79 RR |
475 | return; |
476 | } | |
cf7a7e13 | 477 | |
c626a8b7 VZ |
478 | if (menu->GetEventHandler()->ProcessEvent(event)) |
479 | return; | |
cf7a7e13 | 480 | |
83624f79 | 481 | wxWindow *win = menu->GetInvokingWindow(); |
c626a8b7 VZ |
482 | if (win) |
483 | win->GetEventHandler()->ProcessEvent( event ); | |
cf7a7e13 RR |
484 | } |
485 | ||
486 | //----------------------------------------------------------------------------- | |
487 | // "select" | |
488 | //----------------------------------------------------------------------------- | |
489 | ||
490 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
491 | { | |
acfd422a RR |
492 | if (g_isIdle) wxapp_install_idle_handler(); |
493 | ||
83624f79 RR |
494 | int id = menu->FindMenuIdByMenuItem(widget); |
495 | ||
496 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 497 | |
c626a8b7 VZ |
498 | if (!menu->IsEnabled(id)) |
499 | return; | |
cf7a7e13 | 500 | |
342b6a2f | 501 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 502 | event.SetEventObject( menu ); |
cf7a7e13 | 503 | |
c626a8b7 VZ |
504 | if (menu->GetEventHandler()->ProcessEvent(event)) |
505 | return; | |
6de97a3b | 506 | |
83624f79 RR |
507 | wxWindow *win = menu->GetInvokingWindow(); |
508 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 509 | } |
c801d85f | 510 | |
cd743a6f RR |
511 | //----------------------------------------------------------------------------- |
512 | // "deselect" | |
513 | //----------------------------------------------------------------------------- | |
514 | ||
515 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
516 | { | |
acfd422a RR |
517 | if (g_isIdle) wxapp_install_idle_handler(); |
518 | ||
cd743a6f RR |
519 | int id = menu->FindMenuIdByMenuItem(widget); |
520 | ||
521 | wxASSERT( id != -1 ); // should find it! | |
522 | ||
c626a8b7 VZ |
523 | if (!menu->IsEnabled(id)) |
524 | return; | |
cd743a6f RR |
525 | |
526 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
527 | event.SetEventObject( menu ); | |
528 | ||
c626a8b7 VZ |
529 | if (menu->GetEventHandler()->ProcessEvent(event)) |
530 | return; | |
cd743a6f RR |
531 | |
532 | wxWindow *win = menu->GetInvokingWindow(); | |
c626a8b7 VZ |
533 | if (win) |
534 | win->GetEventHandler()->ProcessEvent( event ); | |
cd743a6f RR |
535 | } |
536 | ||
cf7a7e13 | 537 | //----------------------------------------------------------------------------- |
db1b4961 | 538 | // wxMenuItem |
cf7a7e13 RR |
539 | //----------------------------------------------------------------------------- |
540 | ||
c801d85f | 541 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) |
96fd301f VZ |
542 | |
543 | wxMenuItem::wxMenuItem() | |
c801d85f | 544 | { |
83624f79 RR |
545 | m_id = ID_SEPARATOR; |
546 | m_isCheckMenu = FALSE; | |
547 | m_isChecked = FALSE; | |
548 | m_isEnabled = TRUE; | |
549 | m_subMenu = (wxMenu *) NULL; | |
550 | m_menuItem = (GtkWidget *) NULL; | |
6de97a3b | 551 | } |
c801d85f | 552 | |
c626a8b7 | 553 | // it's valid for this function to be called even if m_menuItem == NULL |
83624f79 | 554 | void wxMenuItem::SetName( const wxString& str ) |
716b7364 | 555 | { |
ab46dc18 | 556 | /* '\t' is the deliminator indicating a hot key */ |
b019151f | 557 | m_text = _T(""); |
ab46dc18 RR |
558 | const wxChar *pc = str; |
559 | for (; (*pc != _T('\0')) && (*pc != _T('\t')); pc++ ) | |
83624f79 | 560 | { |
b019151f | 561 | if (*pc == _T('&')) |
23280650 | 562 | { |
034be888 | 563 | #if (GTK_MINOR_VERSION > 0) |
b019151f | 564 | m_text << _T('_'); |
23280650 VZ |
565 | } else |
566 | if (*pc == _T('/')) /* we have to filter out slashes ... */ | |
567 | { | |
6bc8a1c8 | 568 | m_text << _T('\\'); /* ... and replace them with back slashes */ |
034be888 RR |
569 | #endif |
570 | } | |
d38ceae8 KB |
571 | else |
572 | m_text << *pc; | |
83624f79 | 573 | } |
23280650 | 574 | |
837904f2 | 575 | /* only GTK 1.2 knows about hot keys */ |
ab46dc18 RR |
576 | m_hotKey = _T(""); |
577 | #if (GTK_MINOR_VERSION > 0) | |
d7dbc98a KB |
578 | if(*pc == _T('\t')) |
579 | { | |
580 | pc++; | |
581 | m_hotKey = pc; | |
582 | } | |
ab46dc18 | 583 | #endif |
96fd301f | 584 | |
83624f79 RR |
585 | if (m_menuItem) |
586 | { | |
587 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
1e133b7d | 588 | gtk_label_set( label, m_text.mb_str()); |
83624f79 | 589 | } |
716b7364 RR |
590 | } |
591 | ||
96fd301f | 592 | void wxMenuItem::Check( bool check ) |
716b7364 | 593 | { |
b019151f | 594 | wxCHECK_RET( m_menuItem, _T("invalid menu item") ); |
db1b4961 | 595 | |
b019151f | 596 | wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") ) |
96fd301f | 597 | |
2d17d68f RR |
598 | if (check == m_isChecked) return; |
599 | ||
83624f79 RR |
600 | m_isChecked = check; |
601 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
602 | } |
603 | ||
8bbe427f VZ |
604 | void wxMenuItem::Enable( bool enable ) |
605 | { | |
b019151f | 606 | wxCHECK_RET( m_menuItem, _T("invalid menu item") ); |
db1b4961 | 607 | |
83624f79 RR |
608 | gtk_widget_set_sensitive( m_menuItem, enable ); |
609 | m_isEnabled = enable; | |
a9c96bcc RR |
610 | } |
611 | ||
96fd301f | 612 | bool wxMenuItem::IsChecked() const |
716b7364 | 613 | { |
b019151f | 614 | wxCHECK_MSG( m_menuItem, FALSE, _T("invalid menu item") ); |
db1b4961 | 615 | |
83624f79 | 616 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
96fd301f | 617 | |
83624f79 | 618 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
96fd301f | 619 | |
83624f79 | 620 | return bIsChecked; |
716b7364 RR |
621 | } |
622 | ||
db1b4961 | 623 | //----------------------------------------------------------------------------- |
83624f79 | 624 | // wxMenu |
db1b4961 RR |
625 | //----------------------------------------------------------------------------- |
626 | ||
c801d85f KB |
627 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
628 | ||
ae53c98c | 629 | void |
b908d224 | 630 | wxMenu::Init( const wxString& title, |
1ecffbff | 631 | long style |
b908d224 VZ |
632 | #ifdef WXWIN_COMPATIBILITY |
633 | , const wxFunction func | |
634 | #endif | |
635 | ) | |
c801d85f | 636 | { |
83624f79 RR |
637 | m_title = title; |
638 | m_items.DeleteContents( TRUE ); | |
639 | m_invokingWindow = (wxWindow *) NULL; | |
ae53c98c | 640 | m_style = style; |
23280650 | 641 | |
034be888 RR |
642 | #if (GTK_MINOR_VERSION > 0) |
643 | m_accel = gtk_accel_group_new(); | |
644 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); | |
645 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 646 | #else |
83624f79 | 647 | m_menu = gtk_menu_new(); // Do not show! |
034be888 | 648 | #endif |
8bbe427f | 649 | |
b908d224 | 650 | #ifdef WXWIN_COMPATIBILITY |
83624f79 | 651 | m_callback = func; |
b908d224 VZ |
652 | #endif |
653 | ||
83624f79 RR |
654 | m_eventHandler = this; |
655 | m_clientData = (void*) NULL; | |
8bbe427f | 656 | |
b019151f OK |
657 | if (m_title.IsNull()) m_title = _T(""); |
658 | if (m_title != _T("")) | |
83624f79 RR |
659 | { |
660 | Append(-2, m_title); | |
661 | AppendSeparator(); | |
662 | } | |
c626a8b7 | 663 | |
2b1c162e | 664 | m_owner = (GtkWidget*) NULL; |
2b2edbed KB |
665 | |
666 | #if (GTK_MINOR_VERSION > 0) | |
667 | /* Tearoffs are entries, just like separators. So if we want this | |
668 | menu to be a tear-off one, we just append a tearoff entry | |
669 | immediately. */ | |
670 | if(m_style & wxMENU_TEAROFF) | |
671 | { | |
672 | GtkItemFactoryEntry entry; | |
673 | entry.path = "/tearoff"; | |
674 | entry.callback = (GtkItemFactoryCallback) NULL; | |
675 | entry.callback_action = 0; | |
676 | entry.item_type = "<Tearoff>"; | |
677 | entry.accelerator = (gchar*) NULL; | |
678 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
23280650 | 679 | //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" ); |
2b2edbed KB |
680 | } |
681 | #endif | |
6de97a3b | 682 | } |
c801d85f | 683 | |
034be888 RR |
684 | wxMenu::~wxMenu() |
685 | { | |
15a2076a KB |
686 | /* how do we delete an item-factory ? */ |
687 | gtk_widget_destroy( m_menu ); | |
688 | ||
034be888 RR |
689 | } |
690 | ||
c2dd8380 GL |
691 | void wxMenu::SetTitle( const wxString& title ) |
692 | { | |
c626a8b7 | 693 | // TODO Waiting for something better |
83624f79 | 694 | m_title = title; |
c2dd8380 GL |
695 | } |
696 | ||
697 | const wxString wxMenu::GetTitle() const | |
698 | { | |
83624f79 | 699 | return m_title; |
c2dd8380 GL |
700 | } |
701 | ||
96fd301f | 702 | void wxMenu::AppendSeparator() |
c801d85f | 703 | { |
83624f79 RR |
704 | wxMenuItem *mitem = new wxMenuItem(); |
705 | mitem->SetId(ID_SEPARATOR); | |
96fd301f | 706 | |
08fc1744 RR |
707 | #if (GTK_MINOR_VERSION > 0) |
708 | GtkItemFactoryEntry entry; | |
709 | entry.path = "/sep"; | |
710 | entry.callback = (GtkItemFactoryCallback) NULL; | |
711 | entry.callback_action = 0; | |
712 | entry.item_type = "<Separator>"; | |
713 | entry.accelerator = (gchar*) NULL; | |
23280650 | 714 | |
08fc1744 | 715 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 716 | |
08fc1744 RR |
717 | /* this will be wrong for more than one separator. do we care? */ |
718 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" ); | |
719 | #else | |
83624f79 RR |
720 | GtkWidget *menuItem = gtk_menu_item_new(); |
721 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
722 | gtk_widget_show( menuItem ); | |
08fc1744 | 723 | #endif |
23280650 | 724 | |
83624f79 RR |
725 | mitem->SetMenuItem(menuItem); |
726 | m_items.Append( mitem ); | |
6de97a3b | 727 | } |
c801d85f | 728 | |
837904f2 RR |
729 | static char* GetHotKey( const wxString &hotkey, char *hotbuf ) |
730 | { | |
731 | if (hotkey.IsEmpty()) return (char*) NULL; | |
23280650 | 732 | |
837904f2 RR |
733 | switch (hotkey[0]) |
734 | { | |
735 | case _T('a'): /* Alt */ | |
23280650 VZ |
736 | case _T('A'): |
737 | case _T('m'): /* Meta */ | |
738 | case _T('M'): | |
739 | { | |
740 | strcpy( hotbuf, "<alt>" ); | |
741 | wxString last = hotkey.Right(1); | |
742 | strcat( hotbuf, last.mb_str() ); | |
837904f2 | 743 | return hotbuf; |
23280650 VZ |
744 | } |
745 | case _T('c'): /* Ctrl */ | |
746 | case _T('C'): | |
747 | case _T('s'): /* Strg, yeah man, I'm German */ | |
748 | case _T('S'): | |
749 | { | |
750 | strcpy( hotbuf, "<control>" ); | |
751 | wxString last = hotkey.Right(1); | |
752 | strcat( hotbuf, last.mb_str() ); | |
837904f2 RR |
753 | return hotbuf; |
754 | } | |
23280650 VZ |
755 | case _T('F'): /* function keys */ |
756 | { | |
757 | strcpy( hotbuf, hotkey.mb_str() ); | |
837904f2 | 758 | return hotbuf; |
23280650 VZ |
759 | } |
760 | default: | |
761 | { | |
762 | } | |
837904f2 RR |
763 | } |
764 | return (char*) NULL; | |
765 | } | |
766 | ||
debe6624 | 767 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f | 768 | { |
83624f79 RR |
769 | wxMenuItem *mitem = new wxMenuItem(); |
770 | mitem->SetId(id); | |
771 | mitem->SetText(item); | |
772 | mitem->SetHelp(helpStr); | |
773 | mitem->SetCheckable(checkable); | |
23280650 | 774 | |
034be888 | 775 | #if (GTK_MINOR_VERSION > 0) |
23280650 | 776 | /* text has "_" instead of "&" after mitem->SetText() */ |
1e133b7d | 777 | wxString text( mitem->GetText() ); |
23280650 | 778 | |
1e133b7d RR |
779 | /* local buffer in multibyte form */ |
780 | char buf[200]; | |
781 | strcpy( buf, "/" ); | |
782 | strcat( buf, text.mb_str() ); | |
23280650 | 783 | |
034be888 | 784 | GtkItemFactoryEntry entry; |
1e133b7d | 785 | entry.path = buf; |
034be888 RR |
786 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; |
787 | entry.callback_action = 0; | |
788 | if (checkable) | |
789 | entry.item_type = "<CheckItem>"; | |
790 | else | |
791 | entry.item_type = "<Item>"; | |
23280650 | 792 | |
ab46dc18 | 793 | char hotbuf[50]; |
837904f2 | 794 | entry.accelerator = GetHotKey( mitem->GetHotKey(), hotbuf ); |
23280650 | 795 | |
034be888 | 796 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 797 | |
034be888 | 798 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
b019151f OK |
799 | wxString s = _T("<main>/"); |
800 | for ( const wxChar *pc = text; *pc != _T('\0'); pc++ ) | |
034be888 | 801 | { |
b019151f | 802 | if (*pc == _T('_')) pc++; /* skip it */ |
034be888 RR |
803 | s << *pc; |
804 | } | |
23280650 | 805 | |
1e133b7d | 806 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() ); |
23280650 | 807 | |
034be888 RR |
808 | #else |
809 | ||
d38ceae8 KB |
810 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() ) |
811 | : gtk_menu_item_new_with_label( mitem->GetText().mb_str() ); | |
23280650 | 812 | |
83624f79 RR |
813 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
814 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
034be888 | 815 | (gpointer)this ); |
23280650 | 816 | |
034be888 RR |
817 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
818 | gtk_widget_show( menuItem ); | |
23280650 | 819 | |
034be888 | 820 | #endif |
96fd301f | 821 | |
83624f79 RR |
822 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
823 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
034be888 | 824 | (gpointer)this ); |
cf7a7e13 | 825 | |
cd743a6f RR |
826 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
827 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
034be888 RR |
828 | (gpointer)this ); |
829 | ||
830 | mitem->SetMenuItem(menuItem); | |
cd743a6f | 831 | |
83624f79 | 832 | m_items.Append( mitem ); |
6de97a3b | 833 | } |
c801d85f | 834 | |
837904f2 | 835 | void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f | 836 | { |
83624f79 RR |
837 | wxMenuItem *mitem = new wxMenuItem(); |
838 | mitem->SetId(id); | |
837904f2 | 839 | mitem->SetText(item); |
828f655f | 840 | mitem->SetHelp(helpStr); |
96fd301f | 841 | |
837904f2 | 842 | #if (GTK_MINOR_VERSION > 0) |
23280650 | 843 | /* text has "_" instead of "&" after mitem->SetText() */ |
837904f2 | 844 | wxString text( mitem->GetText() ); |
23280650 | 845 | |
837904f2 RR |
846 | /* local buffer in multibyte form */ |
847 | char buf[200]; | |
848 | strcpy( buf, "/" ); | |
849 | strcat( buf, text.mb_str() ); | |
23280650 | 850 | |
837904f2 RR |
851 | GtkItemFactoryEntry entry; |
852 | entry.path = buf; | |
853 | entry.callback = (GtkItemFactoryCallback) 0; | |
854 | entry.callback_action = 0; | |
2b2edbed | 855 | entry.item_type = "<Branch>"; |
23280650 | 856 | |
837904f2 | 857 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 858 | |
837904f2 RR |
859 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
860 | wxString s = _T("<main>/"); | |
861 | for ( const wxChar *pc = text; *pc != _T('\0'); pc++ ) | |
862 | { | |
863 | if (*pc == _T('_')) pc++; /* skip it */ | |
864 | s << *pc; | |
865 | } | |
23280650 | 866 | |
837904f2 | 867 | GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() ); |
23280650 | 868 | |
837904f2 RR |
869 | #else |
870 | ||
b019151f | 871 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str()); |
23280650 | 872 | |
837904f2 RR |
873 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
874 | gtk_widget_show( menuItem ); | |
23280650 VZ |
875 | |
876 | #endif | |
96fd301f | 877 | |
cd743a6f RR |
878 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
879 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
880 | (gpointer*)this ); | |
881 | ||
882 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
883 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
884 | (gpointer*)this ); | |
885 | ||
83624f79 | 886 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); |
23280650 | 887 | |
837904f2 RR |
888 | mitem->SetMenuItem(menuItem); |
889 | mitem->SetSubMenu(subMenu); | |
890 | ||
83624f79 | 891 | m_items.Append( mitem ); |
6de97a3b | 892 | } |
c801d85f | 893 | |
828f655f RR |
894 | void wxMenu::Append( wxMenuItem *item ) |
895 | { | |
896 | m_items.Append( item ); | |
c626a8b7 | 897 | |
828f655f RR |
898 | GtkWidget *menuItem = (GtkWidget*) NULL; |
899 | ||
c626a8b7 | 900 | if (item->IsSeparator()) |
828f655f | 901 | menuItem = gtk_menu_item_new(); |
c626a8b7 | 902 | else if (item->IsSubMenu()) |
b019151f | 903 | menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str()); |
c626a8b7 | 904 | else |
b019151f OK |
905 | menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str()) |
906 | : gtk_menu_item_new_with_label(item->GetText().mbc_str()); | |
828f655f RR |
907 | |
908 | if (!item->IsSeparator()) | |
909 | { | |
910 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
911 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
912 | (gpointer*)this ); | |
913 | ||
914 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
915 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
916 | (gpointer*)this ); | |
c626a8b7 VZ |
917 | |
918 | if (!item->IsSubMenu()) | |
919 | { | |
828f655f RR |
920 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
921 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
922 | (gpointer*)this ); | |
c626a8b7 | 923 | } |
828f655f | 924 | } |
c626a8b7 | 925 | |
828f655f RR |
926 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
927 | gtk_widget_show( menuItem ); | |
928 | item->SetMenuItem(menuItem); | |
929 | } | |
930 | ||
c801d85f KB |
931 | int wxMenu::FindItem( const wxString itemString ) const |
932 | { | |
b019151f OK |
933 | wxString s = _T(""); |
934 | for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ ) | |
83624f79 | 935 | { |
b019151f | 936 | if (*pc == _T('&')) |
23280650 VZ |
937 | { |
938 | pc++; /* skip it */ | |
034be888 | 939 | #if (GTK_MINOR_VERSION > 0) |
b019151f | 940 | s << _T('_'); |
034be888 RR |
941 | #endif |
942 | } | |
943 | s << *pc; | |
944 | } | |
96fd301f | 945 | |
83624f79 RR |
946 | wxNode *node = m_items.First(); |
947 | while (node) | |
948 | { | |
949 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
950 | if (item->GetText() == s) | |
c626a8b7 | 951 | { |
83624f79 | 952 | return item->GetId(); |
c626a8b7 | 953 | } |
83624f79 RR |
954 | node = node->Next(); |
955 | } | |
96fd301f | 956 | |
c626a8b7 | 957 | return wxNOT_FOUND; |
6de97a3b | 958 | } |
c801d85f | 959 | |
96fd301f | 960 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 961 | { |
83624f79 | 962 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 963 | |
b019151f | 964 | wxCHECK_RET( item, _T("wxMenu::Enable: no such item") ); |
c626a8b7 VZ |
965 | |
966 | item->Enable(enable); | |
6de97a3b | 967 | } |
716b7364 | 968 | |
96fd301f | 969 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 970 | { |
83624f79 | 971 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 972 | |
b019151f | 973 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") ); |
c626a8b7 VZ |
974 | |
975 | return item->IsEnabled(); | |
6de97a3b | 976 | } |
e2414cbe | 977 | |
96fd301f | 978 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 979 | { |
83624f79 | 980 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 981 | |
b019151f | 982 | wxCHECK_RET( item, _T("wxMenu::Check: no such item") ); |
c626a8b7 VZ |
983 | |
984 | item->Check(enable); | |
6de97a3b | 985 | } |
c801d85f | 986 | |
96fd301f | 987 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 988 | { |
83624f79 | 989 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 990 | |
b019151f | 991 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") ); |
c626a8b7 VZ |
992 | |
993 | return item->IsChecked(); | |
6de97a3b | 994 | } |
c801d85f | 995 | |
debe6624 | 996 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 997 | { |
83624f79 | 998 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 999 | |
b019151f | 1000 | wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") ); |
c626a8b7 VZ |
1001 | |
1002 | item->SetText(label); | |
6de97a3b | 1003 | } |
96fd301f | 1004 | |
c33c4050 RR |
1005 | wxString wxMenu::GetLabel( int id ) const |
1006 | { | |
83624f79 | 1007 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1008 | |
b019151f | 1009 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") ); |
c626a8b7 VZ |
1010 | |
1011 | return item->GetText(); | |
c33c4050 RR |
1012 | } |
1013 | ||
1014 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
1015 | { | |
83624f79 | 1016 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1017 | |
b019151f | 1018 | wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") ); |
c626a8b7 VZ |
1019 | |
1020 | item->SetHelp( helpString ); | |
c33c4050 RR |
1021 | } |
1022 | ||
1023 | wxString wxMenu::GetHelpString( int id ) const | |
1024 | { | |
83624f79 | 1025 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1026 | |
b019151f | 1027 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") ); |
c626a8b7 VZ |
1028 | |
1029 | return item->GetHelp(); | |
c33c4050 RR |
1030 | } |
1031 | ||
96fd301f VZ |
1032 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
1033 | { | |
83624f79 RR |
1034 | wxNode *node = m_items.First(); |
1035 | while (node) | |
1036 | { | |
1037 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1038 | if (item->GetMenuItem() == menuItem) | |
1039 | return item->GetId(); | |
1040 | node = node->Next(); | |
1041 | } | |
96fd301f | 1042 | |
c626a8b7 | 1043 | return wxNOT_FOUND; |
6de97a3b | 1044 | } |
c801d85f | 1045 | |
96fd301f | 1046 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f | 1047 | { |
83624f79 | 1048 | wxNode *node = m_items.First(); |
c626a8b7 | 1049 | while (node) |
83624f79 RR |
1050 | { |
1051 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1052 | if (item->GetId() == id) | |
c626a8b7 | 1053 | { |
83624f79 | 1054 | return item; |
c626a8b7 | 1055 | } |
83624f79 RR |
1056 | node = node->Next(); |
1057 | } | |
96fd301f | 1058 | |
83624f79 RR |
1059 | /* Not finding anything here can be correct |
1060 | * when search the entire menu system for | |
1061 | * an entry -> no error message. */ | |
8bbe427f | 1062 | |
83624f79 | 1063 | return (wxMenuItem *) NULL; |
96fd301f | 1064 | } |
c801d85f KB |
1065 | |
1066 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
1067 | { | |
83624f79 | 1068 | m_invokingWindow = win; |
6de97a3b | 1069 | } |
c801d85f | 1070 | |
96fd301f | 1071 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f | 1072 | { |
83624f79 | 1073 | return m_invokingWindow; |
6de97a3b | 1074 | } |
c801d85f | 1075 | |
c626a8b7 VZ |
1076 | // Update a menu and all submenus recursively. source is the object that has |
1077 | // the update event handlers defined for it. If NULL, the menu or associated | |
1078 | // window will be used. | |
631f1bfe JS |
1079 | void wxMenu::UpdateUI(wxEvtHandler* source) |
1080 | { | |
1081 | if (!source && GetInvokingWindow()) | |
1082 | source = GetInvokingWindow()->GetEventHandler(); | |
1083 | if (!source) | |
1084 | source = GetEventHandler(); | |
1085 | if (!source) | |
1086 | source = this; | |
1087 | ||
1088 | wxNode* node = GetItems().First(); | |
1089 | while (node) | |
1090 | { | |
1091 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
1092 | if ( !item->IsSeparator() ) | |
1093 | { | |
1094 | wxWindowID id = item->GetId(); | |
1095 | wxUpdateUIEvent event(id); | |
1096 | event.SetEventObject( source ); | |
1097 | ||
1098 | if (source->ProcessEvent(event)) | |
1099 | { | |
1100 | if (event.GetSetText()) | |
1101 | SetLabel(id, event.GetText()); | |
1102 | if (event.GetSetChecked()) | |
1103 | Check(id, event.GetChecked()); | |
1104 | if (event.GetSetEnabled()) | |
1105 | Enable(id, event.GetEnabled()); | |
1106 | } | |
1107 | ||
1108 | if (item->GetSubMenu()) | |
1109 | item->GetSubMenu()->UpdateUI(source); | |
1110 | } | |
1111 | node = node->Next(); | |
1112 | } | |
1113 | } | |
1114 |