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