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