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