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