]>
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" | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | static void | |
43 | gtk_combo_clicked_callback( 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 | if (combo->m_alreadySent) | |
52 | { | |
53 | combo->m_alreadySent = FALSE; | |
54 | return; | |
55 | } | |
56 | ||
57 | combo->m_alreadySent = TRUE; | |
58 | ||
59 | int curSelection = combo->GetSelection(); | |
60 | ||
61 | if (combo->m_prevSelection != curSelection) | |
62 | { | |
63 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
64 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
65 | } | |
66 | ||
67 | combo->m_prevSelection = curSelection; | |
68 | ||
69 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
70 | event.SetInt( curSelection ); | |
71 | event.SetString( combo->GetStringSelection() ); | |
72 | event.SetEventObject( combo ); | |
73 | ||
74 | combo->GetEventHandler()->ProcessEvent( event ); | |
75 | } | |
76 | ||
77 | //----------------------------------------------------------------------------- | |
78 | // "changed" | |
79 | //----------------------------------------------------------------------------- | |
80 | ||
81 | static void | |
82 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
83 | { | |
84 | if (g_isIdle) wxapp_install_idle_handler(); | |
85 | ||
86 | if (!combo->m_hasVMT) return; | |
87 | ||
88 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
89 | event.SetString( combo->GetValue() ); | |
90 | event.SetEventObject( combo ); | |
91 | combo->GetEventHandler()->ProcessEvent( event ); | |
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 | ||
125 | // make it more useable | |
126 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); | |
127 | ||
128 | // and case-sensitive | |
129 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
130 | ||
131 | ||
132 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
133 | ||
134 | #ifndef __WXGTK20__ | |
135 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); | |
136 | #endif | |
137 | ||
138 | for (int i = 0; i < n; i++) | |
139 | { | |
140 | /* don't send first event, which GTK sends aways when | |
141 | inserting the first item */ | |
142 | m_alreadySent = TRUE; | |
143 | ||
144 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); | |
145 | ||
146 | m_clientDataList.Append( (wxObject*)NULL ); | |
147 | m_clientObjectList.Append( (wxObject*)NULL ); | |
148 | ||
149 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
150 | ||
151 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
152 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
153 | ||
154 | gtk_widget_show( list_item ); | |
155 | } | |
156 | ||
157 | m_parent->DoAddChild( this ); | |
158 | ||
159 | m_focusWidget = GTK_COMBO(m_widget)->entry; | |
160 | ||
161 | PostCreation(); | |
162 | ||
163 | ConnectWidget( GTK_COMBO(m_widget)->button ); | |
164 | ||
165 | if (!value.IsNull()) SetValue( value ); | |
166 | ||
167 | if (style & wxCB_READONLY) | |
168 | gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE ); | |
169 | ||
170 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
171 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
172 | ||
173 | wxSize size_best( DoGetBestSize() ); | |
174 | wxSize new_size( size ); | |
175 | if (new_size.x == -1) | |
176 | new_size.x = size_best.x; | |
177 | if (new_size.y == -1) | |
178 | new_size.y = size_best.y; | |
179 | if (new_size.y > size_best.y) | |
180 | new_size.y = size_best.y; | |
181 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
182 | { | |
183 | SetSize( new_size.x, new_size.y ); | |
184 | ||
185 | // This is required for tool bar support | |
186 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); | |
187 | } | |
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 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
228 | ||
229 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
230 | ||
231 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
232 | ||
233 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
234 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
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 | ||
247 | void wxComboBox::Append( const wxString &item ) | |
248 | { | |
249 | m_clientDataList.Append( (wxObject*) NULL ); | |
250 | m_clientObjectList.Append( (wxObject*) NULL ); | |
251 | ||
252 | AppendCommon( item ); | |
253 | } | |
254 | ||
255 | void wxComboBox::Append( const wxString &item, void *clientData ) | |
256 | { | |
257 | m_clientDataList.Append( (wxObject*) clientData ); | |
258 | m_clientObjectList.Append( (wxObject*)NULL ); | |
259 | ||
260 | AppendCommon( item ); | |
261 | } | |
262 | ||
263 | void wxComboBox::Append( const wxString &item, wxClientData *clientData ) | |
264 | { | |
265 | m_clientDataList.Append( (wxObject*) NULL ); | |
266 | m_clientObjectList.Append( (wxObject*) clientData ); | |
267 | ||
268 | AppendCommon( item ); | |
269 | } | |
270 | ||
271 | void wxComboBox::SetClientData( int n, void* clientData ) | |
272 | { | |
273 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
274 | ||
275 | wxNode *node = m_clientDataList.Item( n ); | |
276 | if (!node) return; | |
277 | ||
278 | node->SetData( (wxObject*) clientData ); | |
279 | } | |
280 | ||
281 | void* wxComboBox::GetClientData( int n ) | |
282 | { | |
283 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); | |
284 | ||
285 | wxNode *node = m_clientDataList.Item( n ); | |
286 | if (!node) return NULL; | |
287 | ||
288 | return node->GetData(); | |
289 | } | |
290 | ||
291 | void wxComboBox::SetClientObject( int n, wxClientData* clientData ) | |
292 | { | |
293 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
294 | ||
295 | wxNode *node = m_clientObjectList.Item( n ); | |
296 | if (!node) return; | |
297 | ||
298 | wxClientData *cd = (wxClientData*) node->GetData(); | |
299 | if (cd) delete cd; | |
300 | ||
301 | node->SetData( (wxObject*) clientData ); | |
302 | } | |
303 | ||
304 | wxClientData* wxComboBox::GetClientObject( int n ) | |
305 | { | |
306 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); | |
307 | ||
308 | wxNode *node = m_clientObjectList.Item( n ); | |
309 | if (!node) return (wxClientData*) NULL; | |
310 | ||
311 | return (wxClientData*) node->GetData(); | |
312 | } | |
313 | ||
314 | void wxComboBox::Clear() | |
315 | { | |
316 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
317 | ||
318 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
319 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
320 | ||
321 | wxNode *node = m_clientObjectList.GetFirst(); | |
322 | while (node) | |
323 | { | |
324 | wxClientData *cd = (wxClientData*)node->GetData(); | |
325 | if (cd) delete cd; | |
326 | node = node->GetNext(); | |
327 | } | |
328 | m_clientObjectList.Clear(); | |
329 | ||
330 | m_clientDataList.Clear(); | |
331 | } | |
332 | ||
333 | void wxComboBox::Delete( int n ) | |
334 | { | |
335 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
336 | ||
337 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
338 | ||
339 | GList *child = g_list_nth( listbox->children, n ); | |
340 | ||
341 | if (!child) | |
342 | { | |
343 | wxFAIL_MSG(wxT("wrong index")); | |
344 | return; | |
345 | } | |
346 | ||
347 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
348 | gtk_list_remove_items( listbox, list ); | |
349 | g_list_free( list ); | |
350 | ||
351 | wxNode *node = m_clientObjectList.Item( n ); | |
352 | if (node) | |
353 | { | |
354 | wxClientData *cd = (wxClientData*)node->GetData(); | |
355 | if (cd) delete cd; | |
356 | m_clientObjectList.DeleteNode( node ); | |
357 | } | |
358 | ||
359 | node = m_clientDataList.Item( n ); | |
360 | if (node) | |
361 | { | |
362 | m_clientDataList.DeleteNode( node ); | |
363 | } | |
364 | } | |
365 | ||
366 | int wxComboBox::FindString( const wxString &item ) | |
367 | { | |
368 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
369 | ||
370 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
371 | ||
372 | GList *child = GTK_LIST(list)->children; | |
373 | int count = 0; | |
374 | while (child) | |
375 | { | |
376 | GtkBin *bin = GTK_BIN( child->data ); | |
377 | GtkLabel *label = GTK_LABEL( bin->child ); | |
378 | #ifdef __WXGTK20__ | |
379 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
380 | #else | |
381 | wxString str( label->label ); | |
382 | #endif | |
383 | if (item == str) | |
384 | return count; | |
385 | ||
386 | count++; | |
387 | child = child->next; | |
388 | } | |
389 | ||
390 | return wxNOT_FOUND; | |
391 | } | |
392 | ||
393 | int wxComboBox::GetSelection() const | |
394 | { | |
395 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
396 | ||
397 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
398 | ||
399 | GList *selection = GTK_LIST(list)->selection; | |
400 | if (selection) | |
401 | { | |
402 | GList *child = GTK_LIST(list)->children; | |
403 | int count = 0; | |
404 | while (child) | |
405 | { | |
406 | if (child->data == selection->data) return count; | |
407 | count++; | |
408 | child = child->next; | |
409 | } | |
410 | } | |
411 | ||
412 | return -1; | |
413 | } | |
414 | ||
415 | wxString wxComboBox::GetString( int n ) const | |
416 | { | |
417 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
418 | ||
419 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
420 | ||
421 | wxString str; | |
422 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
423 | if (child) | |
424 | { | |
425 | GtkBin *bin = GTK_BIN( child->data ); | |
426 | GtkLabel *label = GTK_LABEL( bin->child ); | |
427 | #ifdef __WXGTK20__ | |
428 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); | |
429 | #else | |
430 | str = wxString( label->label ); | |
431 | #endif | |
432 | } | |
433 | else | |
434 | { | |
435 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
436 | } | |
437 | ||
438 | return str; | |
439 | } | |
440 | ||
441 | wxString wxComboBox::GetStringSelection() const | |
442 | { | |
443 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
444 | ||
445 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
446 | ||
447 | GList *selection = GTK_LIST(list)->selection; | |
448 | if (selection) | |
449 | { | |
450 | GtkBin *bin = GTK_BIN( selection->data ); | |
451 | GtkLabel *label = GTK_LABEL( bin->child ); | |
452 | #ifdef __WXGTK20__ | |
453 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
454 | #else | |
455 | wxString tmp( label->label ); | |
456 | #endif | |
457 | return tmp; | |
458 | } | |
459 | ||
460 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); | |
461 | ||
462 | return wxT(""); | |
463 | } | |
464 | ||
465 | int wxComboBox::Number() const | |
466 | { | |
467 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); | |
468 | ||
469 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
470 | ||
471 | GList *child = GTK_LIST(list)->children; | |
472 | int count = 0; | |
473 | while (child) { count++; child = child->next; } | |
474 | return count; | |
475 | } | |
476 | ||
477 | void wxComboBox::SetSelection( int n ) | |
478 | { | |
479 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
480 | ||
481 | DisableEvents(); | |
482 | ||
483 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
484 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); | |
485 | gtk_list_select_item( GTK_LIST(list), n ); | |
486 | m_prevSelection = n; | |
487 | ||
488 | EnableEvents(); | |
489 | } | |
490 | ||
491 | void wxComboBox::SetStringSelection( const wxString &string ) | |
492 | { | |
493 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
494 | ||
495 | int res = FindString( string ); | |
496 | if (res == -1) return; | |
497 | SetSelection( res ); | |
498 | } | |
499 | ||
500 | wxString wxComboBox::GetValue() const | |
501 | { | |
502 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
503 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
504 | ||
505 | #if 0 | |
506 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) | |
507 | { | |
508 | wxChar c = tmp[i]; | |
509 | printf( "%d ", (int) (c) ); | |
510 | } | |
511 | printf( "\n" ); | |
512 | #endif | |
513 | ||
514 | return tmp; | |
515 | } | |
516 | ||
517 | void wxComboBox::SetValue( const wxString& value ) | |
518 | { | |
519 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
520 | ||
521 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
522 | wxString tmp = wxT(""); | |
523 | if (!value.IsNull()) tmp = value; | |
524 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); | |
525 | } | |
526 | ||
527 | void wxComboBox::Copy() | |
528 | { | |
529 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
530 | ||
531 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
532 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
533 | } | |
534 | ||
535 | void wxComboBox::Cut() | |
536 | { | |
537 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
538 | ||
539 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
540 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
541 | } | |
542 | ||
543 | void wxComboBox::Paste() | |
544 | { | |
545 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
546 | ||
547 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
548 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); | |
549 | } | |
550 | ||
551 | void wxComboBox::SetInsertionPoint( long pos ) | |
552 | { | |
553 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
554 | ||
555 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
556 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); | |
557 | } | |
558 | ||
559 | void wxComboBox::SetInsertionPointEnd() | |
560 | { | |
561 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
562 | ||
563 | SetInsertionPoint( -1 ); | |
564 | } | |
565 | ||
566 | long wxComboBox::GetInsertionPoint() const | |
567 | { | |
568 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); | |
569 | } | |
570 | ||
571 | long wxComboBox::GetLastPosition() const | |
572 | { | |
573 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
574 | int pos = GTK_ENTRY(entry)->text_length; | |
575 | return (long) pos-1; | |
576 | } | |
577 | ||
578 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
579 | { | |
580 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
581 | ||
582 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
583 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
584 | if (value.IsNull()) return; | |
585 | gint pos = (gint)to; | |
586 | ||
587 | #if wxUSE_UNICODE | |
588 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
589 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
590 | #else | |
591 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
592 | #endif | |
593 | } | |
594 | ||
595 | void wxComboBox::Remove(long from, long to) | |
596 | { | |
597 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
598 | ||
599 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
600 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
601 | } | |
602 | ||
603 | void wxComboBox::SetSelection( long from, long to ) | |
604 | { | |
605 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
606 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
607 | } | |
608 | ||
609 | void wxComboBox::SetEditable( bool editable ) | |
610 | { | |
611 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
612 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
613 | } | |
614 | ||
615 | void wxComboBox::OnChar( wxKeyEvent &event ) | |
616 | { | |
617 | if ( event.GetKeyCode() == WXK_RETURN ) | |
618 | { | |
619 | wxString value = GetValue(); | |
620 | ||
621 | if ( Number() == 0 ) | |
622 | { | |
623 | // make Enter generate "selected" event if there is only one item | |
624 | // in the combobox - without it, it's impossible to select it at | |
625 | // all! | |
626 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() ); | |
627 | event.SetInt( 0 ); | |
628 | event.SetString( value ); | |
629 | event.SetEventObject( this ); | |
630 | GetEventHandler()->ProcessEvent( event ); | |
631 | } | |
632 | else | |
633 | { | |
634 | // add the item to the list if it's not there yet | |
635 | if ( FindString(value) == wxNOT_FOUND ) | |
636 | { | |
637 | Append(value); | |
638 | SetStringSelection(value); | |
639 | ||
640 | // and generate the selected event for it | |
641 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() ); | |
642 | event.SetInt( Number() - 1 ); | |
643 | event.SetString( value ); | |
644 | event.SetEventObject( this ); | |
645 | GetEventHandler()->ProcessEvent( event ); | |
646 | } | |
647 | ||
648 | // This will invoke the dialog default action, such | |
649 | // as the clicking the default button. | |
650 | ||
651 | wxWindow *top_frame = m_parent; | |
652 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
653 | top_frame = top_frame->GetParent(); | |
654 | ||
655 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
656 | { | |
657 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
658 | ||
659 | if (window->default_widget) | |
660 | { | |
661 | gtk_widget_activate (window->default_widget); | |
662 | return; | |
663 | } | |
664 | } | |
665 | ||
666 | return; | |
667 | } | |
668 | } | |
669 | ||
670 | event.Skip(); | |
671 | } | |
672 | ||
673 | void wxComboBox::DisableEvents() | |
674 | { | |
675 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
676 | GList *child = list->children; | |
677 | while (child) | |
678 | { | |
679 | gtk_signal_disconnect_by_func( GTK_OBJECT(child->data), | |
680 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
681 | ||
682 | child = child->next; | |
683 | } | |
684 | } | |
685 | ||
686 | void wxComboBox::EnableEvents() | |
687 | { | |
688 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
689 | GList *child = list->children; | |
690 | while (child) | |
691 | { | |
692 | gtk_signal_connect( GTK_OBJECT(child->data), "select", | |
693 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
694 | ||
695 | child = child->next; | |
696 | } | |
697 | } | |
698 | ||
699 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
700 | { | |
701 | event.Skip(); | |
702 | ||
703 | #if 0 | |
704 | int w = 21; | |
705 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
706 | ||
707 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); | |
708 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
709 | #endif // 0 | |
710 | } | |
711 | ||
712 | void wxComboBox::ApplyWidgetStyle() | |
713 | { | |
714 | SetWidgetStyle(); | |
715 | ||
716 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); | |
717 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); | |
718 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
719 | ||
720 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
721 | GList *child = list->children; | |
722 | while (child) | |
723 | { | |
724 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
725 | ||
726 | GtkBin *bin = GTK_BIN(child->data); | |
727 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
728 | ||
729 | child = child->next; | |
730 | } | |
731 | } | |
732 | ||
733 | GtkWidget* wxComboBox::GetConnectWidget() | |
734 | { | |
735 | return GTK_COMBO(m_widget)->entry; | |
736 | } | |
737 | ||
738 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
739 | { | |
740 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
741 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
742 | } | |
743 | ||
744 | wxSize wxComboBox::DoGetBestSize() const | |
745 | { | |
746 | wxSize ret( wxControl::DoGetBestSize() ); | |
747 | ||
748 | // we know better our horizontal extent: it depends on the longest string | |
749 | // in the combobox | |
750 | ret.x = 0; | |
751 | if ( m_widget ) | |
752 | { | |
753 | int width; | |
754 | size_t count = GetCount(); | |
755 | for ( size_t n = 0; n < count; n++ ) | |
756 | { | |
757 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
758 | if ( width > ret.x ) | |
759 | ret.x = width; | |
760 | } | |
761 | } | |
762 | ||
763 | // empty combobox should have some reasonable default size too | |
764 | if ( ret.x < 100 ) | |
765 | ret.x = 100; | |
766 | return ret; | |
767 | } | |
768 | ||
769 | #endif |