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