]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
Contains changes for autoconf_inc.m4,1.77,1.78 which was the big cleanup.
[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 {
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
fd0eed64 222 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 223
2e1d7104 224#ifndef __WXGTK20__
81a0614b 225 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
2e1d7104 226#endif
159b66c0 227
fd0eed64
RR
228 for (int i = 0; i < n; i++)
229 {
fab591c5 230 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
805dd538 231
fd0eed64 232 m_clientDataList.Append( (wxObject*)NULL );
f5e27805 233 m_clientObjectList.Append( (wxObject*)NULL );
805dd538 234
fd0eed64 235 gtk_container_add( GTK_CONTAINER(list), list_item );
805dd538 236
19da4326 237 gtk_widget_show( list_item );
fd0eed64 238 }
805dd538 239
f03fc89f 240 m_parent->DoAddChild( this );
30ed6e5c 241
461573cc 242 m_focusWidget = combo->entry;
805dd538 243
abdeb9e7 244 PostCreation(size);
53010e52 245
461573cc 246 ConnectWidget( combo->button );
805dd538 247
461573cc
RR
248 // MSW's combo box shows the value and the selection is -1
249 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
250 gtk_list_unselect_all( GTK_LIST(combo->list) );
805dd538 251
a260fe6a 252 if (style & wxCB_READONLY)
461573cc 253 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
a260fe6a 254
9d6a9fdd
RR
255 // "show" and "hide" events are generated when user click on the combobox button which popups a list
256 // this list is the "popwin" gtk widget
257 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
258 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
259 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
260 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
261
461573cc
RR
262 gtk_signal_connect( GTK_OBJECT(combo->entry), "changed",
263 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
264
265 gtk_signal_connect( GTK_OBJECT(combo->list), "select-child",
266 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
805dd538 267
abdeb9e7 268 SetBestSize(size); // need this too because this is a wxControlWithItems
805dd538 269
abdeb9e7
RD
270 // This is required for tool bar support
271 wxSize setsize = GetSize();
272 gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
150e31d2 273
fd0eed64
RR
274 return TRUE;
275}
276
277wxComboBox::~wxComboBox()
278{
222ed1d6 279 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
280 while (node)
281 {
b1d4dd7a 282 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 283 if (cd) delete cd;
b1d4dd7a 284 node = node->GetNext();
fd0eed64 285 }
7d6d2cd4
RR
286 m_clientObjectList.Clear();
287
fd0eed64 288 m_clientDataList.Clear();
6de97a3b 289}
53010e52 290
2b5f62a0
VZ
291void wxComboBox::SetFocus()
292{
293 if ( m_hasFocus )
294 {
295 // don't do anything if we already have focus
296 return;
297 }
298
299 gtk_widget_grab_focus( m_focusWidget );
300}
301
6f6f938f 302int wxComboBox::DoAppend( const wxString &item )
53010e52 303{
2a68b7a0 304 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 305
461573cc 306 DisableEvents();
30ed6e5c 307
fd0eed64 308 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 309
fab591c5 310 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
805dd538 311
ec5d85fb
RR
312 gtk_container_add( GTK_CONTAINER(list), list_item );
313
2b07d713
RR
314 if (GTK_WIDGET_REALIZED(m_widget))
315 {
316 gtk_widget_realize( list_item );
317 gtk_widget_realize( GTK_BIN(list_item)->child );
631a05be 318 }
2b07d713 319
631a05be
RL
320 // Apply current widget style to the new list_item
321 GtkRcStyle *style = CreateWidgetStyle();
322 if (style)
323 {
324 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
325 GtkBin *bin = GTK_BIN( list_item );
326 GtkWidget *label = GTK_WIDGET( bin->child );
327 gtk_widget_modify_style( label, style );
328 gtk_rc_style_unref( style );
2b07d713 329 }
805dd538 330
fd0eed64 331 gtk_widget_show( list_item );
30ed6e5c 332
6f6f938f 333 const int count = GetCount();
53010e52 334
6f6f938f 335 if ( (int)m_clientDataList.GetCount() < count )
f5e27805 336 m_clientDataList.Append( (wxObject*) NULL );
6f6f938f 337 if ( (int)m_clientObjectList.GetCount() < count )
f5e27805 338 m_clientObjectList.Append( (wxObject*) NULL );
805dd538 339
6f6f938f 340 EnableEvents();
805dd538 341
b0021947
VS
342 InvalidateBestSize();
343
6f6f938f 344 return count - 1;
fd0eed64
RR
345}
346
6f6f938f 347int wxComboBox::DoInsert( const wxString &item, int pos )
243dbf1a 348{
708c45a6
VZ
349 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
350 wxT("can't insert into sorted list"));
351
352 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
243dbf1a
VZ
353
354 int count = GetCount();
6f6f938f
VZ
355 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
356
243dbf1a 357 if (pos == count)
6f6f938f 358 return Append(item);
243dbf1a
VZ
359
360 DisableEvents();
361
362 GtkWidget *list = GTK_COMBO(m_widget)->list;
363
364 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
365
366 GList *gitem_list = g_list_alloc ();
367 gitem_list->data = list_item;
368 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
369
370 if (GTK_WIDGET_REALIZED(m_widget))
371 {
372 gtk_widget_realize( list_item );
373 gtk_widget_realize( GTK_BIN(list_item)->child );
374
f40fdaa3 375 ApplyWidgetStyle();
243dbf1a
VZ
376 }
377
378 gtk_widget_show( list_item );
379
6f6f938f 380 count = GetCount();
243dbf1a 381
6f6f938f 382 if ( (int)m_clientDataList.GetCount() < count )
243dbf1a 383 m_clientDataList.Insert( pos, (wxObject*) NULL );
6f6f938f 384 if ( (int)m_clientObjectList.GetCount() < count )
243dbf1a
VZ
385 m_clientObjectList.Insert( pos, (wxObject*) NULL );
386
6f6f938f 387 EnableEvents();
150e31d2 388
b0021947 389 InvalidateBestSize();
243dbf1a 390
6f6f938f 391 return pos;
243dbf1a
VZ
392}
393
6f6f938f 394void wxComboBox::DoSetItemClientData( int n, void* clientData )
fd0eed64 395{
223d09f6 396 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 397
222ed1d6 398 wxList::compatibility_iterator node = m_clientDataList.Item( n );
fd0eed64 399 if (!node) return;
805dd538 400
f5e27805 401 node->SetData( (wxObject*) clientData );
6de97a3b 402}
53010e52 403
6f6f938f 404void* wxComboBox::DoGetItemClientData( int n ) const
53010e52 405{
223d09f6 406 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
805dd538 407
222ed1d6 408 wxList::compatibility_iterator node = m_clientDataList.Item( n );
805dd538 409
30ed6e5c 410 return node ? node->GetData() : NULL;
fd0eed64
RR
411}
412
6f6f938f 413void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 414{
223d09f6 415 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 416
222ed1d6 417 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
fd0eed64 418 if (!node) return;
805dd538 419
e94e2e95 420 // wxItemContainer already deletes data for us
805dd538 421
fd0eed64 422 node->SetData( (wxObject*) clientData );
6de97a3b 423}
53010e52 424
6f6f938f 425wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
53010e52 426{
223d09f6 427 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
805dd538 428
222ed1d6 429 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
805dd538 430
30ed6e5c 431 return node ? (wxClientData*) node->GetData() : NULL;
fd0eed64
RR
432}
433
434void wxComboBox::Clear()
435{
223d09f6 436 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 437
461573cc 438 DisableEvents();
30ed6e5c 439
fd0eed64 440 GtkWidget *list = GTK_COMBO(m_widget)->list;
6f6f938f 441 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
805dd538 442
222ed1d6 443 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
fd0eed64
RR
444 while (node)
445 {
b1d4dd7a 446 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 447 if (cd) delete cd;
b1d4dd7a 448 node = node->GetNext();
fd0eed64 449 }
f5e27805 450 m_clientObjectList.Clear();
805dd538 451
fd0eed64 452 m_clientDataList.Clear();
30ed6e5c 453
461573cc 454 EnableEvents();
b0021947
VS
455
456 InvalidateBestSize();
6de97a3b 457}
53010e52 458
fd0eed64 459void wxComboBox::Delete( int n )
53010e52 460{
223d09f6 461 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 462
fd0eed64 463 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
805dd538 464
fd0eed64 465 GList *child = g_list_nth( listbox->children, n );
805dd538 466
fd0eed64
RR
467 if (!child)
468 {
223d09f6 469 wxFAIL_MSG(wxT("wrong index"));
fd0eed64
RR
470 return;
471 }
805dd538 472
461573cc 473 DisableEvents();
30ed6e5c 474
bbe0af5b 475 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
476 gtk_list_remove_items( listbox, list );
477 g_list_free( list );
805dd538 478
222ed1d6 479 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
f5e27805 480 if (node)
fd0eed64 481 {
b1d4dd7a 482 wxClientData *cd = (wxClientData*)node->GetData();
fd0eed64 483 if (cd) delete cd;
222ed1d6 484 m_clientObjectList.Erase( node );
f5e27805 485 }
805dd538 486
b1d4dd7a 487 node = m_clientDataList.Item( n );
f5e27805 488 if (node)
222ed1d6 489 m_clientDataList.Erase( node );
150e31d2 490
461573cc 491 EnableEvents();
150e31d2 492
b0021947 493 InvalidateBestSize();
461573cc
RR
494}
495
496void wxComboBox::SetString(int n, const wxString &text)
497{
498 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
499
500 GtkWidget *list = GTK_COMBO(m_widget)->list;
501
502 GList *child = g_list_nth( GTK_LIST(list)->children, n );
503 if (child)
504 {
505 GtkBin *bin = GTK_BIN( child->data );
506 GtkLabel *label = GTK_LABEL( bin->child );
507 gtk_label_set_text(label, wxGTK_CONV(text));
508 }
509 else
510 {
511 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 512 }
150e31d2 513
b0021947 514 InvalidateBestSize();
6de97a3b 515}
53010e52 516
6f6f938f 517int wxComboBox::FindString( const wxString &item ) const
53010e52 518{
223d09f6 519 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 520
fd0eed64 521 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 522
53010e52
RR
523 GList *child = GTK_LIST(list)->children;
524 int count = 0;
525 while (child)
526 {
fd0eed64
RR
527 GtkBin *bin = GTK_BIN( child->data );
528 GtkLabel *label = GTK_LABEL( bin->child );
2b5f62a0
VZ
529#ifdef __WXGTK20__
530 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
531#else
532 wxString str( label->label );
533#endif
534 if (item == str)
7cf8cb48 535 return count;
30ed6e5c 536
fd0eed64
RR
537 count++;
538 child = child->next;
539 }
805dd538 540
7cf8cb48 541 return wxNOT_FOUND;
fd0eed64
RR
542}
543
544int wxComboBox::GetSelection() const
545{
223d09f6 546 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 547
fd0eed64 548 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 549
fd0eed64
RR
550 GList *selection = GTK_LIST(list)->selection;
551 if (selection)
552 {
553 GList *child = GTK_LIST(list)->children;
554 int count = 0;
555 while (child)
556 {
557 if (child->data == selection->data) return count;
558 count++;
559 child = child->next;
560 }
6de97a3b 561 }
805dd538 562
fd0eed64 563 return -1;
6de97a3b 564}
53010e52 565
debe6624 566wxString wxComboBox::GetString( int n ) const
53010e52 567{
223d09f6 568 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 569
fd0eed64 570 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 571
7cf8cb48 572 wxString str;
fd0eed64
RR
573 GList *child = g_list_nth( GTK_LIST(list)->children, n );
574 if (child)
575 {
576 GtkBin *bin = GTK_BIN( child->data );
577 GtkLabel *label = GTK_LABEL( bin->child );
2e1d7104 578#ifdef __WXGTK20__
2b5f62a0 579 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
2e1d7104
RR
580#else
581 str = wxString( label->label );
582#endif
7cf8cb48
VZ
583 }
584 else
585 {
223d09f6 586 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
fd0eed64 587 }
805dd538 588
7cf8cb48 589 return str;
6de97a3b 590}
53010e52 591
fd0eed64 592wxString wxComboBox::GetStringSelection() const
53010e52 593{
223d09f6 594 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
805dd538 595
fd0eed64 596 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 597
fd0eed64
RR
598 GList *selection = GTK_LIST(list)->selection;
599 if (selection)
600 {
601 GtkBin *bin = GTK_BIN( selection->data );
2b5f62a0
VZ
602 GtkLabel *label = GTK_LABEL( bin->child );
603#ifdef __WXGTK20__
604 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
605#else
606 wxString tmp( label->label );
607#endif
fd0eed64
RR
608 return tmp;
609 }
805dd538 610
223d09f6 611 wxFAIL_MSG( wxT("wxComboBox: no selection") );
805dd538 612
223d09f6 613 return wxT("");
6de97a3b 614}
53010e52 615
6f6f938f 616int wxComboBox::GetCount() const
53010e52 617{
223d09f6 618 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 619
fd0eed64 620 GtkWidget *list = GTK_COMBO(m_widget)->list;
805dd538 621
fd0eed64
RR
622 GList *child = GTK_LIST(list)->children;
623 int count = 0;
624 while (child) { count++; child = child->next; }
625 return count;
6de97a3b 626}
53010e52 627
debe6624 628void wxComboBox::SetSelection( int n )
53010e52 629{
223d09f6 630 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 631
953704c1
RR
632 DisableEvents();
633
fd0eed64 634 GtkWidget *list = GTK_COMBO(m_widget)->list;
159b66c0 635 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
fd0eed64 636 gtk_list_select_item( GTK_LIST(list), n );
159b66c0 637 m_prevSelection = n;
953704c1
RR
638
639 EnableEvents();
6de97a3b 640}
53010e52 641
95561ddf 642bool wxComboBox::SetStringSelection( const wxString &string )
47908e25 643{
95561ddf 644 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") );
805dd538 645
fd0eed64 646 int res = FindString( string );
95561ddf 647 if (res == -1) return false;
fd0eed64 648 SetSelection( res );
95561ddf 649 return true;
6de97a3b 650}
47908e25 651
fd0eed64 652wxString wxComboBox::GetValue() const
53010e52 653{
2e1d7104
RR
654 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
655 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
656
30ed6e5c 657#if 0
2e1d7104
RR
658 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
659 {
660 wxChar c = tmp[i];
661 printf( "%d ", (int) (c) );
662 }
663 printf( "\n" );
664#endif
30ed6e5c 665
fd0eed64 666 return tmp;
6de97a3b 667}
53010e52
RR
668
669void wxComboBox::SetValue( const wxString& value )
670{
223d09f6 671 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 672
fd0eed64 673 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
223d09f6 674 wxString tmp = wxT("");
fd0eed64 675 if (!value.IsNull()) tmp = value;
fab591c5 676 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
150e31d2 677
b0021947 678 InvalidateBestSize();
6de97a3b 679}
53010e52 680
fd0eed64 681void wxComboBox::Copy()
53010e52 682{
223d09f6 683 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 684
fd0eed64 685 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 686 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 687}
53010e52 688
fd0eed64 689void wxComboBox::Cut()
53010e52 690{
223d09f6 691 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 692
fd0eed64 693 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 694 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
6de97a3b 695}
53010e52 696
fd0eed64 697void wxComboBox::Paste()
53010e52 698{
223d09f6 699 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 700
fd0eed64 701 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
9e691f46 702 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
6de97a3b 703}
53010e52 704
150e31d2
JS
705void wxComboBox::Undo()
706{
707 // TODO
708}
709
710void wxComboBox::Redo()
711{
712 // TODO
713}
714
715void wxComboBox::SelectAll()
716{
4e324a3f 717 SetSelection(0, GetLastPosition());
150e31d2
JS
718}
719
720bool wxComboBox::CanUndo() const
721{
722 // TODO
723 return false;
724}
725
726bool wxComboBox::CanRedo() const
727{
728 // TODO
729 return false;
730}
731
732bool wxComboBox::HasSelection() const
733{
734 long from, to;
735 GetSelection(&from, &to);
736 return from != to;
737}
738
739bool wxComboBox::CanCopy() const
740{
741 // Can copy if there's a selection
742 return HasSelection();
743}
744
745bool wxComboBox::CanCut() const
746{
747 return CanCopy() && IsEditable();
748}
749
750bool wxComboBox::CanPaste() const
751{
752 // TODO: check for text on the clipboard
753 return IsEditable() ;
754}
755
756bool wxComboBox::IsEditable() const
757{
758 return !HasFlag(wxCB_READONLY);
759}
760
761
debe6624 762void wxComboBox::SetInsertionPoint( long pos )
53010e52 763{
223d09f6 764 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 765
6f6f938f
VZ
766 if ( pos == GetLastPosition() )
767 pos = -1;
768
fd0eed64 769 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
073c8fe9 770 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
6de97a3b 771}
53010e52 772
fd0eed64 773long wxComboBox::GetInsertionPoint() const
53010e52 774{
9e691f46 775 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
6de97a3b 776}
53010e52 777
fd0eed64 778long wxComboBox::GetLastPosition() const
53010e52 779{
fd0eed64
RR
780 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
781 int pos = GTK_ENTRY(entry)->text_length;
782 return (long) pos-1;
6de97a3b 783}
53010e52 784
debe6624 785void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 786{
223d09f6 787 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 788
fd0eed64
RR
789 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
790 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
791 if (value.IsNull()) return;
792 gint pos = (gint)to;
30ed6e5c 793
2e1d7104
RR
794#if wxUSE_UNICODE
795 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
796 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
797#else
798 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
799#endif
6de97a3b 800}
53010e52 801
20d10ee1 802void wxComboBox::SetSelection( long from, long to )
53010e52 803{
20d10ee1
VZ
804 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
805 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 806}
53010e52 807
150e31d2
JS
808void wxComboBox::GetSelection( long* from, long* to ) const
809{
810 if (IsEditable())
811 {
812 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
41eb6d76 813#ifdef __WXGTK20__
4e324a3f
JS
814 gint start, end;
815 gtk_editable_get_selection_bounds(editable, & start, & end);
816 *from = start;
817 *to = end;
818#else
150e31d2
JS
819 *from = (long) editable->selection_start_pos;
820 *to = (long) editable->selection_end_pos;
4e324a3f 821#endif
150e31d2
JS
822 }
823}
824
20d10ee1 825void wxComboBox::SetEditable( bool editable )
53010e52 826{
20d10ee1
VZ
827 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
828 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
b4071e91
RR
829}
830
8a85884a
VZ
831void wxComboBox::OnChar( wxKeyEvent &event )
832{
12a3f227 833 if ( event.GetKeyCode() == WXK_RETURN )
8a85884a 834 {
461573cc
RR
835 // GTK automatically selects an item if its in the list
836 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
837 event.SetString( GetValue() );
838 event.SetInt( GetSelection() );
839 event.SetEventObject( this );
3352cfff
RR
840
841 if (!GetEventHandler()->ProcessEvent( event ))
842 {
843 // This will invoke the dialog default action, such
844 // as the clicking the default button.
845
846 wxWindow *top_frame = m_parent;
847 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
848 top_frame = top_frame->GetParent();
849
850 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
851 {
852 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
853
854 if (window->default_widget)
150e31d2 855 gtk_widget_activate (window->default_widget);
3352cfff
RR
856 }
857 }
30ed6e5c 858
461573cc
RR
859 // Catch GTK event so that GTK doesn't open the drop
860 // down list upon RETURN.
0878fb4c 861 return;
8a85884a 862 }
30ed6e5c 863
7cf8cb48 864 event.Skip();
8a85884a
VZ
865}
866
953704c1
RR
867void wxComboBox::DisableEvents()
868{
461573cc
RR
869 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
870 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
871 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
872 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
873}
874
875void wxComboBox::EnableEvents()
876{
461573cc
RR
877 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
878 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
879 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
880 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
953704c1
RR
881}
882
b4071e91
RR
883void wxComboBox::OnSize( wxSizeEvent &event )
884{
260a67b7
VS
885 // NB: In some situations (e.g. on non-first page of a wizard, if the
886 // size used is default size), GtkCombo widget is resized correctly,
887 // but it's look is not updated, it's rendered as if it was much wider.
888 // No other widgets are affected, so it looks like a bug in GTK+.
889 // Manually requesting resize calculation (as gtk_pizza_set_size does)
890 // fixes it.
891 if (GTK_WIDGET_VISIBLE(m_widget))
892 gtk_widget_queue_resize(m_widget);
893
f03fc89f 894 event.Skip();
6de97a3b 895}
53010e52 896
f40fdaa3 897void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 898{
f40fdaa3 899// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
ea2d542c 900
f40fdaa3
VS
901 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
902 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
805dd538 903
fd0eed64
RR
904 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
905 GList *child = list->children;
906 while (child)
907 {
f40fdaa3 908 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
805dd538 909
fd0eed64 910 GtkBin *bin = GTK_BIN(child->data);
f40fdaa3 911 gtk_widget_modify_style( bin->child, style );
805dd538 912
fd0eed64
RR
913 child = child->next;
914 }
868a2826 915}
b4071e91 916
fd0eed64 917GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 918{
fd0eed64 919 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
920}
921
b4071e91
RR
922bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
923{
fd0eed64
RR
924 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
925 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 926}
ac57418f 927
f68586e5
VZ
928wxSize wxComboBox::DoGetBestSize() const
929{
db434467 930 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
931
932 // we know better our horizontal extent: it depends on the longest string
933 // in the combobox
a6fc8ae3
VZ
934 if ( m_widget )
935 {
60d85ccb
RR
936 int width;
937 size_t count = GetCount();
a6fc8ae3
VZ
938 for ( size_t n = 0; n < count; n++ )
939 {
2b1ff57f 940 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
941 if ( width > ret.x )
942 ret.x = width;
943 }
944 }
945
946 // empty combobox should have some reasonable default size too
947 if ( ret.x < 100 )
948 ret.x = 100;
9f884528
RD
949
950 CacheBestSize(ret);
db434467 951 return ret;
f68586e5
VZ
952}
953
9d522606
RD
954// static
955wxVisualAttributes
956wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
957{
958 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
959}
960
150e31d2
JS
961// ----------------------------------------------------------------------------
962// standard event handling
963// ----------------------------------------------------------------------------
964
965void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
966{
967 Cut();
968}
969
970void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
971{
972 Copy();
973}
974
975void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
976{
977 Paste();
978}
979
980void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
981{
982 Undo();
983}
984
985void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
986{
987 Redo();
988}
989
990void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
991{
992 long from, to;
993 GetSelection(& from, & to);
994 if (from != -1 && to != -1)
995 Remove(from, to);
996}
997
998void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
999{
1000 SetSelection(-1, -1);
1001}
1002
1003void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1004{
1005 event.Enable( CanCut() );
1006}
1007
1008void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1009{
1010 event.Enable( CanCopy() );
1011}
1012
1013void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1014{
1015 event.Enable( CanPaste() );
1016}
1017
1018void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1019{
1020 event.Enable( CanUndo() );
1021}
1022
1023void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1024{
1025 event.Enable( CanRedo() );
1026}
1027
1028void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1029{
1030 event.Enable(HasSelection() && IsEditable()) ;
1031}
1032
1033void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1034{
1035 event.Enable(GetLastPosition() > 0);
1036}
1037
dcf924a3 1038#endif
150e31d2 1039