]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
added wxMouseState::GetPosition(), for consistency with wxMouseEvent
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
53010e52
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
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
9e691f46 24#include "wx/gtk/private.h"
83624f79 25
ff654490
VZ
26// ----------------------------------------------------------------------------
27// GTK callbacks
28// ----------------------------------------------------------------------------
461573cc 29
590f50d6
RR
30extern "C" {
31static void
32gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
33{
590f50d6
RR
34 if (!combo->m_hasVMT) return;
35
36 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
37 event.SetString( combo->GetValue() );
38 event.SetEventObject( combo );
937013e0 39 combo->HandleWindowEvent( event );
590f50d6 40}
590f50d6 41
590f50d6
RR
42static void
43gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
44{
590f50d6 45 if (!combo->m_hasVMT) return;
ddd53873
RR
46
47 if (combo->GetSelection() == -1)
48 return;
590f50d6
RR
49
50 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
51 event.SetInt( combo->GetSelection() );
52 event.SetString( combo->GetStringSelection() );
53 event.SetEventObject( combo );
937013e0 54 combo->HandleWindowEvent( event );
590f50d6
RR
55}
56}
a73ae836 57
e1e955e1
RR
58//-----------------------------------------------------------------------------
59// wxComboBox
53010e52
RR
60//-----------------------------------------------------------------------------
61
62IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
63
b4071e91 64BEGIN_EVENT_TABLE(wxComboBox, wxControl)
8a85884a 65 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
66
67 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
68 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
69 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
70 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
71 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
72 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
73 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
74
75 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
76 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
77 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
78 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
79 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
80 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
81 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
82END_EVENT_TABLE()
83
584ad2a3
MB
84bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
85 const wxString& value,
86 const wxPoint& pos, const wxSize& size,
87 const wxArrayString& choices,
88 long style, const wxValidator& validator,
89 const wxString& name )
90{
91 wxCArrayString chs(choices);
92
93 return Create( parent, id, value, pos, size, chs.GetCount(),
94 chs.GetStrings(), style, validator, name );
95}
96
fd0eed64
RR
97bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
98 const wxPoint& pos, const wxSize& size,
99 int n, const wxString choices[],
805dd538
VZ
100 long style, const wxValidator& validator,
101 const wxString& name )
53010e52 102{
a236aa20 103 m_strings = NULL;
7d8268a1 104 m_ignoreNextUpdate = false;
159b66c0 105 m_prevSelection = 0;
805dd538 106
db434467 107 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
108 !CreateBase( parent, id, pos, size, style, validator, name ))
109 {
223d09f6 110 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 111 return false;
4dcaf11a 112 }
6de97a3b 113
2f515791 114 if (HasFlag(wxCB_SORT))
a236aa20
VZ
115 m_strings = new wxSortedArrayString();
116
ff654490 117 m_widget = gtk_combo_box_entry_new_text();
30ed6e5c 118
2f515791
RR
119 if (HasFlag(wxBORDER_NONE))
120 {
121 // Doesn't seem to work
122 // g_object_set (m_widget, "has-frame", FALSE, NULL);
123 }
124
ff654490 125 GtkEntry * const entry = GetEntry();
3ca6a5f0 126
ff654490 127 gtk_entry_set_editable( entry, TRUE );
805dd538 128
a236aa20 129 Append(n, choices);
590f50d6 130
f03fc89f 131 m_parent->DoAddChild( this );
30ed6e5c 132
590f50d6 133 m_focusWidget = GTK_WIDGET( entry );
805dd538 134
abdeb9e7 135 PostCreation(size);
53010e52 136
ff654490 137 ConnectWidget( m_widget );
805dd538 138
ff654490 139 gtk_entry_set_text( entry, wxGTK_CONV(value) );
8228b893 140
ff654490
VZ
141 if (style & wxCB_READONLY)
142 gtk_entry_set_editable( entry, FALSE );
8228b893 143
ff654490
VZ
144 g_signal_connect_after (entry, "changed",
145 G_CALLBACK (gtkcombobox_text_changed_callback), this);
8228b893 146
ff654490
VZ
147 g_signal_connect_after (m_widget, "changed",
148 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 149
805dd538 150
170acdc9 151 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 152
7d8268a1 153 return true;
fd0eed64
RR
154}
155
ff654490
VZ
156GtkEntry *wxComboBox::GetEntry() const
157{
158 return GTK_ENTRY(GTK_BIN(m_widget)->child);
159}
160
0ec1179b
VZ
161GtkEditable *wxComboBox::GetEditable() const
162{
ff654490 163 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
0ec1179b
VZ
164}
165
fd0eed64
RR
166wxComboBox::~wxComboBox()
167{
a236aa20 168 Clear();
7d6d2cd4 169
a236aa20 170 delete m_strings;
6de97a3b 171}
53010e52 172
a236aa20
VZ
173int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
174 unsigned int pos,
175 void **clientData, wxClientDataType type)
53010e52 176{
2a68b7a0 177 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 178
a236aa20
VZ
179 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
180 _T("In a sorted combobox data could only be appended"));
181
182 const int count = items.GetCount();
183
184 int n = wxNOT_FOUND;
185
ff654490
VZ
186 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
187 for( int i = 0; i < count; ++i )
590f50d6 188 {
ff654490
VZ
189 n = pos + i;
190 // If sorted, use this wxSortedArrayStrings to determine
191 // the right insertion point
192 if(m_strings)
193 n = m_strings->Add(items[i]);
243dbf1a 194
ff654490 195 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
243dbf1a 196
ff654490
VZ
197 m_clientData.Insert( NULL, n );
198 AssignNewItemClientData(n, clientData, i, type);
590f50d6 199 }
8228b893 200
b0021947 201 InvalidateBestSize();
243dbf1a 202
a236aa20 203 return n;
243dbf1a
VZ
204}
205
aa61d352 206void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 207{
a236aa20 208 m_clientData[n] = clientData;
6de97a3b 209}
53010e52 210
aa61d352 211void* wxComboBox::DoGetItemClientData(unsigned int n) const
53010e52 212{
a236aa20 213 return m_clientData[n];
fd0eed64
RR
214}
215
a236aa20 216void wxComboBox::DoClear()
fd0eed64 217{
223d09f6 218 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 219
461573cc 220 DisableEvents();
30ed6e5c 221
ff654490
VZ
222 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
223 const unsigned int count = GetCount();
224 for (unsigned int i = 0; i < count; i++)
225 gtk_combo_box_remove_text( combobox, 0 );
8228b893 226
a236aa20 227 m_clientData.Clear();
805dd538 228
a236aa20
VZ
229 if(m_strings)
230 m_strings->Clear();
30ed6e5c 231
461573cc 232 EnableEvents();
b0021947
VS
233
234 InvalidateBestSize();
6de97a3b 235}
53010e52 236
a236aa20 237void wxComboBox::DoDeleteOneItem(unsigned int n)
53010e52 238{
223d09f6 239 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 240
ff654490 241 wxCHECK_RET( IsValid(n), wxT("invalid index") );
30ed6e5c 242
ff654490
VZ
243 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
244 gtk_combo_box_remove_text( combobox, n );
8228b893 245
a236aa20
VZ
246 m_clientData.RemoveAt( n );
247 if(m_strings)
248 m_strings->RemoveAt( n );
150e31d2 249
b0021947 250 InvalidateBestSize();
461573cc
RR
251}
252
aa61d352 253void wxComboBox::SetString(unsigned int n, const wxString &text)
461573cc
RR
254{
255 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
256
ff654490
VZ
257 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
258 wxCHECK_RET( IsValid(n), wxT("invalid index") );
8228b893 259
ff654490
VZ
260 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
261 GtkTreeIter iter;
262 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
461573cc 263 {
ff654490
VZ
264 GValue value = { 0, };
265 g_value_init( &value, G_TYPE_STRING );
266 g_value_set_string( &value, wxGTK_CONV( text ) );
267 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
268 g_value_unset( &value );
590f50d6 269 }
8228b893 270
b0021947 271 InvalidateBestSize();
6de97a3b 272}
53010e52 273
11e62fe6 274int wxComboBox::FindString( const wxString &item, bool bCase ) const
53010e52 275{
0a164d4c 276 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
805dd538 277
ff654490
VZ
278 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
279 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
280 GtkTreeIter iter;
281 gtk_tree_model_get_iter_first( model, &iter );
282 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
283 return -1;
284 int count = 0;
285 do
53010e52 286 {
ff654490
VZ
287 GValue value = { 0, };
288 gtk_tree_model_get_value( model, &iter, 0, &value );
289 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
290 g_value_unset( &value );
8228b893 291
ff654490
VZ
292 if (item.IsSameAs( str, bCase ) )
293 return count;
8228b893 294
ff654490 295 count++;
fd0eed64 296 }
ff654490 297 while ( gtk_tree_model_iter_next(model, &iter) );
805dd538 298
7cf8cb48 299 return wxNOT_FOUND;
fd0eed64
RR
300}
301
302int wxComboBox::GetSelection() const
40eb3606 303{
ff654490
VZ
304 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
305 return gtk_combo_box_get_active( combobox );
40eb3606
VZ
306}
307
308int wxComboBox::GetCurrentSelection() const
fd0eed64 309{
223d09f6 310 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
805dd538 311
ff654490
VZ
312 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
313 return gtk_combo_box_get_active( combobox );
6de97a3b 314}
53010e52 315
aa61d352 316wxString wxComboBox::GetString(unsigned int n) const
53010e52 317{
0a164d4c 318 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
805dd538 319
7cf8cb48 320 wxString str;
8228b893 321
ff654490
VZ
322 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
323 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
324 GtkTreeIter iter;
325 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
8228b893 326 {
ff654490
VZ
327 GValue value = { 0, };
328 gtk_tree_model_get_value( model, &iter, 0, &value );
329 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
330 g_value_unset( &value );
331 return tmp;
fd0eed64 332 }
805dd538 333
7cf8cb48 334 return str;
6de97a3b 335}
53010e52 336
aa61d352 337unsigned int wxComboBox::GetCount() const
53010e52 338{
223d09f6 339 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
805dd538 340
ff654490
VZ
341 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
342 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
343 GtkTreeIter iter;
344 gtk_tree_model_get_iter_first( model, &iter );
345 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
346 return 0;
347 unsigned int ret = 1;
348 while (gtk_tree_model_iter_next( model, &iter ))
349 ret++;
350 return ret;
6de97a3b 351}
53010e52 352
debe6624 353void wxComboBox::SetSelection( int n )
53010e52 354{
223d09f6 355 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
805dd538 356
953704c1
RR
357 DisableEvents();
358
ff654490
VZ
359 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
360 gtk_combo_box_set_active( combobox, n );
8228b893 361
953704c1 362 EnableEvents();
6de97a3b 363}
53010e52 364
8a85884a
VZ
365void wxComboBox::OnChar( wxKeyEvent &event )
366{
643c973b 367 switch ( event.GetKeyCode() )
8a85884a 368 {
643c973b
VZ
369 case WXK_RETURN:
370 if ( HasFlag(wxTE_PROCESS_ENTER) )
3352cfff 371 {
643c973b
VZ
372 // GTK automatically selects an item if its in the list
373 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
374 eventEnter.SetString( GetValue() );
375 eventEnter.SetInt( GetSelection() );
376 eventEnter.SetEventObject( this );
377
937013e0 378 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
379 {
380 // Catch GTK event so that GTK doesn't open the drop
381 // down list upon RETURN.
382 return;
383 }
3352cfff 384 }
5e9b723f
RR
385
386 // On enter key press, we must give a signal to default control,
387 // Otherwise, nothing happens when pressing Enter from inside a
388 // combo box in a dialog.
389 wxWindow *top_frame = wxGetTopLevelParent(this);
390 if( top_frame && GTK_IS_WINDOW(top_frame->m_widget) )
391 {
392 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
393 if ( window->default_widget )
394 gtk_widget_activate( window->default_widget );
395 }
643c973b 396 break;
8a85884a 397 }
30ed6e5c 398
7cf8cb48 399 event.Skip();
8a85884a
VZ
400}
401
953704c1
RR
402void wxComboBox::DisableEvents()
403{
ff654490
VZ
404 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
405 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 406
ff654490
VZ
407 g_signal_handlers_block_by_func(m_widget,
408 (gpointer)gtkcombobox_changed_callback, this);
953704c1
RR
409}
410
411void wxComboBox::EnableEvents()
412{
ff654490
VZ
413 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
414 (gpointer)gtkcombobox_text_changed_callback, this);
ea2d542c 415
ff654490
VZ
416 g_signal_handlers_unblock_by_func(m_widget,
417 (gpointer)gtkcombobox_changed_callback, this);
868a2826 418}
b4071e91 419
fd0eed64 420GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 421{
ff654490 422 return GTK_WIDGET( GetEntry() );
97b3455a
RR
423}
424
ef5c70f9 425GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
b4071e91 426{
ff654490 427 wxUnusedVar(windows);
ef5c70f9 428
ff654490 429 return GetEntry()->text_area;
b4071e91 430}
ac57418f 431
f68586e5
VZ
432wxSize wxComboBox::DoGetBestSize() const
433{
db434467 434 wxSize ret( wxControl::DoGetBestSize() );
a6fc8ae3
VZ
435
436 // we know better our horizontal extent: it depends on the longest string
437 // in the combobox
a6fc8ae3
VZ
438 if ( m_widget )
439 {
60d85ccb 440 int width;
aa61d352
VZ
441 unsigned int count = GetCount();
442 for ( unsigned int n = 0; n < count; n++ )
a6fc8ae3 443 {
aa61d352 444 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
a6fc8ae3
VZ
445 if ( width > ret.x )
446 ret.x = width;
447 }
448 }
449
450 // empty combobox should have some reasonable default size too
451 if ( ret.x < 100 )
452 ret.x = 100;
9f884528
RD
453
454 CacheBestSize(ret);
db434467 455 return ret;
f68586e5
VZ
456}
457
9d522606
RD
458// static
459wxVisualAttributes
460wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
461{
ff654490 462 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9d522606
RD
463}
464
150e31d2
JS
465// ----------------------------------------------------------------------------
466// standard event handling
467// ----------------------------------------------------------------------------
468
469void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
470{
471 Cut();
472}
473
474void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
475{
476 Copy();
477}
478
479void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
480{
481 Paste();
482}
483
484void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
485{
486 Undo();
487}
488
489void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
490{
491 Redo();
492}
493
494void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
495{
5a25f858 496 RemoveSelection();
150e31d2
JS
497}
498
499void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
500{
e976429d 501 SelectAll();
150e31d2
JS
502}
503
504void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
505{
506 event.Enable( CanCut() );
507}
508
509void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
510{
511 event.Enable( CanCopy() );
512}
513
514void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
515{
516 event.Enable( CanPaste() );
517}
518
519void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
520{
521 event.Enable( CanUndo() );
522}
523
524void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
525{
526 event.Enable( CanRedo() );
527}
528
529void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
530{
531 event.Enable(HasSelection() && IsEditable()) ;
532}
533
534void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
535{
e976429d 536 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
537}
538
0ec1179b 539#endif // wxUSE_COMBOBOX