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