]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
centralized the handling of border styles; added borders support for wxListBox and...
[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
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
aa61d352 350 const unsigned int count = GetCount();
53010e52 351
8228b893 352 if ( m_clientDataList.GetCount() < count )
0a164d4c 353 m_clientDataList.Append( (wxObject*) NULL );
8228b893 354 if ( 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
aa61d352 364int wxComboBox::DoInsert( const wxString &item, unsigned 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 370
8228b893 371 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") );
6f6f938f 372
aa61d352 373 if (pos == GetCount())
6f6f938f 374 return Append(item);
243dbf1a
VZ
375
376 DisableEvents();
377
378 GtkWidget *list = GTK_COMBO(m_widget)->list;
379
380 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
381
382 GList *gitem_list = g_list_alloc ();
383 gitem_list->data = list_item;
384 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
385
386 if (GTK_WIDGET_REALIZED(m_widget))
387 {
388 gtk_widget_realize( list_item );
389 gtk_widget_realize( GTK_BIN(list_item)->child );
390
f40fdaa3 391 ApplyWidgetStyle();
243dbf1a
VZ
392 }
393
394 gtk_widget_show( list_item );
395
aa61d352 396 const unsigned int count = GetCount();
243dbf1a 397
8228b893 398 if ( m_clientDataList.GetCount() < count )
0a164d4c 399 m_clientDataList.Insert( pos, (wxObject*) NULL );
8228b893 400 if ( m_clientObjectList.GetCount() < count )
0a164d4c 401 m_clientObjectList.Insert( pos, (wxObject*) NULL );
243dbf1a 402
6f6f938f 403 EnableEvents();
150e31d2 404
b0021947 405 InvalidateBestSize();
243dbf1a 406
6f6f938f 407 return pos;
243dbf1a
VZ
408}
409
aa61d352 410void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 411{
223d09f6 412 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 413
222ed1d6 414 wxList::compatibility_iterator node = m_clientDataList.Item( n );
fd0eed64 415 if (!node) return;
805dd538 416
f5e27805 417 node->SetData( (wxObject*) clientData );
6de97a3b 418}
53010e52 419
aa61d352 420void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 421{
223d09f6 422 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 423
222ed1d6 424 wxList::compatibility_iterator node = m_clientDataList.Item( n );
805dd538 425
30ed6e5c 426 return node ? node->GetData() : NULL;
fd0eed64
RR
427}
428
aa61d352 429void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
fd0eed64 430{
223d09f6 431 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 432
222ed1d6 433 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
fd0eed64 434 if (!node) return;
805dd538 435
e94e2e95 436 // wxItemContainer already deletes data for us
805dd538 437
fd0eed64 438 node->SetData( (wxObject*) clientData );
6de97a3b 439}
53010e52 440
aa61d352 441wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
53010e52 442{
223d09f6 443 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 444
222ed1d6 445 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
805dd538 446
30ed6e5c 447 return node ? (wxClientData*) node->GetData() : NULL;
fd0eed64
RR
448}
449
450void wxComboBox::Clear()
451{
223d09f6 452 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 453
461573cc 454 DisableEvents();
30ed6e5c 455
fd0eed64 456 GtkWidget *list = GTK_COMBO(m_widget)->list;
8228b893 457 gtk_list_clear_items( GTK_LIST(list), 0, (int)GetCount() );
805dd538 458
222ed1d6 459 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
460 while (node)
461 {
b1d4dd7a 462 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 463 if (cd) delete cd;
b1d4dd7a 464 node = node->GetNext();
fd0eed64 465 }
f5e27805 466 m_clientObjectList.Clear();
805dd538 467
fd0eed64 468 m_clientDataList.Clear();
30ed6e5c 469
461573cc 470 EnableEvents();
b0021947
VS
471
472 InvalidateBestSize();
6de97a3b 473}
53010e52 474
aa61d352 475void wxComboBox::Delete(unsigned int n)
53010e52 476{
223d09f6 477 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 478
fd0eed64 479 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 480
fd0eed64 481 GList *child = g_list_nth( listbox->children, n );
805dd538 482
fd0eed64
RR
483 if (!child)
484 {
223d09f6 485 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
486 return;
487 }
805dd538 488
461573cc 489 DisableEvents();
30ed6e5c 490
bbe0af5b 491 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
492 gtk_list_remove_items( listbox, list );
493 g_list_free( list );
805dd538 494
222ed1d6 495 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
f5e27805 496 if (node)
fd0eed64 497 {
b1d4dd7a 498 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 499 if (cd) delete cd;
222ed1d6 500 m_clientObjectList.Erase( node );
f5e27805 501 }
805dd538 502
b1d4dd7a 503 node = m_clientDataList.Item( n );
f5e27805 504 if (node)
222ed1d6 505 m_clientDataList.Erase( node );
150e31d2 506
461573cc 507 EnableEvents();
150e31d2 508
b0021947 509 InvalidateBestSize();
461573cc
RR
510}
511
aa61d352 512void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
513{
514 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
515
516 GtkWidget *list = GTK_COMBO(m_widget)->list;
517
518 GList *child = g_list_nth( GTK_LIST(list)->children, n );
519 if (child)
520 {
521 GtkBin *bin = GTK_BIN( child->data );
522 GtkLabel *label = GTK_LABEL( bin->child );
523 gtk_label_set_text(label, wxGTK_CONV(text));
524 }
525 else
526 {
527 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 528 }
150e31d2 529
b0021947 530 InvalidateBestSize();
6de97a3b 531}
53010e52 532
11e62fe6 533int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 534{
0a164d4c 535 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 536
fd0eed64 537 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 538
53010e52
RR
539 GList *child = GTK_LIST(list)->children;
540 int count = 0;
541 while (child)
542 {
fd0eed64
RR
543 GtkBin *bin = GTK_BIN( child->data );
544 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0 545 wxString str( label->label );
11e62fe6 546 if (item.IsSameAs( str , bCase ) )
7cf8cb48 547 return count;
30ed6e5c 548
fd0eed64
RR
549 count++;
550 child = child->next;
551 }
805dd538 552
7cf8cb48 553 return wxNOT_FOUND;
fd0eed64
RR
554}
555
556int wxComboBox::GetSelection() const
40eb3606
VZ
557{
558 // if the popup is currently opened, use the selection as it had been
559 // before it dropped down
560 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
561 : g_SelectionBeforePopup;
562}
563
564int wxComboBox::GetCurrentSelection() const
fd0eed64 565{
223d09f6 566 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 567
fd0eed64 568 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 569
fd0eed64
RR
570 GList *selection = GTK_LIST(list)->selection;
571 if (selection)
572 {
573 GList *child = GTK_LIST(list)->children;
574 int count = 0;
575 while (child)
576 {
577 if (child->data == selection->data) return count;
578 count++;
579 child = child->next;
580 }
6de97a3b 581 }
805dd538 582
8228b893 583 return wxNOT_FOUND;
6de97a3b 584}
53010e52 585
aa61d352 586wxString wxComboBox::GetString(unsigned int n) const
53010e52 587{
0a164d4c 588 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 589
fd0eed64 590 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 591
7cf8cb48 592 wxString str;
fd0eed64
RR
593 GList *child = g_list_nth( GTK_LIST(list)->children, n );
594 if (child)
595 {
596 GtkBin *bin = GTK_BIN( child->data );
597 GtkLabel *label = GTK_LABEL( bin->child );
2e1d7104 598 str = wxString( label->label );
7cf8cb48
VZ
599 }
600 else
601 {
223d09f6 602 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 603 }
805dd538 604
7cf8cb48 605 return str;
6de97a3b 606}
53010e52 607
fd0eed64 608wxString wxComboBox::GetStringSelection() const
53010e52 609{
0a164d4c 610 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 611
fd0eed64 612 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 613
fd0eed64
RR
614 GList *selection = GTK_LIST(list)->selection;
615 if (selection)
616 {
617 GtkBin *bin = GTK_BIN( selection->data );
2b5f62a0 618 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0 619 wxString tmp( label->label );
fd0eed64
RR
620 return tmp;
621 }
805dd538 622
223d09f6 623 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 624
0a164d4c 625 return wxEmptyString;
6de97a3b 626}
53010e52 627
aa61d352 628unsigned int wxComboBox::GetCount() const
53010e52 629{
223d09f6 630 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 631
fd0eed64 632 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 633
fd0eed64 634 GList *child = GTK_LIST(list)->children;
aa61d352 635 unsigned int count = 0;
fd0eed64
RR
636 while (child) { count++; child = child->next; }
637 return count;
6de97a3b 638}
53010e52 639
debe6624 640void wxComboBox::SetSelection( int n )
53010e52 641{
223d09f6 642 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 643
953704c1
RR
644 DisableEvents();
645
fd0eed64 646 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 647 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 648 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 649 m_prevSelection = n;
953704c1
RR
650
651 EnableEvents();
6de97a3b 652}
53010e52 653
fd0eed64 654wxString wxComboBox::GetValue() const
53010e52 655{
2e1d7104
RR
656 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
657 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
658
30ed6e5c 659#if 0
2e1d7104
RR
660 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
661 {
662 wxChar c = tmp[i];
663 printf( "%d ", (int) (c) );
664 }
665 printf( "\n" );
666#endif
30ed6e5c 667
fd0eed64 668 return tmp;
6de97a3b 669}
53010e52
RR
670
671void wxComboBox::SetValue( const wxString& value )
672{
223d09f6 673 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 674
fd0eed64 675 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
0a164d4c 676 wxString tmp;
fd0eed64 677 if (!value.IsNull()) tmp = value;
fab591c5 678 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
150e31d2 679
b0021947 680 InvalidateBestSize();
6de97a3b 681}
53010e52 682
fd0eed64 683void wxComboBox::Copy()
53010e52 684{
223d09f6 685 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 686
fd0eed64 687 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 688 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 689}
53010e52 690
fd0eed64 691void wxComboBox::Cut()
53010e52 692{
223d09f6 693 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 694
fd0eed64 695 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 696 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 697}
53010e52 698
fd0eed64 699void wxComboBox::Paste()
53010e52 700{
223d09f6 701 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 702
fd0eed64 703 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 704 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 705}
53010e52 706
150e31d2
JS
707void wxComboBox::Undo()
708{
709 // TODO
710}
711
712void wxComboBox::Redo()
713{
714 // TODO
715}
716
717void wxComboBox::SelectAll()
718{
4e324a3f 719 SetSelection(0, GetLastPosition());
150e31d2
JS
720}
721
722bool wxComboBox::CanUndo() const
723{
724 // TODO
725 return false;
726}
727
728bool wxComboBox::CanRedo() const
729{
730 // TODO
731 return false;
732}
733
734bool wxComboBox::HasSelection() const
735{
736 long from, to;
737 GetSelection(&from, &to);
738 return from != to;
739}
740
741bool wxComboBox::CanCopy() const
742{
743 // Can copy if there's a selection
744 return HasSelection();
745}
746
747bool wxComboBox::CanCut() const
748{
749 return CanCopy() && IsEditable();
750}
751
752bool wxComboBox::CanPaste() const
753{
754 // TODO: check for text on the clipboard
755 return IsEditable() ;
756}
757
758bool wxComboBox::IsEditable() const
759{
760 return !HasFlag(wxCB_READONLY);
761}
762
763
debe6624 764void wxComboBox::SetInsertionPoint( long pos )
53010e52 765{
223d09f6 766 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 767
6f6f938f
VZ
768 if ( pos == GetLastPosition() )
769 pos = -1;
770
fd0eed64 771 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 772 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 773}
53010e52 774
fd0eed64 775long wxComboBox::GetInsertionPoint() const
53010e52 776{
9e691f46 777 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 778}
53010e52 779
7d8268a1 780wxTextPos wxComboBox::GetLastPosition() const
53010e52 781{
fd0eed64
RR
782 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
783 int pos = GTK_ENTRY(entry)->text_length;
784 return (long) pos-1;
6de97a3b 785}
53010e52 786
debe6624 787void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 788{
223d09f6 789 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 790
fd0eed64
RR
791 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
792 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
793 if (value.IsNull()) return;
794 gint pos = (gint)to;
30ed6e5c 795
2e1d7104
RR
796#if wxUSE_UNICODE
797 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
798 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
799#else
8228b893 800 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos );
2e1d7104 801#endif
6de97a3b 802}
53010e52 803
20d10ee1 804void wxComboBox::SetSelection( long from, long to )
53010e52 805{
20d10ee1
VZ
806 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
807 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 808}
53010e52 809
150e31d2
JS
810void wxComboBox::GetSelection( long* from, long* to ) const
811{
812 if (IsEditable())
813 {
814 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
815 *from = (long) editable->selection_start_pos;
816 *to = (long) editable->selection_end_pos;
817 }
818}
819
20d10ee1 820void wxComboBox::SetEditable( bool editable )
53010e52 821{
20d10ee1
VZ
822 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
823 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
824}
825
8a85884a
VZ
826void wxComboBox::OnChar( wxKeyEvent &event )
827{
12a3f227 828 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 829 {
461573cc 830 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
831 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
832 eventEnter.SetString( GetValue() );
833 eventEnter.SetInt( GetSelection() );
834 eventEnter.SetEventObject( this );
3352cfff 835
17a1ebd1 836 if (!GetEventHandler()->ProcessEvent( eventEnter ))
3352cfff
RR
837 {
838 // This will invoke the dialog default action, such
839 // as the clicking the default button.
840
841 wxWindow *top_frame = m_parent;
842 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
843 top_frame = top_frame->GetParent();
844
845 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
846 {
847 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
848
849 if (window->default_widget)
150e31d2 850 gtk_widget_activate (window->default_widget);
3352cfff
RR
851 }
852 }
30ed6e5c 853
461573cc
RR
854 // Catch GTK event so that GTK doesn't open the drop
855 // down list upon RETURN.
0878fb4c 856 return;
8a85884a 857 }
30ed6e5c 858
7cf8cb48 859 event.Skip();
8a85884a
VZ
860}
861
953704c1
RR
862void wxComboBox::DisableEvents()
863{
461573cc
RR
864 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
865 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
866 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
867 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
868}
869
870void wxComboBox::EnableEvents()
871{
58b907f6 872 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
461573cc 873 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
58b907f6 874 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
461573cc 875 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
876}
877
b4071e91
RR
878void wxComboBox::OnSize( wxSizeEvent &event )
879{
260a67b7
VS
880 // NB: In some situations (e.g. on non-first page of a wizard, if the
881 // size used is default size), GtkCombo widget is resized correctly,
882 // but it's look is not updated, it's rendered as if it was much wider.
883 // No other widgets are affected, so it looks like a bug in GTK+.
884 // Manually requesting resize calculation (as gtk_pizza_set_size does)
885 // fixes it.
886 if (GTK_WIDGET_VISIBLE(m_widget))
887 gtk_widget_queue_resize(m_widget);
888
f03fc89f 889 event.Skip();
6de97a3b 890}
53010e52 891
f40fdaa3 892void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 893{
f40fdaa3 894// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 895
f40fdaa3
VS
896 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
897 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 898
fd0eed64
RR
899 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
900 GList *child = list->children;
901 while (child)
902 {
f40fdaa3 903 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 904
fd0eed64 905 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 906 gtk_widget_modify_style( bin->child, style );
805dd538 907
fd0eed64
RR
908 child = child->next;
909 }
868a2826 910}
b4071e91 911
fd0eed64 912GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 913{
fd0eed64 914 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
915}
916
b4071e91
RR
917bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
918{
fd0eed64
RR
919 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
920 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 921}
ac57418f 922
f68586e5
VZ
923wxSize wxComboBox::DoGetBestSize() const
924{
db434467 925 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
926
927 // we know better our horizontal extent: it depends on the longest string
928 // in the combobox
a6fc8ae3
VZ
929 if ( m_widget )
930 {
60d85ccb 931 int width;
aa61d352
VZ
932 unsigned int count = GetCount();
933 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 934 {
aa61d352 935 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
936 if ( width > ret.x )
937 ret.x = width;
938 }
939 }
940
941 // empty combobox should have some reasonable default size too
942 if ( ret.x < 100 )
943 ret.x = 100;
9f884528
RD
944
945 CacheBestSize(ret);
db434467 946 return ret;
f68586e5
VZ
947}
948
9d522606
RD
949// static
950wxVisualAttributes
951wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
952{
953 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
954}
955
150e31d2
JS
956// ----------------------------------------------------------------------------
957// standard event handling
958// ----------------------------------------------------------------------------
959
960void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
961{
962 Cut();
963}
964
965void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
966{
967 Copy();
968}
969
970void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
971{
972 Paste();
973}
974
975void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
976{
977 Undo();
978}
979
980void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
981{
982 Redo();
983}
984
985void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
986{
987 long from, to;
988 GetSelection(& from, & to);
989 if (from != -1 && to != -1)
990 Remove(from, to);
991}
992
993void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
994{
995 SetSelection(-1, -1);
996}
997
998void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
999{
1000 event.Enable( CanCut() );
1001}
1002
1003void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1004{
1005 event.Enable( CanCopy() );
1006}
1007
1008void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1009{
1010 event.Enable( CanPaste() );
1011}
1012
1013void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1014{
1015 event.Enable( CanUndo() );
1016}
1017
1018void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1019{
1020 event.Enable( CanRedo() );
1021}
1022
1023void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1024{
1025 event.Enable(HasSelection() && IsEditable()) ;
1026}
1027
1028void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1029{
1030 event.Enable(GetLastPosition() > 0);
1031}
1032
dcf924a3 1033#endif