]>
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 | static int g_SelectionBeforePopup = -2; // -2 <=> the popup is hidden | |
42 | //----------------------------------------------------------------------------- | |
43 | // "changed" - typing and list item matches get changed, select-child | |
44 | // if it doesn't match an item then just get a single changed | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
47 | static void | |
48 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
49 | { | |
50 | if (g_isIdle) wxapp_install_idle_handler(); | |
51 | ||
52 | if (combo->m_ignoreNextUpdate) | |
53 | { | |
54 | combo->m_ignoreNextUpdate = false; | |
55 | return; | |
56 | } | |
57 | ||
58 | if (!combo->m_hasVMT) return; | |
59 | ||
60 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
61 | event.SetString( combo->GetValue() ); | |
62 | event.SetEventObject( combo ); | |
63 | combo->GetEventHandler()->ProcessEvent( event ); | |
64 | } | |
65 | ||
66 | static void | |
67 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
68 | { | |
69 | } | |
70 | ||
71 | static void | |
72 | gtk_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) | |
73 | { | |
74 | // when the popup is hidden, throw a SELECTED event only if the combobox | |
75 | // selection changed. | |
76 | int curSelection = combo->GetSelection(); | |
77 | if (g_SelectionBeforePopup != curSelection) | |
78 | { | |
79 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
80 | event.SetInt( curSelection ); | |
81 | event.SetString( combo->GetStringSelection() ); | |
82 | event.SetEventObject( combo ); | |
83 | combo->GetEventHandler()->ProcessEvent( event ); | |
84 | } | |
85 | ||
86 | // reset the selection flag to an identifiable value (-2 = hidden) | |
87 | g_SelectionBeforePopup = -2; | |
88 | } | |
89 | ||
90 | static void | |
91 | gtk_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) | |
92 | { | |
93 | // store the combobox selection value before the popup is shown | |
94 | // if there is no selection, combo->GetSelection() returns -1 | |
95 | g_SelectionBeforePopup = combo->GetSelection(); | |
96 | } | |
97 | ||
98 | //----------------------------------------------------------------------------- | |
99 | // "select-child" - click/cursor get select-child, changed, select-child | |
100 | //----------------------------------------------------------------------------- | |
101 | ||
102 | static void | |
103 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
104 | { | |
105 | if (g_isIdle) wxapp_install_idle_handler(); | |
106 | ||
107 | if (!combo->m_hasVMT) return; | |
108 | ||
109 | if (g_blockEventsOnDrag) return; | |
110 | ||
111 | int curSelection = combo->GetSelection(); | |
112 | ||
113 | if (combo->m_prevSelection == curSelection) return; | |
114 | ||
115 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
116 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
117 | ||
118 | combo->m_prevSelection = curSelection; | |
119 | ||
120 | // Quickly set the value of the combo box | |
121 | // as GTK+ does that only AFTER the event | |
122 | // is sent. | |
123 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), | |
124 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); | |
125 | combo->SetValue( combo->GetStringSelection() ); | |
126 | gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed", | |
127 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); | |
128 | ||
129 | // throw a SELECTED event only if the combobox popup is hidden (-2) | |
130 | // because when combobox popup is shown, gtk_combo_select_child_callback is | |
131 | // called each times the mouse is over an item with a pressed button so a lot | |
132 | // of SELECTED event could be generated if the user keep the mouse button down | |
133 | // and select other items ... | |
134 | if (g_SelectionBeforePopup == -2) | |
135 | { | |
136 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
137 | event.SetInt( curSelection ); | |
138 | event.SetString( combo->GetStringSelection() ); | |
139 | event.SetEventObject( combo ); | |
140 | combo->GetEventHandler()->ProcessEvent( event ); | |
141 | } | |
142 | ||
143 | // Now send the event ourselves | |
144 | wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
145 | event2.SetString( combo->GetValue() ); | |
146 | event2.SetEventObject( combo ); | |
147 | combo->GetEventHandler()->ProcessEvent( event2 ); | |
148 | } | |
149 | ||
150 | //----------------------------------------------------------------------------- | |
151 | // wxComboBox | |
152 | //----------------------------------------------------------------------------- | |
153 | ||
154 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
155 | ||
156 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) | |
157 | EVT_SIZE(wxComboBox::OnSize) | |
158 | EVT_CHAR(wxComboBox::OnChar) | |
159 | ||
160 | EVT_MENU(wxID_CUT, wxComboBox::OnCut) | |
161 | EVT_MENU(wxID_COPY, wxComboBox::OnCopy) | |
162 | EVT_MENU(wxID_PASTE, wxComboBox::OnPaste) | |
163 | EVT_MENU(wxID_UNDO, wxComboBox::OnUndo) | |
164 | EVT_MENU(wxID_REDO, wxComboBox::OnRedo) | |
165 | EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete) | |
166 | EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll) | |
167 | ||
168 | EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut) | |
169 | EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy) | |
170 | EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste) | |
171 | EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo) | |
172 | EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo) | |
173 | EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete) | |
174 | EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) | |
175 | END_EVENT_TABLE() | |
176 | ||
177 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, | |
178 | const wxString& value, | |
179 | const wxPoint& pos, const wxSize& size, | |
180 | const wxArrayString& choices, | |
181 | long style, const wxValidator& validator, | |
182 | const wxString& name ) | |
183 | { | |
184 | wxCArrayString chs(choices); | |
185 | ||
186 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
187 | chs.GetStrings(), style, validator, name ); | |
188 | } | |
189 | ||
190 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, | |
191 | const wxPoint& pos, const wxSize& size, | |
192 | int n, const wxString choices[], | |
193 | long style, const wxValidator& validator, | |
194 | const wxString& name ) | |
195 | { | |
196 | m_ignoreNextUpdate = false; | |
197 | m_needParent = true; | |
198 | m_acceptsFocus = true; | |
199 | m_prevSelection = 0; | |
200 | ||
201 | if (!PreCreation( parent, pos, size ) || | |
202 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
203 | { | |
204 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); | |
205 | return false; | |
206 | } | |
207 | ||
208 | m_widget = gtk_combo_new(); | |
209 | GtkCombo *combo = GTK_COMBO(m_widget); | |
210 | ||
211 | // Disable GTK's broken events ... | |
212 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); | |
213 | // ... and add surogate handler. | |
214 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", | |
215 | (GtkSignalFunc) gtk_dummy_callback, combo); | |
216 | ||
217 | // make it more useable | |
218 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); | |
219 | ||
220 | // and case-sensitive | |
221 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
222 | ||
223 | #ifdef __WXGTK20__ | |
224 | if (style & wxNO_BORDER) | |
225 | g_object_set( GTK_ENTRY( combo->entry ), "has-frame", FALSE, NULL ); | |
226 | #endif | |
227 | ||
228 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
229 | ||
230 | #ifndef __WXGTK20__ | |
231 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); | |
232 | #endif | |
233 | ||
234 | for (int i = 0; i < n; i++) | |
235 | { | |
236 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); | |
237 | ||
238 | m_clientDataList.Append( (wxObject*)NULL ); | |
239 | m_clientObjectList.Append( (wxObject*)NULL ); | |
240 | ||
241 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
242 | ||
243 | gtk_widget_show( list_item ); | |
244 | } | |
245 | ||
246 | m_parent->DoAddChild( this ); | |
247 | ||
248 | m_focusWidget = combo->entry; | |
249 | ||
250 | PostCreation(size); | |
251 | ||
252 | ConnectWidget( combo->button ); | |
253 | ||
254 | // MSW's combo box shows the value and the selection is -1 | |
255 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
256 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
257 | ||
258 | if (style & wxCB_READONLY) | |
259 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); | |
260 | ||
261 | // "show" and "hide" events are generated when user click on the combobox button which popups a list | |
262 | // this list is the "popwin" gtk widget | |
263 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide", | |
264 | GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this ); | |
265 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show", | |
266 | GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this ); | |
267 | ||
268 | gtk_signal_connect_after( GTK_OBJECT(combo->entry), "changed", | |
269 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
270 | ||
271 | gtk_signal_connect_after( GTK_OBJECT(combo->list), "select-child", | |
272 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
273 | ||
274 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
275 | ||
276 | // This is required for tool bar support | |
277 | // wxSize setsize = GetSize(); | |
278 | // gtk_widget_set_usize( m_widget, setsize.x, setsize.y ); | |
279 | ||
280 | return true; | |
281 | } | |
282 | ||
283 | wxComboBox::~wxComboBox() | |
284 | { | |
285 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
286 | while (node) | |
287 | { | |
288 | wxClientData *cd = (wxClientData*)node->GetData(); | |
289 | if (cd) delete cd; | |
290 | node = node->GetNext(); | |
291 | } | |
292 | m_clientObjectList.Clear(); | |
293 | ||
294 | m_clientDataList.Clear(); | |
295 | } | |
296 | ||
297 | void wxComboBox::SetFocus() | |
298 | { | |
299 | if ( m_hasFocus ) | |
300 | { | |
301 | // don't do anything if we already have focus | |
302 | return; | |
303 | } | |
304 | ||
305 | gtk_widget_grab_focus( m_focusWidget ); | |
306 | } | |
307 | ||
308 | int wxComboBox::DoAppend( const wxString &item ) | |
309 | { | |
310 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
311 | ||
312 | DisableEvents(); | |
313 | ||
314 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
315 | ||
316 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
317 | ||
318 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
319 | ||
320 | if (GTK_WIDGET_REALIZED(m_widget)) | |
321 | { | |
322 | gtk_widget_realize( list_item ); | |
323 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
324 | } | |
325 | ||
326 | // Apply current widget style to the new list_item | |
327 | GtkRcStyle *style = CreateWidgetStyle(); | |
328 | if (style) | |
329 | { | |
330 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); | |
331 | GtkBin *bin = GTK_BIN( list_item ); | |
332 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
333 | gtk_widget_modify_style( label, style ); | |
334 | gtk_rc_style_unref( style ); | |
335 | } | |
336 | ||
337 | gtk_widget_show( list_item ); | |
338 | ||
339 | const int count = GetCount(); | |
340 | ||
341 | if ( (int)m_clientDataList.GetCount() < count ) | |
342 | m_clientDataList.Append( (wxObject*) NULL ); | |
343 | if ( (int)m_clientObjectList.GetCount() < count ) | |
344 | m_clientObjectList.Append( (wxObject*) NULL ); | |
345 | ||
346 | EnableEvents(); | |
347 | ||
348 | InvalidateBestSize(); | |
349 | ||
350 | return count - 1; | |
351 | } | |
352 | ||
353 | int wxComboBox::DoInsert( const wxString &item, int pos ) | |
354 | { | |
355 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, | |
356 | wxT("can't insert into sorted list")); | |
357 | ||
358 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
359 | ||
360 | int count = GetCount(); | |
361 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); | |
362 | ||
363 | if (pos == count) | |
364 | return Append(item); | |
365 | ||
366 | DisableEvents(); | |
367 | ||
368 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
369 | ||
370 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
371 | ||
372 | GList *gitem_list = g_list_alloc (); | |
373 | gitem_list->data = list_item; | |
374 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
375 | ||
376 | if (GTK_WIDGET_REALIZED(m_widget)) | |
377 | { | |
378 | gtk_widget_realize( list_item ); | |
379 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
380 | ||
381 | ApplyWidgetStyle(); | |
382 | } | |
383 | ||
384 | gtk_widget_show( list_item ); | |
385 | ||
386 | count = GetCount(); | |
387 | ||
388 | if ( (int)m_clientDataList.GetCount() < count ) | |
389 | m_clientDataList.Insert( pos, (wxObject*) NULL ); | |
390 | if ( (int)m_clientObjectList.GetCount() < count ) | |
391 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); | |
392 | ||
393 | EnableEvents(); | |
394 | ||
395 | InvalidateBestSize(); | |
396 | ||
397 | return pos; | |
398 | } | |
399 | ||
400 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) | |
401 | { | |
402 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
403 | ||
404 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
405 | if (!node) return; | |
406 | ||
407 | node->SetData( (wxObject*) clientData ); | |
408 | } | |
409 | ||
410 | void* wxComboBox::DoGetItemClientData( int n ) const | |
411 | { | |
412 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); | |
413 | ||
414 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
415 | ||
416 | return node ? node->GetData() : NULL; | |
417 | } | |
418 | ||
419 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) | |
420 | { | |
421 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
422 | ||
423 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
424 | if (!node) return; | |
425 | ||
426 | // wxItemContainer already deletes data for us | |
427 | ||
428 | node->SetData( (wxObject*) clientData ); | |
429 | } | |
430 | ||
431 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const | |
432 | { | |
433 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); | |
434 | ||
435 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
436 | ||
437 | return node ? (wxClientData*) node->GetData() : NULL; | |
438 | } | |
439 | ||
440 | void wxComboBox::Clear() | |
441 | { | |
442 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
443 | ||
444 | DisableEvents(); | |
445 | ||
446 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
447 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); | |
448 | ||
449 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
450 | while (node) | |
451 | { | |
452 | wxClientData *cd = (wxClientData*)node->GetData(); | |
453 | if (cd) delete cd; | |
454 | node = node->GetNext(); | |
455 | } | |
456 | m_clientObjectList.Clear(); | |
457 | ||
458 | m_clientDataList.Clear(); | |
459 | ||
460 | EnableEvents(); | |
461 | ||
462 | InvalidateBestSize(); | |
463 | } | |
464 | ||
465 | void wxComboBox::Delete( int n ) | |
466 | { | |
467 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
468 | ||
469 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
470 | ||
471 | GList *child = g_list_nth( listbox->children, n ); | |
472 | ||
473 | if (!child) | |
474 | { | |
475 | wxFAIL_MSG(wxT("wrong index")); | |
476 | return; | |
477 | } | |
478 | ||
479 | DisableEvents(); | |
480 | ||
481 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
482 | gtk_list_remove_items( listbox, list ); | |
483 | g_list_free( list ); | |
484 | ||
485 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
486 | if (node) | |
487 | { | |
488 | wxClientData *cd = (wxClientData*)node->GetData(); | |
489 | if (cd) delete cd; | |
490 | m_clientObjectList.Erase( node ); | |
491 | } | |
492 | ||
493 | node = m_clientDataList.Item( n ); | |
494 | if (node) | |
495 | m_clientDataList.Erase( node ); | |
496 | ||
497 | EnableEvents(); | |
498 | ||
499 | InvalidateBestSize(); | |
500 | } | |
501 | ||
502 | void wxComboBox::SetString(int n, const wxString &text) | |
503 | { | |
504 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
505 | ||
506 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
507 | ||
508 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
509 | if (child) | |
510 | { | |
511 | GtkBin *bin = GTK_BIN( child->data ); | |
512 | GtkLabel *label = GTK_LABEL( bin->child ); | |
513 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
514 | } | |
515 | else | |
516 | { | |
517 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
518 | } | |
519 | ||
520 | InvalidateBestSize(); | |
521 | } | |
522 | ||
523 | int wxComboBox::FindString( const wxString &item ) const | |
524 | { | |
525 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
526 | ||
527 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
528 | ||
529 | GList *child = GTK_LIST(list)->children; | |
530 | int count = 0; | |
531 | while (child) | |
532 | { | |
533 | GtkBin *bin = GTK_BIN( child->data ); | |
534 | GtkLabel *label = GTK_LABEL( bin->child ); | |
535 | #ifdef __WXGTK20__ | |
536 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
537 | #else | |
538 | wxString str( label->label ); | |
539 | #endif | |
540 | if (item == str) | |
541 | return count; | |
542 | ||
543 | count++; | |
544 | child = child->next; | |
545 | } | |
546 | ||
547 | return wxNOT_FOUND; | |
548 | } | |
549 | ||
550 | int wxComboBox::GetSelection() const | |
551 | { | |
552 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
553 | ||
554 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
555 | ||
556 | GList *selection = GTK_LIST(list)->selection; | |
557 | if (selection) | |
558 | { | |
559 | GList *child = GTK_LIST(list)->children; | |
560 | int count = 0; | |
561 | while (child) | |
562 | { | |
563 | if (child->data == selection->data) return count; | |
564 | count++; | |
565 | child = child->next; | |
566 | } | |
567 | } | |
568 | ||
569 | return -1; | |
570 | } | |
571 | ||
572 | wxString wxComboBox::GetString( int n ) const | |
573 | { | |
574 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
575 | ||
576 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
577 | ||
578 | wxString str; | |
579 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
580 | if (child) | |
581 | { | |
582 | GtkBin *bin = GTK_BIN( child->data ); | |
583 | GtkLabel *label = GTK_LABEL( bin->child ); | |
584 | #ifdef __WXGTK20__ | |
585 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); | |
586 | #else | |
587 | str = wxString( label->label ); | |
588 | #endif | |
589 | } | |
590 | else | |
591 | { | |
592 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
593 | } | |
594 | ||
595 | return str; | |
596 | } | |
597 | ||
598 | wxString wxComboBox::GetStringSelection() const | |
599 | { | |
600 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); | |
601 | ||
602 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
603 | ||
604 | GList *selection = GTK_LIST(list)->selection; | |
605 | if (selection) | |
606 | { | |
607 | GtkBin *bin = GTK_BIN( selection->data ); | |
608 | GtkLabel *label = GTK_LABEL( bin->child ); | |
609 | #ifdef __WXGTK20__ | |
610 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
611 | #else | |
612 | wxString tmp( label->label ); | |
613 | #endif | |
614 | return tmp; | |
615 | } | |
616 | ||
617 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); | |
618 | ||
619 | return wxT(""); | |
620 | } | |
621 | ||
622 | int wxComboBox::GetCount() const | |
623 | { | |
624 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); | |
625 | ||
626 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
627 | ||
628 | GList *child = GTK_LIST(list)->children; | |
629 | int count = 0; | |
630 | while (child) { count++; child = child->next; } | |
631 | return count; | |
632 | } | |
633 | ||
634 | void wxComboBox::SetSelection( int n ) | |
635 | { | |
636 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
637 | ||
638 | DisableEvents(); | |
639 | ||
640 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
641 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); | |
642 | gtk_list_select_item( GTK_LIST(list), n ); | |
643 | m_prevSelection = n; | |
644 | ||
645 | EnableEvents(); | |
646 | } | |
647 | ||
648 | wxString wxComboBox::GetValue() const | |
649 | { | |
650 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
651 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
652 | ||
653 | #if 0 | |
654 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) | |
655 | { | |
656 | wxChar c = tmp[i]; | |
657 | printf( "%d ", (int) (c) ); | |
658 | } | |
659 | printf( "\n" ); | |
660 | #endif | |
661 | ||
662 | return tmp; | |
663 | } | |
664 | ||
665 | void wxComboBox::SetValue( const wxString& value ) | |
666 | { | |
667 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
668 | ||
669 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
670 | wxString tmp = wxT(""); | |
671 | if (!value.IsNull()) tmp = value; | |
672 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); | |
673 | ||
674 | InvalidateBestSize(); | |
675 | } | |
676 | ||
677 | void wxComboBox::Copy() | |
678 | { | |
679 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
680 | ||
681 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
682 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
683 | } | |
684 | ||
685 | void wxComboBox::Cut() | |
686 | { | |
687 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
688 | ||
689 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
690 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); | |
691 | } | |
692 | ||
693 | void wxComboBox::Paste() | |
694 | { | |
695 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
696 | ||
697 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
698 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); | |
699 | } | |
700 | ||
701 | void wxComboBox::Undo() | |
702 | { | |
703 | // TODO | |
704 | } | |
705 | ||
706 | void wxComboBox::Redo() | |
707 | { | |
708 | // TODO | |
709 | } | |
710 | ||
711 | void wxComboBox::SelectAll() | |
712 | { | |
713 | SetSelection(0, GetLastPosition()); | |
714 | } | |
715 | ||
716 | bool wxComboBox::CanUndo() const | |
717 | { | |
718 | // TODO | |
719 | return false; | |
720 | } | |
721 | ||
722 | bool wxComboBox::CanRedo() const | |
723 | { | |
724 | // TODO | |
725 | return false; | |
726 | } | |
727 | ||
728 | bool wxComboBox::HasSelection() const | |
729 | { | |
730 | long from, to; | |
731 | GetSelection(&from, &to); | |
732 | return from != to; | |
733 | } | |
734 | ||
735 | bool wxComboBox::CanCopy() const | |
736 | { | |
737 | // Can copy if there's a selection | |
738 | return HasSelection(); | |
739 | } | |
740 | ||
741 | bool wxComboBox::CanCut() const | |
742 | { | |
743 | return CanCopy() && IsEditable(); | |
744 | } | |
745 | ||
746 | bool wxComboBox::CanPaste() const | |
747 | { | |
748 | // TODO: check for text on the clipboard | |
749 | return IsEditable() ; | |
750 | } | |
751 | ||
752 | bool wxComboBox::IsEditable() const | |
753 | { | |
754 | return !HasFlag(wxCB_READONLY); | |
755 | } | |
756 | ||
757 | ||
758 | void wxComboBox::SetInsertionPoint( long pos ) | |
759 | { | |
760 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
761 | ||
762 | if ( pos == GetLastPosition() ) | |
763 | pos = -1; | |
764 | ||
765 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
766 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); | |
767 | } | |
768 | ||
769 | long wxComboBox::GetInsertionPoint() const | |
770 | { | |
771 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); | |
772 | } | |
773 | ||
774 | wxTextPos wxComboBox::GetLastPosition() const | |
775 | { | |
776 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
777 | int pos = GTK_ENTRY(entry)->text_length; | |
778 | return (long) pos-1; | |
779 | } | |
780 | ||
781 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
782 | { | |
783 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
784 | ||
785 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
786 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
787 | if (value.IsNull()) return; | |
788 | gint pos = (gint)to; | |
789 | ||
790 | #if wxUSE_UNICODE | |
791 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
792 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
793 | #else | |
794 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
795 | #endif | |
796 | } | |
797 | ||
798 | void wxComboBox::SetSelection( long from, long to ) | |
799 | { | |
800 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
801 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
802 | } | |
803 | ||
804 | void wxComboBox::GetSelection( long* from, long* to ) const | |
805 | { | |
806 | if (IsEditable()) | |
807 | { | |
808 | GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry); | |
809 | #ifdef __WXGTK20__ | |
810 | gint start, end; | |
811 | gtk_editable_get_selection_bounds(editable, & start, & end); | |
812 | *from = start; | |
813 | *to = end; | |
814 | #else | |
815 | *from = (long) editable->selection_start_pos; | |
816 | *to = (long) editable->selection_end_pos; | |
817 | #endif | |
818 | } | |
819 | } | |
820 | ||
821 | void wxComboBox::SetEditable( bool editable ) | |
822 | { | |
823 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
824 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
825 | } | |
826 | ||
827 | void wxComboBox::OnChar( wxKeyEvent &event ) | |
828 | { | |
829 | if ( event.GetKeyCode() == WXK_RETURN ) | |
830 | { | |
831 | // GTK automatically selects an item if its in the list | |
832 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
833 | event.SetString( GetValue() ); | |
834 | event.SetInt( GetSelection() ); | |
835 | event.SetEventObject( this ); | |
836 | ||
837 | if (!GetEventHandler()->ProcessEvent( event )) | |
838 | { | |
839 | // This will invoke the dialog default action, such | |
840 | // as the clicking the default button. | |
841 | ||
842 | wxWindow *top_frame = m_parent; | |
843 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
844 | top_frame = top_frame->GetParent(); | |
845 | ||
846 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
847 | { | |
848 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
849 | ||
850 | if (window->default_widget) | |
851 | gtk_widget_activate (window->default_widget); | |
852 | } | |
853 | } | |
854 | ||
855 | // Catch GTK event so that GTK doesn't open the drop | |
856 | // down list upon RETURN. | |
857 | return; | |
858 | } | |
859 | ||
860 | event.Skip(); | |
861 | } | |
862 | ||
863 | void wxComboBox::DisableEvents() | |
864 | { | |
865 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), | |
866 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
867 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
868 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
869 | } | |
870 | ||
871 | void wxComboBox::EnableEvents() | |
872 | { | |
873 | gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", | |
874 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
875 | gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
876 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
877 | } | |
878 | ||
879 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
880 | { | |
881 | // NB: In some situations (e.g. on non-first page of a wizard, if the | |
882 | // size used is default size), GtkCombo widget is resized correctly, | |
883 | // but it's look is not updated, it's rendered as if it was much wider. | |
884 | // No other widgets are affected, so it looks like a bug in GTK+. | |
885 | // Manually requesting resize calculation (as gtk_pizza_set_size does) | |
886 | // fixes it. | |
887 | if (GTK_WIDGET_VISIBLE(m_widget)) | |
888 | gtk_widget_queue_resize(m_widget); | |
889 | ||
890 | event.Skip(); | |
891 | } | |
892 | ||
893 | void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style) | |
894 | { | |
895 | // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle ); | |
896 | ||
897 | gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style ); | |
898 | gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style ); | |
899 | ||
900 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
901 | GList *child = list->children; | |
902 | while (child) | |
903 | { | |
904 | gtk_widget_modify_style( GTK_WIDGET(child->data), style ); | |
905 | ||
906 | GtkBin *bin = GTK_BIN(child->data); | |
907 | gtk_widget_modify_style( bin->child, style ); | |
908 | ||
909 | child = child->next; | |
910 | } | |
911 | } | |
912 | ||
913 | GtkWidget* wxComboBox::GetConnectWidget() | |
914 | { | |
915 | return GTK_COMBO(m_widget)->entry; | |
916 | } | |
917 | ||
918 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
919 | { | |
920 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
921 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
922 | } | |
923 | ||
924 | wxSize wxComboBox::DoGetBestSize() const | |
925 | { | |
926 | wxSize ret( wxControl::DoGetBestSize() ); | |
927 | ||
928 | // we know better our horizontal extent: it depends on the longest string | |
929 | // in the combobox | |
930 | if ( m_widget ) | |
931 | { | |
932 | int width; | |
933 | size_t count = GetCount(); | |
934 | for ( size_t n = 0; n < count; n++ ) | |
935 | { | |
936 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); | |
937 | if ( width > ret.x ) | |
938 | ret.x = width; | |
939 | } | |
940 | } | |
941 | ||
942 | // empty combobox should have some reasonable default size too | |
943 | if ( ret.x < 100 ) | |
944 | ret.x = 100; | |
945 | ||
946 | CacheBestSize(ret); | |
947 | return ret; | |
948 | } | |
949 | ||
950 | // static | |
951 | wxVisualAttributes | |
952 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
953 | { | |
954 | return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true); | |
955 | } | |
956 | ||
957 | // ---------------------------------------------------------------------------- | |
958 | // standard event handling | |
959 | // ---------------------------------------------------------------------------- | |
960 | ||
961 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
962 | { | |
963 | Cut(); | |
964 | } | |
965 | ||
966 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
967 | { | |
968 | Copy(); | |
969 | } | |
970 | ||
971 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
972 | { | |
973 | Paste(); | |
974 | } | |
975 | ||
976 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
977 | { | |
978 | Undo(); | |
979 | } | |
980 | ||
981 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
982 | { | |
983 | Redo(); | |
984 | } | |
985 | ||
986 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
987 | { | |
988 | long from, to; | |
989 | GetSelection(& from, & to); | |
990 | if (from != -1 && to != -1) | |
991 | Remove(from, to); | |
992 | } | |
993 | ||
994 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
995 | { | |
996 | SetSelection(-1, -1); | |
997 | } | |
998 | ||
999 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
1000 | { | |
1001 | event.Enable( CanCut() ); | |
1002 | } | |
1003 | ||
1004 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
1005 | { | |
1006 | event.Enable( CanCopy() ); | |
1007 | } | |
1008 | ||
1009 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
1010 | { | |
1011 | event.Enable( CanPaste() ); | |
1012 | } | |
1013 | ||
1014 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
1015 | { | |
1016 | event.Enable( CanUndo() ); | |
1017 | } | |
1018 | ||
1019 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
1020 | { | |
1021 | event.Enable( CanRedo() ); | |
1022 | } | |
1023 | ||
1024 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
1025 | { | |
1026 | event.Enable(HasSelection() && IsEditable()) ; | |
1027 | } | |
1028 | ||
1029 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
1030 | { | |
1031 | event.Enable(GetLastPosition() > 0); | |
1032 | } | |
1033 | ||
1034 | #endif | |
1035 |