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