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