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