]>
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" |
b62c3631 | 22 | #include "wx/intl.h" |
53010e52 | 23 | |
78bcfcfc VZ |
24 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
25 | ||
9e691f46 | 26 | #include "wx/gtk/private.h" |
83624f79 | 27 | |
acfd422a RR |
28 | //----------------------------------------------------------------------------- |
29 | // idle system | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | extern void wxapp_install_idle_handler(); | |
33 | extern bool g_isIdle; | |
34 | ||
47908e25 RR |
35 | //----------------------------------------------------------------------------- |
36 | // data | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | extern bool g_blockEventsOnDrag; | |
40 | ||
53010e52 | 41 | //----------------------------------------------------------------------------- |
461573cc | 42 | // "select-child" - click/cursor get select-child, changed, select-child |
47908e25 | 43 | //----------------------------------------------------------------------------- |
47908e25 | 44 | |
8a85884a | 45 | static void |
461573cc | 46 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
53010e52 | 47 | { |
acfd422a | 48 | if (g_isIdle) wxapp_install_idle_handler(); |
8a85884a | 49 | |
a2053b27 | 50 | if (!combo->m_hasVMT) return; |
30ed6e5c | 51 | |
acfd422a | 52 | if (g_blockEventsOnDrag) return; |
805dd538 | 53 | |
159b66c0 | 54 | int curSelection = combo->GetSelection(); |
30ed6e5c | 55 | |
159b66c0 RR |
56 | if (combo->m_prevSelection != curSelection) |
57 | { | |
58 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
59 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
60 | } | |
159b66c0 RR |
61 | combo->m_prevSelection = curSelection; |
62 | ||
8a85884a | 63 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
159b66c0 | 64 | event.SetInt( curSelection ); |
29006414 | 65 | event.SetString( combo->GetStringSelection() ); |
8a85884a | 66 | event.SetEventObject( combo ); |
31528cd3 | 67 | |
8a85884a | 68 | combo->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 69 | } |
47908e25 | 70 | |
0c77152e | 71 | //----------------------------------------------------------------------------- |
461573cc RR |
72 | // "changed" - typing and list item matches get changed, select-child |
73 | // if it doesn't match an item then just get a single changed | |
0c77152e RR |
74 | //----------------------------------------------------------------------------- |
75 | ||
eeccd5d9 | 76 | static void |
0c77152e RR |
77 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
78 | { | |
acfd422a | 79 | if (g_isIdle) wxapp_install_idle_handler(); |
31528cd3 | 80 | |
a2053b27 RR |
81 | if (!combo->m_hasVMT) return; |
82 | ||
8a85884a | 83 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
29006414 | 84 | event.SetString( combo->GetValue() ); |
0c77152e RR |
85 | event.SetEventObject( combo ); |
86 | combo->GetEventHandler()->ProcessEvent( event ); | |
0c77152e RR |
87 | } |
88 | ||
461573cc RR |
89 | static void |
90 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
91 | { | |
92 | } | |
93 | ||
e1e955e1 RR |
94 | //----------------------------------------------------------------------------- |
95 | // wxComboBox | |
53010e52 RR |
96 | //----------------------------------------------------------------------------- |
97 | ||
98 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
99 | ||
b4071e91 | 100 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
fd0eed64 | 101 | EVT_SIZE(wxComboBox::OnSize) |
8a85884a | 102 | EVT_CHAR(wxComboBox::OnChar) |
b4071e91 RR |
103 | END_EVENT_TABLE() |
104 | ||
fd0eed64 RR |
105 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
106 | const wxPoint& pos, const wxSize& size, | |
107 | int n, const wxString choices[], | |
805dd538 VZ |
108 | long style, const wxValidator& validator, |
109 | const wxString& name ) | |
53010e52 | 110 | { |
fd0eed64 RR |
111 | m_alreadySent = FALSE; |
112 | m_needParent = TRUE; | |
b292e2f5 | 113 | m_acceptsFocus = TRUE; |
159b66c0 | 114 | m_prevSelection = 0; |
805dd538 | 115 | |
db434467 | 116 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
117 | !CreateBase( parent, id, pos, size, style, validator, name )) |
118 | { | |
223d09f6 | 119 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
9d9b7755 | 120 | return FALSE; |
4dcaf11a | 121 | } |
6de97a3b | 122 | |
fd0eed64 | 123 | m_widget = gtk_combo_new(); |
461573cc | 124 | GtkCombo *combo = GTK_COMBO(m_widget); |
30ed6e5c | 125 | |
461573cc RR |
126 | // Disable GTK's broken events ... |
127 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); | |
128 | // ... and add surogate handler. | |
129 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", | |
130 | (GtkSignalFunc) gtk_dummy_callback, combo); | |
805dd538 | 131 | |
8a85884a | 132 | // make it more useable |
3ca6a5f0 | 133 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
30ed6e5c | 134 | |
3ca6a5f0 BP |
135 | // and case-sensitive |
136 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
137 | ||
fd0eed64 | 138 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 139 | |
2e1d7104 | 140 | #ifndef __WXGTK20__ |
81a0614b | 141 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); |
2e1d7104 | 142 | #endif |
159b66c0 | 143 | |
fd0eed64 RR |
144 | for (int i = 0; i < n; i++) |
145 | { | |
fab591c5 | 146 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
805dd538 | 147 | |
fd0eed64 | 148 | m_clientDataList.Append( (wxObject*)NULL ); |
f5e27805 | 149 | m_clientObjectList.Append( (wxObject*)NULL ); |
805dd538 | 150 | |
fd0eed64 | 151 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
805dd538 | 152 | |
19da4326 | 153 | gtk_widget_show( list_item ); |
fd0eed64 | 154 | } |
805dd538 | 155 | |
f03fc89f | 156 | m_parent->DoAddChild( this ); |
30ed6e5c | 157 | |
461573cc | 158 | m_focusWidget = combo->entry; |
805dd538 | 159 | |
fd0eed64 | 160 | PostCreation(); |
e8e24dfa | 161 | InheritAttributes(); |
53010e52 | 162 | |
461573cc | 163 | ConnectWidget( combo->button ); |
805dd538 | 164 | |
461573cc RR |
165 | // MSW's combo box shows the value and the selection is -1 |
166 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
167 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
805dd538 | 168 | |
a260fe6a | 169 | if (style & wxCB_READONLY) |
461573cc | 170 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
a260fe6a | 171 | |
461573cc RR |
172 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
173 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
174 | ||
175 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
176 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
805dd538 | 177 | |
db434467 RR |
178 | wxSize size_best( DoGetBestSize() ); |
179 | wxSize new_size( size ); | |
180 | if (new_size.x == -1) | |
181 | new_size.x = size_best.x; | |
182 | if (new_size.y == -1) | |
183 | new_size.y = size_best.y; | |
184 | if (new_size.y > size_best.y) | |
185 | new_size.y = size_best.y; | |
186 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
4494ad58 | 187 | { |
db434467 | 188 | SetSize( new_size.x, new_size.y ); |
30ed6e5c | 189 | |
4494ad58 RR |
190 | // This is required for tool bar support |
191 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); | |
192 | } | |
193 | ||
fd0eed64 | 194 | Show( TRUE ); |
805dd538 | 195 | |
fd0eed64 RR |
196 | return TRUE; |
197 | } | |
198 | ||
199 | wxComboBox::~wxComboBox() | |
200 | { | |
222ed1d6 | 201 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
202 | while (node) |
203 | { | |
b1d4dd7a | 204 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 205 | if (cd) delete cd; |
b1d4dd7a | 206 | node = node->GetNext(); |
fd0eed64 | 207 | } |
7d6d2cd4 RR |
208 | m_clientObjectList.Clear(); |
209 | ||
fd0eed64 | 210 | m_clientDataList.Clear(); |
6de97a3b | 211 | } |
53010e52 | 212 | |
2b5f62a0 VZ |
213 | void wxComboBox::SetFocus() |
214 | { | |
215 | if ( m_hasFocus ) | |
216 | { | |
217 | // don't do anything if we already have focus | |
218 | return; | |
219 | } | |
220 | ||
221 | gtk_widget_grab_focus( m_focusWidget ); | |
222 | } | |
223 | ||
6f6f938f | 224 | int wxComboBox::DoAppend( const wxString &item ) |
53010e52 | 225 | { |
2a68b7a0 | 226 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 227 | |
461573cc | 228 | DisableEvents(); |
30ed6e5c | 229 | |
fd0eed64 | 230 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 231 | |
fab591c5 | 232 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
805dd538 | 233 | |
ec5d85fb RR |
234 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
235 | ||
2b07d713 RR |
236 | if (GTK_WIDGET_REALIZED(m_widget)) |
237 | { | |
238 | gtk_widget_realize( list_item ); | |
239 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
240 | ||
241 | if (m_widgetStyle) ApplyWidgetStyle(); | |
242 | } | |
805dd538 | 243 | |
fd0eed64 | 244 | gtk_widget_show( list_item ); |
30ed6e5c | 245 | |
6f6f938f | 246 | const int count = GetCount(); |
53010e52 | 247 | |
6f6f938f | 248 | if ( (int)m_clientDataList.GetCount() < count ) |
f5e27805 | 249 | m_clientDataList.Append( (wxObject*) NULL ); |
6f6f938f | 250 | if ( (int)m_clientObjectList.GetCount() < count ) |
f5e27805 | 251 | m_clientObjectList.Append( (wxObject*) NULL ); |
805dd538 | 252 | |
6f6f938f | 253 | EnableEvents(); |
805dd538 | 254 | |
6f6f938f | 255 | return count - 1; |
fd0eed64 RR |
256 | } |
257 | ||
6f6f938f | 258 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
243dbf1a | 259 | { |
708c45a6 VZ |
260 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
261 | wxT("can't insert into sorted list")); | |
262 | ||
263 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
243dbf1a VZ |
264 | |
265 | int count = GetCount(); | |
6f6f938f VZ |
266 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
267 | ||
243dbf1a | 268 | if (pos == count) |
6f6f938f | 269 | return Append(item); |
243dbf1a VZ |
270 | |
271 | DisableEvents(); | |
272 | ||
273 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
274 | ||
275 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
276 | ||
277 | GList *gitem_list = g_list_alloc (); | |
278 | gitem_list->data = list_item; | |
279 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
280 | ||
281 | if (GTK_WIDGET_REALIZED(m_widget)) | |
282 | { | |
283 | gtk_widget_realize( list_item ); | |
284 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
285 | ||
708c45a6 VZ |
286 | if (m_widgetStyle) |
287 | ApplyWidgetStyle(); | |
243dbf1a VZ |
288 | } |
289 | ||
290 | gtk_widget_show( list_item ); | |
291 | ||
6f6f938f | 292 | count = GetCount(); |
243dbf1a | 293 | |
6f6f938f | 294 | if ( (int)m_clientDataList.GetCount() < count ) |
243dbf1a | 295 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
6f6f938f | 296 | if ( (int)m_clientObjectList.GetCount() < count ) |
243dbf1a VZ |
297 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
298 | ||
6f6f938f | 299 | EnableEvents(); |
243dbf1a | 300 | |
6f6f938f | 301 | return pos; |
243dbf1a VZ |
302 | } |
303 | ||
6f6f938f | 304 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 305 | { |
223d09f6 | 306 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 307 | |
222ed1d6 | 308 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
fd0eed64 | 309 | if (!node) return; |
805dd538 | 310 | |
f5e27805 | 311 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 312 | } |
53010e52 | 313 | |
6f6f938f | 314 | void* wxComboBox::DoGetItemClientData( int n ) const |
53010e52 | 315 | { |
223d09f6 | 316 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
805dd538 | 317 | |
222ed1d6 | 318 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
805dd538 | 319 | |
30ed6e5c | 320 | return node ? node->GetData() : NULL; |
fd0eed64 RR |
321 | } |
322 | ||
6f6f938f | 323 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 324 | { |
223d09f6 | 325 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 326 | |
222ed1d6 | 327 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
fd0eed64 | 328 | if (!node) return; |
805dd538 | 329 | |
b1d4dd7a | 330 | wxClientData *cd = (wxClientData*) node->GetData(); |
fd0eed64 | 331 | if (cd) delete cd; |
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 | |
47908e25 RR |
547 | void wxComboBox::SetStringSelection( const wxString &string ) |
548 | { | |
223d09f6 | 549 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 550 | |
fd0eed64 RR |
551 | int res = FindString( string ); |
552 | if (res == -1) return; | |
553 | SetSelection( res ); | |
6de97a3b | 554 | } |
47908e25 | 555 | |
fd0eed64 | 556 | wxString wxComboBox::GetValue() const |
53010e52 | 557 | { |
2e1d7104 RR |
558 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
559 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
560 | ||
30ed6e5c | 561 | #if 0 |
2e1d7104 RR |
562 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
563 | { | |
564 | wxChar c = tmp[i]; | |
565 | printf( "%d ", (int) (c) ); | |
566 | } | |
567 | printf( "\n" ); | |
568 | #endif | |
30ed6e5c | 569 | |
fd0eed64 | 570 | return tmp; |
6de97a3b | 571 | } |
53010e52 RR |
572 | |
573 | void wxComboBox::SetValue( const wxString& value ) | |
574 | { | |
223d09f6 | 575 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 576 | |
fd0eed64 | 577 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 578 | wxString tmp = wxT(""); |
fd0eed64 | 579 | if (!value.IsNull()) tmp = value; |
fab591c5 | 580 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
6de97a3b | 581 | } |
53010e52 | 582 | |
fd0eed64 | 583 | void wxComboBox::Copy() |
53010e52 | 584 | { |
223d09f6 | 585 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 586 | |
fd0eed64 | 587 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 588 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 589 | } |
53010e52 | 590 | |
fd0eed64 | 591 | void wxComboBox::Cut() |
53010e52 | 592 | { |
223d09f6 | 593 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 594 | |
fd0eed64 | 595 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 596 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 597 | } |
53010e52 | 598 | |
fd0eed64 | 599 | void wxComboBox::Paste() |
53010e52 | 600 | { |
223d09f6 | 601 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 602 | |
fd0eed64 | 603 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 604 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 605 | } |
53010e52 | 606 | |
debe6624 | 607 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 608 | { |
223d09f6 | 609 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 610 | |
6f6f938f VZ |
611 | if ( pos == GetLastPosition() ) |
612 | pos = -1; | |
613 | ||
fd0eed64 | 614 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 615 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 616 | } |
53010e52 | 617 | |
fd0eed64 | 618 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 619 | { |
9e691f46 | 620 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 621 | } |
53010e52 | 622 | |
fd0eed64 | 623 | long wxComboBox::GetLastPosition() const |
53010e52 | 624 | { |
fd0eed64 RR |
625 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
626 | int pos = GTK_ENTRY(entry)->text_length; | |
627 | return (long) pos-1; | |
6de97a3b | 628 | } |
53010e52 | 629 | |
debe6624 | 630 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 631 | { |
223d09f6 | 632 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 633 | |
fd0eed64 RR |
634 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
635 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
636 | if (value.IsNull()) return; | |
637 | gint pos = (gint)to; | |
30ed6e5c | 638 | |
2e1d7104 RR |
639 | #if wxUSE_UNICODE |
640 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
641 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
642 | #else | |
643 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
644 | #endif | |
6de97a3b | 645 | } |
53010e52 | 646 | |
20d10ee1 | 647 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 648 | { |
20d10ee1 VZ |
649 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
650 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 651 | } |
53010e52 | 652 | |
20d10ee1 | 653 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 654 | { |
20d10ee1 VZ |
655 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
656 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
657 | } |
658 | ||
8a85884a VZ |
659 | void wxComboBox::OnChar( wxKeyEvent &event ) |
660 | { | |
12a3f227 | 661 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 662 | { |
461573cc RR |
663 | // GTK automatically selects an item if its in the list |
664 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
665 | event.SetString( GetValue() ); | |
666 | event.SetInt( GetSelection() ); | |
667 | event.SetEventObject( this ); | |
3352cfff RR |
668 | |
669 | if (!GetEventHandler()->ProcessEvent( event )) | |
670 | { | |
671 | // This will invoke the dialog default action, such | |
672 | // as the clicking the default button. | |
673 | ||
674 | wxWindow *top_frame = m_parent; | |
675 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
676 | top_frame = top_frame->GetParent(); | |
677 | ||
678 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
679 | { | |
680 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
681 | ||
682 | if (window->default_widget) | |
683 | gtk_widget_activate (window->default_widget); | |
684 | } | |
685 | } | |
30ed6e5c | 686 | |
461573cc RR |
687 | // Catch GTK event so that GTK doesn't open the drop |
688 | // down list upon RETURN. | |
0878fb4c | 689 | return; |
8a85884a | 690 | } |
30ed6e5c | 691 | |
7cf8cb48 | 692 | event.Skip(); |
8a85884a VZ |
693 | } |
694 | ||
953704c1 RR |
695 | void wxComboBox::DisableEvents() |
696 | { | |
461573cc RR |
697 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
698 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
699 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
700 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
701 | } |
702 | ||
703 | void wxComboBox::EnableEvents() | |
704 | { | |
461573cc RR |
705 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
706 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
707 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
708 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
709 | } |
710 | ||
b4071e91 RR |
711 | void wxComboBox::OnSize( wxSizeEvent &event ) |
712 | { | |
f03fc89f | 713 | event.Skip(); |
31528cd3 | 714 | |
b02da6b1 | 715 | #if 0 |
fd0eed64 RR |
716 | int w = 21; |
717 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
805dd538 | 718 | |
fd0eed64 RR |
719 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
720 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
b02da6b1 | 721 | #endif // 0 |
6de97a3b | 722 | } |
53010e52 | 723 | |
58614078 | 724 | void wxComboBox::ApplyWidgetStyle() |
868a2826 | 725 | { |
fd0eed64 | 726 | SetWidgetStyle(); |
805dd538 | 727 | |
72a16063 | 728 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
fd0eed64 RR |
729 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
730 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
805dd538 | 731 | |
fd0eed64 RR |
732 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
733 | GList *child = list->children; | |
734 | while (child) | |
735 | { | |
736 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
805dd538 | 737 | |
fd0eed64 RR |
738 | GtkBin *bin = GTK_BIN(child->data); |
739 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
805dd538 | 740 | |
fd0eed64 RR |
741 | child = child->next; |
742 | } | |
868a2826 | 743 | } |
b4071e91 | 744 | |
fd0eed64 | 745 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 746 | { |
fd0eed64 | 747 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
748 | } |
749 | ||
b4071e91 RR |
750 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
751 | { | |
fd0eed64 RR |
752 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
753 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 754 | } |
ac57418f | 755 | |
f68586e5 VZ |
756 | wxSize wxComboBox::DoGetBestSize() const |
757 | { | |
db434467 | 758 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
759 | |
760 | // we know better our horizontal extent: it depends on the longest string | |
761 | // in the combobox | |
762 | ret.x = 0; | |
763 | if ( m_widget ) | |
764 | { | |
60d85ccb RR |
765 | int width; |
766 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
767 | for ( size_t n = 0; n < count; n++ ) |
768 | { | |
60d85ccb | 769 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); |
a6fc8ae3 VZ |
770 | if ( width > ret.x ) |
771 | ret.x = width; | |
772 | } | |
773 | } | |
774 | ||
775 | // empty combobox should have some reasonable default size too | |
776 | if ( ret.x < 100 ) | |
777 | ret.x = 100; | |
db434467 | 778 | return ret; |
f68586e5 VZ |
779 | } |
780 | ||
dcf924a3 | 781 | #endif |