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