]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
gtk version check should be runtime, not compile time
[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.
9fa72bd2 135 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo->GetHandle())->entry,
1fb33bdb 136 (gpointer) gtkcombo_text_changed_callback,
9fa72bd2 137 combo);
78b3b018 138 combo->SetValue( combo->GetStringSelection() );
9fa72bd2 139 g_signal_connect_after (GTK_COMBO (combo->GetHandle())->entry, "changed",
1fb33bdb 140 G_CALLBACK (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{
7d8268a1 244 m_ignoreNextUpdate = false;
159b66c0 245 m_prevSelection = 0;
805dd538 246
db434467 247 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
248 !CreateBase( parent, id, pos, size, style, validator, name ))
249 {
223d09f6 250 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 251 return false;
4dcaf11a 252 }
6de97a3b 253
590f50d6
RR
254#ifdef __WXGTK24__
255 if (!gtk_check_version(2,4,0))
256 {
257 m_widget = gtk_combo_box_entry_new_text();
258 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
8228b893 259
590f50d6 260 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE );
8228b893 261
590f50d6
RR
262 for (int i = 0; i < n; i++)
263 {
264 gtk_combo_box_append_text( combobox, wxGTK_CONV( choices[i] ) );
30ed6e5c 265
590f50d6
RR
266 m_clientDataList.Append( (wxObject*)NULL );
267 m_clientObjectList.Append( (wxObject*)NULL );
268 }
269 }
270 else
271#endif
272 {
273 m_widget = gtk_combo_new();
274 GtkCombo* combo = GTK_COMBO(m_widget);
8228b893 275
590f50d6
RR
276 // Disable GTK's broken events ...
277 g_signal_handler_disconnect (combo->entry, combo->entry_change_id);
278 // ... and add surrogate handler.
279 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
1fb33bdb 280 G_CALLBACK (gtkcombo_dummy_callback),
9fa72bd2 281 combo);
805dd538 282
590f50d6
RR
283 // make it more useable
284 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
30ed6e5c 285
590f50d6
RR
286 // and case-sensitive
287 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
3ca6a5f0 288
590f50d6 289 if (style & wxNO_BORDER)
7d1fea10 290 g_object_set (combo->entry, "has-frame", FALSE, NULL );
8228b893 291
590f50d6 292 GtkWidget *list = combo->list;
7d8268a1 293
590f50d6
RR
294 for (int i = 0; i < n; i++)
295 {
296 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
805dd538 297
590f50d6
RR
298 m_clientDataList.Append( (wxObject*)NULL );
299 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 300
590f50d6 301 gtk_container_add( GTK_CONTAINER(list), list_item );
805dd538 302
590f50d6
RR
303 gtk_widget_show( list_item );
304 }
fd0eed64 305 }
805dd538 306
590f50d6 307
f03fc89f 308 m_parent->DoAddChild( this );
30ed6e5c 309
590f50d6
RR
310 GtkEntry *entry = NULL;
311#ifdef __WXGTK24__
312 if (!gtk_check_version(2,4,0))
313 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
314 else
8228b893 315#endif
590f50d6 316 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 317
590f50d6 318 m_focusWidget = GTK_WIDGET( entry );
805dd538 319
abdeb9e7 320 PostCreation(size);
53010e52 321
590f50d6
RR
322#ifdef __WXGTK24__
323 if (!gtk_check_version(2,4,0))
324 ConnectWidget( m_widget );
325 else
326#endif
327 ConnectWidget( GTK_COMBO(m_widget)->button );
805dd538 328
590f50d6
RR
329#ifdef __WXGTK24__
330 if (!gtk_check_version(2,4,0))
331 {
332 gtk_entry_set_text( entry, wxGTK_CONV(value) );
8228b893 333
590f50d6
RR
334 if (style & wxCB_READONLY)
335 gtk_entry_set_editable( entry, FALSE );
8228b893 336
590f50d6
RR
337 g_signal_connect_after (entry, "changed",
338 G_CALLBACK (gtkcombobox_text_changed_callback), this);
8228b893 339
590f50d6
RR
340 g_signal_connect_after (m_widget, "changed",
341 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 342
590f50d6
RR
343 }
344 else
345#endif
346 {
347 GtkCombo *combo = GTK_COMBO(m_widget);
348 // MSW's combo box shows the value and the selection is -1
349 gtk_entry_set_text( entry, wxGTK_CONV(value) );
350 gtk_list_unselect_all( GTK_LIST(combo->list) );
805dd538 351
590f50d6
RR
352 if (style & wxCB_READONLY)
353 gtk_entry_set_editable( entry, FALSE );
a260fe6a 354
590f50d6
RR
355 // "show" and "hide" events are generated when user click on the combobox button which popups a list
356 // this list is the "popwin" gtk widget
357 g_signal_connect (GTK_COMBO(combo)->popwin, "hide",
1fb33bdb 358 G_CALLBACK (gtkcombo_popup_hide_callback), this);
590f50d6 359 g_signal_connect (GTK_COMBO(combo)->popwin, "show",
1fb33bdb 360 G_CALLBACK (gtkcombo_popup_show_callback), this);
590f50d6 361 g_signal_connect_after (combo->list, "select-child",
1fb33bdb 362 G_CALLBACK (gtkcombo_combo_select_child_callback),
9fa72bd2 363 this);
590f50d6
RR
364 g_signal_connect_after (entry, "changed",
365 G_CALLBACK (gtkcombo_text_changed_callback), this);
590f50d6 366 }
805dd538 367
170acdc9 368 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 369
7d8268a1 370 return true;
fd0eed64
RR
371}
372
373wxComboBox::~wxComboBox()
374{
222ed1d6 375 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
376 while (node)
377 {
b1d4dd7a 378 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 379 if (cd) delete cd;
b1d4dd7a 380 node = node->GetNext();
fd0eed64 381 }
7d6d2cd4
RR
382 m_clientObjectList.Clear();
383
fd0eed64 384 m_clientDataList.Clear();
6de97a3b 385}
53010e52 386
2b5f62a0
VZ
387void wxComboBox::SetFocus()
388{
389 if ( m_hasFocus )
390 {
391 // don't do anything if we already have focus
392 return;
393 }
394
395 gtk_widget_grab_focus( m_focusWidget );
396}
397
6f6f938f 398int wxComboBox::DoAppend( const wxString &item )
53010e52 399{
2a68b7a0 400 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 401
590f50d6
RR
402#ifdef __WXGTK24__
403 if (!gtk_check_version(2,4,0))
404 {
405 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
406 gtk_combo_box_append_text( combobox, wxGTK_CONV( item ) );
407 }
408 else
409#endif
410 {
411 DisableEvents();
805dd538 412
590f50d6
RR
413 GtkWidget *list = GTK_COMBO(m_widget)->list;
414 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
805dd538 415
590f50d6 416 gtk_container_add( GTK_CONTAINER(list), list_item );
ec5d85fb 417
590f50d6
RR
418 if (GTK_WIDGET_REALIZED(m_widget))
419 {
420 gtk_widget_realize( list_item );
421 gtk_widget_realize( GTK_BIN(list_item)->child );
422 }
2b07d713 423
590f50d6
RR
424 // Apply current widget style to the new list_item
425 GtkRcStyle *style = CreateWidgetStyle();
426 if (style)
427 {
10bd1f7d 428 gtk_widget_modify_style(list_item, style);
590f50d6 429 GtkBin *bin = GTK_BIN( list_item );
10bd1f7d 430 GtkWidget *label = bin->child;
590f50d6
RR
431 gtk_widget_modify_style( label, style );
432 gtk_rc_style_unref( style );
433 }
805dd538 434
590f50d6 435 gtk_widget_show( list_item );
30ed6e5c 436
590f50d6
RR
437 EnableEvents();
438 }
53010e52 439
aa61d352 440 const unsigned int count = GetCount();
8228b893
WS
441
442 if ( m_clientDataList.GetCount() < count )
0a164d4c 443 m_clientDataList.Append( (wxObject*) NULL );
8228b893 444 if ( m_clientObjectList.GetCount() < count )
0a164d4c 445 m_clientObjectList.Append( (wxObject*) NULL );
805dd538 446
b0021947
VS
447 InvalidateBestSize();
448
6f6f938f 449 return count - 1;
fd0eed64
RR
450}
451
aa61d352 452int wxComboBox::DoInsert(const wxString &item, unsigned int pos)
243dbf1a 453{
708c45a6
VZ
454 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
455 wxT("can't insert into sorted list"));
456
457 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
8228b893 458 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") );
243dbf1a 459
aa61d352 460 unsigned int count = GetCount();
6f6f938f 461
aa61d352 462 if (pos == count)
6f6f938f 463 return Append(item);
243dbf1a 464
590f50d6
RR
465#ifdef __WXGTK24__
466 if (!gtk_check_version(2,4,0))
467 {
468 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
469 gtk_combo_box_insert_text( combobox, pos, wxGTK_CONV( item ) );
470 }
471 else
472#endif
473 {
474 DisableEvents();
243dbf1a 475
590f50d6
RR
476 GtkWidget *list = GTK_COMBO(m_widget)->list;
477 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
243dbf1a 478
590f50d6
RR
479 GList *gitem_list = g_list_alloc ();
480 gitem_list->data = list_item;
481 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
243dbf1a 482
590f50d6
RR
483 if (GTK_WIDGET_REALIZED(m_widget))
484 {
485 gtk_widget_realize( list_item );
486 gtk_widget_realize( GTK_BIN(list_item)->child );
243dbf1a 487
590f50d6
RR
488 ApplyWidgetStyle();
489 }
243dbf1a 490
590f50d6 491 gtk_widget_show( list_item );
243dbf1a 492
590f50d6
RR
493 EnableEvents();
494 }
8228b893 495
6f6f938f 496 count = GetCount();
243dbf1a 497
8228b893 498 if ( m_clientDataList.GetCount() < count )
0a164d4c 499 m_clientDataList.Insert( pos, (wxObject*) NULL );
8228b893 500 if ( m_clientObjectList.GetCount() < count )
0a164d4c 501 m_clientObjectList.Insert( pos, (wxObject*) NULL );
243dbf1a 502
b0021947 503 InvalidateBestSize();
243dbf1a 504
6f6f938f 505 return pos;
243dbf1a
VZ
506}
507
aa61d352 508void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 509{
223d09f6 510 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 511
222ed1d6 512 wxList::compatibility_iterator node = m_clientDataList.Item( n );
fd0eed64 513 if (!node) return;
805dd538 514
f5e27805 515 node->SetData( (wxObject*) clientData );
6de97a3b 516}
53010e52 517
aa61d352 518void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 519{
223d09f6 520 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 521
222ed1d6 522 wxList::compatibility_iterator node = m_clientDataList.Item( n );
805dd538 523
30ed6e5c 524 return node ? node->GetData() : NULL;
fd0eed64
RR
525}
526
aa61d352 527void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
fd0eed64 528{
223d09f6 529 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 530
222ed1d6 531 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
fd0eed64 532 if (!node) return;
805dd538 533
e94e2e95 534 // wxItemContainer already deletes data for us
805dd538 535
fd0eed64 536 node->SetData( (wxObject*) clientData );
6de97a3b 537}
53010e52 538
aa61d352 539wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
53010e52 540{
223d09f6 541 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 542
222ed1d6 543 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
805dd538 544
30ed6e5c 545 return node ? (wxClientData*) node->GetData() : NULL;
fd0eed64
RR
546}
547
548void wxComboBox::Clear()
549{
223d09f6 550 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 551
461573cc 552 DisableEvents();
30ed6e5c 553
590f50d6
RR
554#ifdef __WXGTK24__
555 if (!gtk_check_version(2,4,0))
556 {
557 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
aa61d352
VZ
558 const unsigned int count = GetCount();
559 for (unsigned int i = 0; i < count; i++)
590f50d6
RR
560 gtk_combo_box_remove_text( combobox, 0 );
561 }
c1a3ff25
VZ
562 else // GTK+ < 2.4.0
563#endif // __WXGTK24__
590f50d6
RR
564 {
565 GtkWidget *list = GTK_COMBO(m_widget)->list;
566 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
567 }
8228b893 568
222ed1d6 569 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
570 while (node)
571 {
b1d4dd7a 572 wxClientData *cd = (wxClientData*)node->GetData();
c1a3ff25 573 delete cd;
b1d4dd7a 574 node = node->GetNext();
fd0eed64 575 }
f5e27805 576 m_clientObjectList.Clear();
805dd538 577
fd0eed64 578 m_clientDataList.Clear();
30ed6e5c 579
461573cc 580 EnableEvents();
b0021947
VS
581
582 InvalidateBestSize();
6de97a3b 583}
53010e52 584
aa61d352 585void wxComboBox::Delete(unsigned int n)
53010e52 586{
223d09f6 587 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 588
590f50d6
RR
589#ifdef __WXGTK24__
590 if (!gtk_check_version(2,4,0))
fd0eed64 591 {
8228b893
WS
592 wxCHECK_RET( IsValid(n), wxT("invalid index") );
593
590f50d6
RR
594 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
595 gtk_combo_box_remove_text( combobox, n );
fd0eed64 596 }
590f50d6
RR
597 else
598#endif
599 {
600 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 601
590f50d6
RR
602 GList *child = g_list_nth( listbox->children, n );
603
604 if (!child)
605 {
606 wxFAIL_MSG(wxT("wrong index"));
607 return;
608 }
30ed6e5c 609
590f50d6 610 DisableEvents();
805dd538 611
590f50d6
RR
612 GList *list = g_list_append( (GList*) NULL, child->data );
613 gtk_list_remove_items( listbox, list );
614 g_list_free( list );
615
616 EnableEvents();
617 }
8228b893 618
222ed1d6 619 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
f5e27805 620 if (node)
fd0eed64 621 {
b1d4dd7a 622 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 623 if (cd) delete cd;
222ed1d6 624 m_clientObjectList.Erase( node );
f5e27805 625 }
805dd538 626
b1d4dd7a 627 node = m_clientDataList.Item( n );
f5e27805 628 if (node)
222ed1d6 629 m_clientDataList.Erase( node );
150e31d2 630
b0021947 631 InvalidateBestSize();
461573cc
RR
632}
633
aa61d352 634void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
635{
636 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
637
590f50d6
RR
638#ifdef __WXGTK24__
639 if (!gtk_check_version(2,4,0))
461573cc 640 {
590f50d6 641 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
8228b893
WS
642 wxCHECK_RET( IsValid(n), wxT("invalid index") );
643
590f50d6
RR
644 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
645 GtkTreeIter iter;
646 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
647 {
648 GValue value = { 0, };
649 g_value_init( &value, G_TYPE_STRING );
650 g_value_set_string( &value, wxGTK_CONV( text ) );
651 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
652 g_value_unset( &value );
653 }
461573cc
RR
654 }
655 else
590f50d6 656#endif
461573cc 657 {
590f50d6 658 GtkWidget *list = GTK_COMBO(m_widget)->list;
150e31d2 659
590f50d6
RR
660 GList *child = g_list_nth( GTK_LIST(list)->children, n );
661 if (child)
662 {
663 GtkBin *bin = GTK_BIN( child->data );
664 GtkLabel *label = GTK_LABEL( bin->child );
665 gtk_label_set_text(label, wxGTK_CONV(text));
666 }
667 else
668 {
669 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
670 }
671 }
8228b893 672
b0021947 673 InvalidateBestSize();
6de97a3b 674}
53010e52 675
11e62fe6 676int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 677{
0a164d4c 678 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 679
590f50d6
RR
680#ifdef __WXGTK24__
681 if (!gtk_check_version(2,4,0))
53010e52 682 {
590f50d6
RR
683 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
684 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
685 GtkTreeIter iter;
686 gtk_tree_model_get_iter_first( model, &iter );
687 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
688 return -1;
689 int count = 0;
8228b893 690 do
590f50d6
RR
691 {
692 GValue value = { 0, };
693 gtk_tree_model_get_value( model, &iter, 0, &value );
694 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
695 g_value_unset( &value );
8228b893 696
590f50d6
RR
697 if (item.IsSameAs( str, bCase ) )
698 return count;
8228b893 699
590f50d6 700 count++;
8228b893 701
590f50d6
RR
702 } while (gtk_tree_model_iter_next( model, &iter ));
703 }
704 else
705#endif
706 {
707 GtkWidget *list = GTK_COMBO(m_widget)->list;
68567a96 708
590f50d6
RR
709 GList *child = GTK_LIST(list)->children;
710 int count = 0;
711 while (child)
712 {
713 GtkBin *bin = GTK_BIN( child->data );
714 GtkLabel *label = GTK_LABEL( bin->child );
715 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
716
717 if (item.IsSameAs( str , bCase ) )
718 return count;
30ed6e5c 719
590f50d6
RR
720 count++;
721 child = child->next;
722 }
fd0eed64 723 }
805dd538 724
7cf8cb48 725 return wxNOT_FOUND;
fd0eed64
RR
726}
727
728int wxComboBox::GetSelection() const
40eb3606 729{
590f50d6
RR
730#ifdef __WXGTK24__
731 if (!gtk_check_version(2,4,0))
732 {
733 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
734 return gtk_combo_box_get_active( combobox );
735 }
736 else
737#endif
738 // if the popup is currently opened, use the selection as it had been
739 // before it dropped down
740 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
40eb3606
VZ
741 : g_SelectionBeforePopup;
742}
743
744int wxComboBox::GetCurrentSelection() const
fd0eed64 745{
223d09f6 746 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 747
590f50d6
RR
748#ifdef __WXGTK24__
749 if (!gtk_check_version(2,4,0))
fd0eed64 750 {
590f50d6
RR
751 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
752 return gtk_combo_box_get_active( combobox );
753 }
754 else
755#endif
756 {
757 GtkWidget *list = GTK_COMBO(m_widget)->list;
758
759 GList *selection = GTK_LIST(list)->selection;
760 if (selection)
fd0eed64 761 {
590f50d6
RR
762 GList *child = GTK_LIST(list)->children;
763 int count = 0;
764 while (child)
765 {
766 if (child->data == selection->data) return count;
767 count++;
768 child = child->next;
769 }
fd0eed64 770 }
6de97a3b 771 }
805dd538 772
fd0eed64 773 return -1;
6de97a3b 774}
53010e52 775
aa61d352 776wxString wxComboBox::GetString(unsigned int n) const
53010e52 777{
0a164d4c 778 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 779
7cf8cb48 780 wxString str;
8228b893 781
590f50d6
RR
782#ifdef __WXGTK24__
783 if (!gtk_check_version(2,4,0))
fd0eed64 784 {
590f50d6
RR
785 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
786 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
787 GtkTreeIter iter;
788 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
789 {
790 GValue value = { 0, };
791 gtk_tree_model_get_value( model, &iter, 0, &value );
792 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
793 g_value_unset( &value );
794 return tmp;
795 }
7cf8cb48
VZ
796 }
797 else
590f50d6 798#endif
8228b893 799 {
590f50d6
RR
800 GtkWidget *list = GTK_COMBO(m_widget)->list;
801
802 GList *child = g_list_nth( GTK_LIST(list)->children, n );
803 if (child)
804 {
805 GtkBin *bin = GTK_BIN( child->data );
806 GtkLabel *label = GTK_LABEL( bin->child );
807 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
808 }
809 else
810 {
811 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
812 }
fd0eed64 813 }
805dd538 814
7cf8cb48 815 return str;
6de97a3b 816}
53010e52 817
fd0eed64 818wxString wxComboBox::GetStringSelection() const
53010e52 819{
0a164d4c 820 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 821
590f50d6
RR
822#ifdef __WXGTK24__
823 if (!gtk_check_version(2,4,0))
fd0eed64 824 {
590f50d6
RR
825 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
826 int sel = gtk_combo_box_get_active( combobox );
827 if (sel == -1)
828 return wxEmptyString;
aa61d352 829 return GetString(sel);
fd0eed64 830 }
590f50d6
RR
831 else
832#endif
8228b893 833 {
590f50d6
RR
834 GtkWidget *list = GTK_COMBO(m_widget)->list;
835
836 GList *selection = GTK_LIST(list)->selection;
837 if (selection)
838 {
839 GtkBin *bin = GTK_BIN( selection->data );
840 GtkLabel *label = GTK_LABEL( bin->child );
841 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
842 return tmp;
843 }
805dd538 844
590f50d6
RR
845 wxFAIL_MSG( wxT("wxComboBox: no selection") );
846 }
805dd538 847
0a164d4c 848 return wxEmptyString;
6de97a3b 849}
53010e52 850
aa61d352 851unsigned int wxComboBox::GetCount() const
53010e52 852{
223d09f6 853 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 854
590f50d6
RR
855#ifdef __WXGTK24__
856 if (!gtk_check_version(2,4,0))
857 {
858 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
859 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
860 GtkTreeIter iter;
861 gtk_tree_model_get_iter_first( model, &iter );
862 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
863 return 0;
aa61d352 864 unsigned int ret = 1;
590f50d6
RR
865 while (gtk_tree_model_iter_next( model, &iter ))
866 ret++;
867 return ret;
868 }
869 else
870#endif
8228b893 871 {
590f50d6 872 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 873
590f50d6 874 GList *child = GTK_LIST(list)->children;
aa61d352
VZ
875 unsigned int count = 0;
876 while (child)
877 {
878 count++;
879 child = child->next;
880 }
590f50d6
RR
881 return count;
882 }
8228b893 883
590f50d6 884 return 0;
6de97a3b 885}
53010e52 886
debe6624 887void wxComboBox::SetSelection( int n )
53010e52 888{
223d09f6 889 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 890
953704c1
RR
891 DisableEvents();
892
590f50d6
RR
893#ifdef __WXGTK24__
894 if (!gtk_check_version(2,4,0))
895 {
896 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
897 gtk_combo_box_set_active( combobox, n );
898 }
899 else
900#endif
901 {
902 GtkWidget *list = GTK_COMBO(m_widget)->list;
903 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
904 gtk_list_select_item( GTK_LIST(list), n );
905 m_prevSelection = n;
906 }
8228b893 907
953704c1 908 EnableEvents();
6de97a3b 909}
53010e52 910
fd0eed64 911wxString wxComboBox::GetValue() const
53010e52 912{
590f50d6
RR
913 GtkEntry *entry = NULL;
914#ifdef __WXGTK24__
915 if (!gtk_check_version(2,4,0))
916 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
917 else
8228b893 918#endif
590f50d6 919 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 920
2e1d7104
RR
921 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
922
30ed6e5c 923#if 0
2e1d7104
RR
924 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
925 {
926 wxChar c = tmp[i];
927 printf( "%d ", (int) (c) );
928 }
929 printf( "\n" );
930#endif
30ed6e5c 931
fd0eed64 932 return tmp;
6de97a3b 933}
53010e52
RR
934
935void wxComboBox::SetValue( const wxString& value )
936{
223d09f6 937 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 938
590f50d6
RR
939 GtkEntry *entry = NULL;
940#ifdef __WXGTK24__
941 if (!gtk_check_version(2,4,0))
942 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
943 else
8228b893 944#endif
590f50d6 945 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 946
0a164d4c 947 wxString tmp;
fd0eed64 948 if (!value.IsNull()) tmp = value;
21dd7c5b
RR
949
950 DisableEvents();
590f50d6 951 gtk_entry_set_text( entry, wxGTK_CONV( tmp ) );
21dd7c5b 952 EnableEvents();
150e31d2 953
b0021947 954 InvalidateBestSize();
6de97a3b 955}
53010e52 956
fd0eed64 957void wxComboBox::Copy()
53010e52 958{
223d09f6 959 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 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
afa7bd1e 969 gtk_editable_copy_clipboard(GTK_EDITABLE(entry));
6de97a3b 970}
53010e52 971
fd0eed64 972void wxComboBox::Cut()
53010e52 973{
223d09f6 974 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 975
590f50d6
RR
976 GtkEntry *entry = NULL;
977#ifdef __WXGTK24__
978 if (!gtk_check_version(2,4,0))
979 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
980 else
8228b893 981#endif
590f50d6 982 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 983
afa7bd1e 984 gtk_editable_cut_clipboard(GTK_EDITABLE(entry));
6de97a3b 985}
53010e52 986
fd0eed64 987void wxComboBox::Paste()
53010e52 988{
223d09f6 989 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 990
590f50d6
RR
991 GtkEntry *entry = NULL;
992#ifdef __WXGTK24__
993 if (!gtk_check_version(2,4,0))
994 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
995 else
8228b893 996#endif
590f50d6 997 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 998
afa7bd1e 999 gtk_editable_paste_clipboard(GTK_EDITABLE(entry));
6de97a3b 1000}
53010e52 1001
150e31d2
JS
1002void wxComboBox::Undo()
1003{
1004 // TODO
1005}
1006
1007void wxComboBox::Redo()
1008{
1009 // TODO
1010}
1011
1012void wxComboBox::SelectAll()
1013{
4e324a3f 1014 SetSelection(0, GetLastPosition());
150e31d2
JS
1015}
1016
1017bool wxComboBox::CanUndo() const
1018{
1019 // TODO
1020 return false;
1021}
1022
1023bool wxComboBox::CanRedo() const
1024{
1025 // TODO
1026 return false;
1027}
1028
1029bool wxComboBox::HasSelection() const
1030{
1031 long from, to;
1032 GetSelection(&from, &to);
1033 return from != to;
1034}
1035
1036bool wxComboBox::CanCopy() const
1037{
1038 // Can copy if there's a selection
1039 return HasSelection();
1040}
1041
1042bool wxComboBox::CanCut() const
1043{
1044 return CanCopy() && IsEditable();
1045}
1046
1047bool wxComboBox::CanPaste() const
1048{
1049 // TODO: check for text on the clipboard
1050 return IsEditable() ;
1051}
1052
1053bool wxComboBox::IsEditable() const
1054{
1055 return !HasFlag(wxCB_READONLY);
1056}
1057
1058
debe6624 1059void wxComboBox::SetInsertionPoint( long pos )
53010e52 1060{
223d09f6 1061 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 1062
6f6f938f
VZ
1063 if ( pos == GetLastPosition() )
1064 pos = -1;
1065
590f50d6
RR
1066 GtkEntry *entry = NULL;
1067#ifdef __WXGTK24__
1068 if (!gtk_check_version(2,4,0))
1069 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1070 else
8228b893 1071#endif
590f50d6 1072 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1073
590f50d6 1074 gtk_entry_set_position( entry, (int)pos );
6de97a3b 1075}
53010e52 1076
fd0eed64 1077long wxComboBox::GetInsertionPoint() const
53010e52 1078{
590f50d6
RR
1079 GtkEntry *entry = NULL;
1080#ifdef __WXGTK24__
1081 if (!gtk_check_version(2,4,0))
1082 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1083 else
8228b893 1084#endif
590f50d6 1085 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1086
590f50d6 1087 return (long) gtk_editable_get_position(GTK_EDITABLE(entry));
6de97a3b 1088}
53010e52 1089
7d8268a1 1090wxTextPos wxComboBox::GetLastPosition() const
53010e52 1091{
590f50d6
RR
1092 GtkEntry *entry = NULL;
1093#ifdef __WXGTK24__
1094 if (!gtk_check_version(2,4,0))
1095 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1096 else
8228b893 1097#endif
590f50d6 1098 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1099
590f50d6 1100 int pos = entry->text_length;
fd0eed64 1101 return (long) pos-1;
6de97a3b 1102}
53010e52 1103
debe6624 1104void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 1105{
223d09f6 1106 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 1107
590f50d6
RR
1108 GtkEntry *entry = NULL;
1109#ifdef __WXGTK24__
1110 if (!gtk_check_version(2,4,0))
1111 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1112 else
8228b893 1113#endif
590f50d6 1114 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1115
fd0eed64
RR
1116 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
1117 if (value.IsNull()) return;
1118 gint pos = (gint)to;
30ed6e5c 1119
2e1d7104
RR
1120#if wxUSE_UNICODE
1121 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
1122 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
1123#else
8228b893 1124 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos );
2e1d7104 1125#endif
6de97a3b 1126}
53010e52 1127
20d10ee1 1128void wxComboBox::SetSelection( long from, long to )
53010e52 1129{
590f50d6
RR
1130 GtkEntry *entry = NULL;
1131#ifdef __WXGTK24__
1132 if (!gtk_check_version(2,4,0))
1133 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1134 else
8228b893 1135#endif
590f50d6 1136 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1137
20d10ee1 1138 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 1139}
53010e52 1140
150e31d2
JS
1141void wxComboBox::GetSelection( long* from, long* to ) const
1142{
590f50d6
RR
1143 GtkEntry *entry = NULL;
1144#ifdef __WXGTK24__
1145 if (!gtk_check_version(2,4,0))
1146 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1147 else
8228b893 1148#endif
590f50d6 1149 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1150
150e31d2
JS
1151 if (IsEditable())
1152 {
590f50d6 1153 GtkEditable *editable = GTK_EDITABLE(entry);
4e324a3f
JS
1154 gint start, end;
1155 gtk_editable_get_selection_bounds(editable, & start, & end);
1156 *from = start;
1157 *to = end;
150e31d2
JS
1158 }
1159}
1160
20d10ee1 1161void wxComboBox::SetEditable( bool editable )
53010e52 1162{
590f50d6
RR
1163 GtkEntry *entry = NULL;
1164#ifdef __WXGTK24__
1165 if (!gtk_check_version(2,4,0))
1166 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1167 else
8228b893 1168#endif
590f50d6 1169 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1170
20d10ee1 1171 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
1172}
1173
8a85884a
VZ
1174void wxComboBox::OnChar( wxKeyEvent &event )
1175{
12a3f227 1176 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 1177 {
461573cc 1178 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
1179 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
1180 eventEnter.SetString( GetValue() );
1181 eventEnter.SetInt( GetSelection() );
1182 eventEnter.SetEventObject( this );
3352cfff 1183
17a1ebd1 1184 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
1185 {
1186 // This will invoke the dialog default action, such
1187 // as the clicking the default button.
1188
1189 wxWindow *top_frame = m_parent;
1190 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1191 top_frame = top_frame->GetParent();
1192
1193 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1194 {
1195 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1196
1197 if (window->default_widget)
150e31d2 1198 gtk_widget_activate (window->default_widget);
3352cfff
RR
1199 }
1200 }
30ed6e5c 1201
461573cc
RR
1202 // Catch GTK event so that GTK doesn't open the drop
1203 // down list upon RETURN.
0878fb4c 1204 return;
8a85884a 1205 }
30ed6e5c 1206
7cf8cb48 1207 event.Skip();
8a85884a
VZ
1208}
1209
953704c1
RR
1210void wxComboBox::DisableEvents()
1211{
590f50d6
RR
1212#ifdef __WXGTK24__
1213 if (!gtk_check_version(2,4,0))
1214 {
8228b893 1215 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget)->child,
590f50d6 1216 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 1217
590f50d6
RR
1218 g_signal_handlers_disconnect_by_func (m_widget,
1219 (gpointer)gtkcombobox_changed_callback, this);
1220 }
1221 else
8228b893 1222#endif
590f50d6
RR
1223 {
1224 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->list,
1225 (gpointer) gtkcombo_combo_select_child_callback, this);
8228b893 1226
590f50d6
RR
1227 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->entry,
1228 (gpointer) gtkcombo_text_changed_callback, this);
1229 }
953704c1
RR
1230}
1231
1232void wxComboBox::EnableEvents()
1233{
590f50d6
RR
1234#ifdef __WXGTK24__
1235 if (!gtk_check_version(2,4,0))
1236 {
1237 g_signal_connect_after (GTK_BIN(m_widget)->child, "changed",
1238 G_CALLBACK (gtkcombobox_text_changed_callback), this);
8228b893 1239
590f50d6
RR
1240 g_signal_connect_after (m_widget, "changed",
1241 G_CALLBACK (gtkcombobox_changed_callback), this);
1242 }
1243 else
8228b893 1244#endif
590f50d6
RR
1245 {
1246 g_signal_connect_after (GTK_COMBO(m_widget)->list, "select-child",
1fb33bdb 1247 G_CALLBACK (gtkcombo_combo_select_child_callback),
9fa72bd2 1248 this);
590f50d6 1249 g_signal_connect_after (GTK_COMBO(m_widget)->entry, "changed",
1fb33bdb 1250 G_CALLBACK (gtkcombo_text_changed_callback),
9fa72bd2 1251 this );
590f50d6 1252 }
953704c1
RR
1253}
1254
b4071e91
RR
1255void wxComboBox::OnSize( wxSizeEvent &event )
1256{
590f50d6
RR
1257#ifdef __WXGTK24__
1258 if (!gtk_check_version(2,4,0))
1259 {
1260 // Do nothing
1261 }
1262 else
1263#endif
1264 {
1265 // NB: In some situations (e.g. on non-first page of a wizard, if the
1266 // size used is default size), GtkCombo widget is resized correctly,
1267 // but it's look is not updated, it's rendered as if it was much wider.
1268 // No other widgets are affected, so it looks like a bug in GTK+.
1269 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1270 // fixes it.
1271 if (GTK_WIDGET_VISIBLE(m_widget))
1272 gtk_widget_queue_resize(m_widget);
1273 }
260a67b7 1274
f03fc89f 1275 event.Skip();
6de97a3b 1276}
53010e52 1277
f40fdaa3 1278void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 1279{
590f50d6
RR
1280#ifdef __WXGTK24__
1281 if (!gtk_check_version(2,4,0))
1282 {
1283 // Do nothing
1284 }
1285 else
1286#endif
1287 {
f40fdaa3 1288// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 1289
590f50d6
RR
1290 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
1291 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 1292
590f50d6
RR
1293 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
1294 GList *child = list->children;
1295 while (child)
1296 {
1297 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 1298
590f50d6
RR
1299 GtkBin *bin = GTK_BIN(child->data);
1300 gtk_widget_modify_style( bin->child, style );
805dd538 1301
590f50d6
RR
1302 child = child->next;
1303 }
fd0eed64 1304 }
868a2826 1305}
b4071e91 1306
fd0eed64 1307GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 1308{
590f50d6
RR
1309 GtkEntry *entry = NULL;
1310#ifdef __WXGTK24__
1311 if (!gtk_check_version(2,4,0))
1312 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
1313 else
8228b893 1314#endif
590f50d6 1315 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
8228b893 1316
590f50d6 1317 return GTK_WIDGET( entry );
97b3455a
RR
1318}
1319
ef5c70f9 1320GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
b4071e91 1321{
590f50d6
RR
1322#ifdef __WXGTK24__
1323 if (!gtk_check_version(2,4,0))
1324 {
ef5c70f9
VZ
1325 wxUnusedVar(windows);
1326
1327 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
590f50d6
RR
1328 }
1329 else
ef5c70f9 1330#endif // GTK+ 2.4
590f50d6 1331 {
ef5c70f9
VZ
1332 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
1333 windows.push_back(GTK_COMBO(m_widget)->button->window);
1334
1335 // indicate that we return multiple windows in the windows array
1336 return NULL;
590f50d6 1337 }
b4071e91 1338}
ac57418f 1339
f68586e5
VZ
1340wxSize wxComboBox::DoGetBestSize() const
1341{
db434467 1342 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
1343
1344 // we know better our horizontal extent: it depends on the longest string
1345 // in the combobox
a6fc8ae3
VZ
1346 if ( m_widget )
1347 {
60d85ccb 1348 int width;
aa61d352
VZ
1349 unsigned int count = GetCount();
1350 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 1351 {
aa61d352 1352 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
1353 if ( width > ret.x )
1354 ret.x = width;
1355 }
1356 }
1357
1358 // empty combobox should have some reasonable default size too
1359 if ( ret.x < 100 )
1360 ret.x = 100;
9f884528
RD
1361
1362 CacheBestSize(ret);
db434467 1363 return ret;
f68586e5
VZ
1364}
1365
9d522606
RD
1366// static
1367wxVisualAttributes
1368wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1369{
590f50d6
RR
1370#ifdef __WXGTK24__
1371 if (!gtk_check_version(2,4,0))
1372 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
1373 else
1374#endif
1375 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
9d522606
RD
1376}
1377
150e31d2
JS
1378// ----------------------------------------------------------------------------
1379// standard event handling
1380// ----------------------------------------------------------------------------
1381
1382void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
1383{
1384 Cut();
1385}
1386
1387void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
1388{
1389 Copy();
1390}
1391
1392void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
1393{
1394 Paste();
1395}
1396
1397void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1398{
1399 Undo();
1400}
1401
1402void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1403{
1404 Redo();
1405}
1406
1407void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1408{
1409 long from, to;
1410 GetSelection(& from, & to);
1411 if (from != -1 && to != -1)
1412 Remove(from, to);
1413}
1414
1415void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1416{
1417 SetSelection(-1, -1);
1418}
1419
1420void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1421{
1422 event.Enable( CanCut() );
1423}
1424
1425void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1426{
1427 event.Enable( CanCopy() );
1428}
1429
1430void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1431{
1432 event.Enable( CanPaste() );
1433}
1434
1435void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1436{
1437 event.Enable( CanUndo() );
1438}
1439
1440void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1441{
1442 event.Enable( CanRedo() );
1443}
1444
1445void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1446{
1447 event.Enable(HasSelection() && IsEditable()) ;
1448}
1449
1450void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1451{
1452 event.Enable(GetLastPosition() > 0);
1453}
1454
dcf924a3 1455#endif