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