]>
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 | InheritAttributes(); | |
162 | ||
163 | ConnectWidget( combo->button ); | |
164 | ||
165 | // MSW's combo box shows the value and the selection is -1 | |
166 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
167 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
168 | ||
169 | if (style & wxCB_READONLY) | |
170 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); | |
171 | ||
172 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", | |
173 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
174 | ||
175 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
176 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
177 | ||
178 | wxSize size_best( DoGetBestSize() ); | |
179 | wxSize new_size( size ); | |
180 | if (new_size.x == -1) | |
181 | new_size.x = size_best.x; | |
182 | if (new_size.y == -1) | |
183 | new_size.y = size_best.y; | |
184 | if (new_size.y > size_best.y) | |
185 | new_size.y = size_best.y; | |
186 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
187 | { | |
188 | SetSize( new_size.x, new_size.y ); | |
189 | ||
190 | // This is required for tool bar support | |
191 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); | |
192 | } | |
193 | ||
194 | Show( TRUE ); | |
195 | ||
196 | return TRUE; | |
197 | } | |
198 | ||
199 | wxComboBox::~wxComboBox() | |
200 | { | |
201 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
202 | while (node) | |
203 | { | |
204 | wxClientData *cd = (wxClientData*)node->GetData(); | |
205 | if (cd) delete cd; | |
206 | node = node->GetNext(); | |
207 | } | |
208 | m_clientObjectList.Clear(); | |
209 | ||
210 | m_clientDataList.Clear(); | |
211 | } | |
212 | ||
213 | void wxComboBox::SetFocus() | |
214 | { | |
215 | if ( m_hasFocus ) | |
216 | { | |
217 | // don't do anything if we already have focus | |
218 | return; | |
219 | } | |
220 | ||
221 | gtk_widget_grab_focus( m_focusWidget ); | |
222 | } | |
223 | ||
224 | int wxComboBox::DoAppend( const wxString &item ) | |
225 | { | |
226 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
227 | ||
228 | DisableEvents(); | |
229 | ||
230 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
231 | ||
232 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
233 | ||
234 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
235 | ||
236 | if (GTK_WIDGET_REALIZED(m_widget)) | |
237 | { | |
238 | gtk_widget_realize( list_item ); | |
239 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
240 | ||
241 | if (m_widgetStyle) ApplyWidgetStyle(); | |
242 | } | |
243 | ||
244 | gtk_widget_show( list_item ); | |
245 | ||
246 | const int count = GetCount(); | |
247 | ||
248 | if ( (int)m_clientDataList.GetCount() < count ) | |
249 | m_clientDataList.Append( (wxObject*) NULL ); | |
250 | if ( (int)m_clientObjectList.GetCount() < count ) | |
251 | m_clientObjectList.Append( (wxObject*) NULL ); | |
252 | ||
253 | EnableEvents(); | |
254 | ||
255 | return count - 1; | |
256 | } | |
257 | ||
258 | int wxComboBox::DoInsert( const wxString &item, int pos ) | |
259 | { | |
260 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, | |
261 | wxT("can't insert into sorted list")); | |
262 | ||
263 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
264 | ||
265 | int count = GetCount(); | |
266 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); | |
267 | ||
268 | if (pos == count) | |
269 | return Append(item); | |
270 | ||
271 | DisableEvents(); | |
272 | ||
273 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
274 | ||
275 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
276 | ||
277 | GList *gitem_list = g_list_alloc (); | |
278 | gitem_list->data = list_item; | |
279 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
280 | ||
281 | if (GTK_WIDGET_REALIZED(m_widget)) | |
282 | { | |
283 | gtk_widget_realize( list_item ); | |
284 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
285 | ||
286 | if (m_widgetStyle) | |
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 | wxClientData *cd = (wxClientData*) node->GetData(); | |
331 | if (cd) delete cd; | |
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 | void wxComboBox::SetStringSelection( const wxString &string ) | |
548 | { | |
549 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
550 | ||
551 | int res = FindString( string ); | |
552 | if (res == -1) return; | |
553 | SetSelection( res ); | |
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::ApplyWidgetStyle() | |
725 | { | |
726 | SetWidgetStyle(); | |
727 | ||
728 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); | |
729 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); | |
730 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
731 | ||
732 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
733 | GList *child = list->children; | |
734 | while (child) | |
735 | { | |
736 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
737 | ||
738 | GtkBin *bin = GTK_BIN(child->data); | |
739 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
740 | ||
741 | child = child->next; | |
742 | } | |
743 | } | |
744 | ||
745 | GtkWidget* wxComboBox::GetConnectWidget() | |
746 | { | |
747 | return GTK_COMBO(m_widget)->entry; | |
748 | } | |
749 | ||
750 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
751 | { | |
752 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
753 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
754 | } | |
755 | ||
756 | wxSize wxComboBox::DoGetBestSize() const | |
757 | { | |
758 | wxSize ret( wxControl::DoGetBestSize() ); | |
759 | ||
760 | // we know better our horizontal extent: it depends on the longest string | |
761 | // in the combobox | |
762 | ret.x = 0; | |
763 | if ( m_widget ) | |
764 | { | |
765 | int width; | |
766 | size_t count = GetCount(); | |
767 | for ( size_t n = 0; n < count; n++ ) | |
768 | { | |
769 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
770 | if ( width > ret.x ) | |
771 | ret.x = width; | |
772 | } | |
773 | } | |
774 | ||
775 | // empty combobox should have some reasonable default size too | |
776 | if ( ret.x < 100 ) | |
777 | ret.x = 100; | |
778 | return ret; | |
779 | } | |
780 | ||
781 | #endif |