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