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