]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/menu.cpp
(1) Denis Pershin's patch for wxGTK (memory leaks corrections)
[wxWidgets.git] / src / gtk1 / menu.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
96fd301f 6// Id: $Id$
c801d85f 7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
96fd301f 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "menu.h"
14#endif
15
16#include "wx/menu.h"
96fd301f 17#include "wx/log.h"
c801d85f
KB
18
19//-----------------------------------------------------------------------------
20// wxMenuBar
21//-----------------------------------------------------------------------------
22
23IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
24
96fd301f 25wxMenuBar::wxMenuBar()
c801d85f
KB
26{
27 m_needParent = FALSE; // hmmm
96fd301f 28
c801d85f
KB
29 PreCreation( NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
30
31 m_menus.DeleteContents( TRUE );
96fd301f 32
c801d85f 33 m_widget = gtk_handle_box_new();
96fd301f 34
c801d85f 35 m_menubar = gtk_menu_bar_new();
96fd301f 36
c801d85f 37 gtk_container_add( GTK_CONTAINER(m_widget), m_menubar );
96fd301f 38
c801d85f 39 gtk_widget_show( m_menubar );
96fd301f 40
c801d85f
KB
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; // ??????
96fd301f 50
d355d3fe 51 int pos;
c801d85f
KB
52 do {
53 pos = menu->m_title.First( '&' );
d355d3fe
RR
54 if (pos != -1) menu->m_title.Remove( pos, 1 );
55 } while (pos != -1);
96fd301f 56
c801d85f
KB
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 );
96fd301f 61
c801d85f
KB
62 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
63};
96fd301f 64
716b7364 65static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f
KB
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();
96fd301f
VZ
76 if (item->IsSubMenu())
77 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
c801d85f
KB
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
716b7364 96// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 97static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 98{
96fd301f 99 wxMenuItem* result = menu->FindItem(id);
716b7364
RR
100
101 wxNode *node = menu->m_items.First();
96fd301f 102 while ( node && result == NULL ) {
716b7364 103 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
104 if ( item->IsSubMenu() )
105 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
716b7364
RR
106 node = node->Next();
107 };
96fd301f 108
716b7364
RR
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
54ff4a70
RR
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
716b7364
RR
132{
133 wxMenuItem* item = FindMenuItemById( id );
134 if (item) return item->IsChecked();
135 return FALSE;
54ff4a70 136};
716b7364 137
54ff4a70
RR
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
716b7364
RR
145{
146 wxMenuItem* item = FindMenuItemById( id );
147 if (item) return item->IsEnabled();
148 return FALSE;
54ff4a70 149};
716b7364 150
c801d85f
KB
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);
96fd301f
VZ
159
160 wxASSERT( id != -1 ); // should find it!
161
162 if (!menu->IsEnabled(id))
163 return;
164
c801d85f
KB
165 wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id );
166 event.SetEventObject( menu );
167 event.SetInt(id );
168 wxWindow *win = menu->GetInvokingWindow();
cf4219e7 169 if (win) win->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
170};
171
172IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
173
174wxMenuItem::wxMenuItem()
c801d85f 175{
96fd301f 176 m_id = ID_SEPARATOR;
c801d85f 177 m_isCheckMenu = FALSE;
96fd301f
VZ
178 m_isChecked = FALSE;
179 m_isEnabled = TRUE;
c801d85f 180 m_subMenu = NULL;
c801d85f
KB
181 m_menuItem = NULL;
182};
183
96fd301f 184void wxMenuItem::SetText(const wxString& str)
716b7364 185{
96fd301f
VZ
186 for ( const char *pc = str; *pc != '\0'; pc++ ) {
187 if ( *pc == '&' )
188 pc++; // skip it
189
190 m_text << *pc;
716b7364
RR
191 }
192}
193
96fd301f 194void wxMenuItem::Check( bool check )
716b7364 195{
96fd301f
VZ
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 );
716b7364
RR
200}
201
96fd301f 202bool wxMenuItem::IsChecked() const
716b7364 203{
96fd301f
VZ
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;
716b7364
RR
211}
212
c801d85f
KB
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
96fd301f 223void wxMenu::AppendSeparator()
c801d85f
KB
224{
225 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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);
c801d85f
KB
232 m_items.Append( mitem );
233};
234
debe6624 235void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f
KB
236{
237 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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 );
c801d85f
KB
253 m_items.Append( mitem );
254};
255
96fd301f 256void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
c801d85f
KB
257{
258 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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 );
c801d85f
KB
270 m_items.Append( mitem );
271};
272
273int wxMenu::FindItem( const wxString itemString ) const
274{
275 wxString s( itemString );
96fd301f 276
d355d3fe 277 int pos;
c801d85f
KB
278 do {
279 pos = s.First( '&' );
d355d3fe
RR
280 if (pos != -1) s.Remove( pos, 1 );
281 } while (pos != -1);
96fd301f 282
c801d85f
KB
283 wxNode *node = m_items.First();
284 while (node)
285 {
286 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
287 if (item->GetText() == s)
288 return item->GetId();
c801d85f
KB
289 node = node->Next();
290 };
96fd301f 291
c801d85f
KB
292 return -1;
293};
294
96fd301f 295void wxMenu::Enable( int id, bool enable )
716b7364 296{
96fd301f
VZ
297 wxMenuItem *item = FindItem(id);
298 if ( item )
299 item->Enable(enable);
300};
716b7364 301
96fd301f 302bool wxMenu::IsEnabled( int id ) const
e2414cbe 303{
96fd301f
VZ
304 wxMenuItem *item = FindItem(id);
305 if ( item )
306 return item->IsEnabled();
307 else
308 return FALSE;
e2414cbe
RR
309};
310
96fd301f 311void wxMenu::Check( int id, bool enable )
c801d85f 312{
96fd301f
VZ
313 wxMenuItem *item = FindItem(id);
314 if ( item )
315 item->Check(enable);
c801d85f
KB
316};
317
96fd301f 318bool wxMenu::IsChecked( int id ) const
c801d85f 319{
96fd301f
VZ
320 wxMenuItem *item = FindItem(id);
321 if ( item )
322 return item->IsChecked();
323 else
324 return FALSE;
c801d85f
KB
325};
326
debe6624 327void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 328{
96fd301f
VZ
329 wxMenuItem *item = FindItem(id);
330 if ( item )
331 item->SetText(label);
332};
333
334int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
335{
c801d85f
KB
336 wxNode *node = m_items.First();
337 while (node)
338 {
339 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
340 if (item->GetMenuItem() == menuItem)
341 return item->GetId();
c801d85f
KB
342 node = node->Next();
343 };
96fd301f
VZ
344
345 return -1;
c801d85f
KB
346};
347
96fd301f 348wxMenuItem *wxMenu::FindItem(int id) const
c801d85f
KB
349{
350 wxNode *node = m_items.First();
96fd301f 351 while (node) {
c801d85f 352 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
353 if ( item->GetId() == id )
354 return item;
c801d85f
KB
355 node = node->Next();
356 };
96fd301f
VZ
357
358 wxLogDebug("wxMenu::FindItem: item %d not found.", id);
359
360 return NULL;
361}
c801d85f
KB
362
363void wxMenu::SetInvokingWindow( wxWindow *win )
364{
365 m_invokingWindow = win;
366};
367
96fd301f 368wxWindow *wxMenu::GetInvokingWindow()
c801d85f
KB
369{
370 return m_invokingWindow;
371};
372
373