]>
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 | 75 | |
b019151f OK |
76 | wxString s = _T(""); |
77 | for ( const wxChar *pc = title; *pc != _T('\0'); pc++ ) | |
83624f79 | 78 | { |
b019151f | 79 | if (*pc == _T('&')) |
034be888 RR |
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 | 89 | menu->SetTitle(s); |
b019151f | 90 | menu->m_owner = gtk_menu_item_new_with_label( MBSTRINGCAST s.mbc_str() ); |
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 | 180 | |
b019151f | 181 | wxCHECK_RET( item, _T("wxMenuBar::Check: no such item") ); |
c626a8b7 VZ |
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 | 189 | |
b019151f | 190 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked: no such item") ); |
c626a8b7 VZ |
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 | 198 | |
b019151f | 199 | wxCHECK_RET( item, _T("wxMenuBar::Enable: no such item") ); |
c626a8b7 VZ |
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 | 207 | |
b019151f | 208 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled: no such item") ); |
c626a8b7 VZ |
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 | 216 | |
b019151f | 217 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel: no such item") ); |
c626a8b7 VZ |
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 | 225 | |
b019151f | 226 | wxCHECK_RET( item, _T("wxMenuBar::SetLabel: no such item") ); |
c626a8b7 VZ |
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 | |
b019151f | 235 | wxCHECK_RET( node, _T("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 | |
b019151f | 247 | wxCHECK_MSG( node, _T("invalid"), _T("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 | |
b019151f | 258 | wxCHECK_RET( node, _T("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 | 268 | |
b019151f | 269 | wxCHECK_RET( item, _T("wxMenuBar::SetHelpString: no such item") ); |
c626a8b7 VZ |
270 | |
271 | item->SetHelp( helpString ); | |
342b6a2f RR |
272 | } |
273 | ||
274 | wxString wxMenuBar::GetHelpString( int id ) const | |
275 | { | |
276 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 277 | |
b019151f | 278 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString: no such item") ); |
c626a8b7 VZ |
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 | 297 | wxMenuItem* item = menu->FindItem( id ); |
b019151f | 298 | wxCHECK_RET( item, _T("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 | { |
b019151f OK |
407 | m_text = _T(""); |
408 | for ( const wxChar *pc = str; *pc != _T('\0'); pc++ ) | |
83624f79 | 409 | { |
b019151f | 410 | if (*pc == _T('&')) |
034be888 RR |
411 | { |
412 | pc++; /* skip it */ | |
413 | #if (GTK_MINOR_VERSION > 0) | |
b019151f | 414 | m_text << _T('_'); |
034be888 RR |
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 ); | |
b019151f | 423 | gtk_label_set( label, m_text.mbc_str()); |
83624f79 | 424 | } |
716b7364 RR |
425 | } |
426 | ||
96fd301f | 427 | void wxMenuItem::Check( bool check ) |
716b7364 | 428 | { |
b019151f | 429 | wxCHECK_RET( m_menuItem, _T("invalid menu item") ); |
db1b4961 | 430 | |
b019151f | 431 | wxCHECK_RET( IsCheckable(), _T("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 | { | |
b019151f | 441 | wxCHECK_RET( m_menuItem, _T("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 | { |
b019151f | 449 | wxCHECK_MSG( m_menuItem, FALSE, _T("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 | |
b019151f OK |
482 | if (m_title.IsNull()) m_title = _T(""); |
483 | if (m_title != _T("")) | |
83624f79 RR |
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); | |
b019151f | 528 | const wxChar *text = mitem->GetText(); |
034be888 RR |
529 | |
530 | #if (GTK_MINOR_VERSION > 0) | |
b019151f OK |
531 | wxChar buf[100]; |
532 | wxStrcpy( buf, _T("/") ); | |
533 | wxStrcat( buf, text ); | |
034be888 | 534 | |
e06937ad | 535 | const wxWX2MBbuf pbuf = wxConv_current->cWX2MB(buf); |
034be888 | 536 | GtkItemFactoryEntry entry; |
b019151f | 537 | entry.path = MBSTRINGCAST pbuf; |
034be888 RR |
538 | entry.accelerator = (gchar*) NULL; |
539 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; | |
540 | entry.callback_action = 0; | |
541 | if (checkable) | |
542 | entry.item_type = "<CheckItem>"; | |
543 | else | |
544 | entry.item_type = "<Item>"; | |
545 | ||
546 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
547 | ||
548 | /* in order to get the pointer to the item we need the item text _without_ underscores */ | |
b019151f OK |
549 | wxString s = _T("<main>/"); |
550 | for ( const wxChar *pc = text; *pc != _T('\0'); pc++ ) | |
034be888 | 551 | { |
b019151f | 552 | if (*pc == _T('_')) pc++; /* skip it */ |
034be888 RR |
553 | s << *pc; |
554 | } | |
555 | ||
b019151f | 556 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mbc_str() ); |
034be888 RR |
557 | |
558 | #else | |
559 | ||
83624f79 RR |
560 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) |
561 | : gtk_menu_item_new_with_label(text); | |
034be888 | 562 | |
83624f79 RR |
563 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
564 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
034be888 RR |
565 | (gpointer)this ); |
566 | ||
567 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
568 | gtk_widget_show( menuItem ); | |
569 | ||
570 | #endif | |
96fd301f | 571 | |
83624f79 RR |
572 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
573 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
034be888 | 574 | (gpointer)this ); |
cf7a7e13 | 575 | |
cd743a6f RR |
576 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
577 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
034be888 RR |
578 | (gpointer)this ); |
579 | ||
580 | mitem->SetMenuItem(menuItem); | |
cd743a6f | 581 | |
83624f79 | 582 | m_items.Append( mitem ); |
6de97a3b | 583 | } |
c801d85f | 584 | |
96fd301f | 585 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f | 586 | { |
83624f79 RR |
587 | wxMenuItem *mitem = new wxMenuItem(); |
588 | mitem->SetId(id); | |
589 | mitem->SetText(text); | |
828f655f | 590 | mitem->SetHelp(helpStr); |
96fd301f | 591 | |
b019151f | 592 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str()); |
83624f79 RR |
593 | mitem->SetMenuItem(menuItem); |
594 | mitem->SetSubMenu(subMenu); | |
96fd301f | 595 | |
cd743a6f RR |
596 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
597 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
598 | (gpointer*)this ); | |
599 | ||
600 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
601 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
602 | (gpointer*)this ); | |
603 | ||
83624f79 RR |
604 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); |
605 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
606 | gtk_widget_show( menuItem ); | |
607 | m_items.Append( mitem ); | |
6de97a3b | 608 | } |
c801d85f | 609 | |
828f655f RR |
610 | void wxMenu::Append( wxMenuItem *item ) |
611 | { | |
612 | m_items.Append( item ); | |
c626a8b7 | 613 | |
828f655f RR |
614 | GtkWidget *menuItem = (GtkWidget*) NULL; |
615 | ||
c626a8b7 | 616 | if (item->IsSeparator()) |
828f655f | 617 | menuItem = gtk_menu_item_new(); |
c626a8b7 | 618 | else if (item->IsSubMenu()) |
b019151f | 619 | menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str()); |
c626a8b7 | 620 | else |
b019151f OK |
621 | menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str()) |
622 | : gtk_menu_item_new_with_label(item->GetText().mbc_str()); | |
828f655f RR |
623 | |
624 | if (!item->IsSeparator()) | |
625 | { | |
626 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
627 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
628 | (gpointer*)this ); | |
629 | ||
630 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
631 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
632 | (gpointer*)this ); | |
c626a8b7 VZ |
633 | |
634 | if (!item->IsSubMenu()) | |
635 | { | |
828f655f RR |
636 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
637 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
638 | (gpointer*)this ); | |
c626a8b7 | 639 | } |
828f655f | 640 | } |
c626a8b7 | 641 | |
828f655f RR |
642 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
643 | gtk_widget_show( menuItem ); | |
644 | item->SetMenuItem(menuItem); | |
645 | } | |
646 | ||
c801d85f KB |
647 | int wxMenu::FindItem( const wxString itemString ) const |
648 | { | |
b019151f OK |
649 | wxString s = _T(""); |
650 | for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ ) | |
83624f79 | 651 | { |
b019151f | 652 | if (*pc == _T('&')) |
034be888 RR |
653 | { |
654 | pc++; /* skip it */ | |
655 | #if (GTK_MINOR_VERSION > 0) | |
b019151f | 656 | s << _T('_'); |
034be888 RR |
657 | #endif |
658 | } | |
659 | s << *pc; | |
660 | } | |
96fd301f | 661 | |
83624f79 RR |
662 | wxNode *node = m_items.First(); |
663 | while (node) | |
664 | { | |
665 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
666 | if (item->GetText() == s) | |
c626a8b7 | 667 | { |
83624f79 | 668 | return item->GetId(); |
c626a8b7 | 669 | } |
83624f79 RR |
670 | node = node->Next(); |
671 | } | |
96fd301f | 672 | |
c626a8b7 | 673 | return wxNOT_FOUND; |
6de97a3b | 674 | } |
c801d85f | 675 | |
96fd301f | 676 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 677 | { |
83624f79 | 678 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 679 | |
b019151f | 680 | wxCHECK_RET( item, _T("wxMenu::Enable: no such item") ); |
c626a8b7 VZ |
681 | |
682 | item->Enable(enable); | |
6de97a3b | 683 | } |
716b7364 | 684 | |
96fd301f | 685 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 686 | { |
83624f79 | 687 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 688 | |
b019151f | 689 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") ); |
c626a8b7 VZ |
690 | |
691 | return item->IsEnabled(); | |
6de97a3b | 692 | } |
e2414cbe | 693 | |
96fd301f | 694 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 695 | { |
83624f79 | 696 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 697 | |
b019151f | 698 | wxCHECK_RET( item, _T("wxMenu::Check: no such item") ); |
c626a8b7 VZ |
699 | |
700 | item->Check(enable); | |
6de97a3b | 701 | } |
c801d85f | 702 | |
96fd301f | 703 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 704 | { |
83624f79 | 705 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 706 | |
b019151f | 707 | wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") ); |
c626a8b7 VZ |
708 | |
709 | return item->IsChecked(); | |
6de97a3b | 710 | } |
c801d85f | 711 | |
debe6624 | 712 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 713 | { |
83624f79 | 714 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 715 | |
b019151f | 716 | wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") ); |
c626a8b7 VZ |
717 | |
718 | item->SetText(label); | |
6de97a3b | 719 | } |
96fd301f | 720 | |
c33c4050 RR |
721 | wxString wxMenu::GetLabel( int id ) const |
722 | { | |
83624f79 | 723 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 724 | |
b019151f | 725 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") ); |
c626a8b7 VZ |
726 | |
727 | return item->GetText(); | |
c33c4050 RR |
728 | } |
729 | ||
730 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
731 | { | |
83624f79 | 732 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 733 | |
b019151f | 734 | wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") ); |
c626a8b7 VZ |
735 | |
736 | item->SetHelp( helpString ); | |
c33c4050 RR |
737 | } |
738 | ||
739 | wxString wxMenu::GetHelpString( int id ) const | |
740 | { | |
83624f79 | 741 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 742 | |
b019151f | 743 | wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") ); |
c626a8b7 VZ |
744 | |
745 | return item->GetHelp(); | |
c33c4050 RR |
746 | } |
747 | ||
96fd301f VZ |
748 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
749 | { | |
83624f79 RR |
750 | wxNode *node = m_items.First(); |
751 | while (node) | |
752 | { | |
753 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
754 | if (item->GetMenuItem() == menuItem) | |
755 | return item->GetId(); | |
756 | node = node->Next(); | |
757 | } | |
96fd301f | 758 | |
c626a8b7 | 759 | return wxNOT_FOUND; |
6de97a3b | 760 | } |
c801d85f | 761 | |
96fd301f | 762 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f | 763 | { |
83624f79 | 764 | wxNode *node = m_items.First(); |
c626a8b7 | 765 | while (node) |
83624f79 RR |
766 | { |
767 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
768 | if (item->GetId() == id) | |
c626a8b7 | 769 | { |
83624f79 | 770 | return item; |
c626a8b7 | 771 | } |
83624f79 RR |
772 | node = node->Next(); |
773 | } | |
96fd301f | 774 | |
83624f79 RR |
775 | /* Not finding anything here can be correct |
776 | * when search the entire menu system for | |
777 | * an entry -> no error message. */ | |
8bbe427f | 778 | |
83624f79 | 779 | return (wxMenuItem *) NULL; |
96fd301f | 780 | } |
c801d85f KB |
781 | |
782 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
783 | { | |
83624f79 | 784 | m_invokingWindow = win; |
6de97a3b | 785 | } |
c801d85f | 786 | |
96fd301f | 787 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f | 788 | { |
83624f79 | 789 | return m_invokingWindow; |
6de97a3b | 790 | } |
c801d85f | 791 | |
c626a8b7 VZ |
792 | // Update a menu and all submenus recursively. source is the object that has |
793 | // the update event handlers defined for it. If NULL, the menu or associated | |
794 | // window will be used. | |
631f1bfe JS |
795 | void wxMenu::UpdateUI(wxEvtHandler* source) |
796 | { | |
797 | if (!source && GetInvokingWindow()) | |
798 | source = GetInvokingWindow()->GetEventHandler(); | |
799 | if (!source) | |
800 | source = GetEventHandler(); | |
801 | if (!source) | |
802 | source = this; | |
803 | ||
804 | wxNode* node = GetItems().First(); | |
805 | while (node) | |
806 | { | |
807 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
808 | if ( !item->IsSeparator() ) | |
809 | { | |
810 | wxWindowID id = item->GetId(); | |
811 | wxUpdateUIEvent event(id); | |
812 | event.SetEventObject( source ); | |
813 | ||
814 | if (source->ProcessEvent(event)) | |
815 | { | |
816 | if (event.GetSetText()) | |
817 | SetLabel(id, event.GetText()); | |
818 | if (event.GetSetChecked()) | |
819 | Check(id, event.GetChecked()); | |
820 | if (event.GetSetEnabled()) | |
821 | Enable(id, event.GetEnabled()); | |
822 | } | |
823 | ||
824 | if (item->GetSubMenu()) | |
825 | item->GetSubMenu()->UpdateUI(source); | |
826 | } | |
827 | node = node->Next(); | |
828 | } | |
829 | } | |
830 |