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