]>
Commit | Line | Data |
---|---|---|
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 | ||
33 | extern void wxapp_install_idle_handler(); | |
34 | extern bool g_isIdle; | |
35 | ||
47908e25 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern bool g_blockEventsOnDrag; | |
41 | ||
53010e52 | 42 | //----------------------------------------------------------------------------- |
461573cc | 43 | // "select-child" - click/cursor get select-child, changed, select-child |
47908e25 | 44 | //----------------------------------------------------------------------------- |
47908e25 | 45 | |
8a85884a | 46 | static void |
461573cc | 47 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
53010e52 | 48 | { |
acfd422a | 49 | if (g_isIdle) wxapp_install_idle_handler(); |
8a85884a | 50 | |
a2053b27 | 51 | if (!combo->m_hasVMT) return; |
30ed6e5c | 52 | |
acfd422a | 53 | if (g_blockEventsOnDrag) return; |
805dd538 | 54 | |
159b66c0 | 55 | int curSelection = combo->GetSelection(); |
30ed6e5c | 56 | |
3c4e4af6 RR |
57 | if (combo->m_prevSelection == curSelection) return; |
58 | ||
59 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
60 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
61 | ||
159b66c0 RR |
62 | combo->m_prevSelection = curSelection; |
63 | ||
8a85884a | 64 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
159b66c0 | 65 | event.SetInt( curSelection ); |
29006414 | 66 | event.SetString( combo->GetStringSelection() ); |
8a85884a | 67 | event.SetEventObject( combo ); |
3c4e4af6 | 68 | |
8a85884a | 69 | combo->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 70 | } |
47908e25 | 71 | |
0c77152e | 72 | //----------------------------------------------------------------------------- |
461573cc RR |
73 | // "changed" - typing and list item matches get changed, select-child |
74 | // if it doesn't match an item then just get a single changed | |
0c77152e RR |
75 | //----------------------------------------------------------------------------- |
76 | ||
eeccd5d9 | 77 | static void |
0c77152e RR |
78 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
79 | { | |
acfd422a | 80 | if (g_isIdle) wxapp_install_idle_handler(); |
31528cd3 | 81 | |
a2053b27 RR |
82 | if (!combo->m_hasVMT) return; |
83 | ||
8a85884a | 84 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
29006414 | 85 | event.SetString( combo->GetValue() ); |
0c77152e RR |
86 | event.SetEventObject( combo ); |
87 | combo->GetEventHandler()->ProcessEvent( event ); | |
0c77152e RR |
88 | } |
89 | ||
461573cc RR |
90 | static void |
91 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
92 | { | |
93 | } | |
94 | ||
e1e955e1 RR |
95 | //----------------------------------------------------------------------------- |
96 | // wxComboBox | |
53010e52 RR |
97 | //----------------------------------------------------------------------------- |
98 | ||
99 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
100 | ||
b4071e91 | 101 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
fd0eed64 | 102 | EVT_SIZE(wxComboBox::OnSize) |
8a85884a | 103 | EVT_CHAR(wxComboBox::OnChar) |
b4071e91 RR |
104 | END_EVENT_TABLE() |
105 | ||
584ad2a3 MB |
106 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
107 | const wxString& value, | |
108 | const wxPoint& pos, const wxSize& size, | |
109 | const wxArrayString& choices, | |
110 | long style, const wxValidator& validator, | |
111 | const wxString& name ) | |
112 | { | |
113 | wxCArrayString chs(choices); | |
114 | ||
115 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
116 | chs.GetStrings(), style, validator, name ); | |
117 | } | |
118 | ||
fd0eed64 RR |
119 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
120 | const wxPoint& pos, const wxSize& size, | |
121 | int n, const wxString choices[], | |
805dd538 VZ |
122 | long style, const wxValidator& validator, |
123 | const wxString& name ) | |
53010e52 | 124 | { |
fd0eed64 RR |
125 | m_alreadySent = FALSE; |
126 | m_needParent = TRUE; | |
b292e2f5 | 127 | m_acceptsFocus = TRUE; |
159b66c0 | 128 | m_prevSelection = 0; |
805dd538 | 129 | |
db434467 | 130 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
131 | !CreateBase( parent, id, pos, size, style, validator, name )) |
132 | { | |
223d09f6 | 133 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
9d9b7755 | 134 | return FALSE; |
4dcaf11a | 135 | } |
6de97a3b | 136 | |
fd0eed64 | 137 | m_widget = gtk_combo_new(); |
461573cc | 138 | GtkCombo *combo = GTK_COMBO(m_widget); |
30ed6e5c | 139 | |
461573cc RR |
140 | // Disable GTK's broken events ... |
141 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); | |
142 | // ... and add surogate handler. | |
143 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", | |
144 | (GtkSignalFunc) gtk_dummy_callback, combo); | |
805dd538 | 145 | |
8a85884a | 146 | // make it more useable |
3ca6a5f0 | 147 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
30ed6e5c | 148 | |
3ca6a5f0 BP |
149 | // and case-sensitive |
150 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
151 | ||
fd0eed64 | 152 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 153 | |
2e1d7104 | 154 | #ifndef __WXGTK20__ |
81a0614b | 155 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); |
2e1d7104 | 156 | #endif |
159b66c0 | 157 | |
fd0eed64 RR |
158 | for (int i = 0; i < n; i++) |
159 | { | |
fab591c5 | 160 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
805dd538 | 161 | |
fd0eed64 | 162 | m_clientDataList.Append( (wxObject*)NULL ); |
f5e27805 | 163 | m_clientObjectList.Append( (wxObject*)NULL ); |
805dd538 | 164 | |
fd0eed64 | 165 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
805dd538 | 166 | |
19da4326 | 167 | gtk_widget_show( list_item ); |
fd0eed64 | 168 | } |
805dd538 | 169 | |
f03fc89f | 170 | m_parent->DoAddChild( this ); |
30ed6e5c | 171 | |
461573cc | 172 | m_focusWidget = combo->entry; |
805dd538 | 173 | |
abdeb9e7 | 174 | PostCreation(size); |
53010e52 | 175 | |
461573cc | 176 | ConnectWidget( combo->button ); |
805dd538 | 177 | |
461573cc RR |
178 | // MSW's combo box shows the value and the selection is -1 |
179 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
180 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
805dd538 | 181 | |
a260fe6a | 182 | if (style & wxCB_READONLY) |
461573cc | 183 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
a260fe6a | 184 | |
461573cc RR |
185 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
186 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
187 | ||
188 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
189 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
805dd538 | 190 | |
abdeb9e7 | 191 | SetBestSize(size); // need this too because this is a wxControlWithItems |
805dd538 | 192 | |
abdeb9e7 RD |
193 | // This is required for tool bar support |
194 | wxSize setsize = GetSize(); | |
195 | gtk_widget_set_usize( m_widget, setsize.x, setsize.y ); | |
196 | ||
fd0eed64 RR |
197 | return TRUE; |
198 | } | |
199 | ||
200 | wxComboBox::~wxComboBox() | |
201 | { | |
222ed1d6 | 202 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
203 | while (node) |
204 | { | |
b1d4dd7a | 205 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 206 | if (cd) delete cd; |
b1d4dd7a | 207 | node = node->GetNext(); |
fd0eed64 | 208 | } |
7d6d2cd4 RR |
209 | m_clientObjectList.Clear(); |
210 | ||
fd0eed64 | 211 | m_clientDataList.Clear(); |
6de97a3b | 212 | } |
53010e52 | 213 | |
2b5f62a0 VZ |
214 | void wxComboBox::SetFocus() |
215 | { | |
216 | if ( m_hasFocus ) | |
217 | { | |
218 | // don't do anything if we already have focus | |
219 | return; | |
220 | } | |
221 | ||
222 | gtk_widget_grab_focus( m_focusWidget ); | |
223 | } | |
224 | ||
6f6f938f | 225 | int wxComboBox::DoAppend( const wxString &item ) |
53010e52 | 226 | { |
2a68b7a0 | 227 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 228 | |
461573cc | 229 | DisableEvents(); |
30ed6e5c | 230 | |
fd0eed64 | 231 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 232 | |
fab591c5 | 233 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
805dd538 | 234 | |
ec5d85fb RR |
235 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
236 | ||
2b07d713 RR |
237 | if (GTK_WIDGET_REALIZED(m_widget)) |
238 | { | |
239 | gtk_widget_realize( list_item ); | |
240 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
631a05be | 241 | } |
2b07d713 | 242 | |
631a05be RL |
243 | // Apply current widget style to the new list_item |
244 | GtkRcStyle *style = CreateWidgetStyle(); | |
245 | if (style) | |
246 | { | |
247 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); | |
248 | GtkBin *bin = GTK_BIN( list_item ); | |
249 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
250 | gtk_widget_modify_style( label, style ); | |
251 | gtk_rc_style_unref( style ); | |
2b07d713 | 252 | } |
805dd538 | 253 | |
fd0eed64 | 254 | gtk_widget_show( list_item ); |
30ed6e5c | 255 | |
6f6f938f | 256 | const int count = GetCount(); |
53010e52 | 257 | |
6f6f938f | 258 | if ( (int)m_clientDataList.GetCount() < count ) |
f5e27805 | 259 | m_clientDataList.Append( (wxObject*) NULL ); |
6f6f938f | 260 | if ( (int)m_clientObjectList.GetCount() < count ) |
f5e27805 | 261 | m_clientObjectList.Append( (wxObject*) NULL ); |
805dd538 | 262 | |
6f6f938f | 263 | EnableEvents(); |
805dd538 | 264 | |
b0021947 VS |
265 | InvalidateBestSize(); |
266 | ||
6f6f938f | 267 | return count - 1; |
fd0eed64 RR |
268 | } |
269 | ||
6f6f938f | 270 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
243dbf1a | 271 | { |
708c45a6 VZ |
272 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
273 | wxT("can't insert into sorted list")); | |
274 | ||
275 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
243dbf1a VZ |
276 | |
277 | int count = GetCount(); | |
6f6f938f VZ |
278 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
279 | ||
243dbf1a | 280 | if (pos == count) |
6f6f938f | 281 | return Append(item); |
243dbf1a VZ |
282 | |
283 | DisableEvents(); | |
284 | ||
285 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
286 | ||
287 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
288 | ||
289 | GList *gitem_list = g_list_alloc (); | |
290 | gitem_list->data = list_item; | |
291 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
292 | ||
293 | if (GTK_WIDGET_REALIZED(m_widget)) | |
294 | { | |
295 | gtk_widget_realize( list_item ); | |
296 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
297 | ||
f40fdaa3 | 298 | ApplyWidgetStyle(); |
243dbf1a VZ |
299 | } |
300 | ||
301 | gtk_widget_show( list_item ); | |
302 | ||
6f6f938f | 303 | count = GetCount(); |
243dbf1a | 304 | |
6f6f938f | 305 | if ( (int)m_clientDataList.GetCount() < count ) |
243dbf1a | 306 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
6f6f938f | 307 | if ( (int)m_clientObjectList.GetCount() < count ) |
243dbf1a VZ |
308 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
309 | ||
6f6f938f | 310 | EnableEvents(); |
b0021947 VS |
311 | |
312 | InvalidateBestSize(); | |
243dbf1a | 313 | |
6f6f938f | 314 | return pos; |
243dbf1a VZ |
315 | } |
316 | ||
6f6f938f | 317 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 318 | { |
223d09f6 | 319 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 320 | |
222ed1d6 | 321 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
fd0eed64 | 322 | if (!node) return; |
805dd538 | 323 | |
f5e27805 | 324 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 325 | } |
53010e52 | 326 | |
6f6f938f | 327 | void* wxComboBox::DoGetItemClientData( int n ) const |
53010e52 | 328 | { |
223d09f6 | 329 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
805dd538 | 330 | |
222ed1d6 | 331 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
805dd538 | 332 | |
30ed6e5c | 333 | return node ? node->GetData() : NULL; |
fd0eed64 RR |
334 | } |
335 | ||
6f6f938f | 336 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 337 | { |
223d09f6 | 338 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 339 | |
222ed1d6 | 340 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
fd0eed64 | 341 | if (!node) return; |
805dd538 | 342 | |
e94e2e95 | 343 | // wxItemContainer already deletes data for us |
805dd538 | 344 | |
fd0eed64 | 345 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 346 | } |
53010e52 | 347 | |
6f6f938f | 348 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
53010e52 | 349 | { |
223d09f6 | 350 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
805dd538 | 351 | |
222ed1d6 | 352 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
805dd538 | 353 | |
30ed6e5c | 354 | return node ? (wxClientData*) node->GetData() : NULL; |
fd0eed64 RR |
355 | } |
356 | ||
357 | void wxComboBox::Clear() | |
358 | { | |
223d09f6 | 359 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 360 | |
461573cc | 361 | DisableEvents(); |
30ed6e5c | 362 | |
fd0eed64 | 363 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
6f6f938f | 364 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
805dd538 | 365 | |
222ed1d6 | 366 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
367 | while (node) |
368 | { | |
b1d4dd7a | 369 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 370 | if (cd) delete cd; |
b1d4dd7a | 371 | node = node->GetNext(); |
fd0eed64 | 372 | } |
f5e27805 | 373 | m_clientObjectList.Clear(); |
805dd538 | 374 | |
fd0eed64 | 375 | m_clientDataList.Clear(); |
30ed6e5c | 376 | |
461573cc | 377 | EnableEvents(); |
b0021947 VS |
378 | |
379 | InvalidateBestSize(); | |
6de97a3b | 380 | } |
53010e52 | 381 | |
fd0eed64 | 382 | void wxComboBox::Delete( int n ) |
53010e52 | 383 | { |
223d09f6 | 384 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 385 | |
fd0eed64 | 386 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
805dd538 | 387 | |
fd0eed64 | 388 | GList *child = g_list_nth( listbox->children, n ); |
805dd538 | 389 | |
fd0eed64 RR |
390 | if (!child) |
391 | { | |
223d09f6 | 392 | wxFAIL_MSG(wxT("wrong index")); |
fd0eed64 RR |
393 | return; |
394 | } | |
805dd538 | 395 | |
461573cc | 396 | DisableEvents(); |
30ed6e5c | 397 | |
bbe0af5b | 398 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
399 | gtk_list_remove_items( listbox, list ); |
400 | g_list_free( list ); | |
805dd538 | 401 | |
222ed1d6 | 402 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
f5e27805 | 403 | if (node) |
fd0eed64 | 404 | { |
b1d4dd7a | 405 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 406 | if (cd) delete cd; |
222ed1d6 | 407 | m_clientObjectList.Erase( node ); |
f5e27805 | 408 | } |
805dd538 | 409 | |
b1d4dd7a | 410 | node = m_clientDataList.Item( n ); |
f5e27805 | 411 | if (node) |
222ed1d6 MB |
412 | m_clientDataList.Erase( node ); |
413 | ||
461573cc | 414 | EnableEvents(); |
b0021947 VS |
415 | |
416 | InvalidateBestSize(); | |
461573cc RR |
417 | } |
418 | ||
419 | void wxComboBox::SetString(int n, const wxString &text) | |
420 | { | |
421 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
422 | ||
423 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
424 | ||
425 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
426 | if (child) | |
427 | { | |
428 | GtkBin *bin = GTK_BIN( child->data ); | |
429 | GtkLabel *label = GTK_LABEL( bin->child ); | |
430 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
431 | } | |
432 | else | |
433 | { | |
434 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
fd0eed64 | 435 | } |
b0021947 VS |
436 | |
437 | InvalidateBestSize(); | |
6de97a3b | 438 | } |
53010e52 | 439 | |
6f6f938f | 440 | int wxComboBox::FindString( const wxString &item ) const |
53010e52 | 441 | { |
223d09f6 | 442 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 443 | |
fd0eed64 | 444 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 445 | |
53010e52 RR |
446 | GList *child = GTK_LIST(list)->children; |
447 | int count = 0; | |
448 | while (child) | |
449 | { | |
fd0eed64 RR |
450 | GtkBin *bin = GTK_BIN( child->data ); |
451 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2b5f62a0 VZ |
452 | #ifdef __WXGTK20__ |
453 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
454 | #else | |
455 | wxString str( label->label ); | |
456 | #endif | |
457 | if (item == str) | |
7cf8cb48 | 458 | return count; |
30ed6e5c | 459 | |
fd0eed64 RR |
460 | count++; |
461 | child = child->next; | |
462 | } | |
805dd538 | 463 | |
7cf8cb48 | 464 | return wxNOT_FOUND; |
fd0eed64 RR |
465 | } |
466 | ||
467 | int wxComboBox::GetSelection() const | |
468 | { | |
223d09f6 | 469 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 470 | |
fd0eed64 | 471 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 472 | |
fd0eed64 RR |
473 | GList *selection = GTK_LIST(list)->selection; |
474 | if (selection) | |
475 | { | |
476 | GList *child = GTK_LIST(list)->children; | |
477 | int count = 0; | |
478 | while (child) | |
479 | { | |
480 | if (child->data == selection->data) return count; | |
481 | count++; | |
482 | child = child->next; | |
483 | } | |
6de97a3b | 484 | } |
805dd538 | 485 | |
fd0eed64 | 486 | return -1; |
6de97a3b | 487 | } |
53010e52 | 488 | |
debe6624 | 489 | wxString wxComboBox::GetString( int n ) const |
53010e52 | 490 | { |
223d09f6 | 491 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 492 | |
fd0eed64 | 493 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 494 | |
7cf8cb48 | 495 | wxString str; |
fd0eed64 RR |
496 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
497 | if (child) | |
498 | { | |
499 | GtkBin *bin = GTK_BIN( child->data ); | |
500 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2e1d7104 | 501 | #ifdef __WXGTK20__ |
2b5f62a0 | 502 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
2e1d7104 RR |
503 | #else |
504 | str = wxString( label->label ); | |
505 | #endif | |
7cf8cb48 VZ |
506 | } |
507 | else | |
508 | { | |
223d09f6 | 509 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
fd0eed64 | 510 | } |
805dd538 | 511 | |
7cf8cb48 | 512 | return str; |
6de97a3b | 513 | } |
53010e52 | 514 | |
fd0eed64 | 515 | wxString wxComboBox::GetStringSelection() const |
53010e52 | 516 | { |
223d09f6 | 517 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 518 | |
fd0eed64 | 519 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 520 | |
fd0eed64 RR |
521 | GList *selection = GTK_LIST(list)->selection; |
522 | if (selection) | |
523 | { | |
524 | GtkBin *bin = GTK_BIN( selection->data ); | |
2b5f62a0 VZ |
525 | GtkLabel *label = GTK_LABEL( bin->child ); |
526 | #ifdef __WXGTK20__ | |
527 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
528 | #else | |
529 | wxString tmp( label->label ); | |
530 | #endif | |
fd0eed64 RR |
531 | return tmp; |
532 | } | |
805dd538 | 533 | |
223d09f6 | 534 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
805dd538 | 535 | |
223d09f6 | 536 | return wxT(""); |
6de97a3b | 537 | } |
53010e52 | 538 | |
6f6f938f | 539 | int wxComboBox::GetCount() const |
53010e52 | 540 | { |
223d09f6 | 541 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
805dd538 | 542 | |
fd0eed64 | 543 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 544 | |
fd0eed64 RR |
545 | GList *child = GTK_LIST(list)->children; |
546 | int count = 0; | |
547 | while (child) { count++; child = child->next; } | |
548 | return count; | |
6de97a3b | 549 | } |
53010e52 | 550 | |
debe6624 | 551 | void wxComboBox::SetSelection( int n ) |
53010e52 | 552 | { |
223d09f6 | 553 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 554 | |
953704c1 RR |
555 | DisableEvents(); |
556 | ||
fd0eed64 | 557 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
159b66c0 | 558 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
fd0eed64 | 559 | gtk_list_select_item( GTK_LIST(list), n ); |
159b66c0 | 560 | m_prevSelection = n; |
953704c1 RR |
561 | |
562 | EnableEvents(); | |
6de97a3b | 563 | } |
53010e52 | 564 | |
95561ddf | 565 | bool wxComboBox::SetStringSelection( const wxString &string ) |
47908e25 | 566 | { |
95561ddf | 567 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") ); |
805dd538 | 568 | |
fd0eed64 | 569 | int res = FindString( string ); |
95561ddf | 570 | if (res == -1) return false; |
fd0eed64 | 571 | SetSelection( res ); |
95561ddf | 572 | return true; |
6de97a3b | 573 | } |
47908e25 | 574 | |
fd0eed64 | 575 | wxString wxComboBox::GetValue() const |
53010e52 | 576 | { |
2e1d7104 RR |
577 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
578 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
579 | ||
30ed6e5c | 580 | #if 0 |
2e1d7104 RR |
581 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
582 | { | |
583 | wxChar c = tmp[i]; | |
584 | printf( "%d ", (int) (c) ); | |
585 | } | |
586 | printf( "\n" ); | |
587 | #endif | |
30ed6e5c | 588 | |
fd0eed64 | 589 | return tmp; |
6de97a3b | 590 | } |
53010e52 RR |
591 | |
592 | void wxComboBox::SetValue( const wxString& value ) | |
593 | { | |
223d09f6 | 594 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 595 | |
fd0eed64 | 596 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 597 | wxString tmp = wxT(""); |
fd0eed64 | 598 | if (!value.IsNull()) tmp = value; |
fab591c5 | 599 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
b0021947 VS |
600 | |
601 | InvalidateBestSize(); | |
6de97a3b | 602 | } |
53010e52 | 603 | |
fd0eed64 | 604 | void wxComboBox::Copy() |
53010e52 | 605 | { |
223d09f6 | 606 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 607 | |
fd0eed64 | 608 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 609 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 610 | } |
53010e52 | 611 | |
fd0eed64 | 612 | void wxComboBox::Cut() |
53010e52 | 613 | { |
223d09f6 | 614 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 615 | |
fd0eed64 | 616 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 617 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 618 | } |
53010e52 | 619 | |
fd0eed64 | 620 | void wxComboBox::Paste() |
53010e52 | 621 | { |
223d09f6 | 622 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 623 | |
fd0eed64 | 624 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 625 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 626 | } |
53010e52 | 627 | |
debe6624 | 628 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 629 | { |
223d09f6 | 630 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 631 | |
6f6f938f VZ |
632 | if ( pos == GetLastPosition() ) |
633 | pos = -1; | |
634 | ||
fd0eed64 | 635 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 636 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 637 | } |
53010e52 | 638 | |
fd0eed64 | 639 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 640 | { |
9e691f46 | 641 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 642 | } |
53010e52 | 643 | |
fd0eed64 | 644 | long wxComboBox::GetLastPosition() const |
53010e52 | 645 | { |
fd0eed64 RR |
646 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
647 | int pos = GTK_ENTRY(entry)->text_length; | |
648 | return (long) pos-1; | |
6de97a3b | 649 | } |
53010e52 | 650 | |
debe6624 | 651 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 652 | { |
223d09f6 | 653 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 654 | |
fd0eed64 RR |
655 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
656 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
657 | if (value.IsNull()) return; | |
658 | gint pos = (gint)to; | |
30ed6e5c | 659 | |
2e1d7104 RR |
660 | #if wxUSE_UNICODE |
661 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
662 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
663 | #else | |
664 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
665 | #endif | |
6de97a3b | 666 | } |
53010e52 | 667 | |
20d10ee1 | 668 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 669 | { |
20d10ee1 VZ |
670 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
671 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 672 | } |
53010e52 | 673 | |
20d10ee1 | 674 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 675 | { |
20d10ee1 VZ |
676 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
677 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
678 | } |
679 | ||
8a85884a VZ |
680 | void wxComboBox::OnChar( wxKeyEvent &event ) |
681 | { | |
12a3f227 | 682 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 683 | { |
461573cc RR |
684 | // GTK automatically selects an item if its in the list |
685 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
686 | event.SetString( GetValue() ); | |
687 | event.SetInt( GetSelection() ); | |
688 | event.SetEventObject( this ); | |
3352cfff RR |
689 | |
690 | if (!GetEventHandler()->ProcessEvent( event )) | |
691 | { | |
692 | // This will invoke the dialog default action, such | |
693 | // as the clicking the default button. | |
694 | ||
695 | wxWindow *top_frame = m_parent; | |
696 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
697 | top_frame = top_frame->GetParent(); | |
698 | ||
699 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
700 | { | |
701 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
702 | ||
703 | if (window->default_widget) | |
704 | gtk_widget_activate (window->default_widget); | |
705 | } | |
706 | } | |
30ed6e5c | 707 | |
461573cc RR |
708 | // Catch GTK event so that GTK doesn't open the drop |
709 | // down list upon RETURN. | |
0878fb4c | 710 | return; |
8a85884a | 711 | } |
30ed6e5c | 712 | |
7cf8cb48 | 713 | event.Skip(); |
8a85884a VZ |
714 | } |
715 | ||
953704c1 RR |
716 | void wxComboBox::DisableEvents() |
717 | { | |
461573cc RR |
718 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
719 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
720 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
721 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
722 | } |
723 | ||
724 | void wxComboBox::EnableEvents() | |
725 | { | |
461573cc RR |
726 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
727 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
728 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
729 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
730 | } |
731 | ||
b4071e91 RR |
732 | void wxComboBox::OnSize( wxSizeEvent &event ) |
733 | { | |
260a67b7 VS |
734 | // NB: In some situations (e.g. on non-first page of a wizard, if the |
735 | // size used is default size), GtkCombo widget is resized correctly, | |
736 | // but it's look is not updated, it's rendered as if it was much wider. | |
737 | // No other widgets are affected, so it looks like a bug in GTK+. | |
738 | // Manually requesting resize calculation (as gtk_pizza_set_size does) | |
739 | // fixes it. | |
740 | if (GTK_WIDGET_VISIBLE(m_widget)) | |
741 | gtk_widget_queue_resize(m_widget); | |
742 | ||
f03fc89f | 743 | event.Skip(); |
6de97a3b | 744 | } |
53010e52 | 745 | |
f40fdaa3 | 746 | void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 747 | { |
f40fdaa3 | 748 | // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle ); |
ea2d542c | 749 | |
f40fdaa3 VS |
750 | gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style ); |
751 | gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style ); | |
805dd538 | 752 | |
fd0eed64 RR |
753 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
754 | GList *child = list->children; | |
755 | while (child) | |
756 | { | |
f40fdaa3 | 757 | gtk_widget_modify_style( GTK_WIDGET(child->data), style ); |
805dd538 | 758 | |
fd0eed64 | 759 | GtkBin *bin = GTK_BIN(child->data); |
f40fdaa3 | 760 | gtk_widget_modify_style( bin->child, style ); |
805dd538 | 761 | |
fd0eed64 RR |
762 | child = child->next; |
763 | } | |
868a2826 | 764 | } |
b4071e91 | 765 | |
fd0eed64 | 766 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 767 | { |
fd0eed64 | 768 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
769 | } |
770 | ||
b4071e91 RR |
771 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
772 | { | |
fd0eed64 RR |
773 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
774 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 775 | } |
ac57418f | 776 | |
f68586e5 VZ |
777 | wxSize wxComboBox::DoGetBestSize() const |
778 | { | |
db434467 | 779 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
780 | |
781 | // we know better our horizontal extent: it depends on the longest string | |
782 | // in the combobox | |
a6fc8ae3 VZ |
783 | if ( m_widget ) |
784 | { | |
60d85ccb RR |
785 | int width; |
786 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
787 | for ( size_t n = 0; n < count; n++ ) |
788 | { | |
2b1ff57f | 789 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
a6fc8ae3 VZ |
790 | if ( width > ret.x ) |
791 | ret.x = width; | |
792 | } | |
793 | } | |
794 | ||
795 | // empty combobox should have some reasonable default size too | |
796 | if ( ret.x < 100 ) | |
797 | ret.x = 100; | |
9f884528 RD |
798 | |
799 | CacheBestSize(ret); | |
db434467 | 800 | return ret; |
f68586e5 VZ |
801 | } |
802 | ||
9d522606 RD |
803 | // static |
804 | wxVisualAttributes | |
805 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
806 | { | |
807 | return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true); | |
808 | } | |
809 | ||
dcf924a3 | 810 | #endif |