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