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