]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
avoid hidden GetImageList
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
53010e52 13#include "wx/combobox.h"
dcf924a3
RR
14
15#if wxUSE_COMBOBOX
16
72a16063 17#include "wx/settings.h"
584ad2a3 18#include "wx/arrstr.h"
b62c3631 19#include "wx/intl.h"
53010e52 20
78bcfcfc
VZ
21#include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
9e691f46 23#include "wx/gtk/private.h"
83624f79 24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
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
RR
45static void
46gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
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
RR
66static void
67gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
68{
69}
865bb325 70}
78b3b018 71
865bb325 72extern "C" {
9d6a9fdd
RR
73static void
74gtk_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.
40eb3606 78 int curSelection = combo->GetCurrentSelection();
9d6a9fdd
RR
79 if (g_SelectionBeforePopup != curSelection)
80 {
81 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
82 event.SetInt( curSelection );
83 event.SetString( combo->GetStringSelection() );
84 event.SetEventObject( combo );
85 combo->GetEventHandler()->ProcessEvent( event );
345bdf13
KH
86
87 // for consistency with the other ports, send TEXT event
88 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
89 event2.SetString( combo->GetStringSelection() );
90 event2.SetEventObject( combo );
91 combo->GetEventHandler()->ProcessEvent( event2 );
9d6a9fdd 92 }
7d8268a1 93
40eb3606
VZ
94 // reset the selection flag to value meaning that it is hidden
95 g_SelectionBeforePopup = wxID_NONE;
9d6a9fdd 96}
865bb325 97}
9d6a9fdd 98
865bb325 99extern "C" {
9d6a9fdd
RR
100static void
101gtk_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
102{
103 // store the combobox selection value before the popup is shown
40eb3606 104 g_SelectionBeforePopup = combo->GetCurrentSelection();
9d6a9fdd 105}
865bb325 106}
9d6a9fdd 107
53010e52 108//-----------------------------------------------------------------------------
461573cc 109// "select-child" - click/cursor get select-child, changed, select-child
47908e25 110//-----------------------------------------------------------------------------
47908e25 111
865bb325 112extern "C" {
8a85884a 113static void
461573cc 114gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 115{
acfd422a 116 if (g_isIdle) wxapp_install_idle_handler();
8a85884a 117
a2053b27 118 if (!combo->m_hasVMT) return;
30ed6e5c 119
acfd422a 120 if (g_blockEventsOnDrag) return;
805dd538 121
40eb3606 122 int curSelection = combo->GetCurrentSelection();
30ed6e5c 123
3c4e4af6
RR
124 if (combo->m_prevSelection == curSelection) return;
125
126 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
127 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
150e31d2 128
159b66c0
RR
129 combo->m_prevSelection = curSelection;
130
78b3b018
RR
131 // Quickly set the value of the combo box
132 // as GTK+ does that only AFTER the event
133 // is sent.
134 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry),
135 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
136 combo->SetValue( combo->GetStringSelection() );
58b907f6 137 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
78b3b018
RR
138 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
139
40eb3606 140 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
9d6a9fdd
RR
141 // because when combobox popup is shown, gtk_combo_select_child_callback is
142 // called each times the mouse is over an item with a pressed button so a lot
143 // of SELECTED event could be generated if the user keep the mouse button down
144 // and select other items ...
40eb3606 145 if (g_SelectionBeforePopup == wxID_NONE)
9d6a9fdd
RR
146 {
147 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
148 event.SetInt( curSelection );
149 event.SetString( combo->GetStringSelection() );
150 event.SetEventObject( combo );
151 combo->GetEventHandler()->ProcessEvent( event );
0c77152e 152
345bdf13 153 // for consistency with the other ports, don't generate text update
40eb3606
VZ
154 // events while the user is browsing the combobox neither
155 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
156 event2.SetString( combo->GetValue() );
157 event2.SetEventObject( combo );
158 combo->GetEventHandler()->ProcessEvent( event2 );
159 }
461573cc 160}
865bb325 161}
461573cc 162
e1e955e1
RR
163//-----------------------------------------------------------------------------
164// wxComboBox
53010e52
RR
165//-----------------------------------------------------------------------------
166
167IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
168
b4071e91 169BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 170 EVT_SIZE(wxComboBox::OnSize)
8a85884a 171 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
172
173 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
174 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
175 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
176 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
177 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
178 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
179 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
180
181 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
182 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
183 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
184 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
185 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
186 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
187 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
188END_EVENT_TABLE()
189
584ad2a3
MB
190bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
191 const wxString& value,
192 const wxPoint& pos, const wxSize& size,
193 const wxArrayString& choices,
194 long style, const wxValidator& validator,
195 const wxString& name )
196{
197 wxCArrayString chs(choices);
198
199 return Create( parent, id, value, pos, size, chs.GetCount(),
200 chs.GetStrings(), style, validator, name );
201}
202
fd0eed64
RR
203bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
204 const wxPoint& pos, const wxSize& size,
205 int n, const wxString choices[],
805dd538
VZ
206 long style, const wxValidator& validator,
207 const wxString& name )
53010e52 208{
7d8268a1
WS
209 m_ignoreNextUpdate = false;
210 m_needParent = true;
211 m_acceptsFocus = true;
159b66c0 212 m_prevSelection = 0;
805dd538 213
db434467 214 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
215 !CreateBase( parent, id, pos, size, style, validator, name ))
216 {
223d09f6 217 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 218 return false;
4dcaf11a 219 }
6de97a3b 220
fd0eed64 221 m_widget = gtk_combo_new();
461573cc 222 GtkCombo *combo = GTK_COMBO(m_widget);
30ed6e5c 223
461573cc
RR
224 // Disable GTK's broken events ...
225 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
226 // ... and add surogate handler.
227 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
7d8268a1 228 (GtkSignalFunc) gtk_dummy_callback, combo);
805dd538 229
8a85884a 230 // make it more useable
3ca6a5f0 231 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
30ed6e5c 232
3ca6a5f0
BP
233 // and case-sensitive
234 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
235
44c5573d 236#ifdef __WXGTK20__
02a6e358
RR
237 if (style & wxNO_BORDER)
238 g_object_set( GTK_ENTRY( combo->entry ), "has-frame", FALSE, NULL );
44c5573d 239#endif
7d8268a1 240
fd0eed64 241 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 242
2e1d7104 243#ifndef __WXGTK20__
81a0614b 244 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
2e1d7104 245#endif
159b66c0 246
fd0eed64
RR
247 for (int i = 0; i < n; i++)
248 {
fab591c5 249 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
805dd538 250
fd0eed64 251 m_clientDataList.Append( (wxObject*)NULL );
f5e27805 252 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 253
fd0eed64 254 gtk_container_add( GTK_CONTAINER(list), list_item );
805dd538 255
19da4326 256 gtk_widget_show( list_item );
fd0eed64 257 }
805dd538 258
f03fc89f 259 m_parent->DoAddChild( this );
30ed6e5c 260
461573cc 261 m_focusWidget = combo->entry;
805dd538 262
abdeb9e7 263 PostCreation(size);
53010e52 264
461573cc 265 ConnectWidget( combo->button );
805dd538 266
461573cc
RR
267 // MSW's combo box shows the value and the selection is -1
268 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
269 gtk_list_unselect_all( GTK_LIST(combo->list) );
805dd538 270
a260fe6a 271 if (style & wxCB_READONLY)
461573cc 272 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
a260fe6a 273
9d6a9fdd
RR
274 // "show" and "hide" events are generated when user click on the combobox button which popups a list
275 // this list is the "popwin" gtk widget
276 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
7d8268a1 277 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
9d6a9fdd 278 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
7d8268a1 279 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
9d6a9fdd 280
58b907f6 281 gtk_signal_connect_after( GTK_OBJECT(combo->entry), "changed",
461573cc
RR
282 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
283
58b907f6 284 gtk_signal_connect_after( GTK_OBJECT(combo->list), "select-child",
461573cc 285 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
805dd538 286
abdeb9e7 287 SetBestSize(size); // need this too because this is a wxControlWithItems
805dd538 288
abdeb9e7 289 // This is required for tool bar support
024e9a4c
RR
290// wxSize setsize = GetSize();
291// gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
150e31d2 292
7d8268a1 293 return true;
fd0eed64
RR
294}
295
296wxComboBox::~wxComboBox()
297{
222ed1d6 298 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
299 while (node)
300 {
b1d4dd7a 301 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 302 if (cd) delete cd;
b1d4dd7a 303 node = node->GetNext();
fd0eed64 304 }
7d6d2cd4
RR
305 m_clientObjectList.Clear();
306
fd0eed64 307 m_clientDataList.Clear();
6de97a3b 308}
53010e52 309
2b5f62a0
VZ
310void wxComboBox::SetFocus()
311{
312 if ( m_hasFocus )
313 {
314 // don't do anything if we already have focus
315 return;
316 }
317
318 gtk_widget_grab_focus( m_focusWidget );
319}
320
6f6f938f 321int wxComboBox::DoAppend( const wxString &item )
53010e52 322{
2a68b7a0 323 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 324
461573cc 325 DisableEvents();
30ed6e5c 326
fd0eed64 327 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 328
fab591c5 329 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
805dd538 330
ec5d85fb
RR
331 gtk_container_add( GTK_CONTAINER(list), list_item );
332
2b07d713
RR
333 if (GTK_WIDGET_REALIZED(m_widget))
334 {
335 gtk_widget_realize( list_item );
336 gtk_widget_realize( GTK_BIN(list_item)->child );
631a05be 337 }
2b07d713 338
631a05be
RL
339 // Apply current widget style to the new list_item
340 GtkRcStyle *style = CreateWidgetStyle();
341 if (style)
342 {
343 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
344 GtkBin *bin = GTK_BIN( list_item );
345 GtkWidget *label = GTK_WIDGET( bin->child );
346 gtk_widget_modify_style( label, style );
347 gtk_rc_style_unref( style );
2b07d713 348 }
805dd538 349
fd0eed64 350 gtk_widget_show( list_item );
30ed6e5c 351
6f6f938f 352 const int count = GetCount();
53010e52 353
6f6f938f 354 if ( (int)m_clientDataList.GetCount() < count )
0a164d4c 355 m_clientDataList.Append( (wxObject*) NULL );
6f6f938f 356 if ( (int)m_clientObjectList.GetCount() < count )
0a164d4c 357 m_clientObjectList.Append( (wxObject*) NULL );
805dd538 358
6f6f938f 359 EnableEvents();
805dd538 360
b0021947
VS
361 InvalidateBestSize();
362
6f6f938f 363 return count - 1;
fd0eed64
RR
364}
365
6f6f938f 366int wxComboBox::DoInsert( const wxString &item, int pos )
243dbf1a 367{
708c45a6
VZ
368 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
369 wxT("can't insert into sorted list"));
370
371 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
243dbf1a
VZ
372
373 int count = GetCount();
6f6f938f
VZ
374 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
375
243dbf1a 376 if (pos == count)
6f6f938f 377 return Append(item);
243dbf1a
VZ
378
379 DisableEvents();
380
381 GtkWidget *list = GTK_COMBO(m_widget)->list;
382
383 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
384
385 GList *gitem_list = g_list_alloc ();
386 gitem_list->data = list_item;
387 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
388
389 if (GTK_WIDGET_REALIZED(m_widget))
390 {
391 gtk_widget_realize( list_item );
392 gtk_widget_realize( GTK_BIN(list_item)->child );
393
f40fdaa3 394 ApplyWidgetStyle();
243dbf1a
VZ
395 }
396
397 gtk_widget_show( list_item );
398
6f6f938f 399 count = GetCount();
243dbf1a 400
6f6f938f 401 if ( (int)m_clientDataList.GetCount() < count )
0a164d4c 402 m_clientDataList.Insert( pos, (wxObject*) NULL );
6f6f938f 403 if ( (int)m_clientObjectList.GetCount() < count )
0a164d4c 404 m_clientObjectList.Insert( pos, (wxObject*) NULL );
243dbf1a 405
6f6f938f 406 EnableEvents();
150e31d2 407
b0021947 408 InvalidateBestSize();
243dbf1a 409
6f6f938f 410 return pos;
243dbf1a
VZ
411}
412
6f6f938f 413void wxComboBox::DoSetItemClientData( int n, void* clientData )
fd0eed64 414{
223d09f6 415 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 416
222ed1d6 417 wxList::compatibility_iterator node = m_clientDataList.Item( n );
fd0eed64 418 if (!node) return;
805dd538 419
f5e27805 420 node->SetData( (wxObject*) clientData );
6de97a3b 421}
53010e52 422
6f6f938f 423void* wxComboBox::DoGetItemClientData( int n ) const
53010e52 424{
223d09f6 425 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 426
222ed1d6 427 wxList::compatibility_iterator node = m_clientDataList.Item( n );
805dd538 428
30ed6e5c 429 return node ? node->GetData() : NULL;
fd0eed64
RR
430}
431
6f6f938f 432void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 433{
223d09f6 434 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 435
222ed1d6 436 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
fd0eed64 437 if (!node) return;
805dd538 438
e94e2e95 439 // wxItemContainer already deletes data for us
805dd538 440
fd0eed64 441 node->SetData( (wxObject*) clientData );
6de97a3b 442}
53010e52 443
6f6f938f 444wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
53010e52 445{
223d09f6 446 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 447
222ed1d6 448 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
805dd538 449
30ed6e5c 450 return node ? (wxClientData*) node->GetData() : NULL;
fd0eed64
RR
451}
452
453void wxComboBox::Clear()
454{
223d09f6 455 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 456
461573cc 457 DisableEvents();
30ed6e5c 458
fd0eed64 459 GtkWidget *list = GTK_COMBO(m_widget)->list;
6f6f938f 460 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
805dd538 461
222ed1d6 462 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
463 while (node)
464 {
b1d4dd7a 465 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 466 if (cd) delete cd;
b1d4dd7a 467 node = node->GetNext();
fd0eed64 468 }
f5e27805 469 m_clientObjectList.Clear();
805dd538 470
fd0eed64 471 m_clientDataList.Clear();
30ed6e5c 472
461573cc 473 EnableEvents();
b0021947
VS
474
475 InvalidateBestSize();
6de97a3b 476}
53010e52 477
fd0eed64 478void wxComboBox::Delete( int n )
53010e52 479{
223d09f6 480 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 481
fd0eed64 482 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 483
fd0eed64 484 GList *child = g_list_nth( listbox->children, n );
805dd538 485
fd0eed64
RR
486 if (!child)
487 {
223d09f6 488 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
489 return;
490 }
805dd538 491
461573cc 492 DisableEvents();
30ed6e5c 493
bbe0af5b 494 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
495 gtk_list_remove_items( listbox, list );
496 g_list_free( list );
805dd538 497
222ed1d6 498 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
f5e27805 499 if (node)
fd0eed64 500 {
b1d4dd7a 501 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 502 if (cd) delete cd;
222ed1d6 503 m_clientObjectList.Erase( node );
f5e27805 504 }
805dd538 505
b1d4dd7a 506 node = m_clientDataList.Item( n );
f5e27805 507 if (node)
222ed1d6 508 m_clientDataList.Erase( node );
150e31d2 509
461573cc 510 EnableEvents();
150e31d2 511
b0021947 512 InvalidateBestSize();
461573cc
RR
513}
514
515void wxComboBox::SetString(int n, const wxString &text)
516{
517 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
518
519 GtkWidget *list = GTK_COMBO(m_widget)->list;
520
521 GList *child = g_list_nth( GTK_LIST(list)->children, n );
522 if (child)
523 {
524 GtkBin *bin = GTK_BIN( child->data );
525 GtkLabel *label = GTK_LABEL( bin->child );
526 gtk_label_set_text(label, wxGTK_CONV(text));
527 }
528 else
529 {
530 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 531 }
150e31d2 532
b0021947 533 InvalidateBestSize();
6de97a3b 534}
53010e52 535
11e62fe6 536int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 537{
0a164d4c 538 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 539
fd0eed64 540 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 541
53010e52
RR
542 GList *child = GTK_LIST(list)->children;
543 int count = 0;
544 while (child)
545 {
fd0eed64
RR
546 GtkBin *bin = GTK_BIN( child->data );
547 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0
VZ
548#ifdef __WXGTK20__
549 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
550#else
551 wxString str( label->label );
552#endif
11e62fe6 553 if (item.IsSameAs( str , bCase ) )
7cf8cb48 554 return count;
30ed6e5c 555
fd0eed64
RR
556 count++;
557 child = child->next;
558 }
805dd538 559
7cf8cb48 560 return wxNOT_FOUND;
fd0eed64
RR
561}
562
563int wxComboBox::GetSelection() const
40eb3606
VZ
564{
565 // if the popup is currently opened, use the selection as it had been
566 // before it dropped down
567 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
568 : g_SelectionBeforePopup;
569}
570
571int wxComboBox::GetCurrentSelection() const
fd0eed64 572{
223d09f6 573 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 574
fd0eed64 575 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 576
fd0eed64
RR
577 GList *selection = GTK_LIST(list)->selection;
578 if (selection)
579 {
580 GList *child = GTK_LIST(list)->children;
581 int count = 0;
582 while (child)
583 {
584 if (child->data == selection->data) return count;
585 count++;
586 child = child->next;
587 }
6de97a3b 588 }
805dd538 589
fd0eed64 590 return -1;
6de97a3b 591}
53010e52 592
debe6624 593wxString wxComboBox::GetString( int n ) const
53010e52 594{
0a164d4c 595 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 596
fd0eed64 597 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 598
7cf8cb48 599 wxString str;
fd0eed64
RR
600 GList *child = g_list_nth( GTK_LIST(list)->children, n );
601 if (child)
602 {
603 GtkBin *bin = GTK_BIN( child->data );
604 GtkLabel *label = GTK_LABEL( bin->child );
2e1d7104 605#ifdef __WXGTK20__
2b5f62a0 606 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
2e1d7104
RR
607#else
608 str = wxString( label->label );
609#endif
7cf8cb48
VZ
610 }
611 else
612 {
223d09f6 613 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 614 }
805dd538 615
7cf8cb48 616 return str;
6de97a3b 617}
53010e52 618
fd0eed64 619wxString wxComboBox::GetStringSelection() const
53010e52 620{
0a164d4c 621 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 622
fd0eed64 623 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 624
fd0eed64
RR
625 GList *selection = GTK_LIST(list)->selection;
626 if (selection)
627 {
628 GtkBin *bin = GTK_BIN( selection->data );
2b5f62a0
VZ
629 GtkLabel *label = GTK_LABEL( bin->child );
630#ifdef __WXGTK20__
631 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
632#else
633 wxString tmp( label->label );
634#endif
fd0eed64
RR
635 return tmp;
636 }
805dd538 637
223d09f6 638 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 639
0a164d4c 640 return wxEmptyString;
6de97a3b 641}
53010e52 642
6f6f938f 643int wxComboBox::GetCount() const
53010e52 644{
223d09f6 645 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 646
fd0eed64 647 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 648
fd0eed64
RR
649 GList *child = GTK_LIST(list)->children;
650 int count = 0;
651 while (child) { count++; child = child->next; }
652 return count;
6de97a3b 653}
53010e52 654
debe6624 655void wxComboBox::SetSelection( int n )
53010e52 656{
223d09f6 657 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 658
953704c1
RR
659 DisableEvents();
660
fd0eed64 661 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 662 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 663 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 664 m_prevSelection = n;
953704c1
RR
665
666 EnableEvents();
6de97a3b 667}
53010e52 668
fd0eed64 669wxString wxComboBox::GetValue() const
53010e52 670{
2e1d7104
RR
671 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
672 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
673
30ed6e5c 674#if 0
2e1d7104
RR
675 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
676 {
677 wxChar c = tmp[i];
678 printf( "%d ", (int) (c) );
679 }
680 printf( "\n" );
681#endif
30ed6e5c 682
fd0eed64 683 return tmp;
6de97a3b 684}
53010e52
RR
685
686void wxComboBox::SetValue( const wxString& value )
687{
223d09f6 688 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 689
fd0eed64 690 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
0a164d4c 691 wxString tmp;
fd0eed64 692 if (!value.IsNull()) tmp = value;
fab591c5 693 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
150e31d2 694
b0021947 695 InvalidateBestSize();
6de97a3b 696}
53010e52 697
fd0eed64 698void wxComboBox::Copy()
53010e52 699{
223d09f6 700 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 701
fd0eed64 702 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 703 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 704}
53010e52 705
fd0eed64 706void wxComboBox::Cut()
53010e52 707{
223d09f6 708 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 709
fd0eed64 710 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 711 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 712}
53010e52 713
fd0eed64 714void wxComboBox::Paste()
53010e52 715{
223d09f6 716 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 717
fd0eed64 718 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 719 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 720}
53010e52 721
150e31d2
JS
722void wxComboBox::Undo()
723{
724 // TODO
725}
726
727void wxComboBox::Redo()
728{
729 // TODO
730}
731
732void wxComboBox::SelectAll()
733{
4e324a3f 734 SetSelection(0, GetLastPosition());
150e31d2
JS
735}
736
737bool wxComboBox::CanUndo() const
738{
739 // TODO
740 return false;
741}
742
743bool wxComboBox::CanRedo() const
744{
745 // TODO
746 return false;
747}
748
749bool wxComboBox::HasSelection() const
750{
751 long from, to;
752 GetSelection(&from, &to);
753 return from != to;
754}
755
756bool wxComboBox::CanCopy() const
757{
758 // Can copy if there's a selection
759 return HasSelection();
760}
761
762bool wxComboBox::CanCut() const
763{
764 return CanCopy() && IsEditable();
765}
766
767bool wxComboBox::CanPaste() const
768{
769 // TODO: check for text on the clipboard
770 return IsEditable() ;
771}
772
773bool wxComboBox::IsEditable() const
774{
775 return !HasFlag(wxCB_READONLY);
776}
777
778
debe6624 779void wxComboBox::SetInsertionPoint( long pos )
53010e52 780{
223d09f6 781 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 782
6f6f938f
VZ
783 if ( pos == GetLastPosition() )
784 pos = -1;
785
fd0eed64 786 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 787 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 788}
53010e52 789
fd0eed64 790long wxComboBox::GetInsertionPoint() const
53010e52 791{
9e691f46 792 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 793}
53010e52 794
7d8268a1 795wxTextPos wxComboBox::GetLastPosition() const
53010e52 796{
fd0eed64
RR
797 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
798 int pos = GTK_ENTRY(entry)->text_length;
799 return (long) pos-1;
6de97a3b 800}
53010e52 801
debe6624 802void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 803{
223d09f6 804 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 805
fd0eed64
RR
806 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
807 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
808 if (value.IsNull()) return;
809 gint pos = (gint)to;
30ed6e5c 810
2e1d7104
RR
811#if wxUSE_UNICODE
812 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
813 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
814#else
815 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
816#endif
6de97a3b 817}
53010e52 818
20d10ee1 819void wxComboBox::SetSelection( long from, long to )
53010e52 820{
20d10ee1
VZ
821 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
822 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 823}
53010e52 824
150e31d2
JS
825void wxComboBox::GetSelection( long* from, long* to ) const
826{
827 if (IsEditable())
828 {
829 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
41eb6d76 830#ifdef __WXGTK20__
4e324a3f
JS
831 gint start, end;
832 gtk_editable_get_selection_bounds(editable, & start, & end);
833 *from = start;
834 *to = end;
835#else
150e31d2
JS
836 *from = (long) editable->selection_start_pos;
837 *to = (long) editable->selection_end_pos;
4e324a3f 838#endif
150e31d2
JS
839 }
840}
841
20d10ee1 842void wxComboBox::SetEditable( bool editable )
53010e52 843{
20d10ee1
VZ
844 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
845 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
846}
847
8a85884a
VZ
848void wxComboBox::OnChar( wxKeyEvent &event )
849{
12a3f227 850 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 851 {
461573cc 852 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
853 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
854 eventEnter.SetString( GetValue() );
855 eventEnter.SetInt( GetSelection() );
856 eventEnter.SetEventObject( this );
3352cfff 857
17a1ebd1 858 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
859 {
860 // This will invoke the dialog default action, such
861 // as the clicking the default button.
862
863 wxWindow *top_frame = m_parent;
864 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
865 top_frame = top_frame->GetParent();
866
867 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
868 {
869 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
870
871 if (window->default_widget)
150e31d2 872 gtk_widget_activate (window->default_widget);
3352cfff
RR
873 }
874 }
30ed6e5c 875
461573cc
RR
876 // Catch GTK event so that GTK doesn't open the drop
877 // down list upon RETURN.
0878fb4c 878 return;
8a85884a 879 }
30ed6e5c 880
7cf8cb48 881 event.Skip();
8a85884a
VZ
882}
883
953704c1
RR
884void wxComboBox::DisableEvents()
885{
461573cc
RR
886 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
887 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
888 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
889 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
890}
891
892void wxComboBox::EnableEvents()
893{
58b907f6 894 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
461573cc 895 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
58b907f6 896 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
461573cc 897 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
898}
899
b4071e91
RR
900void wxComboBox::OnSize( wxSizeEvent &event )
901{
260a67b7
VS
902 // NB: In some situations (e.g. on non-first page of a wizard, if the
903 // size used is default size), GtkCombo widget is resized correctly,
904 // but it's look is not updated, it's rendered as if it was much wider.
905 // No other widgets are affected, so it looks like a bug in GTK+.
906 // Manually requesting resize calculation (as gtk_pizza_set_size does)
907 // fixes it.
908 if (GTK_WIDGET_VISIBLE(m_widget))
909 gtk_widget_queue_resize(m_widget);
910
f03fc89f 911 event.Skip();
6de97a3b 912}
53010e52 913
f40fdaa3 914void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 915{
f40fdaa3 916// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 917
f40fdaa3
VS
918 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
919 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 920
fd0eed64
RR
921 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
922 GList *child = list->children;
923 while (child)
924 {
f40fdaa3 925 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 926
fd0eed64 927 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 928 gtk_widget_modify_style( bin->child, style );
805dd538 929
fd0eed64
RR
930 child = child->next;
931 }
868a2826 932}
b4071e91 933
fd0eed64 934GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 935{
fd0eed64 936 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
937}
938
b4071e91
RR
939bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
940{
fd0eed64
RR
941 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
942 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 943}
ac57418f 944
f68586e5
VZ
945wxSize wxComboBox::DoGetBestSize() const
946{
db434467 947 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
948
949 // we know better our horizontal extent: it depends on the longest string
950 // in the combobox
a6fc8ae3
VZ
951 if ( m_widget )
952 {
60d85ccb
RR
953 int width;
954 size_t count = GetCount();
a6fc8ae3
VZ
955 for ( size_t n = 0; n < count; n++ )
956 {
2b1ff57f 957 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
958 if ( width > ret.x )
959 ret.x = width;
960 }
961 }
962
963 // empty combobox should have some reasonable default size too
964 if ( ret.x < 100 )
965 ret.x = 100;
9f884528
RD
966
967 CacheBestSize(ret);
db434467 968 return ret;
f68586e5
VZ
969}
970
9d522606
RD
971// static
972wxVisualAttributes
973wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
974{
975 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
976}
977
150e31d2
JS
978// ----------------------------------------------------------------------------
979// standard event handling
980// ----------------------------------------------------------------------------
981
982void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
983{
984 Cut();
985}
986
987void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
988{
989 Copy();
990}
991
992void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
993{
994 Paste();
995}
996
997void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
998{
999 Undo();
1000}
1001
1002void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1003{
1004 Redo();
1005}
1006
1007void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1008{
1009 long from, to;
1010 GetSelection(& from, & to);
1011 if (from != -1 && to != -1)
1012 Remove(from, to);
1013}
1014
1015void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1016{
1017 SetSelection(-1, -1);
1018}
1019
1020void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1021{
1022 event.Enable( CanCut() );
1023}
1024
1025void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1026{
1027 event.Enable( CanCopy() );
1028}
1029
1030void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1031{
1032 event.Enable( CanPaste() );
1033}
1034
1035void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1036{
1037 event.Enable( CanUndo() );
1038}
1039
1040void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1041{
1042 event.Enable( CanRedo() );
1043}
1044
1045void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1046{
1047 event.Enable(HasSelection() && IsEditable()) ;
1048}
1049
1050void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1051{
1052 event.Enable(GetLastPosition() > 0);
1053}
1054
dcf924a3 1055#endif