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