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