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