]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/menu.cpp
added option to hide page controls
[wxWidgets.git] / src / gtk1 / 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( 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// wxMenu
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( wxEVENT_TYPE_MENU_COMMAND, 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
180IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
181
182wxMenuItem::wxMenuItem()
183{
184 m_id = ID_SEPARATOR;
185 m_isCheckMenu = FALSE;
186 m_isChecked = FALSE;
187 m_isEnabled = TRUE;
188 m_subMenu = NULL;
189 m_menuItem = NULL;
190}
191
192void wxMenuItem::SetText(const wxString& str)
193{
194 m_text = "";
195 for ( const char *pc = str; *pc != '\0'; pc++ ) {
196 if ( *pc == '&' )
197 pc++; // skip it
198
199 m_text << *pc;
200 }
201}
202
203void wxMenuItem::Check( bool check )
204{
205 wxCHECK_RET( IsCheckable(), _("Can't check uncheckable item!") )
206
207 m_isChecked = check;
208 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
209}
210
211bool wxMenuItem::IsChecked() const
212{
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;
220}
221
222IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
223
224wxMenu::wxMenu( const wxString& title, const wxFunction func )
225{
226 m_title = title;
227 m_items.DeleteContents( TRUE );
228 m_invokingWindow = NULL;
229 m_menu = gtk_menu_new(); // Do not show!
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}
239
240void wxMenu::AppendSeparator()
241{
242 wxMenuItem *mitem = new wxMenuItem();
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);
249 m_items.Append( mitem );
250}
251
252void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
253{
254 wxMenuItem *mitem = new wxMenuItem();
255 mitem->SetId(id);
256 mitem->SetText(item);
257 mitem->SetHelpString(helpStr);
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 );
270 m_items.Append( mitem );
271}
272
273void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
274{
275 wxMenuItem *mitem = new wxMenuItem();
276 mitem->SetId(id);
277 mitem->SetText(text);
278
279 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
280 mitem->SetHelpString(helpStr);
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 );
287 m_items.Append( mitem );
288}
289
290int wxMenu::FindItem( const wxString itemString ) const
291{
292 wxString s( itemString );
293
294 int pos;
295 do {
296 pos = s.First( '&' );
297 if (pos != -1) s.Remove( pos, 1 );
298 } while (pos != -1);
299
300 wxNode *node = m_items.First();
301 while (node)
302 {
303 wxMenuItem *item = (wxMenuItem*)node->Data();
304 if (item->GetText() == s)
305 return item->GetId();
306 node = node->Next();
307 }
308
309 return -1;
310}
311
312void wxMenu::Enable( int id, bool enable )
313{
314 wxMenuItem *item = FindItem(id);
315 if ( item )
316 item->Enable(enable);
317}
318
319bool wxMenu::IsEnabled( int id ) const
320{
321 wxMenuItem *item = FindItem(id);
322 if ( item )
323 return item->IsEnabled();
324 else
325 return FALSE;
326}
327
328void wxMenu::Check( int id, bool enable )
329{
330 wxMenuItem *item = FindItem(id);
331 if ( item )
332 item->Check(enable);
333}
334
335bool wxMenu::IsChecked( int id ) const
336{
337 wxMenuItem *item = FindItem(id);
338 if ( item )
339 return item->IsChecked();
340 else
341 return FALSE;
342}
343
344void wxMenu::SetLabel( int id, const wxString &label )
345{
346 wxMenuItem *item = FindItem(id);
347 if ( item )
348 item->SetText(label);
349}
350
351int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
352{
353 wxNode *node = m_items.First();
354 while (node)
355 {
356 wxMenuItem *item = (wxMenuItem*)node->Data();
357 if (item->GetMenuItem() == menuItem)
358 return item->GetId();
359 node = node->Next();
360 }
361
362 return -1;
363}
364
365wxMenuItem *wxMenu::FindItem(int id) const
366{
367 wxNode *node = m_items.First();
368 while (node) {
369 wxMenuItem *item = (wxMenuItem*)node->Data();
370 if ( item->GetId() == id )
371 return item;
372 node = node->Next();
373 }
374
375 wxLogDebug(_("wxMenu::FindItem: item %d not found."), id);
376
377 return NULL;
378}
379
380void wxMenu::SetInvokingWindow( wxWindow *win )
381{
382 m_invokingWindow = win;
383}
384
385wxWindow *wxMenu::GetInvokingWindow()
386{
387 return m_invokingWindow;
388}
389
390