]>
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 | |
e94e2e95 | 330 | // wxItemContainer already deletes data for us |
805dd538 | 331 | |
fd0eed64 | 332 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 333 | } |
53010e52 | 334 | |
6f6f938f | 335 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
53010e52 | 336 | { |
223d09f6 | 337 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
805dd538 | 338 | |
222ed1d6 | 339 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
805dd538 | 340 | |
30ed6e5c | 341 | return node ? (wxClientData*) node->GetData() : NULL; |
fd0eed64 RR |
342 | } |
343 | ||
344 | void wxComboBox::Clear() | |
345 | { | |
223d09f6 | 346 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 347 | |
461573cc | 348 | DisableEvents(); |
30ed6e5c | 349 | |
fd0eed64 | 350 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
6f6f938f | 351 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
805dd538 | 352 | |
222ed1d6 | 353 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
354 | while (node) |
355 | { | |
b1d4dd7a | 356 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 357 | if (cd) delete cd; |
b1d4dd7a | 358 | node = node->GetNext(); |
fd0eed64 | 359 | } |
f5e27805 | 360 | m_clientObjectList.Clear(); |
805dd538 | 361 | |
fd0eed64 | 362 | m_clientDataList.Clear(); |
30ed6e5c | 363 | |
461573cc | 364 | EnableEvents(); |
6de97a3b | 365 | } |
53010e52 | 366 | |
fd0eed64 | 367 | void wxComboBox::Delete( int n ) |
53010e52 | 368 | { |
223d09f6 | 369 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 370 | |
fd0eed64 | 371 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
805dd538 | 372 | |
fd0eed64 | 373 | GList *child = g_list_nth( listbox->children, n ); |
805dd538 | 374 | |
fd0eed64 RR |
375 | if (!child) |
376 | { | |
223d09f6 | 377 | wxFAIL_MSG(wxT("wrong index")); |
fd0eed64 RR |
378 | return; |
379 | } | |
805dd538 | 380 | |
461573cc | 381 | DisableEvents(); |
30ed6e5c | 382 | |
bbe0af5b | 383 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
384 | gtk_list_remove_items( listbox, list ); |
385 | g_list_free( list ); | |
805dd538 | 386 | |
222ed1d6 | 387 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
f5e27805 | 388 | if (node) |
fd0eed64 | 389 | { |
b1d4dd7a | 390 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 391 | if (cd) delete cd; |
222ed1d6 | 392 | m_clientObjectList.Erase( node ); |
f5e27805 | 393 | } |
805dd538 | 394 | |
b1d4dd7a | 395 | node = m_clientDataList.Item( n ); |
f5e27805 | 396 | if (node) |
222ed1d6 MB |
397 | m_clientDataList.Erase( node ); |
398 | ||
461573cc RR |
399 | EnableEvents(); |
400 | } | |
401 | ||
402 | void wxComboBox::SetString(int n, const wxString &text) | |
403 | { | |
404 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
405 | ||
406 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
407 | ||
408 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
409 | if (child) | |
410 | { | |
411 | GtkBin *bin = GTK_BIN( child->data ); | |
412 | GtkLabel *label = GTK_LABEL( bin->child ); | |
413 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
414 | } | |
415 | else | |
416 | { | |
417 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
fd0eed64 | 418 | } |
6de97a3b | 419 | } |
53010e52 | 420 | |
6f6f938f | 421 | int wxComboBox::FindString( const wxString &item ) const |
53010e52 | 422 | { |
223d09f6 | 423 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 424 | |
fd0eed64 | 425 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 426 | |
53010e52 RR |
427 | GList *child = GTK_LIST(list)->children; |
428 | int count = 0; | |
429 | while (child) | |
430 | { | |
fd0eed64 RR |
431 | GtkBin *bin = GTK_BIN( child->data ); |
432 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2b5f62a0 VZ |
433 | #ifdef __WXGTK20__ |
434 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
435 | #else | |
436 | wxString str( label->label ); | |
437 | #endif | |
438 | if (item == str) | |
7cf8cb48 | 439 | return count; |
30ed6e5c | 440 | |
fd0eed64 RR |
441 | count++; |
442 | child = child->next; | |
443 | } | |
805dd538 | 444 | |
7cf8cb48 | 445 | return wxNOT_FOUND; |
fd0eed64 RR |
446 | } |
447 | ||
448 | int wxComboBox::GetSelection() const | |
449 | { | |
223d09f6 | 450 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 451 | |
fd0eed64 | 452 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 453 | |
fd0eed64 RR |
454 | GList *selection = GTK_LIST(list)->selection; |
455 | if (selection) | |
456 | { | |
457 | GList *child = GTK_LIST(list)->children; | |
458 | int count = 0; | |
459 | while (child) | |
460 | { | |
461 | if (child->data == selection->data) return count; | |
462 | count++; | |
463 | child = child->next; | |
464 | } | |
6de97a3b | 465 | } |
805dd538 | 466 | |
fd0eed64 | 467 | return -1; |
6de97a3b | 468 | } |
53010e52 | 469 | |
debe6624 | 470 | wxString wxComboBox::GetString( int n ) const |
53010e52 | 471 | { |
223d09f6 | 472 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 473 | |
fd0eed64 | 474 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 475 | |
7cf8cb48 | 476 | wxString str; |
fd0eed64 RR |
477 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
478 | if (child) | |
479 | { | |
480 | GtkBin *bin = GTK_BIN( child->data ); | |
481 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2e1d7104 | 482 | #ifdef __WXGTK20__ |
2b5f62a0 | 483 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
2e1d7104 RR |
484 | #else |
485 | str = wxString( label->label ); | |
486 | #endif | |
7cf8cb48 VZ |
487 | } |
488 | else | |
489 | { | |
223d09f6 | 490 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
fd0eed64 | 491 | } |
805dd538 | 492 | |
7cf8cb48 | 493 | return str; |
6de97a3b | 494 | } |
53010e52 | 495 | |
fd0eed64 | 496 | wxString wxComboBox::GetStringSelection() const |
53010e52 | 497 | { |
223d09f6 | 498 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 499 | |
fd0eed64 | 500 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 501 | |
fd0eed64 RR |
502 | GList *selection = GTK_LIST(list)->selection; |
503 | if (selection) | |
504 | { | |
505 | GtkBin *bin = GTK_BIN( selection->data ); | |
2b5f62a0 VZ |
506 | GtkLabel *label = GTK_LABEL( bin->child ); |
507 | #ifdef __WXGTK20__ | |
508 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
509 | #else | |
510 | wxString tmp( label->label ); | |
511 | #endif | |
fd0eed64 RR |
512 | return tmp; |
513 | } | |
805dd538 | 514 | |
223d09f6 | 515 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
805dd538 | 516 | |
223d09f6 | 517 | return wxT(""); |
6de97a3b | 518 | } |
53010e52 | 519 | |
6f6f938f | 520 | int wxComboBox::GetCount() const |
53010e52 | 521 | { |
223d09f6 | 522 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
805dd538 | 523 | |
fd0eed64 | 524 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 525 | |
fd0eed64 RR |
526 | GList *child = GTK_LIST(list)->children; |
527 | int count = 0; | |
528 | while (child) { count++; child = child->next; } | |
529 | return count; | |
6de97a3b | 530 | } |
53010e52 | 531 | |
debe6624 | 532 | void wxComboBox::SetSelection( int n ) |
53010e52 | 533 | { |
223d09f6 | 534 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 535 | |
953704c1 RR |
536 | DisableEvents(); |
537 | ||
fd0eed64 | 538 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
159b66c0 | 539 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
fd0eed64 | 540 | gtk_list_select_item( GTK_LIST(list), n ); |
159b66c0 | 541 | m_prevSelection = n; |
953704c1 RR |
542 | |
543 | EnableEvents(); | |
6de97a3b | 544 | } |
53010e52 | 545 | |
47908e25 RR |
546 | void wxComboBox::SetStringSelection( const wxString &string ) |
547 | { | |
223d09f6 | 548 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 549 | |
fd0eed64 RR |
550 | int res = FindString( string ); |
551 | if (res == -1) return; | |
552 | SetSelection( res ); | |
6de97a3b | 553 | } |
47908e25 | 554 | |
fd0eed64 | 555 | wxString wxComboBox::GetValue() const |
53010e52 | 556 | { |
2e1d7104 RR |
557 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
558 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
559 | ||
30ed6e5c | 560 | #if 0 |
2e1d7104 RR |
561 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
562 | { | |
563 | wxChar c = tmp[i]; | |
564 | printf( "%d ", (int) (c) ); | |
565 | } | |
566 | printf( "\n" ); | |
567 | #endif | |
30ed6e5c | 568 | |
fd0eed64 | 569 | return tmp; |
6de97a3b | 570 | } |
53010e52 RR |
571 | |
572 | void wxComboBox::SetValue( const wxString& value ) | |
573 | { | |
223d09f6 | 574 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 575 | |
fd0eed64 | 576 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 577 | wxString tmp = wxT(""); |
fd0eed64 | 578 | if (!value.IsNull()) tmp = value; |
fab591c5 | 579 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
6de97a3b | 580 | } |
53010e52 | 581 | |
fd0eed64 | 582 | void wxComboBox::Copy() |
53010e52 | 583 | { |
223d09f6 | 584 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 585 | |
fd0eed64 | 586 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 587 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 588 | } |
53010e52 | 589 | |
fd0eed64 | 590 | void wxComboBox::Cut() |
53010e52 | 591 | { |
223d09f6 | 592 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 593 | |
fd0eed64 | 594 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 595 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 596 | } |
53010e52 | 597 | |
fd0eed64 | 598 | void wxComboBox::Paste() |
53010e52 | 599 | { |
223d09f6 | 600 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 601 | |
fd0eed64 | 602 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 603 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 604 | } |
53010e52 | 605 | |
debe6624 | 606 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 607 | { |
223d09f6 | 608 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 609 | |
6f6f938f VZ |
610 | if ( pos == GetLastPosition() ) |
611 | pos = -1; | |
612 | ||
fd0eed64 | 613 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 614 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 615 | } |
53010e52 | 616 | |
fd0eed64 | 617 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 618 | { |
9e691f46 | 619 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 620 | } |
53010e52 | 621 | |
fd0eed64 | 622 | long wxComboBox::GetLastPosition() const |
53010e52 | 623 | { |
fd0eed64 RR |
624 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
625 | int pos = GTK_ENTRY(entry)->text_length; | |
626 | return (long) pos-1; | |
6de97a3b | 627 | } |
53010e52 | 628 | |
debe6624 | 629 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 630 | { |
223d09f6 | 631 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 632 | |
fd0eed64 RR |
633 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
634 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
635 | if (value.IsNull()) return; | |
636 | gint pos = (gint)to; | |
30ed6e5c | 637 | |
2e1d7104 RR |
638 | #if wxUSE_UNICODE |
639 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
640 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
641 | #else | |
642 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
643 | #endif | |
6de97a3b | 644 | } |
53010e52 | 645 | |
20d10ee1 | 646 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 647 | { |
20d10ee1 VZ |
648 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
649 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 650 | } |
53010e52 | 651 | |
20d10ee1 | 652 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 653 | { |
20d10ee1 VZ |
654 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
655 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
656 | } |
657 | ||
8a85884a VZ |
658 | void wxComboBox::OnChar( wxKeyEvent &event ) |
659 | { | |
12a3f227 | 660 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 661 | { |
461573cc RR |
662 | // GTK automatically selects an item if its in the list |
663 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
664 | event.SetString( GetValue() ); | |
665 | event.SetInt( GetSelection() ); | |
666 | event.SetEventObject( this ); | |
3352cfff RR |
667 | |
668 | if (!GetEventHandler()->ProcessEvent( event )) | |
669 | { | |
670 | // This will invoke the dialog default action, such | |
671 | // as the clicking the default button. | |
672 | ||
673 | wxWindow *top_frame = m_parent; | |
674 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
675 | top_frame = top_frame->GetParent(); | |
676 | ||
677 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
678 | { | |
679 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
680 | ||
681 | if (window->default_widget) | |
682 | gtk_widget_activate (window->default_widget); | |
683 | } | |
684 | } | |
30ed6e5c | 685 | |
461573cc RR |
686 | // Catch GTK event so that GTK doesn't open the drop |
687 | // down list upon RETURN. | |
0878fb4c | 688 | return; |
8a85884a | 689 | } |
30ed6e5c | 690 | |
7cf8cb48 | 691 | event.Skip(); |
8a85884a VZ |
692 | } |
693 | ||
953704c1 RR |
694 | void wxComboBox::DisableEvents() |
695 | { | |
461573cc RR |
696 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
697 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
698 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
699 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
700 | } |
701 | ||
702 | void wxComboBox::EnableEvents() | |
703 | { | |
461573cc RR |
704 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
705 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
706 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
707 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
708 | } |
709 | ||
b4071e91 RR |
710 | void wxComboBox::OnSize( wxSizeEvent &event ) |
711 | { | |
f03fc89f | 712 | event.Skip(); |
31528cd3 | 713 | |
b02da6b1 | 714 | #if 0 |
fd0eed64 RR |
715 | int w = 21; |
716 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
805dd538 | 717 | |
fd0eed64 RR |
718 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
719 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
b02da6b1 | 720 | #endif // 0 |
6de97a3b | 721 | } |
53010e52 | 722 | |
58614078 | 723 | void wxComboBox::ApplyWidgetStyle() |
868a2826 | 724 | { |
fd0eed64 | 725 | SetWidgetStyle(); |
805dd538 | 726 | |
72a16063 | 727 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
fd0eed64 RR |
728 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
729 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
805dd538 | 730 | |
fd0eed64 RR |
731 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
732 | GList *child = list->children; | |
733 | while (child) | |
734 | { | |
735 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
805dd538 | 736 | |
fd0eed64 RR |
737 | GtkBin *bin = GTK_BIN(child->data); |
738 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
805dd538 | 739 | |
fd0eed64 RR |
740 | child = child->next; |
741 | } | |
868a2826 | 742 | } |
b4071e91 | 743 | |
fd0eed64 | 744 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 745 | { |
fd0eed64 | 746 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
747 | } |
748 | ||
b4071e91 RR |
749 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
750 | { | |
fd0eed64 RR |
751 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
752 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 753 | } |
ac57418f | 754 | |
f68586e5 VZ |
755 | wxSize wxComboBox::DoGetBestSize() const |
756 | { | |
db434467 | 757 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
758 | |
759 | // we know better our horizontal extent: it depends on the longest string | |
760 | // in the combobox | |
761 | ret.x = 0; | |
762 | if ( m_widget ) | |
763 | { | |
60d85ccb RR |
764 | int width; |
765 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
766 | for ( size_t n = 0; n < count; n++ ) |
767 | { | |
60d85ccb | 768 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); |
a6fc8ae3 VZ |
769 | if ( width > ret.x ) |
770 | ret.x = width; | |
771 | } | |
772 | } | |
773 | ||
774 | // empty combobox should have some reasonable default size too | |
775 | if ( ret.x < 100 ) | |
776 | ret.x = 100; | |
db434467 | 777 | return ret; |
f68586e5 VZ |
778 | } |
779 | ||
dcf924a3 | 780 | #endif |