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