]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
Added IRIX compile fixes
[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 static 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
180 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
181
182 wxMenuItem::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
192 void 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
203 void 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
211 void wxMenuItem::Enable( bool enable )
212 {
213 gtk_widget_set_sensitive( m_menuItem, enable );
214 m_isEnabled = enable;
215 }
216
217 bool wxMenuItem::IsChecked() const
218 {
219 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
220
221 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
222
223 wxASSERT( bIsChecked == m_isChecked ); // consistency check
224
225 return bIsChecked;
226 }
227
228 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
229
230 wxMenu::wxMenu( const wxString& title, const wxFunction func )
231 {
232 m_title = title;
233 m_items.DeleteContents( TRUE );
234 m_invokingWindow = NULL;
235 m_menu = gtk_menu_new(); // Do not show!
236 m_callback = func;
237 m_eventHandler = this;
238 if (m_title.IsNull()) m_title = "";
239 if (m_title != "")
240 {
241 Append(-2, m_title);
242 AppendSeparator();
243 }
244 }
245
246 void wxMenu::AppendSeparator()
247 {
248 wxMenuItem *mitem = new wxMenuItem();
249 mitem->SetId(ID_SEPARATOR);
250
251 GtkWidget *menuItem = gtk_menu_item_new();
252 gtk_menu_append( GTK_MENU(m_menu), menuItem );
253 gtk_widget_show( menuItem );
254 mitem->SetMenuItem(menuItem);
255 m_items.Append( mitem );
256 }
257
258 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
259 {
260 wxMenuItem *mitem = new wxMenuItem();
261 mitem->SetId(id);
262 mitem->SetText(item);
263 mitem->SetHelp(helpStr);
264 mitem->SetCheckable(checkable);
265 const char *text = mitem->GetText();
266 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
267 : gtk_menu_item_new_with_label(text);
268
269 mitem->SetMenuItem(menuItem);
270
271 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
272 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
273 (gpointer*)this );
274
275 gtk_menu_append( GTK_MENU(m_menu), menuItem );
276 gtk_widget_show( menuItem );
277 m_items.Append( mitem );
278 }
279
280 void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
281 {
282 wxMenuItem *mitem = new wxMenuItem();
283 mitem->SetId(id);
284 mitem->SetText(text);
285
286 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
287 mitem->SetHelp(helpStr);
288 mitem->SetMenuItem(menuItem);
289 mitem->SetSubMenu(subMenu);
290
291 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
292 gtk_menu_append( GTK_MENU(m_menu), menuItem );
293 gtk_widget_show( menuItem );
294 m_items.Append( mitem );
295 }
296
297 int wxMenu::FindItem( const wxString itemString ) const
298 {
299 wxString s( itemString );
300
301 int pos;
302 do {
303 pos = s.First( '&' );
304 if (pos != -1) s.Remove( pos, 1 );
305 } while (pos != -1);
306
307 wxNode *node = m_items.First();
308 while (node)
309 {
310 wxMenuItem *item = (wxMenuItem*)node->Data();
311 if (item->GetText() == s)
312 return item->GetId();
313 node = node->Next();
314 }
315
316 return -1;
317 }
318
319 void wxMenu::Enable( int id, bool enable )
320 {
321 wxMenuItem *item = FindItem(id);
322 if ( item )
323 item->Enable(enable);
324 }
325
326 bool wxMenu::IsEnabled( int id ) const
327 {
328 wxMenuItem *item = FindItem(id);
329 if ( item )
330 return item->IsEnabled();
331 else
332 return FALSE;
333 }
334
335 void wxMenu::Check( int id, bool enable )
336 {
337 wxMenuItem *item = FindItem(id);
338 if ( item )
339 item->Check(enable);
340 }
341
342 bool wxMenu::IsChecked( int id ) const
343 {
344 wxMenuItem *item = FindItem(id);
345 if ( item )
346 return item->IsChecked();
347 else
348 return FALSE;
349 }
350
351 void wxMenu::SetLabel( int id, const wxString &label )
352 {
353 wxMenuItem *item = FindItem(id);
354 if (item)
355 item->SetText(label);
356 }
357
358 wxString wxMenu::GetLabel( int id ) const
359 {
360 wxMenuItem *item = FindItem(id);
361 if (item) return item->GetText();
362 return "";
363 }
364
365 void wxMenu::SetHelpString( int id, const wxString& helpString )
366 {
367 wxMenuItem *item = FindItem(id);
368 if (item) item->SetHelp( helpString );
369 }
370
371 wxString wxMenu::GetHelpString( int id ) const
372 {
373 wxMenuItem *item = FindItem(id);
374 if (item) return item->GetHelp();
375 return "";
376 }
377
378 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
379 {
380 wxNode *node = m_items.First();
381 while (node)
382 {
383 wxMenuItem *item = (wxMenuItem*)node->Data();
384 if (item->GetMenuItem() == menuItem)
385 return item->GetId();
386 node = node->Next();
387 }
388
389 return -1;
390 }
391
392 wxMenuItem *wxMenu::FindItem(int id) const
393 {
394 wxNode *node = m_items.First();
395 while (node) {
396 wxMenuItem *item = (wxMenuItem*)node->Data();
397 if ( item->GetId() == id )
398 return item;
399 node = node->Next();
400 }
401
402 wxLogDebug(_("wxMenu::FindItem: item %d not found."), id);
403
404 return NULL;
405 }
406
407 void wxMenu::SetInvokingWindow( wxWindow *win )
408 {
409 m_invokingWindow = win;
410 }
411
412 wxWindow *wxMenu::GetInvokingWindow()
413 {
414 return m_invokingWindow;
415 }
416
417