]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // wxMenuBar | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
24 | ||
25 | wxMenuBar::wxMenuBar() | |
26 | { | |
27 | m_needParent = FALSE; // hmmm | |
28 | ||
29 | PreCreation( NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" ); | |
30 | ||
31 | m_menus.DeleteContents( TRUE ); | |
32 | ||
33 | m_widget = gtk_handle_box_new(); | |
34 | ||
35 | m_menubar = gtk_menu_bar_new(); | |
36 | ||
37 | gtk_container_add( GTK_CONTAINER(m_widget), m_menubar ); | |
38 | ||
39 | gtk_widget_show( m_menubar ); | |
40 | ||
41 | PostCreation(); | |
42 | ||
43 | Show( TRUE ); | |
44 | }; | |
45 | ||
46 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
47 | { | |
48 | m_menus.Append( menu ); | |
49 | menu->m_title = title; // ?????? | |
50 | ||
51 | int pos; | |
52 | do { | |
53 | pos = menu->m_title.First( '&' ); | |
54 | if (pos != -1) menu->m_title.Remove( pos, 1 ); | |
55 | } while (pos != -1); | |
56 | ||
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 ); | |
61 | ||
62 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu ); | |
63 | }; | |
64 | ||
65 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
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(); | |
76 | if (item->IsSubMenu()) | |
77 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
78 | node = node->Next(); | |
79 | }; | |
80 | return -1; | |
81 | }; | |
82 | ||
83 | int 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 | ||
96 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
97 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
98 | { | |
99 | wxMenuItem* result = menu->FindItem(id); | |
100 | ||
101 | wxNode *node = menu->m_items.First(); | |
102 | while ( node && result == NULL ) { | |
103 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
104 | if ( item->IsSubMenu() ) | |
105 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
106 | node = node->Next(); | |
107 | }; | |
108 | ||
109 | return result; | |
110 | }; | |
111 | ||
112 | wxMenuItem* 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 | ||
125 | bool wxMenuBar::IsChecked( int id ) const | |
126 | { | |
127 | wxMenuItem* item = FindMenuItemById( id ); | |
128 | if (item) return item->IsChecked(); | |
129 | return FALSE; | |
130 | } | |
131 | ||
132 | bool wxMenuBar::IsEnabled( int id ) const | |
133 | { | |
134 | wxMenuItem* item = FindMenuItemById( id ); | |
135 | if (item) return item->IsEnabled(); | |
136 | return FALSE; | |
137 | } | |
138 | ||
139 | //----------------------------------------------------------------------------- | |
140 | // wxMenu | |
141 | //----------------------------------------------------------------------------- | |
142 | ||
143 | void gtk_menu_clicked_callback( GtkWidget *widget, gpointer data ) | |
144 | { | |
145 | wxMenu *menu = (wxMenu*)data; | |
146 | int id = menu->FindMenuIdByMenuItem(widget); | |
147 | ||
148 | wxASSERT( id != -1 ); // should find it! | |
149 | ||
150 | if (!menu->IsEnabled(id)) | |
151 | return; | |
152 | ||
153 | wxCommandEvent event( wxEVENT_TYPE_MENU_COMMAND, id ); | |
154 | event.SetEventObject( menu ); | |
155 | event.SetInt(id ); | |
156 | wxWindow *win = menu->GetInvokingWindow(); | |
157 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
158 | }; | |
159 | ||
160 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject) | |
161 | ||
162 | wxMenuItem::wxMenuItem() | |
163 | { | |
164 | m_id = ID_SEPARATOR; | |
165 | m_isCheckMenu = FALSE; | |
166 | m_isChecked = FALSE; | |
167 | m_isEnabled = TRUE; | |
168 | m_subMenu = NULL; | |
169 | m_menuItem = NULL; | |
170 | }; | |
171 | ||
172 | void wxMenuItem::SetText(const wxString& str) | |
173 | { | |
174 | for ( const char *pc = str; *pc != '\0'; pc++ ) { | |
175 | if ( *pc == '&' ) | |
176 | pc++; // skip it | |
177 | ||
178 | m_text << *pc; | |
179 | } | |
180 | } | |
181 | ||
182 | void wxMenuItem::Check( bool check ) | |
183 | { | |
184 | wxCHECK_RET( IsCheckable(), "can't check uncheckable item!" ) | |
185 | ||
186 | m_isChecked = check; | |
187 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
188 | } | |
189 | ||
190 | bool wxMenuItem::IsChecked() const | |
191 | { | |
192 | wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item! | |
193 | ||
194 | bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
195 | ||
196 | wxASSERT( bIsChecked == m_isChecked ); // consistency check | |
197 | ||
198 | return bIsChecked; | |
199 | } | |
200 | ||
201 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
202 | ||
203 | wxMenu::wxMenu( const wxString &title ) | |
204 | { | |
205 | m_title = title; | |
206 | m_items.DeleteContents( TRUE ); | |
207 | m_invokingWindow = NULL; | |
208 | m_menu = gtk_menu_new(); // Do not show! | |
209 | }; | |
210 | ||
211 | void wxMenu::AppendSeparator() | |
212 | { | |
213 | wxMenuItem *mitem = new wxMenuItem(); | |
214 | mitem->SetId(ID_SEPARATOR); | |
215 | ||
216 | GtkWidget *menuItem = gtk_menu_item_new(); | |
217 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
218 | gtk_widget_show( menuItem ); | |
219 | mitem->SetMenuItem(menuItem); | |
220 | m_items.Append( mitem ); | |
221 | }; | |
222 | ||
223 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) | |
224 | { | |
225 | wxMenuItem *mitem = new wxMenuItem(); | |
226 | mitem->SetId(id); | |
227 | mitem->SetText(item); | |
228 | mitem->SetHelpString(helpStr); | |
229 | mitem->SetCheckable(checkable); | |
230 | const char *text = mitem->GetText(); | |
231 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text) | |
232 | : gtk_menu_item_new_with_label(text); | |
233 | mitem->SetMenuItem(menuItem); | |
234 | ||
235 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
236 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
237 | (gpointer*)this ); | |
238 | ||
239 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
240 | gtk_widget_show( menuItem ); | |
241 | m_items.Append( mitem ); | |
242 | }; | |
243 | ||
244 | void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr ) | |
245 | { | |
246 | wxMenuItem *mitem = new wxMenuItem(); | |
247 | mitem->SetId(id); | |
248 | mitem->SetText(text); | |
249 | ||
250 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText()); | |
251 | mitem->SetHelpString(helpStr); | |
252 | mitem->SetMenuItem(menuItem); | |
253 | mitem->SetSubMenu(subMenu); | |
254 | ||
255 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); | |
256 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
257 | gtk_widget_show( menuItem ); | |
258 | m_items.Append( mitem ); | |
259 | }; | |
260 | ||
261 | int wxMenu::FindItem( const wxString itemString ) const | |
262 | { | |
263 | wxString s( itemString ); | |
264 | ||
265 | int pos; | |
266 | do { | |
267 | pos = s.First( '&' ); | |
268 | if (pos != -1) s.Remove( pos, 1 ); | |
269 | } while (pos != -1); | |
270 | ||
271 | wxNode *node = m_items.First(); | |
272 | while (node) | |
273 | { | |
274 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
275 | if (item->GetText() == s) | |
276 | return item->GetId(); | |
277 | node = node->Next(); | |
278 | }; | |
279 | ||
280 | return -1; | |
281 | }; | |
282 | ||
283 | void wxMenu::Enable( int id, bool enable ) | |
284 | { | |
285 | wxMenuItem *item = FindItem(id); | |
286 | if ( item ) | |
287 | item->Enable(enable); | |
288 | }; | |
289 | ||
290 | bool wxMenu::IsEnabled( int id ) const | |
291 | { | |
292 | wxMenuItem *item = FindItem(id); | |
293 | if ( item ) | |
294 | return item->IsEnabled(); | |
295 | else | |
296 | return FALSE; | |
297 | }; | |
298 | ||
299 | void wxMenu::Check( int id, bool enable ) | |
300 | { | |
301 | wxMenuItem *item = FindItem(id); | |
302 | if ( item ) | |
303 | item->Check(enable); | |
304 | }; | |
305 | ||
306 | bool wxMenu::IsChecked( int id ) const | |
307 | { | |
308 | wxMenuItem *item = FindItem(id); | |
309 | if ( item ) | |
310 | return item->IsChecked(); | |
311 | else | |
312 | return FALSE; | |
313 | }; | |
314 | ||
315 | void wxMenu::SetLabel( int id, const wxString &label ) | |
316 | { | |
317 | wxMenuItem *item = FindItem(id); | |
318 | if ( item ) | |
319 | item->SetText(label); | |
320 | }; | |
321 | ||
322 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
323 | { | |
324 | wxNode *node = m_items.First(); | |
325 | while (node) | |
326 | { | |
327 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
328 | if (item->GetMenuItem() == menuItem) | |
329 | return item->GetId(); | |
330 | node = node->Next(); | |
331 | }; | |
332 | ||
333 | return -1; | |
334 | }; | |
335 | ||
336 | wxMenuItem *wxMenu::FindItem(int id) const | |
337 | { | |
338 | wxNode *node = m_items.First(); | |
339 | while (node) { | |
340 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
341 | if ( item->GetId() == id ) | |
342 | return item; | |
343 | node = node->Next(); | |
344 | }; | |
345 | ||
346 | wxLogDebug("wxMenu::FindItem: item %d not found.", id); | |
347 | ||
348 | return NULL; | |
349 | } | |
350 | ||
351 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
352 | { | |
353 | m_invokingWindow = win; | |
354 | }; | |
355 | ||
356 | wxWindow *wxMenu::GetInvokingWindow() | |
357 | { | |
358 | return m_invokingWindow; | |
359 | }; | |
360 | ||
361 |