]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
Fixed the drawing of the HRules so they don't try to iterate over all
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
805dd538 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "combobox.h"
12#endif
13
14#include "wx/combobox.h"
dcf924a3
RR
15
16#if wxUSE_COMBOBOX
17
72a16063 18#include "wx/settings.h"
b62c3631 19#include "wx/intl.h"
53010e52 20
78bcfcfc
VZ
21#include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
071a2d78
RR
23#include <gdk/gdk.h>
24#include <gtk/gtk.h>
83624f79 25
acfd422a
RR
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
47908e25
RR
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37extern bool g_blockEventsOnDrag;
38
53010e52 39//-----------------------------------------------------------------------------
e1e955e1 40// "select"
47908e25 41//-----------------------------------------------------------------------------
47908e25 42
8a85884a
VZ
43static void
44gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 45{
acfd422a 46 if (g_isIdle) wxapp_install_idle_handler();
8a85884a 47
a2053b27 48 if (!combo->m_hasVMT) return;
acfd422a
RR
49
50 if (g_blockEventsOnDrag) return;
805dd538 51
fd0eed64
RR
52 if (combo->m_alreadySent)
53 {
54 combo->m_alreadySent = FALSE;
55 return;
56 }
47908e25 57
fd0eed64 58 combo->m_alreadySent = TRUE;
805dd538 59
159b66c0
RR
60 int curSelection = combo->GetSelection();
61
62 if (combo->m_prevSelection != curSelection)
63 {
64 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
65 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
66 }
67
68 combo->m_prevSelection = curSelection;
69
8a85884a 70 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
159b66c0 71 event.SetInt( curSelection );
29006414 72 event.SetString( combo->GetStringSelection() );
8a85884a 73 event.SetEventObject( combo );
31528cd3 74
8a85884a 75 combo->GetEventHandler()->ProcessEvent( event );
6de97a3b 76}
47908e25 77
0c77152e
RR
78//-----------------------------------------------------------------------------
79// "changed"
80//-----------------------------------------------------------------------------
81
eeccd5d9 82static void
0c77152e
RR
83gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
84{
acfd422a 85 if (g_isIdle) wxapp_install_idle_handler();
31528cd3 86
a2053b27
RR
87 if (!combo->m_hasVMT) return;
88
8a85884a 89 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
29006414 90 event.SetString( combo->GetValue() );
0c77152e
RR
91 event.SetEventObject( combo );
92 combo->GetEventHandler()->ProcessEvent( event );
0c77152e
RR
93}
94
e1e955e1
RR
95//-----------------------------------------------------------------------------
96// wxComboBox
53010e52
RR
97//-----------------------------------------------------------------------------
98
99IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
100
b4071e91 101BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 102 EVT_SIZE(wxComboBox::OnSize)
8a85884a 103 EVT_CHAR(wxComboBox::OnChar)
b4071e91
RR
104END_EVENT_TABLE()
105
fd0eed64
RR
106bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
107 const wxPoint& pos, const wxSize& size,
108 int n, const wxString choices[],
805dd538
VZ
109 long style, const wxValidator& validator,
110 const wxString& name )
53010e52 111{
fd0eed64
RR
112 m_alreadySent = FALSE;
113 m_needParent = TRUE;
b292e2f5 114 m_acceptsFocus = TRUE;
159b66c0 115 m_prevSelection = 0;
805dd538 116
db434467 117 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
118 !CreateBase( parent, id, pos, size, style, validator, name ))
119 {
223d09f6 120 wxFAIL_MSG( wxT("wxComboBox creation failed") );
9d9b7755 121 return FALSE;
4dcaf11a 122 }
6de97a3b 123
fd0eed64 124 m_widget = gtk_combo_new();
805dd538 125
8a85884a 126 // make it more useable
3ca6a5f0
BP
127 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
128
129 // and case-sensitive
130 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
131
8a85884a 132
fd0eed64 133 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 134
159b66c0
RR
135 gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
136
fd0eed64
RR
137 for (int i = 0; i < n; i++)
138 {
19da4326 139 /* don't send first event, which GTK sends aways when
9d9b7755 140 inserting the first item */
19da4326 141 m_alreadySent = TRUE;
31528cd3 142
93c5dd39 143 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i].mbc_str() );
805dd538 144
fd0eed64 145 m_clientDataList.Append( (wxObject*)NULL );
f5e27805 146 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 147
fd0eed64 148 gtk_container_add( GTK_CONTAINER(list), list_item );
805dd538 149
805dd538 150 gtk_signal_connect( GTK_OBJECT(list_item), "select",
19da4326
RR
151 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
152
153 gtk_widget_show( list_item );
fd0eed64 154 }
805dd538 155
f03fc89f 156 m_parent->DoAddChild( this );
805dd538 157
fd0eed64 158 PostCreation();
53010e52 159
fd0eed64 160 ConnectWidget( GTK_COMBO(m_widget)->button );
805dd538 161
fd0eed64 162 if (!value.IsNull()) SetValue( value );
805dd538 163
a260fe6a
RR
164 if (style & wxCB_READONLY)
165 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE );
166
0c77152e
RR
167 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
168 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
805dd538 169
db434467
RR
170 wxSize size_best( DoGetBestSize() );
171 wxSize new_size( size );
172 if (new_size.x == -1)
173 new_size.x = size_best.x;
174 if (new_size.y == -1)
175 new_size.y = size_best.y;
176 if (new_size.y > size_best.y)
177 new_size.y = size_best.y;
178 if ((new_size.x != size.x) || (new_size.y != size.y))
179 SetSize( new_size.x, new_size.y );
180
72a16063 181 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
fd0eed64 182 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 183
fd0eed64 184 Show( TRUE );
805dd538 185
fd0eed64
RR
186 return TRUE;
187}
188
189wxComboBox::~wxComboBox()
190{
7d6d2cd4 191 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
192 while (node)
193 {
194 wxClientData *cd = (wxClientData*)node->Data();
195 if (cd) delete cd;
196 node = node->Next();
197 }
7d6d2cd4
RR
198 m_clientObjectList.Clear();
199
fd0eed64 200 m_clientDataList.Clear();
6de97a3b 201}
53010e52 202
fd0eed64 203void wxComboBox::AppendCommon( const wxString &item )
53010e52 204{
223d09f6 205 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 206
fd0eed64 207 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 208
93c5dd39 209 GtkWidget *list_item = gtk_list_item_new_with_label( item.mbc_str() );
805dd538 210
ec5d85fb
RR
211 gtk_container_add( GTK_CONTAINER(list), list_item );
212
805dd538 213 gtk_signal_connect( GTK_OBJECT(list_item), "select",
fd0eed64 214 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
805dd538 215
2b07d713
RR
216 if (GTK_WIDGET_REALIZED(m_widget))
217 {
218 gtk_widget_realize( list_item );
219 gtk_widget_realize( GTK_BIN(list_item)->child );
220
221 if (m_widgetStyle) ApplyWidgetStyle();
222 }
805dd538 223
fd0eed64 224 gtk_widget_show( list_item );
6de97a3b 225}
53010e52
RR
226
227void wxComboBox::Append( const wxString &item )
47908e25 228{
f5e27805
RR
229 m_clientDataList.Append( (wxObject*) NULL );
230 m_clientObjectList.Append( (wxObject*) NULL );
805dd538 231
fd0eed64 232 AppendCommon( item );
6de97a3b 233}
47908e25 234
fd0eed64 235void wxComboBox::Append( const wxString &item, void *clientData )
53010e52 236{
f5e27805
RR
237 m_clientDataList.Append( (wxObject*) clientData );
238 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 239
fd0eed64 240 AppendCommon( item );
6de97a3b 241}
53010e52 242
fd0eed64 243void wxComboBox::Append( const wxString &item, wxClientData *clientData )
53010e52 244{
f5e27805
RR
245 m_clientDataList.Append( (wxObject*) NULL );
246 m_clientObjectList.Append( (wxObject*) clientData );
805dd538 247
fd0eed64
RR
248 AppendCommon( item );
249}
250
251void wxComboBox::SetClientData( int n, void* clientData )
252{
223d09f6 253 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 254
fd0eed64
RR
255 wxNode *node = m_clientDataList.Nth( n );
256 if (!node) return;
805dd538 257
f5e27805 258 node->SetData( (wxObject*) clientData );
6de97a3b 259}
53010e52 260
fd0eed64 261void* wxComboBox::GetClientData( int n )
53010e52 262{
223d09f6 263 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 264
fd0eed64
RR
265 wxNode *node = m_clientDataList.Nth( n );
266 if (!node) return NULL;
805dd538 267
f5e27805 268 return node->Data();
fd0eed64
RR
269}
270
271void wxComboBox::SetClientObject( int n, wxClientData* clientData )
272{
223d09f6 273 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 274
f5e27805 275 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 276 if (!node) return;
805dd538 277
fd0eed64
RR
278 wxClientData *cd = (wxClientData*) node->Data();
279 if (cd) delete cd;
805dd538 280
fd0eed64 281 node->SetData( (wxObject*) clientData );
6de97a3b 282}
53010e52 283
fd0eed64 284wxClientData* wxComboBox::GetClientObject( int n )
53010e52 285{
223d09f6 286 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 287
fd0eed64
RR
288 wxNode *node = m_clientDataList.Nth( n );
289 if (!node) return (wxClientData*) NULL;
805dd538 290
fd0eed64
RR
291 return (wxClientData*) node->Data();
292}
293
294void wxComboBox::Clear()
295{
223d09f6 296 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 297
fd0eed64
RR
298 GtkWidget *list = GTK_COMBO(m_widget)->list;
299 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
805dd538 300
f5e27805 301 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
302 while (node)
303 {
304 wxClientData *cd = (wxClientData*)node->Data();
305 if (cd) delete cd;
306 node = node->Next();
307 }
f5e27805 308 m_clientObjectList.Clear();
805dd538 309
fd0eed64 310 m_clientDataList.Clear();
6de97a3b 311}
53010e52 312
fd0eed64 313void wxComboBox::Delete( int n )
53010e52 314{
223d09f6 315 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 316
fd0eed64 317 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 318
fd0eed64 319 GList *child = g_list_nth( listbox->children, n );
805dd538 320
fd0eed64
RR
321 if (!child)
322 {
223d09f6 323 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
324 return;
325 }
805dd538 326
bbe0af5b 327 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
328 gtk_list_remove_items( listbox, list );
329 g_list_free( list );
805dd538 330
f5e27805
RR
331 wxNode *node = m_clientObjectList.Nth( n );
332 if (node)
fd0eed64
RR
333 {
334 wxClientData *cd = (wxClientData*)node->Data();
335 if (cd) delete cd;
f5e27805
RR
336 m_clientObjectList.DeleteNode( node );
337 }
805dd538 338
f5e27805
RR
339 node = m_clientDataList.Nth( n );
340 if (node)
341 {
fd0eed64
RR
342 m_clientDataList.DeleteNode( node );
343 }
6de97a3b 344}
53010e52 345
fd0eed64 346int wxComboBox::FindString( const wxString &item )
53010e52 347{
223d09f6 348 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 349
fd0eed64 350 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 351
53010e52
RR
352 GList *child = GTK_LIST(list)->children;
353 int count = 0;
354 while (child)
355 {
fd0eed64
RR
356 GtkBin *bin = GTK_BIN( child->data );
357 GtkLabel *label = GTK_LABEL( bin->child );
53b88b54 358 if (item == wxString(label->label,*wxConvCurrent))
7cf8cb48 359 return count;
fd0eed64
RR
360 count++;
361 child = child->next;
362 }
805dd538 363
7cf8cb48 364 return wxNOT_FOUND;
fd0eed64
RR
365}
366
367int wxComboBox::GetSelection() const
368{
223d09f6 369 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 370
fd0eed64 371 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 372
fd0eed64
RR
373 GList *selection = GTK_LIST(list)->selection;
374 if (selection)
375 {
376 GList *child = GTK_LIST(list)->children;
377 int count = 0;
378 while (child)
379 {
380 if (child->data == selection->data) return count;
381 count++;
382 child = child->next;
383 }
6de97a3b 384 }
805dd538 385
fd0eed64 386 return -1;
6de97a3b 387}
53010e52 388
debe6624 389wxString wxComboBox::GetString( int n ) const
53010e52 390{
223d09f6 391 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 392
fd0eed64 393 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 394
7cf8cb48 395 wxString str;
fd0eed64
RR
396 GList *child = g_list_nth( GTK_LIST(list)->children, n );
397 if (child)
398 {
399 GtkBin *bin = GTK_BIN( child->data );
400 GtkLabel *label = GTK_LABEL( bin->child );
53b88b54 401 str = wxString(label->label,*wxConvCurrent);
7cf8cb48
VZ
402 }
403 else
404 {
223d09f6 405 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 406 }
805dd538 407
7cf8cb48 408 return str;
6de97a3b 409}
53010e52 410
fd0eed64 411wxString wxComboBox::GetStringSelection() const
53010e52 412{
223d09f6 413 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 414
fd0eed64 415 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 416
fd0eed64
RR
417 GList *selection = GTK_LIST(list)->selection;
418 if (selection)
419 {
420 GtkBin *bin = GTK_BIN( selection->data );
53b88b54 421 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
fd0eed64
RR
422 return tmp;
423 }
805dd538 424
223d09f6 425 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 426
223d09f6 427 return wxT("");
6de97a3b 428}
53010e52 429
fd0eed64 430int wxComboBox::Number() const
53010e52 431{
223d09f6 432 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 433
fd0eed64 434 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 435
fd0eed64
RR
436 GList *child = GTK_LIST(list)->children;
437 int count = 0;
438 while (child) { count++; child = child->next; }
439 return count;
6de97a3b 440}
53010e52 441
debe6624 442void wxComboBox::SetSelection( int n )
53010e52 443{
223d09f6 444 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 445
953704c1
RR
446 DisableEvents();
447
fd0eed64 448 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 449 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 450 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 451 m_prevSelection = n;
953704c1
RR
452
453 EnableEvents();
6de97a3b 454}
53010e52 455
47908e25
RR
456void wxComboBox::SetStringSelection( const wxString &string )
457{
223d09f6 458 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 459
fd0eed64
RR
460 int res = FindString( string );
461 if (res == -1) return;
462 SetSelection( res );
6de97a3b 463}
47908e25 464
fd0eed64 465wxString wxComboBox::GetValue() const
53010e52 466{
fd0eed64 467 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
53b88b54 468 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
fd0eed64 469 return tmp;
6de97a3b 470}
53010e52
RR
471
472void wxComboBox::SetValue( const wxString& value )
473{
223d09f6 474 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 475
fd0eed64 476 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
223d09f6 477 wxString tmp = wxT("");
fd0eed64 478 if (!value.IsNull()) tmp = value;
93c5dd39 479 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
6de97a3b 480}
53010e52 481
fd0eed64 482void wxComboBox::Copy()
53010e52 483{
223d09f6 484 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 485
fd0eed64 486 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 487#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 488 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
75ed1d15 489#else
fd0eed64 490 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 491#endif
6de97a3b 492}
53010e52 493
fd0eed64 494void wxComboBox::Cut()
53010e52 495{
223d09f6 496 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 497
fd0eed64 498 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 499#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 500 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
75ed1d15 501#else
fd0eed64 502 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 503#endif
6de97a3b 504}
53010e52 505
fd0eed64 506void wxComboBox::Paste()
53010e52 507{
223d09f6 508 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 509
fd0eed64 510 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 511#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 512 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
75ed1d15 513#else
fd0eed64 514 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 515#endif
6de97a3b 516}
53010e52 517
debe6624 518void wxComboBox::SetInsertionPoint( long pos )
53010e52 519{
223d09f6 520 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 521
fd0eed64 522 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 523 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 524}
53010e52 525
fd0eed64 526void wxComboBox::SetInsertionPointEnd()
53010e52 527{
223d09f6 528 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538
VZ
529
530 SetInsertionPoint( -1 );
6de97a3b 531}
53010e52 532
fd0eed64 533long wxComboBox::GetInsertionPoint() const
53010e52 534{
fd0eed64
RR
535 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
536 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 537}
53010e52 538
fd0eed64 539long wxComboBox::GetLastPosition() const
53010e52 540{
fd0eed64
RR
541 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
542 int pos = GTK_ENTRY(entry)->text_length;
543 return (long) pos-1;
6de97a3b 544}
53010e52 545
debe6624 546void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 547{
223d09f6 548 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
93c5dd39 549 // FIXME: not quite sure how to do this method right in multibyte mode
805dd538 550
fd0eed64
RR
551 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
552 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
553 if (value.IsNull()) return;
554 gint pos = (gint)to;
93c5dd39 555 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
6de97a3b 556}
53010e52 557
debe6624 558void wxComboBox::Remove(long from, long to)
53010e52 559{
223d09f6 560 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 561
fd0eed64
RR
562 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
563 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 564}
53010e52 565
20d10ee1 566void wxComboBox::SetSelection( long from, long to )
53010e52 567{
20d10ee1
VZ
568 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
569 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 570}
53010e52 571
20d10ee1 572void wxComboBox::SetEditable( bool editable )
53010e52 573{
20d10ee1
VZ
574 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
575 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
576}
577
8a85884a
VZ
578void wxComboBox::OnChar( wxKeyEvent &event )
579{
7cf8cb48 580 if ( event.KeyCode() == WXK_RETURN )
8a85884a 581 {
7cf8cb48 582 wxString value = GetValue();
8a85884a 583
7cf8cb48
VZ
584 if ( Number() == 0 )
585 {
586 // make Enter generate "selected" event if there is only one item
587 // in the combobox - without it, it's impossible to select it at
588 // all!
589 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
590 event.SetInt( 0 );
29006414 591 event.SetString( value );
7cf8cb48
VZ
592 event.SetEventObject( this );
593 GetEventHandler()->ProcessEvent( event );
594 }
595 else
596 {
597 // add the item to the list if it's not there yet
598 if ( FindString(value) == wxNOT_FOUND )
599 {
600 Append(value);
f6bcfd97 601 SetStringSelection(value);
7cf8cb48
VZ
602
603 // and generate the selected event for it
604 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
605 event.SetInt( Number() - 1 );
29006414 606 event.SetString( value );
7cf8cb48
VZ
607 event.SetEventObject( this );
608 GetEventHandler()->ProcessEvent( event );
609 }
610 //else: do nothing, this will open the listbox
611 }
8a85884a 612 }
7cf8cb48
VZ
613
614 event.Skip();
8a85884a
VZ
615}
616
953704c1
RR
617void wxComboBox::DisableEvents()
618{
619 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
620 GList *child = list->children;
621 while (child)
622 {
31528cd3 623 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
953704c1
RR
624 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
625
626 child = child->next;
627 }
628}
629
630void wxComboBox::EnableEvents()
631{
632 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
633 GList *child = list->children;
634 while (child)
635 {
636 gtk_signal_connect( GTK_OBJECT(child->data), "select",
637 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
638
639 child = child->next;
640 }
641}
642
b4071e91
RR
643void wxComboBox::OnSize( wxSizeEvent &event )
644{
f03fc89f 645 event.Skip();
31528cd3 646
b02da6b1 647#if 0
fd0eed64
RR
648 int w = 21;
649 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
805dd538 650
fd0eed64
RR
651 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
652 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
b02da6b1 653#endif // 0
6de97a3b 654}
53010e52 655
58614078 656void wxComboBox::ApplyWidgetStyle()
868a2826 657{
fd0eed64 658 SetWidgetStyle();
805dd538 659
72a16063 660// gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
fd0eed64
RR
661 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
662 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
805dd538 663
fd0eed64
RR
664 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
665 GList *child = list->children;
666 while (child)
667 {
668 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
805dd538 669
fd0eed64
RR
670 GtkBin *bin = GTK_BIN(child->data);
671 gtk_widget_set_style( bin->child, m_widgetStyle );
805dd538 672
fd0eed64
RR
673 child = child->next;
674 }
868a2826 675}
b4071e91 676
fd0eed64 677GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 678{
fd0eed64 679 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
680}
681
b4071e91
RR
682bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
683{
fd0eed64
RR
684 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
685 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 686}
ac57418f 687
f68586e5
VZ
688wxSize wxComboBox::DoGetBestSize() const
689{
db434467 690 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
691
692 // we know better our horizontal extent: it depends on the longest string
693 // in the combobox
694 ret.x = 0;
695 if ( m_widget )
696 {
697 GdkFont *font = m_font.GetInternalFont();
698
699 wxCoord width;
700 size_t count = Number();
701 for ( size_t n = 0; n < count; n++ )
702 {
703 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
704 if ( width > ret.x )
705 ret.x = width;
706 }
707 }
708
709 // empty combobox should have some reasonable default size too
710 if ( ret.x < 100 )
711 ret.x = 100;
db434467 712 return ret;
f68586e5
VZ
713}
714
dcf924a3 715#endif