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