]>
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 ) | |
370 | { | |
371 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); | |
372 | ||
373 | wxNode *node = m_clientDataList.Item( n ); | |
374 | if (!node) return NULL; | |
375 | ||
376 | return node->GetData(); | |
377 | } | |
378 | ||
379 | void wxComboBox::SetClientObject( int n, wxClientData* clientData ) | |
380 | { | |
381 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
382 | ||
383 | wxNode *node = m_clientObjectList.Item( n ); | |
384 | if (!node) return; | |
385 | ||
386 | wxClientData *cd = (wxClientData*) node->GetData(); | |
387 | if (cd) delete cd; | |
388 | ||
389 | node->SetData( (wxObject*) clientData ); | |
390 | } | |
391 | ||
392 | wxClientData* wxComboBox::GetClientObject( int n ) | |
393 | { | |
394 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); | |
395 | ||
396 | wxNode *node = m_clientObjectList.Item( n ); | |
397 | if (!node) return (wxClientData*) NULL; | |
398 | ||
399 | return (wxClientData*) node->GetData(); | |
400 | } | |
401 | ||
402 | void wxComboBox::Clear() | |
403 | { | |
404 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
405 | ||
406 | DisableEvents(); | |
407 | ||
408 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
409 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
410 | ||
411 | wxNode *node = m_clientObjectList.GetFirst(); | |
412 | while (node) | |
413 | { | |
414 | wxClientData *cd = (wxClientData*)node->GetData(); | |
415 | if (cd) delete cd; | |
416 | node = node->GetNext(); | |
417 | } | |
418 | m_clientObjectList.Clear(); | |
419 | ||
420 | m_clientDataList.Clear(); | |
421 | ||
422 | EnableEvents(); | |
423 | } | |
424 | ||
425 | void wxComboBox::Delete( int n ) | |
426 | { | |
427 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
428 | ||
429 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
430 | ||
431 | GList *child = g_list_nth( listbox->children, n ); | |
432 | ||
433 | if (!child) | |
434 | { | |
435 | wxFAIL_MSG(wxT("wrong index")); | |
436 | return; | |
437 | } | |
438 | ||
439 | DisableEvents(); | |
440 | ||
441 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
442 | gtk_list_remove_items( listbox, list ); | |
443 | g_list_free( list ); | |
444 | ||
445 | wxNode *node = m_clientObjectList.Item( n ); | |
446 | if (node) | |
447 | { | |
448 | wxClientData *cd = (wxClientData*)node->GetData(); | |
449 | if (cd) delete cd; | |
450 | m_clientObjectList.DeleteNode( node ); | |
451 | } | |
452 | ||
453 | node = m_clientDataList.Item( n ); | |
454 | if (node) | |
455 | m_clientDataList.DeleteNode( node ); | |
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") ); | |
476 | } | |
477 | } | |
478 | ||
479 | int wxComboBox::FindString( const wxString &item ) | |
480 | { | |
481 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
482 | ||
483 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
484 | ||
485 | GList *child = GTK_LIST(list)->children; | |
486 | int count = 0; | |
487 | while (child) | |
488 | { | |
489 | GtkBin *bin = GTK_BIN( child->data ); | |
490 | GtkLabel *label = GTK_LABEL( bin->child ); | |
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) | |
497 | return count; | |
498 | ||
499 | count++; | |
500 | child = child->next; | |
501 | } | |
502 | ||
503 | return wxNOT_FOUND; | |
504 | } | |
505 | ||
506 | int wxComboBox::GetSelection() const | |
507 | { | |
508 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
509 | ||
510 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
511 | ||
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 | } | |
523 | } | |
524 | ||
525 | return -1; | |
526 | } | |
527 | ||
528 | wxString wxComboBox::GetString( int n ) const | |
529 | { | |
530 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
531 | ||
532 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
533 | ||
534 | wxString str; | |
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 ); | |
540 | #ifdef __WXGTK20__ | |
541 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); | |
542 | #else | |
543 | str = wxString( label->label ); | |
544 | #endif | |
545 | } | |
546 | else | |
547 | { | |
548 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
549 | } | |
550 | ||
551 | return str; | |
552 | } | |
553 | ||
554 | wxString wxComboBox::GetStringSelection() const | |
555 | { | |
556 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
557 | ||
558 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
559 | ||
560 | GList *selection = GTK_LIST(list)->selection; | |
561 | if (selection) | |
562 | { | |
563 | GtkBin *bin = GTK_BIN( selection->data ); | |
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 | |
570 | return tmp; | |
571 | } | |
572 | ||
573 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); | |
574 | ||
575 | return wxT(""); | |
576 | } | |
577 | ||
578 | int wxComboBox::Number() const | |
579 | { | |
580 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); | |
581 | ||
582 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
583 | ||
584 | GList *child = GTK_LIST(list)->children; | |
585 | int count = 0; | |
586 | while (child) { count++; child = child->next; } | |
587 | return count; | |
588 | } | |
589 | ||
590 | void wxComboBox::SetSelection( int n ) | |
591 | { | |
592 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
593 | ||
594 | DisableEvents(); | |
595 | ||
596 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
597 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); | |
598 | gtk_list_select_item( GTK_LIST(list), n ); | |
599 | m_prevSelection = n; | |
600 | ||
601 | EnableEvents(); | |
602 | } | |
603 | ||
604 | void wxComboBox::SetStringSelection( const wxString &string ) | |
605 | { | |
606 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
607 | ||
608 | int res = FindString( string ); | |
609 | if (res == -1) return; | |
610 | SetSelection( res ); | |
611 | } | |
612 | ||
613 | wxString wxComboBox::GetValue() const | |
614 | { | |
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 | ||
627 | return tmp; | |
628 | } | |
629 | ||
630 | void wxComboBox::SetValue( const wxString& value ) | |
631 | { | |
632 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
633 | ||
634 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
635 | wxString tmp = wxT(""); | |
636 | if (!value.IsNull()) tmp = value; | |
637 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); | |
638 | } | |
639 | ||
640 | void wxComboBox::Copy() | |
641 | { | |
642 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
643 | ||
644 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
645 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
646 | } | |
647 | ||
648 | void wxComboBox::Cut() | |
649 | { | |
650 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
651 | ||
652 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
653 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
654 | } | |
655 | ||
656 | void wxComboBox::Paste() | |
657 | { | |
658 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
659 | ||
660 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
661 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); | |
662 | } | |
663 | ||
664 | void wxComboBox::SetInsertionPoint( long pos ) | |
665 | { | |
666 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
667 | ||
668 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
669 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); | |
670 | } | |
671 | ||
672 | void wxComboBox::SetInsertionPointEnd() | |
673 | { | |
674 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
675 | ||
676 | SetInsertionPoint( -1 ); | |
677 | } | |
678 | ||
679 | long wxComboBox::GetInsertionPoint() const | |
680 | { | |
681 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); | |
682 | } | |
683 | ||
684 | long wxComboBox::GetLastPosition() const | |
685 | { | |
686 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
687 | int pos = GTK_ENTRY(entry)->text_length; | |
688 | return (long) pos-1; | |
689 | } | |
690 | ||
691 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
692 | { | |
693 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
694 | ||
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; | |
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 | |
706 | } | |
707 | ||
708 | void wxComboBox::Remove(long from, long to) | |
709 | { | |
710 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
711 | ||
712 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
713 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
714 | } | |
715 | ||
716 | void wxComboBox::SetSelection( long from, long to ) | |
717 | { | |
718 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
719 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
720 | } | |
721 | ||
722 | void wxComboBox::SetEditable( bool editable ) | |
723 | { | |
724 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
725 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
726 | } | |
727 | ||
728 | void wxComboBox::OnChar( wxKeyEvent &event ) | |
729 | { | |
730 | if ( event.GetKeyCode() == WXK_RETURN ) | |
731 | { | |
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 ); | |
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 | } | |
755 | ||
756 | // Catch GTK event so that GTK doesn't open the drop | |
757 | // down list upon RETURN. | |
758 | return; | |
759 | } | |
760 | ||
761 | event.Skip(); | |
762 | } | |
763 | ||
764 | void wxComboBox::DisableEvents() | |
765 | { | |
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 ); | |
770 | } | |
771 | ||
772 | void wxComboBox::EnableEvents() | |
773 | { | |
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 ); | |
778 | } | |
779 | ||
780 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
781 | { | |
782 | event.Skip(); | |
783 | ||
784 | #if 0 | |
785 | int w = 21; | |
786 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
787 | ||
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 ); | |
790 | #endif // 0 | |
791 | } | |
792 | ||
793 | void wxComboBox::ApplyWidgetStyle() | |
794 | { | |
795 | SetWidgetStyle(); | |
796 | ||
797 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); | |
798 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); | |
799 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
800 | ||
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 ); | |
806 | ||
807 | GtkBin *bin = GTK_BIN(child->data); | |
808 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
809 | ||
810 | child = child->next; | |
811 | } | |
812 | } | |
813 | ||
814 | GtkWidget* wxComboBox::GetConnectWidget() | |
815 | { | |
816 | return GTK_COMBO(m_widget)->entry; | |
817 | } | |
818 | ||
819 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
820 | { | |
821 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
822 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
823 | } | |
824 | ||
825 | wxSize wxComboBox::DoGetBestSize() const | |
826 | { | |
827 | wxSize ret( wxControl::DoGetBestSize() ); | |
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 | { | |
834 | int width; | |
835 | size_t count = GetCount(); | |
836 | for ( size_t n = 0; n < count; n++ ) | |
837 | { | |
838 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
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; | |
847 | return ret; | |
848 | } | |
849 | ||
850 | #endif |