]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
added printing
[wxWidgets.git] / src / gtk / 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"
30dea054 18#include "wx/intl.h"
c801d85f
KB
19
20//-----------------------------------------------------------------------------
21// wxMenuBar
22//-----------------------------------------------------------------------------
23
24IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
25
96fd301f 26wxMenuBar::wxMenuBar()
c801d85f
KB
27{
28 m_needParent = FALSE; // hmmm
96fd301f 29
c801d85f
KB
30 PreCreation( NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
31
32 m_menus.DeleteContents( TRUE );
96fd301f 33
c801d85f 34 m_widget = gtk_handle_box_new();
96fd301f 35
c801d85f 36 m_menubar = gtk_menu_bar_new();
96fd301f 37
c801d85f 38 gtk_container_add( GTK_CONTAINER(m_widget), m_menubar );
96fd301f 39
c801d85f 40 gtk_widget_show( m_menubar );
96fd301f 41
c801d85f
KB
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; // ??????
96fd301f 51
d355d3fe 52 int pos;
c801d85f
KB
53 do {
54 pos = menu->m_title.First( '&' );
d355d3fe
RR
55 if (pos != -1) menu->m_title.Remove( pos, 1 );
56 } while (pos != -1);
96fd301f 57
c801d85f
KB
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 );
96fd301f 62
c801d85f
KB
63 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
64};
96fd301f 65
716b7364 66static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f
KB
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();
96fd301f
VZ
77 if (item->IsSubMenu())
78 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
c801d85f
KB
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
716b7364 97// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 98static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 99{
96fd301f 100 wxMenuItem* result = menu->FindItem(id);
716b7364
RR
101
102 wxNode *node = menu->m_items.First();
96fd301f 103 while ( node && result == NULL ) {
716b7364 104 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
105 if ( item->IsSubMenu() )
106 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
716b7364
RR
107 node = node->Next();
108 };
96fd301f 109
716b7364
RR
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
54ff4a70
RR
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
716b7364
RR
133{
134 wxMenuItem* item = FindMenuItemById( id );
135 if (item) return item->IsChecked();
136 return FALSE;
54ff4a70 137};
716b7364 138
54ff4a70
RR
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
716b7364
RR
146{
147 wxMenuItem* item = FindMenuItemById( id );
148 if (item) return item->IsEnabled();
149 return FALSE;
54ff4a70 150};
716b7364 151
c801d85f
KB
152//-----------------------------------------------------------------------------
153// wxMenu
154//-----------------------------------------------------------------------------
155
156void gtk_menu_clicked_callback( GtkWidget *widget, gpointer data )
157{
158 wxMenu *menu = (wxMenu*)data;
159 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f
VZ
160
161 wxASSERT( id != -1 ); // should find it!
162
163 if (!menu->IsEnabled(id))
164 return;
165
c801d85f
KB
166 wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id );
167 event.SetEventObject( menu );
168 event.SetInt(id );
169 wxWindow *win = menu->GetInvokingWindow();
cf4219e7 170 if (win) win->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
171};
172
173IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
174
175wxMenuItem::wxMenuItem()
c801d85f 176{
96fd301f 177 m_id = ID_SEPARATOR;
c801d85f 178 m_isCheckMenu = FALSE;
96fd301f
VZ
179 m_isChecked = FALSE;
180 m_isEnabled = TRUE;
c801d85f 181 m_subMenu = NULL;
c801d85f
KB
182 m_menuItem = NULL;
183};
184
96fd301f 185void wxMenuItem::SetText(const wxString& str)
716b7364 186{
30dea054 187 m_text = "";
96fd301f
VZ
188 for ( const char *pc = str; *pc != '\0'; pc++ ) {
189 if ( *pc == '&' )
190 pc++; // skip it
191
192 m_text << *pc;
716b7364
RR
193 }
194}
195
96fd301f 196void wxMenuItem::Check( bool check )
716b7364 197{
30dea054 198 wxCHECK_RET( IsCheckable(), _("Can't check uncheckable item!") )
96fd301f
VZ
199
200 m_isChecked = check;
201 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
202}
203
96fd301f 204bool wxMenuItem::IsChecked() const
716b7364 205{
96fd301f
VZ
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;
716b7364
RR
213}
214
c801d85f
KB
215IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
216
217wxMenu::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
96fd301f 225void wxMenu::AppendSeparator()
c801d85f
KB
226{
227 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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);
c801d85f
KB
234 m_items.Append( mitem );
235};
236
debe6624 237void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f
KB
238{
239 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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 );
c801d85f
KB
255 m_items.Append( mitem );
256};
257
96fd301f 258void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
c801d85f
KB
259{
260 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
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 );
c801d85f
KB
272 m_items.Append( mitem );
273};
274
275int wxMenu::FindItem( const wxString itemString ) const
276{
277 wxString s( itemString );
96fd301f 278
d355d3fe 279 int pos;
c801d85f
KB
280 do {
281 pos = s.First( '&' );
d355d3fe
RR
282 if (pos != -1) s.Remove( pos, 1 );
283 } while (pos != -1);
96fd301f 284
c801d85f
KB
285 wxNode *node = m_items.First();
286 while (node)
287 {
288 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
289 if (item->GetText() == s)
290 return item->GetId();
c801d85f
KB
291 node = node->Next();
292 };
96fd301f 293
c801d85f
KB
294 return -1;
295};
296
96fd301f 297void wxMenu::Enable( int id, bool enable )
716b7364 298{
96fd301f
VZ
299 wxMenuItem *item = FindItem(id);
300 if ( item )
301 item->Enable(enable);
302};
716b7364 303
96fd301f 304bool wxMenu::IsEnabled( int id ) const
e2414cbe 305{
96fd301f
VZ
306 wxMenuItem *item = FindItem(id);
307 if ( item )
308 return item->IsEnabled();
309 else
310 return FALSE;
e2414cbe
RR
311};
312
96fd301f 313void wxMenu::Check( int id, bool enable )
c801d85f 314{
96fd301f
VZ
315 wxMenuItem *item = FindItem(id);
316 if ( item )
317 item->Check(enable);
c801d85f
KB
318};
319
96fd301f 320bool wxMenu::IsChecked( int id ) const
c801d85f 321{
96fd301f
VZ
322 wxMenuItem *item = FindItem(id);
323 if ( item )
324 return item->IsChecked();
325 else
326 return FALSE;
c801d85f
KB
327};
328
debe6624 329void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 330{
96fd301f
VZ
331 wxMenuItem *item = FindItem(id);
332 if ( item )
333 item->SetText(label);
334};
335
336int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
337{
c801d85f
KB
338 wxNode *node = m_items.First();
339 while (node)
340 {
341 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
342 if (item->GetMenuItem() == menuItem)
343 return item->GetId();
c801d85f
KB
344 node = node->Next();
345 };
96fd301f
VZ
346
347 return -1;
c801d85f
KB
348};
349
96fd301f 350wxMenuItem *wxMenu::FindItem(int id) const
c801d85f
KB
351{
352 wxNode *node = m_items.First();
96fd301f 353 while (node) {
c801d85f 354 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
355 if ( item->GetId() == id )
356 return item;
c801d85f
KB
357 node = node->Next();
358 };
96fd301f 359
1a5a8367 360 wxLogDebug(_("wxMenu::FindItem: item %d not found."), id);
96fd301f
VZ
361
362 return NULL;
363}
c801d85f
KB
364
365void wxMenu::SetInvokingWindow( wxWindow *win )
366{
367 m_invokingWindow = win;
368};
369
96fd301f 370wxWindow *wxMenu::GetInvokingWindow()
c801d85f
KB
371{
372 return m_invokingWindow;
373};
374
375