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