]>
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 | ||
c801d85f KB |
23 | //----------------------------------------------------------------------------- |
24 | // wxMenuBar | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
28 | ||
3502e687 RR |
29 | wxMenuBar::wxMenuBar( long style ) |
30 | { | |
31 | m_needParent = FALSE; // hmmm | |
32 | ||
16bcc879 | 33 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, style, "menu" ); |
3502e687 RR |
34 | |
35 | m_menus.DeleteContents( TRUE ); | |
36 | ||
37 | m_menubar = gtk_menu_bar_new(); | |
38 | ||
39 | if (style & wxMB_DOCKABLE) | |
40 | { | |
41 | m_widget = gtk_handle_box_new(); | |
c626a8b7 VZ |
42 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
43 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
3502e687 RR |
44 | } |
45 | else | |
46 | { | |
47 | m_widget = GTK_WIDGET(m_menubar); | |
48 | } | |
49 | ||
50 | PostCreation(); | |
51 | ||
52 | Show( TRUE ); | |
53 | } | |
54 | ||
96fd301f | 55 | wxMenuBar::wxMenuBar() |
c801d85f | 56 | { |
83624f79 | 57 | m_needParent = FALSE; // hmmm |
96fd301f | 58 | |
83624f79 | 59 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
c801d85f | 60 | |
83624f79 | 61 | m_menus.DeleteContents( TRUE ); |
96fd301f | 62 | |
83624f79 | 63 | m_menubar = gtk_menu_bar_new(); |
8bbe427f | 64 | |
828f655f | 65 | m_widget = GTK_WIDGET(m_menubar); |
96fd301f | 66 | |
83624f79 | 67 | PostCreation(); |
c801d85f | 68 | |
83624f79 | 69 | Show( TRUE ); |
6de97a3b | 70 | } |
c801d85f KB |
71 | |
72 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
73 | { | |
83624f79 | 74 | m_menus.Append( menu ); |
034be888 RR |
75 | |
76 | wxString s = ""; | |
77 | for ( const char *pc = title; *pc != '\0'; pc++ ) | |
83624f79 | 78 | { |
034be888 RR |
79 | if (*pc == '&') |
80 | { | |
81 | pc++; /* skip it */ | |
82 | #if (GTK_MINOR_VERSION > 0) | |
83 | // s << '_'; not yet | |
84 | #endif | |
85 | } | |
86 | s << *pc; | |
87 | } | |
96fd301f | 88 | |
034be888 RR |
89 | menu->SetTitle(s); |
90 | menu->m_owner = gtk_menu_item_new_with_label( WXSTRINGCAST(s) ); | |
2b1c162e RR |
91 | gtk_widget_show( menu->m_owner ); |
92 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
96fd301f | 93 | |
2b1c162e | 94 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
6de97a3b | 95 | } |
96fd301f | 96 | |
716b7364 | 97 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 98 | { |
c626a8b7 | 99 | if (menu->GetTitle() == menuString) |
83624f79 RR |
100 | { |
101 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
102 | if (res != wxNOT_FOUND) |
103 | return res; | |
83624f79 | 104 | } |
c626a8b7 VZ |
105 | |
106 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast | |
83624f79 RR |
107 | while (node) |
108 | { | |
109 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
110 | if (item->IsSubMenu()) | |
111 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 112 | |
83624f79 RR |
113 | node = node->Next(); |
114 | } | |
115 | ||
c626a8b7 VZ |
116 | return wxNOT_FOUND; |
117 | } | |
118 | ||
119 | wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem = NULL) const | |
120 | { | |
121 | if ( menuForItem ) | |
122 | { | |
123 | // TODO return the pointer to the menu | |
124 | ||
125 | *menuForItem = NULL; | |
126 | } | |
127 | ||
128 | return FindItem(itemId); | |
6de97a3b | 129 | } |
c801d85f KB |
130 | |
131 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
132 | { | |
83624f79 RR |
133 | wxNode *node = m_menus.First(); |
134 | while (node) | |
135 | { | |
136 | wxMenu *menu = (wxMenu*)node->Data(); | |
137 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
138 | if (res != -1) return res; | |
139 | node = node->Next(); | |
140 | } | |
141 | return -1; | |
6de97a3b | 142 | } |
c801d85f | 143 | |
c626a8b7 | 144 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 145 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 146 | { |
83624f79 | 147 | wxMenuItem* result = menu->FindItem(id); |
716b7364 | 148 | |
c626a8b7 VZ |
149 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast |
150 | while ( node && result == NULL ) | |
83624f79 RR |
151 | { |
152 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
153 | if (item->IsSubMenu()) | |
c626a8b7 | 154 | { |
83624f79 | 155 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 156 | } |
83624f79 RR |
157 | node = node->Next(); |
158 | } | |
96fd301f | 159 | |
83624f79 | 160 | return result; |
6de97a3b | 161 | } |
716b7364 | 162 | |
c626a8b7 | 163 | wxMenuItem* wxMenuBar::FindItem( int id ) const |
716b7364 | 164 | { |
83624f79 RR |
165 | wxMenuItem* result = 0; |
166 | wxNode *node = m_menus.First(); | |
167 | while (node && result == 0) | |
168 | { | |
169 | wxMenu *menu = (wxMenu*)node->Data(); | |
170 | result = FindMenuItemByIdRecursive( menu, id ); | |
171 | node = node->Next(); | |
172 | } | |
c626a8b7 | 173 | |
83624f79 | 174 | return result; |
716b7364 RR |
175 | } |
176 | ||
54ff4a70 RR |
177 | void wxMenuBar::Check( int id, bool check ) |
178 | { | |
83624f79 | 179 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 VZ |
180 | |
181 | wxCHECK_RET( item, "wxMenuBar::Check: no such item" ); | |
182 | ||
183 | item->Check(check); | |
6de97a3b | 184 | } |
54ff4a70 | 185 | |
c626a8b7 | 186 | bool wxMenuBar::IsChecked( int id ) const |
716b7364 | 187 | { |
83624f79 | 188 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 VZ |
189 | |
190 | wxCHECK_MSG( item, FALSE, "wxMenuBar::IsChecked: no such item" ); | |
191 | ||
192 | return item->IsChecked(); | |
6de97a3b | 193 | } |
716b7364 | 194 | |
54ff4a70 RR |
195 | void wxMenuBar::Enable( int id, bool enable ) |
196 | { | |
83624f79 | 197 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 VZ |
198 | |
199 | wxCHECK_RET( item, "wxMenuBar::Enable: no such item" ); | |
200 | ||
201 | item->Enable(enable); | |
6de97a3b | 202 | } |
54ff4a70 | 203 | |
c626a8b7 | 204 | bool wxMenuBar::IsEnabled( int id ) const |
716b7364 | 205 | { |
83624f79 | 206 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 VZ |
207 | |
208 | wxCHECK_MSG( item, FALSE, "wxMenuBar::IsEnabled: no such item" ); | |
209 | ||
210 | return item->IsEnabled(); | |
6de97a3b | 211 | } |
716b7364 | 212 | |
bbe0af5b RR |
213 | wxString wxMenuBar::GetLabel( int id ) const |
214 | { | |
215 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 VZ |
216 | |
217 | wxCHECK_MSG( item, "", "wxMenuBar::GetLabel: no such item" ); | |
218 | ||
219 | return item->GetText(); | |
bbe0af5b RR |
220 | } |
221 | ||
222 | void wxMenuBar::SetLabel( int id, const wxString &label ) | |
223 | { | |
224 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 VZ |
225 | |
226 | wxCHECK_RET( item, "wxMenuBar::SetLabel: no such item" ); | |
227 | ||
228 | item->SetText( label ); | |
bbe0af5b RR |
229 | } |
230 | ||
2b1c162e | 231 | void wxMenuBar::EnableTop( int pos, bool flag ) |
bbe0af5b | 232 | { |
2b1c162e | 233 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 234 | |
2b1c162e | 235 | wxCHECK_RET( node, "menu not found" ); |
c626a8b7 | 236 | |
2b1c162e | 237 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 VZ |
238 | |
239 | if (menu->m_owner) | |
240 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
241 | } |
242 | ||
2b1c162e | 243 | wxString wxMenuBar::GetLabelTop( int pos ) const |
bbe0af5b | 244 | { |
2b1c162e | 245 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 246 | |
2b1c162e | 247 | wxCHECK_MSG( node, "invalid", "menu not found" ); |
c626a8b7 | 248 | |
2b1c162e | 249 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 250 | |
2b1c162e | 251 | return menu->GetTitle(); |
bbe0af5b RR |
252 | } |
253 | ||
2b1c162e | 254 | void wxMenuBar::SetLabelTop( int pos, const wxString& label ) |
bbe0af5b | 255 | { |
2b1c162e | 256 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 257 | |
2b1c162e | 258 | wxCHECK_RET( node, "menu not found" ); |
c626a8b7 | 259 | |
2b1c162e | 260 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 261 | |
2b1c162e | 262 | menu->SetTitle( label ); |
bbe0af5b RR |
263 | } |
264 | ||
342b6a2f RR |
265 | void wxMenuBar::SetHelpString( int id, const wxString& helpString ) |
266 | { | |
267 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 VZ |
268 | |
269 | wxCHECK_RET( item, "wxMenuBar::SetHelpString: no such item" ); | |
270 | ||
271 | item->SetHelp( helpString ); | |
342b6a2f RR |
272 | } |
273 | ||
274 | wxString wxMenuBar::GetHelpString( int id ) const | |
275 | { | |
276 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 VZ |
277 | |
278 | wxCHECK_MSG( item, "", "wxMenuBar::GetHelpString: no such item" ); | |
279 | ||
280 | return item->GetHelp(); | |
342b6a2f RR |
281 | } |
282 | ||
c801d85f | 283 | //----------------------------------------------------------------------------- |
cf7a7e13 | 284 | // "activate" |
c801d85f KB |
285 | //----------------------------------------------------------------------------- |
286 | ||
6de97a3b | 287 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 288 | { |
83624f79 | 289 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 290 | |
83624f79 | 291 | /* should find it for normal (not popup) menu */ |
c626a8b7 | 292 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); |
96fd301f | 293 | |
c626a8b7 VZ |
294 | if (!menu->IsEnabled(id)) |
295 | return; | |
96fd301f | 296 | |
2d17d68f RR |
297 | wxMenuItem* item = menu->FindItem( id ); |
298 | wxCHECK_RET( item, "error in menu item callback" ); | |
c626a8b7 VZ |
299 | |
300 | if (item->IsCheckable()) | |
2d17d68f | 301 | { |
c626a8b7 | 302 | if (item->GetCheckedFlag() == item->IsChecked()) |
2d17d68f | 303 | { |
c626a8b7 | 304 | /* the menu item has been checked by calling wxMenuItem->Check() */ |
2d17d68f | 305 | return; |
c626a8b7 VZ |
306 | } |
307 | else | |
308 | { | |
309 | /* the user pressed on the menu item -> report */ | |
310 | item->SetCheckedFlag(item->IsChecked()); /* make consistent again */ | |
311 | } | |
2d17d68f RR |
312 | } |
313 | ||
83624f79 RR |
314 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
315 | event.SetEventObject( menu ); | |
316 | event.SetInt(id ); | |
8bbe427f | 317 | |
c626a8b7 | 318 | if (menu->GetCallback()) |
83624f79 | 319 | { |
c626a8b7 | 320 | (void) (*(menu->GetCallback())) (*menu, event); |
83624f79 RR |
321 | return; |
322 | } | |
cf7a7e13 | 323 | |
c626a8b7 VZ |
324 | if (menu->GetEventHandler()->ProcessEvent(event)) |
325 | return; | |
cf7a7e13 | 326 | |
83624f79 | 327 | wxWindow *win = menu->GetInvokingWindow(); |
c626a8b7 VZ |
328 | if (win) |
329 | win->GetEventHandler()->ProcessEvent( event ); | |
cf7a7e13 RR |
330 | } |
331 | ||
332 | //----------------------------------------------------------------------------- | |
333 | // "select" | |
334 | //----------------------------------------------------------------------------- | |
335 | ||
336 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
337 | { | |
83624f79 RR |
338 | int id = menu->FindMenuIdByMenuItem(widget); |
339 | ||
340 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 341 | |
c626a8b7 VZ |
342 | if (!menu->IsEnabled(id)) |
343 | return; | |
cf7a7e13 | 344 | |
342b6a2f | 345 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 346 | event.SetEventObject( menu ); |
cf7a7e13 | 347 | |
83624f79 | 348 | /* wxMSW doesn't call callback here either |
8bbe427f | 349 | |
83624f79 RR |
350 | if (menu->m_callback) |
351 | { | |
352 | (void) (*(menu->m_callback)) (*menu, event); | |
353 | return; | |
354 | } | |
13439807 | 355 | */ |
6de97a3b | 356 | |
c626a8b7 VZ |
357 | if (menu->GetEventHandler()->ProcessEvent(event)) |
358 | return; | |
6de97a3b | 359 | |
83624f79 RR |
360 | wxWindow *win = menu->GetInvokingWindow(); |
361 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 362 | } |
c801d85f | 363 | |
cd743a6f RR |
364 | //----------------------------------------------------------------------------- |
365 | // "deselect" | |
366 | //----------------------------------------------------------------------------- | |
367 | ||
368 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
369 | { | |
370 | int id = menu->FindMenuIdByMenuItem(widget); | |
371 | ||
372 | wxASSERT( id != -1 ); // should find it! | |
373 | ||
c626a8b7 VZ |
374 | if (!menu->IsEnabled(id)) |
375 | return; | |
cd743a6f RR |
376 | |
377 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
378 | event.SetEventObject( menu ); | |
379 | ||
c626a8b7 VZ |
380 | if (menu->GetEventHandler()->ProcessEvent(event)) |
381 | return; | |
cd743a6f RR |
382 | |
383 | wxWindow *win = menu->GetInvokingWindow(); | |
c626a8b7 VZ |
384 | if (win) |
385 | win->GetEventHandler()->ProcessEvent( event ); | |
cd743a6f RR |
386 | } |
387 | ||
cf7a7e13 | 388 | //----------------------------------------------------------------------------- |
db1b4961 | 389 | // wxMenuItem |
cf7a7e13 RR |
390 | //----------------------------------------------------------------------------- |
391 | ||
c801d85f | 392 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) |
96fd301f VZ |
393 | |
394 | wxMenuItem::wxMenuItem() | |
c801d85f | 395 | { |
83624f79 RR |
396 | m_id = ID_SEPARATOR; |
397 | m_isCheckMenu = FALSE; | |
398 | m_isChecked = FALSE; | |
399 | m_isEnabled = TRUE; | |
400 | m_subMenu = (wxMenu *) NULL; | |
401 | m_menuItem = (GtkWidget *) NULL; | |
6de97a3b | 402 | } |
c801d85f | 403 | |
c626a8b7 | 404 | // it's valid for this function to be called even if m_menuItem == NULL |
83624f79 | 405 | void wxMenuItem::SetName( const wxString& str ) |
716b7364 | 406 | { |
83624f79 RR |
407 | m_text = ""; |
408 | for ( const char *pc = str; *pc != '\0'; pc++ ) | |
409 | { | |
034be888 RR |
410 | if (*pc == '&') |
411 | { | |
412 | pc++; /* skip it */ | |
413 | #if (GTK_MINOR_VERSION > 0) | |
414 | m_text << '_'; | |
415 | #endif | |
416 | } | |
83624f79 RR |
417 | m_text << *pc; |
418 | } | |
96fd301f | 419 | |
83624f79 RR |
420 | if (m_menuItem) |
421 | { | |
422 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
423 | gtk_label_set( label, m_text.c_str()); | |
424 | } | |
716b7364 RR |
425 | } |
426 | ||
96fd301f | 427 | void wxMenuItem::Check( bool check ) |
716b7364 | 428 | { |
83624f79 | 429 | wxCHECK_RET( m_menuItem, "invalid menu item" ); |
db1b4961 | 430 | |
83624f79 | 431 | wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" ) |
96fd301f | 432 | |
2d17d68f RR |
433 | if (check == m_isChecked) return; |
434 | ||
83624f79 RR |
435 | m_isChecked = check; |
436 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
437 | } |
438 | ||
8bbe427f VZ |
439 | void wxMenuItem::Enable( bool enable ) |
440 | { | |
83624f79 | 441 | wxCHECK_RET( m_menuItem, "invalid menu item" ); |
db1b4961 | 442 | |
83624f79 RR |
443 | gtk_widget_set_sensitive( m_menuItem, enable ); |
444 | m_isEnabled = enable; | |
a9c96bcc RR |
445 | } |
446 | ||
96fd301f | 447 | bool wxMenuItem::IsChecked() const |
716b7364 | 448 | { |
83624f79 | 449 | wxCHECK_MSG( m_menuItem, FALSE, "invalid menu item" ); |
db1b4961 | 450 | |
83624f79 | 451 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
96fd301f | 452 | |
83624f79 | 453 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
96fd301f | 454 | |
83624f79 | 455 | return bIsChecked; |
716b7364 RR |
456 | } |
457 | ||
db1b4961 | 458 | //----------------------------------------------------------------------------- |
83624f79 | 459 | // wxMenu |
db1b4961 RR |
460 | //----------------------------------------------------------------------------- |
461 | ||
c801d85f KB |
462 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
463 | ||
6de97a3b | 464 | wxMenu::wxMenu( const wxString& title, const wxFunction func ) |
c801d85f | 465 | { |
83624f79 RR |
466 | m_title = title; |
467 | m_items.DeleteContents( TRUE ); | |
468 | m_invokingWindow = (wxWindow *) NULL; | |
034be888 RR |
469 | |
470 | #if (GTK_MINOR_VERSION > 0) | |
471 | m_accel = gtk_accel_group_new(); | |
472 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); | |
473 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
474 | #else | |
83624f79 | 475 | m_menu = gtk_menu_new(); // Do not show! |
034be888 | 476 | #endif |
8bbe427f | 477 | |
83624f79 RR |
478 | m_callback = func; |
479 | m_eventHandler = this; | |
480 | m_clientData = (void*) NULL; | |
8bbe427f | 481 | |
83624f79 RR |
482 | if (m_title.IsNull()) m_title = ""; |
483 | if (m_title != "") | |
484 | { | |
485 | Append(-2, m_title); | |
486 | AppendSeparator(); | |
487 | } | |
c626a8b7 | 488 | |
2b1c162e | 489 | m_owner = (GtkWidget*) NULL; |
6de97a3b | 490 | } |
c801d85f | 491 | |
034be888 RR |
492 | wxMenu::~wxMenu() |
493 | { | |
494 | /* how do we delete an item-factory ? */ | |
495 | } | |
496 | ||
c2dd8380 GL |
497 | void wxMenu::SetTitle( const wxString& title ) |
498 | { | |
c626a8b7 | 499 | // TODO Waiting for something better |
83624f79 | 500 | m_title = title; |
c2dd8380 GL |
501 | } |
502 | ||
503 | const wxString wxMenu::GetTitle() const | |
504 | { | |
83624f79 | 505 | return m_title; |
c2dd8380 GL |
506 | } |
507 | ||
96fd301f | 508 | void wxMenu::AppendSeparator() |
c801d85f | 509 | { |
83624f79 RR |
510 | wxMenuItem *mitem = new wxMenuItem(); |
511 | mitem->SetId(ID_SEPARATOR); | |
96fd301f | 512 | |
83624f79 RR |
513 | GtkWidget *menuItem = gtk_menu_item_new(); |
514 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
515 | gtk_widget_show( menuItem ); | |
034be888 | 516 | |
83624f79 RR |
517 | mitem->SetMenuItem(menuItem); |
518 | m_items.Append( mitem ); | |
6de97a3b | 519 | } |
c801d85f | 520 | |
debe6624 | 521 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f | 522 | { |
83624f79 RR |
523 | wxMenuItem *mitem = new wxMenuItem(); |
524 | mitem->SetId(id); | |
525 | mitem->SetText(item); | |
526 | mitem->SetHelp(helpStr); | |
527 | mitem->SetCheckable(checkable); | |
528 | const char *text = mitem->GetText(); | |
034be888 RR |
529 | |
530 | #if (GTK_MINOR_VERSION > 0) | |
531 | char buf[100]; | |
532 | strcpy( buf, "/" ); | |
533 | strcat( buf, text ); | |
534 | ||
535 | GtkItemFactoryEntry entry; | |
536 | entry.path = buf; | |
537 | entry.accelerator = (gchar*) NULL; | |
538 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; | |
539 | entry.callback_action = 0; | |
540 | if (checkable) | |
541 | entry.item_type = "<CheckItem>"; | |
542 | else | |
543 | entry.item_type = "<Item>"; | |
544 | ||
545 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
546 | ||
547 | /* in order to get the pointer to the item we need the item text _without_ underscores */ | |
548 | wxString s = "<main>/"; | |
549 | for ( const char *pc = text; *pc != '\0'; pc++ ) | |
550 | { | |
551 | if (*pc == '_') pc++; /* skip it */ | |
552 | s << *pc; | |
553 | } | |
554 | ||
a260fe6a | 555 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s ); |
034be888 RR |
556 | |
557 | #else | |
558 | ||
83624f79 RR |
559 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) |
560 | : gtk_menu_item_new_with_label(text); | |
034be888 | 561 | |
83624f79 RR |
562 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
563 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
034be888 RR |
564 | (gpointer)this ); |
565 | ||
566 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
567 | gtk_widget_show( menuItem ); | |
568 | ||
569 | #endif | |
96fd301f | 570 | |
83624f79 RR |
571 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
572 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
034be888 | 573 | (gpointer)this ); |
cf7a7e13 | 574 | |
cd743a6f RR |
575 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
576 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
034be888 RR |
577 | (gpointer)this ); |
578 | ||
579 | mitem->SetMenuItem(menuItem); | |
cd743a6f | 580 | |
83624f79 | 581 | m_items.Append( mitem ); |
6de97a3b | 582 | } |
c801d85f | 583 | |
96fd301f | 584 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f | 585 | { |
83624f79 RR |
586 | wxMenuItem *mitem = new wxMenuItem(); |
587 | mitem->SetId(id); | |
588 | mitem->SetText(text); | |
828f655f | 589 | mitem->SetHelp(helpStr); |
96fd301f | 590 | |
83624f79 | 591 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); |
83624f79 RR |
592 | mitem->SetMenuItem(menuItem); |
593 | mitem->SetSubMenu(subMenu); | |
96fd301f | 594 | |
cd743a6f RR |
595 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
596 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
597 | (gpointer*)this ); | |
598 | ||
599 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
600 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
601 | (gpointer*)this ); | |
602 | ||
83624f79 RR |
603 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); |
604 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
605 | gtk_widget_show( menuItem ); | |
606 | m_items.Append( mitem ); | |
6de97a3b | 607 | } |
c801d85f | 608 | |
828f655f RR |
609 | void wxMenu::Append( wxMenuItem *item ) |
610 | { | |
611 | m_items.Append( item ); | |
c626a8b7 | 612 | |
828f655f RR |
613 | GtkWidget *menuItem = (GtkWidget*) NULL; |
614 | ||
c626a8b7 | 615 | if (item->IsSeparator()) |
828f655f | 616 | menuItem = gtk_menu_item_new(); |
c626a8b7 | 617 | else if (item->IsSubMenu()) |
828f655f | 618 | menuItem = gtk_menu_item_new_with_label(item->GetText()); |
c626a8b7 | 619 | else |
828f655f RR |
620 | menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText()) |
621 | : gtk_menu_item_new_with_label(item->GetText()); | |
622 | ||
623 | if (!item->IsSeparator()) | |
624 | { | |
625 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
626 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
627 | (gpointer*)this ); | |
628 | ||
629 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
630 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
631 | (gpointer*)this ); | |
c626a8b7 VZ |
632 | |
633 | if (!item->IsSubMenu()) | |
634 | { | |
828f655f RR |
635 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
636 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
637 | (gpointer*)this ); | |
c626a8b7 | 638 | } |
828f655f | 639 | } |
c626a8b7 | 640 | |
828f655f RR |
641 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
642 | gtk_widget_show( menuItem ); | |
643 | item->SetMenuItem(menuItem); | |
644 | } | |
645 | ||
c801d85f KB |
646 | int wxMenu::FindItem( const wxString itemString ) const |
647 | { | |
034be888 RR |
648 | wxString s = ""; |
649 | for ( const char *pc = itemString; *pc != '\0'; pc++ ) | |
83624f79 | 650 | { |
034be888 RR |
651 | if (*pc == '&') |
652 | { | |
653 | pc++; /* skip it */ | |
654 | #if (GTK_MINOR_VERSION > 0) | |
655 | s << '_'; | |
656 | #endif | |
657 | } | |
658 | s << *pc; | |
659 | } | |
96fd301f | 660 | |
83624f79 RR |
661 | wxNode *node = m_items.First(); |
662 | while (node) | |
663 | { | |
664 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
665 | if (item->GetText() == s) | |
c626a8b7 | 666 | { |
83624f79 | 667 | return item->GetId(); |
c626a8b7 | 668 | } |
83624f79 RR |
669 | node = node->Next(); |
670 | } | |
96fd301f | 671 | |
c626a8b7 | 672 | return wxNOT_FOUND; |
6de97a3b | 673 | } |
c801d85f | 674 | |
96fd301f | 675 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 676 | { |
83624f79 | 677 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
678 | |
679 | wxCHECK_RET( item, "wxMenu::Enable: no such item" ); | |
680 | ||
681 | item->Enable(enable); | |
6de97a3b | 682 | } |
716b7364 | 683 | |
96fd301f | 684 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 685 | { |
83624f79 | 686 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
687 | |
688 | wxCHECK_MSG( item, FALSE, "wxMenu::IsEnabled: no such item" ); | |
689 | ||
690 | return item->IsEnabled(); | |
6de97a3b | 691 | } |
e2414cbe | 692 | |
96fd301f | 693 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 694 | { |
83624f79 | 695 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
696 | |
697 | wxCHECK_RET( item, "wxMenu::Check: no such item" ); | |
698 | ||
699 | item->Check(enable); | |
6de97a3b | 700 | } |
c801d85f | 701 | |
96fd301f | 702 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 703 | { |
83624f79 | 704 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
705 | |
706 | wxCHECK_MSG( item, FALSE, "wxMenu::IsChecked: no such item" ); | |
707 | ||
708 | return item->IsChecked(); | |
6de97a3b | 709 | } |
c801d85f | 710 | |
debe6624 | 711 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 712 | { |
83624f79 | 713 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
714 | |
715 | wxCHECK_RET( item, "wxMenu::SetLabel: no such item" ); | |
716 | ||
717 | item->SetText(label); | |
6de97a3b | 718 | } |
96fd301f | 719 | |
c33c4050 RR |
720 | wxString wxMenu::GetLabel( int id ) const |
721 | { | |
83624f79 | 722 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
723 | |
724 | wxCHECK_MSG( item, "", "wxMenu::GetLabel: no such item" ); | |
725 | ||
726 | return item->GetText(); | |
c33c4050 RR |
727 | } |
728 | ||
729 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
730 | { | |
83624f79 | 731 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
732 | |
733 | wxCHECK_RET( item, "wxMenu::SetHelpString: no such item" ); | |
734 | ||
735 | item->SetHelp( helpString ); | |
c33c4050 RR |
736 | } |
737 | ||
738 | wxString wxMenu::GetHelpString( int id ) const | |
739 | { | |
83624f79 | 740 | wxMenuItem *item = FindItem(id); |
c626a8b7 VZ |
741 | |
742 | wxCHECK_MSG( item, "", "wxMenu::GetHelpString: no such item" ); | |
743 | ||
744 | return item->GetHelp(); | |
c33c4050 RR |
745 | } |
746 | ||
96fd301f VZ |
747 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
748 | { | |
83624f79 RR |
749 | wxNode *node = m_items.First(); |
750 | while (node) | |
751 | { | |
752 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
753 | if (item->GetMenuItem() == menuItem) | |
754 | return item->GetId(); | |
755 | node = node->Next(); | |
756 | } | |
96fd301f | 757 | |
c626a8b7 | 758 | return wxNOT_FOUND; |
6de97a3b | 759 | } |
c801d85f | 760 | |
96fd301f | 761 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f | 762 | { |
83624f79 | 763 | wxNode *node = m_items.First(); |
c626a8b7 | 764 | while (node) |
83624f79 RR |
765 | { |
766 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
767 | if (item->GetId() == id) | |
c626a8b7 | 768 | { |
83624f79 | 769 | return item; |
c626a8b7 | 770 | } |
83624f79 RR |
771 | node = node->Next(); |
772 | } | |
96fd301f | 773 | |
83624f79 RR |
774 | /* Not finding anything here can be correct |
775 | * when search the entire menu system for | |
776 | * an entry -> no error message. */ | |
8bbe427f | 777 | |
83624f79 | 778 | return (wxMenuItem *) NULL; |
96fd301f | 779 | } |
c801d85f KB |
780 | |
781 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
782 | { | |
83624f79 | 783 | m_invokingWindow = win; |
6de97a3b | 784 | } |
c801d85f | 785 | |
96fd301f | 786 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f | 787 | { |
83624f79 | 788 | return m_invokingWindow; |
6de97a3b | 789 | } |
c801d85f | 790 | |
c626a8b7 VZ |
791 | // Update a menu and all submenus recursively. source is the object that has |
792 | // the update event handlers defined for it. If NULL, the menu or associated | |
793 | // window will be used. | |
631f1bfe JS |
794 | void wxMenu::UpdateUI(wxEvtHandler* source) |
795 | { | |
796 | if (!source && GetInvokingWindow()) | |
797 | source = GetInvokingWindow()->GetEventHandler(); | |
798 | if (!source) | |
799 | source = GetEventHandler(); | |
800 | if (!source) | |
801 | source = this; | |
802 | ||
803 | wxNode* node = GetItems().First(); | |
804 | while (node) | |
805 | { | |
806 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
807 | if ( !item->IsSeparator() ) | |
808 | { | |
809 | wxWindowID id = item->GetId(); | |
810 | wxUpdateUIEvent event(id); | |
811 | event.SetEventObject( source ); | |
812 | ||
813 | if (source->ProcessEvent(event)) | |
814 | { | |
815 | if (event.GetSetText()) | |
816 | SetLabel(id, event.GetText()); | |
817 | if (event.GetSetChecked()) | |
818 | Check(id, event.GetChecked()); | |
819 | if (event.GetSetEnabled()) | |
820 | Enable(id, event.GetEnabled()); | |
821 | } | |
822 | ||
823 | if (item->GetSubMenu()) | |
824 | item->GetSubMenu()->UpdateUI(source); | |
825 | } | |
826 | node = node->Next(); | |
827 | } | |
828 | } | |
829 |