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