]>
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 |
805dd538 | 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 | |
159b66c0 RR |
57 | if (combo->m_prevSelection != curSelection) |
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 ); |
31528cd3 | 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 | |
fd0eed64 | 174 | PostCreation(); |
e8e24dfa | 175 | InheritAttributes(); |
53010e52 | 176 | |
461573cc | 177 | ConnectWidget( combo->button ); |
805dd538 | 178 | |
461573cc RR |
179 | // MSW's combo box shows the value and the selection is -1 |
180 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
181 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
805dd538 | 182 | |
a260fe6a | 183 | if (style & wxCB_READONLY) |
461573cc | 184 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
a260fe6a | 185 | |
461573cc RR |
186 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
187 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
188 | ||
189 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
190 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
805dd538 | 191 | |
db434467 RR |
192 | wxSize size_best( DoGetBestSize() ); |
193 | wxSize new_size( size ); | |
194 | if (new_size.x == -1) | |
195 | new_size.x = size_best.x; | |
196 | if (new_size.y == -1) | |
197 | new_size.y = size_best.y; | |
198 | if (new_size.y > size_best.y) | |
199 | new_size.y = size_best.y; | |
200 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
4494ad58 | 201 | { |
db434467 | 202 | SetSize( new_size.x, new_size.y ); |
30ed6e5c | 203 | |
4494ad58 RR |
204 | // This is required for tool bar support |
205 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); | |
206 | } | |
207 | ||
fd0eed64 | 208 | Show( TRUE ); |
805dd538 | 209 | |
fd0eed64 RR |
210 | return TRUE; |
211 | } | |
212 | ||
213 | wxComboBox::~wxComboBox() | |
214 | { | |
222ed1d6 | 215 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
216 | while (node) |
217 | { | |
b1d4dd7a | 218 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 219 | if (cd) delete cd; |
b1d4dd7a | 220 | node = node->GetNext(); |
fd0eed64 | 221 | } |
7d6d2cd4 RR |
222 | m_clientObjectList.Clear(); |
223 | ||
fd0eed64 | 224 | m_clientDataList.Clear(); |
6de97a3b | 225 | } |
53010e52 | 226 | |
2b5f62a0 VZ |
227 | void wxComboBox::SetFocus() |
228 | { | |
229 | if ( m_hasFocus ) | |
230 | { | |
231 | // don't do anything if we already have focus | |
232 | return; | |
233 | } | |
234 | ||
235 | gtk_widget_grab_focus( m_focusWidget ); | |
236 | } | |
237 | ||
6f6f938f | 238 | int wxComboBox::DoAppend( const wxString &item ) |
53010e52 | 239 | { |
2a68b7a0 | 240 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 241 | |
461573cc | 242 | DisableEvents(); |
30ed6e5c | 243 | |
fd0eed64 | 244 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 245 | |
fab591c5 | 246 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
805dd538 | 247 | |
ec5d85fb RR |
248 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
249 | ||
2b07d713 RR |
250 | if (GTK_WIDGET_REALIZED(m_widget)) |
251 | { | |
252 | gtk_widget_realize( list_item ); | |
253 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
254 | ||
255 | if (m_widgetStyle) ApplyWidgetStyle(); | |
256 | } | |
805dd538 | 257 | |
fd0eed64 | 258 | gtk_widget_show( list_item ); |
30ed6e5c | 259 | |
6f6f938f | 260 | const int count = GetCount(); |
53010e52 | 261 | |
6f6f938f | 262 | if ( (int)m_clientDataList.GetCount() < count ) |
f5e27805 | 263 | m_clientDataList.Append( (wxObject*) NULL ); |
6f6f938f | 264 | if ( (int)m_clientObjectList.GetCount() < count ) |
f5e27805 | 265 | m_clientObjectList.Append( (wxObject*) NULL ); |
805dd538 | 266 | |
6f6f938f | 267 | EnableEvents(); |
805dd538 | 268 | |
6f6f938f | 269 | return count - 1; |
fd0eed64 RR |
270 | } |
271 | ||
6f6f938f | 272 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
243dbf1a | 273 | { |
708c45a6 VZ |
274 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
275 | wxT("can't insert into sorted list")); | |
276 | ||
277 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
243dbf1a VZ |
278 | |
279 | int count = GetCount(); | |
6f6f938f VZ |
280 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
281 | ||
243dbf1a | 282 | if (pos == count) |
6f6f938f | 283 | return Append(item); |
243dbf1a VZ |
284 | |
285 | DisableEvents(); | |
286 | ||
287 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
288 | ||
289 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
290 | ||
291 | GList *gitem_list = g_list_alloc (); | |
292 | gitem_list->data = list_item; | |
293 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
294 | ||
295 | if (GTK_WIDGET_REALIZED(m_widget)) | |
296 | { | |
297 | gtk_widget_realize( list_item ); | |
298 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
299 | ||
708c45a6 VZ |
300 | if (m_widgetStyle) |
301 | ApplyWidgetStyle(); | |
243dbf1a VZ |
302 | } |
303 | ||
304 | gtk_widget_show( list_item ); | |
305 | ||
6f6f938f | 306 | count = GetCount(); |
243dbf1a | 307 | |
6f6f938f | 308 | if ( (int)m_clientDataList.GetCount() < count ) |
243dbf1a | 309 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
6f6f938f | 310 | if ( (int)m_clientObjectList.GetCount() < count ) |
243dbf1a VZ |
311 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
312 | ||
6f6f938f | 313 | EnableEvents(); |
243dbf1a | 314 | |
6f6f938f | 315 | return pos; |
243dbf1a VZ |
316 | } |
317 | ||
6f6f938f | 318 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 319 | { |
223d09f6 | 320 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 321 | |
222ed1d6 | 322 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
fd0eed64 | 323 | if (!node) return; |
805dd538 | 324 | |
f5e27805 | 325 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 326 | } |
53010e52 | 327 | |
6f6f938f | 328 | void* wxComboBox::DoGetItemClientData( int n ) const |
53010e52 | 329 | { |
223d09f6 | 330 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
805dd538 | 331 | |
222ed1d6 | 332 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
805dd538 | 333 | |
30ed6e5c | 334 | return node ? node->GetData() : NULL; |
fd0eed64 RR |
335 | } |
336 | ||
6f6f938f | 337 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 338 | { |
223d09f6 | 339 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 340 | |
222ed1d6 | 341 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
fd0eed64 | 342 | if (!node) return; |
805dd538 | 343 | |
e94e2e95 | 344 | // wxItemContainer already deletes data for us |
805dd538 | 345 | |
fd0eed64 | 346 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 347 | } |
53010e52 | 348 | |
6f6f938f | 349 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
53010e52 | 350 | { |
223d09f6 | 351 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
805dd538 | 352 | |
222ed1d6 | 353 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
805dd538 | 354 | |
30ed6e5c | 355 | return node ? (wxClientData*) node->GetData() : NULL; |
fd0eed64 RR |
356 | } |
357 | ||
358 | void wxComboBox::Clear() | |
359 | { | |
223d09f6 | 360 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 361 | |
461573cc | 362 | DisableEvents(); |
30ed6e5c | 363 | |
fd0eed64 | 364 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
6f6f938f | 365 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
805dd538 | 366 | |
222ed1d6 | 367 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
368 | while (node) |
369 | { | |
b1d4dd7a | 370 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 371 | if (cd) delete cd; |
b1d4dd7a | 372 | node = node->GetNext(); |
fd0eed64 | 373 | } |
f5e27805 | 374 | m_clientObjectList.Clear(); |
805dd538 | 375 | |
fd0eed64 | 376 | m_clientDataList.Clear(); |
30ed6e5c | 377 | |
461573cc | 378 | EnableEvents(); |
6de97a3b | 379 | } |
53010e52 | 380 | |
fd0eed64 | 381 | void wxComboBox::Delete( int n ) |
53010e52 | 382 | { |
223d09f6 | 383 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 384 | |
fd0eed64 | 385 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
805dd538 | 386 | |
fd0eed64 | 387 | GList *child = g_list_nth( listbox->children, n ); |
805dd538 | 388 | |
fd0eed64 RR |
389 | if (!child) |
390 | { | |
223d09f6 | 391 | wxFAIL_MSG(wxT("wrong index")); |
fd0eed64 RR |
392 | return; |
393 | } | |
805dd538 | 394 | |
461573cc | 395 | DisableEvents(); |
30ed6e5c | 396 | |
bbe0af5b | 397 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
398 | gtk_list_remove_items( listbox, list ); |
399 | g_list_free( list ); | |
805dd538 | 400 | |
222ed1d6 | 401 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
f5e27805 | 402 | if (node) |
fd0eed64 | 403 | { |
b1d4dd7a | 404 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 405 | if (cd) delete cd; |
222ed1d6 | 406 | m_clientObjectList.Erase( node ); |
f5e27805 | 407 | } |
805dd538 | 408 | |
b1d4dd7a | 409 | node = m_clientDataList.Item( n ); |
f5e27805 | 410 | if (node) |
222ed1d6 MB |
411 | m_clientDataList.Erase( node ); |
412 | ||
461573cc RR |
413 | EnableEvents(); |
414 | } | |
415 | ||
416 | void wxComboBox::SetString(int n, const wxString &text) | |
417 | { | |
418 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
419 | ||
420 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
421 | ||
422 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
423 | if (child) | |
424 | { | |
425 | GtkBin *bin = GTK_BIN( child->data ); | |
426 | GtkLabel *label = GTK_LABEL( bin->child ); | |
427 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
428 | } | |
429 | else | |
430 | { | |
431 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
fd0eed64 | 432 | } |
6de97a3b | 433 | } |
53010e52 | 434 | |
6f6f938f | 435 | int wxComboBox::FindString( const wxString &item ) const |
53010e52 | 436 | { |
223d09f6 | 437 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 438 | |
fd0eed64 | 439 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 440 | |
53010e52 RR |
441 | GList *child = GTK_LIST(list)->children; |
442 | int count = 0; | |
443 | while (child) | |
444 | { | |
fd0eed64 RR |
445 | GtkBin *bin = GTK_BIN( child->data ); |
446 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2b5f62a0 VZ |
447 | #ifdef __WXGTK20__ |
448 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
449 | #else | |
450 | wxString str( label->label ); | |
451 | #endif | |
452 | if (item == str) | |
7cf8cb48 | 453 | return count; |
30ed6e5c | 454 | |
fd0eed64 RR |
455 | count++; |
456 | child = child->next; | |
457 | } | |
805dd538 | 458 | |
7cf8cb48 | 459 | return wxNOT_FOUND; |
fd0eed64 RR |
460 | } |
461 | ||
462 | int wxComboBox::GetSelection() const | |
463 | { | |
223d09f6 | 464 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 465 | |
fd0eed64 | 466 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 467 | |
fd0eed64 RR |
468 | GList *selection = GTK_LIST(list)->selection; |
469 | if (selection) | |
470 | { | |
471 | GList *child = GTK_LIST(list)->children; | |
472 | int count = 0; | |
473 | while (child) | |
474 | { | |
475 | if (child->data == selection->data) return count; | |
476 | count++; | |
477 | child = child->next; | |
478 | } | |
6de97a3b | 479 | } |
805dd538 | 480 | |
fd0eed64 | 481 | return -1; |
6de97a3b | 482 | } |
53010e52 | 483 | |
debe6624 | 484 | wxString wxComboBox::GetString( int n ) const |
53010e52 | 485 | { |
223d09f6 | 486 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 487 | |
fd0eed64 | 488 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 489 | |
7cf8cb48 | 490 | wxString str; |
fd0eed64 RR |
491 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
492 | if (child) | |
493 | { | |
494 | GtkBin *bin = GTK_BIN( child->data ); | |
495 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2e1d7104 | 496 | #ifdef __WXGTK20__ |
2b5f62a0 | 497 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
2e1d7104 RR |
498 | #else |
499 | str = wxString( label->label ); | |
500 | #endif | |
7cf8cb48 VZ |
501 | } |
502 | else | |
503 | { | |
223d09f6 | 504 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
fd0eed64 | 505 | } |
805dd538 | 506 | |
7cf8cb48 | 507 | return str; |
6de97a3b | 508 | } |
53010e52 | 509 | |
fd0eed64 | 510 | wxString wxComboBox::GetStringSelection() const |
53010e52 | 511 | { |
223d09f6 | 512 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 513 | |
fd0eed64 | 514 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 515 | |
fd0eed64 RR |
516 | GList *selection = GTK_LIST(list)->selection; |
517 | if (selection) | |
518 | { | |
519 | GtkBin *bin = GTK_BIN( selection->data ); | |
2b5f62a0 VZ |
520 | GtkLabel *label = GTK_LABEL( bin->child ); |
521 | #ifdef __WXGTK20__ | |
522 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
523 | #else | |
524 | wxString tmp( label->label ); | |
525 | #endif | |
fd0eed64 RR |
526 | return tmp; |
527 | } | |
805dd538 | 528 | |
223d09f6 | 529 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
805dd538 | 530 | |
223d09f6 | 531 | return wxT(""); |
6de97a3b | 532 | } |
53010e52 | 533 | |
6f6f938f | 534 | int wxComboBox::GetCount() const |
53010e52 | 535 | { |
223d09f6 | 536 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
805dd538 | 537 | |
fd0eed64 | 538 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 539 | |
fd0eed64 RR |
540 | GList *child = GTK_LIST(list)->children; |
541 | int count = 0; | |
542 | while (child) { count++; child = child->next; } | |
543 | return count; | |
6de97a3b | 544 | } |
53010e52 | 545 | |
debe6624 | 546 | void wxComboBox::SetSelection( int n ) |
53010e52 | 547 | { |
223d09f6 | 548 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 549 | |
953704c1 RR |
550 | DisableEvents(); |
551 | ||
fd0eed64 | 552 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
159b66c0 | 553 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
fd0eed64 | 554 | gtk_list_select_item( GTK_LIST(list), n ); |
159b66c0 | 555 | m_prevSelection = n; |
953704c1 RR |
556 | |
557 | EnableEvents(); | |
6de97a3b | 558 | } |
53010e52 | 559 | |
47908e25 RR |
560 | void wxComboBox::SetStringSelection( const wxString &string ) |
561 | { | |
223d09f6 | 562 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 563 | |
fd0eed64 RR |
564 | int res = FindString( string ); |
565 | if (res == -1) return; | |
566 | SetSelection( res ); | |
6de97a3b | 567 | } |
47908e25 | 568 | |
fd0eed64 | 569 | wxString wxComboBox::GetValue() const |
53010e52 | 570 | { |
2e1d7104 RR |
571 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
572 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
573 | ||
30ed6e5c | 574 | #if 0 |
2e1d7104 RR |
575 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
576 | { | |
577 | wxChar c = tmp[i]; | |
578 | printf( "%d ", (int) (c) ); | |
579 | } | |
580 | printf( "\n" ); | |
581 | #endif | |
30ed6e5c | 582 | |
fd0eed64 | 583 | return tmp; |
6de97a3b | 584 | } |
53010e52 RR |
585 | |
586 | void wxComboBox::SetValue( const wxString& value ) | |
587 | { | |
223d09f6 | 588 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 589 | |
fd0eed64 | 590 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 591 | wxString tmp = wxT(""); |
fd0eed64 | 592 | if (!value.IsNull()) tmp = value; |
fab591c5 | 593 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
6de97a3b | 594 | } |
53010e52 | 595 | |
fd0eed64 | 596 | void wxComboBox::Copy() |
53010e52 | 597 | { |
223d09f6 | 598 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 599 | |
fd0eed64 | 600 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 601 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 602 | } |
53010e52 | 603 | |
fd0eed64 | 604 | void wxComboBox::Cut() |
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_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 610 | } |
53010e52 | 611 | |
fd0eed64 | 612 | void wxComboBox::Paste() |
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_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 618 | } |
53010e52 | 619 | |
debe6624 | 620 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 621 | { |
223d09f6 | 622 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 623 | |
6f6f938f VZ |
624 | if ( pos == GetLastPosition() ) |
625 | pos = -1; | |
626 | ||
fd0eed64 | 627 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 628 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 629 | } |
53010e52 | 630 | |
fd0eed64 | 631 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 632 | { |
9e691f46 | 633 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 634 | } |
53010e52 | 635 | |
fd0eed64 | 636 | long wxComboBox::GetLastPosition() const |
53010e52 | 637 | { |
fd0eed64 RR |
638 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
639 | int pos = GTK_ENTRY(entry)->text_length; | |
640 | return (long) pos-1; | |
6de97a3b | 641 | } |
53010e52 | 642 | |
debe6624 | 643 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 644 | { |
223d09f6 | 645 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 646 | |
fd0eed64 RR |
647 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
648 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
649 | if (value.IsNull()) return; | |
650 | gint pos = (gint)to; | |
30ed6e5c | 651 | |
2e1d7104 RR |
652 | #if wxUSE_UNICODE |
653 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
654 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
655 | #else | |
656 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
657 | #endif | |
6de97a3b | 658 | } |
53010e52 | 659 | |
20d10ee1 | 660 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 661 | { |
20d10ee1 VZ |
662 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
663 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 664 | } |
53010e52 | 665 | |
20d10ee1 | 666 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 667 | { |
20d10ee1 VZ |
668 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
669 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
670 | } |
671 | ||
8a85884a VZ |
672 | void wxComboBox::OnChar( wxKeyEvent &event ) |
673 | { | |
12a3f227 | 674 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 675 | { |
461573cc RR |
676 | // GTK automatically selects an item if its in the list |
677 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
678 | event.SetString( GetValue() ); | |
679 | event.SetInt( GetSelection() ); | |
680 | event.SetEventObject( this ); | |
3352cfff RR |
681 | |
682 | if (!GetEventHandler()->ProcessEvent( event )) | |
683 | { | |
684 | // This will invoke the dialog default action, such | |
685 | // as the clicking the default button. | |
686 | ||
687 | wxWindow *top_frame = m_parent; | |
688 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
689 | top_frame = top_frame->GetParent(); | |
690 | ||
691 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
692 | { | |
693 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
694 | ||
695 | if (window->default_widget) | |
696 | gtk_widget_activate (window->default_widget); | |
697 | } | |
698 | } | |
30ed6e5c | 699 | |
461573cc RR |
700 | // Catch GTK event so that GTK doesn't open the drop |
701 | // down list upon RETURN. | |
0878fb4c | 702 | return; |
8a85884a | 703 | } |
30ed6e5c | 704 | |
7cf8cb48 | 705 | event.Skip(); |
8a85884a VZ |
706 | } |
707 | ||
953704c1 RR |
708 | void wxComboBox::DisableEvents() |
709 | { | |
461573cc RR |
710 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
711 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
712 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
713 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
714 | } |
715 | ||
716 | void wxComboBox::EnableEvents() | |
717 | { | |
461573cc RR |
718 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
719 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
720 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
721 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
722 | } |
723 | ||
b4071e91 RR |
724 | void wxComboBox::OnSize( wxSizeEvent &event ) |
725 | { | |
f03fc89f | 726 | event.Skip(); |
31528cd3 | 727 | |
b02da6b1 | 728 | #if 0 |
fd0eed64 RR |
729 | int w = 21; |
730 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
805dd538 | 731 | |
fd0eed64 RR |
732 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
733 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
b02da6b1 | 734 | #endif // 0 |
6de97a3b | 735 | } |
53010e52 | 736 | |
58614078 | 737 | void wxComboBox::ApplyWidgetStyle() |
868a2826 | 738 | { |
fd0eed64 | 739 | SetWidgetStyle(); |
805dd538 | 740 | |
72a16063 | 741 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
fd0eed64 RR |
742 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
743 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
805dd538 | 744 | |
fd0eed64 RR |
745 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
746 | GList *child = list->children; | |
747 | while (child) | |
748 | { | |
749 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
805dd538 | 750 | |
fd0eed64 RR |
751 | GtkBin *bin = GTK_BIN(child->data); |
752 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
805dd538 | 753 | |
fd0eed64 RR |
754 | child = child->next; |
755 | } | |
868a2826 | 756 | } |
b4071e91 | 757 | |
fd0eed64 | 758 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 759 | { |
fd0eed64 | 760 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
761 | } |
762 | ||
b4071e91 RR |
763 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
764 | { | |
fd0eed64 RR |
765 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
766 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 767 | } |
ac57418f | 768 | |
f68586e5 VZ |
769 | wxSize wxComboBox::DoGetBestSize() const |
770 | { | |
db434467 | 771 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
772 | |
773 | // we know better our horizontal extent: it depends on the longest string | |
774 | // in the combobox | |
775 | ret.x = 0; | |
776 | if ( m_widget ) | |
777 | { | |
60d85ccb RR |
778 | int width; |
779 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
780 | for ( size_t n = 0; n < count; n++ ) |
781 | { | |
2b1ff57f | 782 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
a6fc8ae3 VZ |
783 | if ( width > ret.x ) |
784 | ret.x = width; | |
785 | } | |
786 | } | |
787 | ||
788 | // empty combobox should have some reasonable default size too | |
789 | if ( ret.x < 100 ) | |
790 | ret.x = 100; | |
db434467 | 791 | return ret; |
f68586e5 VZ |
792 | } |
793 | ||
dcf924a3 | 794 | #endif |