]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Fixed module code
[wxWidgets.git] / src / gtk / menu.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose:
4// Author: Robert Roebling
96fd301f 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling
96fd301f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
c801d85f
KB
10#ifdef __GNUG__
11#pragma implementation "menu.h"
6ca41e57 12#pragma implementation "menuitem.h"
c801d85f
KB
13#endif
14
15#include "wx/menu.h"
96fd301f 16#include "wx/log.h"
30dea054 17#include "wx/intl.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
c67daf87 29 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
c801d85f
KB
30
31 m_menus.DeleteContents( TRUE );
96fd301f 32
c801d85f 33 m_menubar = gtk_menu_bar_new();
32e9da8b
RR
34
35 m_widget = GTK_WIDGET(m_menubar);
96fd301f 36
c801d85f
KB
37 PostCreation();
38
39 Show( TRUE );
6de97a3b 40}
c801d85f
KB
41
42void wxMenuBar::Append( wxMenu *menu, const wxString &title )
43{
44 m_menus.Append( menu );
45 menu->m_title = title; // ??????
96fd301f 46
d355d3fe 47 int pos;
c801d85f
KB
48 do {
49 pos = menu->m_title.First( '&' );
d355d3fe
RR
50 if (pos != -1) menu->m_title.Remove( pos, 1 );
51 } while (pos != -1);
96fd301f 52
c801d85f
KB
53 GtkWidget *root_menu;
54 root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
55 gtk_widget_show( root_menu );
56 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu );
96fd301f 57
c801d85f 58 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
6de97a3b 59}
96fd301f 60
716b7364 61static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f
KB
62{
63 if (menu->m_title == menuString)
64 {
65 int res = menu->FindItem( itemString );
66 if (res != -1) return res;
6de97a3b 67 }
c801d85f
KB
68 wxNode *node = menu->m_items.First();
69 while (node)
70 {
71 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
72 if (item->IsSubMenu())
73 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
c801d85f 74 node = node->Next();
6de97a3b 75 }
c801d85f 76 return -1;
6de97a3b 77}
c801d85f
KB
78
79int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
80{
81 wxNode *node = m_menus.First();
82 while (node)
83 {
84 wxMenu *menu = (wxMenu*)node->Data();
85 int res = FindMenuItemRecursive( menu, menuString, itemString);
86 if (res != -1) return res;
87 node = node->Next();
6de97a3b 88 }
c801d85f 89 return -1;
6de97a3b 90}
c801d85f 91
716b7364 92// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 93static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 94{
96fd301f 95 wxMenuItem* result = menu->FindItem(id);
716b7364
RR
96
97 wxNode *node = menu->m_items.First();
96fd301f 98 while ( node && result == NULL ) {
716b7364 99 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
100 if ( item->IsSubMenu() )
101 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
716b7364 102 node = node->Next();
6de97a3b 103 }
96fd301f 104
716b7364 105 return result;
6de97a3b 106}
716b7364
RR
107
108wxMenuItem* wxMenuBar::FindMenuItemById( int id ) const
109{
110 wxMenuItem* result = 0;
111 wxNode *node = m_menus.First();
112 while (node && result == 0)
113 {
114 wxMenu *menu = (wxMenu*)node->Data();
115 result = FindMenuItemByIdRecursive( menu, id );
116 node = node->Next();
117 }
118 return result;
119}
120
54ff4a70
RR
121void wxMenuBar::Check( int id, bool check )
122{
123 wxMenuItem* item = FindMenuItemById( id );
124 if (item) item->Check(check);
6de97a3b 125}
54ff4a70
RR
126
127bool wxMenuBar::Checked( int id ) const
716b7364
RR
128{
129 wxMenuItem* item = FindMenuItemById( id );
130 if (item) return item->IsChecked();
131 return FALSE;
6de97a3b 132}
716b7364 133
54ff4a70
RR
134void wxMenuBar::Enable( int id, bool enable )
135{
136 wxMenuItem* item = FindMenuItemById( id );
137 if (item) item->Enable(enable);
6de97a3b 138}
54ff4a70
RR
139
140bool wxMenuBar::Enabled( int id ) const
716b7364
RR
141{
142 wxMenuItem* item = FindMenuItemById( id );
143 if (item) return item->IsEnabled();
144 return FALSE;
6de97a3b 145}
716b7364 146
c801d85f 147//-----------------------------------------------------------------------------
cf7a7e13 148// "activate"
c801d85f
KB
149//-----------------------------------------------------------------------------
150
6de97a3b 151static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 152{
c801d85f 153 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f
VZ
154
155 wxASSERT( id != -1 ); // should find it!
156
6de97a3b 157 if (!menu->IsEnabled(id)) return;
96fd301f 158
cf7a7e13
RR
159 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
160 event.SetEventObject( menu );
161 event.SetInt(id );
162
163 if (menu->m_callback)
164 {
165 (void) (*(menu->m_callback)) (*menu, event);
166 return;
167 }
168
169 if (menu->GetEventHandler()->ProcessEvent(event)) return;
170
171 wxWindow *win = menu->GetInvokingWindow();
172 if (win) win->GetEventHandler()->ProcessEvent( event );
173}
174
175//-----------------------------------------------------------------------------
176// "select"
177//-----------------------------------------------------------------------------
178
179static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
180{
181 int id = menu->FindMenuIdByMenuItem(widget);
182
183 wxASSERT( id != -1 ); // should find it!
184
185 if (!menu->IsEnabled(id)) return;
186
187 wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
c801d85f
KB
188 event.SetEventObject( menu );
189 event.SetInt(id );
6de97a3b 190
13439807 191/* wxMSW doesn't call callback here either
6de97a3b
RR
192 if (menu->m_callback)
193 {
194 (void) (*(menu->m_callback)) (*menu, event);
195 return;
196 }
13439807 197*/
6de97a3b
RR
198
199 if (menu->GetEventHandler()->ProcessEvent(event)) return;
200
c801d85f 201 wxWindow *win = menu->GetInvokingWindow();
cf4219e7 202 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 203}
c801d85f 204
cf7a7e13
RR
205//-----------------------------------------------------------------------------
206// wxMenu
207//-----------------------------------------------------------------------------
208
c801d85f 209IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
210
211wxMenuItem::wxMenuItem()
c801d85f 212{
96fd301f 213 m_id = ID_SEPARATOR;
c801d85f 214 m_isCheckMenu = FALSE;
96fd301f
VZ
215 m_isChecked = FALSE;
216 m_isEnabled = TRUE;
c67daf87
UR
217 m_subMenu = (wxMenu *) NULL;
218 m_menuItem = (GtkWidget *) NULL;
6de97a3b 219}
c801d85f 220
96fd301f 221void wxMenuItem::SetText(const wxString& str)
716b7364 222{
30dea054 223 m_text = "";
96fd301f
VZ
224 for ( const char *pc = str; *pc != '\0'; pc++ ) {
225 if ( *pc == '&' )
226 pc++; // skip it
227
228 m_text << *pc;
716b7364
RR
229 }
230}
231
96fd301f 232void wxMenuItem::Check( bool check )
716b7364 233{
30f82ea4 234 wxCHECK_RET( IsCheckable(), "Can't check uncheckable item!" )
96fd301f
VZ
235
236 m_isChecked = check;
237 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
238}
239
a9c96bcc
RR
240void wxMenuItem::Enable( bool enable )
241{
242 gtk_widget_set_sensitive( m_menuItem, enable );
243 m_isEnabled = enable;
244}
245
96fd301f 246bool wxMenuItem::IsChecked() const
716b7364 247{
96fd301f
VZ
248 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
249
250 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
251
252 wxASSERT( bIsChecked == m_isChecked ); // consistency check
253
254 return bIsChecked;
716b7364
RR
255}
256
c801d85f
KB
257IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
258
6de97a3b 259wxMenu::wxMenu( const wxString& title, const wxFunction func )
c801d85f
KB
260{
261 m_title = title;
262 m_items.DeleteContents( TRUE );
c67daf87 263 m_invokingWindow = (wxWindow *) NULL;
c801d85f 264 m_menu = gtk_menu_new(); // Do not show!
41dee9d0 265
6de97a3b
RR
266 m_callback = func;
267 m_eventHandler = this;
41dee9d0
RR
268 m_clientData = (void*) NULL;
269
6de97a3b
RR
270 if (m_title.IsNull()) m_title = "";
271 if (m_title != "")
272 {
273 Append(-2, m_title);
274 AppendSeparator();
275 }
276}
c801d85f 277
c2dd8380
GL
278void wxMenu::SetTitle( const wxString& title )
279{
280 // Waiting for something better.
281 m_title = title;
282}
283
284const wxString wxMenu::GetTitle() const
285{
286 return m_title;
287}
288
96fd301f 289void wxMenu::AppendSeparator()
c801d85f
KB
290{
291 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
292 mitem->SetId(ID_SEPARATOR);
293
294 GtkWidget *menuItem = gtk_menu_item_new();
295 gtk_menu_append( GTK_MENU(m_menu), menuItem );
296 gtk_widget_show( menuItem );
297 mitem->SetMenuItem(menuItem);
c801d85f 298 m_items.Append( mitem );
6de97a3b 299}
c801d85f 300
debe6624 301void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f
KB
302{
303 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
304 mitem->SetId(id);
305 mitem->SetText(item);
c33c4050 306 mitem->SetHelp(helpStr);
96fd301f
VZ
307 mitem->SetCheckable(checkable);
308 const char *text = mitem->GetText();
309 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
310 : gtk_menu_item_new_with_label(text);
a9c96bcc 311
96fd301f
VZ
312 mitem->SetMenuItem(menuItem);
313
314 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
315 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
316 (gpointer*)this );
317
cf7a7e13
RR
318 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
319 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
320 (gpointer*)this );
321
96fd301f
VZ
322 gtk_menu_append( GTK_MENU(m_menu), menuItem );
323 gtk_widget_show( menuItem );
c801d85f 324 m_items.Append( mitem );
6de97a3b 325}
c801d85f 326
96fd301f 327void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
c801d85f
KB
328{
329 wxMenuItem *mitem = new wxMenuItem();
96fd301f
VZ
330 mitem->SetId(id);
331 mitem->SetText(text);
332
333 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
c33c4050 334 mitem->SetHelp(helpStr);
96fd301f
VZ
335 mitem->SetMenuItem(menuItem);
336 mitem->SetSubMenu(subMenu);
337
338 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
339 gtk_menu_append( GTK_MENU(m_menu), menuItem );
340 gtk_widget_show( menuItem );
c801d85f 341 m_items.Append( mitem );
6de97a3b 342}
c801d85f
KB
343
344int wxMenu::FindItem( const wxString itemString ) const
345{
346 wxString s( itemString );
96fd301f 347
d355d3fe 348 int pos;
c801d85f
KB
349 do {
350 pos = s.First( '&' );
d355d3fe
RR
351 if (pos != -1) s.Remove( pos, 1 );
352 } while (pos != -1);
96fd301f 353
c801d85f
KB
354 wxNode *node = m_items.First();
355 while (node)
356 {
357 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
358 if (item->GetText() == s)
359 return item->GetId();
c801d85f 360 node = node->Next();
6de97a3b 361 }
96fd301f 362
c801d85f 363 return -1;
6de97a3b 364}
c801d85f 365
96fd301f 366void wxMenu::Enable( int id, bool enable )
716b7364 367{
96fd301f
VZ
368 wxMenuItem *item = FindItem(id);
369 if ( item )
370 item->Enable(enable);
6de97a3b 371}
716b7364 372
96fd301f 373bool wxMenu::IsEnabled( int id ) const
e2414cbe 374{
96fd301f
VZ
375 wxMenuItem *item = FindItem(id);
376 if ( item )
377 return item->IsEnabled();
378 else
379 return FALSE;
6de97a3b 380}
e2414cbe 381
96fd301f 382void wxMenu::Check( int id, bool enable )
c801d85f 383{
96fd301f
VZ
384 wxMenuItem *item = FindItem(id);
385 if ( item )
386 item->Check(enable);
6de97a3b 387}
c801d85f 388
96fd301f 389bool wxMenu::IsChecked( int id ) const
c801d85f 390{
96fd301f
VZ
391 wxMenuItem *item = FindItem(id);
392 if ( item )
393 return item->IsChecked();
394 else
395 return FALSE;
6de97a3b 396}
c801d85f 397
debe6624 398void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 399{
96fd301f 400 wxMenuItem *item = FindItem(id);
c33c4050 401 if (item)
96fd301f 402 item->SetText(label);
6de97a3b 403}
96fd301f 404
c33c4050
RR
405wxString wxMenu::GetLabel( int id ) const
406{
407 wxMenuItem *item = FindItem(id);
408 if (item) return item->GetText();
409 return "";
410}
411
412void wxMenu::SetHelpString( int id, const wxString& helpString )
413{
414 wxMenuItem *item = FindItem(id);
415 if (item) item->SetHelp( helpString );
416}
417
418wxString wxMenu::GetHelpString( int id ) const
419{
420 wxMenuItem *item = FindItem(id);
421 if (item) return item->GetHelp();
422 return "";
423}
424
96fd301f
VZ
425int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
426{
c801d85f
KB
427 wxNode *node = m_items.First();
428 while (node)
429 {
430 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
431 if (item->GetMenuItem() == menuItem)
432 return item->GetId();
c801d85f 433 node = node->Next();
6de97a3b 434 }
96fd301f
VZ
435
436 return -1;
6de97a3b 437}
c801d85f 438
96fd301f 439wxMenuItem *wxMenu::FindItem(int id) const
c801d85f
KB
440{
441 wxNode *node = m_items.First();
96fd301f 442 while (node) {
c801d85f 443 wxMenuItem *item = (wxMenuItem*)node->Data();
96fd301f
VZ
444 if ( item->GetId() == id )
445 return item;
c801d85f 446 node = node->Next();
6de97a3b 447 }
96fd301f 448
dc86cb34
RR
449 // Not finding anything here can be correct
450 // when search the entire menu system for
451 // an entry -> no error message.
452
c67daf87 453 return (wxMenuItem *) NULL;
96fd301f 454}
c801d85f
KB
455
456void wxMenu::SetInvokingWindow( wxWindow *win )
457{
458 m_invokingWindow = win;
6de97a3b 459}
c801d85f 460
96fd301f 461wxWindow *wxMenu::GetInvokingWindow()
c801d85f
KB
462{
463 return m_invokingWindow;
6de97a3b 464}
c801d85f
KB
465
466