]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/menu.cpp
Removed redundant makefiles and AIAI icons. Changed dynamic sample source name from
[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( 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
211void wxMenuItem::Enable( bool enable )
212{
213 gtk_widget_set_sensitive( m_menuItem, enable );
214 m_isEnabled = enable;
215}
216
217bool 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
228IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
229
230wxMenu::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
246void wxMenu::SetTitle( const wxString& title )
247{
248 // Waiting for something better.
249 m_title = title;
250}
251
252const wxString wxMenu::GetTitle() const
253{
254 return m_title;
255}
256
257void wxMenu::AppendSeparator()
258{
259 wxMenuItem *mitem = new wxMenuItem();
260 mitem->SetId(ID_SEPARATOR);
261
262 GtkWidget *menuItem = gtk_menu_item_new();
263 gtk_menu_append( GTK_MENU(m_menu), menuItem );
264 gtk_widget_show( menuItem );
265 mitem->SetMenuItem(menuItem);
266 m_items.Append( mitem );
267}
268
269void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
270{
271 wxMenuItem *mitem = new wxMenuItem();
272 mitem->SetId(id);
273 mitem->SetText(item);
274 mitem->SetHelp(helpStr);
275 mitem->SetCheckable(checkable);
276 const char *text = mitem->GetText();
277 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
278 : gtk_menu_item_new_with_label(text);
279
280 mitem->SetMenuItem(menuItem);
281
282 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
283 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
284 (gpointer*)this );
285
286 gtk_menu_append( GTK_MENU(m_menu), menuItem );
287 gtk_widget_show( menuItem );
288 m_items.Append( mitem );
289}
290
291void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
292{
293 wxMenuItem *mitem = new wxMenuItem();
294 mitem->SetId(id);
295 mitem->SetText(text);
296
297 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
298 mitem->SetHelp(helpStr);
299 mitem->SetMenuItem(menuItem);
300 mitem->SetSubMenu(subMenu);
301
302 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
303 gtk_menu_append( GTK_MENU(m_menu), menuItem );
304 gtk_widget_show( menuItem );
305 m_items.Append( mitem );
306}
307
308int wxMenu::FindItem( const wxString itemString ) const
309{
310 wxString s( itemString );
311
312 int pos;
313 do {
314 pos = s.First( '&' );
315 if (pos != -1) s.Remove( pos, 1 );
316 } while (pos != -1);
317
318 wxNode *node = m_items.First();
319 while (node)
320 {
321 wxMenuItem *item = (wxMenuItem*)node->Data();
322 if (item->GetText() == s)
323 return item->GetId();
324 node = node->Next();
325 }
326
327 return -1;
328}
329
330void wxMenu::Enable( int id, bool enable )
331{
332 wxMenuItem *item = FindItem(id);
333 if ( item )
334 item->Enable(enable);
335}
336
337bool wxMenu::IsEnabled( int id ) const
338{
339 wxMenuItem *item = FindItem(id);
340 if ( item )
341 return item->IsEnabled();
342 else
343 return FALSE;
344}
345
346void wxMenu::Check( int id, bool enable )
347{
348 wxMenuItem *item = FindItem(id);
349 if ( item )
350 item->Check(enable);
351}
352
353bool wxMenu::IsChecked( int id ) const
354{
355 wxMenuItem *item = FindItem(id);
356 if ( item )
357 return item->IsChecked();
358 else
359 return FALSE;
360}
361
362void wxMenu::SetLabel( int id, const wxString &label )
363{
364 wxMenuItem *item = FindItem(id);
365 if (item)
366 item->SetText(label);
367}
368
369wxString wxMenu::GetLabel( int id ) const
370{
371 wxMenuItem *item = FindItem(id);
372 if (item) return item->GetText();
373 return "";
374}
375
376void wxMenu::SetHelpString( int id, const wxString& helpString )
377{
378 wxMenuItem *item = FindItem(id);
379 if (item) item->SetHelp( helpString );
380}
381
382wxString wxMenu::GetHelpString( int id ) const
383{
384 wxMenuItem *item = FindItem(id);
385 if (item) return item->GetHelp();
386 return "";
387}
388
389int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
390{
391 wxNode *node = m_items.First();
392 while (node)
393 {
394 wxMenuItem *item = (wxMenuItem*)node->Data();
395 if (item->GetMenuItem() == menuItem)
396 return item->GetId();
397 node = node->Next();
398 }
399
400 return -1;
401}
402
403wxMenuItem *wxMenu::FindItem(int id) const
404{
405 wxNode *node = m_items.First();
406 while (node) {
407 wxMenuItem *item = (wxMenuItem*)node->Data();
408 if ( item->GetId() == id )
409 return item;
410 node = node->Next();
411 }
412
413 wxLogDebug(_("wxMenu::FindItem: item %d not found."), id);
414
415 return NULL;
416}
417
418void wxMenu::SetInvokingWindow( wxWindow *win )
419{
420 m_invokingWindow = win;
421}
422
423wxWindow *wxMenu::GetInvokingWindow()
424{
425 return m_invokingWindow;
426}
427
428