]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
make --static flag act on --cppflags too.
[wxWidgets.git] / src / gtk / 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 );
76fcf0f2
RR
157
158 m_focusWidget = GTK_COMBO(m_widget)->entry;
805dd538 159
fd0eed64 160 PostCreation();
53010e52 161
fd0eed64 162 ConnectWidget( GTK_COMBO(m_widget)->button );
805dd538 163
fd0eed64 164 if (!value.IsNull()) SetValue( value );
805dd538 165
a260fe6a
RR
166 if (style & wxCB_READONLY)
167 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE );
168
0c77152e
RR
169 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
170 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
805dd538 171
db434467
RR
172 wxSize size_best( DoGetBestSize() );
173 wxSize new_size( size );
174 if (new_size.x == -1)
175 new_size.x = size_best.x;
176 if (new_size.y == -1)
177 new_size.y = size_best.y;
178 if (new_size.y > size_best.y)
179 new_size.y = size_best.y;
180 if ((new_size.x != size.x) || (new_size.y != size.y))
181 SetSize( new_size.x, new_size.y );
182
72a16063 183 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
fd0eed64 184 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 185
fd0eed64 186 Show( TRUE );
805dd538 187
fd0eed64
RR
188 return TRUE;
189}
190
191wxComboBox::~wxComboBox()
192{
7d6d2cd4 193 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
194 while (node)
195 {
196 wxClientData *cd = (wxClientData*)node->Data();
197 if (cd) delete cd;
198 node = node->Next();
199 }
7d6d2cd4
RR
200 m_clientObjectList.Clear();
201
fd0eed64 202 m_clientDataList.Clear();
6de97a3b 203}
53010e52 204
fd0eed64 205void wxComboBox::AppendCommon( const wxString &item )
53010e52 206{
223d09f6 207 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 208
fd0eed64 209 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 210
93c5dd39 211 GtkWidget *list_item = gtk_list_item_new_with_label( item.mbc_str() );
805dd538 212
ec5d85fb
RR
213 gtk_container_add( GTK_CONTAINER(list), list_item );
214
805dd538 215 gtk_signal_connect( GTK_OBJECT(list_item), "select",
fd0eed64 216 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
805dd538 217
2b07d713
RR
218 if (GTK_WIDGET_REALIZED(m_widget))
219 {
220 gtk_widget_realize( list_item );
221 gtk_widget_realize( GTK_BIN(list_item)->child );
222
223 if (m_widgetStyle) ApplyWidgetStyle();
224 }
805dd538 225
fd0eed64 226 gtk_widget_show( list_item );
6de97a3b 227}
53010e52
RR
228
229void wxComboBox::Append( const wxString &item )
47908e25 230{
f5e27805
RR
231 m_clientDataList.Append( (wxObject*) NULL );
232 m_clientObjectList.Append( (wxObject*) NULL );
805dd538 233
fd0eed64 234 AppendCommon( item );
6de97a3b 235}
47908e25 236
fd0eed64 237void wxComboBox::Append( const wxString &item, void *clientData )
53010e52 238{
f5e27805
RR
239 m_clientDataList.Append( (wxObject*) clientData );
240 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 241
fd0eed64 242 AppendCommon( item );
6de97a3b 243}
53010e52 244
fd0eed64 245void wxComboBox::Append( const wxString &item, wxClientData *clientData )
53010e52 246{
f5e27805
RR
247 m_clientDataList.Append( (wxObject*) NULL );
248 m_clientObjectList.Append( (wxObject*) clientData );
805dd538 249
fd0eed64
RR
250 AppendCommon( item );
251}
252
253void wxComboBox::SetClientData( int n, void* clientData )
254{
223d09f6 255 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 256
fd0eed64
RR
257 wxNode *node = m_clientDataList.Nth( n );
258 if (!node) return;
805dd538 259
f5e27805 260 node->SetData( (wxObject*) clientData );
6de97a3b 261}
53010e52 262
fd0eed64 263void* wxComboBox::GetClientData( int n )
53010e52 264{
223d09f6 265 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 266
fd0eed64
RR
267 wxNode *node = m_clientDataList.Nth( n );
268 if (!node) return NULL;
805dd538 269
f5e27805 270 return node->Data();
fd0eed64
RR
271}
272
273void wxComboBox::SetClientObject( int n, wxClientData* clientData )
274{
223d09f6 275 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 276
f5e27805 277 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 278 if (!node) return;
805dd538 279
fd0eed64
RR
280 wxClientData *cd = (wxClientData*) node->Data();
281 if (cd) delete cd;
805dd538 282
fd0eed64 283 node->SetData( (wxObject*) clientData );
6de97a3b 284}
53010e52 285
fd0eed64 286wxClientData* wxComboBox::GetClientObject( int n )
53010e52 287{
223d09f6 288 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 289
fd0eed64
RR
290 wxNode *node = m_clientDataList.Nth( n );
291 if (!node) return (wxClientData*) NULL;
805dd538 292
fd0eed64
RR
293 return (wxClientData*) node->Data();
294}
295
296void wxComboBox::Clear()
297{
223d09f6 298 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 299
fd0eed64
RR
300 GtkWidget *list = GTK_COMBO(m_widget)->list;
301 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
805dd538 302
f5e27805 303 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
304 while (node)
305 {
306 wxClientData *cd = (wxClientData*)node->Data();
307 if (cd) delete cd;
308 node = node->Next();
309 }
f5e27805 310 m_clientObjectList.Clear();
805dd538 311
fd0eed64 312 m_clientDataList.Clear();
6de97a3b 313}
53010e52 314
fd0eed64 315void wxComboBox::Delete( int n )
53010e52 316{
223d09f6 317 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 318
fd0eed64 319 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 320
fd0eed64 321 GList *child = g_list_nth( listbox->children, n );
805dd538 322
fd0eed64
RR
323 if (!child)
324 {
223d09f6 325 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
326 return;
327 }
805dd538 328
bbe0af5b 329 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
330 gtk_list_remove_items( listbox, list );
331 g_list_free( list );
805dd538 332
f5e27805
RR
333 wxNode *node = m_clientObjectList.Nth( n );
334 if (node)
fd0eed64
RR
335 {
336 wxClientData *cd = (wxClientData*)node->Data();
337 if (cd) delete cd;
f5e27805
RR
338 m_clientObjectList.DeleteNode( node );
339 }
805dd538 340
f5e27805
RR
341 node = m_clientDataList.Nth( n );
342 if (node)
343 {
fd0eed64
RR
344 m_clientDataList.DeleteNode( node );
345 }
6de97a3b 346}
53010e52 347
fd0eed64 348int wxComboBox::FindString( const wxString &item )
53010e52 349{
223d09f6 350 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 351
fd0eed64 352 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 353
53010e52
RR
354 GList *child = GTK_LIST(list)->children;
355 int count = 0;
356 while (child)
357 {
fd0eed64
RR
358 GtkBin *bin = GTK_BIN( child->data );
359 GtkLabel *label = GTK_LABEL( bin->child );
53b88b54 360 if (item == wxString(label->label,*wxConvCurrent))
7cf8cb48 361 return count;
fd0eed64
RR
362 count++;
363 child = child->next;
364 }
805dd538 365
7cf8cb48 366 return wxNOT_FOUND;
fd0eed64
RR
367}
368
369int wxComboBox::GetSelection() const
370{
223d09f6 371 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 372
fd0eed64 373 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 374
fd0eed64
RR
375 GList *selection = GTK_LIST(list)->selection;
376 if (selection)
377 {
378 GList *child = GTK_LIST(list)->children;
379 int count = 0;
380 while (child)
381 {
382 if (child->data == selection->data) return count;
383 count++;
384 child = child->next;
385 }
6de97a3b 386 }
805dd538 387
fd0eed64 388 return -1;
6de97a3b 389}
53010e52 390
debe6624 391wxString wxComboBox::GetString( int n ) const
53010e52 392{
223d09f6 393 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 394
fd0eed64 395 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 396
7cf8cb48 397 wxString str;
fd0eed64
RR
398 GList *child = g_list_nth( GTK_LIST(list)->children, n );
399 if (child)
400 {
401 GtkBin *bin = GTK_BIN( child->data );
402 GtkLabel *label = GTK_LABEL( bin->child );
53b88b54 403 str = wxString(label->label,*wxConvCurrent);
7cf8cb48
VZ
404 }
405 else
406 {
223d09f6 407 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 408 }
805dd538 409
7cf8cb48 410 return str;
6de97a3b 411}
53010e52 412
fd0eed64 413wxString wxComboBox::GetStringSelection() const
53010e52 414{
223d09f6 415 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 416
fd0eed64 417 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 418
fd0eed64
RR
419 GList *selection = GTK_LIST(list)->selection;
420 if (selection)
421 {
422 GtkBin *bin = GTK_BIN( selection->data );
53b88b54 423 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
fd0eed64
RR
424 return tmp;
425 }
805dd538 426
223d09f6 427 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 428
223d09f6 429 return wxT("");
6de97a3b 430}
53010e52 431
fd0eed64 432int wxComboBox::Number() const
53010e52 433{
223d09f6 434 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 435
fd0eed64 436 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 437
fd0eed64
RR
438 GList *child = GTK_LIST(list)->children;
439 int count = 0;
440 while (child) { count++; child = child->next; }
441 return count;
6de97a3b 442}
53010e52 443
debe6624 444void wxComboBox::SetSelection( int n )
53010e52 445{
223d09f6 446 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 447
953704c1
RR
448 DisableEvents();
449
fd0eed64 450 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 451 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 452 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 453 m_prevSelection = n;
953704c1
RR
454
455 EnableEvents();
6de97a3b 456}
53010e52 457
47908e25
RR
458void wxComboBox::SetStringSelection( const wxString &string )
459{
223d09f6 460 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 461
fd0eed64
RR
462 int res = FindString( string );
463 if (res == -1) return;
464 SetSelection( res );
6de97a3b 465}
47908e25 466
fd0eed64 467wxString wxComboBox::GetValue() const
53010e52 468{
fd0eed64 469 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
53b88b54 470 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
fd0eed64 471 return tmp;
6de97a3b 472}
53010e52
RR
473
474void wxComboBox::SetValue( const wxString& value )
475{
223d09f6 476 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 477
fd0eed64 478 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
223d09f6 479 wxString tmp = wxT("");
fd0eed64 480 if (!value.IsNull()) tmp = value;
93c5dd39 481 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
6de97a3b 482}
53010e52 483
fd0eed64 484void wxComboBox::Copy()
53010e52 485{
223d09f6 486 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 487
fd0eed64 488 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 489#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 490 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
75ed1d15 491#else
fd0eed64 492 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 493#endif
6de97a3b 494}
53010e52 495
fd0eed64 496void wxComboBox::Cut()
53010e52 497{
223d09f6 498 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 499
fd0eed64 500 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 501#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 502 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
75ed1d15 503#else
fd0eed64 504 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 505#endif
6de97a3b 506}
53010e52 507
fd0eed64 508void wxComboBox::Paste()
53010e52 509{
223d09f6 510 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 511
fd0eed64 512 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
13111b2a 513#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
fd0eed64 514 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
75ed1d15 515#else
fd0eed64 516 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 517#endif
6de97a3b 518}
53010e52 519
debe6624 520void wxComboBox::SetInsertionPoint( long pos )
53010e52 521{
223d09f6 522 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 523
fd0eed64 524 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 525 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 526}
53010e52 527
fd0eed64 528void wxComboBox::SetInsertionPointEnd()
53010e52 529{
223d09f6 530 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538
VZ
531
532 SetInsertionPoint( -1 );
6de97a3b 533}
53010e52 534
fd0eed64 535long wxComboBox::GetInsertionPoint() const
53010e52 536{
fd0eed64
RR
537 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
538 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 539}
53010e52 540
fd0eed64 541long wxComboBox::GetLastPosition() const
53010e52 542{
fd0eed64
RR
543 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
544 int pos = GTK_ENTRY(entry)->text_length;
545 return (long) pos-1;
6de97a3b 546}
53010e52 547
debe6624 548void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 549{
223d09f6 550 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
93c5dd39 551 // FIXME: not quite sure how to do this method right in multibyte mode
805dd538 552
fd0eed64
RR
553 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
554 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
555 if (value.IsNull()) return;
556 gint pos = (gint)to;
93c5dd39 557 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
6de97a3b 558}
53010e52 559
debe6624 560void wxComboBox::Remove(long from, long to)
53010e52 561{
223d09f6 562 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 563
fd0eed64
RR
564 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
565 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 566}
53010e52 567
20d10ee1 568void wxComboBox::SetSelection( long from, long to )
53010e52 569{
20d10ee1
VZ
570 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
571 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 572}
53010e52 573
20d10ee1 574void wxComboBox::SetEditable( bool editable )
53010e52 575{
20d10ee1
VZ
576 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
577 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
578}
579
8a85884a
VZ
580void wxComboBox::OnChar( wxKeyEvent &event )
581{
7cf8cb48 582 if ( event.KeyCode() == WXK_RETURN )
8a85884a 583 {
7cf8cb48 584 wxString value = GetValue();
8a85884a 585
7cf8cb48
VZ
586 if ( Number() == 0 )
587 {
588 // make Enter generate "selected" event if there is only one item
589 // in the combobox - without it, it's impossible to select it at
590 // all!
591 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
592 event.SetInt( 0 );
29006414 593 event.SetString( value );
7cf8cb48
VZ
594 event.SetEventObject( this );
595 GetEventHandler()->ProcessEvent( event );
596 }
597 else
598 {
599 // add the item to the list if it's not there yet
600 if ( FindString(value) == wxNOT_FOUND )
601 {
602 Append(value);
f6bcfd97 603 SetStringSelection(value);
7cf8cb48
VZ
604
605 // and generate the selected event for it
606 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
607 event.SetInt( Number() - 1 );
29006414 608 event.SetString( value );
7cf8cb48
VZ
609 event.SetEventObject( this );
610 GetEventHandler()->ProcessEvent( event );
611 }
612 //else: do nothing, this will open the listbox
613 }
8a85884a 614 }
7cf8cb48
VZ
615
616 event.Skip();
8a85884a
VZ
617}
618
953704c1
RR
619void wxComboBox::DisableEvents()
620{
621 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
622 GList *child = list->children;
623 while (child)
624 {
31528cd3 625 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
953704c1
RR
626 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
627
628 child = child->next;
629 }
630}
631
632void wxComboBox::EnableEvents()
633{
634 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
635 GList *child = list->children;
636 while (child)
637 {
638 gtk_signal_connect( GTK_OBJECT(child->data), "select",
639 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
640
641 child = child->next;
642 }
643}
644
b4071e91
RR
645void wxComboBox::OnSize( wxSizeEvent &event )
646{
f03fc89f 647 event.Skip();
31528cd3 648
b02da6b1 649#if 0
fd0eed64
RR
650 int w = 21;
651 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
805dd538 652
fd0eed64
RR
653 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
654 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
b02da6b1 655#endif // 0
6de97a3b 656}
53010e52 657
58614078 658void wxComboBox::ApplyWidgetStyle()
868a2826 659{
fd0eed64 660 SetWidgetStyle();
805dd538 661
72a16063 662// gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
fd0eed64
RR
663 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
664 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
805dd538 665
fd0eed64
RR
666 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
667 GList *child = list->children;
668 while (child)
669 {
670 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
805dd538 671
fd0eed64
RR
672 GtkBin *bin = GTK_BIN(child->data);
673 gtk_widget_set_style( bin->child, m_widgetStyle );
805dd538 674
fd0eed64
RR
675 child = child->next;
676 }
868a2826 677}
b4071e91 678
fd0eed64 679GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 680{
fd0eed64 681 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
682}
683
b4071e91
RR
684bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
685{
fd0eed64
RR
686 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
687 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 688}
ac57418f 689
f68586e5
VZ
690wxSize wxComboBox::DoGetBestSize() const
691{
db434467 692 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
693
694 // we know better our horizontal extent: it depends on the longest string
695 // in the combobox
696 ret.x = 0;
697 if ( m_widget )
698 {
699 GdkFont *font = m_font.GetInternalFont();
700
701 wxCoord width;
702 size_t count = Number();
703 for ( size_t n = 0; n < count; n++ )
704 {
705 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
706 if ( width > ret.x )
707 ret.x = width;
708 }
709 }
710
711 // empty combobox should have some reasonable default size too
712 if ( ret.x < 100 )
713 ret.x = 100;
db434467 714 return ret;
f68586e5
VZ
715}
716
dcf924a3 717#endif