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