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