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