]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: $Id$ | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "menu.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/menu.h" | |
17 | #include "wx/log.h" | |
18 | #include "wx/intl.h" | |
19 | ||
20 | //----------------------------------------------------------------------------- | |
21 | // wxMenuBar | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
25 | ||
26 | wxMenuBar::wxMenuBar() | |
27 | { | |
28 | m_needParent = FALSE; // hmmm | |
29 | ||
30 | PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); | |
31 | ||
32 | m_menus.DeleteContents( TRUE ); | |
33 | ||
34 | m_menubar = gtk_menu_bar_new(); | |
35 | ||
36 | m_widget = GTK_WIDGET(m_menubar); | |
37 | ||
38 | PostCreation(); | |
39 | ||
40 | Show( TRUE ); | |
41 | } | |
42 | ||
43 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
44 | { | |
45 | m_menus.Append( menu ); | |
46 | menu->m_title = title; // ?????? | |
47 | ||
48 | int pos; | |
49 | do { | |
50 | pos = menu->m_title.First( '&' ); | |
51 | if (pos != -1) menu->m_title.Remove( pos, 1 ); | |
52 | } while (pos != -1); | |
53 | ||
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 ); | |
58 | ||
59 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu ); | |
60 | } | |
61 | ||
62 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
63 | { | |
64 | if (menu->m_title == menuString) | |
65 | { | |
66 | int res = menu->FindItem( itemString ); | |
67 | if (res != -1) return res; | |
68 | } | |
69 | wxNode *node = menu->m_items.First(); | |
70 | while (node) | |
71 | { | |
72 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
73 | if (item->IsSubMenu()) | |
74 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
75 | node = node->Next(); | |
76 | } | |
77 | return -1; | |
78 | } | |
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(); | |
89 | } | |
90 | return -1; | |
91 | } | |
92 | ||
93 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
94 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
95 | { | |
96 | wxMenuItem* result = menu->FindItem(id); | |
97 | ||
98 | wxNode *node = menu->m_items.First(); | |
99 | while ( node && result == NULL ) { | |
100 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
101 | if ( item->IsSubMenu() ) | |
102 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
103 | node = node->Next(); | |
104 | } | |
105 | ||
106 | return result; | |
107 | } | |
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 | ||
122 | void wxMenuBar::Check( int id, bool check ) | |
123 | { | |
124 | wxMenuItem* item = FindMenuItemById( id ); | |
125 | if (item) item->Check(check); | |
126 | } | |
127 | ||
128 | bool wxMenuBar::Checked( int id ) const | |
129 | { | |
130 | wxMenuItem* item = FindMenuItemById( id ); | |
131 | if (item) return item->IsChecked(); | |
132 | return FALSE; | |
133 | } | |
134 | ||
135 | void wxMenuBar::Enable( int id, bool enable ) | |
136 | { | |
137 | wxMenuItem* item = FindMenuItemById( id ); | |
138 | if (item) item->Enable(enable); | |
139 | } | |
140 | ||
141 | bool wxMenuBar::Enabled( int id ) const | |
142 | { | |
143 | wxMenuItem* item = FindMenuItemById( id ); | |
144 | if (item) return item->IsEnabled(); | |
145 | return FALSE; | |
146 | } | |
147 | ||
148 | //----------------------------------------------------------------------------- | |
149 | // "activate" | |
150 | //----------------------------------------------------------------------------- | |
151 | ||
152 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) | |
153 | { | |
154 | int id = menu->FindMenuIdByMenuItem(widget); | |
155 | ||
156 | wxASSERT( id != -1 ); // should find it! | |
157 | ||
158 | if (!menu->IsEnabled(id)) return; | |
159 | ||
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 ); | |
189 | event.SetEventObject( menu ); | |
190 | event.SetInt(id ); | |
191 | ||
192 | /* wxMSW doesn't call callback here either | |
193 | if (menu->m_callback) | |
194 | { | |
195 | (void) (*(menu->m_callback)) (*menu, event); | |
196 | return; | |
197 | } | |
198 | */ | |
199 | ||
200 | if (menu->GetEventHandler()->ProcessEvent(event)) return; | |
201 | ||
202 | wxWindow *win = menu->GetInvokingWindow(); | |
203 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
204 | } | |
205 | ||
206 | //----------------------------------------------------------------------------- | |
207 | // wxMenu | |
208 | //----------------------------------------------------------------------------- | |
209 | ||
210 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) | |
211 | ||
212 | wxMenuItem::wxMenuItem() | |
213 | { | |
214 | m_id = ID_SEPARATOR; | |
215 | m_isCheckMenu = FALSE; | |
216 | m_isChecked = FALSE; | |
217 | m_isEnabled = TRUE; | |
218 | m_subMenu = (wxMenu *) NULL; | |
219 | m_menuItem = (GtkWidget *) NULL; | |
220 | } | |
221 | ||
222 | void wxMenuItem::SetText(const wxString& str) | |
223 | { | |
224 | m_text = ""; | |
225 | for ( const char *pc = str; *pc != '\0'; pc++ ) { | |
226 | if ( *pc == '&' ) | |
227 | pc++; // skip it | |
228 | ||
229 | m_text << *pc; | |
230 | } | |
231 | } | |
232 | ||
233 | void wxMenuItem::Check( bool check ) | |
234 | { | |
235 | wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" ) | |
236 | ||
237 | m_isChecked = check; | |
238 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
239 | } | |
240 | ||
241 | void wxMenuItem::Enable( bool enable ) | |
242 | { | |
243 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
244 | m_isEnabled = enable; | |
245 | } | |
246 | ||
247 | bool wxMenuItem::IsChecked() const | |
248 | { | |
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; | |
256 | } | |
257 | ||
258 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
259 | ||
260 | wxMenu::wxMenu( const wxString& title, const wxFunction func ) | |
261 | { | |
262 | m_title = title; | |
263 | m_items.DeleteContents( TRUE ); | |
264 | m_invokingWindow = (wxWindow *) NULL; | |
265 | m_menu = gtk_menu_new(); // Do not show! | |
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 | } | |
275 | ||
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 | ||
287 | void wxMenu::AppendSeparator() | |
288 | { | |
289 | wxMenuItem *mitem = new wxMenuItem(); | |
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); | |
296 | m_items.Append( mitem ); | |
297 | } | |
298 | ||
299 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) | |
300 | { | |
301 | wxMenuItem *mitem = new wxMenuItem(); | |
302 | mitem->SetId(id); | |
303 | mitem->SetText(item); | |
304 | mitem->SetHelp(helpStr); | |
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); | |
309 | ||
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 | ||
316 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
317 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
318 | (gpointer*)this ); | |
319 | ||
320 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
321 | gtk_widget_show( menuItem ); | |
322 | m_items.Append( mitem ); | |
323 | } | |
324 | ||
325 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) | |
326 | { | |
327 | wxMenuItem *mitem = new wxMenuItem(); | |
328 | mitem->SetId(id); | |
329 | mitem->SetText(text); | |
330 | ||
331 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); | |
332 | mitem->SetHelp(helpStr); | |
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 ); | |
339 | m_items.Append( mitem ); | |
340 | } | |
341 | ||
342 | int wxMenu::FindItem( const wxString itemString ) const | |
343 | { | |
344 | wxString s( itemString ); | |
345 | ||
346 | int pos; | |
347 | do { | |
348 | pos = s.First( '&' ); | |
349 | if (pos != -1) s.Remove( pos, 1 ); | |
350 | } while (pos != -1); | |
351 | ||
352 | wxNode *node = m_items.First(); | |
353 | while (node) | |
354 | { | |
355 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
356 | if (item->GetText() == s) | |
357 | return item->GetId(); | |
358 | node = node->Next(); | |
359 | } | |
360 | ||
361 | return -1; | |
362 | } | |
363 | ||
364 | void wxMenu::Enable( int id, bool enable ) | |
365 | { | |
366 | wxMenuItem *item = FindItem(id); | |
367 | if ( item ) | |
368 | item->Enable(enable); | |
369 | } | |
370 | ||
371 | bool wxMenu::IsEnabled( int id ) const | |
372 | { | |
373 | wxMenuItem *item = FindItem(id); | |
374 | if ( item ) | |
375 | return item->IsEnabled(); | |
376 | else | |
377 | return FALSE; | |
378 | } | |
379 | ||
380 | void wxMenu::Check( int id, bool enable ) | |
381 | { | |
382 | wxMenuItem *item = FindItem(id); | |
383 | if ( item ) | |
384 | item->Check(enable); | |
385 | } | |
386 | ||
387 | bool wxMenu::IsChecked( int id ) const | |
388 | { | |
389 | wxMenuItem *item = FindItem(id); | |
390 | if ( item ) | |
391 | return item->IsChecked(); | |
392 | else | |
393 | return FALSE; | |
394 | } | |
395 | ||
396 | void wxMenu::SetLabel( int id, const wxString &label ) | |
397 | { | |
398 | wxMenuItem *item = FindItem(id); | |
399 | if (item) | |
400 | item->SetText(label); | |
401 | } | |
402 | ||
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 | ||
423 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
424 | { | |
425 | wxNode *node = m_items.First(); | |
426 | while (node) | |
427 | { | |
428 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
429 | if (item->GetMenuItem() == menuItem) | |
430 | return item->GetId(); | |
431 | node = node->Next(); | |
432 | } | |
433 | ||
434 | return -1; | |
435 | } | |
436 | ||
437 | wxMenuItem *wxMenu::FindItem(int id) const | |
438 | { | |
439 | wxNode *node = m_items.First(); | |
440 | while (node) { | |
441 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
442 | if ( item->GetId() == id ) | |
443 | return item; | |
444 | node = node->Next(); | |
445 | } | |
446 | ||
447 | wxLogDebug( "wxMenu::FindItem: item %d not found.", id); | |
448 | ||
449 | return (wxMenuItem *) NULL; | |
450 | } | |
451 | ||
452 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
453 | { | |
454 | m_invokingWindow = win; | |
455 | } | |
456 | ||
457 | wxWindow *wxMenu::GetInvokingWindow() | |
458 | { | |
459 | return m_invokingWindow; | |
460 | } | |
461 | ||
462 |