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