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