Corrected wxComboBox::GetValue() after a
[wxWidgets.git] / src / gtk1 / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "combobox.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/combobox.h"
18
19 #if wxUSE_COMBOBOX
20
21 #include "wx/settings.h"
22 #include "wx/arrstr.h"
23 #include "wx/intl.h"
24
25 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
26
27 #include "wx/gtk/private.h"
28
29 //-----------------------------------------------------------------------------
30 // idle system
31 //-----------------------------------------------------------------------------
32
33 extern void wxapp_install_idle_handler();
34 extern bool g_isIdle;
35
36 //-----------------------------------------------------------------------------
37 // data
38 //-----------------------------------------------------------------------------
39
40 extern bool g_blockEventsOnDrag;
41
42 //-----------------------------------------------------------------------------
43 // "changed" - typing and list item matches get changed, select-child
44 // if it doesn't match an item then just get a single changed
45 //-----------------------------------------------------------------------------
46
47 static void
48 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
49 {
50 if (g_isIdle) wxapp_install_idle_handler();
51
52 if (combo->m_ignoreNextUpdate)
53 {
54 combo->m_ignoreNextUpdate = FALSE;
55 return;
56 }
57
58 if (!combo->m_hasVMT) return;
59
60 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
61 event.SetString( combo->GetValue() );
62 event.SetEventObject( combo );
63 combo->GetEventHandler()->ProcessEvent( event );
64 }
65
66 static void
67 gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
68 {
69 }
70
71 //-----------------------------------------------------------------------------
72 // "select-child" - click/cursor get select-child, changed, select-child
73 //-----------------------------------------------------------------------------
74
75 static void
76 gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
77 {
78 if (g_isIdle) wxapp_install_idle_handler();
79
80 if (!combo->m_hasVMT) return;
81
82 if (g_blockEventsOnDrag) return;
83
84 int curSelection = combo->GetSelection();
85
86 if (combo->m_prevSelection == curSelection) return;
87
88 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
89 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
90
91 combo->m_prevSelection = curSelection;
92
93 // Quickly set the value of the combo box
94 // as GTK+ does that only AFTER the event
95 // is sent.
96 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry),
97 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
98 combo->SetValue( combo->GetStringSelection() );
99 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
100 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
101
102 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
103 event.SetInt( curSelection );
104 event.SetString( combo->GetStringSelection() );
105 event.SetEventObject( combo );
106
107 combo->GetEventHandler()->ProcessEvent( event );
108
109 // Now send the event ourselves
110 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
111 event2.SetString( combo->GetValue() );
112 event2.SetEventObject( combo );
113 combo->GetEventHandler()->ProcessEvent( event2 );
114 }
115
116 //-----------------------------------------------------------------------------
117 // wxComboBox
118 //-----------------------------------------------------------------------------
119
120 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
121
122 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
123 EVT_SIZE(wxComboBox::OnSize)
124 EVT_CHAR(wxComboBox::OnChar)
125 END_EVENT_TABLE()
126
127 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
128 const wxString& value,
129 const wxPoint& pos, const wxSize& size,
130 const wxArrayString& choices,
131 long style, const wxValidator& validator,
132 const wxString& name )
133 {
134 wxCArrayString chs(choices);
135
136 return Create( parent, id, value, pos, size, chs.GetCount(),
137 chs.GetStrings(), style, validator, name );
138 }
139
140 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
141 const wxPoint& pos, const wxSize& size,
142 int n, const wxString choices[],
143 long style, const wxValidator& validator,
144 const wxString& name )
145 {
146 m_ignoreNextUpdate = FALSE;
147 m_needParent = TRUE;
148 m_acceptsFocus = TRUE;
149 m_prevSelection = 0;
150
151 if (!PreCreation( parent, pos, size ) ||
152 !CreateBase( parent, id, pos, size, style, validator, name ))
153 {
154 wxFAIL_MSG( wxT("wxComboBox creation failed") );
155 return FALSE;
156 }
157
158 m_widget = gtk_combo_new();
159 GtkCombo *combo = GTK_COMBO(m_widget);
160
161 // Disable GTK's broken events ...
162 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
163 // ... and add surogate handler.
164 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
165 (GtkSignalFunc) gtk_dummy_callback, combo);
166
167 // make it more useable
168 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
169
170 // and case-sensitive
171 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
172
173 GtkWidget *list = GTK_COMBO(m_widget)->list;
174
175 #ifndef __WXGTK20__
176 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
177 #endif
178
179 for (int i = 0; i < n; i++)
180 {
181 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
182
183 m_clientDataList.Append( (wxObject*)NULL );
184 m_clientObjectList.Append( (wxObject*)NULL );
185
186 gtk_container_add( GTK_CONTAINER(list), list_item );
187
188 gtk_widget_show( list_item );
189 }
190
191 m_parent->DoAddChild( this );
192
193 m_focusWidget = combo->entry;
194
195 PostCreation(size);
196
197 ConnectWidget( combo->button );
198
199 // MSW's combo box shows the value and the selection is -1
200 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
201 gtk_list_unselect_all( GTK_LIST(combo->list) );
202
203 if (style & wxCB_READONLY)
204 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
205
206 gtk_signal_connect( GTK_OBJECT(combo->entry), "changed",
207 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
208
209 gtk_signal_connect( GTK_OBJECT(combo->list), "select-child",
210 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
211
212 SetBestSize(size); // need this too because this is a wxControlWithItems
213
214 // This is required for tool bar support
215 wxSize setsize = GetSize();
216 gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
217
218 return TRUE;
219 }
220
221 wxComboBox::~wxComboBox()
222 {
223 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
224 while (node)
225 {
226 wxClientData *cd = (wxClientData*)node->GetData();
227 if (cd) delete cd;
228 node = node->GetNext();
229 }
230 m_clientObjectList.Clear();
231
232 m_clientDataList.Clear();
233 }
234
235 void wxComboBox::SetFocus()
236 {
237 if ( m_hasFocus )
238 {
239 // don't do anything if we already have focus
240 return;
241 }
242
243 gtk_widget_grab_focus( m_focusWidget );
244 }
245
246 int wxComboBox::DoAppend( const wxString &item )
247 {
248 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
249
250 DisableEvents();
251
252 GtkWidget *list = GTK_COMBO(m_widget)->list;
253
254 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
255
256 gtk_container_add( GTK_CONTAINER(list), list_item );
257
258 if (GTK_WIDGET_REALIZED(m_widget))
259 {
260 gtk_widget_realize( list_item );
261 gtk_widget_realize( GTK_BIN(list_item)->child );
262 }
263
264 // Apply current widget style to the new list_item
265 GtkRcStyle *style = CreateWidgetStyle();
266 if (style)
267 {
268 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
269 GtkBin *bin = GTK_BIN( list_item );
270 GtkWidget *label = GTK_WIDGET( bin->child );
271 gtk_widget_modify_style( label, style );
272 gtk_rc_style_unref( style );
273 }
274
275 gtk_widget_show( list_item );
276
277 const int count = GetCount();
278
279 if ( (int)m_clientDataList.GetCount() < count )
280 m_clientDataList.Append( (wxObject*) NULL );
281 if ( (int)m_clientObjectList.GetCount() < count )
282 m_clientObjectList.Append( (wxObject*) NULL );
283
284 EnableEvents();
285
286 InvalidateBestSize();
287
288 return count - 1;
289 }
290
291 int wxComboBox::DoInsert( const wxString &item, int pos )
292 {
293 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
294 wxT("can't insert into sorted list"));
295
296 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
297
298 int count = GetCount();
299 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
300
301 if (pos == count)
302 return Append(item);
303
304 DisableEvents();
305
306 GtkWidget *list = GTK_COMBO(m_widget)->list;
307
308 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
309
310 GList *gitem_list = g_list_alloc ();
311 gitem_list->data = list_item;
312 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
313
314 if (GTK_WIDGET_REALIZED(m_widget))
315 {
316 gtk_widget_realize( list_item );
317 gtk_widget_realize( GTK_BIN(list_item)->child );
318
319 ApplyWidgetStyle();
320 }
321
322 gtk_widget_show( list_item );
323
324 count = GetCount();
325
326 if ( (int)m_clientDataList.GetCount() < count )
327 m_clientDataList.Insert( pos, (wxObject*) NULL );
328 if ( (int)m_clientObjectList.GetCount() < count )
329 m_clientObjectList.Insert( pos, (wxObject*) NULL );
330
331 EnableEvents();
332
333 InvalidateBestSize();
334
335 return pos;
336 }
337
338 void wxComboBox::DoSetItemClientData( int n, void* clientData )
339 {
340 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
341
342 wxList::compatibility_iterator node = m_clientDataList.Item( n );
343 if (!node) return;
344
345 node->SetData( (wxObject*) clientData );
346 }
347
348 void* wxComboBox::DoGetItemClientData( int n ) const
349 {
350 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
351
352 wxList::compatibility_iterator node = m_clientDataList.Item( n );
353
354 return node ? node->GetData() : NULL;
355 }
356
357 void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
358 {
359 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
360
361 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
362 if (!node) return;
363
364 // wxItemContainer already deletes data for us
365
366 node->SetData( (wxObject*) clientData );
367 }
368
369 wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
370 {
371 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
372
373 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
374
375 return node ? (wxClientData*) node->GetData() : NULL;
376 }
377
378 void wxComboBox::Clear()
379 {
380 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
381
382 DisableEvents();
383
384 GtkWidget *list = GTK_COMBO(m_widget)->list;
385 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
386
387 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
388 while (node)
389 {
390 wxClientData *cd = (wxClientData*)node->GetData();
391 if (cd) delete cd;
392 node = node->GetNext();
393 }
394 m_clientObjectList.Clear();
395
396 m_clientDataList.Clear();
397
398 EnableEvents();
399
400 InvalidateBestSize();
401 }
402
403 void wxComboBox::Delete( int n )
404 {
405 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
406
407 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
408
409 GList *child = g_list_nth( listbox->children, n );
410
411 if (!child)
412 {
413 wxFAIL_MSG(wxT("wrong index"));
414 return;
415 }
416
417 DisableEvents();
418
419 GList *list = g_list_append( (GList*) NULL, child->data );
420 gtk_list_remove_items( listbox, list );
421 g_list_free( list );
422
423 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
424 if (node)
425 {
426 wxClientData *cd = (wxClientData*)node->GetData();
427 if (cd) delete cd;
428 m_clientObjectList.Erase( node );
429 }
430
431 node = m_clientDataList.Item( n );
432 if (node)
433 m_clientDataList.Erase( node );
434
435 EnableEvents();
436
437 InvalidateBestSize();
438 }
439
440 void wxComboBox::SetString(int n, const wxString &text)
441 {
442 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
443
444 GtkWidget *list = GTK_COMBO(m_widget)->list;
445
446 GList *child = g_list_nth( GTK_LIST(list)->children, n );
447 if (child)
448 {
449 GtkBin *bin = GTK_BIN( child->data );
450 GtkLabel *label = GTK_LABEL( bin->child );
451 gtk_label_set_text(label, wxGTK_CONV(text));
452 }
453 else
454 {
455 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
456 }
457
458 InvalidateBestSize();
459 }
460
461 int wxComboBox::FindString( const wxString &item ) const
462 {
463 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
464
465 GtkWidget *list = GTK_COMBO(m_widget)->list;
466
467 GList *child = GTK_LIST(list)->children;
468 int count = 0;
469 while (child)
470 {
471 GtkBin *bin = GTK_BIN( child->data );
472 GtkLabel *label = GTK_LABEL( bin->child );
473 #ifdef __WXGTK20__
474 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
475 #else
476 wxString str( label->label );
477 #endif
478 if (item == str)
479 return count;
480
481 count++;
482 child = child->next;
483 }
484
485 return wxNOT_FOUND;
486 }
487
488 int wxComboBox::GetSelection() const
489 {
490 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
491
492 GtkWidget *list = GTK_COMBO(m_widget)->list;
493
494 GList *selection = GTK_LIST(list)->selection;
495 if (selection)
496 {
497 GList *child = GTK_LIST(list)->children;
498 int count = 0;
499 while (child)
500 {
501 if (child->data == selection->data) return count;
502 count++;
503 child = child->next;
504 }
505 }
506
507 return -1;
508 }
509
510 wxString wxComboBox::GetString( int n ) const
511 {
512 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
513
514 GtkWidget *list = GTK_COMBO(m_widget)->list;
515
516 wxString str;
517 GList *child = g_list_nth( GTK_LIST(list)->children, n );
518 if (child)
519 {
520 GtkBin *bin = GTK_BIN( child->data );
521 GtkLabel *label = GTK_LABEL( bin->child );
522 #ifdef __WXGTK20__
523 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
524 #else
525 str = wxString( label->label );
526 #endif
527 }
528 else
529 {
530 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
531 }
532
533 return str;
534 }
535
536 wxString wxComboBox::GetStringSelection() const
537 {
538 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
539
540 GtkWidget *list = GTK_COMBO(m_widget)->list;
541
542 GList *selection = GTK_LIST(list)->selection;
543 if (selection)
544 {
545 GtkBin *bin = GTK_BIN( selection->data );
546 GtkLabel *label = GTK_LABEL( bin->child );
547 #ifdef __WXGTK20__
548 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
549 #else
550 wxString tmp( label->label );
551 #endif
552 return tmp;
553 }
554
555 wxFAIL_MSG( wxT("wxComboBox: no selection") );
556
557 return wxT("");
558 }
559
560 int wxComboBox::GetCount() const
561 {
562 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
563
564 GtkWidget *list = GTK_COMBO(m_widget)->list;
565
566 GList *child = GTK_LIST(list)->children;
567 int count = 0;
568 while (child) { count++; child = child->next; }
569 return count;
570 }
571
572 void wxComboBox::SetSelection( int n )
573 {
574 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
575
576 DisableEvents();
577
578 GtkWidget *list = GTK_COMBO(m_widget)->list;
579 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
580 gtk_list_select_item( GTK_LIST(list), n );
581 m_prevSelection = n;
582
583 EnableEvents();
584 }
585
586 bool wxComboBox::SetStringSelection( const wxString &string )
587 {
588 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") );
589
590 int res = FindString( string );
591 if (res == -1) return false;
592 SetSelection( res );
593 return true;
594 }
595
596 wxString wxComboBox::GetValue() const
597 {
598 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
599 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
600
601 #if 0
602 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
603 {
604 wxChar c = tmp[i];
605 printf( "%d ", (int) (c) );
606 }
607 printf( "\n" );
608 #endif
609
610 return tmp;
611 }
612
613 void wxComboBox::SetValue( const wxString& value )
614 {
615 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
616
617 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
618 wxString tmp = wxT("");
619 if (!value.IsNull()) tmp = value;
620 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
621
622 InvalidateBestSize();
623 }
624
625 void wxComboBox::Copy()
626 {
627 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
628
629 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
630 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
631 }
632
633 void wxComboBox::Cut()
634 {
635 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
636
637 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
638 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
639 }
640
641 void wxComboBox::Paste()
642 {
643 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
644
645 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
646 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
647 }
648
649 void wxComboBox::SetInsertionPoint( long pos )
650 {
651 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
652
653 if ( pos == GetLastPosition() )
654 pos = -1;
655
656 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
657 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
658 }
659
660 long wxComboBox::GetInsertionPoint() const
661 {
662 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
663 }
664
665 long wxComboBox::GetLastPosition() const
666 {
667 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
668 int pos = GTK_ENTRY(entry)->text_length;
669 return (long) pos-1;
670 }
671
672 void wxComboBox::Replace( long from, long to, const wxString& value )
673 {
674 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
675
676 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
677 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
678 if (value.IsNull()) return;
679 gint pos = (gint)to;
680
681 #if wxUSE_UNICODE
682 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
683 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
684 #else
685 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
686 #endif
687 }
688
689 void wxComboBox::SetSelection( long from, long to )
690 {
691 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
692 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
693 }
694
695 void wxComboBox::SetEditable( bool editable )
696 {
697 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
698 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
699 }
700
701 void wxComboBox::OnChar( wxKeyEvent &event )
702 {
703 if ( event.GetKeyCode() == WXK_RETURN )
704 {
705 // GTK automatically selects an item if its in the list
706 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
707 event.SetString( GetValue() );
708 event.SetInt( GetSelection() );
709 event.SetEventObject( this );
710
711 if (!GetEventHandler()->ProcessEvent( event ))
712 {
713 // This will invoke the dialog default action, such
714 // as the clicking the default button.
715
716 wxWindow *top_frame = m_parent;
717 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
718 top_frame = top_frame->GetParent();
719
720 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
721 {
722 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
723
724 if (window->default_widget)
725 gtk_widget_activate (window->default_widget);
726 }
727 }
728
729 // Catch GTK event so that GTK doesn't open the drop
730 // down list upon RETURN.
731 return;
732 }
733
734 event.Skip();
735 }
736
737 void wxComboBox::DisableEvents()
738 {
739 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
740 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
741 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
742 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
743 }
744
745 void wxComboBox::EnableEvents()
746 {
747 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
748 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
749 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
750 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
751 }
752
753 void wxComboBox::OnSize( wxSizeEvent &event )
754 {
755 // NB: In some situations (e.g. on non-first page of a wizard, if the
756 // size used is default size), GtkCombo widget is resized correctly,
757 // but it's look is not updated, it's rendered as if it was much wider.
758 // No other widgets are affected, so it looks like a bug in GTK+.
759 // Manually requesting resize calculation (as gtk_pizza_set_size does)
760 // fixes it.
761 if (GTK_WIDGET_VISIBLE(m_widget))
762 gtk_widget_queue_resize(m_widget);
763
764 event.Skip();
765 }
766
767 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
768 {
769 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
770
771 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
772 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
773
774 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
775 GList *child = list->children;
776 while (child)
777 {
778 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
779
780 GtkBin *bin = GTK_BIN(child->data);
781 gtk_widget_modify_style( bin->child, style );
782
783 child = child->next;
784 }
785 }
786
787 GtkWidget* wxComboBox::GetConnectWidget()
788 {
789 return GTK_COMBO(m_widget)->entry;
790 }
791
792 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
793 {
794 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
795 (window == GTK_COMBO(m_widget)->button->window ) );
796 }
797
798 wxSize wxComboBox::DoGetBestSize() const
799 {
800 wxSize ret( wxControl::DoGetBestSize() );
801
802 // we know better our horizontal extent: it depends on the longest string
803 // in the combobox
804 if ( m_widget )
805 {
806 int width;
807 size_t count = GetCount();
808 for ( size_t n = 0; n < count; n++ )
809 {
810 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
811 if ( width > ret.x )
812 ret.x = width;
813 }
814 }
815
816 // empty combobox should have some reasonable default size too
817 if ( ret.x < 100 )
818 ret.x = 100;
819
820 CacheBestSize(ret);
821 return ret;
822 }
823
824 // static
825 wxVisualAttributes
826 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
827 {
828 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
829 }
830
831 #endif