]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
reverted unrelated changes accidentally committed as part of r49411
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
dcf924a3
RR
13#if wxUSE_COMBOBOX
14
8228b893
WS
15#include "wx/combobox.h"
16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
9eddec69 19 #include "wx/settings.h"
fec9cc08 20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
aaa6d89a 21 #include "wx/arrstr.h"
88a7a4e1
WS
22#endif
23
1efb5db8
MR
24// We use GtkCombo which has been deprecated since GTK+ 2.3.0
25// in favour of GtkComboBox for <GTK2.4 runtime
5b008dd7
MR
26// We also use GtkList
27#ifdef GTK_DISABLE_DEPRECATED
1efb5db8
MR
28#undef GTK_DISABLE_DEPRECATED
29#endif
9e691f46 30#include "wx/gtk/private.h"
83624f79 31
47908e25
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
40eb3606
VZ
37static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
38
78b3b018
RR
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
865bb325 44extern "C" {
78b3b018 45static void
1fb33bdb 46gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
78b3b018 47{
78b3b018 48 if (combo->m_ignoreNextUpdate)
150e31d2 49 {
7d8268a1 50 combo->m_ignoreNextUpdate = false;
78b3b018
RR
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}
865bb325 61}
78b3b018 62
865bb325 63extern "C" {
78b3b018 64static void
1fb33bdb 65gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
78b3b018
RR
66{
67}
865bb325 68}
78b3b018 69
865bb325 70extern "C" {
9d6a9fdd 71static void
1fb33bdb 72gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
7d8268a1 73{
9d6a9fdd
RR
74 // when the popup is hidden, throw a SELECTED event only if the combobox
75 // selection changed.
3dbfe8f4
VZ
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 )
9d6a9fdd
RR
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 );
345bdf13
KH
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 );
9d6a9fdd 98 }
9d6a9fdd 99}
865bb325 100}
9d6a9fdd 101
865bb325 102extern "C" {
9d6a9fdd 103static void
1fb33bdb 104gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
9d6a9fdd
RR
105{
106 // store the combobox selection value before the popup is shown
40eb3606 107 g_SelectionBeforePopup = combo->GetCurrentSelection();
9d6a9fdd 108}
865bb325 109}
9d6a9fdd 110
53010e52 111//-----------------------------------------------------------------------------
461573cc 112// "select-child" - click/cursor get select-child, changed, select-child
47908e25 113//-----------------------------------------------------------------------------
47908e25 114
865bb325 115extern "C" {
8a85884a 116static void
1fb33bdb 117gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 118{
a2053b27 119 if (!combo->m_hasVMT) return;
30ed6e5c 120
acfd422a 121 if (g_blockEventsOnDrag) return;
805dd538 122
40eb3606 123 int curSelection = combo->GetCurrentSelection();
30ed6e5c 124
3c4e4af6
RR
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 );
150e31d2 129
159b66c0
RR
130 combo->m_prevSelection = curSelection;
131
78b3b018
RR
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
134 // is sent.
98264520
PC
135 GtkWidget* entry = GTK_COMBO(combo->GetHandle())->entry;
136 g_signal_handlers_block_by_func(
137 entry, (gpointer)gtkcombo_text_changed_callback, combo);
78b3b018 138 combo->SetValue( combo->GetStringSelection() );
98264520
PC
139 g_signal_handlers_unblock_by_func(
140 entry, (gpointer)gtkcombo_text_changed_callback, combo);
78b3b018 141
40eb3606 142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
1fb33bdb 143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
9d6a9fdd
RR
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 ...
40eb3606 147 if (g_SelectionBeforePopup == wxID_NONE)
9d6a9fdd
RR
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 );
0c77152e 154
345bdf13 155 // for consistency with the other ports, don't generate text update
40eb3606
VZ
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 }
461573cc 162}
865bb325 163}
461573cc 164
590f50d6
RR
165#ifdef __WXGTK24__
166extern "C" {
167static void
168gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
169{
590f50d6
RR
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
179extern "C" {
180static void
181gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
182{
590f50d6 183 if (!combo->m_hasVMT) return;
ddd53873
RR
184
185 if (combo->GetSelection() == -1)
186 return;
590f50d6
RR
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}
a73ae836 195
590f50d6
RR
196#endif
197
e1e955e1
RR
198//-----------------------------------------------------------------------------
199// wxComboBox
53010e52
RR
200//-----------------------------------------------------------------------------
201
202IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
203
b4071e91 204BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 205 EVT_SIZE(wxComboBox::OnSize)
8a85884a 206 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
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)
b4071e91
RR
223END_EVENT_TABLE()
224
584ad2a3
MB
225bool 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
fd0eed64
RR
238bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
239 const wxPoint& pos, const wxSize& size,
240 int n, const wxString choices[],
805dd538
VZ
241 long style, const wxValidator& validator,
242 const wxString& name )
53010e52 243{
a236aa20 244 m_strings = NULL;
7d8268a1 245 m_ignoreNextUpdate = false;
159b66c0 246 m_prevSelection = 0;
805dd538 247
db434467 248 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
249 !CreateBase( parent, id, pos, size, style, validator, name ))
250 {
223d09f6 251 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 252 return false;
4dcaf11a 253 }
6de97a3b 254
a236aa20
VZ
255 if(HasFlag(wxCB_SORT))
256 m_strings = new wxSortedArrayString();
257
590f50d6
RR
258#ifdef __WXGTK24__
259 if (!gtk_check_version(2,4,0))
260 {
261 m_widget = gtk_combo_box_entry_new_text();
8228b893 262
590f50d6 263 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE );
590f50d6
RR
264 }
265 else
266#endif
267 {
268 m_widget = gtk_combo_new();
269 GtkCombo* combo = GTK_COMBO(m_widget);
8228b893 270
590f50d6
RR
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",
1fb33bdb 275 G_CALLBACK (gtkcombo_dummy_callback),
9fa72bd2 276 combo);
805dd538 277
590f50d6
RR
278 // make it more useable
279 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
30ed6e5c 280
590f50d6
RR
281 // and case-sensitive
282 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
3ca6a5f0 283
590f50d6 284 if (style & wxNO_BORDER)
7d1fea10 285 g_object_set (combo->entry, "has-frame", FALSE, NULL );
fd0eed64 286 }
805dd538 287
a236aa20 288 Append(n, choices);
590f50d6 289
f03fc89f 290 m_parent->DoAddChild( this );
30ed6e5c 291
590f50d6
RR
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
8228b893 297#endif
590f50d6 298 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 299
590f50d6 300 m_focusWidget = GTK_WIDGET( entry );
805dd538 301
abdeb9e7 302 PostCreation(size);
53010e52 303
590f50d6
RR
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 );
805dd538 310
590f50d6
RR
311#ifdef __WXGTK24__
312 if (!gtk_check_version(2,4,0))
313 {
314 gtk_entry_set_text( entry, wxGTK_CONV(value) );
8228b893 315
590f50d6
RR
316 if (style & wxCB_READONLY)
317 gtk_entry_set_editable( entry, FALSE );
8228b893 318
590f50d6
RR
319 g_signal_connect_after (entry, "changed",
320 G_CALLBACK (gtkcombobox_text_changed_callback), this);
8228b893 321
590f50d6
RR
322 g_signal_connect_after (m_widget, "changed",
323 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 324
590f50d6
RR
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) );
805dd538 333
590f50d6
RR
334 if (style & wxCB_READONLY)
335 gtk_entry_set_editable( entry, FALSE );
a260fe6a 336
590f50d6
RR
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",
1fb33bdb 340 G_CALLBACK (gtkcombo_popup_hide_callback), this);
590f50d6 341 g_signal_connect (GTK_COMBO(combo)->popwin, "show",
1fb33bdb 342 G_CALLBACK (gtkcombo_popup_show_callback), this);
590f50d6 343 g_signal_connect_after (combo->list, "select-child",
1fb33bdb 344 G_CALLBACK (gtkcombo_combo_select_child_callback),
9fa72bd2 345 this);
590f50d6
RR
346 g_signal_connect_after (entry, "changed",
347 G_CALLBACK (gtkcombo_text_changed_callback), this);
590f50d6 348 }
805dd538 349
170acdc9 350 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 351
7d8268a1 352 return true;
fd0eed64
RR
353}
354
0ec1179b
VZ
355GtkEditable *wxComboBox::GetEditable() const
356{
357#ifdef __WXGTK24__
358 if ( !gtk_check_version(2,4,0) )
359 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
360 else
361#endif
362 return GTK_EDITABLE( GTK_COMBO(m_widget)->entry );
363}
364
fd0eed64
RR
365wxComboBox::~wxComboBox()
366{
a236aa20 367 Clear();
7d6d2cd4 368
a236aa20 369 delete m_strings;
6de97a3b 370}
53010e52 371
2b5f62a0
VZ
372void wxComboBox::SetFocus()
373{
374 if ( m_hasFocus )
375 {
376 // don't do anything if we already have focus
377 return;
378 }
379
380 gtk_widget_grab_focus( m_focusWidget );
381}
382
a236aa20
VZ
383int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
384 unsigned int pos,
385 void **clientData, wxClientDataType type)
53010e52 386{
2a68b7a0 387 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 388
a236aa20
VZ
389 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
390 _T("In a sorted combobox data could only be appended"));
391
392 const int count = items.GetCount();
393
394 int n = wxNOT_FOUND;
395
590f50d6
RR
396#ifdef __WXGTK24__
397 if (!gtk_check_version(2,4,0))
398 {
399 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
a236aa20
VZ
400 for( int i = 0; i < count; ++i )
401 {
402 n = pos + i;
403 // If sorted, use this wxSortedArrayStrings to determine
404 // the right insertion point
405 if(m_strings)
406 n = m_strings->Add(items[i]);
407
408 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
409
410 m_clientData.Insert( NULL, n );
411 AssignNewItemClientData(n, clientData, i, type);
412 }
590f50d6
RR
413 }
414 else
415#endif
416 {
417 DisableEvents();
805dd538 418
590f50d6 419 GtkWidget *list = GTK_COMBO(m_widget)->list;
a236aa20 420 for( int i = 0; i < count; ++i )
590f50d6 421 {
a236aa20
VZ
422 n = pos + i;
423 // If sorted, use this wxSortedArrayStrings to determine
424 // the right insertion point
425 if(m_strings)
426 n = m_strings->Add(items[i]);
243dbf1a 427
a236aa20 428 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
6f6f938f 429
a236aa20
VZ
430 // TODO construct a list with all items and call gtk_list_insert_items once?
431 GList *gitem_list = g_list_alloc ();
432 gitem_list->data = list_item;
433 gtk_list_insert_items( GTK_LIST (list), gitem_list, n );
243dbf1a 434
a236aa20
VZ
435 m_clientData.Insert( NULL, n );
436 AssignNewItemClientData(n, clientData, i, type);
243dbf1a 437
a236aa20
VZ
438 if (GTK_WIDGET_REALIZED(m_widget))
439 {
440 gtk_widget_realize( list_item );
441 gtk_widget_realize( GTK_BIN(list_item)->child );
243dbf1a 442
a236aa20
VZ
443 ApplyWidgetStyle();
444 }
243dbf1a 445
a236aa20 446 gtk_widget_show( list_item );
590f50d6 447 }
243dbf1a 448
590f50d6
RR
449 EnableEvents();
450 }
8228b893 451
b0021947 452 InvalidateBestSize();
243dbf1a 453
a236aa20 454 return n;
243dbf1a
VZ
455}
456
aa61d352 457void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 458{
a236aa20 459 m_clientData[n] = clientData;
6de97a3b 460}
53010e52 461
aa61d352 462void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 463{
a236aa20 464 return m_clientData[n];
fd0eed64
RR
465}
466
a236aa20 467void wxComboBox::DoClear()
fd0eed64 468{
223d09f6 469 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 470
461573cc 471 DisableEvents();
30ed6e5c 472
590f50d6
RR
473#ifdef __WXGTK24__
474 if (!gtk_check_version(2,4,0))
475 {
476 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
aa61d352
VZ
477 const unsigned int count = GetCount();
478 for (unsigned int i = 0; i < count; i++)
590f50d6
RR
479 gtk_combo_box_remove_text( combobox, 0 );
480 }
c1a3ff25
VZ
481 else // GTK+ < 2.4.0
482#endif // __WXGTK24__
590f50d6
RR
483 {
484 GtkWidget *list = GTK_COMBO(m_widget)->list;
485 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
486 }
8228b893 487
a236aa20 488 m_clientData.Clear();
805dd538 489
a236aa20
VZ
490 if(m_strings)
491 m_strings->Clear();
30ed6e5c 492
461573cc 493 EnableEvents();
b0021947
VS
494
495 InvalidateBestSize();
6de97a3b 496}
53010e52 497
a236aa20 498void wxComboBox::DoDeleteOneItem(unsigned int n)
53010e52 499{
223d09f6 500 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 501
590f50d6
RR
502#ifdef __WXGTK24__
503 if (!gtk_check_version(2,4,0))
fd0eed64 504 {
8228b893
WS
505 wxCHECK_RET( IsValid(n), wxT("invalid index") );
506
590f50d6
RR
507 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
508 gtk_combo_box_remove_text( combobox, n );
fd0eed64 509 }
590f50d6
RR
510 else
511#endif
512 {
513 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 514
590f50d6
RR
515 GList *child = g_list_nth( listbox->children, n );
516
517 if (!child)
518 {
519 wxFAIL_MSG(wxT("wrong index"));
520 return;
521 }
30ed6e5c 522
590f50d6 523 DisableEvents();
805dd538 524
590f50d6
RR
525 GList *list = g_list_append( (GList*) NULL, child->data );
526 gtk_list_remove_items( listbox, list );
527 g_list_free( list );
528
529 EnableEvents();
530 }
8228b893 531
a236aa20
VZ
532 m_clientData.RemoveAt( n );
533 if(m_strings)
534 m_strings->RemoveAt( n );
150e31d2 535
b0021947 536 InvalidateBestSize();
461573cc
RR
537}
538
aa61d352 539void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
540{
541 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
542
590f50d6
RR
543#ifdef __WXGTK24__
544 if (!gtk_check_version(2,4,0))
461573cc 545 {
590f50d6 546 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
8228b893
WS
547 wxCHECK_RET( IsValid(n), wxT("invalid index") );
548
590f50d6
RR
549 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
550 GtkTreeIter iter;
551 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
552 {
553 GValue value = { 0, };
554 g_value_init( &value, G_TYPE_STRING );
555 g_value_set_string( &value, wxGTK_CONV( text ) );
556 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
557 g_value_unset( &value );
558 }
461573cc
RR
559 }
560 else
590f50d6 561#endif
461573cc 562 {
590f50d6 563 GtkWidget *list = GTK_COMBO(m_widget)->list;
150e31d2 564
590f50d6
RR
565 GList *child = g_list_nth( GTK_LIST(list)->children, n );
566 if (child)
567 {
568 GtkBin *bin = GTK_BIN( child->data );
569 GtkLabel *label = GTK_LABEL( bin->child );
570 gtk_label_set_text(label, wxGTK_CONV(text));
571 }
572 else
573 {
574 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
575 }
576 }
8228b893 577
b0021947 578 InvalidateBestSize();
6de97a3b 579}
53010e52 580
11e62fe6 581int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 582{
0a164d4c 583 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 584
590f50d6
RR
585#ifdef __WXGTK24__
586 if (!gtk_check_version(2,4,0))
53010e52 587 {
590f50d6
RR
588 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
589 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
590 GtkTreeIter iter;
591 gtk_tree_model_get_iter_first( model, &iter );
592 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
593 return -1;
594 int count = 0;
8228b893 595 do
590f50d6
RR
596 {
597 GValue value = { 0, };
598 gtk_tree_model_get_value( model, &iter, 0, &value );
599 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
600 g_value_unset( &value );
8228b893 601
590f50d6
RR
602 if (item.IsSameAs( str, bCase ) )
603 return count;
8228b893 604
590f50d6 605 count++;
8228b893 606
590f50d6
RR
607 } while (gtk_tree_model_iter_next( model, &iter ));
608 }
609 else
610#endif
611 {
612 GtkWidget *list = GTK_COMBO(m_widget)->list;
68567a96 613
590f50d6
RR
614 GList *child = GTK_LIST(list)->children;
615 int count = 0;
616 while (child)
617 {
618 GtkBin *bin = GTK_BIN( child->data );
619 GtkLabel *label = GTK_LABEL( bin->child );
620 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
621
622 if (item.IsSameAs( str , bCase ) )
623 return count;
30ed6e5c 624
590f50d6
RR
625 count++;
626 child = child->next;
627 }
fd0eed64 628 }
805dd538 629
7cf8cb48 630 return wxNOT_FOUND;
fd0eed64
RR
631}
632
633int wxComboBox::GetSelection() const
40eb3606 634{
590f50d6
RR
635#ifdef __WXGTK24__
636 if (!gtk_check_version(2,4,0))
637 {
638 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
639 return gtk_combo_box_get_active( combobox );
640 }
641 else
642#endif
643 // if the popup is currently opened, use the selection as it had been
644 // before it dropped down
645 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
40eb3606
VZ
646 : g_SelectionBeforePopup;
647}
648
649int wxComboBox::GetCurrentSelection() const
fd0eed64 650{
223d09f6 651 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 652
590f50d6
RR
653#ifdef __WXGTK24__
654 if (!gtk_check_version(2,4,0))
fd0eed64 655 {
590f50d6
RR
656 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
657 return gtk_combo_box_get_active( combobox );
658 }
659 else
660#endif
661 {
662 GtkWidget *list = GTK_COMBO(m_widget)->list;
663
664 GList *selection = GTK_LIST(list)->selection;
665 if (selection)
fd0eed64 666 {
590f50d6
RR
667 GList *child = GTK_LIST(list)->children;
668 int count = 0;
669 while (child)
670 {
671 if (child->data == selection->data) return count;
672 count++;
673 child = child->next;
674 }
fd0eed64 675 }
6de97a3b 676 }
805dd538 677
fd0eed64 678 return -1;
6de97a3b 679}
53010e52 680
aa61d352 681wxString wxComboBox::GetString(unsigned int n) const
53010e52 682{
0a164d4c 683 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 684
7cf8cb48 685 wxString str;
8228b893 686
590f50d6
RR
687#ifdef __WXGTK24__
688 if (!gtk_check_version(2,4,0))
fd0eed64 689 {
590f50d6
RR
690 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
691 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
692 GtkTreeIter iter;
693 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
694 {
695 GValue value = { 0, };
696 gtk_tree_model_get_value( model, &iter, 0, &value );
697 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
698 g_value_unset( &value );
699 return tmp;
700 }
7cf8cb48
VZ
701 }
702 else
590f50d6 703#endif
8228b893 704 {
590f50d6
RR
705 GtkWidget *list = GTK_COMBO(m_widget)->list;
706
707 GList *child = g_list_nth( GTK_LIST(list)->children, n );
708 if (child)
709 {
710 GtkBin *bin = GTK_BIN( child->data );
711 GtkLabel *label = GTK_LABEL( bin->child );
712 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
713 }
714 else
715 {
716 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
717 }
fd0eed64 718 }
805dd538 719
7cf8cb48 720 return str;
6de97a3b 721}
53010e52 722
aa61d352 723unsigned int wxComboBox::GetCount() const
53010e52 724{
223d09f6 725 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 726
590f50d6
RR
727#ifdef __WXGTK24__
728 if (!gtk_check_version(2,4,0))
729 {
730 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
731 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
732 GtkTreeIter iter;
733 gtk_tree_model_get_iter_first( model, &iter );
734 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
735 return 0;
aa61d352 736 unsigned int ret = 1;
590f50d6
RR
737 while (gtk_tree_model_iter_next( model, &iter ))
738 ret++;
739 return ret;
740 }
741 else
742#endif
8228b893 743 {
590f50d6 744 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 745
590f50d6 746 GList *child = GTK_LIST(list)->children;
aa61d352
VZ
747 unsigned int count = 0;
748 while (child)
749 {
750 count++;
751 child = child->next;
752 }
590f50d6
RR
753 return count;
754 }
6de97a3b 755}
53010e52 756
debe6624 757void wxComboBox::SetSelection( int n )
53010e52 758{
223d09f6 759 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 760
953704c1
RR
761 DisableEvents();
762
590f50d6
RR
763#ifdef __WXGTK24__
764 if (!gtk_check_version(2,4,0))
765 {
766 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
767 gtk_combo_box_set_active( combobox, n );
768 }
769 else
770#endif
771 {
772 GtkWidget *list = GTK_COMBO(m_widget)->list;
773 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
774 gtk_list_select_item( GTK_LIST(list), n );
775 m_prevSelection = n;
776 }
8228b893 777
953704c1 778 EnableEvents();
6de97a3b 779}
53010e52 780
8a85884a
VZ
781void wxComboBox::OnChar( wxKeyEvent &event )
782{
12a3f227 783 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 784 {
461573cc 785 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
786 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
787 eventEnter.SetString( GetValue() );
788 eventEnter.SetInt( GetSelection() );
789 eventEnter.SetEventObject( this );
3352cfff 790
17a1ebd1 791 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
792 {
793 // This will invoke the dialog default action, such
794 // as the clicking the default button.
795
796 wxWindow *top_frame = m_parent;
797 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
798 top_frame = top_frame->GetParent();
799
800 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
801 {
802 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
803
804 if (window->default_widget)
150e31d2 805 gtk_widget_activate (window->default_widget);
3352cfff
RR
806 }
807 }
30ed6e5c 808
461573cc
RR
809 // Catch GTK event so that GTK doesn't open the drop
810 // down list upon RETURN.
0878fb4c 811 return;
8a85884a 812 }
30ed6e5c 813
7cf8cb48 814 event.Skip();
8a85884a
VZ
815}
816
953704c1
RR
817void wxComboBox::DisableEvents()
818{
590f50d6
RR
819#ifdef __WXGTK24__
820 if (!gtk_check_version(2,4,0))
821 {
98264520
PC
822 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
823 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 824
98264520
PC
825 g_signal_handlers_block_by_func(m_widget,
826 (gpointer)gtkcombobox_changed_callback, this);
590f50d6
RR
827 }
828 else
8228b893 829#endif
590f50d6 830 {
98264520
PC
831 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->list,
832 (gpointer) gtkcombo_combo_select_child_callback, this);
8228b893 833
98264520
PC
834 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->entry,
835 (gpointer) gtkcombo_text_changed_callback, this);
590f50d6 836 }
953704c1
RR
837}
838
839void wxComboBox::EnableEvents()
840{
590f50d6
RR
841#ifdef __WXGTK24__
842 if (!gtk_check_version(2,4,0))
843 {
98264520
PC
844 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
845 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 846
98264520
PC
847 g_signal_handlers_unblock_by_func(m_widget,
848 (gpointer)gtkcombobox_changed_callback, this);
590f50d6
RR
849 }
850 else
8228b893 851#endif
590f50d6 852 {
98264520
PC
853 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->list,
854 (gpointer) gtkcombo_combo_select_child_callback, this);
855
856 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->entry,
857 (gpointer) gtkcombo_text_changed_callback, this);
590f50d6 858 }
953704c1
RR
859}
860
b4071e91
RR
861void wxComboBox::OnSize( wxSizeEvent &event )
862{
590f50d6
RR
863#ifdef __WXGTK24__
864 if (!gtk_check_version(2,4,0))
865 {
866 // Do nothing
867 }
868 else
869#endif
870 {
871 // NB: In some situations (e.g. on non-first page of a wizard, if the
872 // size used is default size), GtkCombo widget is resized correctly,
873 // but it's look is not updated, it's rendered as if it was much wider.
874 // No other widgets are affected, so it looks like a bug in GTK+.
875 // Manually requesting resize calculation (as gtk_pizza_set_size does)
876 // fixes it.
877 if (GTK_WIDGET_VISIBLE(m_widget))
878 gtk_widget_queue_resize(m_widget);
879 }
260a67b7 880
f03fc89f 881 event.Skip();
6de97a3b 882}
53010e52 883
f40fdaa3 884void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 885{
590f50d6
RR
886#ifdef __WXGTK24__
887 if (!gtk_check_version(2,4,0))
888 {
889 // Do nothing
890 }
891 else
892#endif
893 {
f40fdaa3 894// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 895
590f50d6
RR
896 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
897 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 898
590f50d6
RR
899 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
900 GList *child = list->children;
901 while (child)
902 {
903 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 904
590f50d6
RR
905 GtkBin *bin = GTK_BIN(child->data);
906 gtk_widget_modify_style( bin->child, style );
805dd538 907
590f50d6
RR
908 child = child->next;
909 }
fd0eed64 910 }
868a2826 911}
b4071e91 912
fd0eed64 913GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 914{
590f50d6
RR
915 GtkEntry *entry = NULL;
916#ifdef __WXGTK24__
917 if (!gtk_check_version(2,4,0))
918 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
919 else
8228b893 920#endif
590f50d6 921 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 922
590f50d6 923 return GTK_WIDGET( entry );
97b3455a
RR
924}
925
ef5c70f9 926GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
b4071e91 927{
590f50d6
RR
928#ifdef __WXGTK24__
929 if (!gtk_check_version(2,4,0))
930 {
ef5c70f9
VZ
931 wxUnusedVar(windows);
932
933 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
590f50d6
RR
934 }
935 else
ef5c70f9 936#endif // GTK+ 2.4
590f50d6 937 {
ef5c70f9
VZ
938 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
939 windows.push_back(GTK_COMBO(m_widget)->button->window);
940
941 // indicate that we return multiple windows in the windows array
942 return NULL;
590f50d6 943 }
b4071e91 944}
ac57418f 945
f68586e5
VZ
946wxSize wxComboBox::DoGetBestSize() const
947{
db434467 948 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
949
950 // we know better our horizontal extent: it depends on the longest string
951 // in the combobox
a6fc8ae3
VZ
952 if ( m_widget )
953 {
60d85ccb 954 int width;
aa61d352
VZ
955 unsigned int count = GetCount();
956 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 957 {
aa61d352 958 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
959 if ( width > ret.x )
960 ret.x = width;
961 }
962 }
963
964 // empty combobox should have some reasonable default size too
965 if ( ret.x < 100 )
966 ret.x = 100;
9f884528
RD
967
968 CacheBestSize(ret);
db434467 969 return ret;
f68586e5
VZ
970}
971
9d522606
RD
972// static
973wxVisualAttributes
974wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
975{
590f50d6
RR
976#ifdef __WXGTK24__
977 if (!gtk_check_version(2,4,0))
978 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
979 else
980#endif
981 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
9d522606
RD
982}
983
150e31d2
JS
984// ----------------------------------------------------------------------------
985// standard event handling
986// ----------------------------------------------------------------------------
987
988void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
989{
990 Cut();
991}
992
993void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
994{
995 Copy();
996}
997
998void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
999{
1000 Paste();
1001}
1002
1003void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1004{
1005 Undo();
1006}
1007
1008void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1009{
1010 Redo();
1011}
1012
1013void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1014{
1015 long from, to;
1016 GetSelection(& from, & to);
1017 if (from != -1 && to != -1)
1018 Remove(from, to);
1019}
1020
1021void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1022{
1023 SetSelection(-1, -1);
1024}
1025
1026void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1027{
1028 event.Enable( CanCut() );
1029}
1030
1031void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1032{
1033 event.Enable( CanCopy() );
1034}
1035
1036void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1037{
1038 event.Enable( CanPaste() );
1039}
1040
1041void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1042{
1043 event.Enable( CanUndo() );
1044}
1045
1046void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1047{
1048 event.Enable( CanRedo() );
1049}
1050
1051void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1052{
1053 event.Enable(HasSelection() && IsEditable()) ;
1054}
1055
1056void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1057{
1058 event.Enable(GetLastPosition() > 0);
1059}
1060
0ec1179b 1061#endif // wxUSE_COMBOBOX