]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
96fd301f | 6 | // Id: $Id$ |
c801d85f | 7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
96fd301f | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "menu.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/menu.h" | |
96fd301f | 17 | #include "wx/log.h" |
30dea054 | 18 | #include "wx/intl.h" |
c801d85f KB |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // wxMenuBar | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
25 | ||
96fd301f | 26 | wxMenuBar::wxMenuBar() |
c801d85f KB |
27 | { |
28 | m_needParent = FALSE; // hmmm | |
96fd301f | 29 | |
c67daf87 | 30 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); |
c801d85f KB |
31 | |
32 | m_menus.DeleteContents( TRUE ); | |
96fd301f | 33 | |
c801d85f | 34 | m_menubar = gtk_menu_bar_new(); |
32e9da8b RR |
35 | |
36 | m_widget = GTK_WIDGET(m_menubar); | |
96fd301f | 37 | |
c801d85f KB |
38 | PostCreation(); |
39 | ||
40 | Show( TRUE ); | |
6de97a3b | 41 | } |
c801d85f KB |
42 | |
43 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
44 | { | |
45 | m_menus.Append( menu ); | |
46 | menu->m_title = title; // ?????? | |
96fd301f | 47 | |
d355d3fe | 48 | int pos; |
c801d85f KB |
49 | do { |
50 | pos = menu->m_title.First( '&' ); | |
d355d3fe RR |
51 | if (pos != -1) menu->m_title.Remove( pos, 1 ); |
52 | } while (pos != -1); | |
96fd301f | 53 | |
c801d85f KB |
54 | GtkWidget *root_menu; |
55 | root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) ); | |
56 | gtk_widget_show( root_menu ); | |
57 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu ); | |
96fd301f | 58 | |
c801d85f | 59 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu ); |
6de97a3b | 60 | } |
96fd301f | 61 | |
716b7364 | 62 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f KB |
63 | { |
64 | if (menu->m_title == menuString) | |
65 | { | |
66 | int res = menu->FindItem( itemString ); | |
67 | if (res != -1) return res; | |
6de97a3b | 68 | } |
c801d85f KB |
69 | wxNode *node = menu->m_items.First(); |
70 | while (node) | |
71 | { | |
72 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
73 | if (item->IsSubMenu()) |
74 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
c801d85f | 75 | node = node->Next(); |
6de97a3b | 76 | } |
c801d85f | 77 | return -1; |
6de97a3b | 78 | } |
c801d85f KB |
79 | |
80 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
81 | { | |
82 | wxNode *node = m_menus.First(); | |
83 | while (node) | |
84 | { | |
85 | wxMenu *menu = (wxMenu*)node->Data(); | |
86 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
87 | if (res != -1) return res; | |
88 | node = node->Next(); | |
6de97a3b | 89 | } |
c801d85f | 90 | return -1; |
6de97a3b | 91 | } |
c801d85f | 92 | |
716b7364 | 93 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 94 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 95 | { |
96fd301f | 96 | wxMenuItem* result = menu->FindItem(id); |
716b7364 RR |
97 | |
98 | wxNode *node = menu->m_items.First(); | |
96fd301f | 99 | while ( node && result == NULL ) { |
716b7364 | 100 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
101 | if ( item->IsSubMenu() ) |
102 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
716b7364 | 103 | node = node->Next(); |
6de97a3b | 104 | } |
96fd301f | 105 | |
716b7364 | 106 | return result; |
6de97a3b | 107 | } |
716b7364 RR |
108 | |
109 | wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const | |
110 | { | |
111 | wxMenuItem* result = 0; | |
112 | wxNode *node = m_menus.First(); | |
113 | while (node && result == 0) | |
114 | { | |
115 | wxMenu *menu = (wxMenu*)node->Data(); | |
116 | result = FindMenuItemByIdRecursive( menu, id ); | |
117 | node = node->Next(); | |
118 | } | |
119 | return result; | |
120 | } | |
121 | ||
54ff4a70 RR |
122 | void wxMenuBar::Check( int id, bool check ) |
123 | { | |
124 | wxMenuItem* item = FindMenuItemById( id ); | |
125 | if (item) item->Check(check); | |
6de97a3b | 126 | } |
54ff4a70 RR |
127 | |
128 | bool wxMenuBar::Checked( int id ) const | |
716b7364 RR |
129 | { |
130 | wxMenuItem* item = FindMenuItemById( id ); | |
131 | if (item) return item->IsChecked(); | |
132 | return FALSE; | |
6de97a3b | 133 | } |
716b7364 | 134 | |
54ff4a70 RR |
135 | void wxMenuBar::Enable( int id, bool enable ) |
136 | { | |
137 | wxMenuItem* item = FindMenuItemById( id ); | |
138 | if (item) item->Enable(enable); | |
6de97a3b | 139 | } |
54ff4a70 RR |
140 | |
141 | bool wxMenuBar::Enabled( int id ) const | |
716b7364 RR |
142 | { |
143 | wxMenuItem* item = FindMenuItemById( id ); | |
144 | if (item) return item->IsEnabled(); | |
145 | return FALSE; | |
6de97a3b | 146 | } |
716b7364 | 147 | |
c801d85f | 148 | //----------------------------------------------------------------------------- |
cf7a7e13 | 149 | // "activate" |
c801d85f KB |
150 | //----------------------------------------------------------------------------- |
151 | ||
6de97a3b | 152 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 153 | { |
c801d85f | 154 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f VZ |
155 | |
156 | wxASSERT( id != -1 ); // should find it! | |
157 | ||
6de97a3b | 158 | if (!menu->IsEnabled(id)) return; |
96fd301f | 159 | |
cf7a7e13 RR |
160 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
161 | event.SetEventObject( menu ); | |
162 | event.SetInt(id ); | |
163 | ||
164 | if (menu->m_callback) | |
165 | { | |
166 | (void) (*(menu->m_callback)) (*menu, event); | |
167 | return; | |
168 | } | |
169 | ||
170 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
171 | ||
172 | wxWindow *win = menu->GetInvokingWindow(); | |
173 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
174 | } | |
175 | ||
176 | //----------------------------------------------------------------------------- | |
177 | // "select" | |
178 | //----------------------------------------------------------------------------- | |
179 | ||
180 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
181 | { | |
182 | int id = menu->FindMenuIdByMenuItem(widget); | |
183 | ||
184 | wxASSERT( id != -1 ); // should find it! | |
185 | ||
186 | if (!menu->IsEnabled(id)) return; | |
187 | ||
188 | wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id ); | |
c801d85f KB |
189 | event.SetEventObject( menu ); |
190 | event.SetInt(id ); | |
6de97a3b | 191 | |
13439807 | 192 | /* wxMSW doesn't call callback here either |
6de97a3b RR |
193 | if (menu->m_callback) |
194 | { | |
195 | (void) (*(menu->m_callback)) (*menu, event); | |
196 | return; | |
197 | } | |
13439807 | 198 | */ |
6de97a3b RR |
199 | |
200 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
201 | ||
c801d85f | 202 | wxWindow *win = menu->GetInvokingWindow(); |
cf4219e7 | 203 | if (win) win->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 204 | } |
c801d85f | 205 | |
cf7a7e13 RR |
206 | //----------------------------------------------------------------------------- |
207 | // wxMenu | |
208 | //----------------------------------------------------------------------------- | |
209 | ||
c801d85f | 210 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) |
96fd301f VZ |
211 | |
212 | wxMenuItem::wxMenuItem() | |
c801d85f | 213 | { |
96fd301f | 214 | m_id = ID_SEPARATOR; |
c801d85f | 215 | m_isCheckMenu = FALSE; |
96fd301f VZ |
216 | m_isChecked = FALSE; |
217 | m_isEnabled = TRUE; | |
c67daf87 UR |
218 | m_subMenu = (wxMenu *) NULL; |
219 | m_menuItem = (GtkWidget *) NULL; | |
6de97a3b | 220 | } |
c801d85f | 221 | |
96fd301f | 222 | void wxMenuItem::SetText(const wxString& str) |
716b7364 | 223 | { |
30dea054 | 224 | m_text = ""; |
96fd301f VZ |
225 | for ( const char *pc = str; *pc != '\0'; pc++ ) { |
226 | if ( *pc == '&' ) | |
227 | pc++; // skip it | |
228 | ||
229 | m_text << *pc; | |
716b7364 RR |
230 | } |
231 | } | |
232 | ||
96fd301f | 233 | void wxMenuItem::Check( bool check ) |
716b7364 | 234 | { |
30f82ea4 | 235 | wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" ) |
96fd301f VZ |
236 | |
237 | m_isChecked = check; | |
238 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
716b7364 RR |
239 | } |
240 | ||
a9c96bcc RR |
241 | void wxMenuItem::Enable( bool enable ) |
242 | { | |
243 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
244 | m_isEnabled = enable; | |
245 | } | |
246 | ||
96fd301f | 247 | bool wxMenuItem::IsChecked() const |
716b7364 | 248 | { |
96fd301f VZ |
249 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! |
250 | ||
251 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
252 | ||
253 | wxASSERT( bIsChecked == m_isChecked ); // consistency check | |
254 | ||
255 | return bIsChecked; | |
716b7364 RR |
256 | } |
257 | ||
c801d85f KB |
258 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
259 | ||
6de97a3b | 260 | wxMenu::wxMenu( const wxString& title, const wxFunction func ) |
c801d85f KB |
261 | { |
262 | m_title = title; | |
263 | m_items.DeleteContents( TRUE ); | |
c67daf87 | 264 | m_invokingWindow = (wxWindow *) NULL; |
c801d85f | 265 | m_menu = gtk_menu_new(); // Do not show! |
6de97a3b RR |
266 | m_callback = func; |
267 | m_eventHandler = this; | |
268 | if (m_title.IsNull()) m_title = ""; | |
269 | if (m_title != "") | |
270 | { | |
271 | Append(-2, m_title); | |
272 | AppendSeparator(); | |
273 | } | |
274 | } | |
c801d85f | 275 | |
c2dd8380 GL |
276 | void wxMenu::SetTitle( const wxString& title ) |
277 | { | |
278 | // Waiting for something better. | |
279 | m_title = title; | |
280 | } | |
281 | ||
282 | const wxString wxMenu::GetTitle() const | |
283 | { | |
284 | return m_title; | |
285 | } | |
286 | ||
96fd301f | 287 | void wxMenu::AppendSeparator() |
c801d85f KB |
288 | { |
289 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
290 | mitem->SetId(ID_SEPARATOR); |
291 | ||
292 | GtkWidget *menuItem = gtk_menu_item_new(); | |
293 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
294 | gtk_widget_show( menuItem ); | |
295 | mitem->SetMenuItem(menuItem); | |
c801d85f | 296 | m_items.Append( mitem ); |
6de97a3b | 297 | } |
c801d85f | 298 | |
debe6624 | 299 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f KB |
300 | { |
301 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
302 | mitem->SetId(id); |
303 | mitem->SetText(item); | |
c33c4050 | 304 | mitem->SetHelp(helpStr); |
96fd301f VZ |
305 | mitem->SetCheckable(checkable); |
306 | const char *text = mitem->GetText(); | |
307 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) | |
308 | : gtk_menu_item_new_with_label(text); | |
a9c96bcc | 309 | |
96fd301f VZ |
310 | mitem->SetMenuItem(menuItem); |
311 | ||
312 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
313 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
314 | (gpointer*)this ); | |
315 | ||
cf7a7e13 RR |
316 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
317 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
318 | (gpointer*)this ); | |
319 | ||
96fd301f VZ |
320 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
321 | gtk_widget_show( menuItem ); | |
c801d85f | 322 | m_items.Append( mitem ); |
6de97a3b | 323 | } |
c801d85f | 324 | |
96fd301f | 325 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f KB |
326 | { |
327 | wxMenuItem *mitem = new wxMenuItem(); | |
96fd301f VZ |
328 | mitem->SetId(id); |
329 | mitem->SetText(text); | |
330 | ||
331 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); | |
c33c4050 | 332 | mitem->SetHelp(helpStr); |
96fd301f VZ |
333 | mitem->SetMenuItem(menuItem); |
334 | mitem->SetSubMenu(subMenu); | |
335 | ||
336 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); | |
337 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
338 | gtk_widget_show( menuItem ); | |
c801d85f | 339 | m_items.Append( mitem ); |
6de97a3b | 340 | } |
c801d85f KB |
341 | |
342 | int wxMenu::FindItem( const wxString itemString ) const | |
343 | { | |
344 | wxString s( itemString ); | |
96fd301f | 345 | |
d355d3fe | 346 | int pos; |
c801d85f KB |
347 | do { |
348 | pos = s.First( '&' ); | |
d355d3fe RR |
349 | if (pos != -1) s.Remove( pos, 1 ); |
350 | } while (pos != -1); | |
96fd301f | 351 | |
c801d85f KB |
352 | wxNode *node = m_items.First(); |
353 | while (node) | |
354 | { | |
355 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
356 | if (item->GetText() == s) |
357 | return item->GetId(); | |
c801d85f | 358 | node = node->Next(); |
6de97a3b | 359 | } |
96fd301f | 360 | |
c801d85f | 361 | return -1; |
6de97a3b | 362 | } |
c801d85f | 363 | |
96fd301f | 364 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 365 | { |
96fd301f VZ |
366 | wxMenuItem *item = FindItem(id); |
367 | if ( item ) | |
368 | item->Enable(enable); | |
6de97a3b | 369 | } |
716b7364 | 370 | |
96fd301f | 371 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 372 | { |
96fd301f VZ |
373 | wxMenuItem *item = FindItem(id); |
374 | if ( item ) | |
375 | return item->IsEnabled(); | |
376 | else | |
377 | return FALSE; | |
6de97a3b | 378 | } |
e2414cbe | 379 | |
96fd301f | 380 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 381 | { |
96fd301f VZ |
382 | wxMenuItem *item = FindItem(id); |
383 | if ( item ) | |
384 | item->Check(enable); | |
6de97a3b | 385 | } |
c801d85f | 386 | |
96fd301f | 387 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 388 | { |
96fd301f VZ |
389 | wxMenuItem *item = FindItem(id); |
390 | if ( item ) | |
391 | return item->IsChecked(); | |
392 | else | |
393 | return FALSE; | |
6de97a3b | 394 | } |
c801d85f | 395 | |
debe6624 | 396 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 397 | { |
96fd301f | 398 | wxMenuItem *item = FindItem(id); |
c33c4050 | 399 | if (item) |
96fd301f | 400 | item->SetText(label); |
6de97a3b | 401 | } |
96fd301f | 402 | |
c33c4050 RR |
403 | wxString wxMenu::GetLabel( int id ) const |
404 | { | |
405 | wxMenuItem *item = FindItem(id); | |
406 | if (item) return item->GetText(); | |
407 | return ""; | |
408 | } | |
409 | ||
410 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
411 | { | |
412 | wxMenuItem *item = FindItem(id); | |
413 | if (item) item->SetHelp( helpString ); | |
414 | } | |
415 | ||
416 | wxString wxMenu::GetHelpString( int id ) const | |
417 | { | |
418 | wxMenuItem *item = FindItem(id); | |
419 | if (item) return item->GetHelp(); | |
420 | return ""; | |
421 | } | |
422 | ||
96fd301f VZ |
423 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
424 | { | |
c801d85f KB |
425 | wxNode *node = m_items.First(); |
426 | while (node) | |
427 | { | |
428 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
96fd301f VZ |
429 | if (item->GetMenuItem() == menuItem) |
430 | return item->GetId(); | |
c801d85f | 431 | node = node->Next(); |
6de97a3b | 432 | } |
96fd301f VZ |
433 | |
434 | return -1; | |
6de97a3b | 435 | } |
c801d85f | 436 | |
96fd301f | 437 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f KB |
438 | { |
439 | wxNode *node = m_items.First(); | |
96fd301f | 440 | while (node) { |
c801d85f | 441 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
96fd301f VZ |
442 | if ( item->GetId() == id ) |
443 | return item; | |
c801d85f | 444 | node = node->Next(); |
6de97a3b | 445 | } |
96fd301f | 446 | |
30f82ea4 | 447 | wxLogDebug( "wxMenu::FindItem: item %d not found.", id); |
96fd301f | 448 | |
c67daf87 | 449 | return (wxMenuItem *) NULL; |
96fd301f | 450 | } |
c801d85f KB |
451 | |
452 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
453 | { | |
454 | m_invokingWindow = win; | |
6de97a3b | 455 | } |
c801d85f | 456 | |
96fd301f | 457 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f KB |
458 | { |
459 | return m_invokingWindow; | |
6de97a3b | 460 | } |
c801d85f KB |
461 | |
462 |