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