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