]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
fixed SetUserScale() calls to use correct scale on Y axis
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
dcf924a3
RR
13#if wxUSE_COMBOBOX
14
8228b893
WS
15#include "wx/combobox.h"
16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
9eddec69 19 #include "wx/settings.h"
fec9cc08 20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
aaa6d89a 21 #include "wx/arrstr.h"
88a7a4e1
WS
22#endif
23
3cbab641 24#include "wx/gtk1/private.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;
40eb3606
VZ
38static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
39
78b3b018
RR
40//-----------------------------------------------------------------------------
41// "changed" - typing and list item matches get changed, select-child
42// if it doesn't match an item then just get a single changed
43//-----------------------------------------------------------------------------
44
865bb325 45extern "C" {
78b3b018
RR
46static void
47gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
48{
49 if (g_isIdle) wxapp_install_idle_handler();
50
51 if (combo->m_ignoreNextUpdate)
150e31d2 52 {
7d8268a1 53 combo->m_ignoreNextUpdate = false;
78b3b018
RR
54 return;
55 }
56
57 if (!combo->m_hasVMT) return;
58
59 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
60 event.SetString( combo->GetValue() );
61 event.SetEventObject( combo );
937013e0 62 combo->HandleWindowEvent( event );
78b3b018 63}
865bb325 64}
78b3b018 65
865bb325 66extern "C" {
78b3b018
RR
67static void
68gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
69{
70}
865bb325 71}
78b3b018 72
865bb325 73extern "C" {
9d6a9fdd
RR
74static void
75gtk_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
7d8268a1 76{
9d6a9fdd
RR
77 // when the popup is hidden, throw a SELECTED event only if the combobox
78 // selection changed.
3dbfe8f4
VZ
79 const int curSelection = combo->GetCurrentSelection();
80
81 const bool hasChanged = curSelection != g_SelectionBeforePopup;
82
83 // reset the selection flag to value meaning that it is hidden and do it
84 // now, before generating the events, so that GetSelection() returns the
85 // new value from the event handler
86 g_SelectionBeforePopup = wxID_NONE;
87
88 if ( hasChanged )
9d6a9fdd
RR
89 {
90 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
91 event.SetInt( curSelection );
92 event.SetString( combo->GetStringSelection() );
93 event.SetEventObject( combo );
937013e0 94 combo->HandleWindowEvent( event );
345bdf13
KH
95
96 // for consistency with the other ports, send TEXT event
97 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
98 event2.SetString( combo->GetStringSelection() );
99 event2.SetEventObject( combo );
937013e0 100 combo->HandleWindowEvent( event2 );
9d6a9fdd 101 }
9d6a9fdd 102}
865bb325 103}
9d6a9fdd 104
865bb325 105extern "C" {
9d6a9fdd
RR
106static void
107gtk_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
108{
109 // store the combobox selection value before the popup is shown
40eb3606 110 g_SelectionBeforePopup = combo->GetCurrentSelection();
9d6a9fdd 111}
865bb325 112}
9d6a9fdd 113
53010e52 114//-----------------------------------------------------------------------------
461573cc 115// "select-child" - click/cursor get select-child, changed, select-child
47908e25 116//-----------------------------------------------------------------------------
47908e25 117
865bb325 118extern "C" {
8a85884a 119static void
461573cc 120gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 121{
acfd422a 122 if (g_isIdle) wxapp_install_idle_handler();
8a85884a 123
a2053b27 124 if (!combo->m_hasVMT) return;
30ed6e5c 125
acfd422a 126 if (g_blockEventsOnDrag) return;
805dd538 127
40eb3606 128 int curSelection = combo->GetCurrentSelection();
30ed6e5c 129
3c4e4af6
RR
130 if (combo->m_prevSelection == curSelection) return;
131
132 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
133 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
150e31d2 134
159b66c0
RR
135 combo->m_prevSelection = curSelection;
136
78b3b018
RR
137 // Quickly set the value of the combo box
138 // as GTK+ does that only AFTER the event
139 // is sent.
140 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry),
141 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
142 combo->SetValue( combo->GetStringSelection() );
58b907f6 143 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
78b3b018
RR
144 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
145
40eb3606 146 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
9d6a9fdd
RR
147 // because when combobox popup is shown, gtk_combo_select_child_callback is
148 // called each times the mouse is over an item with a pressed button so a lot
149 // of SELECTED event could be generated if the user keep the mouse button down
150 // and select other items ...
40eb3606 151 if (g_SelectionBeforePopup == wxID_NONE)
9d6a9fdd
RR
152 {
153 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
154 event.SetInt( curSelection );
155 event.SetString( combo->GetStringSelection() );
156 event.SetEventObject( combo );
937013e0 157 combo->HandleWindowEvent( event );
0c77152e 158
345bdf13 159 // for consistency with the other ports, don't generate text update
40eb3606
VZ
160 // events while the user is browsing the combobox neither
161 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
162 event2.SetString( combo->GetValue() );
163 event2.SetEventObject( combo );
937013e0 164 combo->HandleWindowEvent( event2 );
40eb3606 165 }
461573cc 166}
865bb325 167}
461573cc 168
e1e955e1
RR
169//-----------------------------------------------------------------------------
170// wxComboBox
53010e52
RR
171//-----------------------------------------------------------------------------
172
173IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
174
b4071e91 175BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 176 EVT_SIZE(wxComboBox::OnSize)
8a85884a 177 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
178
179 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
180 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
181 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
182 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
183 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
184 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
185 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
186
187 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
188 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
189 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
190 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
191 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
192 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
193 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
194END_EVENT_TABLE()
195
584ad2a3
MB
196bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
197 const wxString& value,
198 const wxPoint& pos, const wxSize& size,
199 const wxArrayString& choices,
200 long style, const wxValidator& validator,
201 const wxString& name )
202{
203 wxCArrayString chs(choices);
204
205 return Create( parent, id, value, pos, size, chs.GetCount(),
206 chs.GetStrings(), style, validator, name );
207}
208
fd0eed64
RR
209bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
210 const wxPoint& pos, const wxSize& size,
211 int n, const wxString choices[],
805dd538
VZ
212 long style, const wxValidator& validator,
213 const wxString& name )
53010e52 214{
7d8268a1
WS
215 m_ignoreNextUpdate = false;
216 m_needParent = true;
217 m_acceptsFocus = true;
159b66c0 218 m_prevSelection = 0;
805dd538 219
db434467 220 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
221 !CreateBase( parent, id, pos, size, style, validator, name ))
222 {
223d09f6 223 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 224 return false;
4dcaf11a 225 }
6de97a3b 226
fd0eed64 227 m_widget = gtk_combo_new();
461573cc 228 GtkCombo *combo = GTK_COMBO(m_widget);
30ed6e5c 229
461573cc
RR
230 // Disable GTK's broken events ...
231 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
90e572f1 232 // ... and add surrogate handler.
461573cc 233 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
7d8268a1 234 (GtkSignalFunc) gtk_dummy_callback, combo);
805dd538 235
8a85884a 236 // make it more useable
3ca6a5f0 237 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
30ed6e5c 238
3ca6a5f0
BP
239 // and case-sensitive
240 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
241
fd0eed64 242 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 243
81a0614b 244 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
159b66c0 245
fd0eed64
RR
246 for (int i = 0; i < n; i++)
247 {
fab591c5 248 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
805dd538 249
fd0eed64 250 m_clientDataList.Append( (wxObject*)NULL );
f5e27805 251 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 252
fd0eed64 253 gtk_container_add( GTK_CONTAINER(list), list_item );
805dd538 254
19da4326 255 gtk_widget_show( list_item );
fd0eed64 256 }
805dd538 257
f03fc89f 258 m_parent->DoAddChild( this );
30ed6e5c 259
461573cc 260 m_focusWidget = combo->entry;
805dd538 261
abdeb9e7 262 PostCreation(size);
53010e52 263
461573cc 264 ConnectWidget( combo->button );
805dd538 265
461573cc
RR
266 // MSW's combo box shows the value and the selection is -1
267 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
268 gtk_list_unselect_all( GTK_LIST(combo->list) );
805dd538 269
a260fe6a 270 if (style & wxCB_READONLY)
461573cc 271 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
a260fe6a 272
9d6a9fdd
RR
273 // "show" and "hide" events are generated when user click on the combobox button which popups a list
274 // this list is the "popwin" gtk widget
275 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
7d8268a1 276 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
9d6a9fdd 277 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
7d8268a1 278 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
9d6a9fdd 279
58b907f6 280 gtk_signal_connect_after( GTK_OBJECT(combo->entry), "changed",
461573cc
RR
281 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
282
58b907f6 283 gtk_signal_connect_after( GTK_OBJECT(combo->list), "select-child",
461573cc 284 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
805dd538 285
170acdc9 286 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 287
abdeb9e7 288 // This is required for tool bar support
024e9a4c
RR
289// wxSize setsize = GetSize();
290// gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
150e31d2 291
7d8268a1 292 return true;
fd0eed64
RR
293}
294
295wxComboBox::~wxComboBox()
296{
222ed1d6 297 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
298 while (node)
299 {
b1d4dd7a 300 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 301 if (cd) delete cd;
b1d4dd7a 302 node = node->GetNext();
fd0eed64 303 }
7d6d2cd4
RR
304 m_clientObjectList.Clear();
305
fd0eed64 306 m_clientDataList.Clear();
6de97a3b 307}
53010e52 308
2b5f62a0
VZ
309void wxComboBox::SetFocus()
310{
311 if ( m_hasFocus )
312 {
313 // don't do anything if we already have focus
314 return;
315 }
316
317 gtk_widget_grab_focus( m_focusWidget );
318}
319
a236aa20
VZ
320int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
321 unsigned int pos,
322 void **clientData,
323 wxClientDataType type)
53010e52 324{
2a68b7a0 325 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 326
461573cc 327 DisableEvents();
30ed6e5c 328
fd0eed64 329 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 330
631a05be 331 GtkRcStyle *style = CreateWidgetStyle();
243dbf1a 332
a236aa20
VZ
333 const unsigned int count = items.GetCount();
334 for( unsigned int i = 0; i < count; ++i, ++pos )
243dbf1a 335 {
a236aa20
VZ
336 GtkWidget *
337 list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
338
339 if ( pos == GetCount() )
340 {
341 gtk_container_add( GTK_CONTAINER(list), list_item );
342 }
343 else // insert, not append
344 {
345 GList *gitem_list = g_list_alloc ();
346 gitem_list->data = list_item;
347 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
348 }
349
350 if (GTK_WIDGET_REALIZED(m_widget))
351 {
352 gtk_widget_realize( list_item );
353 gtk_widget_realize( GTK_BIN(list_item)->child );
354
355 if (style)
356 {
357 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
358 GtkBin *bin = GTK_BIN( list_item );
359 GtkWidget *label = GTK_WIDGET( bin->child );
360 gtk_widget_modify_style( label, style );
361 }
362
363 }
243dbf1a 364
a236aa20 365 gtk_widget_show( list_item );
243dbf1a 366
a236aa20
VZ
367 if ( m_clientDataList.GetCount() < GetCount() )
368 m_clientDataList.Insert( pos, (wxObject*) NULL );
369 if ( m_clientObjectList.GetCount() < GetCount() )
370 m_clientObjectList.Insert( pos, (wxObject*) NULL );
243dbf1a 371
a236aa20
VZ
372 AssignNewItemClientData(pos, clientData, i, type);
373 }
243dbf1a 374
a236aa20
VZ
375 if ( style )
376 gtk_rc_style_unref( style );
243dbf1a 377
6f6f938f 378 EnableEvents();
150e31d2 379
b0021947 380 InvalidateBestSize();
243dbf1a 381
a236aa20 382 return pos - 1;
243dbf1a
VZ
383}
384
aa61d352 385void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 386{
223d09f6 387 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 388
222ed1d6 389 wxList::compatibility_iterator node = m_clientDataList.Item( n );
fd0eed64 390 if (!node) return;
805dd538 391
f5e27805 392 node->SetData( (wxObject*) clientData );
6de97a3b 393}
53010e52 394
aa61d352 395void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 396{
223d09f6 397 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 398
222ed1d6 399 wxList::compatibility_iterator node = m_clientDataList.Item( n );
805dd538 400
30ed6e5c 401 return node ? node->GetData() : NULL;
fd0eed64
RR
402}
403
a236aa20 404void wxComboBox::DoClear()
fd0eed64 405{
223d09f6 406 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 407
461573cc 408 DisableEvents();
30ed6e5c 409
fd0eed64 410 GtkWidget *list = GTK_COMBO(m_widget)->list;
8228b893 411 gtk_list_clear_items( GTK_LIST(list), 0, (int)GetCount() );
805dd538 412
f5e27805 413 m_clientObjectList.Clear();
805dd538 414
fd0eed64 415 m_clientDataList.Clear();
30ed6e5c 416
461573cc 417 EnableEvents();
b0021947
VS
418
419 InvalidateBestSize();
6de97a3b 420}
53010e52 421
a236aa20 422void wxComboBox::DoDeleteOneItem(unsigned int n)
53010e52 423{
223d09f6 424 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 425
fd0eed64 426 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 427
fd0eed64 428 GList *child = g_list_nth( listbox->children, n );
805dd538 429
fd0eed64
RR
430 if (!child)
431 {
223d09f6 432 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
433 return;
434 }
805dd538 435
461573cc 436 DisableEvents();
30ed6e5c 437
bbe0af5b 438 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
439 gtk_list_remove_items( listbox, list );
440 g_list_free( list );
805dd538 441
222ed1d6 442 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
f5e27805 443 if (node)
fd0eed64 444 {
222ed1d6 445 m_clientObjectList.Erase( node );
f5e27805 446 }
805dd538 447
b1d4dd7a 448 node = m_clientDataList.Item( n );
f5e27805 449 if (node)
222ed1d6 450 m_clientDataList.Erase( node );
150e31d2 451
461573cc 452 EnableEvents();
150e31d2 453
b0021947 454 InvalidateBestSize();
461573cc
RR
455}
456
aa61d352 457void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
458{
459 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
460
461 GtkWidget *list = GTK_COMBO(m_widget)->list;
462
463 GList *child = g_list_nth( GTK_LIST(list)->children, n );
464 if (child)
465 {
466 GtkBin *bin = GTK_BIN( child->data );
467 GtkLabel *label = GTK_LABEL( bin->child );
468 gtk_label_set_text(label, wxGTK_CONV(text));
469 }
470 else
471 {
472 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 473 }
150e31d2 474
b0021947 475 InvalidateBestSize();
6de97a3b 476}
53010e52 477
11e62fe6 478int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 479{
0a164d4c 480 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 481
fd0eed64 482 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 483
53010e52
RR
484 GList *child = GTK_LIST(list)->children;
485 int count = 0;
486 while (child)
487 {
fd0eed64
RR
488 GtkBin *bin = GTK_BIN( child->data );
489 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0 490 wxString str( label->label );
11e62fe6 491 if (item.IsSameAs( str , bCase ) )
7cf8cb48 492 return count;
30ed6e5c 493
fd0eed64
RR
494 count++;
495 child = child->next;
496 }
805dd538 497
7cf8cb48 498 return wxNOT_FOUND;
fd0eed64
RR
499}
500
501int wxComboBox::GetSelection() const
40eb3606
VZ
502{
503 // if the popup is currently opened, use the selection as it had been
504 // before it dropped down
505 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
506 : g_SelectionBeforePopup;
507}
508
509int wxComboBox::GetCurrentSelection() const
fd0eed64 510{
223d09f6 511 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 512
fd0eed64 513 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 514
fd0eed64
RR
515 GList *selection = GTK_LIST(list)->selection;
516 if (selection)
517 {
518 GList *child = GTK_LIST(list)->children;
519 int count = 0;
520 while (child)
521 {
522 if (child->data == selection->data) return count;
523 count++;
524 child = child->next;
525 }
6de97a3b 526 }
805dd538 527
8228b893 528 return wxNOT_FOUND;
6de97a3b 529}
53010e52 530
aa61d352 531wxString wxComboBox::GetString(unsigned int n) const
53010e52 532{
0a164d4c 533 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 534
fd0eed64 535 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 536
7cf8cb48 537 wxString str;
fd0eed64
RR
538 GList *child = g_list_nth( GTK_LIST(list)->children, n );
539 if (child)
540 {
541 GtkBin *bin = GTK_BIN( child->data );
542 GtkLabel *label = GTK_LABEL( bin->child );
2e1d7104 543 str = wxString( label->label );
7cf8cb48
VZ
544 }
545 else
546 {
223d09f6 547 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 548 }
805dd538 549
7cf8cb48 550 return str;
6de97a3b 551}
53010e52 552
fd0eed64 553wxString wxComboBox::GetStringSelection() const
53010e52 554{
0a164d4c 555 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 556
fd0eed64 557 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 558
fd0eed64
RR
559 GList *selection = GTK_LIST(list)->selection;
560 if (selection)
561 {
562 GtkBin *bin = GTK_BIN( selection->data );
2b5f62a0 563 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0 564 wxString tmp( label->label );
fd0eed64
RR
565 return tmp;
566 }
805dd538 567
223d09f6 568 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 569
0a164d4c 570 return wxEmptyString;
6de97a3b 571}
53010e52 572
aa61d352 573unsigned int wxComboBox::GetCount() const
53010e52 574{
223d09f6 575 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 576
fd0eed64 577 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 578
fd0eed64 579 GList *child = GTK_LIST(list)->children;
aa61d352 580 unsigned int count = 0;
fd0eed64
RR
581 while (child) { count++; child = child->next; }
582 return count;
6de97a3b 583}
53010e52 584
debe6624 585void wxComboBox::SetSelection( int n )
53010e52 586{
223d09f6 587 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 588
953704c1
RR
589 DisableEvents();
590
fd0eed64 591 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 592 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 593 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 594 m_prevSelection = n;
953704c1
RR
595
596 EnableEvents();
6de97a3b 597}
53010e52 598
fd0eed64 599wxString wxComboBox::GetValue() const
53010e52 600{
2e1d7104
RR
601 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
602 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
603
30ed6e5c 604#if 0
2e1d7104
RR
605 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
606 {
607 wxChar c = tmp[i];
608 printf( "%d ", (int) (c) );
609 }
610 printf( "\n" );
611#endif
30ed6e5c 612
fd0eed64 613 return tmp;
6de97a3b 614}
53010e52
RR
615
616void wxComboBox::SetValue( const wxString& value )
617{
223d09f6 618 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 619
fd0eed64 620 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
c282ec44 621 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( value ) );
150e31d2 622
b0021947 623 InvalidateBestSize();
6de97a3b 624}
53010e52 625
c282ec44
VZ
626void wxComboBox::WriteText(const wxString& value)
627{
628 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
629
630 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
631 GtkEditable * const edit = GTK_EDITABLE(entry);
632
633 gtk_editable_delete_selection(edit);
634 gint len = gtk_editable_get_position(edit);
635 gtk_editable_insert_text(edit, wxGTK_CONV(value), -1, &len);
636 gtk_editable_set_position(edit, len);
637}
638
fd0eed64 639void wxComboBox::Copy()
53010e52 640{
223d09f6 641 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 642
fd0eed64 643 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 644 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 645}
53010e52 646
fd0eed64 647void wxComboBox::Cut()
53010e52 648{
223d09f6 649 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 650
fd0eed64 651 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 652 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 653}
53010e52 654
fd0eed64 655void wxComboBox::Paste()
53010e52 656{
223d09f6 657 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 658
fd0eed64 659 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 660 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 661}
53010e52 662
150e31d2
JS
663void wxComboBox::Undo()
664{
665 // TODO
666}
667
668void wxComboBox::Redo()
669{
670 // TODO
671}
672
673void wxComboBox::SelectAll()
674{
4e324a3f 675 SetSelection(0, GetLastPosition());
150e31d2
JS
676}
677
678bool wxComboBox::CanUndo() const
679{
680 // TODO
681 return false;
682}
683
684bool wxComboBox::CanRedo() const
685{
686 // TODO
687 return false;
688}
689
690bool wxComboBox::HasSelection() const
691{
692 long from, to;
693 GetSelection(&from, &to);
694 return from != to;
695}
696
697bool wxComboBox::CanCopy() const
698{
699 // Can copy if there's a selection
700 return HasSelection();
701}
702
703bool wxComboBox::CanCut() const
704{
705 return CanCopy() && IsEditable();
706}
707
708bool wxComboBox::CanPaste() const
709{
710 // TODO: check for text on the clipboard
711 return IsEditable() ;
712}
713
714bool wxComboBox::IsEditable() const
715{
716 return !HasFlag(wxCB_READONLY);
717}
718
719
debe6624 720void wxComboBox::SetInsertionPoint( long pos )
53010e52 721{
223d09f6 722 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 723
6f6f938f
VZ
724 if ( pos == GetLastPosition() )
725 pos = -1;
726
fd0eed64 727 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 728 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 729}
53010e52 730
fd0eed64 731long wxComboBox::GetInsertionPoint() const
53010e52 732{
9e691f46 733 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 734}
53010e52 735
7d8268a1 736wxTextPos wxComboBox::GetLastPosition() const
53010e52 737{
fd0eed64
RR
738 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
739 int pos = GTK_ENTRY(entry)->text_length;
740 return (long) pos-1;
6de97a3b 741}
53010e52 742
debe6624 743void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 744{
223d09f6 745 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 746
fd0eed64
RR
747 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
748 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
749 if (value.IsNull()) return;
750 gint pos = (gint)to;
30ed6e5c 751
2e1d7104
RR
752#if wxUSE_UNICODE
753 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
754 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
755#else
8228b893 756 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos );
2e1d7104 757#endif
6de97a3b 758}
53010e52 759
20d10ee1 760void wxComboBox::SetSelection( long from, long to )
53010e52 761{
20d10ee1
VZ
762 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
763 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 764}
53010e52 765
150e31d2
JS
766void wxComboBox::GetSelection( long* from, long* to ) const
767{
768 if (IsEditable())
769 {
770 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
771 *from = (long) editable->selection_start_pos;
772 *to = (long) editable->selection_end_pos;
773 }
774}
775
20d10ee1 776void wxComboBox::SetEditable( bool editable )
53010e52 777{
20d10ee1
VZ
778 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
779 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
780}
781
8a85884a
VZ
782void wxComboBox::OnChar( wxKeyEvent &event )
783{
12a3f227 784 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 785 {
461573cc 786 // GTK automatically selects an item if its in the list
17a1ebd1
VZ
787 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
788 eventEnter.SetString( GetValue() );
789 eventEnter.SetInt( GetSelection() );
790 eventEnter.SetEventObject( this );
3352cfff 791
937013e0 792 if (!HandleWindowEvent( eventEnter ))
3352cfff
RR
793 {
794 // This will invoke the dialog default action, such
795 // as the clicking the default button.
796
797 wxWindow *top_frame = m_parent;
798 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
799 top_frame = top_frame->GetParent();
800
801 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
802 {
803 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
804
805 if (window->default_widget)
150e31d2 806 gtk_widget_activate (window->default_widget);
3352cfff
RR
807 }
808 }
30ed6e5c 809
461573cc
RR
810 // Catch GTK event so that GTK doesn't open the drop
811 // down list upon RETURN.
0878fb4c 812 return;
8a85884a 813 }
30ed6e5c 814
7cf8cb48 815 event.Skip();
8a85884a
VZ
816}
817
953704c1
RR
818void wxComboBox::DisableEvents()
819{
461573cc
RR
820 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
821 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
822 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
823 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
824}
825
826void wxComboBox::EnableEvents()
827{
58b907f6 828 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
461573cc 829 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
58b907f6 830 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
461573cc 831 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
832}
833
b4071e91
RR
834void wxComboBox::OnSize( wxSizeEvent &event )
835{
260a67b7
VS
836 // NB: In some situations (e.g. on non-first page of a wizard, if the
837 // size used is default size), GtkCombo widget is resized correctly,
838 // but it's look is not updated, it's rendered as if it was much wider.
839 // No other widgets are affected, so it looks like a bug in GTK+.
840 // Manually requesting resize calculation (as gtk_pizza_set_size does)
841 // fixes it.
842 if (GTK_WIDGET_VISIBLE(m_widget))
843 gtk_widget_queue_resize(m_widget);
844
f03fc89f 845 event.Skip();
6de97a3b 846}
53010e52 847
f40fdaa3 848void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 849{
f40fdaa3 850// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 851
f40fdaa3
VS
852 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
853 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 854
fd0eed64
RR
855 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
856 GList *child = list->children;
857 while (child)
858 {
f40fdaa3 859 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 860
fd0eed64 861 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 862 gtk_widget_modify_style( bin->child, style );
805dd538 863
fd0eed64
RR
864 child = child->next;
865 }
868a2826 866}
b4071e91 867
fd0eed64 868GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 869{
fd0eed64 870 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
871}
872
b4071e91
RR
873bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
874{
fd0eed64
RR
875 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
876 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 877}
ac57418f 878
f68586e5
VZ
879wxSize wxComboBox::DoGetBestSize() const
880{
db434467 881 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
882
883 // we know better our horizontal extent: it depends on the longest string
884 // in the combobox
a6fc8ae3
VZ
885 if ( m_widget )
886 {
60d85ccb 887 int width;
aa61d352
VZ
888 unsigned int count = GetCount();
889 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 890 {
aa61d352 891 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
892 if ( width > ret.x )
893 ret.x = width;
894 }
895 }
896
897 // empty combobox should have some reasonable default size too
898 if ( ret.x < 100 )
899 ret.x = 100;
9f884528
RD
900
901 CacheBestSize(ret);
db434467 902 return ret;
f68586e5
VZ
903}
904
9d522606
RD
905// static
906wxVisualAttributes
907wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
908{
909 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
910}
911
150e31d2
JS
912// ----------------------------------------------------------------------------
913// standard event handling
914// ----------------------------------------------------------------------------
915
916void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
917{
918 Cut();
919}
920
921void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
922{
923 Copy();
924}
925
926void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
927{
928 Paste();
929}
930
931void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
932{
933 Undo();
934}
935
936void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
937{
938 Redo();
939}
940
941void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
942{
943 long from, to;
944 GetSelection(& from, & to);
945 if (from != -1 && to != -1)
946 Remove(from, to);
947}
948
949void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
950{
951 SetSelection(-1, -1);
952}
953
954void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
955{
956 event.Enable( CanCut() );
957}
958
959void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
960{
961 event.Enable( CanCopy() );
962}
963
964void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
965{
966 event.Enable( CanPaste() );
967}
968
969void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
970{
971 event.Enable( CanUndo() );
972}
973
974void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
975{
976 event.Enable( CanRedo() );
977}
978
979void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
980{
981 event.Enable(HasSelection() && IsEditable()) ;
982}
983
984void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
985{
986 event.Enable(GetLastPosition() > 0);
987}
988
dcf924a3 989#endif