]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
change wxDataViewModel::Compare() to including column and sortorder
[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
355wxComboBox::~wxComboBox()
356{
a236aa20 357 Clear();
7d6d2cd4 358
a236aa20 359 delete m_strings;
6de97a3b 360}
53010e52 361
2b5f62a0
VZ
362void 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
a236aa20
VZ
373int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
374 unsigned int pos,
375 void **clientData, wxClientDataType type)
53010e52 376{
2a68b7a0 377 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 378
a236aa20
VZ
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
590f50d6
RR
386#ifdef __WXGTK24__
387 if (!gtk_check_version(2,4,0))
388 {
389 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
a236aa20
VZ
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 }
590f50d6
RR
403 }
404 else
405#endif
406 {
407 DisableEvents();
805dd538 408
590f50d6 409 GtkWidget *list = GTK_COMBO(m_widget)->list;
a236aa20 410 for( int i = 0; i < count; ++i )
590f50d6 411 {
a236aa20
VZ
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]);
243dbf1a 417
a236aa20 418 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
6f6f938f 419
a236aa20
VZ
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 );
243dbf1a 424
a236aa20
VZ
425 m_clientData.Insert( NULL, n );
426 AssignNewItemClientData(n, clientData, i, type);
243dbf1a 427
a236aa20
VZ
428 if (GTK_WIDGET_REALIZED(m_widget))
429 {
430 gtk_widget_realize( list_item );
431 gtk_widget_realize( GTK_BIN(list_item)->child );
243dbf1a 432
a236aa20
VZ
433 ApplyWidgetStyle();
434 }
243dbf1a 435
a236aa20 436 gtk_widget_show( list_item );
590f50d6 437 }
243dbf1a 438
590f50d6
RR
439 EnableEvents();
440 }
8228b893 441
b0021947 442 InvalidateBestSize();
243dbf1a 443
a236aa20 444 return n;
243dbf1a
VZ
445}
446
aa61d352 447void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 448{
a236aa20 449 m_clientData[n] = clientData;
6de97a3b 450}
53010e52 451
aa61d352 452void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 453{
a236aa20 454 return m_clientData[n];
fd0eed64
RR
455}
456
a236aa20 457void wxComboBox::DoClear()
fd0eed64 458{
223d09f6 459 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 460
461573cc 461 DisableEvents();
30ed6e5c 462
590f50d6
RR
463#ifdef __WXGTK24__
464 if (!gtk_check_version(2,4,0))
465 {
466 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
aa61d352
VZ
467 const unsigned int count = GetCount();
468 for (unsigned int i = 0; i < count; i++)
590f50d6
RR
469 gtk_combo_box_remove_text( combobox, 0 );
470 }
c1a3ff25
VZ
471 else // GTK+ < 2.4.0
472#endif // __WXGTK24__
590f50d6
RR
473 {
474 GtkWidget *list = GTK_COMBO(m_widget)->list;
475 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
476 }
8228b893 477
a236aa20 478 m_clientData.Clear();
805dd538 479
a236aa20
VZ
480 if(m_strings)
481 m_strings->Clear();
30ed6e5c 482
461573cc 483 EnableEvents();
b0021947
VS
484
485 InvalidateBestSize();
6de97a3b 486}
53010e52 487
a236aa20 488void wxComboBox::DoDeleteOneItem(unsigned int n)
53010e52 489{
223d09f6 490 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 491
590f50d6
RR
492#ifdef __WXGTK24__
493 if (!gtk_check_version(2,4,0))
fd0eed64 494 {
8228b893
WS
495 wxCHECK_RET( IsValid(n), wxT("invalid index") );
496
590f50d6
RR
497 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
498 gtk_combo_box_remove_text( combobox, n );
fd0eed64 499 }
590f50d6
RR
500 else
501#endif
502 {
503 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 504
590f50d6
RR
505 GList *child = g_list_nth( listbox->children, n );
506
507 if (!child)
508 {
509 wxFAIL_MSG(wxT("wrong index"));
510 return;
511 }
30ed6e5c 512
590f50d6 513 DisableEvents();
805dd538 514
590f50d6
RR
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 }
8228b893 521
a236aa20
VZ
522 m_clientData.RemoveAt( n );
523 if(m_strings)
524 m_strings->RemoveAt( n );
150e31d2 525
b0021947 526 InvalidateBestSize();
461573cc
RR
527}
528
aa61d352 529void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
530{
531 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
532
590f50d6
RR
533#ifdef __WXGTK24__
534 if (!gtk_check_version(2,4,0))
461573cc 535 {
590f50d6 536 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
8228b893
WS
537 wxCHECK_RET( IsValid(n), wxT("invalid index") );
538
590f50d6
RR
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 }
461573cc
RR
549 }
550 else
590f50d6 551#endif
461573cc 552 {
590f50d6 553 GtkWidget *list = GTK_COMBO(m_widget)->list;
150e31d2 554
590f50d6
RR
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 }
8228b893 567
b0021947 568 InvalidateBestSize();
6de97a3b 569}
53010e52 570
11e62fe6 571int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 572{
0a164d4c 573 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 574
590f50d6
RR
575#ifdef __WXGTK24__
576 if (!gtk_check_version(2,4,0))
53010e52 577 {
590f50d6
RR
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;
8228b893 585 do
590f50d6
RR
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 );
8228b893 591
590f50d6
RR
592 if (item.IsSameAs( str, bCase ) )
593 return count;
8228b893 594
590f50d6 595 count++;
8228b893 596
590f50d6
RR
597 } while (gtk_tree_model_iter_next( model, &iter ));
598 }
599 else
600#endif
601 {
602 GtkWidget *list = GTK_COMBO(m_widget)->list;
68567a96 603
590f50d6
RR
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;
30ed6e5c 614
590f50d6
RR
615 count++;
616 child = child->next;
617 }
fd0eed64 618 }
805dd538 619
7cf8cb48 620 return wxNOT_FOUND;
fd0eed64
RR
621}
622
623int wxComboBox::GetSelection() const
40eb3606 624{
590f50d6
RR
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()
40eb3606
VZ
636 : g_SelectionBeforePopup;
637}
638
639int wxComboBox::GetCurrentSelection() const
fd0eed64 640{
223d09f6 641 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 642
590f50d6
RR
643#ifdef __WXGTK24__
644 if (!gtk_check_version(2,4,0))
fd0eed64 645 {
590f50d6
RR
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)
fd0eed64 656 {
590f50d6
RR
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 }
fd0eed64 665 }
6de97a3b 666 }
805dd538 667
fd0eed64 668 return -1;
6de97a3b 669}
53010e52 670
aa61d352 671wxString wxComboBox::GetString(unsigned int n) const
53010e52 672{
0a164d4c 673 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 674
7cf8cb48 675 wxString str;
8228b893 676
590f50d6
RR
677#ifdef __WXGTK24__
678 if (!gtk_check_version(2,4,0))
fd0eed64 679 {
590f50d6
RR
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 }
7cf8cb48
VZ
691 }
692 else
590f50d6 693#endif
8228b893 694 {
590f50d6
RR
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 }
fd0eed64 708 }
805dd538 709
7cf8cb48 710 return str;
6de97a3b 711}
53010e52 712
fd0eed64 713wxString wxComboBox::GetStringSelection() const
53010e52 714{
0a164d4c 715 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 716
590f50d6
RR
717#ifdef __WXGTK24__
718 if (!gtk_check_version(2,4,0))
fd0eed64 719 {
590f50d6
RR
720 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
721 int sel = gtk_combo_box_get_active( combobox );
722 if (sel == -1)
723 return wxEmptyString;
aa61d352 724 return GetString(sel);
fd0eed64 725 }
590f50d6
RR
726 else
727#endif
8228b893 728 {
590f50d6
RR
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 }
805dd538 739
590f50d6
RR
740 wxFAIL_MSG( wxT("wxComboBox: no selection") );
741 }
805dd538 742
0a164d4c 743 return wxEmptyString;
6de97a3b 744}
53010e52 745
aa61d352 746unsigned int wxComboBox::GetCount() const
53010e52 747{
223d09f6 748 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 749
590f50d6
RR
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;
aa61d352 759 unsigned int ret = 1;
590f50d6
RR
760 while (gtk_tree_model_iter_next( model, &iter ))
761 ret++;
762 return ret;
763 }
764 else
765#endif
8228b893 766 {
590f50d6 767 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 768
590f50d6 769 GList *child = GTK_LIST(list)->children;
aa61d352
VZ
770 unsigned int count = 0;
771 while (child)
772 {
773 count++;
774 child = child->next;
775 }
590f50d6
RR
776 return count;
777 }
8228b893 778
590f50d6 779 return 0;
6de97a3b 780}
53010e52 781
debe6624 782void wxComboBox::SetSelection( int n )
53010e52 783{
223d09f6 784 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 785
953704c1
RR
786 DisableEvents();
787
590f50d6
RR
788#ifdef __WXGTK24__
789 if (!gtk_check_version(2,4,0))
790 {
791 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
792 gtk_combo_box_set_active( combobox, n );
793 }
794 else
795#endif
796 {
797 GtkWidget *list = GTK_COMBO(m_widget)->list;
798 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
799 gtk_list_select_item( GTK_LIST(list), n );
800 m_prevSelection = n;
801 }
8228b893 802
953704c1 803 EnableEvents();
6de97a3b 804}
53010e52 805
fd0eed64 806wxString wxComboBox::GetValue() const
53010e52 807{
590f50d6
RR
808 GtkEntry *entry = NULL;
809#ifdef __WXGTK24__
810 if (!gtk_check_version(2,4,0))
811 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
812 else
8228b893 813#endif
590f50d6 814 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 815
2e1d7104
RR
816 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
817
30ed6e5c 818#if 0
2e1d7104
RR
819 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
820 {
821 wxChar c = tmp[i];
822 printf( "%d ", (int) (c) );
823 }
824 printf( "\n" );
825#endif
30ed6e5c 826
fd0eed64 827 return tmp;
6de97a3b 828}
53010e52
RR
829
830void wxComboBox::SetValue( const wxString& value )
831{
223d09f6 832 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 833
590f50d6
RR
834 GtkEntry *entry = NULL;
835#ifdef __WXGTK24__
836 if (!gtk_check_version(2,4,0))
837 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
838 else
8228b893 839#endif
590f50d6 840 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 841
0a164d4c 842 wxString tmp;
fd0eed64 843 if (!value.IsNull()) tmp = value;
a236aa20 844
21dd7c5b 845 DisableEvents();
590f50d6 846 gtk_entry_set_text( entry, wxGTK_CONV( tmp ) );
21dd7c5b 847 EnableEvents();
150e31d2 848
b0021947 849 InvalidateBestSize();
6de97a3b 850}
53010e52 851
fd0eed64 852void wxComboBox::Copy()
53010e52 853{
223d09f6 854 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 855
590f50d6
RR
856 GtkEntry *entry = NULL;
857#ifdef __WXGTK24__
858 if (!gtk_check_version(2,4,0))
859 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
860 else
8228b893 861#endif
590f50d6 862 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 863
afa7bd1e 864 gtk_editable_copy_clipboard(GTK_EDITABLE(entry));
6de97a3b 865}
53010e52 866
fd0eed64 867void wxComboBox::Cut()
53010e52 868{
223d09f6 869 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 870
590f50d6
RR
871 GtkEntry *entry = NULL;
872#ifdef __WXGTK24__
873 if (!gtk_check_version(2,4,0))
874 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
875 else
8228b893 876#endif
590f50d6 877 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 878
afa7bd1e 879 gtk_editable_cut_clipboard(GTK_EDITABLE(entry));
6de97a3b 880}
53010e52 881
fd0eed64 882void wxComboBox::Paste()
53010e52 883{
223d09f6 884 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 885
590f50d6
RR
886 GtkEntry *entry = NULL;
887#ifdef __WXGTK24__
888 if (!gtk_check_version(2,4,0))
889 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
890 else
8228b893 891#endif
590f50d6 892 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 893
afa7bd1e 894 gtk_editable_paste_clipboard(GTK_EDITABLE(entry));
6de97a3b 895}
53010e52 896
150e31d2
JS
897void wxComboBox::Undo()
898{
899 // TODO
900}
901
902void wxComboBox::Redo()
903{
904 // TODO
905}
906
907void wxComboBox::SelectAll()
908{
4e324a3f 909 SetSelection(0, GetLastPosition());
150e31d2
JS
910}
911
912bool wxComboBox::CanUndo() const
913{
914 // TODO
915 return false;
916}
917
918bool wxComboBox::CanRedo() const
919{
920 // TODO
921 return false;
922}
923
924bool wxComboBox::HasSelection() const
925{
926 long from, to;
927 GetSelection(&from, &to);
928 return from != to;
929}
930
931bool wxComboBox::CanCopy() const
932{
933 // Can copy if there's a selection
934 return HasSelection();
935}
936
937bool wxComboBox::CanCut() const
938{
939 return CanCopy() && IsEditable();
940}
941
942bool wxComboBox::CanPaste() const
943{
944 // TODO: check for text on the clipboard
945 return IsEditable() ;
946}
947
948bool wxComboBox::IsEditable() const
949{
950 return !HasFlag(wxCB_READONLY);
951}
952
953
debe6624 954void wxComboBox::SetInsertionPoint( long pos )
53010e52 955{
223d09f6 956 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 957
6f6f938f
VZ
958 if ( pos == GetLastPosition() )
959 pos = -1;
960
590f50d6
RR
961 GtkEntry *entry = NULL;
962#ifdef __WXGTK24__
963 if (!gtk_check_version(2,4,0))
964 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
965 else
8228b893 966#endif
590f50d6 967 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 968
590f50d6 969 gtk_entry_set_position( entry, (int)pos );
6de97a3b 970}
53010e52 971
fd0eed64 972long wxComboBox::GetInsertionPoint() const
53010e52 973{
590f50d6
RR
974 GtkEntry *entry = NULL;
975#ifdef __WXGTK24__
976 if (!gtk_check_version(2,4,0))
977 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
978 else
8228b893 979#endif
590f50d6 980 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 981
590f50d6 982 return (long) gtk_editable_get_position(GTK_EDITABLE(entry));
6de97a3b 983}
53010e52 984
7d8268a1 985wxTextPos wxComboBox::GetLastPosition() const
53010e52 986{
590f50d6
RR
987 GtkEntry *entry = NULL;
988#ifdef __WXGTK24__
989 if (!gtk_check_version(2,4,0))
990 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
991 else
8228b893 992#endif
590f50d6 993 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 994
590f50d6 995 int pos = entry->text_length;
fd0eed64 996 return (long) pos-1;
6de97a3b 997}
53010e52 998
debe6624 999void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 1000{
223d09f6 1001 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 1002
590f50d6
RR
1003 GtkEntry *entry = NULL;
1004#ifdef __WXGTK24__
1005 if (!gtk_check_version(2,4,0))
1006 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1007 else
8228b893 1008#endif
590f50d6 1009 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1010
fd0eed64
RR
1011 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
1012 if (value.IsNull()) return;
1013 gint pos = (gint)to;
30ed6e5c 1014
e0a050e3 1015 // FIXME-UTF8: wouldn't be needed if utf8_str() always returned a buffer
37a4076e
VS
1016#if wxUSE_UNICODE_UTF8
1017 const char *utf8 = value.utf8_str();
2e1d7104 1018#else
37a4076e 1019 wxCharBuffer buffer(value.utf8_str());
056b067f 1020 const char *utf8 = buffer;
2e1d7104 1021#endif
37a4076e 1022 gtk_editable_insert_text(GTK_EDITABLE(entry), utf8, strlen(utf8), &pos);
6de97a3b 1023}
53010e52 1024
20d10ee1 1025void wxComboBox::SetSelection( long from, long to )
53010e52 1026{
590f50d6
RR
1027 GtkEntry *entry = NULL;
1028#ifdef __WXGTK24__
1029 if (!gtk_check_version(2,4,0))
1030 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1031 else
8228b893 1032#endif
590f50d6 1033 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1034
20d10ee1 1035 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 1036}
53010e52 1037
150e31d2
JS
1038void wxComboBox::GetSelection( long* from, long* to ) const
1039{
590f50d6
RR
1040 GtkEntry *entry = NULL;
1041#ifdef __WXGTK24__
1042 if (!gtk_check_version(2,4,0))
1043 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1044 else
8228b893 1045#endif
590f50d6 1046 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1047
150e31d2
JS
1048 if (IsEditable())
1049 {
590f50d6 1050 GtkEditable *editable = GTK_EDITABLE(entry);
4e324a3f
JS
1051 gint start, end;
1052 gtk_editable_get_selection_bounds(editable, & start, & end);
1053 *from = start;
1054 *to = end;
150e31d2
JS
1055 }
1056}
1057
20d10ee1 1058void wxComboBox::SetEditable( bool editable )
53010e52 1059{
590f50d6
RR
1060 GtkEntry *entry = NULL;
1061#ifdef __WXGTK24__
1062 if (!gtk_check_version(2,4,0))
1063 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1064 else
8228b893 1065#endif
590f50d6 1066 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1067
20d10ee1 1068 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
1069}
1070
8a85884a
VZ
1071void wxComboBox::OnChar( wxKeyEvent &event )
1072{
12a3f227 1073 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 1074 {
461573cc 1075 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
1076 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
1077 eventEnter.SetString( GetValue() );
1078 eventEnter.SetInt( GetSelection() );
1079 eventEnter.SetEventObject( this );
3352cfff 1080
17a1ebd1 1081 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
1082 {
1083 // This will invoke the dialog default action, such
1084 // as the clicking the default button.
1085
1086 wxWindow *top_frame = m_parent;
1087 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1088 top_frame = top_frame->GetParent();
1089
1090 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1091 {
1092 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1093
1094 if (window->default_widget)
150e31d2 1095 gtk_widget_activate (window->default_widget);
3352cfff
RR
1096 }
1097 }
30ed6e5c 1098
461573cc
RR
1099 // Catch GTK event so that GTK doesn't open the drop
1100 // down list upon RETURN.
0878fb4c 1101 return;
8a85884a 1102 }
30ed6e5c 1103
7cf8cb48 1104 event.Skip();
8a85884a
VZ
1105}
1106
953704c1
RR
1107void wxComboBox::DisableEvents()
1108{
590f50d6
RR
1109#ifdef __WXGTK24__
1110 if (!gtk_check_version(2,4,0))
1111 {
98264520
PC
1112 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
1113 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 1114
98264520
PC
1115 g_signal_handlers_block_by_func(m_widget,
1116 (gpointer)gtkcombobox_changed_callback, this);
590f50d6
RR
1117 }
1118 else
8228b893 1119#endif
590f50d6 1120 {
98264520
PC
1121 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->list,
1122 (gpointer) gtkcombo_combo_select_child_callback, this);
8228b893 1123
98264520
PC
1124 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->entry,
1125 (gpointer) gtkcombo_text_changed_callback, this);
590f50d6 1126 }
953704c1
RR
1127}
1128
1129void wxComboBox::EnableEvents()
1130{
590f50d6
RR
1131#ifdef __WXGTK24__
1132 if (!gtk_check_version(2,4,0))
1133 {
98264520
PC
1134 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
1135 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 1136
98264520
PC
1137 g_signal_handlers_unblock_by_func(m_widget,
1138 (gpointer)gtkcombobox_changed_callback, this);
590f50d6
RR
1139 }
1140 else
8228b893 1141#endif
590f50d6 1142 {
98264520
PC
1143 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->list,
1144 (gpointer) gtkcombo_combo_select_child_callback, this);
1145
1146 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->entry,
1147 (gpointer) gtkcombo_text_changed_callback, this);
590f50d6 1148 }
953704c1
RR
1149}
1150
b4071e91
RR
1151void wxComboBox::OnSize( wxSizeEvent &event )
1152{
590f50d6
RR
1153#ifdef __WXGTK24__
1154 if (!gtk_check_version(2,4,0))
1155 {
1156 // Do nothing
1157 }
1158 else
1159#endif
1160 {
1161 // NB: In some situations (e.g. on non-first page of a wizard, if the
1162 // size used is default size), GtkCombo widget is resized correctly,
1163 // but it's look is not updated, it's rendered as if it was much wider.
1164 // No other widgets are affected, so it looks like a bug in GTK+.
1165 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1166 // fixes it.
1167 if (GTK_WIDGET_VISIBLE(m_widget))
1168 gtk_widget_queue_resize(m_widget);
1169 }
260a67b7 1170
f03fc89f 1171 event.Skip();
6de97a3b 1172}
53010e52 1173
f40fdaa3 1174void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 1175{
590f50d6
RR
1176#ifdef __WXGTK24__
1177 if (!gtk_check_version(2,4,0))
1178 {
1179 // Do nothing
1180 }
1181 else
1182#endif
1183 {
f40fdaa3 1184// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 1185
590f50d6
RR
1186 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
1187 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 1188
590f50d6
RR
1189 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
1190 GList *child = list->children;
1191 while (child)
1192 {
1193 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 1194
590f50d6
RR
1195 GtkBin *bin = GTK_BIN(child->data);
1196 gtk_widget_modify_style( bin->child, style );
805dd538 1197
590f50d6
RR
1198 child = child->next;
1199 }
fd0eed64 1200 }
868a2826 1201}
b4071e91 1202
fd0eed64 1203GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 1204{
590f50d6
RR
1205 GtkEntry *entry = NULL;
1206#ifdef __WXGTK24__
1207 if (!gtk_check_version(2,4,0))
1208 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1209 else
8228b893 1210#endif
590f50d6 1211 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1212
590f50d6 1213 return GTK_WIDGET( entry );
97b3455a
RR
1214}
1215
ef5c70f9 1216GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
b4071e91 1217{
590f50d6
RR
1218#ifdef __WXGTK24__
1219 if (!gtk_check_version(2,4,0))
1220 {
ef5c70f9
VZ
1221 wxUnusedVar(windows);
1222
1223 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
590f50d6
RR
1224 }
1225 else
ef5c70f9 1226#endif // GTK+ 2.4
590f50d6 1227 {
ef5c70f9
VZ
1228 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
1229 windows.push_back(GTK_COMBO(m_widget)->button->window);
1230
1231 // indicate that we return multiple windows in the windows array
1232 return NULL;
590f50d6 1233 }
b4071e91 1234}
ac57418f 1235
f68586e5
VZ
1236wxSize wxComboBox::DoGetBestSize() const
1237{
db434467 1238 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
1239
1240 // we know better our horizontal extent: it depends on the longest string
1241 // in the combobox
a6fc8ae3
VZ
1242 if ( m_widget )
1243 {
60d85ccb 1244 int width;
aa61d352
VZ
1245 unsigned int count = GetCount();
1246 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 1247 {
aa61d352 1248 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
1249 if ( width > ret.x )
1250 ret.x = width;
1251 }
1252 }
1253
1254 // empty combobox should have some reasonable default size too
1255 if ( ret.x < 100 )
1256 ret.x = 100;
9f884528
RD
1257
1258 CacheBestSize(ret);
db434467 1259 return ret;
f68586e5
VZ
1260}
1261
9d522606
RD
1262// static
1263wxVisualAttributes
1264wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1265{
590f50d6
RR
1266#ifdef __WXGTK24__
1267 if (!gtk_check_version(2,4,0))
1268 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
1269 else
1270#endif
1271 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
9d522606
RD
1272}
1273
150e31d2
JS
1274// ----------------------------------------------------------------------------
1275// standard event handling
1276// ----------------------------------------------------------------------------
1277
1278void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
1279{
1280 Cut();
1281}
1282
1283void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
1284{
1285 Copy();
1286}
1287
1288void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
1289{
1290 Paste();
1291}
1292
1293void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1294{
1295 Undo();
1296}
1297
1298void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1299{
1300 Redo();
1301}
1302
1303void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1304{
1305 long from, to;
1306 GetSelection(& from, & to);
1307 if (from != -1 && to != -1)
1308 Remove(from, to);
1309}
1310
1311void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1312{
1313 SetSelection(-1, -1);
1314}
1315
1316void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1317{
1318 event.Enable( CanCut() );
1319}
1320
1321void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1322{
1323 event.Enable( CanCopy() );
1324}
1325
1326void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1327{
1328 event.Enable( CanPaste() );
1329}
1330
1331void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1332{
1333 event.Enable( CanUndo() );
1334}
1335
1336void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1337{
1338 event.Enable( CanRedo() );
1339}
1340
1341void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1342{
1343 event.Enable(HasSelection() && IsEditable()) ;
1344}
1345
1346void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1347{
1348 event.Enable(GetLastPosition() > 0);
1349}
1350
dcf924a3 1351#endif