]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
compilation fixes; pruned API
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
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 {
78b3b018
RR
54 combo->m_ignoreNextUpdate = FALSE;
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)
73{
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 }
85
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() );
125 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
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{
78b3b018 195 m_ignoreNextUpdate = FALSE;
fd0eed64 196 m_needParent = TRUE;
b292e2f5 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") );
9d9b7755 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",
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
02a6e358 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",
263 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
264 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
265 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
266
461573cc
RR
267 gtk_signal_connect( GTK_OBJECT(combo->entry), "changed",
268 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
269
270 gtk_signal_connect( GTK_OBJECT(combo->list), "select-child",
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
RD
275 // This is required for tool bar support
276 wxSize setsize = GetSize();
277 gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
150e31d2 278
fd0eed64
RR
279 return TRUE;
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
95561ddf 647bool wxComboBox::SetStringSelection( const wxString &string )
47908e25 648{
95561ddf 649 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") );
805dd538 650
fd0eed64 651 int res = FindString( string );
95561ddf 652 if (res == -1) return false;
fd0eed64 653 SetSelection( res );
95561ddf 654 return true;
6de97a3b 655}
47908e25 656
fd0eed64 657wxString wxComboBox::GetValue() const
53010e52 658{
2e1d7104
RR
659 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
660 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
661
30ed6e5c 662#if 0
2e1d7104
RR
663 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
664 {
665 wxChar c = tmp[i];
666 printf( "%d ", (int) (c) );
667 }
668 printf( "\n" );
669#endif
30ed6e5c 670
fd0eed64 671 return tmp;
6de97a3b 672}
53010e52
RR
673
674void wxComboBox::SetValue( const wxString& value )
675{
223d09f6 676 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 677
fd0eed64 678 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
223d09f6 679 wxString tmp = wxT("");
fd0eed64 680 if (!value.IsNull()) tmp = value;
fab591c5 681 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
150e31d2 682
b0021947 683 InvalidateBestSize();
6de97a3b 684}
53010e52 685
fd0eed64 686void wxComboBox::Copy()
53010e52 687{
223d09f6 688 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 689
fd0eed64 690 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 691 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 692}
53010e52 693
fd0eed64 694void wxComboBox::Cut()
53010e52 695{
223d09f6 696 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 697
fd0eed64 698 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 699 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 700}
53010e52 701
fd0eed64 702void wxComboBox::Paste()
53010e52 703{
223d09f6 704 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 705
fd0eed64 706 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 707 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 708}
53010e52 709
150e31d2
JS
710void wxComboBox::Undo()
711{
712 // TODO
713}
714
715void wxComboBox::Redo()
716{
717 // TODO
718}
719
720void wxComboBox::SelectAll()
721{
4e324a3f 722 SetSelection(0, GetLastPosition());
150e31d2
JS
723}
724
725bool wxComboBox::CanUndo() const
726{
727 // TODO
728 return false;
729}
730
731bool wxComboBox::CanRedo() const
732{
733 // TODO
734 return false;
735}
736
737bool wxComboBox::HasSelection() const
738{
739 long from, to;
740 GetSelection(&from, &to);
741 return from != to;
742}
743
744bool wxComboBox::CanCopy() const
745{
746 // Can copy if there's a selection
747 return HasSelection();
748}
749
750bool wxComboBox::CanCut() const
751{
752 return CanCopy() && IsEditable();
753}
754
755bool wxComboBox::CanPaste() const
756{
757 // TODO: check for text on the clipboard
758 return IsEditable() ;
759}
760
761bool wxComboBox::IsEditable() const
762{
763 return !HasFlag(wxCB_READONLY);
764}
765
766
debe6624 767void wxComboBox::SetInsertionPoint( long pos )
53010e52 768{
223d09f6 769 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 770
6f6f938f
VZ
771 if ( pos == GetLastPosition() )
772 pos = -1;
773
fd0eed64 774 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 775 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 776}
53010e52 777
fd0eed64 778long wxComboBox::GetInsertionPoint() const
53010e52 779{
9e691f46 780 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 781}
53010e52 782
fd0eed64 783long wxComboBox::GetLastPosition() const
53010e52 784{
fd0eed64
RR
785 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
786 int pos = GTK_ENTRY(entry)->text_length;
787 return (long) pos-1;
6de97a3b 788}
53010e52 789
debe6624 790void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 791{
223d09f6 792 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 793
fd0eed64
RR
794 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
795 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
796 if (value.IsNull()) return;
797 gint pos = (gint)to;
30ed6e5c 798
2e1d7104
RR
799#if wxUSE_UNICODE
800 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
801 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
802#else
803 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
804#endif
6de97a3b 805}
53010e52 806
20d10ee1 807void wxComboBox::SetSelection( long from, long to )
53010e52 808{
20d10ee1
VZ
809 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
810 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 811}
53010e52 812
150e31d2
JS
813void wxComboBox::GetSelection( long* from, long* to ) const
814{
815 if (IsEditable())
816 {
817 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
41eb6d76 818#ifdef __WXGTK20__
4e324a3f
JS
819 gint start, end;
820 gtk_editable_get_selection_bounds(editable, & start, & end);
821 *from = start;
822 *to = end;
823#else
150e31d2
JS
824 *from = (long) editable->selection_start_pos;
825 *to = (long) editable->selection_end_pos;
4e324a3f 826#endif
150e31d2
JS
827 }
828}
829
20d10ee1 830void wxComboBox::SetEditable( bool editable )
53010e52 831{
20d10ee1
VZ
832 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
833 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
834}
835
8a85884a
VZ
836void wxComboBox::OnChar( wxKeyEvent &event )
837{
12a3f227 838 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 839 {
461573cc
RR
840 // GTK automatically selects an item if its in the list
841 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
842 event.SetString( GetValue() );
843 event.SetInt( GetSelection() );
844 event.SetEventObject( this );
3352cfff
RR
845
846 if (!GetEventHandler()->ProcessEvent( event ))
847 {
848 // This will invoke the dialog default action, such
849 // as the clicking the default button.
850
851 wxWindow *top_frame = m_parent;
852 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
853 top_frame = top_frame->GetParent();
854
855 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
856 {
857 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
858
859 if (window->default_widget)
150e31d2 860 gtk_widget_activate (window->default_widget);
3352cfff
RR
861 }
862 }
30ed6e5c 863
461573cc
RR
864 // Catch GTK event so that GTK doesn't open the drop
865 // down list upon RETURN.
0878fb4c 866 return;
8a85884a 867 }
30ed6e5c 868
7cf8cb48 869 event.Skip();
8a85884a
VZ
870}
871
953704c1
RR
872void wxComboBox::DisableEvents()
873{
461573cc
RR
874 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
875 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
876 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
877 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
878}
879
880void wxComboBox::EnableEvents()
881{
461573cc
RR
882 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
883 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
884 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
885 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
886}
887
b4071e91
RR
888void wxComboBox::OnSize( wxSizeEvent &event )
889{
260a67b7
VS
890 // NB: In some situations (e.g. on non-first page of a wizard, if the
891 // size used is default size), GtkCombo widget is resized correctly,
892 // but it's look is not updated, it's rendered as if it was much wider.
893 // No other widgets are affected, so it looks like a bug in GTK+.
894 // Manually requesting resize calculation (as gtk_pizza_set_size does)
895 // fixes it.
896 if (GTK_WIDGET_VISIBLE(m_widget))
897 gtk_widget_queue_resize(m_widget);
898
f03fc89f 899 event.Skip();
6de97a3b 900}
53010e52 901
f40fdaa3 902void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 903{
f40fdaa3 904// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 905
f40fdaa3
VS
906 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
907 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 908
fd0eed64
RR
909 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
910 GList *child = list->children;
911 while (child)
912 {
f40fdaa3 913 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 914
fd0eed64 915 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 916 gtk_widget_modify_style( bin->child, style );
805dd538 917
fd0eed64
RR
918 child = child->next;
919 }
868a2826 920}
b4071e91 921
fd0eed64 922GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 923{
fd0eed64 924 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
925}
926
b4071e91
RR
927bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
928{
fd0eed64
RR
929 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
930 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 931}
ac57418f 932
f68586e5
VZ
933wxSize wxComboBox::DoGetBestSize() const
934{
db434467 935 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
936
937 // we know better our horizontal extent: it depends on the longest string
938 // in the combobox
a6fc8ae3
VZ
939 if ( m_widget )
940 {
60d85ccb
RR
941 int width;
942 size_t count = GetCount();
a6fc8ae3
VZ
943 for ( size_t n = 0; n < count; n++ )
944 {
2b1ff57f 945 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
946 if ( width > ret.x )
947 ret.x = width;
948 }
949 }
950
951 // empty combobox should have some reasonable default size too
952 if ( ret.x < 100 )
953 ret.x = 100;
9f884528
RD
954
955 CacheBestSize(ret);
db434467 956 return ret;
f68586e5
VZ
957}
958
9d522606
RD
959// static
960wxVisualAttributes
961wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
962{
963 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
964}
965
150e31d2
JS
966// ----------------------------------------------------------------------------
967// standard event handling
968// ----------------------------------------------------------------------------
969
970void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
971{
972 Cut();
973}
974
975void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
976{
977 Copy();
978}
979
980void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
981{
982 Paste();
983}
984
985void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
986{
987 Undo();
988}
989
990void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
991{
992 Redo();
993}
994
995void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
996{
997 long from, to;
998 GetSelection(& from, & to);
999 if (from != -1 && to != -1)
1000 Remove(from, to);
1001}
1002
1003void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1004{
1005 SetSelection(-1, -1);
1006}
1007
1008void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1009{
1010 event.Enable( CanCut() );
1011}
1012
1013void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1014{
1015 event.Enable( CanCopy() );
1016}
1017
1018void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1019{
1020 event.Enable( CanPaste() );
1021}
1022
1023void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1024{
1025 event.Enable( CanUndo() );
1026}
1027
1028void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1029{
1030 event.Enable( CanRedo() );
1031}
1032
1033void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1034{
1035 event.Enable(HasSelection() && IsEditable()) ;
1036}
1037
1038void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1039{
1040 event.Enable(GetLastPosition() > 0);
1041}
1042
dcf924a3 1043#endif
150e31d2 1044