]>
Commit | Line | Data |
---|---|---|
53010e52 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
805dd538 | 7 | // Licence: wxWindows licence |
53010e52 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
53010e52 RR |
11 | #pragma implementation "combobox.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
53010e52 | 17 | #include "wx/combobox.h" |
dcf924a3 RR |
18 | |
19 | #if wxUSE_COMBOBOX | |
20 | ||
72a16063 | 21 | #include "wx/settings.h" |
584ad2a3 | 22 | #include "wx/arrstr.h" |
b62c3631 | 23 | #include "wx/intl.h" |
53010e52 | 24 | |
78bcfcfc VZ |
25 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
26 | ||
9e691f46 | 27 | #include "wx/gtk/private.h" |
83624f79 | 28 | |
acfd422a RR |
29 | //----------------------------------------------------------------------------- |
30 | // idle system | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern void wxapp_install_idle_handler(); | |
34 | extern bool g_isIdle; | |
35 | ||
47908e25 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern bool g_blockEventsOnDrag; | |
41 | ||
53010e52 | 42 | //----------------------------------------------------------------------------- |
461573cc | 43 | // "select-child" - click/cursor get select-child, changed, select-child |
47908e25 | 44 | //----------------------------------------------------------------------------- |
47908e25 | 45 | |
8a85884a | 46 | static void |
461573cc | 47 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
53010e52 | 48 | { |
acfd422a | 49 | if (g_isIdle) wxapp_install_idle_handler(); |
8a85884a | 50 | |
a2053b27 | 51 | if (!combo->m_hasVMT) return; |
30ed6e5c | 52 | |
acfd422a | 53 | if (g_blockEventsOnDrag) return; |
805dd538 | 54 | |
159b66c0 | 55 | int curSelection = combo->GetSelection(); |
30ed6e5c | 56 | |
159b66c0 RR |
57 | if (combo->m_prevSelection != curSelection) |
58 | { | |
59 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
60 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
61 | } | |
159b66c0 RR |
62 | combo->m_prevSelection = curSelection; |
63 | ||
8a85884a | 64 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
159b66c0 | 65 | event.SetInt( curSelection ); |
29006414 | 66 | event.SetString( combo->GetStringSelection() ); |
8a85884a | 67 | event.SetEventObject( combo ); |
31528cd3 | 68 | |
8a85884a | 69 | combo->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 70 | } |
47908e25 | 71 | |
0c77152e | 72 | //----------------------------------------------------------------------------- |
461573cc RR |
73 | // "changed" - typing and list item matches get changed, select-child |
74 | // if it doesn't match an item then just get a single changed | |
0c77152e RR |
75 | //----------------------------------------------------------------------------- |
76 | ||
eeccd5d9 | 77 | static void |
0c77152e RR |
78 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
79 | { | |
acfd422a | 80 | if (g_isIdle) wxapp_install_idle_handler(); |
31528cd3 | 81 | |
a2053b27 RR |
82 | if (!combo->m_hasVMT) return; |
83 | ||
8a85884a | 84 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
29006414 | 85 | event.SetString( combo->GetValue() ); |
0c77152e RR |
86 | event.SetEventObject( combo ); |
87 | combo->GetEventHandler()->ProcessEvent( event ); | |
0c77152e RR |
88 | } |
89 | ||
461573cc RR |
90 | static void |
91 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
92 | { | |
93 | } | |
94 | ||
e1e955e1 RR |
95 | //----------------------------------------------------------------------------- |
96 | // wxComboBox | |
53010e52 RR |
97 | //----------------------------------------------------------------------------- |
98 | ||
99 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
100 | ||
b4071e91 | 101 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
fd0eed64 | 102 | EVT_SIZE(wxComboBox::OnSize) |
8a85884a | 103 | EVT_CHAR(wxComboBox::OnChar) |
b4071e91 RR |
104 | END_EVENT_TABLE() |
105 | ||
584ad2a3 MB |
106 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
107 | const wxString& value, | |
108 | const wxPoint& pos, const wxSize& size, | |
109 | const wxArrayString& choices, | |
110 | long style, const wxValidator& validator, | |
111 | const wxString& name ) | |
112 | { | |
113 | wxCArrayString chs(choices); | |
114 | ||
115 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
116 | chs.GetStrings(), style, validator, name ); | |
117 | } | |
118 | ||
fd0eed64 RR |
119 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
120 | const wxPoint& pos, const wxSize& size, | |
121 | int n, const wxString choices[], | |
805dd538 VZ |
122 | long style, const wxValidator& validator, |
123 | const wxString& name ) | |
53010e52 | 124 | { |
fd0eed64 RR |
125 | m_alreadySent = FALSE; |
126 | m_needParent = TRUE; | |
b292e2f5 | 127 | m_acceptsFocus = TRUE; |
159b66c0 | 128 | m_prevSelection = 0; |
805dd538 | 129 | |
db434467 | 130 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
131 | !CreateBase( parent, id, pos, size, style, validator, name )) |
132 | { | |
223d09f6 | 133 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
9d9b7755 | 134 | return FALSE; |
4dcaf11a | 135 | } |
6de97a3b | 136 | |
fd0eed64 | 137 | m_widget = gtk_combo_new(); |
461573cc | 138 | GtkCombo *combo = GTK_COMBO(m_widget); |
30ed6e5c | 139 | |
461573cc RR |
140 | // Disable GTK's broken events ... |
141 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); | |
142 | // ... and add surogate handler. | |
143 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", | |
144 | (GtkSignalFunc) gtk_dummy_callback, combo); | |
805dd538 | 145 | |
8a85884a | 146 | // make it more useable |
3ca6a5f0 | 147 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
30ed6e5c | 148 | |
3ca6a5f0 BP |
149 | // and case-sensitive |
150 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
151 | ||
fd0eed64 | 152 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 153 | |
2e1d7104 | 154 | #ifndef __WXGTK20__ |
81a0614b | 155 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); |
2e1d7104 | 156 | #endif |
159b66c0 | 157 | |
fd0eed64 RR |
158 | for (int i = 0; i < n; i++) |
159 | { | |
fab591c5 | 160 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
805dd538 | 161 | |
fd0eed64 | 162 | m_clientDataList.Append( (wxObject*)NULL ); |
f5e27805 | 163 | m_clientObjectList.Append( (wxObject*)NULL ); |
805dd538 | 164 | |
fd0eed64 | 165 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
805dd538 | 166 | |
19da4326 | 167 | gtk_widget_show( list_item ); |
fd0eed64 | 168 | } |
805dd538 | 169 | |
f03fc89f | 170 | m_parent->DoAddChild( this ); |
30ed6e5c | 171 | |
461573cc | 172 | m_focusWidget = combo->entry; |
805dd538 | 173 | |
abdeb9e7 | 174 | PostCreation(size); |
53010e52 | 175 | |
461573cc | 176 | ConnectWidget( combo->button ); |
805dd538 | 177 | |
461573cc RR |
178 | // MSW's combo box shows the value and the selection is -1 |
179 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
180 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
805dd538 | 181 | |
a260fe6a | 182 | if (style & wxCB_READONLY) |
461573cc | 183 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
a260fe6a | 184 | |
461573cc RR |
185 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
186 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
187 | ||
188 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
189 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
805dd538 | 190 | |
abdeb9e7 | 191 | SetBestSize(size); // need this too because this is a wxControlWithItems |
805dd538 | 192 | |
abdeb9e7 RD |
193 | // This is required for tool bar support |
194 | wxSize setsize = GetSize(); | |
195 | gtk_widget_set_usize( m_widget, setsize.x, setsize.y ); | |
196 | ||
fd0eed64 RR |
197 | return TRUE; |
198 | } | |
199 | ||
200 | wxComboBox::~wxComboBox() | |
201 | { | |
222ed1d6 | 202 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
203 | while (node) |
204 | { | |
b1d4dd7a | 205 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 206 | if (cd) delete cd; |
b1d4dd7a | 207 | node = node->GetNext(); |
fd0eed64 | 208 | } |
7d6d2cd4 RR |
209 | m_clientObjectList.Clear(); |
210 | ||
fd0eed64 | 211 | m_clientDataList.Clear(); |
6de97a3b | 212 | } |
53010e52 | 213 | |
2b5f62a0 VZ |
214 | void wxComboBox::SetFocus() |
215 | { | |
216 | if ( m_hasFocus ) | |
217 | { | |
218 | // don't do anything if we already have focus | |
219 | return; | |
220 | } | |
221 | ||
222 | gtk_widget_grab_focus( m_focusWidget ); | |
223 | } | |
224 | ||
6f6f938f | 225 | int wxComboBox::DoAppend( const wxString &item ) |
53010e52 | 226 | { |
2a68b7a0 | 227 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 228 | |
461573cc | 229 | DisableEvents(); |
30ed6e5c | 230 | |
fd0eed64 | 231 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 232 | |
fab591c5 | 233 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
805dd538 | 234 | |
ec5d85fb RR |
235 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
236 | ||
2b07d713 RR |
237 | if (GTK_WIDGET_REALIZED(m_widget)) |
238 | { | |
239 | gtk_widget_realize( list_item ); | |
240 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
241 | ||
242 | if (m_widgetStyle) ApplyWidgetStyle(); | |
243 | } | |
805dd538 | 244 | |
fd0eed64 | 245 | gtk_widget_show( list_item ); |
30ed6e5c | 246 | |
6f6f938f | 247 | const int count = GetCount(); |
53010e52 | 248 | |
6f6f938f | 249 | if ( (int)m_clientDataList.GetCount() < count ) |
f5e27805 | 250 | m_clientDataList.Append( (wxObject*) NULL ); |
6f6f938f | 251 | if ( (int)m_clientObjectList.GetCount() < count ) |
f5e27805 | 252 | m_clientObjectList.Append( (wxObject*) NULL ); |
805dd538 | 253 | |
6f6f938f | 254 | EnableEvents(); |
805dd538 | 255 | |
6f6f938f | 256 | return count - 1; |
fd0eed64 RR |
257 | } |
258 | ||
6f6f938f | 259 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
243dbf1a | 260 | { |
708c45a6 VZ |
261 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
262 | wxT("can't insert into sorted list")); | |
263 | ||
264 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
243dbf1a VZ |
265 | |
266 | int count = GetCount(); | |
6f6f938f VZ |
267 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
268 | ||
243dbf1a | 269 | if (pos == count) |
6f6f938f | 270 | return Append(item); |
243dbf1a VZ |
271 | |
272 | DisableEvents(); | |
273 | ||
274 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
275 | ||
276 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
277 | ||
278 | GList *gitem_list = g_list_alloc (); | |
279 | gitem_list->data = list_item; | |
280 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
281 | ||
282 | if (GTK_WIDGET_REALIZED(m_widget)) | |
283 | { | |
284 | gtk_widget_realize( list_item ); | |
285 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
286 | ||
708c45a6 VZ |
287 | if (m_widgetStyle) |
288 | ApplyWidgetStyle(); | |
243dbf1a VZ |
289 | } |
290 | ||
291 | gtk_widget_show( list_item ); | |
292 | ||
6f6f938f | 293 | count = GetCount(); |
243dbf1a | 294 | |
6f6f938f | 295 | if ( (int)m_clientDataList.GetCount() < count ) |
243dbf1a | 296 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
6f6f938f | 297 | if ( (int)m_clientObjectList.GetCount() < count ) |
243dbf1a VZ |
298 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
299 | ||
6f6f938f | 300 | EnableEvents(); |
243dbf1a | 301 | |
6f6f938f | 302 | return pos; |
243dbf1a VZ |
303 | } |
304 | ||
6f6f938f | 305 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 306 | { |
223d09f6 | 307 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 308 | |
222ed1d6 | 309 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
fd0eed64 | 310 | if (!node) return; |
805dd538 | 311 | |
f5e27805 | 312 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 313 | } |
53010e52 | 314 | |
6f6f938f | 315 | void* wxComboBox::DoGetItemClientData( int n ) const |
53010e52 | 316 | { |
223d09f6 | 317 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
805dd538 | 318 | |
222ed1d6 | 319 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
805dd538 | 320 | |
30ed6e5c | 321 | return node ? node->GetData() : NULL; |
fd0eed64 RR |
322 | } |
323 | ||
6f6f938f | 324 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 325 | { |
223d09f6 | 326 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 327 | |
222ed1d6 | 328 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
fd0eed64 | 329 | if (!node) return; |
805dd538 | 330 | |
e94e2e95 | 331 | // wxItemContainer already deletes data for us |
805dd538 | 332 | |
fd0eed64 | 333 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 334 | } |
53010e52 | 335 | |
6f6f938f | 336 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
53010e52 | 337 | { |
223d09f6 | 338 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
805dd538 | 339 | |
222ed1d6 | 340 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
805dd538 | 341 | |
30ed6e5c | 342 | return node ? (wxClientData*) node->GetData() : NULL; |
fd0eed64 RR |
343 | } |
344 | ||
345 | void wxComboBox::Clear() | |
346 | { | |
223d09f6 | 347 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 348 | |
461573cc | 349 | DisableEvents(); |
30ed6e5c | 350 | |
fd0eed64 | 351 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
6f6f938f | 352 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
805dd538 | 353 | |
222ed1d6 | 354 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
355 | while (node) |
356 | { | |
b1d4dd7a | 357 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 358 | if (cd) delete cd; |
b1d4dd7a | 359 | node = node->GetNext(); |
fd0eed64 | 360 | } |
f5e27805 | 361 | m_clientObjectList.Clear(); |
805dd538 | 362 | |
fd0eed64 | 363 | m_clientDataList.Clear(); |
30ed6e5c | 364 | |
461573cc | 365 | EnableEvents(); |
6de97a3b | 366 | } |
53010e52 | 367 | |
fd0eed64 | 368 | void wxComboBox::Delete( int n ) |
53010e52 | 369 | { |
223d09f6 | 370 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 371 | |
fd0eed64 | 372 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
805dd538 | 373 | |
fd0eed64 | 374 | GList *child = g_list_nth( listbox->children, n ); |
805dd538 | 375 | |
fd0eed64 RR |
376 | if (!child) |
377 | { | |
223d09f6 | 378 | wxFAIL_MSG(wxT("wrong index")); |
fd0eed64 RR |
379 | return; |
380 | } | |
805dd538 | 381 | |
461573cc | 382 | DisableEvents(); |
30ed6e5c | 383 | |
bbe0af5b | 384 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
385 | gtk_list_remove_items( listbox, list ); |
386 | g_list_free( list ); | |
805dd538 | 387 | |
222ed1d6 | 388 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
f5e27805 | 389 | if (node) |
fd0eed64 | 390 | { |
b1d4dd7a | 391 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 392 | if (cd) delete cd; |
222ed1d6 | 393 | m_clientObjectList.Erase( node ); |
f5e27805 | 394 | } |
805dd538 | 395 | |
b1d4dd7a | 396 | node = m_clientDataList.Item( n ); |
f5e27805 | 397 | if (node) |
222ed1d6 MB |
398 | m_clientDataList.Erase( node ); |
399 | ||
461573cc RR |
400 | EnableEvents(); |
401 | } | |
402 | ||
403 | void wxComboBox::SetString(int n, const wxString &text) | |
404 | { | |
405 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
406 | ||
407 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
408 | ||
409 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
410 | if (child) | |
411 | { | |
412 | GtkBin *bin = GTK_BIN( child->data ); | |
413 | GtkLabel *label = GTK_LABEL( bin->child ); | |
414 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
415 | } | |
416 | else | |
417 | { | |
418 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
fd0eed64 | 419 | } |
6de97a3b | 420 | } |
53010e52 | 421 | |
6f6f938f | 422 | int wxComboBox::FindString( const wxString &item ) const |
53010e52 | 423 | { |
223d09f6 | 424 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 425 | |
fd0eed64 | 426 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 427 | |
53010e52 RR |
428 | GList *child = GTK_LIST(list)->children; |
429 | int count = 0; | |
430 | while (child) | |
431 | { | |
fd0eed64 RR |
432 | GtkBin *bin = GTK_BIN( child->data ); |
433 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2b5f62a0 VZ |
434 | #ifdef __WXGTK20__ |
435 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
436 | #else | |
437 | wxString str( label->label ); | |
438 | #endif | |
439 | if (item == str) | |
7cf8cb48 | 440 | return count; |
30ed6e5c | 441 | |
fd0eed64 RR |
442 | count++; |
443 | child = child->next; | |
444 | } | |
805dd538 | 445 | |
7cf8cb48 | 446 | return wxNOT_FOUND; |
fd0eed64 RR |
447 | } |
448 | ||
449 | int wxComboBox::GetSelection() const | |
450 | { | |
223d09f6 | 451 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 452 | |
fd0eed64 | 453 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 454 | |
fd0eed64 RR |
455 | GList *selection = GTK_LIST(list)->selection; |
456 | if (selection) | |
457 | { | |
458 | GList *child = GTK_LIST(list)->children; | |
459 | int count = 0; | |
460 | while (child) | |
461 | { | |
462 | if (child->data == selection->data) return count; | |
463 | count++; | |
464 | child = child->next; | |
465 | } | |
6de97a3b | 466 | } |
805dd538 | 467 | |
fd0eed64 | 468 | return -1; |
6de97a3b | 469 | } |
53010e52 | 470 | |
debe6624 | 471 | wxString wxComboBox::GetString( int n ) const |
53010e52 | 472 | { |
223d09f6 | 473 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 474 | |
fd0eed64 | 475 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 476 | |
7cf8cb48 | 477 | wxString str; |
fd0eed64 RR |
478 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
479 | if (child) | |
480 | { | |
481 | GtkBin *bin = GTK_BIN( child->data ); | |
482 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2e1d7104 | 483 | #ifdef __WXGTK20__ |
2b5f62a0 | 484 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
2e1d7104 RR |
485 | #else |
486 | str = wxString( label->label ); | |
487 | #endif | |
7cf8cb48 VZ |
488 | } |
489 | else | |
490 | { | |
223d09f6 | 491 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
fd0eed64 | 492 | } |
805dd538 | 493 | |
7cf8cb48 | 494 | return str; |
6de97a3b | 495 | } |
53010e52 | 496 | |
fd0eed64 | 497 | wxString wxComboBox::GetStringSelection() const |
53010e52 | 498 | { |
223d09f6 | 499 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 500 | |
fd0eed64 | 501 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 502 | |
fd0eed64 RR |
503 | GList *selection = GTK_LIST(list)->selection; |
504 | if (selection) | |
505 | { | |
506 | GtkBin *bin = GTK_BIN( selection->data ); | |
2b5f62a0 VZ |
507 | GtkLabel *label = GTK_LABEL( bin->child ); |
508 | #ifdef __WXGTK20__ | |
509 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
510 | #else | |
511 | wxString tmp( label->label ); | |
512 | #endif | |
fd0eed64 RR |
513 | return tmp; |
514 | } | |
805dd538 | 515 | |
223d09f6 | 516 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
805dd538 | 517 | |
223d09f6 | 518 | return wxT(""); |
6de97a3b | 519 | } |
53010e52 | 520 | |
6f6f938f | 521 | int wxComboBox::GetCount() const |
53010e52 | 522 | { |
223d09f6 | 523 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
805dd538 | 524 | |
fd0eed64 | 525 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 526 | |
fd0eed64 RR |
527 | GList *child = GTK_LIST(list)->children; |
528 | int count = 0; | |
529 | while (child) { count++; child = child->next; } | |
530 | return count; | |
6de97a3b | 531 | } |
53010e52 | 532 | |
debe6624 | 533 | void wxComboBox::SetSelection( int n ) |
53010e52 | 534 | { |
223d09f6 | 535 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 536 | |
953704c1 RR |
537 | DisableEvents(); |
538 | ||
fd0eed64 | 539 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
159b66c0 | 540 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
fd0eed64 | 541 | gtk_list_select_item( GTK_LIST(list), n ); |
159b66c0 | 542 | m_prevSelection = n; |
953704c1 RR |
543 | |
544 | EnableEvents(); | |
6de97a3b | 545 | } |
53010e52 | 546 | |
95561ddf | 547 | bool wxComboBox::SetStringSelection( const wxString &string ) |
47908e25 | 548 | { |
95561ddf | 549 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") ); |
805dd538 | 550 | |
fd0eed64 | 551 | int res = FindString( string ); |
95561ddf | 552 | if (res == -1) return false; |
fd0eed64 | 553 | SetSelection( res ); |
95561ddf | 554 | return true; |
6de97a3b | 555 | } |
47908e25 | 556 | |
fd0eed64 | 557 | wxString wxComboBox::GetValue() const |
53010e52 | 558 | { |
2e1d7104 RR |
559 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
560 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
561 | ||
30ed6e5c | 562 | #if 0 |
2e1d7104 RR |
563 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
564 | { | |
565 | wxChar c = tmp[i]; | |
566 | printf( "%d ", (int) (c) ); | |
567 | } | |
568 | printf( "\n" ); | |
569 | #endif | |
30ed6e5c | 570 | |
fd0eed64 | 571 | return tmp; |
6de97a3b | 572 | } |
53010e52 RR |
573 | |
574 | void wxComboBox::SetValue( const wxString& value ) | |
575 | { | |
223d09f6 | 576 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 577 | |
fd0eed64 | 578 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 579 | wxString tmp = wxT(""); |
fd0eed64 | 580 | if (!value.IsNull()) tmp = value; |
fab591c5 | 581 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
6de97a3b | 582 | } |
53010e52 | 583 | |
fd0eed64 | 584 | void wxComboBox::Copy() |
53010e52 | 585 | { |
223d09f6 | 586 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 587 | |
fd0eed64 | 588 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 589 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 590 | } |
53010e52 | 591 | |
fd0eed64 | 592 | void wxComboBox::Cut() |
53010e52 | 593 | { |
223d09f6 | 594 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 595 | |
fd0eed64 | 596 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 597 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 598 | } |
53010e52 | 599 | |
fd0eed64 | 600 | void wxComboBox::Paste() |
53010e52 | 601 | { |
223d09f6 | 602 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 603 | |
fd0eed64 | 604 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 605 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 606 | } |
53010e52 | 607 | |
debe6624 | 608 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 609 | { |
223d09f6 | 610 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 611 | |
6f6f938f VZ |
612 | if ( pos == GetLastPosition() ) |
613 | pos = -1; | |
614 | ||
fd0eed64 | 615 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 616 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 617 | } |
53010e52 | 618 | |
fd0eed64 | 619 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 620 | { |
9e691f46 | 621 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 622 | } |
53010e52 | 623 | |
fd0eed64 | 624 | long wxComboBox::GetLastPosition() const |
53010e52 | 625 | { |
fd0eed64 RR |
626 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
627 | int pos = GTK_ENTRY(entry)->text_length; | |
628 | return (long) pos-1; | |
6de97a3b | 629 | } |
53010e52 | 630 | |
debe6624 | 631 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 632 | { |
223d09f6 | 633 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 634 | |
fd0eed64 RR |
635 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
636 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
637 | if (value.IsNull()) return; | |
638 | gint pos = (gint)to; | |
30ed6e5c | 639 | |
2e1d7104 RR |
640 | #if wxUSE_UNICODE |
641 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
642 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
643 | #else | |
644 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
645 | #endif | |
6de97a3b | 646 | } |
53010e52 | 647 | |
20d10ee1 | 648 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 649 | { |
20d10ee1 VZ |
650 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
651 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 652 | } |
53010e52 | 653 | |
20d10ee1 | 654 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 655 | { |
20d10ee1 VZ |
656 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
657 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
658 | } |
659 | ||
8a85884a VZ |
660 | void wxComboBox::OnChar( wxKeyEvent &event ) |
661 | { | |
12a3f227 | 662 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 663 | { |
461573cc RR |
664 | // GTK automatically selects an item if its in the list |
665 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
666 | event.SetString( GetValue() ); | |
667 | event.SetInt( GetSelection() ); | |
668 | event.SetEventObject( this ); | |
3352cfff RR |
669 | |
670 | if (!GetEventHandler()->ProcessEvent( event )) | |
671 | { | |
672 | // This will invoke the dialog default action, such | |
673 | // as the clicking the default button. | |
674 | ||
675 | wxWindow *top_frame = m_parent; | |
676 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
677 | top_frame = top_frame->GetParent(); | |
678 | ||
679 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
680 | { | |
681 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
682 | ||
683 | if (window->default_widget) | |
684 | gtk_widget_activate (window->default_widget); | |
685 | } | |
686 | } | |
30ed6e5c | 687 | |
461573cc RR |
688 | // Catch GTK event so that GTK doesn't open the drop |
689 | // down list upon RETURN. | |
0878fb4c | 690 | return; |
8a85884a | 691 | } |
30ed6e5c | 692 | |
7cf8cb48 | 693 | event.Skip(); |
8a85884a VZ |
694 | } |
695 | ||
953704c1 RR |
696 | void wxComboBox::DisableEvents() |
697 | { | |
461573cc RR |
698 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
699 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
700 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
701 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
702 | } |
703 | ||
704 | void wxComboBox::EnableEvents() | |
705 | { | |
461573cc RR |
706 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
707 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
708 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
709 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
710 | } |
711 | ||
b4071e91 RR |
712 | void wxComboBox::OnSize( wxSizeEvent &event ) |
713 | { | |
f03fc89f | 714 | event.Skip(); |
31528cd3 | 715 | |
b02da6b1 | 716 | #if 0 |
fd0eed64 RR |
717 | int w = 21; |
718 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
805dd538 | 719 | |
fd0eed64 RR |
720 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
721 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
b02da6b1 | 722 | #endif // 0 |
6de97a3b | 723 | } |
53010e52 | 724 | |
58614078 | 725 | void wxComboBox::ApplyWidgetStyle() |
868a2826 | 726 | { |
fd0eed64 | 727 | SetWidgetStyle(); |
805dd538 | 728 | |
72a16063 | 729 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
fd0eed64 RR |
730 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
731 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
805dd538 | 732 | |
fd0eed64 RR |
733 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
734 | GList *child = list->children; | |
735 | while (child) | |
736 | { | |
737 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
805dd538 | 738 | |
fd0eed64 RR |
739 | GtkBin *bin = GTK_BIN(child->data); |
740 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
805dd538 | 741 | |
fd0eed64 RR |
742 | child = child->next; |
743 | } | |
868a2826 | 744 | } |
b4071e91 | 745 | |
fd0eed64 | 746 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 747 | { |
fd0eed64 | 748 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
749 | } |
750 | ||
b4071e91 RR |
751 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
752 | { | |
fd0eed64 RR |
753 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
754 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 755 | } |
ac57418f | 756 | |
f68586e5 VZ |
757 | wxSize wxComboBox::DoGetBestSize() const |
758 | { | |
db434467 | 759 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
760 | |
761 | // we know better our horizontal extent: it depends on the longest string | |
762 | // in the combobox | |
763 | ret.x = 0; | |
764 | if ( m_widget ) | |
765 | { | |
60d85ccb RR |
766 | int width; |
767 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
768 | for ( size_t n = 0; n < count; n++ ) |
769 | { | |
2b1ff57f | 770 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
a6fc8ae3 VZ |
771 | if ( width > ret.x ) |
772 | ret.x = width; | |
773 | } | |
774 | } | |
775 | ||
776 | // empty combobox should have some reasonable default size too | |
777 | if ( ret.x < 100 ) | |
778 | ret.x = 100; | |
db434467 | 779 | return ret; |
f68586e5 VZ |
780 | } |
781 | ||
dcf924a3 | 782 | #endif |