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