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