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