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