]>
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 | void wxComboBox::AppendCommon( const wxString &item ) | |
227 | { | |
228 | wxCHECK_RET( m_widget != NULL, 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 | ||
251 | void wxComboBox::Append( const wxString &item ) | |
252 | { | |
253 | m_clientDataList.Append( (wxObject*) NULL ); | |
254 | m_clientObjectList.Append( (wxObject*) NULL ); | |
255 | ||
256 | AppendCommon( item ); | |
257 | } | |
258 | ||
259 | void wxComboBox::Append( const wxString &item, void *clientData ) | |
260 | { | |
261 | m_clientDataList.Append( (wxObject*) clientData ); | |
262 | m_clientObjectList.Append( (wxObject*)NULL ); | |
263 | ||
264 | AppendCommon( item ); | |
265 | } | |
266 | ||
267 | void wxComboBox::Append( const wxString &item, wxClientData *clientData ) | |
268 | { | |
269 | m_clientDataList.Append( (wxObject*) NULL ); | |
270 | m_clientObjectList.Append( (wxObject*) clientData ); | |
271 | ||
272 | AppendCommon( item ); | |
273 | } | |
274 | ||
275 | void wxComboBox::InsertCommon( const wxString &item, int pos ) | |
276 | { | |
277 | wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); | |
278 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
279 | ||
280 | int count = GetCount(); | |
281 | wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); | |
282 | if (pos == count) | |
283 | { | |
284 | AppendCommon(item); | |
285 | return; | |
286 | } | |
287 | ||
288 | DisableEvents(); | |
289 | ||
290 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
291 | ||
292 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
293 | ||
294 | GList *gitem_list = g_list_alloc (); | |
295 | gitem_list->data = list_item; | |
296 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
297 | ||
298 | if (GTK_WIDGET_REALIZED(m_widget)) | |
299 | { | |
300 | gtk_widget_realize( list_item ); | |
301 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
302 | ||
303 | if (m_widgetStyle) ApplyWidgetStyle(); | |
304 | } | |
305 | ||
306 | gtk_widget_show( list_item ); | |
307 | ||
308 | EnableEvents(); | |
309 | } | |
310 | ||
311 | void wxComboBox::Insert( const wxString &item, int pos ) | |
312 | { | |
313 | wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); | |
314 | int count = GetCount(); | |
315 | wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); | |
316 | if (pos == count) | |
317 | { | |
318 | Append(item); | |
319 | return; | |
320 | } | |
321 | ||
322 | m_clientDataList.Insert( pos, (wxObject*) NULL ); | |
323 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); | |
324 | ||
325 | InsertCommon( item, pos ); | |
326 | } | |
327 | ||
328 | void wxComboBox::Insert( const wxString &item, int pos, void *clientData ) | |
329 | { | |
330 | wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); | |
331 | int count = GetCount(); | |
332 | wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); | |
333 | if (pos == count) | |
334 | { | |
335 | Append(item, clientData); | |
336 | return; | |
337 | } | |
338 | ||
339 | m_clientDataList.Insert( pos, (wxObject*) clientData ); | |
340 | m_clientObjectList.Insert( pos, (wxObject*)NULL ); | |
341 | ||
342 | InsertCommon( item, pos ); | |
343 | } | |
344 | ||
345 | void wxComboBox::Insert( const wxString &item, int pos, wxClientData *clientData ) | |
346 | { | |
347 | wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); | |
348 | int count = GetCount(); | |
349 | wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); | |
350 | if (pos == count) | |
351 | { | |
352 | Append(item, clientData); | |
353 | return; | |
354 | } | |
355 | ||
356 | m_clientDataList.Insert( pos, (wxObject*) NULL ); | |
357 | m_clientObjectList.Insert( pos, (wxObject*) clientData ); | |
358 | ||
359 | InsertCommon( item, pos ); | |
360 | } | |
361 | ||
362 | void wxComboBox::SetClientData( int n, void* clientData ) | |
363 | { | |
364 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
365 | ||
366 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
367 | if (!node) return; | |
368 | ||
369 | node->SetData( (wxObject*) clientData ); | |
370 | } | |
371 | ||
372 | void* wxComboBox::GetClientData( int n ) const | |
373 | { | |
374 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); | |
375 | ||
376 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
377 | ||
378 | return node ? node->GetData() : NULL; | |
379 | } | |
380 | ||
381 | void wxComboBox::SetClientObject( int n, wxClientData* clientData ) | |
382 | { | |
383 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
384 | ||
385 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
386 | if (!node) return; | |
387 | ||
388 | wxClientData *cd = (wxClientData*) node->GetData(); | |
389 | if (cd) delete cd; | |
390 | ||
391 | node->SetData( (wxObject*) clientData ); | |
392 | } | |
393 | ||
394 | wxClientData* wxComboBox::GetClientObject( int n ) const | |
395 | { | |
396 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); | |
397 | ||
398 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
399 | ||
400 | return node ? (wxClientData*) node->GetData() : NULL; | |
401 | } | |
402 | ||
403 | void wxComboBox::Clear() | |
404 | { | |
405 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
406 | ||
407 | DisableEvents(); | |
408 | ||
409 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
410 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
411 | ||
412 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
413 | while (node) | |
414 | { | |
415 | wxClientData *cd = (wxClientData*)node->GetData(); | |
416 | if (cd) delete cd; | |
417 | node = node->GetNext(); | |
418 | } | |
419 | m_clientObjectList.Clear(); | |
420 | ||
421 | m_clientDataList.Clear(); | |
422 | ||
423 | EnableEvents(); | |
424 | } | |
425 | ||
426 | void wxComboBox::Delete( int n ) | |
427 | { | |
428 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
429 | ||
430 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
431 | ||
432 | GList *child = g_list_nth( listbox->children, n ); | |
433 | ||
434 | if (!child) | |
435 | { | |
436 | wxFAIL_MSG(wxT("wrong index")); | |
437 | return; | |
438 | } | |
439 | ||
440 | DisableEvents(); | |
441 | ||
442 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
443 | gtk_list_remove_items( listbox, list ); | |
444 | g_list_free( list ); | |
445 | ||
446 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
447 | if (node) | |
448 | { | |
449 | wxClientData *cd = (wxClientData*)node->GetData(); | |
450 | if (cd) delete cd; | |
451 | m_clientObjectList.Erase( node ); | |
452 | } | |
453 | ||
454 | node = m_clientDataList.Item( n ); | |
455 | if (node) | |
456 | m_clientDataList.Erase( node ); | |
457 | ||
458 | EnableEvents(); | |
459 | } | |
460 | ||
461 | void wxComboBox::SetString(int n, const wxString &text) | |
462 | { | |
463 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
464 | ||
465 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
466 | ||
467 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
468 | if (child) | |
469 | { | |
470 | GtkBin *bin = GTK_BIN( child->data ); | |
471 | GtkLabel *label = GTK_LABEL( bin->child ); | |
472 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
473 | } | |
474 | else | |
475 | { | |
476 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
477 | } | |
478 | } | |
479 | ||
480 | int wxComboBox::FindString( const wxString &item ) | |
481 | { | |
482 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
483 | ||
484 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
485 | ||
486 | GList *child = GTK_LIST(list)->children; | |
487 | int count = 0; | |
488 | while (child) | |
489 | { | |
490 | GtkBin *bin = GTK_BIN( child->data ); | |
491 | GtkLabel *label = GTK_LABEL( bin->child ); | |
492 | #ifdef __WXGTK20__ | |
493 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
494 | #else | |
495 | wxString str( label->label ); | |
496 | #endif | |
497 | if (item == str) | |
498 | return count; | |
499 | ||
500 | count++; | |
501 | child = child->next; | |
502 | } | |
503 | ||
504 | return wxNOT_FOUND; | |
505 | } | |
506 | ||
507 | int wxComboBox::GetSelection() const | |
508 | { | |
509 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
510 | ||
511 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
512 | ||
513 | GList *selection = GTK_LIST(list)->selection; | |
514 | if (selection) | |
515 | { | |
516 | GList *child = GTK_LIST(list)->children; | |
517 | int count = 0; | |
518 | while (child) | |
519 | { | |
520 | if (child->data == selection->data) return count; | |
521 | count++; | |
522 | child = child->next; | |
523 | } | |
524 | } | |
525 | ||
526 | return -1; | |
527 | } | |
528 | ||
529 | wxString wxComboBox::GetString( int n ) const | |
530 | { | |
531 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
532 | ||
533 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
534 | ||
535 | wxString str; | |
536 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
537 | if (child) | |
538 | { | |
539 | GtkBin *bin = GTK_BIN( child->data ); | |
540 | GtkLabel *label = GTK_LABEL( bin->child ); | |
541 | #ifdef __WXGTK20__ | |
542 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); | |
543 | #else | |
544 | str = wxString( label->label ); | |
545 | #endif | |
546 | } | |
547 | else | |
548 | { | |
549 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
550 | } | |
551 | ||
552 | return str; | |
553 | } | |
554 | ||
555 | wxString wxComboBox::GetStringSelection() const | |
556 | { | |
557 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
558 | ||
559 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
560 | ||
561 | GList *selection = GTK_LIST(list)->selection; | |
562 | if (selection) | |
563 | { | |
564 | GtkBin *bin = GTK_BIN( selection->data ); | |
565 | GtkLabel *label = GTK_LABEL( bin->child ); | |
566 | #ifdef __WXGTK20__ | |
567 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
568 | #else | |
569 | wxString tmp( label->label ); | |
570 | #endif | |
571 | return tmp; | |
572 | } | |
573 | ||
574 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); | |
575 | ||
576 | return wxT(""); | |
577 | } | |
578 | ||
579 | int wxComboBox::Number() const | |
580 | { | |
581 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); | |
582 | ||
583 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
584 | ||
585 | GList *child = GTK_LIST(list)->children; | |
586 | int count = 0; | |
587 | while (child) { count++; child = child->next; } | |
588 | return count; | |
589 | } | |
590 | ||
591 | void wxComboBox::SetSelection( int n ) | |
592 | { | |
593 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
594 | ||
595 | DisableEvents(); | |
596 | ||
597 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
598 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); | |
599 | gtk_list_select_item( GTK_LIST(list), n ); | |
600 | m_prevSelection = n; | |
601 | ||
602 | EnableEvents(); | |
603 | } | |
604 | ||
605 | void wxComboBox::SetStringSelection( const wxString &string ) | |
606 | { | |
607 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
608 | ||
609 | int res = FindString( string ); | |
610 | if (res == -1) return; | |
611 | SetSelection( res ); | |
612 | } | |
613 | ||
614 | wxString wxComboBox::GetValue() const | |
615 | { | |
616 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
617 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
618 | ||
619 | #if 0 | |
620 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) | |
621 | { | |
622 | wxChar c = tmp[i]; | |
623 | printf( "%d ", (int) (c) ); | |
624 | } | |
625 | printf( "\n" ); | |
626 | #endif | |
627 | ||
628 | return tmp; | |
629 | } | |
630 | ||
631 | void wxComboBox::SetValue( const wxString& value ) | |
632 | { | |
633 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
634 | ||
635 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
636 | wxString tmp = wxT(""); | |
637 | if (!value.IsNull()) tmp = value; | |
638 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); | |
639 | } | |
640 | ||
641 | void wxComboBox::Copy() | |
642 | { | |
643 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
644 | ||
645 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
646 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
647 | } | |
648 | ||
649 | void wxComboBox::Cut() | |
650 | { | |
651 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
652 | ||
653 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
654 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
655 | } | |
656 | ||
657 | void wxComboBox::Paste() | |
658 | { | |
659 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
660 | ||
661 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
662 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); | |
663 | } | |
664 | ||
665 | void wxComboBox::SetInsertionPoint( long pos ) | |
666 | { | |
667 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
668 | ||
669 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
670 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); | |
671 | } | |
672 | ||
673 | void wxComboBox::SetInsertionPointEnd() | |
674 | { | |
675 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
676 | ||
677 | SetInsertionPoint( -1 ); | |
678 | } | |
679 | ||
680 | long wxComboBox::GetInsertionPoint() const | |
681 | { | |
682 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); | |
683 | } | |
684 | ||
685 | long wxComboBox::GetLastPosition() const | |
686 | { | |
687 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
688 | int pos = GTK_ENTRY(entry)->text_length; | |
689 | return (long) pos-1; | |
690 | } | |
691 | ||
692 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
693 | { | |
694 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
695 | ||
696 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
697 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
698 | if (value.IsNull()) return; | |
699 | gint pos = (gint)to; | |
700 | ||
701 | #if wxUSE_UNICODE | |
702 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
703 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
704 | #else | |
705 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
706 | #endif | |
707 | } | |
708 | ||
709 | void wxComboBox::Remove(long from, long to) | |
710 | { | |
711 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
712 | ||
713 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
714 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
715 | } | |
716 | ||
717 | void wxComboBox::SetSelection( long from, long to ) | |
718 | { | |
719 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
720 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
721 | } | |
722 | ||
723 | void wxComboBox::SetEditable( bool editable ) | |
724 | { | |
725 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
726 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
727 | } | |
728 | ||
729 | void wxComboBox::OnChar( wxKeyEvent &event ) | |
730 | { | |
731 | if ( event.GetKeyCode() == WXK_RETURN ) | |
732 | { | |
733 | // GTK automatically selects an item if its in the list | |
734 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
735 | event.SetString( GetValue() ); | |
736 | event.SetInt( GetSelection() ); | |
737 | event.SetEventObject( this ); | |
738 | ||
739 | if (!GetEventHandler()->ProcessEvent( event )) | |
740 | { | |
741 | // This will invoke the dialog default action, such | |
742 | // as the clicking the default button. | |
743 | ||
744 | wxWindow *top_frame = m_parent; | |
745 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
746 | top_frame = top_frame->GetParent(); | |
747 | ||
748 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
749 | { | |
750 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
751 | ||
752 | if (window->default_widget) | |
753 | gtk_widget_activate (window->default_widget); | |
754 | } | |
755 | } | |
756 | ||
757 | // Catch GTK event so that GTK doesn't open the drop | |
758 | // down list upon RETURN. | |
759 | return; | |
760 | } | |
761 | ||
762 | event.Skip(); | |
763 | } | |
764 | ||
765 | void wxComboBox::DisableEvents() | |
766 | { | |
767 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), | |
768 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
769 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
770 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
771 | } | |
772 | ||
773 | void wxComboBox::EnableEvents() | |
774 | { | |
775 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", | |
776 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
777 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
778 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
779 | } | |
780 | ||
781 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
782 | { | |
783 | event.Skip(); | |
784 | ||
785 | #if 0 | |
786 | int w = 21; | |
787 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
788 | ||
789 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); | |
790 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
791 | #endif // 0 | |
792 | } | |
793 | ||
794 | void wxComboBox::ApplyWidgetStyle() | |
795 | { | |
796 | SetWidgetStyle(); | |
797 | ||
798 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); | |
799 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); | |
800 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
801 | ||
802 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
803 | GList *child = list->children; | |
804 | while (child) | |
805 | { | |
806 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
807 | ||
808 | GtkBin *bin = GTK_BIN(child->data); | |
809 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
810 | ||
811 | child = child->next; | |
812 | } | |
813 | } | |
814 | ||
815 | GtkWidget* wxComboBox::GetConnectWidget() | |
816 | { | |
817 | return GTK_COMBO(m_widget)->entry; | |
818 | } | |
819 | ||
820 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
821 | { | |
822 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
823 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
824 | } | |
825 | ||
826 | wxSize wxComboBox::DoGetBestSize() const | |
827 | { | |
828 | wxSize ret( wxControl::DoGetBestSize() ); | |
829 | ||
830 | // we know better our horizontal extent: it depends on the longest string | |
831 | // in the combobox | |
832 | ret.x = 0; | |
833 | if ( m_widget ) | |
834 | { | |
835 | int width; | |
836 | size_t count = GetCount(); | |
837 | for ( size_t n = 0; n < count; n++ ) | |
838 | { | |
839 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
840 | if ( width > ret.x ) | |
841 | ret.x = width; | |
842 | } | |
843 | } | |
844 | ||
845 | // empty combobox should have some reasonable default size too | |
846 | if ( ret.x < 100 ) | |
847 | ret.x = 100; | |
848 | return ret; | |
849 | } | |
850 | ||
851 | #endif |