]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/combobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_COMBOBOX | |
14 | ||
15 | #include "wx/combobox.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/settings.h" | |
20 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED | |
21 | #include "wx/arrstr.h" | |
22 | #endif | |
23 | ||
24 | // We use GtkCombo which has been deprecated since GTK+ 2.3.0 | |
25 | // in favour of GtkComboBox for <GTK2.4 runtime | |
26 | // We also use GtkList | |
27 | #ifdef GTK_DISABLE_DEPRECATED | |
28 | #undef GTK_DISABLE_DEPRECATED | |
29 | #endif | |
30 | #include "wx/gtk/private.h" | |
31 | ||
32 | //----------------------------------------------------------------------------- | |
33 | // data | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern bool g_blockEventsOnDrag; | |
37 | static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // "changed" - typing and list item matches get changed, select-child | |
41 | // if it doesn't match an item then just get a single changed | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | extern "C" { | |
45 | static void | |
46 | gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
47 | { | |
48 | if (g_isIdle) wxapp_install_idle_handler(); | |
49 | ||
50 | if (combo->m_ignoreNextUpdate) | |
51 | { | |
52 | combo->m_ignoreNextUpdate = false; | |
53 | return; | |
54 | } | |
55 | ||
56 | if (!combo->m_hasVMT) return; | |
57 | ||
58 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
59 | event.SetString( combo->GetValue() ); | |
60 | event.SetEventObject( combo ); | |
61 | combo->GetEventHandler()->ProcessEvent( event ); | |
62 | } | |
63 | } | |
64 | ||
65 | extern "C" { | |
66 | static void | |
67 | gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
68 | { | |
69 | } | |
70 | } | |
71 | ||
72 | extern "C" { | |
73 | static void | |
74 | gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) | |
75 | { | |
76 | // when the popup is hidden, throw a SELECTED event only if the combobox | |
77 | // selection changed. | |
78 | const int curSelection = combo->GetCurrentSelection(); | |
79 | ||
80 | const bool hasChanged = curSelection != g_SelectionBeforePopup; | |
81 | ||
82 | // reset the selection flag to value meaning that it is hidden and do it | |
83 | // now, before generating the events, so that GetSelection() returns the | |
84 | // new value from the event handler | |
85 | g_SelectionBeforePopup = wxID_NONE; | |
86 | ||
87 | if ( hasChanged ) | |
88 | { | |
89 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
90 | event.SetInt( curSelection ); | |
91 | event.SetString( combo->GetStringSelection() ); | |
92 | event.SetEventObject( combo ); | |
93 | combo->GetEventHandler()->ProcessEvent( event ); | |
94 | ||
95 | // for consistency with the other ports, send TEXT event | |
96 | wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
97 | event2.SetString( combo->GetStringSelection() ); | |
98 | event2.SetEventObject( combo ); | |
99 | combo->GetEventHandler()->ProcessEvent( event2 ); | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | extern "C" { | |
105 | static void | |
106 | gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) | |
107 | { | |
108 | // store the combobox selection value before the popup is shown | |
109 | g_SelectionBeforePopup = combo->GetCurrentSelection(); | |
110 | } | |
111 | } | |
112 | ||
113 | //----------------------------------------------------------------------------- | |
114 | // "select-child" - click/cursor get select-child, changed, select-child | |
115 | //----------------------------------------------------------------------------- | |
116 | ||
117 | extern "C" { | |
118 | static void | |
119 | gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
120 | { | |
121 | if (g_isIdle) wxapp_install_idle_handler(); | |
122 | ||
123 | if (!combo->m_hasVMT) return; | |
124 | ||
125 | if (g_blockEventsOnDrag) return; | |
126 | ||
127 | int curSelection = combo->GetCurrentSelection(); | |
128 | ||
129 | if (combo->m_prevSelection == curSelection) return; | |
130 | ||
131 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
132 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
133 | ||
134 | combo->m_prevSelection = curSelection; | |
135 | ||
136 | // Quickly set the value of the combo box | |
137 | // as GTK+ does that only AFTER the event | |
138 | // is sent. | |
139 | g_signal_handlers_disconnect_by_func (GTK_COMBO (combo->GetHandle())->entry, | |
140 | (gpointer) gtkcombo_text_changed_callback, | |
141 | combo); | |
142 | combo->SetValue( combo->GetStringSelection() ); | |
143 | g_signal_connect_after (GTK_COMBO (combo->GetHandle())->entry, "changed", | |
144 | G_CALLBACK (gtkcombo_text_changed_callback), combo); | |
145 | ||
146 | // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE) | |
147 | // because when combobox popup is shown, gtkcombo_combo_select_child_callback is | |
148 | // called each times the mouse is over an item with a pressed button so a lot | |
149 | // of SELECTED event could be generated if the user keep the mouse button down | |
150 | // and select other items ... | |
151 | if (g_SelectionBeforePopup == wxID_NONE) | |
152 | { | |
153 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
154 | event.SetInt( curSelection ); | |
155 | event.SetString( combo->GetStringSelection() ); | |
156 | event.SetEventObject( combo ); | |
157 | combo->GetEventHandler()->ProcessEvent( event ); | |
158 | ||
159 | // for consistency with the other ports, don't generate text update | |
160 | // events while the user is browsing the combobox neither | |
161 | wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
162 | event2.SetString( combo->GetValue() ); | |
163 | event2.SetEventObject( combo ); | |
164 | combo->GetEventHandler()->ProcessEvent( event2 ); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | #ifdef __WXGTK24__ | |
170 | extern "C" { | |
171 | static void | |
172 | gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
173 | { | |
174 | if (g_isIdle) wxapp_install_idle_handler(); | |
175 | ||
176 | if (!combo->m_hasVMT) return; | |
177 | ||
178 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); | |
179 | event.SetString( combo->GetValue() ); | |
180 | event.SetEventObject( combo ); | |
181 | combo->GetEventHandler()->ProcessEvent( event ); | |
182 | } | |
183 | } | |
184 | ||
185 | extern "C" { | |
186 | static void | |
187 | gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
188 | { | |
189 | if (g_isIdle) wxapp_install_idle_handler(); | |
190 | ||
191 | if (!combo->m_hasVMT) return; | |
192 | ||
193 | if (combo->GetSelection() == -1) | |
194 | return; | |
195 | ||
196 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); | |
197 | event.SetInt( combo->GetSelection() ); | |
198 | event.SetString( combo->GetStringSelection() ); | |
199 | event.SetEventObject( combo ); | |
200 | combo->GetEventHandler()->ProcessEvent( event ); | |
201 | } | |
202 | } | |
203 | #endif | |
204 | ||
205 | //----------------------------------------------------------------------------- | |
206 | // wxComboBox | |
207 | //----------------------------------------------------------------------------- | |
208 | ||
209 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
210 | ||
211 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) | |
212 | EVT_SIZE(wxComboBox::OnSize) | |
213 | EVT_CHAR(wxComboBox::OnChar) | |
214 | ||
215 | EVT_MENU(wxID_CUT, wxComboBox::OnCut) | |
216 | EVT_MENU(wxID_COPY, wxComboBox::OnCopy) | |
217 | EVT_MENU(wxID_PASTE, wxComboBox::OnPaste) | |
218 | EVT_MENU(wxID_UNDO, wxComboBox::OnUndo) | |
219 | EVT_MENU(wxID_REDO, wxComboBox::OnRedo) | |
220 | EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete) | |
221 | EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll) | |
222 | ||
223 | EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut) | |
224 | EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy) | |
225 | EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste) | |
226 | EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo) | |
227 | EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo) | |
228 | EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete) | |
229 | EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) | |
230 | END_EVENT_TABLE() | |
231 | ||
232 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, | |
233 | const wxString& value, | |
234 | const wxPoint& pos, const wxSize& size, | |
235 | const wxArrayString& choices, | |
236 | long style, const wxValidator& validator, | |
237 | const wxString& name ) | |
238 | { | |
239 | wxCArrayString chs(choices); | |
240 | ||
241 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
242 | chs.GetStrings(), style, validator, name ); | |
243 | } | |
244 | ||
245 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, | |
246 | const wxPoint& pos, const wxSize& size, | |
247 | int n, const wxString choices[], | |
248 | long style, const wxValidator& validator, | |
249 | const wxString& name ) | |
250 | { | |
251 | m_ignoreNextUpdate = false; | |
252 | m_needParent = true; | |
253 | m_acceptsFocus = true; | |
254 | m_prevSelection = 0; | |
255 | ||
256 | if (!PreCreation( parent, pos, size ) || | |
257 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
258 | { | |
259 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); | |
260 | return false; | |
261 | } | |
262 | ||
263 | #ifdef __WXGTK24__ | |
264 | if (!gtk_check_version(2,4,0)) | |
265 | { | |
266 | m_widget = gtk_combo_box_entry_new_text(); | |
267 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
268 | ||
269 | gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE ); | |
270 | ||
271 | for (int i = 0; i < n; i++) | |
272 | { | |
273 | gtk_combo_box_append_text( combobox, wxGTK_CONV( choices[i] ) ); | |
274 | ||
275 | m_clientDataList.Append( (wxObject*)NULL ); | |
276 | m_clientObjectList.Append( (wxObject*)NULL ); | |
277 | } | |
278 | } | |
279 | else | |
280 | #endif | |
281 | { | |
282 | m_widget = gtk_combo_new(); | |
283 | GtkCombo* combo = GTK_COMBO(m_widget); | |
284 | ||
285 | // Disable GTK's broken events ... | |
286 | g_signal_handler_disconnect (combo->entry, combo->entry_change_id); | |
287 | // ... and add surrogate handler. | |
288 | combo->entry_change_id = g_signal_connect (combo->entry, "changed", | |
289 | G_CALLBACK (gtkcombo_dummy_callback), | |
290 | combo); | |
291 | ||
292 | // make it more useable | |
293 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); | |
294 | ||
295 | // and case-sensitive | |
296 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
297 | ||
298 | if (style & wxNO_BORDER) | |
299 | g_object_set (combo->entry, "has-frame", FALSE, NULL ); | |
300 | ||
301 | GtkWidget *list = combo->list; | |
302 | ||
303 | for (int i = 0; i < n; i++) | |
304 | { | |
305 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); | |
306 | ||
307 | m_clientDataList.Append( (wxObject*)NULL ); | |
308 | m_clientObjectList.Append( (wxObject*)NULL ); | |
309 | ||
310 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
311 | ||
312 | gtk_widget_show( list_item ); | |
313 | } | |
314 | } | |
315 | ||
316 | ||
317 | m_parent->DoAddChild( this ); | |
318 | ||
319 | GtkEntry *entry = NULL; | |
320 | #ifdef __WXGTK24__ | |
321 | if (!gtk_check_version(2,4,0)) | |
322 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
323 | else | |
324 | #endif | |
325 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
326 | ||
327 | m_focusWidget = GTK_WIDGET( entry ); | |
328 | ||
329 | PostCreation(size); | |
330 | ||
331 | #ifdef __WXGTK24__ | |
332 | if (!gtk_check_version(2,4,0)) | |
333 | ConnectWidget( m_widget ); | |
334 | else | |
335 | #endif | |
336 | ConnectWidget( GTK_COMBO(m_widget)->button ); | |
337 | ||
338 | #ifdef __WXGTK24__ | |
339 | if (!gtk_check_version(2,4,0)) | |
340 | { | |
341 | gtk_entry_set_text( entry, wxGTK_CONV(value) ); | |
342 | ||
343 | if (style & wxCB_READONLY) | |
344 | gtk_entry_set_editable( entry, FALSE ); | |
345 | ||
346 | g_signal_connect_after (entry, "changed", | |
347 | G_CALLBACK (gtkcombobox_text_changed_callback), this); | |
348 | ||
349 | g_signal_connect_after (m_widget, "changed", | |
350 | G_CALLBACK (gtkcombobox_changed_callback), this); | |
351 | } | |
352 | else | |
353 | #endif | |
354 | { | |
355 | GtkCombo *combo = GTK_COMBO(m_widget); | |
356 | // MSW's combo box shows the value and the selection is -1 | |
357 | gtk_entry_set_text( entry, wxGTK_CONV(value) ); | |
358 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
359 | ||
360 | if (style & wxCB_READONLY) | |
361 | gtk_entry_set_editable( entry, FALSE ); | |
362 | ||
363 | // "show" and "hide" events are generated when user click on the combobox button which popups a list | |
364 | // this list is the "popwin" gtk widget | |
365 | g_signal_connect (GTK_COMBO(combo)->popwin, "hide", | |
366 | G_CALLBACK (gtkcombo_popup_hide_callback), this); | |
367 | g_signal_connect (GTK_COMBO(combo)->popwin, "show", | |
368 | G_CALLBACK (gtkcombo_popup_show_callback), this); | |
369 | g_signal_connect_after (combo->list, "select-child", | |
370 | G_CALLBACK (gtkcombo_combo_select_child_callback), | |
371 | this); | |
372 | g_signal_connect_after (entry, "changed", | |
373 | G_CALLBACK (gtkcombo_text_changed_callback), this); | |
374 | ||
375 | // This is required for tool bar support | |
376 | // Doesn't currently work | |
377 | // wxSize setsize = GetSize(); | |
378 | // gtk_widget_set_size_request( m_widget, setsize.x, setsize.y ); | |
379 | } | |
380 | ||
381 | SetInitialSize(size); // need this too because this is a wxControlWithItems | |
382 | ||
383 | ||
384 | return true; | |
385 | } | |
386 | ||
387 | wxComboBox::~wxComboBox() | |
388 | { | |
389 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
390 | while (node) | |
391 | { | |
392 | wxClientData *cd = (wxClientData*)node->GetData(); | |
393 | if (cd) delete cd; | |
394 | node = node->GetNext(); | |
395 | } | |
396 | m_clientObjectList.Clear(); | |
397 | ||
398 | m_clientDataList.Clear(); | |
399 | } | |
400 | ||
401 | void wxComboBox::SetFocus() | |
402 | { | |
403 | if ( m_hasFocus ) | |
404 | { | |
405 | // don't do anything if we already have focus | |
406 | return; | |
407 | } | |
408 | ||
409 | gtk_widget_grab_focus( m_focusWidget ); | |
410 | } | |
411 | ||
412 | int wxComboBox::DoAppend( const wxString &item ) | |
413 | { | |
414 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
415 | ||
416 | #ifdef __WXGTK24__ | |
417 | if (!gtk_check_version(2,4,0)) | |
418 | { | |
419 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
420 | gtk_combo_box_append_text( combobox, wxGTK_CONV( item ) ); | |
421 | } | |
422 | else | |
423 | #endif | |
424 | { | |
425 | DisableEvents(); | |
426 | ||
427 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
428 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
429 | ||
430 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
431 | ||
432 | if (GTK_WIDGET_REALIZED(m_widget)) | |
433 | { | |
434 | gtk_widget_realize( list_item ); | |
435 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
436 | } | |
437 | ||
438 | // Apply current widget style to the new list_item | |
439 | GtkRcStyle *style = CreateWidgetStyle(); | |
440 | if (style) | |
441 | { | |
442 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); | |
443 | GtkBin *bin = GTK_BIN( list_item ); | |
444 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
445 | gtk_widget_modify_style( label, style ); | |
446 | gtk_rc_style_unref( style ); | |
447 | } | |
448 | ||
449 | gtk_widget_show( list_item ); | |
450 | ||
451 | EnableEvents(); | |
452 | } | |
453 | ||
454 | const unsigned int count = GetCount(); | |
455 | ||
456 | if ( m_clientDataList.GetCount() < count ) | |
457 | m_clientDataList.Append( (wxObject*) NULL ); | |
458 | if ( m_clientObjectList.GetCount() < count ) | |
459 | m_clientObjectList.Append( (wxObject*) NULL ); | |
460 | ||
461 | InvalidateBestSize(); | |
462 | ||
463 | return count - 1; | |
464 | } | |
465 | ||
466 | int wxComboBox::DoInsert(const wxString &item, unsigned int pos) | |
467 | { | |
468 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, | |
469 | wxT("can't insert into sorted list")); | |
470 | ||
471 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
472 | wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") ); | |
473 | ||
474 | unsigned int count = GetCount(); | |
475 | ||
476 | if (pos == count) | |
477 | return Append(item); | |
478 | ||
479 | #ifdef __WXGTK24__ | |
480 | if (!gtk_check_version(2,4,0)) | |
481 | { | |
482 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
483 | gtk_combo_box_insert_text( combobox, pos, wxGTK_CONV( item ) ); | |
484 | } | |
485 | else | |
486 | #endif | |
487 | { | |
488 | DisableEvents(); | |
489 | ||
490 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
491 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
492 | ||
493 | GList *gitem_list = g_list_alloc (); | |
494 | gitem_list->data = list_item; | |
495 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
496 | ||
497 | if (GTK_WIDGET_REALIZED(m_widget)) | |
498 | { | |
499 | gtk_widget_realize( list_item ); | |
500 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
501 | ||
502 | ApplyWidgetStyle(); | |
503 | } | |
504 | ||
505 | gtk_widget_show( list_item ); | |
506 | ||
507 | EnableEvents(); | |
508 | } | |
509 | ||
510 | count = GetCount(); | |
511 | ||
512 | if ( m_clientDataList.GetCount() < count ) | |
513 | m_clientDataList.Insert( pos, (wxObject*) NULL ); | |
514 | if ( m_clientObjectList.GetCount() < count ) | |
515 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); | |
516 | ||
517 | InvalidateBestSize(); | |
518 | ||
519 | return pos; | |
520 | } | |
521 | ||
522 | void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData) | |
523 | { | |
524 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
525 | ||
526 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
527 | if (!node) return; | |
528 | ||
529 | node->SetData( (wxObject*) clientData ); | |
530 | } | |
531 | ||
532 | void* wxComboBox::DoGetItemClientData(unsigned int n) const | |
533 | { | |
534 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); | |
535 | ||
536 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); | |
537 | ||
538 | return node ? node->GetData() : NULL; | |
539 | } | |
540 | ||
541 | void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData) | |
542 | { | |
543 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
544 | ||
545 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
546 | if (!node) return; | |
547 | ||
548 | // wxItemContainer already deletes data for us | |
549 | ||
550 | node->SetData( (wxObject*) clientData ); | |
551 | } | |
552 | ||
553 | wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const | |
554 | { | |
555 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); | |
556 | ||
557 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
558 | ||
559 | return node ? (wxClientData*) node->GetData() : NULL; | |
560 | } | |
561 | ||
562 | void wxComboBox::Clear() | |
563 | { | |
564 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
565 | ||
566 | DisableEvents(); | |
567 | ||
568 | #ifdef __WXGTK24__ | |
569 | if (!gtk_check_version(2,4,0)) | |
570 | { | |
571 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
572 | const unsigned int count = GetCount(); | |
573 | for (unsigned int i = 0; i < count; i++) | |
574 | gtk_combo_box_remove_text( combobox, 0 ); | |
575 | } | |
576 | else // GTK+ < 2.4.0 | |
577 | #endif // __WXGTK24__ | |
578 | { | |
579 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
580 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); | |
581 | } | |
582 | ||
583 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); | |
584 | while (node) | |
585 | { | |
586 | wxClientData *cd = (wxClientData*)node->GetData(); | |
587 | delete cd; | |
588 | node = node->GetNext(); | |
589 | } | |
590 | m_clientObjectList.Clear(); | |
591 | ||
592 | m_clientDataList.Clear(); | |
593 | ||
594 | EnableEvents(); | |
595 | ||
596 | InvalidateBestSize(); | |
597 | } | |
598 | ||
599 | void wxComboBox::Delete(unsigned int n) | |
600 | { | |
601 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
602 | ||
603 | #ifdef __WXGTK24__ | |
604 | if (!gtk_check_version(2,4,0)) | |
605 | { | |
606 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); | |
607 | ||
608 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
609 | gtk_combo_box_remove_text( combobox, n ); | |
610 | } | |
611 | else | |
612 | #endif | |
613 | { | |
614 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
615 | ||
616 | GList *child = g_list_nth( listbox->children, n ); | |
617 | ||
618 | if (!child) | |
619 | { | |
620 | wxFAIL_MSG(wxT("wrong index")); | |
621 | return; | |
622 | } | |
623 | ||
624 | DisableEvents(); | |
625 | ||
626 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
627 | gtk_list_remove_items( listbox, list ); | |
628 | g_list_free( list ); | |
629 | ||
630 | EnableEvents(); | |
631 | } | |
632 | ||
633 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); | |
634 | if (node) | |
635 | { | |
636 | wxClientData *cd = (wxClientData*)node->GetData(); | |
637 | if (cd) delete cd; | |
638 | m_clientObjectList.Erase( node ); | |
639 | } | |
640 | ||
641 | node = m_clientDataList.Item( n ); | |
642 | if (node) | |
643 | m_clientDataList.Erase( node ); | |
644 | ||
645 | InvalidateBestSize(); | |
646 | } | |
647 | ||
648 | void wxComboBox::SetString(unsigned int n, const wxString &text) | |
649 | { | |
650 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
651 | ||
652 | #ifdef __WXGTK24__ | |
653 | if (!gtk_check_version(2,4,0)) | |
654 | { | |
655 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
656 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); | |
657 | ||
658 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); | |
659 | GtkTreeIter iter; | |
660 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
661 | { | |
662 | GValue value = { 0, }; | |
663 | g_value_init( &value, G_TYPE_STRING ); | |
664 | g_value_set_string( &value, wxGTK_CONV( text ) ); | |
665 | gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value ); | |
666 | g_value_unset( &value ); | |
667 | } | |
668 | } | |
669 | else | |
670 | #endif | |
671 | { | |
672 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
673 | ||
674 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
675 | if (child) | |
676 | { | |
677 | GtkBin *bin = GTK_BIN( child->data ); | |
678 | GtkLabel *label = GTK_LABEL( bin->child ); | |
679 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
680 | } | |
681 | else | |
682 | { | |
683 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
684 | } | |
685 | } | |
686 | ||
687 | InvalidateBestSize(); | |
688 | } | |
689 | ||
690 | int wxComboBox::FindString( const wxString &item, bool bCase ) const | |
691 | { | |
692 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") ); | |
693 | ||
694 | #ifdef __WXGTK24__ | |
695 | if (!gtk_check_version(2,4,0)) | |
696 | { | |
697 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
698 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
699 | GtkTreeIter iter; | |
700 | gtk_tree_model_get_iter_first( model, &iter ); | |
701 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
702 | return -1; | |
703 | int count = 0; | |
704 | do | |
705 | { | |
706 | GValue value = { 0, }; | |
707 | gtk_tree_model_get_value( model, &iter, 0, &value ); | |
708 | wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) ); | |
709 | g_value_unset( &value ); | |
710 | ||
711 | if (item.IsSameAs( str, bCase ) ) | |
712 | return count; | |
713 | ||
714 | count++; | |
715 | ||
716 | } while (gtk_tree_model_iter_next( model, &iter )); | |
717 | } | |
718 | else | |
719 | #endif | |
720 | { | |
721 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
722 | ||
723 | GList *child = GTK_LIST(list)->children; | |
724 | int count = 0; | |
725 | while (child) | |
726 | { | |
727 | GtkBin *bin = GTK_BIN( child->data ); | |
728 | GtkLabel *label = GTK_LABEL( bin->child ); | |
729 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
730 | ||
731 | if (item.IsSameAs( str , bCase ) ) | |
732 | return count; | |
733 | ||
734 | count++; | |
735 | child = child->next; | |
736 | } | |
737 | } | |
738 | ||
739 | return wxNOT_FOUND; | |
740 | } | |
741 | ||
742 | int wxComboBox::GetSelection() const | |
743 | { | |
744 | #ifdef __WXGTK24__ | |
745 | if (!gtk_check_version(2,4,0)) | |
746 | { | |
747 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
748 | return gtk_combo_box_get_active( combobox ); | |
749 | } | |
750 | else | |
751 | #endif | |
752 | // if the popup is currently opened, use the selection as it had been | |
753 | // before it dropped down | |
754 | return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection() | |
755 | : g_SelectionBeforePopup; | |
756 | } | |
757 | ||
758 | int wxComboBox::GetCurrentSelection() const | |
759 | { | |
760 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
761 | ||
762 | #ifdef __WXGTK24__ | |
763 | if (!gtk_check_version(2,4,0)) | |
764 | { | |
765 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
766 | return gtk_combo_box_get_active( combobox ); | |
767 | } | |
768 | else | |
769 | #endif | |
770 | { | |
771 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
772 | ||
773 | GList *selection = GTK_LIST(list)->selection; | |
774 | if (selection) | |
775 | { | |
776 | GList *child = GTK_LIST(list)->children; | |
777 | int count = 0; | |
778 | while (child) | |
779 | { | |
780 | if (child->data == selection->data) return count; | |
781 | count++; | |
782 | child = child->next; | |
783 | } | |
784 | } | |
785 | } | |
786 | ||
787 | return -1; | |
788 | } | |
789 | ||
790 | wxString wxComboBox::GetString(unsigned int n) const | |
791 | { | |
792 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") ); | |
793 | ||
794 | wxString str; | |
795 | ||
796 | #ifdef __WXGTK24__ | |
797 | if (!gtk_check_version(2,4,0)) | |
798 | { | |
799 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
800 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); | |
801 | GtkTreeIter iter; | |
802 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
803 | { | |
804 | GValue value = { 0, }; | |
805 | gtk_tree_model_get_value( model, &iter, 0, &value ); | |
806 | wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) ); | |
807 | g_value_unset( &value ); | |
808 | return tmp; | |
809 | } | |
810 | } | |
811 | else | |
812 | #endif | |
813 | { | |
814 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
815 | ||
816 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
817 | if (child) | |
818 | { | |
819 | GtkBin *bin = GTK_BIN( child->data ); | |
820 | GtkLabel *label = GTK_LABEL( bin->child ); | |
821 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); | |
822 | } | |
823 | else | |
824 | { | |
825 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
826 | } | |
827 | } | |
828 | ||
829 | return str; | |
830 | } | |
831 | ||
832 | wxString wxComboBox::GetStringSelection() const | |
833 | { | |
834 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") ); | |
835 | ||
836 | #ifdef __WXGTK24__ | |
837 | if (!gtk_check_version(2,4,0)) | |
838 | { | |
839 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
840 | int sel = gtk_combo_box_get_active( combobox ); | |
841 | if (sel == -1) | |
842 | return wxEmptyString; | |
843 | return GetString(sel); | |
844 | } | |
845 | else | |
846 | #endif | |
847 | { | |
848 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
849 | ||
850 | GList *selection = GTK_LIST(list)->selection; | |
851 | if (selection) | |
852 | { | |
853 | GtkBin *bin = GTK_BIN( selection->data ); | |
854 | GtkLabel *label = GTK_LABEL( bin->child ); | |
855 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
856 | return tmp; | |
857 | } | |
858 | ||
859 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); | |
860 | } | |
861 | ||
862 | return wxEmptyString; | |
863 | } | |
864 | ||
865 | unsigned int wxComboBox::GetCount() const | |
866 | { | |
867 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); | |
868 | ||
869 | #ifdef __WXGTK24__ | |
870 | if (!gtk_check_version(2,4,0)) | |
871 | { | |
872 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
873 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
874 | GtkTreeIter iter; | |
875 | gtk_tree_model_get_iter_first( model, &iter ); | |
876 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
877 | return 0; | |
878 | unsigned int ret = 1; | |
879 | while (gtk_tree_model_iter_next( model, &iter )) | |
880 | ret++; | |
881 | return ret; | |
882 | } | |
883 | else | |
884 | #endif | |
885 | { | |
886 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
887 | ||
888 | GList *child = GTK_LIST(list)->children; | |
889 | unsigned int count = 0; | |
890 | while (child) | |
891 | { | |
892 | count++; | |
893 | child = child->next; | |
894 | } | |
895 | return count; | |
896 | } | |
897 | ||
898 | return 0; | |
899 | } | |
900 | ||
901 | void wxComboBox::SetSelection( int n ) | |
902 | { | |
903 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
904 | ||
905 | DisableEvents(); | |
906 | ||
907 | #ifdef __WXGTK24__ | |
908 | if (!gtk_check_version(2,4,0)) | |
909 | { | |
910 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
911 | gtk_combo_box_set_active( combobox, n ); | |
912 | } | |
913 | else | |
914 | #endif | |
915 | { | |
916 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
917 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); | |
918 | gtk_list_select_item( GTK_LIST(list), n ); | |
919 | m_prevSelection = n; | |
920 | } | |
921 | ||
922 | EnableEvents(); | |
923 | } | |
924 | ||
925 | wxString wxComboBox::GetValue() const | |
926 | { | |
927 | GtkEntry *entry = NULL; | |
928 | #ifdef __WXGTK24__ | |
929 | if (!gtk_check_version(2,4,0)) | |
930 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
931 | else | |
932 | #endif | |
933 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
934 | ||
935 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
936 | ||
937 | #if 0 | |
938 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) | |
939 | { | |
940 | wxChar c = tmp[i]; | |
941 | printf( "%d ", (int) (c) ); | |
942 | } | |
943 | printf( "\n" ); | |
944 | #endif | |
945 | ||
946 | return tmp; | |
947 | } | |
948 | ||
949 | void wxComboBox::SetValue( const wxString& value ) | |
950 | { | |
951 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
952 | ||
953 | GtkEntry *entry = NULL; | |
954 | #ifdef __WXGTK24__ | |
955 | if (!gtk_check_version(2,4,0)) | |
956 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
957 | else | |
958 | #endif | |
959 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
960 | ||
961 | wxString tmp; | |
962 | if (!value.IsNull()) tmp = value; | |
963 | gtk_entry_set_text( entry, wxGTK_CONV( tmp ) ); | |
964 | ||
965 | InvalidateBestSize(); | |
966 | } | |
967 | ||
968 | void wxComboBox::Copy() | |
969 | { | |
970 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
971 | ||
972 | GtkEntry *entry = NULL; | |
973 | #ifdef __WXGTK24__ | |
974 | if (!gtk_check_version(2,4,0)) | |
975 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
976 | else | |
977 | #endif | |
978 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
979 | ||
980 | gtk_editable_copy_clipboard(GTK_EDITABLE(entry)); | |
981 | } | |
982 | ||
983 | void wxComboBox::Cut() | |
984 | { | |
985 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
986 | ||
987 | GtkEntry *entry = NULL; | |
988 | #ifdef __WXGTK24__ | |
989 | if (!gtk_check_version(2,4,0)) | |
990 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
991 | else | |
992 | #endif | |
993 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
994 | ||
995 | gtk_editable_cut_clipboard(GTK_EDITABLE(entry)); | |
996 | } | |
997 | ||
998 | void wxComboBox::Paste() | |
999 | { | |
1000 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
1001 | ||
1002 | GtkEntry *entry = NULL; | |
1003 | #ifdef __WXGTK24__ | |
1004 | if (!gtk_check_version(2,4,0)) | |
1005 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1006 | else | |
1007 | #endif | |
1008 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1009 | ||
1010 | gtk_editable_paste_clipboard(GTK_EDITABLE(entry)); | |
1011 | } | |
1012 | ||
1013 | void wxComboBox::Undo() | |
1014 | { | |
1015 | // TODO | |
1016 | } | |
1017 | ||
1018 | void wxComboBox::Redo() | |
1019 | { | |
1020 | // TODO | |
1021 | } | |
1022 | ||
1023 | void wxComboBox::SelectAll() | |
1024 | { | |
1025 | SetSelection(0, GetLastPosition()); | |
1026 | } | |
1027 | ||
1028 | bool wxComboBox::CanUndo() const | |
1029 | { | |
1030 | // TODO | |
1031 | return false; | |
1032 | } | |
1033 | ||
1034 | bool wxComboBox::CanRedo() const | |
1035 | { | |
1036 | // TODO | |
1037 | return false; | |
1038 | } | |
1039 | ||
1040 | bool wxComboBox::HasSelection() const | |
1041 | { | |
1042 | long from, to; | |
1043 | GetSelection(&from, &to); | |
1044 | return from != to; | |
1045 | } | |
1046 | ||
1047 | bool wxComboBox::CanCopy() const | |
1048 | { | |
1049 | // Can copy if there's a selection | |
1050 | return HasSelection(); | |
1051 | } | |
1052 | ||
1053 | bool wxComboBox::CanCut() const | |
1054 | { | |
1055 | return CanCopy() && IsEditable(); | |
1056 | } | |
1057 | ||
1058 | bool wxComboBox::CanPaste() const | |
1059 | { | |
1060 | // TODO: check for text on the clipboard | |
1061 | return IsEditable() ; | |
1062 | } | |
1063 | ||
1064 | bool wxComboBox::IsEditable() const | |
1065 | { | |
1066 | return !HasFlag(wxCB_READONLY); | |
1067 | } | |
1068 | ||
1069 | ||
1070 | void wxComboBox::SetInsertionPoint( long pos ) | |
1071 | { | |
1072 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
1073 | ||
1074 | if ( pos == GetLastPosition() ) | |
1075 | pos = -1; | |
1076 | ||
1077 | GtkEntry *entry = NULL; | |
1078 | #ifdef __WXGTK24__ | |
1079 | if (!gtk_check_version(2,4,0)) | |
1080 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1081 | else | |
1082 | #endif | |
1083 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1084 | ||
1085 | gtk_entry_set_position( entry, (int)pos ); | |
1086 | } | |
1087 | ||
1088 | long wxComboBox::GetInsertionPoint() const | |
1089 | { | |
1090 | GtkEntry *entry = NULL; | |
1091 | #ifdef __WXGTK24__ | |
1092 | if (!gtk_check_version(2,4,0)) | |
1093 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1094 | else | |
1095 | #endif | |
1096 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1097 | ||
1098 | return (long) gtk_editable_get_position(GTK_EDITABLE(entry)); | |
1099 | } | |
1100 | ||
1101 | wxTextPos wxComboBox::GetLastPosition() const | |
1102 | { | |
1103 | GtkEntry *entry = NULL; | |
1104 | #ifdef __WXGTK24__ | |
1105 | if (!gtk_check_version(2,4,0)) | |
1106 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1107 | else | |
1108 | #endif | |
1109 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1110 | ||
1111 | int pos = entry->text_length; | |
1112 | return (long) pos-1; | |
1113 | } | |
1114 | ||
1115 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
1116 | { | |
1117 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
1118 | ||
1119 | GtkEntry *entry = NULL; | |
1120 | #ifdef __WXGTK24__ | |
1121 | if (!gtk_check_version(2,4,0)) | |
1122 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1123 | else | |
1124 | #endif | |
1125 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1126 | ||
1127 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
1128 | if (value.IsNull()) return; | |
1129 | gint pos = (gint)to; | |
1130 | ||
1131 | #if wxUSE_UNICODE | |
1132 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
1133 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
1134 | #else | |
1135 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos ); | |
1136 | #endif | |
1137 | } | |
1138 | ||
1139 | void wxComboBox::SetSelection( long from, long to ) | |
1140 | { | |
1141 | GtkEntry *entry = NULL; | |
1142 | #ifdef __WXGTK24__ | |
1143 | if (!gtk_check_version(2,4,0)) | |
1144 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1145 | else | |
1146 | #endif | |
1147 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1148 | ||
1149 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
1150 | } | |
1151 | ||
1152 | void wxComboBox::GetSelection( long* from, long* to ) const | |
1153 | { | |
1154 | GtkEntry *entry = NULL; | |
1155 | #ifdef __WXGTK24__ | |
1156 | if (!gtk_check_version(2,4,0)) | |
1157 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1158 | else | |
1159 | #endif | |
1160 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1161 | ||
1162 | if (IsEditable()) | |
1163 | { | |
1164 | GtkEditable *editable = GTK_EDITABLE(entry); | |
1165 | gint start, end; | |
1166 | gtk_editable_get_selection_bounds(editable, & start, & end); | |
1167 | *from = start; | |
1168 | *to = end; | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | void wxComboBox::SetEditable( bool editable ) | |
1173 | { | |
1174 | GtkEntry *entry = NULL; | |
1175 | #ifdef __WXGTK24__ | |
1176 | if (!gtk_check_version(2,4,0)) | |
1177 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1178 | else | |
1179 | #endif | |
1180 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1181 | ||
1182 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
1183 | } | |
1184 | ||
1185 | void wxComboBox::OnChar( wxKeyEvent &event ) | |
1186 | { | |
1187 | if ( event.GetKeyCode() == WXK_RETURN ) | |
1188 | { | |
1189 | // GTK automatically selects an item if its in the list | |
1190 | wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
1191 | eventEnter.SetString( GetValue() ); | |
1192 | eventEnter.SetInt( GetSelection() ); | |
1193 | eventEnter.SetEventObject( this ); | |
1194 | ||
1195 | if (!GetEventHandler()->ProcessEvent( eventEnter )) | |
1196 | { | |
1197 | // This will invoke the dialog default action, such | |
1198 | // as the clicking the default button. | |
1199 | ||
1200 | wxWindow *top_frame = m_parent; | |
1201 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
1202 | top_frame = top_frame->GetParent(); | |
1203 | ||
1204 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
1205 | { | |
1206 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
1207 | ||
1208 | if (window->default_widget) | |
1209 | gtk_widget_activate (window->default_widget); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | // Catch GTK event so that GTK doesn't open the drop | |
1214 | // down list upon RETURN. | |
1215 | return; | |
1216 | } | |
1217 | ||
1218 | event.Skip(); | |
1219 | } | |
1220 | ||
1221 | void wxComboBox::DisableEvents() | |
1222 | { | |
1223 | #ifdef __WXGTK24__ | |
1224 | if (!gtk_check_version(2,4,0)) | |
1225 | { | |
1226 | g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget)->child, | |
1227 | (gpointer)gtkcombobox_text_changed_callback, this); | |
1228 | ||
1229 | g_signal_handlers_disconnect_by_func (m_widget, | |
1230 | (gpointer)gtkcombobox_changed_callback, this); | |
1231 | } | |
1232 | else | |
1233 | #endif | |
1234 | { | |
1235 | g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->list, | |
1236 | (gpointer) gtkcombo_combo_select_child_callback, this); | |
1237 | ||
1238 | g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->entry, | |
1239 | (gpointer) gtkcombo_text_changed_callback, this); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | void wxComboBox::EnableEvents() | |
1244 | { | |
1245 | #ifdef __WXGTK24__ | |
1246 | if (!gtk_check_version(2,4,0)) | |
1247 | { | |
1248 | g_signal_connect_after (GTK_BIN(m_widget)->child, "changed", | |
1249 | G_CALLBACK (gtkcombobox_text_changed_callback), this); | |
1250 | ||
1251 | g_signal_connect_after (m_widget, "changed", | |
1252 | G_CALLBACK (gtkcombobox_changed_callback), this); | |
1253 | } | |
1254 | else | |
1255 | #endif | |
1256 | { | |
1257 | g_signal_connect_after (GTK_COMBO(m_widget)->list, "select-child", | |
1258 | G_CALLBACK (gtkcombo_combo_select_child_callback), | |
1259 | this); | |
1260 | g_signal_connect_after (GTK_COMBO(m_widget)->entry, "changed", | |
1261 | G_CALLBACK (gtkcombo_text_changed_callback), | |
1262 | this ); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
1267 | { | |
1268 | #ifdef __WXGTK24__ | |
1269 | if (!gtk_check_version(2,4,0)) | |
1270 | { | |
1271 | // Do nothing | |
1272 | } | |
1273 | else | |
1274 | #endif | |
1275 | { | |
1276 | // NB: In some situations (e.g. on non-first page of a wizard, if the | |
1277 | // size used is default size), GtkCombo widget is resized correctly, | |
1278 | // but it's look is not updated, it's rendered as if it was much wider. | |
1279 | // No other widgets are affected, so it looks like a bug in GTK+. | |
1280 | // Manually requesting resize calculation (as gtk_pizza_set_size does) | |
1281 | // fixes it. | |
1282 | if (GTK_WIDGET_VISIBLE(m_widget)) | |
1283 | gtk_widget_queue_resize(m_widget); | |
1284 | } | |
1285 | ||
1286 | event.Skip(); | |
1287 | } | |
1288 | ||
1289 | void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style) | |
1290 | { | |
1291 | #ifdef __WXGTK24__ | |
1292 | if (!gtk_check_version(2,4,0)) | |
1293 | { | |
1294 | // Do nothing | |
1295 | } | |
1296 | else | |
1297 | #endif | |
1298 | { | |
1299 | // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle ); | |
1300 | ||
1301 | gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style ); | |
1302 | gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style ); | |
1303 | ||
1304 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
1305 | GList *child = list->children; | |
1306 | while (child) | |
1307 | { | |
1308 | gtk_widget_modify_style( GTK_WIDGET(child->data), style ); | |
1309 | ||
1310 | GtkBin *bin = GTK_BIN(child->data); | |
1311 | gtk_widget_modify_style( bin->child, style ); | |
1312 | ||
1313 | child = child->next; | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | GtkWidget* wxComboBox::GetConnectWidget() | |
1319 | { | |
1320 | GtkEntry *entry = NULL; | |
1321 | #ifdef __WXGTK24__ | |
1322 | if (!gtk_check_version(2,4,0)) | |
1323 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); | |
1324 | else | |
1325 | #endif | |
1326 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); | |
1327 | ||
1328 | return GTK_WIDGET( entry ); | |
1329 | } | |
1330 | ||
1331 | GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const | |
1332 | { | |
1333 | #ifdef __WXGTK24__ | |
1334 | if (!gtk_check_version(2,4,0)) | |
1335 | { | |
1336 | wxUnusedVar(windows); | |
1337 | ||
1338 | return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area; | |
1339 | } | |
1340 | else | |
1341 | #endif // GTK+ 2.4 | |
1342 | { | |
1343 | windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area); | |
1344 | windows.push_back(GTK_COMBO(m_widget)->button->window); | |
1345 | ||
1346 | // indicate that we return multiple windows in the windows array | |
1347 | return NULL; | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | wxSize wxComboBox::DoGetBestSize() const | |
1352 | { | |
1353 | wxSize ret( wxControl::DoGetBestSize() ); | |
1354 | ||
1355 | // we know better our horizontal extent: it depends on the longest string | |
1356 | // in the combobox | |
1357 | if ( m_widget ) | |
1358 | { | |
1359 | int width; | |
1360 | unsigned int count = GetCount(); | |
1361 | for ( unsigned int n = 0; n < count; n++ ) | |
1362 | { | |
1363 | GetTextExtent(GetString(n), &width, NULL, NULL, NULL ); | |
1364 | if ( width > ret.x ) | |
1365 | ret.x = width; | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | // empty combobox should have some reasonable default size too | |
1370 | if ( ret.x < 100 ) | |
1371 | ret.x = 100; | |
1372 | ||
1373 | CacheBestSize(ret); | |
1374 | return ret; | |
1375 | } | |
1376 | ||
1377 | // static | |
1378 | wxVisualAttributes | |
1379 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1380 | { | |
1381 | #ifdef __WXGTK24__ | |
1382 | if (!gtk_check_version(2,4,0)) | |
1383 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true); | |
1384 | else | |
1385 | #endif | |
1386 | return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true); | |
1387 | } | |
1388 | ||
1389 | // ---------------------------------------------------------------------------- | |
1390 | // standard event handling | |
1391 | // ---------------------------------------------------------------------------- | |
1392 | ||
1393 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
1394 | { | |
1395 | Cut(); | |
1396 | } | |
1397 | ||
1398 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1399 | { | |
1400 | Copy(); | |
1401 | } | |
1402 | ||
1403 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1404 | { | |
1405 | Paste(); | |
1406 | } | |
1407 | ||
1408 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1409 | { | |
1410 | Undo(); | |
1411 | } | |
1412 | ||
1413 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1414 | { | |
1415 | Redo(); | |
1416 | } | |
1417 | ||
1418 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
1419 | { | |
1420 | long from, to; | |
1421 | GetSelection(& from, & to); | |
1422 | if (from != -1 && to != -1) | |
1423 | Remove(from, to); | |
1424 | } | |
1425 | ||
1426 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
1427 | { | |
1428 | SetSelection(-1, -1); | |
1429 | } | |
1430 | ||
1431 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
1432 | { | |
1433 | event.Enable( CanCut() ); | |
1434 | } | |
1435 | ||
1436 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
1437 | { | |
1438 | event.Enable( CanCopy() ); | |
1439 | } | |
1440 | ||
1441 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
1442 | { | |
1443 | event.Enable( CanPaste() ); | |
1444 | } | |
1445 | ||
1446 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
1447 | { | |
1448 | event.Enable( CanUndo() ); | |
1449 | } | |
1450 | ||
1451 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
1452 | { | |
1453 | event.Enable( CanRedo() ); | |
1454 | } | |
1455 | ||
1456 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
1457 | { | |
1458 | event.Enable(HasSelection() && IsEditable()) ; | |
1459 | } | |
1460 | ||
1461 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
1462 | { | |
1463 | event.Enable(GetLastPosition() > 0); | |
1464 | } | |
1465 | ||
1466 | #endif |