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