]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
Applied patch [ 1427626 ] Fix wxFileConfig so that it still works after DeleteAll()
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
53010e52 13#include "wx/combobox.h"
dcf924a3
RR
14
15#if wxUSE_COMBOBOX
16
72a16063 17#include "wx/settings.h"
584ad2a3 18#include "wx/arrstr.h"
b62c3631 19#include "wx/intl.h"
53010e52 20
78bcfcfc
VZ
21#include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
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
58 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
59 event.SetString( combo->GetValue() );
60 event.SetEventObject( combo );
61 combo->GetEventHandler()->ProcessEvent( event );
62}
865bb325 63}
78b3b018 64
865bb325 65extern "C" {
78b3b018
RR
66static void
67gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
68{
69}
865bb325 70}
78b3b018 71
865bb325 72extern "C" {
9d6a9fdd
RR
73static void
74gtk_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
7d8268a1 75{
9d6a9fdd
RR
76 // when the popup is hidden, throw a SELECTED event only if the combobox
77 // selection changed.
3dbfe8f4
VZ
78 const int curSelection = combo->GetCurrentSelection();
79
80 const bool hasChanged = curSelection != g_SelectionBeforePopup;
81
82 // reset the selection flag to value meaning that it is hidden and do it
83 // now, before generating the events, so that GetSelection() returns the
84 // new value from the event handler
85 g_SelectionBeforePopup = wxID_NONE;
86
87 if ( hasChanged )
9d6a9fdd
RR
88 {
89 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
90 event.SetInt( curSelection );
91 event.SetString( combo->GetStringSelection() );
92 event.SetEventObject( combo );
93 combo->GetEventHandler()->ProcessEvent( event );
345bdf13
KH
94
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
97 event2.SetString( combo->GetStringSelection() );
98 event2.SetEventObject( combo );
99 combo->GetEventHandler()->ProcessEvent( event2 );
9d6a9fdd 100 }
9d6a9fdd 101}
865bb325 102}
9d6a9fdd 103
865bb325 104extern "C" {
9d6a9fdd
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
RR
151 {
152 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
153 event.SetInt( curSelection );
154 event.SetString( combo->GetStringSelection() );
155 event.SetEventObject( combo );
156 combo->GetEventHandler()->ProcessEvent( event );
0c77152e 157
345bdf13 158 // for consistency with the other ports, don't generate text update
40eb3606
VZ
159 // events while the user is browsing the combobox neither
160 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
161 event2.SetString( combo->GetValue() );
162 event2.SetEventObject( combo );
163 combo->GetEventHandler()->ProcessEvent( event2 );
164 }
461573cc 165}
865bb325 166}
461573cc 167
e1e955e1
RR
168//-----------------------------------------------------------------------------
169// wxComboBox
53010e52
RR
170//-----------------------------------------------------------------------------
171
172IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
173
b4071e91 174BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 175 EVT_SIZE(wxComboBox::OnSize)
8a85884a 176 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
177
178 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
179 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
180 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
181 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
182 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
183 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
184 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
185
186 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
187 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
188 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
189 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
190 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
191 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
192 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
193END_EVENT_TABLE()
194
584ad2a3
MB
195bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
196 const wxString& value,
197 const wxPoint& pos, const wxSize& size,
198 const wxArrayString& choices,
199 long style, const wxValidator& validator,
200 const wxString& name )
201{
202 wxCArrayString chs(choices);
203
204 return Create( parent, id, value, pos, size, chs.GetCount(),
205 chs.GetStrings(), style, validator, name );
206}
207
fd0eed64
RR
208bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
209 const wxPoint& pos, const wxSize& size,
210 int n, const wxString choices[],
805dd538
VZ
211 long style, const wxValidator& validator,
212 const wxString& name )
53010e52 213{
7d8268a1
WS
214 m_ignoreNextUpdate = false;
215 m_needParent = true;
216 m_acceptsFocus = true;
159b66c0 217 m_prevSelection = 0;
805dd538 218
db434467 219 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
220 !CreateBase( parent, id, pos, size, style, validator, name ))
221 {
223d09f6 222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 223 return false;
4dcaf11a 224 }
6de97a3b 225
fd0eed64 226 m_widget = gtk_combo_new();
461573cc 227 GtkCombo *combo = GTK_COMBO(m_widget);
30ed6e5c 228
461573cc
RR
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
90e572f1 231 // ... and add surrogate handler.
461573cc 232 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
7d8268a1 233 (GtkSignalFunc) gtk_dummy_callback, combo);
805dd538 234
8a85884a 235 // make it more useable
3ca6a5f0 236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
30ed6e5c 237
3ca6a5f0
BP
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
240
fd0eed64 241 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 242
81a0614b 243 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
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
11e62fe6 534int wxComboBox::FindString( const wxString &item, bool bCase ) 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 546 wxString str( label->label );
11e62fe6 547 if (item.IsSameAs( str , bCase ) )
7cf8cb48 548 return count;
30ed6e5c 549
fd0eed64
RR
550 count++;
551 child = child->next;
552 }
805dd538 553
7cf8cb48 554 return wxNOT_FOUND;
fd0eed64
RR
555}
556
557int wxComboBox::GetSelection() const
40eb3606
VZ
558{
559 // if the popup is currently opened, use the selection as it had been
560 // before it dropped down
561 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
562 : g_SelectionBeforePopup;
563}
564
565int wxComboBox::GetCurrentSelection() const
fd0eed64 566{
223d09f6 567 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 568
fd0eed64 569 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 570
fd0eed64
RR
571 GList *selection = GTK_LIST(list)->selection;
572 if (selection)
573 {
574 GList *child = GTK_LIST(list)->children;
575 int count = 0;
576 while (child)
577 {
578 if (child->data == selection->data) return count;
579 count++;
580 child = child->next;
581 }
6de97a3b 582 }
805dd538 583
fd0eed64 584 return -1;
6de97a3b 585}
53010e52 586
debe6624 587wxString wxComboBox::GetString( int n ) const
53010e52 588{
0a164d4c 589 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 590
fd0eed64 591 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 592
7cf8cb48 593 wxString str;
fd0eed64
RR
594 GList *child = g_list_nth( GTK_LIST(list)->children, n );
595 if (child)
596 {
597 GtkBin *bin = GTK_BIN( child->data );
598 GtkLabel *label = GTK_LABEL( bin->child );
2e1d7104 599 str = wxString( label->label );
7cf8cb48
VZ
600 }
601 else
602 {
223d09f6 603 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 604 }
805dd538 605
7cf8cb48 606 return str;
6de97a3b 607}
53010e52 608
fd0eed64 609wxString wxComboBox::GetStringSelection() const
53010e52 610{
0a164d4c 611 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 612
fd0eed64 613 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 614
fd0eed64
RR
615 GList *selection = GTK_LIST(list)->selection;
616 if (selection)
617 {
618 GtkBin *bin = GTK_BIN( selection->data );
2b5f62a0 619 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0 620 wxString tmp( label->label );
fd0eed64
RR
621 return tmp;
622 }
805dd538 623
223d09f6 624 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 625
0a164d4c 626 return wxEmptyString;
6de97a3b 627}
53010e52 628
6f6f938f 629int wxComboBox::GetCount() const
53010e52 630{
223d09f6 631 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 632
fd0eed64 633 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 634
fd0eed64
RR
635 GList *child = GTK_LIST(list)->children;
636 int count = 0;
637 while (child) { count++; child = child->next; }
638 return count;
6de97a3b 639}
53010e52 640
debe6624 641void wxComboBox::SetSelection( int n )
53010e52 642{
223d09f6 643 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 644
953704c1
RR
645 DisableEvents();
646
fd0eed64 647 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 648 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 649 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 650 m_prevSelection = n;
953704c1
RR
651
652 EnableEvents();
6de97a3b 653}
53010e52 654
fd0eed64 655wxString wxComboBox::GetValue() const
53010e52 656{
2e1d7104
RR
657 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
658 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
659
30ed6e5c 660#if 0
2e1d7104
RR
661 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
662 {
663 wxChar c = tmp[i];
664 printf( "%d ", (int) (c) );
665 }
666 printf( "\n" );
667#endif
30ed6e5c 668
fd0eed64 669 return tmp;
6de97a3b 670}
53010e52
RR
671
672void wxComboBox::SetValue( const wxString& value )
673{
223d09f6 674 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 675
fd0eed64 676 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
0a164d4c 677 wxString tmp;
fd0eed64 678 if (!value.IsNull()) tmp = value;
fab591c5 679 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
150e31d2 680
b0021947 681 InvalidateBestSize();
6de97a3b 682}
53010e52 683
fd0eed64 684void wxComboBox::Copy()
53010e52 685{
223d09f6 686 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 687
fd0eed64 688 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 689 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 690}
53010e52 691
fd0eed64 692void wxComboBox::Cut()
53010e52 693{
223d09f6 694 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 695
fd0eed64 696 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 697 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 698}
53010e52 699
fd0eed64 700void wxComboBox::Paste()
53010e52 701{
223d09f6 702 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 703
fd0eed64 704 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 705 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 706}
53010e52 707
150e31d2
JS
708void wxComboBox::Undo()
709{
710 // TODO
711}
712
713void wxComboBox::Redo()
714{
715 // TODO
716}
717
718void wxComboBox::SelectAll()
719{
4e324a3f 720 SetSelection(0, GetLastPosition());
150e31d2
JS
721}
722
723bool wxComboBox::CanUndo() const
724{
725 // TODO
726 return false;
727}
728
729bool wxComboBox::CanRedo() const
730{
731 // TODO
732 return false;
733}
734
735bool wxComboBox::HasSelection() const
736{
737 long from, to;
738 GetSelection(&from, &to);
739 return from != to;
740}
741
742bool wxComboBox::CanCopy() const
743{
744 // Can copy if there's a selection
745 return HasSelection();
746}
747
748bool wxComboBox::CanCut() const
749{
750 return CanCopy() && IsEditable();
751}
752
753bool wxComboBox::CanPaste() const
754{
755 // TODO: check for text on the clipboard
756 return IsEditable() ;
757}
758
759bool wxComboBox::IsEditable() const
760{
761 return !HasFlag(wxCB_READONLY);
762}
763
764
debe6624 765void wxComboBox::SetInsertionPoint( long pos )
53010e52 766{
223d09f6 767 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 768
6f6f938f
VZ
769 if ( pos == GetLastPosition() )
770 pos = -1;
771
fd0eed64 772 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 773 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 774}
53010e52 775
fd0eed64 776long wxComboBox::GetInsertionPoint() const
53010e52 777{
9e691f46 778 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 779}
53010e52 780
7d8268a1 781wxTextPos wxComboBox::GetLastPosition() const
53010e52 782{
fd0eed64
RR
783 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
784 int pos = GTK_ENTRY(entry)->text_length;
785 return (long) pos-1;
6de97a3b 786}
53010e52 787
debe6624 788void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 789{
223d09f6 790 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 791
fd0eed64
RR
792 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
793 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
794 if (value.IsNull()) return;
795 gint pos = (gint)to;
30ed6e5c 796
2e1d7104
RR
797#if wxUSE_UNICODE
798 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
799 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
800#else
801 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
802#endif
6de97a3b 803}
53010e52 804
20d10ee1 805void wxComboBox::SetSelection( long from, long to )
53010e52 806{
20d10ee1
VZ
807 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
808 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 809}
53010e52 810
150e31d2
JS
811void wxComboBox::GetSelection( long* from, long* to ) const
812{
813 if (IsEditable())
814 {
815 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
816 *from = (long) editable->selection_start_pos;
817 *to = (long) editable->selection_end_pos;
818 }
819}
820
20d10ee1 821void wxComboBox::SetEditable( bool editable )
53010e52 822{
20d10ee1
VZ
823 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
824 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
825}
826
8a85884a
VZ
827void wxComboBox::OnChar( wxKeyEvent &event )
828{
12a3f227 829 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 830 {
461573cc 831 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
832 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
833 eventEnter.SetString( GetValue() );
834 eventEnter.SetInt( GetSelection() );
835 eventEnter.SetEventObject( this );
3352cfff 836
17a1ebd1 837 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
838 {
839 // This will invoke the dialog default action, such
840 // as the clicking the default button.
841
842 wxWindow *top_frame = m_parent;
843 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
844 top_frame = top_frame->GetParent();
845
846 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
847 {
848 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
849
850 if (window->default_widget)
150e31d2 851 gtk_widget_activate (window->default_widget);
3352cfff
RR
852 }
853 }
30ed6e5c 854
461573cc
RR
855 // Catch GTK event so that GTK doesn't open the drop
856 // down list upon RETURN.
0878fb4c 857 return;
8a85884a 858 }
30ed6e5c 859
7cf8cb48 860 event.Skip();
8a85884a
VZ
861}
862
953704c1
RR
863void wxComboBox::DisableEvents()
864{
461573cc
RR
865 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
866 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
867 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
868 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
869}
870
871void wxComboBox::EnableEvents()
872{
58b907f6 873 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
461573cc 874 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
58b907f6 875 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
461573cc 876 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
877}
878
b4071e91
RR
879void wxComboBox::OnSize( wxSizeEvent &event )
880{
260a67b7
VS
881 // NB: In some situations (e.g. on non-first page of a wizard, if the
882 // size used is default size), GtkCombo widget is resized correctly,
883 // but it's look is not updated, it's rendered as if it was much wider.
884 // No other widgets are affected, so it looks like a bug in GTK+.
885 // Manually requesting resize calculation (as gtk_pizza_set_size does)
886 // fixes it.
887 if (GTK_WIDGET_VISIBLE(m_widget))
888 gtk_widget_queue_resize(m_widget);
889
f03fc89f 890 event.Skip();
6de97a3b 891}
53010e52 892
f40fdaa3 893void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 894{
f40fdaa3 895// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 896
f40fdaa3
VS
897 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
898 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 899
fd0eed64
RR
900 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
901 GList *child = list->children;
902 while (child)
903 {
f40fdaa3 904 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 905
fd0eed64 906 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 907 gtk_widget_modify_style( bin->child, style );
805dd538 908
fd0eed64
RR
909 child = child->next;
910 }
868a2826 911}
b4071e91 912
fd0eed64 913GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 914{
fd0eed64 915 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
916}
917
b4071e91
RR
918bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
919{
fd0eed64
RR
920 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
921 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 922}
ac57418f 923
f68586e5
VZ
924wxSize wxComboBox::DoGetBestSize() const
925{
db434467 926 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
927
928 // we know better our horizontal extent: it depends on the longest string
929 // in the combobox
a6fc8ae3
VZ
930 if ( m_widget )
931 {
60d85ccb
RR
932 int width;
933 size_t count = GetCount();
a6fc8ae3
VZ
934 for ( size_t n = 0; n < count; n++ )
935 {
2b1ff57f 936 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
937 if ( width > ret.x )
938 ret.x = width;
939 }
940 }
941
942 // empty combobox should have some reasonable default size too
943 if ( ret.x < 100 )
944 ret.x = 100;
9f884528
RD
945
946 CacheBestSize(ret);
db434467 947 return ret;
f68586e5
VZ
948}
949
9d522606
RD
950// static
951wxVisualAttributes
952wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
953{
954 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
955}
956
150e31d2
JS
957// ----------------------------------------------------------------------------
958// standard event handling
959// ----------------------------------------------------------------------------
960
961void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
962{
963 Cut();
964}
965
966void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
967{
968 Copy();
969}
970
971void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
972{
973 Paste();
974}
975
976void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
977{
978 Undo();
979}
980
981void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
982{
983 Redo();
984}
985
986void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
987{
988 long from, to;
989 GetSelection(& from, & to);
990 if (from != -1 && to != -1)
991 Remove(from, to);
992}
993
994void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
995{
996 SetSelection(-1, -1);
997}
998
999void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1000{
1001 event.Enable( CanCut() );
1002}
1003
1004void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1005{
1006 event.Enable( CanCopy() );
1007}
1008
1009void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1010{
1011 event.Enable( CanPaste() );
1012}
1013
1014void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1015{
1016 event.Enable( CanUndo() );
1017}
1018
1019void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1020{
1021 event.Enable( CanRedo() );
1022}
1023
1024void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1025{
1026 event.Enable(HasSelection() && IsEditable()) ;
1027}
1028
1029void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1030{
1031 event.Enable(GetLastPosition() > 0);
1032}
1033
dcf924a3 1034#endif