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