]>
Commit | Line | Data |
---|---|---|
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" |
06cfab17 | 18 | #include "wx/app.h" |
c801d85f | 19 | |
974e8d94 VZ |
20 | #if wxUSE_ACCEL |
21 | #include "wx/accel.h" | |
22 | #endif // wxUSE_ACCEL | |
23 | ||
83624f79 RR |
24 | #include "gdk/gdk.h" |
25 | #include "gtk/gtk.h" | |
26 | ||
acfd422a RR |
27 | //----------------------------------------------------------------------------- |
28 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
c801d85f KB |
34 | //----------------------------------------------------------------------------- |
35 | // wxMenuBar | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
39 | ||
3502e687 RR |
40 | wxMenuBar::wxMenuBar( long style ) |
41 | { | |
1e133b7d | 42 | /* the parent window is known after wxFrame::SetMenu() */ |
23280650 | 43 | m_needParent = FALSE; |
ae53c98c | 44 | m_style = style; |
9c884972 | 45 | m_invokingWindow = (wxWindow*) NULL; |
23280650 | 46 | |
4dcaf11a | 47 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
223d09f6 | 48 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) |
4dcaf11a | 49 | { |
223d09f6 | 50 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 51 | return; |
4dcaf11a | 52 | } |
3502e687 RR |
53 | |
54 | m_menus.DeleteContents( TRUE ); | |
55 | ||
1e133b7d RR |
56 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
57 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
58 | m_accel = gtk_accel_group_new(); | |
59 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
60 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 61 | #else |
3502e687 | 62 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 63 | #endif |
3502e687 RR |
64 | |
65 | if (style & wxMB_DOCKABLE) | |
66 | { | |
67 | m_widget = gtk_handle_box_new(); | |
c626a8b7 VZ |
68 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
69 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
3502e687 RR |
70 | } |
71 | else | |
72 | { | |
73 | m_widget = GTK_WIDGET(m_menubar); | |
74 | } | |
75 | ||
76 | PostCreation(); | |
3502e687 RR |
77 | } |
78 | ||
96fd301f | 79 | wxMenuBar::wxMenuBar() |
c801d85f | 80 | { |
1e133b7d RR |
81 | /* the parent window is known after wxFrame::SetMenu() */ |
82 | m_needParent = FALSE; | |
ae53c98c | 83 | m_style = 0; |
9c884972 | 84 | m_invokingWindow = (wxWindow*) NULL; |
23280650 | 85 | |
4dcaf11a | 86 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
223d09f6 | 87 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") )) |
4dcaf11a | 88 | { |
223d09f6 | 89 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 90 | return; |
4dcaf11a | 91 | } |
974e8d94 | 92 | |
83624f79 | 93 | m_menus.DeleteContents( TRUE ); |
96fd301f | 94 | |
1e133b7d RR |
95 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
96 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
97 | m_accel = gtk_accel_group_new(); | |
98 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); | |
99 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 100 | #else |
83624f79 | 101 | m_menubar = gtk_menu_bar_new(); |
1e133b7d | 102 | #endif |
8bbe427f | 103 | |
828f655f | 104 | m_widget = GTK_WIDGET(m_menubar); |
96fd301f | 105 | |
83624f79 | 106 | PostCreation(); |
6de97a3b | 107 | } |
c801d85f | 108 | |
1e133b7d RR |
109 | wxMenuBar::~wxMenuBar() |
110 | { | |
d1b15f03 | 111 | // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ? |
1e133b7d RR |
112 | } |
113 | ||
5bd9e519 RR |
114 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
115 | { | |
116 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
117 | ||
118 | #if (GTK_MINOR_VERSION > 0) | |
119 | wxWindow *top_frame = win; | |
974e8d94 | 120 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) |
bd77da97 | 121 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
122 | |
123 | /* support for native hot keys */ | |
124 | gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
125 | #endif | |
126 | ||
127 | wxNode *node = menu->GetItems().First(); | |
128 | while (node) | |
129 | { | |
130 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
131 | if (menuitem->IsSubMenu()) | |
132 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
133 | node = node->Next(); | |
134 | } | |
135 | } | |
136 | ||
137 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
138 | { | |
139 | menu->SetInvokingWindow( win ); | |
140 | ||
141 | #if (GTK_MINOR_VERSION > 0) | |
142 | wxWindow *top_frame = win; | |
974e8d94 | 143 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) |
bd77da97 | 144 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
145 | |
146 | /* support for native hot keys */ | |
147 | gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
148 | #endif | |
149 | ||
150 | wxNode *node = menu->GetItems().First(); | |
151 | while (node) | |
152 | { | |
153 | wxMenuItem *menuitem = (wxMenuItem*)node->Data(); | |
154 | if (menuitem->IsSubMenu()) | |
155 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
156 | node = node->Next(); | |
157 | } | |
158 | } | |
159 | ||
160 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
161 | { | |
9c884972 | 162 | m_invokingWindow = win; |
5bd9e519 RR |
163 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
164 | wxWindow *top_frame = win; | |
974e8d94 | 165 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) |
bd77da97 | 166 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
167 | |
168 | /* support for native key accelerators indicated by underscroes */ | |
169 | gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
170 | #endif | |
171 | ||
172 | wxNode *node = m_menus.First(); | |
173 | while (node) | |
174 | { | |
175 | wxMenu *menu = (wxMenu*)node->Data(); | |
176 | wxMenubarSetInvokingWindow( menu, win ); | |
177 | node = node->Next(); | |
178 | } | |
179 | } | |
180 | ||
181 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
182 | { | |
9c884972 | 183 | m_invokingWindow = (wxWindow*) NULL; |
5bd9e519 RR |
184 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
185 | wxWindow *top_frame = win; | |
974e8d94 | 186 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) |
bd77da97 | 187 | top_frame = top_frame->GetParent(); |
5bd9e519 RR |
188 | |
189 | /* support for native key accelerators indicated by underscroes */ | |
190 | gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) ); | |
191 | #endif | |
192 | ||
193 | wxNode *node = m_menus.First(); | |
194 | while (node) | |
195 | { | |
196 | wxMenu *menu = (wxMenu*)node->Data(); | |
197 | wxMenubarUnsetInvokingWindow( menu, win ); | |
198 | node = node->Next(); | |
199 | } | |
200 | } | |
201 | ||
c801d85f KB |
202 | void wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
203 | { | |
83624f79 | 204 | m_menus.Append( menu ); |
23280650 | 205 | |
a533f5c1 | 206 | const wxChar *pc; |
23280650 | 207 | |
1e133b7d RR |
208 | /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ |
209 | wxString str; | |
223d09f6 | 210 | for ( pc = title; *pc != wxT('\0'); pc++ ) |
83624f79 | 211 | { |
223d09f6 | 212 | if (*pc == wxT('&')) |
23280650 | 213 | { |
1e133b7d | 214 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
223d09f6 | 215 | str << wxT('_'); |
455fadaa | 216 | } |
223d09f6 | 217 | else if (*pc == wxT('/')) |
23280650 | 218 | { |
223d09f6 | 219 | str << wxT('\\'); |
034be888 | 220 | #endif |
23280650 | 221 | } |
d38ceae8 | 222 | else |
455fadaa | 223 | { |
90a53a3a | 224 | #if __WXGTK12__ |
223d09f6 | 225 | if ( *pc == wxT('_') ) |
455fadaa VZ |
226 | { |
227 | // underscores must be doubled to prevent them from being | |
228 | // interpreted as accelerator character prefix by GTK | |
229 | str << *pc; | |
230 | } | |
90a53a3a | 231 | #endif // GTK+ 1.2 |
455fadaa VZ |
232 | |
233 | str << *pc; | |
234 | } | |
1e133b7d RR |
235 | } |
236 | ||
237 | /* this doesn't have much effect right now */ | |
238 | menu->SetTitle( str ); | |
23280650 | 239 | |
1e133b7d RR |
240 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
241 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) | |
242 | ||
243 | /* local buffer in multibyte form */ | |
2b2edbed | 244 | wxString buf; |
223d09f6 | 245 | buf << wxT('/') << str.c_str(); |
c980c992 | 246 | |
dc6c62a9 | 247 | char *cbuf = new char[buf.Length()+1]; |
c980c992 GL |
248 | strcpy(cbuf, buf.mbc_str()); |
249 | ||
1e133b7d | 250 | GtkItemFactoryEntry entry; |
c980c992 | 251 | entry.path = (gchar *)cbuf; // const_cast |
1e133b7d RR |
252 | entry.accelerator = (gchar*) NULL; |
253 | entry.callback = (GtkItemFactoryCallback) NULL; | |
254 | entry.callback_action = 0; | |
2b2edbed KB |
255 | entry.item_type = "<Branch>"; |
256 | ||
1e133b7d | 257 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
1e133b7d | 258 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
223d09f6 KB |
259 | wxString tmp = wxT("<main>/"); |
260 | for ( pc = str; *pc != wxT('\0'); pc++ ) | |
1e133b7d | 261 | { |
455fadaa VZ |
262 | // contrary to the common sense, we must throw out _all_ underscores, |
263 | // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we | |
974e8d94 | 264 | // might naively think). IMHO it's a bug in GTK+ (VZ) |
223d09f6 | 265 | while (*pc == wxT('_')) |
455fadaa | 266 | pc++; |
2b2edbed | 267 | tmp << *pc; |
034be888 | 268 | } |
1e133b7d | 269 | menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() ); |
1e133b7d | 270 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
2b2edbed | 271 | delete [] cbuf; |
1e133b7d | 272 | #else |
96fd301f | 273 | |
1e133b7d | 274 | menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() ); |
2b1c162e RR |
275 | gtk_widget_show( menu->m_owner ); |
276 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
96fd301f | 277 | |
2b1c162e | 278 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
23280650 | 279 | |
1e133b7d | 280 | #endif |
9c884972 RR |
281 | |
282 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables | |
283 | // adding menu later on. | |
284 | if (m_invokingWindow) | |
285 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
6de97a3b | 286 | } |
96fd301f | 287 | |
716b7364 | 288 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 289 | { |
c626a8b7 | 290 | if (menu->GetTitle() == menuString) |
83624f79 RR |
291 | { |
292 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
293 | if (res != wxNOT_FOUND) |
294 | return res; | |
83624f79 | 295 | } |
c626a8b7 VZ |
296 | |
297 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast | |
83624f79 RR |
298 | while (node) |
299 | { | |
300 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
301 | if (item->IsSubMenu()) | |
302 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 303 | |
83624f79 RR |
304 | node = node->Next(); |
305 | } | |
306 | ||
c626a8b7 VZ |
307 | return wxNOT_FOUND; |
308 | } | |
309 | ||
80ec928c | 310 | wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem ) const |
c626a8b7 VZ |
311 | { |
312 | if ( menuForItem ) | |
313 | { | |
314 | // TODO return the pointer to the menu | |
315 | ||
316 | *menuForItem = NULL; | |
317 | } | |
318 | ||
319 | return FindItem(itemId); | |
6de97a3b | 320 | } |
c801d85f KB |
321 | |
322 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
323 | { | |
83624f79 RR |
324 | wxNode *node = m_menus.First(); |
325 | while (node) | |
326 | { | |
327 | wxMenu *menu = (wxMenu*)node->Data(); | |
328 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
329 | if (res != -1) return res; | |
330 | node = node->Next(); | |
331 | } | |
332 | return -1; | |
6de97a3b | 333 | } |
c801d85f | 334 | |
c626a8b7 | 335 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 336 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 337 | { |
83624f79 | 338 | wxMenuItem* result = menu->FindItem(id); |
716b7364 | 339 | |
c626a8b7 VZ |
340 | wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast |
341 | while ( node && result == NULL ) | |
83624f79 RR |
342 | { |
343 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
344 | if (item->IsSubMenu()) | |
c626a8b7 | 345 | { |
83624f79 | 346 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 347 | } |
83624f79 RR |
348 | node = node->Next(); |
349 | } | |
96fd301f | 350 | |
83624f79 | 351 | return result; |
6de97a3b | 352 | } |
716b7364 | 353 | |
c626a8b7 | 354 | wxMenuItem* wxMenuBar::FindItem( int id ) const |
716b7364 | 355 | { |
83624f79 RR |
356 | wxMenuItem* result = 0; |
357 | wxNode *node = m_menus.First(); | |
358 | while (node && result == 0) | |
359 | { | |
360 | wxMenu *menu = (wxMenu*)node->Data(); | |
361 | result = FindMenuItemByIdRecursive( menu, id ); | |
362 | node = node->Next(); | |
363 | } | |
c626a8b7 | 364 | |
83624f79 | 365 | return result; |
716b7364 RR |
366 | } |
367 | ||
54ff4a70 RR |
368 | void wxMenuBar::Check( int id, bool check ) |
369 | { | |
83624f79 | 370 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 371 | |
223d09f6 | 372 | wxCHECK_RET( item, wxT("wxMenuBar::Check: no such item") ); |
c626a8b7 VZ |
373 | |
374 | item->Check(check); | |
6de97a3b | 375 | } |
54ff4a70 | 376 | |
c626a8b7 | 377 | bool wxMenuBar::IsChecked( int id ) const |
716b7364 | 378 | { |
83624f79 | 379 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 380 | |
223d09f6 | 381 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked: no such item") ); |
c626a8b7 VZ |
382 | |
383 | return item->IsChecked(); | |
6de97a3b | 384 | } |
716b7364 | 385 | |
54ff4a70 RR |
386 | void wxMenuBar::Enable( int id, bool enable ) |
387 | { | |
83624f79 | 388 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 389 | |
223d09f6 | 390 | wxCHECK_RET( item, wxT("wxMenuBar::Enable: no such item") ); |
c626a8b7 VZ |
391 | |
392 | item->Enable(enable); | |
6de97a3b | 393 | } |
54ff4a70 | 394 | |
c626a8b7 | 395 | bool wxMenuBar::IsEnabled( int id ) const |
716b7364 | 396 | { |
83624f79 | 397 | wxMenuItem* item = FindMenuItemById( id ); |
c626a8b7 | 398 | |
223d09f6 | 399 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled: no such item") ); |
c626a8b7 VZ |
400 | |
401 | return item->IsEnabled(); | |
6de97a3b | 402 | } |
716b7364 | 403 | |
bbe0af5b RR |
404 | wxString wxMenuBar::GetLabel( int id ) const |
405 | { | |
406 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 407 | |
223d09f6 | 408 | wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel: no such item") ); |
c626a8b7 VZ |
409 | |
410 | return item->GetText(); | |
bbe0af5b RR |
411 | } |
412 | ||
413 | void wxMenuBar::SetLabel( int id, const wxString &label ) | |
414 | { | |
415 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 416 | |
223d09f6 | 417 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel: no such item") ); |
c626a8b7 VZ |
418 | |
419 | item->SetText( label ); | |
bbe0af5b RR |
420 | } |
421 | ||
2b1c162e | 422 | void wxMenuBar::EnableTop( int pos, bool flag ) |
bbe0af5b | 423 | { |
2b1c162e | 424 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 425 | |
223d09f6 | 426 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 427 | |
2b1c162e | 428 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 VZ |
429 | |
430 | if (menu->m_owner) | |
431 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
432 | } |
433 | ||
2b1c162e | 434 | wxString wxMenuBar::GetLabelTop( int pos ) const |
bbe0af5b | 435 | { |
2b1c162e | 436 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 437 | |
223d09f6 | 438 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
c626a8b7 | 439 | |
2b1c162e | 440 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 441 | |
2b1c162e | 442 | return menu->GetTitle(); |
bbe0af5b RR |
443 | } |
444 | ||
2b1c162e | 445 | void wxMenuBar::SetLabelTop( int pos, const wxString& label ) |
bbe0af5b | 446 | { |
2b1c162e | 447 | wxNode *node = m_menus.Nth( pos ); |
c626a8b7 | 448 | |
223d09f6 | 449 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 450 | |
2b1c162e | 451 | wxMenu* menu = (wxMenu*)node->Data(); |
c626a8b7 | 452 | |
2b1c162e | 453 | menu->SetTitle( label ); |
bbe0af5b RR |
454 | } |
455 | ||
342b6a2f RR |
456 | void wxMenuBar::SetHelpString( int id, const wxString& helpString ) |
457 | { | |
458 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 459 | |
223d09f6 | 460 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString: no such item") ); |
c626a8b7 VZ |
461 | |
462 | item->SetHelp( helpString ); | |
342b6a2f RR |
463 | } |
464 | ||
465 | wxString wxMenuBar::GetHelpString( int id ) const | |
466 | { | |
467 | wxMenuItem* item = FindMenuItemById( id ); | |
c626a8b7 | 468 | |
223d09f6 | 469 | wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString: no such item") ); |
c626a8b7 VZ |
470 | |
471 | return item->GetHelp(); | |
342b6a2f RR |
472 | } |
473 | ||
c801d85f | 474 | //----------------------------------------------------------------------------- |
cf7a7e13 | 475 | // "activate" |
c801d85f KB |
476 | //----------------------------------------------------------------------------- |
477 | ||
6de97a3b | 478 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
c801d85f | 479 | { |
acfd422a RR |
480 | if (g_isIdle) wxapp_install_idle_handler(); |
481 | ||
83624f79 | 482 | int id = menu->FindMenuIdByMenuItem(widget); |
96fd301f | 483 | |
83624f79 | 484 | /* should find it for normal (not popup) menu */ |
c626a8b7 | 485 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); |
96fd301f | 486 | |
c626a8b7 VZ |
487 | if (!menu->IsEnabled(id)) |
488 | return; | |
96fd301f | 489 | |
2d17d68f | 490 | wxMenuItem* item = menu->FindItem( id ); |
223d09f6 | 491 | wxCHECK_RET( item, wxT("error in menu item callback") ); |
c626a8b7 VZ |
492 | |
493 | if (item->IsCheckable()) | |
2d17d68f | 494 | { |
974e8d94 VZ |
495 | bool isReallyChecked = item->IsChecked(); |
496 | if ( item->wxMenuItemBase::IsChecked() == isReallyChecked ) | |
2d17d68f | 497 | { |
c626a8b7 | 498 | /* the menu item has been checked by calling wxMenuItem->Check() */ |
2d17d68f | 499 | return; |
c626a8b7 VZ |
500 | } |
501 | else | |
502 | { | |
974e8d94 VZ |
503 | /* the user pressed on the menu item -> report and make consistent |
504 | * again */ | |
505 | item->wxMenuItemBase::Check(isReallyChecked); | |
c626a8b7 | 506 | } |
2d17d68f RR |
507 | } |
508 | ||
83624f79 RR |
509 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
510 | event.SetEventObject( menu ); | |
511 | event.SetInt(id ); | |
8bbe427f | 512 | |
c626a8b7 | 513 | if (menu->GetCallback()) |
83624f79 | 514 | { |
c626a8b7 | 515 | (void) (*(menu->GetCallback())) (*menu, event); |
83624f79 RR |
516 | return; |
517 | } | |
cf7a7e13 | 518 | |
c626a8b7 VZ |
519 | if (menu->GetEventHandler()->ProcessEvent(event)) |
520 | return; | |
cf7a7e13 | 521 | |
83624f79 | 522 | wxWindow *win = menu->GetInvokingWindow(); |
c626a8b7 VZ |
523 | if (win) |
524 | win->GetEventHandler()->ProcessEvent( event ); | |
cf7a7e13 RR |
525 | } |
526 | ||
527 | //----------------------------------------------------------------------------- | |
528 | // "select" | |
529 | //----------------------------------------------------------------------------- | |
530 | ||
531 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
532 | { | |
acfd422a RR |
533 | if (g_isIdle) wxapp_install_idle_handler(); |
534 | ||
83624f79 RR |
535 | int id = menu->FindMenuIdByMenuItem(widget); |
536 | ||
537 | wxASSERT( id != -1 ); // should find it! | |
cf7a7e13 | 538 | |
c626a8b7 VZ |
539 | if (!menu->IsEnabled(id)) |
540 | return; | |
cf7a7e13 | 541 | |
342b6a2f | 542 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
83624f79 | 543 | event.SetEventObject( menu ); |
cf7a7e13 | 544 | |
c626a8b7 VZ |
545 | if (menu->GetEventHandler()->ProcessEvent(event)) |
546 | return; | |
6de97a3b | 547 | |
83624f79 RR |
548 | wxWindow *win = menu->GetInvokingWindow(); |
549 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 550 | } |
c801d85f | 551 | |
cd743a6f RR |
552 | //----------------------------------------------------------------------------- |
553 | // "deselect" | |
554 | //----------------------------------------------------------------------------- | |
555 | ||
556 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
557 | { | |
acfd422a RR |
558 | if (g_isIdle) wxapp_install_idle_handler(); |
559 | ||
cd743a6f RR |
560 | int id = menu->FindMenuIdByMenuItem(widget); |
561 | ||
562 | wxASSERT( id != -1 ); // should find it! | |
563 | ||
c626a8b7 VZ |
564 | if (!menu->IsEnabled(id)) |
565 | return; | |
cd743a6f RR |
566 | |
567 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
568 | event.SetEventObject( menu ); | |
569 | ||
c626a8b7 VZ |
570 | if (menu->GetEventHandler()->ProcessEvent(event)) |
571 | return; | |
cd743a6f RR |
572 | |
573 | wxWindow *win = menu->GetInvokingWindow(); | |
c626a8b7 VZ |
574 | if (win) |
575 | win->GetEventHandler()->ProcessEvent( event ); | |
cd743a6f RR |
576 | } |
577 | ||
cf7a7e13 | 578 | //----------------------------------------------------------------------------- |
db1b4961 | 579 | // wxMenuItem |
cf7a7e13 RR |
580 | //----------------------------------------------------------------------------- |
581 | ||
974e8d94 VZ |
582 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase) |
583 | ||
584 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
585 | int id, | |
586 | const wxString& name, | |
587 | const wxString& help, | |
588 | bool isCheckable, | |
589 | wxMenu *subMenu) | |
590 | { | |
591 | return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu); | |
592 | } | |
96fd301f | 593 | |
974e8d94 VZ |
594 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
595 | int id, | |
596 | const wxString& text, | |
597 | const wxString& help, | |
598 | bool isCheckable, | |
599 | wxMenu *subMenu) | |
c801d85f | 600 | { |
974e8d94 VZ |
601 | m_id = id; |
602 | m_isCheckable = isCheckable; | |
83624f79 RR |
603 | m_isChecked = FALSE; |
604 | m_isEnabled = TRUE; | |
974e8d94 VZ |
605 | m_subMenu = subMenu; |
606 | m_parentMenu = parentMenu; | |
607 | m_help = help; | |
608 | ||
83624f79 | 609 | m_menuItem = (GtkWidget *) NULL; |
974e8d94 VZ |
610 | |
611 | // call it after initializing m_menuItem to NULL | |
612 | DoSetText(text); | |
6de97a3b | 613 | } |
c801d85f | 614 | |
d1b15f03 RR |
615 | wxMenuItem::~wxMenuItem() |
616 | { | |
617 | // don't delete menu items, the menus take care of that | |
618 | } | |
619 | ||
c626a8b7 | 620 | // it's valid for this function to be called even if m_menuItem == NULL |
974e8d94 | 621 | void wxMenuItem::DoSetText( const wxString& str ) |
716b7364 | 622 | { |
ab46dc18 | 623 | /* '\t' is the deliminator indicating a hot key */ |
974e8d94 | 624 | m_text.Empty(); |
ab46dc18 | 625 | const wxChar *pc = str; |
223d09f6 | 626 | for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ ) |
83624f79 | 627 | { |
223d09f6 | 628 | if (*pc == wxT('&')) |
23280650 | 629 | { |
034be888 | 630 | #if (GTK_MINOR_VERSION > 0) |
223d09f6 | 631 | m_text << wxT('_'); |
572d7461 | 632 | } |
223d09f6 | 633 | else if ( *pc == wxT('_') ) // escape underscores |
572d7461 | 634 | { |
223d09f6 | 635 | m_text << wxT("__"); |
572d7461 | 636 | } |
223d09f6 | 637 | else if (*pc == wxT('/')) /* we have to filter out slashes ... */ |
23280650 | 638 | { |
223d09f6 | 639 | m_text << wxT('\\'); /* ... and replace them with back slashes */ |
034be888 RR |
640 | #endif |
641 | } | |
d38ceae8 KB |
642 | else |
643 | m_text << *pc; | |
83624f79 | 644 | } |
23280650 | 645 | |
837904f2 | 646 | /* only GTK 1.2 knows about hot keys */ |
223d09f6 | 647 | m_hotKey = wxT(""); |
ab46dc18 | 648 | #if (GTK_MINOR_VERSION > 0) |
223d09f6 | 649 | if(*pc == wxT('\t')) |
d7dbc98a KB |
650 | { |
651 | pc++; | |
652 | m_hotKey = pc; | |
653 | } | |
ab46dc18 | 654 | #endif |
96fd301f | 655 | |
83624f79 RR |
656 | if (m_menuItem) |
657 | { | |
658 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
1e133b7d | 659 | gtk_label_set( label, m_text.mb_str()); |
83624f79 | 660 | } |
716b7364 RR |
661 | } |
662 | ||
96fd301f | 663 | void wxMenuItem::Check( bool check ) |
716b7364 | 664 | { |
223d09f6 | 665 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 666 | |
223d09f6 | 667 | wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") ) |
96fd301f | 668 | |
974e8d94 VZ |
669 | if (check == m_isChecked) |
670 | return; | |
2d17d68f | 671 | |
974e8d94 | 672 | wxMenuItemBase::Check( check ); |
83624f79 | 673 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); |
716b7364 RR |
674 | } |
675 | ||
8bbe427f VZ |
676 | void wxMenuItem::Enable( bool enable ) |
677 | { | |
223d09f6 | 678 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 679 | |
83624f79 | 680 | gtk_widget_set_sensitive( m_menuItem, enable ); |
974e8d94 | 681 | wxMenuItemBase::Enable( enable ); |
a9c96bcc RR |
682 | } |
683 | ||
96fd301f | 684 | bool wxMenuItem::IsChecked() const |
716b7364 | 685 | { |
223d09f6 | 686 | wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") ); |
db1b4961 | 687 | |
974e8d94 VZ |
688 | wxCHECK_MSG( IsCheckable(), FALSE, |
689 | wxT("can't get state of uncheckable item!") ); | |
96fd301f | 690 | |
974e8d94 | 691 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
716b7364 RR |
692 | } |
693 | ||
db1b4961 | 694 | //----------------------------------------------------------------------------- |
83624f79 | 695 | // wxMenu |
db1b4961 RR |
696 | //----------------------------------------------------------------------------- |
697 | ||
c801d85f KB |
698 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
699 | ||
ae53c98c | 700 | void |
b908d224 | 701 | wxMenu::Init( const wxString& title, |
33961d59 | 702 | long style, |
455fadaa VZ |
703 | const wxFunction func |
704 | ) | |
c801d85f | 705 | { |
83624f79 RR |
706 | m_title = title; |
707 | m_items.DeleteContents( TRUE ); | |
708 | m_invokingWindow = (wxWindow *) NULL; | |
ae53c98c | 709 | m_style = style; |
23280650 | 710 | |
034be888 RR |
711 | #if (GTK_MINOR_VERSION > 0) |
712 | m_accel = gtk_accel_group_new(); | |
713 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); | |
714 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); | |
23280650 | 715 | #else |
83624f79 | 716 | m_menu = gtk_menu_new(); // Do not show! |
034be888 | 717 | #endif |
8bbe427f | 718 | |
83624f79 | 719 | m_callback = func; |
b908d224 | 720 | |
83624f79 RR |
721 | m_eventHandler = this; |
722 | m_clientData = (void*) NULL; | |
8bbe427f | 723 | |
223d09f6 KB |
724 | if (m_title.IsNull()) m_title = wxT(""); |
725 | if (m_title != wxT("")) | |
83624f79 RR |
726 | { |
727 | Append(-2, m_title); | |
728 | AppendSeparator(); | |
729 | } | |
c626a8b7 | 730 | |
2b1c162e | 731 | m_owner = (GtkWidget*) NULL; |
2b2edbed KB |
732 | |
733 | #if (GTK_MINOR_VERSION > 0) | |
734 | /* Tearoffs are entries, just like separators. So if we want this | |
735 | menu to be a tear-off one, we just append a tearoff entry | |
736 | immediately. */ | |
737 | if(m_style & wxMENU_TEAROFF) | |
738 | { | |
739 | GtkItemFactoryEntry entry; | |
740 | entry.path = "/tearoff"; | |
741 | entry.callback = (GtkItemFactoryCallback) NULL; | |
742 | entry.callback_action = 0; | |
743 | entry.item_type = "<Tearoff>"; | |
744 | entry.accelerator = (gchar*) NULL; | |
745 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ | |
23280650 | 746 | //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" ); |
2b2edbed KB |
747 | } |
748 | #endif | |
6de97a3b | 749 | } |
c801d85f | 750 | |
034be888 RR |
751 | wxMenu::~wxMenu() |
752 | { | |
d1b15f03 RR |
753 | wxNode *node = m_items.First(); |
754 | while (node) | |
755 | { | |
756 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
974e8d94 VZ |
757 | wxMenu *submenu = item->GetSubMenu(); |
758 | if (submenu) | |
759 | delete submenu; | |
d1b15f03 RR |
760 | node = node->Next(); |
761 | } | |
15a2076a | 762 | |
d1b15f03 | 763 | gtk_widget_destroy( m_menu ); |
974e8d94 | 764 | |
d1b15f03 | 765 | gtk_object_unref( GTK_OBJECT(m_factory) ); |
034be888 RR |
766 | } |
767 | ||
c2dd8380 GL |
768 | void wxMenu::SetTitle( const wxString& title ) |
769 | { | |
c626a8b7 | 770 | // TODO Waiting for something better |
83624f79 | 771 | m_title = title; |
c2dd8380 GL |
772 | } |
773 | ||
774 | const wxString wxMenu::GetTitle() const | |
775 | { | |
83624f79 | 776 | return m_title; |
c2dd8380 GL |
777 | } |
778 | ||
96fd301f | 779 | void wxMenu::AppendSeparator() |
c801d85f | 780 | { |
974e8d94 | 781 | wxMenuItem *mitem = new wxMenuItem(this, wxID_SEPARATOR); |
96fd301f | 782 | |
08fc1744 RR |
783 | #if (GTK_MINOR_VERSION > 0) |
784 | GtkItemFactoryEntry entry; | |
785 | entry.path = "/sep"; | |
786 | entry.callback = (GtkItemFactoryCallback) NULL; | |
787 | entry.callback_action = 0; | |
788 | entry.item_type = "<Separator>"; | |
789 | entry.accelerator = (gchar*) NULL; | |
23280650 | 790 | |
08fc1744 | 791 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 792 | |
08fc1744 RR |
793 | /* this will be wrong for more than one separator. do we care? */ |
794 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" ); | |
795 | #else | |
83624f79 RR |
796 | GtkWidget *menuItem = gtk_menu_item_new(); |
797 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); | |
798 | gtk_widget_show( menuItem ); | |
08fc1744 | 799 | #endif |
23280650 | 800 | |
83624f79 RR |
801 | mitem->SetMenuItem(menuItem); |
802 | m_items.Append( mitem ); | |
6de97a3b | 803 | } |
c801d85f | 804 | |
974e8d94 VZ |
805 | #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL |
806 | static wxString GetHotKey( const wxMenuItem& item ) | |
837904f2 | 807 | { |
974e8d94 | 808 | wxString hotkey; |
23280650 | 809 | |
974e8d94 VZ |
810 | // as wxGetAccelFromString() looks for TAB, insert a dummy one here |
811 | wxString label; | |
812 | label << wxT('\t') << item.GetHotKey(); | |
50592885 VZ |
813 | |
814 | // but if the hotkey is empty don't do anything | |
815 | if ( label.length() > 1 ) | |
837904f2 | 816 | { |
50592885 VZ |
817 | wxAcceleratorEntry *accel = wxGetAccelFromString(label); |
818 | if ( accel ) | |
23280650 | 819 | { |
50592885 VZ |
820 | int flags = accel->GetFlags(); |
821 | if ( flags & wxACCEL_ALT ) | |
822 | hotkey += wxT("<alt>"); | |
823 | if ( flags & wxACCEL_CTRL ) | |
824 | hotkey += wxT("<control>"); | |
825 | if ( flags & wxACCEL_SHIFT ) | |
826 | hotkey += wxT("<shift>"); | |
827 | ||
828 | int code = accel->GetKeyCode(); | |
829 | switch ( code ) | |
830 | { | |
831 | case WXK_F1: | |
832 | case WXK_F2: | |
833 | case WXK_F3: | |
834 | case WXK_F4: | |
835 | case WXK_F5: | |
836 | case WXK_F6: | |
837 | case WXK_F7: | |
838 | case WXK_F8: | |
839 | case WXK_F9: | |
840 | case WXK_F10: | |
841 | case WXK_F11: | |
842 | case WXK_F12: | |
843 | hotkey << wxT('F') << code = WXK_F1 + 1; | |
974e8d94 | 844 | break; |
974e8d94 | 845 | |
50592885 VZ |
846 | // if there are any other keys wxGetAccelFromString() may return, |
847 | // we should process them here | |
848 | ||
849 | default: | |
850 | if ( wxIsalnum(code) ) | |
851 | { | |
852 | hotkey << (wxChar)code; | |
853 | ||
854 | break; | |
855 | } | |
856 | ||
857 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
858 | } | |
859 | ||
860 | delete accel; | |
23280650 | 861 | } |
837904f2 | 862 | } |
974e8d94 VZ |
863 | |
864 | return hotkey; | |
837904f2 | 865 | } |
974e8d94 | 866 | #endif // wxUSE_ACCEL |
837904f2 | 867 | |
debe6624 | 868 | void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable ) |
c801d85f | 869 | { |
974e8d94 | 870 | wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, checkable); |
23280650 | 871 | |
034be888 | 872 | #if (GTK_MINOR_VERSION > 0) |
23280650 | 873 | /* text has "_" instead of "&" after mitem->SetText() */ |
1e133b7d | 874 | wxString text( mitem->GetText() ); |
23280650 | 875 | |
1e133b7d RR |
876 | /* local buffer in multibyte form */ |
877 | char buf[200]; | |
878 | strcpy( buf, "/" ); | |
879 | strcat( buf, text.mb_str() ); | |
23280650 | 880 | |
034be888 | 881 | GtkItemFactoryEntry entry; |
1e133b7d | 882 | entry.path = buf; |
034be888 RR |
883 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; |
884 | entry.callback_action = 0; | |
885 | if (checkable) | |
886 | entry.item_type = "<CheckItem>"; | |
887 | else | |
888 | entry.item_type = "<Item>"; | |
23280650 | 889 | |
974e8d94 VZ |
890 | #if wxUSE_ACCEL |
891 | // due to an apparent bug in GTK+, we have to use a static buffer here - | |
892 | // otherwise GTK+ 1.2.2 manages to override the memory we pass to it | |
893 | // somehow! (VZ) | |
894 | static char s_accel[32]; // must be big enough for <control><alt><shift>F12 | |
895 | strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel)); | |
896 | entry.accelerator = s_accel; | |
897 | #else | |
898 | entry.accelerator = NULL; | |
899 | #endif | |
23280650 | 900 | |
034be888 | 901 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 902 | |
034be888 | 903 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
223d09f6 KB |
904 | wxString s = wxT("<main>/"); |
905 | for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ ) | |
034be888 | 906 | { |
223d09f6 | 907 | while (*pc == wxT('_')) pc++; /* skip it */ |
034be888 RR |
908 | s << *pc; |
909 | } | |
23280650 | 910 | |
1e133b7d | 911 | GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() ); |
23280650 | 912 | |
034be888 RR |
913 | #else |
914 | ||
d38ceae8 KB |
915 | GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() ) |
916 | : gtk_menu_item_new_with_label( mitem->GetText().mb_str() ); | |
23280650 | 917 | |
83624f79 RR |
918 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
919 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
034be888 | 920 | (gpointer)this ); |
23280650 | 921 | |
034be888 RR |
922 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
923 | gtk_widget_show( menuItem ); | |
23280650 | 924 | |
034be888 | 925 | #endif |
96fd301f | 926 | |
83624f79 RR |
927 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
928 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
034be888 | 929 | (gpointer)this ); |
cf7a7e13 | 930 | |
cd743a6f RR |
931 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
932 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
034be888 RR |
933 | (gpointer)this ); |
934 | ||
935 | mitem->SetMenuItem(menuItem); | |
cd743a6f | 936 | |
83624f79 | 937 | m_items.Append( mitem ); |
6de97a3b | 938 | } |
c801d85f | 939 | |
837904f2 | 940 | void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr ) |
c801d85f | 941 | { |
974e8d94 | 942 | wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, FALSE, subMenu); |
96fd301f | 943 | |
837904f2 | 944 | #if (GTK_MINOR_VERSION > 0) |
23280650 | 945 | /* text has "_" instead of "&" after mitem->SetText() */ |
837904f2 | 946 | wxString text( mitem->GetText() ); |
23280650 | 947 | |
837904f2 RR |
948 | /* local buffer in multibyte form */ |
949 | char buf[200]; | |
950 | strcpy( buf, "/" ); | |
951 | strcat( buf, text.mb_str() ); | |
23280650 | 952 | |
837904f2 RR |
953 | GtkItemFactoryEntry entry; |
954 | entry.path = buf; | |
955 | entry.callback = (GtkItemFactoryCallback) 0; | |
956 | entry.callback_action = 0; | |
2b2edbed | 957 | entry.item_type = "<Branch>"; |
23280650 | 958 | |
837904f2 | 959 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
23280650 | 960 | |
837904f2 | 961 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
223d09f6 KB |
962 | wxString s = wxT("<main>/"); |
963 | for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ ) | |
837904f2 | 964 | { |
223d09f6 | 965 | if (*pc == wxT('_')) pc++; /* skip it */ |
837904f2 RR |
966 | s << *pc; |
967 | } | |
23280650 | 968 | |
837904f2 | 969 | GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() ); |
23280650 | 970 | |
837904f2 RR |
971 | #else |
972 | ||
b019151f | 973 | GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str()); |
23280650 | 974 | |
837904f2 RR |
975 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
976 | gtk_widget_show( menuItem ); | |
23280650 VZ |
977 | |
978 | #endif | |
96fd301f | 979 | |
cd743a6f RR |
980 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
981 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
982 | (gpointer*)this ); | |
983 | ||
984 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
985 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
986 | (gpointer*)this ); | |
987 | ||
83624f79 | 988 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu ); |
23280650 | 989 | |
837904f2 | 990 | mitem->SetMenuItem(menuItem); |
837904f2 | 991 | |
83624f79 | 992 | m_items.Append( mitem ); |
6de97a3b | 993 | } |
c801d85f | 994 | |
828f655f RR |
995 | void wxMenu::Append( wxMenuItem *item ) |
996 | { | |
997 | m_items.Append( item ); | |
c626a8b7 | 998 | |
828f655f RR |
999 | GtkWidget *menuItem = (GtkWidget*) NULL; |
1000 | ||
c626a8b7 | 1001 | if (item->IsSeparator()) |
828f655f | 1002 | menuItem = gtk_menu_item_new(); |
c626a8b7 | 1003 | else if (item->IsSubMenu()) |
b019151f | 1004 | menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str()); |
c626a8b7 | 1005 | else |
b019151f OK |
1006 | menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str()) |
1007 | : gtk_menu_item_new_with_label(item->GetText().mbc_str()); | |
828f655f RR |
1008 | |
1009 | if (!item->IsSeparator()) | |
1010 | { | |
1011 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
1012 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
1013 | (gpointer*)this ); | |
1014 | ||
1015 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
1016 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
1017 | (gpointer*)this ); | |
c626a8b7 VZ |
1018 | |
1019 | if (!item->IsSubMenu()) | |
1020 | { | |
828f655f RR |
1021 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
1022 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
1023 | (gpointer*)this ); | |
c626a8b7 | 1024 | } |
828f655f | 1025 | } |
c626a8b7 | 1026 | |
828f655f RR |
1027 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
1028 | gtk_widget_show( menuItem ); | |
1029 | item->SetMenuItem(menuItem); | |
1030 | } | |
1031 | ||
d1b15f03 RR |
1032 | void wxMenu::Delete( int id ) |
1033 | { | |
1034 | wxNode *node = m_items.First(); | |
1035 | while (node) | |
1036 | { | |
1037 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1038 | if (item->GetId() == id) | |
1039 | { | |
974e8d94 VZ |
1040 | gtk_widget_destroy( item->GetMenuItem() ); |
1041 | m_items.DeleteNode( node ); | |
d1b15f03 RR |
1042 | return; |
1043 | } | |
1044 | node = node->Next(); | |
1045 | } | |
1046 | } | |
1047 | ||
c801d85f KB |
1048 | int wxMenu::FindItem( const wxString itemString ) const |
1049 | { | |
223d09f6 KB |
1050 | wxString s = wxT(""); |
1051 | for ( const wxChar *pc = itemString; *pc != wxT('\0'); pc++ ) | |
83624f79 | 1052 | { |
223d09f6 | 1053 | if (*pc == wxT('&')) |
23280650 VZ |
1054 | { |
1055 | pc++; /* skip it */ | |
034be888 | 1056 | #if (GTK_MINOR_VERSION > 0) |
223d09f6 | 1057 | s << wxT('_'); |
034be888 RR |
1058 | #endif |
1059 | } | |
1060 | s << *pc; | |
1061 | } | |
96fd301f | 1062 | |
83624f79 RR |
1063 | wxNode *node = m_items.First(); |
1064 | while (node) | |
1065 | { | |
1066 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1067 | if (item->GetText() == s) | |
c626a8b7 | 1068 | { |
83624f79 | 1069 | return item->GetId(); |
c626a8b7 | 1070 | } |
83624f79 RR |
1071 | node = node->Next(); |
1072 | } | |
96fd301f | 1073 | |
c626a8b7 | 1074 | return wxNOT_FOUND; |
6de97a3b | 1075 | } |
c801d85f | 1076 | |
96fd301f | 1077 | void wxMenu::Enable( int id, bool enable ) |
716b7364 | 1078 | { |
83624f79 | 1079 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1080 | |
223d09f6 | 1081 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); |
c626a8b7 VZ |
1082 | |
1083 | item->Enable(enable); | |
6de97a3b | 1084 | } |
716b7364 | 1085 | |
96fd301f | 1086 | bool wxMenu::IsEnabled( int id ) const |
e2414cbe | 1087 | { |
83624f79 | 1088 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1089 | |
223d09f6 | 1090 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") ); |
c626a8b7 VZ |
1091 | |
1092 | return item->IsEnabled(); | |
6de97a3b | 1093 | } |
e2414cbe | 1094 | |
96fd301f | 1095 | void wxMenu::Check( int id, bool enable ) |
c801d85f | 1096 | { |
83624f79 | 1097 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1098 | |
223d09f6 | 1099 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); |
c626a8b7 VZ |
1100 | |
1101 | item->Check(enable); | |
6de97a3b | 1102 | } |
c801d85f | 1103 | |
96fd301f | 1104 | bool wxMenu::IsChecked( int id ) const |
c801d85f | 1105 | { |
83624f79 | 1106 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1107 | |
223d09f6 | 1108 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") ); |
c626a8b7 VZ |
1109 | |
1110 | return item->IsChecked(); | |
6de97a3b | 1111 | } |
c801d85f | 1112 | |
debe6624 | 1113 | void wxMenu::SetLabel( int id, const wxString &label ) |
c801d85f | 1114 | { |
83624f79 | 1115 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1116 | |
223d09f6 | 1117 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); |
c626a8b7 VZ |
1118 | |
1119 | item->SetText(label); | |
6de97a3b | 1120 | } |
96fd301f | 1121 | |
c33c4050 RR |
1122 | wxString wxMenu::GetLabel( int id ) const |
1123 | { | |
83624f79 | 1124 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1125 | |
223d09f6 | 1126 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") ); |
c626a8b7 VZ |
1127 | |
1128 | return item->GetText(); | |
c33c4050 RR |
1129 | } |
1130 | ||
1131 | void wxMenu::SetHelpString( int id, const wxString& helpString ) | |
1132 | { | |
83624f79 | 1133 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1134 | |
223d09f6 | 1135 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); |
c626a8b7 VZ |
1136 | |
1137 | item->SetHelp( helpString ); | |
c33c4050 RR |
1138 | } |
1139 | ||
1140 | wxString wxMenu::GetHelpString( int id ) const | |
1141 | { | |
83624f79 | 1142 | wxMenuItem *item = FindItem(id); |
c626a8b7 | 1143 | |
223d09f6 | 1144 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") ); |
c626a8b7 VZ |
1145 | |
1146 | return item->GetHelp(); | |
c33c4050 RR |
1147 | } |
1148 | ||
96fd301f VZ |
1149 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
1150 | { | |
83624f79 RR |
1151 | wxNode *node = m_items.First(); |
1152 | while (node) | |
1153 | { | |
1154 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1155 | if (item->GetMenuItem() == menuItem) | |
1156 | return item->GetId(); | |
1157 | node = node->Next(); | |
1158 | } | |
96fd301f | 1159 | |
c626a8b7 | 1160 | return wxNOT_FOUND; |
6de97a3b | 1161 | } |
c801d85f | 1162 | |
96fd301f | 1163 | wxMenuItem *wxMenu::FindItem(int id) const |
c801d85f | 1164 | { |
83624f79 | 1165 | wxNode *node = m_items.First(); |
c626a8b7 | 1166 | while (node) |
83624f79 RR |
1167 | { |
1168 | wxMenuItem *item = (wxMenuItem*)node->Data(); | |
1169 | if (item->GetId() == id) | |
c626a8b7 | 1170 | { |
83624f79 | 1171 | return item; |
c626a8b7 | 1172 | } |
83624f79 RR |
1173 | node = node->Next(); |
1174 | } | |
96fd301f | 1175 | |
83624f79 RR |
1176 | /* Not finding anything here can be correct |
1177 | * when search the entire menu system for | |
1178 | * an entry -> no error message. */ | |
8bbe427f | 1179 | |
83624f79 | 1180 | return (wxMenuItem *) NULL; |
96fd301f | 1181 | } |
c801d85f KB |
1182 | |
1183 | void wxMenu::SetInvokingWindow( wxWindow *win ) | |
1184 | { | |
83624f79 | 1185 | m_invokingWindow = win; |
6de97a3b | 1186 | } |
c801d85f | 1187 | |
96fd301f | 1188 | wxWindow *wxMenu::GetInvokingWindow() |
c801d85f | 1189 | { |
83624f79 | 1190 | return m_invokingWindow; |
6de97a3b | 1191 | } |
c801d85f | 1192 | |
c626a8b7 VZ |
1193 | // Update a menu and all submenus recursively. source is the object that has |
1194 | // the update event handlers defined for it. If NULL, the menu or associated | |
1195 | // window will be used. | |
631f1bfe JS |
1196 | void wxMenu::UpdateUI(wxEvtHandler* source) |
1197 | { | |
1198 | if (!source && GetInvokingWindow()) | |
1199 | source = GetInvokingWindow()->GetEventHandler(); | |
1200 | if (!source) | |
1201 | source = GetEventHandler(); | |
1202 | if (!source) | |
1203 | source = this; | |
1204 | ||
1205 | wxNode* node = GetItems().First(); | |
1206 | while (node) | |
1207 | { | |
1208 | wxMenuItem* item = (wxMenuItem*) node->Data(); | |
1209 | if ( !item->IsSeparator() ) | |
1210 | { | |
1211 | wxWindowID id = item->GetId(); | |
1212 | wxUpdateUIEvent event(id); | |
1213 | event.SetEventObject( source ); | |
1214 | ||
1215 | if (source->ProcessEvent(event)) | |
1216 | { | |
1217 | if (event.GetSetText()) | |
1218 | SetLabel(id, event.GetText()); | |
1219 | if (event.GetSetChecked()) | |
1220 | Check(id, event.GetChecked()); | |
1221 | if (event.GetSetEnabled()) | |
1222 | Enable(id, event.GetEnabled()); | |
1223 | } | |
1224 | ||
1225 | if (item->GetSubMenu()) | |
1226 | item->GetSubMenu()->UpdateUI(source); | |
1227 | } | |
1228 | node = node->Next(); | |
1229 | } | |
1230 | } | |
1231 |