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