]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_CHOICE | |
18 | ||
19 | #include "wx/choice.h" | |
20 | #include "wx/arrstr.h" | |
21 | ||
22 | #include "wx/gtk/private.h" | |
23 | ||
24 | //----------------------------------------------------------------------------- | |
25 | // idle system | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
28 | extern void wxapp_install_idle_handler(); | |
29 | extern bool g_isIdle; | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // data | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | extern bool g_blockEventsOnDrag; | |
36 | ||
37 | //----------------------------------------------------------------------------- | |
38 | // "activate" | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) | |
42 | { | |
43 | if (g_isIdle) | |
44 | wxapp_install_idle_handler(); | |
45 | ||
46 | if (!choice->m_hasVMT) return; | |
47 | ||
48 | if (g_blockEventsOnDrag) return; | |
49 | ||
50 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); | |
51 | int n = choice->GetSelection(); | |
52 | ||
53 | event.SetInt( n ); | |
54 | event.SetString( choice->GetStringSelection() ); | |
55 | event.SetEventObject(choice); | |
56 | ||
57 | if ( choice->HasClientObjectData() ) | |
58 | event.SetClientObject( choice->GetClientObject(n) ); | |
59 | else if ( choice->HasClientUntypedData() ) | |
60 | event.SetClientData( choice->GetClientData(n) ); | |
61 | ||
62 | choice->GetEventHandler()->ProcessEvent(event); | |
63 | } | |
64 | ||
65 | //----------------------------------------------------------------------------- | |
66 | // wxChoice | |
67 | //----------------------------------------------------------------------------- | |
68 | ||
69 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) | |
70 | ||
71 | wxChoice::wxChoice() | |
72 | { | |
73 | m_strings = (wxSortedArrayString *)NULL; | |
74 | } | |
75 | ||
76 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, | |
77 | const wxPoint &pos, const wxSize &size, | |
78 | const wxArrayString& choices, | |
79 | long style, const wxValidator& validator, | |
80 | const wxString &name ) | |
81 | { | |
82 | wxCArrayString chs(choices); | |
83 | ||
84 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
85 | style, validator, name ); | |
86 | } | |
87 | ||
88 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, | |
89 | const wxPoint &pos, const wxSize &size, | |
90 | int n, const wxString choices[], | |
91 | long style, const wxValidator& validator, const wxString &name ) | |
92 | { | |
93 | m_needParent = TRUE; | |
94 | #if (GTK_MINOR_VERSION > 0) | |
95 | m_acceptsFocus = TRUE; | |
96 | #endif | |
97 | ||
98 | if (!PreCreation( parent, pos, size ) || | |
99 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
100 | { | |
101 | wxFAIL_MSG( wxT("wxChoice creation failed") ); | |
102 | return FALSE; | |
103 | } | |
104 | ||
105 | m_widget = gtk_option_menu_new(); | |
106 | ||
107 | if ( style & wxCB_SORT ) | |
108 | { | |
109 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
110 | // items in the correct order | |
111 | m_strings = new wxSortedArrayString; | |
112 | } | |
113 | ||
114 | GtkWidget *menu = gtk_menu_new(); | |
115 | ||
116 | for (int i = 0; i < n; i++) | |
117 | { | |
118 | GtkAddHelper(menu, i, choices[i]); | |
119 | } | |
120 | ||
121 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
122 | ||
123 | m_parent->DoAddChild( this ); | |
124 | ||
125 | PostCreation(); | |
126 | InheritAttributes(); | |
127 | ||
128 | SetBestSize(size); | |
129 | ||
130 | Show( TRUE ); | |
131 | ||
132 | return TRUE; | |
133 | } | |
134 | ||
135 | wxChoice::~wxChoice() | |
136 | { | |
137 | Clear(); | |
138 | ||
139 | delete m_strings; | |
140 | } | |
141 | ||
142 | int wxChoice::DoAppend( const wxString &item ) | |
143 | { | |
144 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
145 | ||
146 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
147 | ||
148 | return GtkAddHelper(menu, GetCount(), item); | |
149 | } | |
150 | ||
151 | int wxChoice::DoInsert( const wxString &item, int pos ) | |
152 | { | |
153 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
154 | wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
155 | ||
156 | if (pos == GetCount()) | |
157 | return DoAppend(item); | |
158 | ||
159 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
160 | ||
161 | return GtkAddHelper(menu, pos, item); | |
162 | } | |
163 | ||
164 | void wxChoice::DoSetItemClientData( int n, void* clientData ) | |
165 | { | |
166 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
167 | ||
168 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
169 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); | |
170 | ||
171 | node->SetData( (wxObject*) clientData ); | |
172 | } | |
173 | ||
174 | void* wxChoice::DoGetItemClientData( int n ) const | |
175 | { | |
176 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); | |
177 | ||
178 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
179 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); | |
180 | ||
181 | return node->GetData(); | |
182 | } | |
183 | ||
184 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) | |
185 | { | |
186 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
187 | ||
188 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
189 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); | |
190 | ||
191 | // wxItemContainer already deletes data for us | |
192 | ||
193 | node->SetData( (wxObject*) clientData ); | |
194 | } | |
195 | ||
196 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const | |
197 | { | |
198 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); | |
199 | ||
200 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
201 | wxCHECK_MSG( node, (wxClientData *)NULL, | |
202 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); | |
203 | ||
204 | return (wxClientData*) node->GetData(); | |
205 | } | |
206 | ||
207 | void wxChoice::Clear() | |
208 | { | |
209 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
210 | ||
211 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); | |
212 | GtkWidget *menu = gtk_menu_new(); | |
213 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
214 | ||
215 | if ( HasClientObjectData() ) | |
216 | { | |
217 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
218 | // and not wxList<wxClientData> we can't just say | |
219 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
220 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
221 | while ( node ) | |
222 | { | |
223 | delete (wxClientData *)node->GetData(); | |
224 | node = node->GetNext(); | |
225 | } | |
226 | } | |
227 | m_clientList.Clear(); | |
228 | ||
229 | if ( m_strings ) | |
230 | m_strings->Clear(); | |
231 | } | |
232 | ||
233 | void wxChoice::Delete( int n ) | |
234 | { | |
235 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
236 | ||
237 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
238 | // in 2.0), hence this dumb implementation -- still better than nothing | |
239 | int i, | |
240 | count = GetCount(); | |
241 | ||
242 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
243 | ||
244 | const bool hasClientData = m_clientDataItemsType != wxClientData_None; | |
245 | const bool hasObjectData = m_clientDataItemsType == wxClientData_Object; | |
246 | ||
247 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
248 | ||
249 | wxArrayString items; | |
250 | wxArrayPtrVoid itemsData; | |
251 | items.Alloc(count); | |
252 | for ( i = 0; i < count; i++ ) | |
253 | { | |
254 | if ( i != n ) | |
255 | { | |
256 | items.Add(GetString(i)); | |
257 | if ( hasClientData ) | |
258 | { | |
259 | // also save the client data | |
260 | itemsData.Add(node->GetData()); | |
261 | } | |
262 | } | |
263 | else // need to delete the client object too | |
264 | { | |
265 | if ( hasObjectData ) | |
266 | { | |
267 | delete (wxClientData *)node->GetData(); | |
268 | } | |
269 | } | |
270 | ||
271 | if ( hasClientData ) | |
272 | { | |
273 | node = node->GetNext(); | |
274 | } | |
275 | } | |
276 | ||
277 | if ( hasObjectData ) | |
278 | { | |
279 | // prevent Clear() from destroying all client data | |
280 | m_clientDataItemsType = wxClientData_None; | |
281 | } | |
282 | ||
283 | Clear(); | |
284 | ||
285 | for ( i = 0; i < count - 1; i++ ) | |
286 | { | |
287 | Append(items[i]); | |
288 | ||
289 | if ( hasObjectData ) | |
290 | SetClientObject(i, (wxClientData *)itemsData[i]); | |
291 | else if ( hasClientData ) | |
292 | SetClientData(i, itemsData[i]); | |
293 | } | |
294 | } | |
295 | ||
296 | int wxChoice::FindString( const wxString &string ) const | |
297 | { | |
298 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
299 | ||
300 | // If you read this code once and you think you understand | |
301 | // it, then you are very wrong. Robert Roebling. | |
302 | ||
303 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
304 | int count = 0; | |
305 | GList *child = menu_shell->children; | |
306 | while (child) | |
307 | { | |
308 | GtkBin *bin = GTK_BIN( child->data ); | |
309 | GtkLabel *label = (GtkLabel *) NULL; | |
310 | if (bin->child) | |
311 | label = GTK_LABEL(bin->child); | |
312 | if (!label) | |
313 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
314 | ||
315 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
316 | ||
317 | #ifdef __WXGTK20__ | |
318 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
319 | #else | |
320 | wxString tmp( label->label ); | |
321 | #endif | |
322 | if (string == tmp) | |
323 | return count; | |
324 | ||
325 | child = child->next; | |
326 | count++; | |
327 | } | |
328 | ||
329 | return -1; | |
330 | } | |
331 | ||
332 | int wxChoice::GetSelection() const | |
333 | { | |
334 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
335 | ||
336 | #ifdef __WXGTK20__ | |
337 | ||
338 | return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) ); | |
339 | ||
340 | #else | |
341 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
342 | int count = 0; | |
343 | ||
344 | GList *child = menu_shell->children; | |
345 | while (child) | |
346 | { | |
347 | GtkBin *bin = GTK_BIN( child->data ); | |
348 | if (!bin->child) return count; | |
349 | child = child->next; | |
350 | count++; | |
351 | } | |
352 | ||
353 | return -1; | |
354 | #endif | |
355 | } | |
356 | ||
357 | void wxChoice::SetString( int n, const wxString& str ) | |
358 | { | |
359 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
360 | ||
361 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
362 | int count = 0; | |
363 | GList *child = menu_shell->children; | |
364 | while (child) | |
365 | { | |
366 | GtkBin *bin = GTK_BIN( child->data ); | |
367 | if (count == n) | |
368 | { | |
369 | GtkLabel *label = (GtkLabel *) NULL; | |
370 | if (bin->child) | |
371 | label = GTK_LABEL(bin->child); | |
372 | if (!label) | |
373 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
374 | ||
375 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
376 | ||
377 | gtk_label_set_text( label, wxGTK_CONV( str ) ); | |
378 | ||
379 | return; | |
380 | } | |
381 | child = child->next; | |
382 | count++; | |
383 | } | |
384 | } | |
385 | ||
386 | wxString wxChoice::GetString( int n ) const | |
387 | { | |
388 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); | |
389 | ||
390 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
391 | int count = 0; | |
392 | GList *child = menu_shell->children; | |
393 | while (child) | |
394 | { | |
395 | GtkBin *bin = GTK_BIN( child->data ); | |
396 | if (count == n) | |
397 | { | |
398 | GtkLabel *label = (GtkLabel *) NULL; | |
399 | if (bin->child) | |
400 | label = GTK_LABEL(bin->child); | |
401 | if (!label) | |
402 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
403 | ||
404 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
405 | ||
406 | #ifdef __WXGTK20__ | |
407 | return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
408 | #else | |
409 | return wxString( label->label ); | |
410 | #endif | |
411 | } | |
412 | child = child->next; | |
413 | count++; | |
414 | } | |
415 | ||
416 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); | |
417 | ||
418 | return wxT(""); | |
419 | } | |
420 | ||
421 | int wxChoice::GetCount() const | |
422 | { | |
423 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); | |
424 | ||
425 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
426 | int count = 0; | |
427 | GList *child = menu_shell->children; | |
428 | while (child) | |
429 | { | |
430 | count++; | |
431 | child = child->next; | |
432 | } | |
433 | return count; | |
434 | } | |
435 | ||
436 | void wxChoice::SetSelection( int n ) | |
437 | { | |
438 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
439 | ||
440 | int tmp = n; | |
441 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
442 | } | |
443 | ||
444 | void wxChoice::ApplyWidgetStyle() | |
445 | { | |
446 | SetWidgetStyle(); | |
447 | ||
448 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
449 | ||
450 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
451 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
452 | ||
453 | GList *child = menu_shell->children; | |
454 | while (child) | |
455 | { | |
456 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
457 | ||
458 | GtkBin *bin = GTK_BIN( child->data ); | |
459 | GtkWidget *label = (GtkWidget *) NULL; | |
460 | if (bin->child) | |
461 | label = bin->child; | |
462 | if (!label) | |
463 | label = BUTTON_CHILD(m_widget); | |
464 | ||
465 | gtk_widget_set_style( label, m_widgetStyle ); | |
466 | ||
467 | child = child->next; | |
468 | } | |
469 | } | |
470 | ||
471 | int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) | |
472 | { | |
473 | wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); | |
474 | ||
475 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); | |
476 | ||
477 | size_t index; | |
478 | if ( m_strings ) | |
479 | { | |
480 | // sorted control, need to insert at the correct index | |
481 | index = m_strings->Add(item); | |
482 | ||
483 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
484 | ||
485 | if ( index ) | |
486 | { | |
487 | m_clientList.Insert( m_clientList.Item(index - 1), | |
488 | (wxObject*) NULL ); | |
489 | } | |
490 | else | |
491 | { | |
492 | m_clientList.Insert( (wxObject*) NULL ); | |
493 | } | |
494 | } | |
495 | else | |
496 | { | |
497 | // don't call wxChoice::GetCount() from here because it doesn't work | |
498 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
499 | ||
500 | // normal control, just append | |
501 | if (pos == (int)m_clientList.GetCount()) | |
502 | { | |
503 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
504 | m_clientList.Append( (wxObject*) NULL ); | |
505 | index = m_clientList.GetCount() - 1; | |
506 | } | |
507 | else | |
508 | { | |
509 | gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); | |
510 | m_clientList.Insert( pos, (wxObject*) NULL ); | |
511 | index = pos; | |
512 | } | |
513 | } | |
514 | ||
515 | if (GTK_WIDGET_REALIZED(m_widget)) | |
516 | { | |
517 | gtk_widget_realize( menu_item ); | |
518 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
519 | ||
520 | if (m_widgetStyle) ApplyWidgetStyle(); | |
521 | } | |
522 | ||
523 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
524 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
525 | ||
526 | gtk_widget_show( menu_item ); | |
527 | ||
528 | // return the index of the item in the control | |
529 | return index; | |
530 | } | |
531 | ||
532 | wxSize wxChoice::DoGetBestSize() const | |
533 | { | |
534 | wxSize ret( wxControl::DoGetBestSize() ); | |
535 | ||
536 | // we know better our horizontal extent: it depends on the longest string | |
537 | // we have | |
538 | ret.x = 0; | |
539 | if ( m_widget ) | |
540 | { | |
541 | int width; | |
542 | size_t count = GetCount(); | |
543 | for ( size_t n = 0; n < count; n++ ) | |
544 | { | |
545 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
546 | if ( width > ret.x ) | |
547 | ret.x = width; | |
548 | } | |
549 | ||
550 | // add extra for the choice "=" button | |
551 | ||
552 | // VZ: I don't know how to get the right value, it seems to be in | |
553 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get | |
554 | // to it - maybe we can use gtk_option_menu_size_request() for this | |
555 | // somehow? | |
556 | // | |
557 | // This default value works only for the default GTK+ theme (i.e. | |
558 | // no theme at all) (FIXME) | |
559 | static const int widthChoiceIndicator = 35; | |
560 | ret.x += widthChoiceIndicator; | |
561 | } | |
562 | ||
563 | // but not less than the minimal width | |
564 | if ( ret.x < 80 ) | |
565 | ret.x = 80; | |
566 | ||
567 | ret.y = 16 + GetCharHeight(); | |
568 | ||
569 | return ret; | |
570 | } | |
571 | ||
572 | bool wxChoice::IsOwnGtkWindow( GdkWindow *window ) | |
573 | { | |
574 | #ifdef __WXGTK20__ | |
575 | return GTK_BUTTON(m_widget)->event_window; | |
576 | #else | |
577 | return (window == m_widget->window); | |
578 | #endif | |
579 | } | |
580 | ||
581 | ||
582 | #endif // wxUSE_CHOICE | |
583 |