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