]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/menu.cpp
no message
[wxWidgets.git] / src / gtk / menu.cpp
... / ...
CommitLineData
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
24IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
25
26wxMenuBar::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_widget = gtk_handle_box_new();
35
36 m_menubar = gtk_menu_bar_new();
37
38 gtk_container_add( GTK_CONTAINER(m_widget), m_menubar );
39
40 gtk_widget_show( m_menubar );
41
42 PostCreation();
43
44 Show( TRUE );
45}
46
47void wxMenuBar::Append( wxMenu *menu, const wxString &title )
48{
49 m_menus.Append( menu );
50 menu->m_title = title; // ??????
51
52 int pos;
53 do {
54 pos = menu->m_title.First( '&' );
55 if (pos != -1) menu->m_title.Remove( pos, 1 );
56 } while (pos != -1);
57
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 );
62
63 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
64}
65
66static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
67{
68 if (menu->m_title == menuString)
69 {
70 int res = menu->FindItem( itemString );
71 if (res != -1) return res;
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 return -1;
82}
83
84int 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();
93 }
94 return -1;
95}
96
97// Find a wxMenuItem using its id. Recurses down into sub-menus
98static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
99{
100 wxMenuItem* result = menu->FindItem(id);
101
102 wxNode *node = menu->m_items.First();
103 while ( node && result == NULL ) {
104 wxMenuItem *item = (wxMenuItem*)node->Data();
105 if ( item->IsSubMenu() )
106 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
107 node = node->Next();
108 }
109
110 return result;
111}
112
113wxMenuItem* 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
126void wxMenuBar::Check( int id, bool check )
127{
128 wxMenuItem* item = FindMenuItemById( id );
129 if (item) item->Check(check);
130}
131
132bool wxMenuBar::Checked( int id ) const
133{
134 wxMenuItem* item = FindMenuItemById( id );
135 if (item) return item->IsChecked();
136 return FALSE;
137}
138
139void wxMenuBar::Enable( int id, bool enable )
140{
141 wxMenuItem* item = FindMenuItemById( id );
142 if (item) item->Enable(enable);
143}
144
145bool wxMenuBar::Enabled( int id ) const
146{
147 wxMenuItem* item = FindMenuItemById( id );
148 if (item) return item->IsEnabled();
149 return FALSE;
150}
151
152//-----------------------------------------------------------------------------
153// "activate"
154//-----------------------------------------------------------------------------
155
156static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
157{
158 int id = menu->FindMenuIdByMenuItem(widget);
159
160 wxASSERT( id != -1 ); // should find it!
161
162 if (!menu->IsEnabled(id)) return;
163
164 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
165 event.SetEventObject( menu );
166 event.SetInt(id );
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
176 wxWindow *win = menu->GetInvokingWindow();
177 if (win) win->GetEventHandler()->ProcessEvent( event );
178}
179
180//-----------------------------------------------------------------------------
181// "select"
182//-----------------------------------------------------------------------------
183
184static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
185{
186 int id = menu->FindMenuIdByMenuItem(widget);
187
188 wxASSERT( id != -1 ); // should find it!
189
190 if (!menu->IsEnabled(id)) return;
191
192 wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
193 event.SetEventObject( menu );
194 event.SetInt(id );
195
196/* wxMSW doesn't call callback here either
197 if (menu->m_callback)
198 {
199 (void) (*(menu->m_callback)) (*menu, event);
200 return;
201 }
202*/
203
204 if (menu->GetEventHandler()->ProcessEvent(event)) return;
205
206 wxWindow *win = menu->GetInvokingWindow();
207 if (win) win->GetEventHandler()->ProcessEvent( event );
208}
209
210//-----------------------------------------------------------------------------
211// wxMenu
212//-----------------------------------------------------------------------------
213
214IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
215
216wxMenuItem::wxMenuItem()
217{
218 m_id = ID_SEPARATOR;
219 m_isCheckMenu = FALSE;
220 m_isChecked = FALSE;
221 m_isEnabled = TRUE;
222 m_subMenu = (wxMenu *) NULL;
223 m_menuItem = (GtkWidget *) NULL;
224}
225
226void wxMenuItem::SetText(const wxString& str)
227{
228 m_text = "";
229 for ( const char *pc = str; *pc != '\0'; pc++ ) {
230 if ( *pc == '&' )
231 pc++; // skip it
232
233 m_text << *pc;
234 }
235}
236
237void wxMenuItem::Check( bool check )
238{
239 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
240
241 m_isChecked = check;
242 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
243}
244
245void wxMenuItem::Enable( bool enable )
246{
247 gtk_widget_set_sensitive( m_menuItem, enable );
248 m_isEnabled = enable;
249}
250
251bool wxMenuItem::IsChecked() const
252{
253 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
254
255 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
256
257 wxASSERT( bIsChecked == m_isChecked ); // consistency check
258
259 return bIsChecked;
260}
261
262IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
263
264wxMenu::wxMenu( const wxString& title, const wxFunction func )
265{
266 m_title = title;
267 m_items.DeleteContents( TRUE );
268 m_invokingWindow = (wxWindow *) NULL;
269 m_menu = gtk_menu_new(); // Do not show!
270 m_callback = func;
271 m_eventHandler = this;
272 if (m_title.IsNull()) m_title = "";
273 if (m_title != "")
274 {
275 Append(-2, m_title);
276 AppendSeparator();
277 }
278}
279
280void wxMenu::SetTitle( const wxString& title )
281{
282 // Waiting for something better.
283 m_title = title;
284}
285
286const wxString wxMenu::GetTitle() const
287{
288 return m_title;
289}
290
291void wxMenu::AppendSeparator()
292{
293 wxMenuItem *mitem = new wxMenuItem();
294 mitem->SetId(ID_SEPARATOR);
295
296 GtkWidget *menuItem = gtk_menu_item_new();
297 gtk_menu_append( GTK_MENU(m_menu), menuItem );
298 gtk_widget_show( menuItem );
299 mitem->SetMenuItem(menuItem);
300 m_items.Append( mitem );
301}
302
303void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
304{
305 wxMenuItem *mitem = new wxMenuItem();
306 mitem->SetId(id);
307 mitem->SetText(item);
308 mitem->SetHelp(helpStr);
309 mitem->SetCheckable(checkable);
310 const char *text = mitem->GetText();
311 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
312 : gtk_menu_item_new_with_label(text);
313
314 mitem->SetMenuItem(menuItem);
315
316 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
317 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
318 (gpointer*)this );
319
320 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
321 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
322 (gpointer*)this );
323
324 gtk_menu_append( GTK_MENU(m_menu), menuItem );
325 gtk_widget_show( menuItem );
326 m_items.Append( mitem );
327}
328
329void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
330{
331 wxMenuItem *mitem = new wxMenuItem();
332 mitem->SetId(id);
333 mitem->SetText(text);
334
335 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
336 mitem->SetHelp(helpStr);
337 mitem->SetMenuItem(menuItem);
338 mitem->SetSubMenu(subMenu);
339
340 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
341 gtk_menu_append( GTK_MENU(m_menu), menuItem );
342 gtk_widget_show( menuItem );
343 m_items.Append( mitem );
344}
345
346int wxMenu::FindItem( const wxString itemString ) const
347{
348 wxString s( itemString );
349
350 int pos;
351 do {
352 pos = s.First( '&' );
353 if (pos != -1) s.Remove( pos, 1 );
354 } while (pos != -1);
355
356 wxNode *node = m_items.First();
357 while (node)
358 {
359 wxMenuItem *item = (wxMenuItem*)node->Data();
360 if (item->GetText() == s)
361 return item->GetId();
362 node = node->Next();
363 }
364
365 return -1;
366}
367
368void wxMenu::Enable( int id, bool enable )
369{
370 wxMenuItem *item = FindItem(id);
371 if ( item )
372 item->Enable(enable);
373}
374
375bool wxMenu::IsEnabled( int id ) const
376{
377 wxMenuItem *item = FindItem(id);
378 if ( item )
379 return item->IsEnabled();
380 else
381 return FALSE;
382}
383
384void wxMenu::Check( int id, bool enable )
385{
386 wxMenuItem *item = FindItem(id);
387 if ( item )
388 item->Check(enable);
389}
390
391bool wxMenu::IsChecked( int id ) const
392{
393 wxMenuItem *item = FindItem(id);
394 if ( item )
395 return item->IsChecked();
396 else
397 return FALSE;
398}
399
400void wxMenu::SetLabel( int id, const wxString &label )
401{
402 wxMenuItem *item = FindItem(id);
403 if (item)
404 item->SetText(label);
405}
406
407wxString wxMenu::GetLabel( int id ) const
408{
409 wxMenuItem *item = FindItem(id);
410 if (item) return item->GetText();
411 return "";
412}
413
414void wxMenu::SetHelpString( int id, const wxString& helpString )
415{
416 wxMenuItem *item = FindItem(id);
417 if (item) item->SetHelp( helpString );
418}
419
420wxString wxMenu::GetHelpString( int id ) const
421{
422 wxMenuItem *item = FindItem(id);
423 if (item) return item->GetHelp();
424 return "";
425}
426
427int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
428{
429 wxNode *node = m_items.First();
430 while (node)
431 {
432 wxMenuItem *item = (wxMenuItem*)node->Data();
433 if (item->GetMenuItem() == menuItem)
434 return item->GetId();
435 node = node->Next();
436 }
437
438 return -1;
439}
440
441wxMenuItem *wxMenu::FindItem(int id) const
442{
443 wxNode *node = m_items.First();
444 while (node) {
445 wxMenuItem *item = (wxMenuItem*)node->Data();
446 if ( item->GetId() == id )
447 return item;
448 node = node->Next();
449 }
450
451 wxLogDebug( "wxMenu::FindItem: item %d not found.", id);
452
453 return (wxMenuItem *) NULL;
454}
455
456void wxMenu::SetInvokingWindow( wxWindow *win )
457{
458 m_invokingWindow = win;
459}
460
461wxWindow *wxMenu::GetInvokingWindow()
462{
463 return m_invokingWindow;
464}
465
466