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