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