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