various warning fixes for icc 9.1 compilation
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_COMBOBOX
14
15 #include "wx/combobox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
22 #endif
23
24 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
25 // in favour of GtkComboBox for <GTK2.4 runtime
26 // We also use GtkList
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
29 #endif
30 #include "wx/gtk/private.h"
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37 static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
38
39 //-----------------------------------------------------------------------------
40 // "changed" - typing and list item matches get changed, select-child
41 // if it doesn't match an item then just get a single changed
42 //-----------------------------------------------------------------------------
43
44 extern "C" {
45 static void
46 gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
47 {
48 if (combo->m_ignoreNextUpdate)
49 {
50 combo->m_ignoreNextUpdate = false;
51 return;
52 }
53
54 if (!combo->m_hasVMT) return;
55
56 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
57 event.SetString( combo->GetValue() );
58 event.SetEventObject( combo );
59 combo->GetEventHandler()->ProcessEvent( event );
60 }
61 }
62
63 extern "C" {
64 static void
65 gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
66 {
67 }
68 }
69
70 extern "C" {
71 static void
72 gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
73 {
74 // when the popup is hidden, throw a SELECTED event only if the combobox
75 // selection changed.
76 const int curSelection = combo->GetCurrentSelection();
77
78 const bool hasChanged = curSelection != g_SelectionBeforePopup;
79
80 // reset the selection flag to value meaning that it is hidden and do it
81 // now, before generating the events, so that GetSelection() returns the
82 // new value from the event handler
83 g_SelectionBeforePopup = wxID_NONE;
84
85 if ( hasChanged )
86 {
87 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
88 event.SetInt( curSelection );
89 event.SetString( combo->GetStringSelection() );
90 event.SetEventObject( combo );
91 combo->GetEventHandler()->ProcessEvent( event );
92
93 // for consistency with the other ports, send TEXT event
94 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
95 event2.SetString( combo->GetStringSelection() );
96 event2.SetEventObject( combo );
97 combo->GetEventHandler()->ProcessEvent( event2 );
98 }
99 }
100 }
101
102 extern "C" {
103 static void
104 gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
105 {
106 // store the combobox selection value before the popup is shown
107 g_SelectionBeforePopup = combo->GetCurrentSelection();
108 }
109 }
110
111 //-----------------------------------------------------------------------------
112 // "select-child" - click/cursor get select-child, changed, select-child
113 //-----------------------------------------------------------------------------
114
115 extern "C" {
116 static void
117 gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
118 {
119 if (!combo->m_hasVMT) return;
120
121 if (g_blockEventsOnDrag) return;
122
123 int curSelection = combo->GetCurrentSelection();
124
125 if (combo->m_prevSelection == curSelection) return;
126
127 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
128 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
129
130 combo->m_prevSelection = curSelection;
131
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
134 // is sent.
135 GtkWidget* entry = GTK_COMBO(combo->GetHandle())->entry;
136 g_signal_handlers_block_by_func(
137 entry, (gpointer)gtkcombo_text_changed_callback, combo);
138 combo->SetValue( combo->GetStringSelection() );
139 g_signal_handlers_unblock_by_func(
140 entry, (gpointer)gtkcombo_text_changed_callback, combo);
141
142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
144 // called each times the mouse is over an item with a pressed button so a lot
145 // of SELECTED event could be generated if the user keep the mouse button down
146 // and select other items ...
147 if (g_SelectionBeforePopup == wxID_NONE)
148 {
149 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
150 event.SetInt( curSelection );
151 event.SetString( combo->GetStringSelection() );
152 event.SetEventObject( combo );
153 combo->GetEventHandler()->ProcessEvent( event );
154
155 // for consistency with the other ports, don't generate text update
156 // events while the user is browsing the combobox neither
157 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
158 event2.SetString( combo->GetValue() );
159 event2.SetEventObject( combo );
160 combo->GetEventHandler()->ProcessEvent( event2 );
161 }
162 }
163 }
164
165 #ifdef __WXGTK24__
166 extern "C" {
167 static void
168 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
169 {
170 if (!combo->m_hasVMT) return;
171
172 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
173 event.SetString( combo->GetValue() );
174 event.SetEventObject( combo );
175 combo->GetEventHandler()->ProcessEvent( event );
176 }
177 }
178
179 extern "C" {
180 static void
181 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
182 {
183 if (!combo->m_hasVMT) return;
184
185 if (combo->GetSelection() == -1)
186 return;
187
188 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
189 event.SetInt( combo->GetSelection() );
190 event.SetString( combo->GetStringSelection() );
191 event.SetEventObject( combo );
192 combo->GetEventHandler()->ProcessEvent( event );
193 }
194 }
195
196 #endif
197
198 //-----------------------------------------------------------------------------
199 // wxComboBox
200 //-----------------------------------------------------------------------------
201
202 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
203
204 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
205 EVT_SIZE(wxComboBox::OnSize)
206 EVT_CHAR(wxComboBox::OnChar)
207
208 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
209 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
210 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
211 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
212 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
213 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
214 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
215
216 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
217 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
218 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
219 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
220 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
221 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
222 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
223 END_EVENT_TABLE()
224
225 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
226 const wxString& value,
227 const wxPoint& pos, const wxSize& size,
228 const wxArrayString& choices,
229 long style, const wxValidator& validator,
230 const wxString& name )
231 {
232 wxCArrayString chs(choices);
233
234 return Create( parent, id, value, pos, size, chs.GetCount(),
235 chs.GetStrings(), style, validator, name );
236 }
237
238 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
239 const wxPoint& pos, const wxSize& size,
240 int n, const wxString choices[],
241 long style, const wxValidator& validator,
242 const wxString& name )
243 {
244 m_strings = NULL;
245 m_ignoreNextUpdate = false;
246 m_prevSelection = 0;
247
248 if (!PreCreation( parent, pos, size ) ||
249 !CreateBase( parent, id, pos, size, style, validator, name ))
250 {
251 wxFAIL_MSG( wxT("wxComboBox creation failed") );
252 return false;
253 }
254
255 if(HasFlag(wxCB_SORT))
256 m_strings = new wxSortedArrayString();
257
258 #ifdef __WXGTK24__
259 if (!gtk_check_version(2,4,0))
260 {
261 m_widget = gtk_combo_box_entry_new_text();
262
263 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE );
264 }
265 else
266 #endif
267 {
268 m_widget = gtk_combo_new();
269 GtkCombo* combo = GTK_COMBO(m_widget);
270
271 // Disable GTK's broken events ...
272 g_signal_handler_disconnect (combo->entry, combo->entry_change_id);
273 // ... and add surrogate handler.
274 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
275 G_CALLBACK (gtkcombo_dummy_callback),
276 combo);
277
278 // make it more useable
279 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
280
281 // and case-sensitive
282 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
283
284 if (style & wxNO_BORDER)
285 g_object_set (combo->entry, "has-frame", FALSE, NULL );
286 }
287
288 Append(n, choices);
289
290 m_parent->DoAddChild( this );
291
292 GtkEntry *entry = NULL;
293 #ifdef __WXGTK24__
294 if (!gtk_check_version(2,4,0))
295 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
296 else
297 #endif
298 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
299
300 m_focusWidget = GTK_WIDGET( entry );
301
302 PostCreation(size);
303
304 #ifdef __WXGTK24__
305 if (!gtk_check_version(2,4,0))
306 ConnectWidget( m_widget );
307 else
308 #endif
309 ConnectWidget( GTK_COMBO(m_widget)->button );
310
311 #ifdef __WXGTK24__
312 if (!gtk_check_version(2,4,0))
313 {
314 gtk_entry_set_text( entry, wxGTK_CONV(value) );
315
316 if (style & wxCB_READONLY)
317 gtk_entry_set_editable( entry, FALSE );
318
319 g_signal_connect_after (entry, "changed",
320 G_CALLBACK (gtkcombobox_text_changed_callback), this);
321
322 g_signal_connect_after (m_widget, "changed",
323 G_CALLBACK (gtkcombobox_changed_callback), this);
324
325 }
326 else
327 #endif
328 {
329 GtkCombo *combo = GTK_COMBO(m_widget);
330 // MSW's combo box shows the value and the selection is -1
331 gtk_entry_set_text( entry, wxGTK_CONV(value) );
332 gtk_list_unselect_all( GTK_LIST(combo->list) );
333
334 if (style & wxCB_READONLY)
335 gtk_entry_set_editable( entry, FALSE );
336
337 // "show" and "hide" events are generated when user click on the combobox button which popups a list
338 // this list is the "popwin" gtk widget
339 g_signal_connect (GTK_COMBO(combo)->popwin, "hide",
340 G_CALLBACK (gtkcombo_popup_hide_callback), this);
341 g_signal_connect (GTK_COMBO(combo)->popwin, "show",
342 G_CALLBACK (gtkcombo_popup_show_callback), this);
343 g_signal_connect_after (combo->list, "select-child",
344 G_CALLBACK (gtkcombo_combo_select_child_callback),
345 this);
346 g_signal_connect_after (entry, "changed",
347 G_CALLBACK (gtkcombo_text_changed_callback), this);
348 }
349
350 SetInitialSize(size); // need this too because this is a wxControlWithItems
351
352 return true;
353 }
354
355 wxComboBox::~wxComboBox()
356 {
357 Clear();
358
359 delete m_strings;
360 }
361
362 void wxComboBox::SetFocus()
363 {
364 if ( m_hasFocus )
365 {
366 // don't do anything if we already have focus
367 return;
368 }
369
370 gtk_widget_grab_focus( m_focusWidget );
371 }
372
373 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
374 unsigned int pos,
375 void **clientData, wxClientDataType type)
376 {
377 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
378
379 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
380 _T("In a sorted combobox data could only be appended"));
381
382 const int count = items.GetCount();
383
384 int n = wxNOT_FOUND;
385
386 #ifdef __WXGTK24__
387 if (!gtk_check_version(2,4,0))
388 {
389 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
390 for( int i = 0; i < count; ++i )
391 {
392 n = pos + i;
393 // If sorted, use this wxSortedArrayStrings to determine
394 // the right insertion point
395 if(m_strings)
396 n = m_strings->Add(items[i]);
397
398 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
399
400 m_clientData.Insert( NULL, n );
401 AssignNewItemClientData(n, clientData, i, type);
402 }
403 }
404 else
405 #endif
406 {
407 DisableEvents();
408
409 GtkWidget *list = GTK_COMBO(m_widget)->list;
410 for( int i = 0; i < count; ++i )
411 {
412 n = pos + i;
413 // If sorted, use this wxSortedArrayStrings to determine
414 // the right insertion point
415 if(m_strings)
416 n = m_strings->Add(items[i]);
417
418 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
419
420 // TODO construct a list with all items and call gtk_list_insert_items once?
421 GList *gitem_list = g_list_alloc ();
422 gitem_list->data = list_item;
423 gtk_list_insert_items( GTK_LIST (list), gitem_list, n );
424
425 m_clientData.Insert( NULL, n );
426 AssignNewItemClientData(n, clientData, i, type);
427
428 if (GTK_WIDGET_REALIZED(m_widget))
429 {
430 gtk_widget_realize( list_item );
431 gtk_widget_realize( GTK_BIN(list_item)->child );
432
433 ApplyWidgetStyle();
434 }
435
436 gtk_widget_show( list_item );
437 }
438
439 EnableEvents();
440 }
441
442 InvalidateBestSize();
443
444 return n;
445 }
446
447 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
448 {
449 m_clientData[n] = clientData;
450 }
451
452 void* wxComboBox::DoGetItemClientData(unsigned int n) const
453 {
454 return m_clientData[n];
455 }
456
457 void wxComboBox::DoClear()
458 {
459 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
460
461 DisableEvents();
462
463 #ifdef __WXGTK24__
464 if (!gtk_check_version(2,4,0))
465 {
466 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
467 const unsigned int count = GetCount();
468 for (unsigned int i = 0; i < count; i++)
469 gtk_combo_box_remove_text( combobox, 0 );
470 }
471 else // GTK+ < 2.4.0
472 #endif // __WXGTK24__
473 {
474 GtkWidget *list = GTK_COMBO(m_widget)->list;
475 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
476 }
477
478 m_clientData.Clear();
479
480 if(m_strings)
481 m_strings->Clear();
482
483 EnableEvents();
484
485 InvalidateBestSize();
486 }
487
488 void wxComboBox::DoDeleteOneItem(unsigned int n)
489 {
490 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
491
492 #ifdef __WXGTK24__
493 if (!gtk_check_version(2,4,0))
494 {
495 wxCHECK_RET( IsValid(n), wxT("invalid index") );
496
497 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
498 gtk_combo_box_remove_text( combobox, n );
499 }
500 else
501 #endif
502 {
503 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
504
505 GList *child = g_list_nth( listbox->children, n );
506
507 if (!child)
508 {
509 wxFAIL_MSG(wxT("wrong index"));
510 return;
511 }
512
513 DisableEvents();
514
515 GList *list = g_list_append( (GList*) NULL, child->data );
516 gtk_list_remove_items( listbox, list );
517 g_list_free( list );
518
519 EnableEvents();
520 }
521
522 m_clientData.RemoveAt( n );
523 if(m_strings)
524 m_strings->RemoveAt( n );
525
526 InvalidateBestSize();
527 }
528
529 void wxComboBox::SetString(unsigned int n, const wxString &text)
530 {
531 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
532
533 #ifdef __WXGTK24__
534 if (!gtk_check_version(2,4,0))
535 {
536 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
537 wxCHECK_RET( IsValid(n), wxT("invalid index") );
538
539 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
540 GtkTreeIter iter;
541 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
542 {
543 GValue value = { 0, };
544 g_value_init( &value, G_TYPE_STRING );
545 g_value_set_string( &value, wxGTK_CONV( text ) );
546 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
547 g_value_unset( &value );
548 }
549 }
550 else
551 #endif
552 {
553 GtkWidget *list = GTK_COMBO(m_widget)->list;
554
555 GList *child = g_list_nth( GTK_LIST(list)->children, n );
556 if (child)
557 {
558 GtkBin *bin = GTK_BIN( child->data );
559 GtkLabel *label = GTK_LABEL( bin->child );
560 gtk_label_set_text(label, wxGTK_CONV(text));
561 }
562 else
563 {
564 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
565 }
566 }
567
568 InvalidateBestSize();
569 }
570
571 int wxComboBox::FindString( const wxString &item, bool bCase ) const
572 {
573 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
574
575 #ifdef __WXGTK24__
576 if (!gtk_check_version(2,4,0))
577 {
578 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
579 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
580 GtkTreeIter iter;
581 gtk_tree_model_get_iter_first( model, &iter );
582 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
583 return -1;
584 int count = 0;
585 do
586 {
587 GValue value = { 0, };
588 gtk_tree_model_get_value( model, &iter, 0, &value );
589 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
590 g_value_unset( &value );
591
592 if (item.IsSameAs( str, bCase ) )
593 return count;
594
595 count++;
596
597 } while (gtk_tree_model_iter_next( model, &iter ));
598 }
599 else
600 #endif
601 {
602 GtkWidget *list = GTK_COMBO(m_widget)->list;
603
604 GList *child = GTK_LIST(list)->children;
605 int count = 0;
606 while (child)
607 {
608 GtkBin *bin = GTK_BIN( child->data );
609 GtkLabel *label = GTK_LABEL( bin->child );
610 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
611
612 if (item.IsSameAs( str , bCase ) )
613 return count;
614
615 count++;
616 child = child->next;
617 }
618 }
619
620 return wxNOT_FOUND;
621 }
622
623 int wxComboBox::GetSelection() const
624 {
625 #ifdef __WXGTK24__
626 if (!gtk_check_version(2,4,0))
627 {
628 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
629 return gtk_combo_box_get_active( combobox );
630 }
631 else
632 #endif
633 // if the popup is currently opened, use the selection as it had been
634 // before it dropped down
635 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
636 : g_SelectionBeforePopup;
637 }
638
639 int wxComboBox::GetCurrentSelection() const
640 {
641 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
642
643 #ifdef __WXGTK24__
644 if (!gtk_check_version(2,4,0))
645 {
646 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
647 return gtk_combo_box_get_active( combobox );
648 }
649 else
650 #endif
651 {
652 GtkWidget *list = GTK_COMBO(m_widget)->list;
653
654 GList *selection = GTK_LIST(list)->selection;
655 if (selection)
656 {
657 GList *child = GTK_LIST(list)->children;
658 int count = 0;
659 while (child)
660 {
661 if (child->data == selection->data) return count;
662 count++;
663 child = child->next;
664 }
665 }
666 }
667
668 return -1;
669 }
670
671 wxString wxComboBox::GetString(unsigned int n) const
672 {
673 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
674
675 wxString str;
676
677 #ifdef __WXGTK24__
678 if (!gtk_check_version(2,4,0))
679 {
680 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
681 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
682 GtkTreeIter iter;
683 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
684 {
685 GValue value = { 0, };
686 gtk_tree_model_get_value( model, &iter, 0, &value );
687 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
688 g_value_unset( &value );
689 return tmp;
690 }
691 }
692 else
693 #endif
694 {
695 GtkWidget *list = GTK_COMBO(m_widget)->list;
696
697 GList *child = g_list_nth( GTK_LIST(list)->children, n );
698 if (child)
699 {
700 GtkBin *bin = GTK_BIN( child->data );
701 GtkLabel *label = GTK_LABEL( bin->child );
702 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
703 }
704 else
705 {
706 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
707 }
708 }
709
710 return str;
711 }
712
713 wxString wxComboBox::GetStringSelection() const
714 {
715 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
716
717 #ifdef __WXGTK24__
718 if (!gtk_check_version(2,4,0))
719 {
720 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
721 int sel = gtk_combo_box_get_active( combobox );
722 if (sel == -1)
723 return wxEmptyString;
724 return GetString(sel);
725 }
726 else
727 #endif
728 {
729 GtkWidget *list = GTK_COMBO(m_widget)->list;
730
731 GList *selection = GTK_LIST(list)->selection;
732 if (selection)
733 {
734 GtkBin *bin = GTK_BIN( selection->data );
735 GtkLabel *label = GTK_LABEL( bin->child );
736 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
737 return tmp;
738 }
739
740 wxFAIL_MSG( wxT("wxComboBox: no selection") );
741 }
742
743 return wxEmptyString;
744 }
745
746 unsigned int wxComboBox::GetCount() const
747 {
748 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
749
750 #ifdef __WXGTK24__
751 if (!gtk_check_version(2,4,0))
752 {
753 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
754 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
755 GtkTreeIter iter;
756 gtk_tree_model_get_iter_first( model, &iter );
757 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
758 return 0;
759 unsigned int ret = 1;
760 while (gtk_tree_model_iter_next( model, &iter ))
761 ret++;
762 return ret;
763 }
764 else
765 #endif
766 {
767 GtkWidget *list = GTK_COMBO(m_widget)->list;
768
769 GList *child = GTK_LIST(list)->children;
770 unsigned int count = 0;
771 while (child)
772 {
773 count++;
774 child = child->next;
775 }
776 return count;
777 }
778 }
779
780 void wxComboBox::SetSelection( int n )
781 {
782 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
783
784 DisableEvents();
785
786 #ifdef __WXGTK24__
787 if (!gtk_check_version(2,4,0))
788 {
789 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
790 gtk_combo_box_set_active( combobox, n );
791 }
792 else
793 #endif
794 {
795 GtkWidget *list = GTK_COMBO(m_widget)->list;
796 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
797 gtk_list_select_item( GTK_LIST(list), n );
798 m_prevSelection = n;
799 }
800
801 EnableEvents();
802 }
803
804 wxString wxComboBox::GetValue() const
805 {
806 GtkEntry *entry = NULL;
807 #ifdef __WXGTK24__
808 if (!gtk_check_version(2,4,0))
809 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
810 else
811 #endif
812 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
813
814 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
815
816 #if 0
817 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
818 {
819 wxChar c = tmp[i];
820 printf( "%d ", (int) (c) );
821 }
822 printf( "\n" );
823 #endif
824
825 return tmp;
826 }
827
828 void wxComboBox::SetValue( const wxString& value )
829 {
830 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
831
832 GtkEntry *entry = NULL;
833 #ifdef __WXGTK24__
834 if (!gtk_check_version(2,4,0))
835 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
836 else
837 #endif
838 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
839
840 wxString tmp;
841 if (!value.IsNull()) tmp = value;
842
843 DisableEvents();
844 gtk_entry_set_text( entry, wxGTK_CONV( tmp ) );
845 EnableEvents();
846
847 InvalidateBestSize();
848 }
849
850 void wxComboBox::Copy()
851 {
852 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
853
854 GtkEntry *entry = NULL;
855 #ifdef __WXGTK24__
856 if (!gtk_check_version(2,4,0))
857 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
858 else
859 #endif
860 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
861
862 gtk_editable_copy_clipboard(GTK_EDITABLE(entry));
863 }
864
865 void wxComboBox::Cut()
866 {
867 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
868
869 GtkEntry *entry = NULL;
870 #ifdef __WXGTK24__
871 if (!gtk_check_version(2,4,0))
872 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
873 else
874 #endif
875 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
876
877 gtk_editable_cut_clipboard(GTK_EDITABLE(entry));
878 }
879
880 void wxComboBox::Paste()
881 {
882 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
883
884 GtkEntry *entry = NULL;
885 #ifdef __WXGTK24__
886 if (!gtk_check_version(2,4,0))
887 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
888 else
889 #endif
890 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
891
892 gtk_editable_paste_clipboard(GTK_EDITABLE(entry));
893 }
894
895 void wxComboBox::Undo()
896 {
897 // TODO
898 }
899
900 void wxComboBox::Redo()
901 {
902 // TODO
903 }
904
905 void wxComboBox::SelectAll()
906 {
907 SetSelection(0, GetLastPosition());
908 }
909
910 bool wxComboBox::CanUndo() const
911 {
912 // TODO
913 return false;
914 }
915
916 bool wxComboBox::CanRedo() const
917 {
918 // TODO
919 return false;
920 }
921
922 bool wxComboBox::HasSelection() const
923 {
924 long from, to;
925 GetSelection(&from, &to);
926 return from != to;
927 }
928
929 bool wxComboBox::CanCopy() const
930 {
931 // Can copy if there's a selection
932 return HasSelection();
933 }
934
935 bool wxComboBox::CanCut() const
936 {
937 return CanCopy() && IsEditable();
938 }
939
940 bool wxComboBox::CanPaste() const
941 {
942 // TODO: check for text on the clipboard
943 return IsEditable() ;
944 }
945
946 bool wxComboBox::IsEditable() const
947 {
948 return !HasFlag(wxCB_READONLY);
949 }
950
951
952 void wxComboBox::SetInsertionPoint( long pos )
953 {
954 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
955
956 if ( pos == GetLastPosition() )
957 pos = -1;
958
959 GtkEntry *entry = NULL;
960 #ifdef __WXGTK24__
961 if (!gtk_check_version(2,4,0))
962 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
963 else
964 #endif
965 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
966
967 gtk_entry_set_position( entry, (int)pos );
968 }
969
970 long wxComboBox::GetInsertionPoint() const
971 {
972 GtkEntry *entry = NULL;
973 #ifdef __WXGTK24__
974 if (!gtk_check_version(2,4,0))
975 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
976 else
977 #endif
978 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
979
980 return (long) gtk_editable_get_position(GTK_EDITABLE(entry));
981 }
982
983 wxTextPos wxComboBox::GetLastPosition() const
984 {
985 GtkEntry *entry = NULL;
986 #ifdef __WXGTK24__
987 if (!gtk_check_version(2,4,0))
988 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
989 else
990 #endif
991 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
992
993 int pos = entry->text_length;
994 return (long) pos-1;
995 }
996
997 void wxComboBox::Replace( long from, long to, const wxString& value )
998 {
999 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
1000
1001 GtkEntry *entry = NULL;
1002 #ifdef __WXGTK24__
1003 if (!gtk_check_version(2,4,0))
1004 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1005 else
1006 #endif
1007 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
1008
1009 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
1010 if (value.IsNull()) return;
1011 gint pos = (gint)to;
1012
1013 // FIXME-UTF8: wouldn't be needed if utf8_str() always returned a buffer
1014 #if wxUSE_UNICODE_UTF8
1015 const char *utf8 = value.utf8_str();
1016 #else
1017 wxCharBuffer buffer(value.utf8_str());
1018 const char *utf8 = buffer;
1019 #endif
1020 gtk_editable_insert_text(GTK_EDITABLE(entry), utf8, strlen(utf8), &pos);
1021 }
1022
1023 void wxComboBox::SetSelection( long from, long to )
1024 {
1025 GtkEntry *entry = NULL;
1026 #ifdef __WXGTK24__
1027 if (!gtk_check_version(2,4,0))
1028 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1029 else
1030 #endif
1031 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
1032
1033 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
1034 }
1035
1036 void wxComboBox::GetSelection( long* from, long* to ) const
1037 {
1038 GtkEntry *entry = NULL;
1039 #ifdef __WXGTK24__
1040 if (!gtk_check_version(2,4,0))
1041 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1042 else
1043 #endif
1044 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
1045
1046 if (IsEditable())
1047 {
1048 GtkEditable *editable = GTK_EDITABLE(entry);
1049 gint start, end;
1050 gtk_editable_get_selection_bounds(editable, & start, & end);
1051 *from = start;
1052 *to = end;
1053 }
1054 }
1055
1056 void wxComboBox::SetEditable( bool editable )
1057 {
1058 GtkEntry *entry = NULL;
1059 #ifdef __WXGTK24__
1060 if (!gtk_check_version(2,4,0))
1061 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1062 else
1063 #endif
1064 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
1065
1066 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
1067 }
1068
1069 void wxComboBox::OnChar( wxKeyEvent &event )
1070 {
1071 if ( event.GetKeyCode() == WXK_RETURN )
1072 {
1073 // GTK automatically selects an item if its in the list
1074 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
1075 eventEnter.SetString( GetValue() );
1076 eventEnter.SetInt( GetSelection() );
1077 eventEnter.SetEventObject( this );
1078
1079 if (!GetEventHandler()->ProcessEvent( eventEnter ))
1080 {
1081 // This will invoke the dialog default action, such
1082 // as the clicking the default button.
1083
1084 wxWindow *top_frame = m_parent;
1085 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1086 top_frame = top_frame->GetParent();
1087
1088 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1089 {
1090 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1091
1092 if (window->default_widget)
1093 gtk_widget_activate (window->default_widget);
1094 }
1095 }
1096
1097 // Catch GTK event so that GTK doesn't open the drop
1098 // down list upon RETURN.
1099 return;
1100 }
1101
1102 event.Skip();
1103 }
1104
1105 void wxComboBox::DisableEvents()
1106 {
1107 #ifdef __WXGTK24__
1108 if (!gtk_check_version(2,4,0))
1109 {
1110 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
1111 (gpointer)gtkcombobox_text_changed_callback, this);
1112
1113 g_signal_handlers_block_by_func(m_widget,
1114 (gpointer)gtkcombobox_changed_callback, this);
1115 }
1116 else
1117 #endif
1118 {
1119 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->list,
1120 (gpointer) gtkcombo_combo_select_child_callback, this);
1121
1122 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->entry,
1123 (gpointer) gtkcombo_text_changed_callback, this);
1124 }
1125 }
1126
1127 void wxComboBox::EnableEvents()
1128 {
1129 #ifdef __WXGTK24__
1130 if (!gtk_check_version(2,4,0))
1131 {
1132 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
1133 (gpointer)gtkcombobox_text_changed_callback, this);
1134
1135 g_signal_handlers_unblock_by_func(m_widget,
1136 (gpointer)gtkcombobox_changed_callback, this);
1137 }
1138 else
1139 #endif
1140 {
1141 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->list,
1142 (gpointer) gtkcombo_combo_select_child_callback, this);
1143
1144 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->entry,
1145 (gpointer) gtkcombo_text_changed_callback, this);
1146 }
1147 }
1148
1149 void wxComboBox::OnSize( wxSizeEvent &event )
1150 {
1151 #ifdef __WXGTK24__
1152 if (!gtk_check_version(2,4,0))
1153 {
1154 // Do nothing
1155 }
1156 else
1157 #endif
1158 {
1159 // NB: In some situations (e.g. on non-first page of a wizard, if the
1160 // size used is default size), GtkCombo widget is resized correctly,
1161 // but it's look is not updated, it's rendered as if it was much wider.
1162 // No other widgets are affected, so it looks like a bug in GTK+.
1163 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1164 // fixes it.
1165 if (GTK_WIDGET_VISIBLE(m_widget))
1166 gtk_widget_queue_resize(m_widget);
1167 }
1168
1169 event.Skip();
1170 }
1171
1172 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
1173 {
1174 #ifdef __WXGTK24__
1175 if (!gtk_check_version(2,4,0))
1176 {
1177 // Do nothing
1178 }
1179 else
1180 #endif
1181 {
1182 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1183
1184 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
1185 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
1186
1187 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
1188 GList *child = list->children;
1189 while (child)
1190 {
1191 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
1192
1193 GtkBin *bin = GTK_BIN(child->data);
1194 gtk_widget_modify_style( bin->child, style );
1195
1196 child = child->next;
1197 }
1198 }
1199 }
1200
1201 GtkWidget* wxComboBox::GetConnectWidget()
1202 {
1203 GtkEntry *entry = NULL;
1204 #ifdef __WXGTK24__
1205 if (!gtk_check_version(2,4,0))
1206 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1207 else
1208 #endif
1209 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
1210
1211 return GTK_WIDGET( entry );
1212 }
1213
1214 GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
1215 {
1216 #ifdef __WXGTK24__
1217 if (!gtk_check_version(2,4,0))
1218 {
1219 wxUnusedVar(windows);
1220
1221 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
1222 }
1223 else
1224 #endif // GTK+ 2.4
1225 {
1226 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
1227 windows.push_back(GTK_COMBO(m_widget)->button->window);
1228
1229 // indicate that we return multiple windows in the windows array
1230 return NULL;
1231 }
1232 }
1233
1234 wxSize wxComboBox::DoGetBestSize() const
1235 {
1236 wxSize ret( wxControl::DoGetBestSize() );
1237
1238 // we know better our horizontal extent: it depends on the longest string
1239 // in the combobox
1240 if ( m_widget )
1241 {
1242 int width;
1243 unsigned int count = GetCount();
1244 for ( unsigned int n = 0; n < count; n++ )
1245 {
1246 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
1247 if ( width > ret.x )
1248 ret.x = width;
1249 }
1250 }
1251
1252 // empty combobox should have some reasonable default size too
1253 if ( ret.x < 100 )
1254 ret.x = 100;
1255
1256 CacheBestSize(ret);
1257 return ret;
1258 }
1259
1260 // static
1261 wxVisualAttributes
1262 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1263 {
1264 #ifdef __WXGTK24__
1265 if (!gtk_check_version(2,4,0))
1266 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
1267 else
1268 #endif
1269 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
1270 }
1271
1272 // ----------------------------------------------------------------------------
1273 // standard event handling
1274 // ----------------------------------------------------------------------------
1275
1276 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
1277 {
1278 Cut();
1279 }
1280
1281 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
1282 {
1283 Copy();
1284 }
1285
1286 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
1287 {
1288 Paste();
1289 }
1290
1291 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1292 {
1293 Undo();
1294 }
1295
1296 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1297 {
1298 Redo();
1299 }
1300
1301 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1302 {
1303 long from, to;
1304 GetSelection(& from, & to);
1305 if (from != -1 && to != -1)
1306 Remove(from, to);
1307 }
1308
1309 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1310 {
1311 SetSelection(-1, -1);
1312 }
1313
1314 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1315 {
1316 event.Enable( CanCut() );
1317 }
1318
1319 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1320 {
1321 event.Enable( CanCopy() );
1322 }
1323
1324 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1325 {
1326 event.Enable( CanPaste() );
1327 }
1328
1329 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1330 {
1331 event.Enable( CanUndo() );
1332 }
1333
1334 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1335 {
1336 event.Enable( CanRedo() );
1337 }
1338
1339 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1340 {
1341 event.Enable(HasSelection() && IsEditable()) ;
1342 }
1343
1344 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1345 {
1346 event.Enable(GetLastPosition() > 0);
1347 }
1348
1349 #endif