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