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