]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Added PushEventHandler, Pop...
[wxWidgets.git] / src / gtk / menu.cpp
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
24 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
25
26 wxMenuBar::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
47 void 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
66 static 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
84 int 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
98 static 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
113 wxMenuItem* 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
126 void wxMenuBar::Check( int id, bool check )
127 {
128 wxMenuItem* item = FindMenuItemById( id );
129 if (item) item->Check(check);
130 };
131
132 bool wxMenuBar::Checked( int id ) const
133 {
134 wxMenuItem* item = FindMenuItemById( id );
135 if (item) return item->IsChecked();
136 return FALSE;
137 };
138
139 void wxMenuBar::Enable( int id, bool enable )
140 {
141 wxMenuItem* item = FindMenuItemById( id );
142 if (item) item->Enable(enable);
143 };
144
145 bool 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
156 void gtk_menu_clicked_callback( GtkWidget *widget, gpointer data )
157 {
158 wxMenu *menu = (wxMenu*)data;
159 int id = menu->FindMenuIdByMenuItem(widget);
160
161 wxASSERT( id != -1 ); // should find it!
162
163 if (!menu->IsEnabled(id))
164 return;
165
166 wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id );
167 event.SetEventObject( menu );
168 event.SetInt(id );
169 wxWindow *win = menu->GetInvokingWindow();
170 if (win) win->GetEventHandler()->ProcessEvent( event );
171 };
172
173 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
174
175 wxMenuItem::wxMenuItem()
176 {
177 m_id = ID_SEPARATOR;
178 m_isCheckMenu = FALSE;
179 m_isChecked = FALSE;
180 m_isEnabled = TRUE;
181 m_subMenu = NULL;
182 m_menuItem = NULL;
183 };
184
185 void wxMenuItem::SetText(const wxString& str)
186 {
187 m_text = "";
188 for ( const char *pc = str; *pc != '\0'; pc++ ) {
189 if ( *pc == '&' )
190 pc++; // skip it
191
192 m_text << *pc;
193 }
194 }
195
196 void wxMenuItem::Check( bool check )
197 {
198 wxCHECK_RET( IsCheckable(), _("Can't check uncheckable item!") )
199
200 m_isChecked = check;
201 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
202 }
203
204 bool wxMenuItem::IsChecked() const
205 {
206 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
207
208 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
209
210 wxASSERT( bIsChecked == m_isChecked ); // consistency check
211
212 return bIsChecked;
213 }
214
215 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
216
217 wxMenu::wxMenu( const wxString &title )
218 {
219 m_title = title;
220 m_items.DeleteContents( TRUE );
221 m_invokingWindow = NULL;
222 m_menu = gtk_menu_new(); // Do not show!
223 };
224
225 void wxMenu::AppendSeparator()
226 {
227 wxMenuItem *mitem = new wxMenuItem();
228 mitem->SetId(ID_SEPARATOR);
229
230 GtkWidget *menuItem = gtk_menu_item_new();
231 gtk_menu_append( GTK_MENU(m_menu), menuItem );
232 gtk_widget_show( menuItem );
233 mitem->SetMenuItem(menuItem);
234 m_items.Append( mitem );
235 };
236
237 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
238 {
239 wxMenuItem *mitem = new wxMenuItem();
240 mitem->SetId(id);
241 mitem->SetText(item);
242 mitem->SetHelpString(helpStr);
243 mitem->SetCheckable(checkable);
244 const char *text = mitem->GetText();
245 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
246 : gtk_menu_item_new_with_label(text);
247 mitem->SetMenuItem(menuItem);
248
249 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
250 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
251 (gpointer*)this );
252
253 gtk_menu_append( GTK_MENU(m_menu), menuItem );
254 gtk_widget_show( menuItem );
255 m_items.Append( mitem );
256 };
257
258 void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
259 {
260 wxMenuItem *mitem = new wxMenuItem();
261 mitem->SetId(id);
262 mitem->SetText(text);
263
264 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
265 mitem->SetHelpString(helpStr);
266 mitem->SetMenuItem(menuItem);
267 mitem->SetSubMenu(subMenu);
268
269 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
270 gtk_menu_append( GTK_MENU(m_menu), menuItem );
271 gtk_widget_show( menuItem );
272 m_items.Append( mitem );
273 };
274
275 int wxMenu::FindItem( const wxString itemString ) const
276 {
277 wxString s( itemString );
278
279 int pos;
280 do {
281 pos = s.First( '&' );
282 if (pos != -1) s.Remove( pos, 1 );
283 } while (pos != -1);
284
285 wxNode *node = m_items.First();
286 while (node)
287 {
288 wxMenuItem *item = (wxMenuItem*)node->Data();
289 if (item->GetText() == s)
290 return item->GetId();
291 node = node->Next();
292 };
293
294 return -1;
295 };
296
297 void wxMenu::Enable( int id, bool enable )
298 {
299 wxMenuItem *item = FindItem(id);
300 if ( item )
301 item->Enable(enable);
302 };
303
304 bool wxMenu::IsEnabled( int id ) const
305 {
306 wxMenuItem *item = FindItem(id);
307 if ( item )
308 return item->IsEnabled();
309 else
310 return FALSE;
311 };
312
313 void wxMenu::Check( int id, bool enable )
314 {
315 wxMenuItem *item = FindItem(id);
316 if ( item )
317 item->Check(enable);
318 };
319
320 bool wxMenu::IsChecked( int id ) const
321 {
322 wxMenuItem *item = FindItem(id);
323 if ( item )
324 return item->IsChecked();
325 else
326 return FALSE;
327 };
328
329 void wxMenu::SetLabel( int id, const wxString &label )
330 {
331 wxMenuItem *item = FindItem(id);
332 if ( item )
333 item->SetText(label);
334 };
335
336 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
337 {
338 wxNode *node = m_items.First();
339 while (node)
340 {
341 wxMenuItem *item = (wxMenuItem*)node->Data();
342 if (item->GetMenuItem() == menuItem)
343 return item->GetId();
344 node = node->Next();
345 };
346
347 return -1;
348 };
349
350 wxMenuItem *wxMenu::FindItem(int id) const
351 {
352 wxNode *node = m_items.First();
353 while (node) {
354 wxMenuItem *item = (wxMenuItem*)node->Data();
355 if ( item->GetId() == id )
356 return item;
357 node = node->Next();
358 };
359
360 wxLogDebug(_("wxMenu::FindItem: item %d not found."), id);
361
362 return NULL;
363 }
364
365 void wxMenu::SetInvokingWindow( wxWindow *win )
366 {
367 m_invokingWindow = win;
368 };
369
370 wxWindow *wxMenu::GetInvokingWindow()
371 {
372 return m_invokingWindow;
373 };
374
375