]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
...fixing up a previous Unicode fix.
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "combobox.h"
12 #endif
13
14 #include "wx/combobox.h"
15
16 #if wxUSE_COMBOBOX
17
18 #include "wx/settings.h"
19
20 #include <wx/intl.h>
21
22 #include "gdk/gdk.h"
23 #include "gtk/gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "select"
40 //-----------------------------------------------------------------------------
41
42 static void
43 gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
44 {
45 if (g_isIdle) wxapp_install_idle_handler();
46
47 if (!combo->m_hasVMT) return;
48
49 if (g_blockEventsOnDrag) return;
50
51 if (combo->m_alreadySent)
52 {
53 combo->m_alreadySent = FALSE;
54 return;
55 }
56
57 combo->m_alreadySent = TRUE;
58
59 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
60 event.SetInt( combo->GetSelection() );
61 event.SetString( combo->GetStringSelection() );
62 event.SetEventObject( combo );
63
64 combo->GetEventHandler()->ProcessEvent( event );
65 }
66
67 //-----------------------------------------------------------------------------
68 // "changed"
69 //-----------------------------------------------------------------------------
70
71 static void
72 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
73 {
74 if (g_isIdle) wxapp_install_idle_handler();
75
76 if (!combo->m_hasVMT) return;
77
78 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
79 event.SetString( combo->GetValue() );
80 event.SetEventObject( combo );
81 combo->GetEventHandler()->ProcessEvent( event );
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxComboBox
86 //-----------------------------------------------------------------------------
87
88 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
89
90 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
91 EVT_SIZE(wxComboBox::OnSize)
92 EVT_CHAR(wxComboBox::OnChar)
93 END_EVENT_TABLE()
94
95 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
96 const wxPoint& pos, const wxSize& size,
97 int n, const wxString choices[],
98 long style, const wxValidator& validator,
99 const wxString& name )
100 {
101 m_alreadySent = FALSE;
102 m_needParent = TRUE;
103 m_acceptsFocus = TRUE;
104
105 PreCreation( parent, id, pos, size, style, name );
106
107 #if wxUSE_VALIDATORS
108 SetValidator( validator );
109 #endif
110
111 m_widget = gtk_combo_new();
112
113 // make it more useable
114 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget), TRUE);
115
116 wxSize newSize = size;
117 if (newSize.x == -1)
118 newSize.x = 100;
119 if (newSize.y == -1)
120 newSize.y = 26;
121 SetSize( newSize.x, newSize.y );
122
123 GtkWidget *list = GTK_COMBO(m_widget)->list;
124
125 for (int i = 0; i < n; i++)
126 {
127 /* don't send first event, which GTK sends aways when
128 inserting the first item */
129 m_alreadySent = TRUE;
130
131 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i].mbc_str() );
132
133 m_clientDataList.Append( (wxObject*)NULL );
134 m_clientObjectList.Append( (wxObject*)NULL );
135
136 gtk_container_add( GTK_CONTAINER(list), list_item );
137
138 gtk_signal_connect( GTK_OBJECT(list_item), "select",
139 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
140
141 gtk_widget_show( list_item );
142 }
143
144 m_parent->DoAddChild( this );
145
146 PostCreation();
147
148 ConnectWidget( GTK_COMBO(m_widget)->button );
149
150 if (!value.IsNull()) SetValue( value );
151
152 if (style & wxCB_READONLY)
153 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE );
154
155 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
156 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
157
158 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
159 SetForegroundColour( parent->GetForegroundColour() );
160 SetFont( parent->GetFont() );
161
162 Show( TRUE );
163
164 return TRUE;
165 }
166
167 wxComboBox::~wxComboBox()
168 {
169 wxNode *node = m_clientDataList.First();
170 while (node)
171 {
172 wxClientData *cd = (wxClientData*)node->Data();
173 if (cd) delete cd;
174 node = node->Next();
175 }
176 m_clientDataList.Clear();
177 }
178
179 void wxComboBox::AppendCommon( const wxString &item )
180 {
181 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
182
183 GtkWidget *list = GTK_COMBO(m_widget)->list;
184
185 GtkWidget *list_item = gtk_list_item_new_with_label( item.mbc_str() );
186
187 gtk_container_add( GTK_CONTAINER(list), list_item );
188
189 gtk_signal_connect( GTK_OBJECT(list_item), "select",
190 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
191
192 if (GTK_WIDGET_REALIZED(m_widget))
193 {
194 gtk_widget_realize( list_item );
195 gtk_widget_realize( GTK_BIN(list_item)->child );
196
197 if (m_widgetStyle) ApplyWidgetStyle();
198 }
199
200 gtk_widget_show( list_item );
201 }
202
203 void wxComboBox::Append( const wxString &item )
204 {
205 m_clientDataList.Append( (wxObject*) NULL );
206 m_clientObjectList.Append( (wxObject*) NULL );
207
208 AppendCommon( item );
209 }
210
211 void wxComboBox::Append( const wxString &item, void *clientData )
212 {
213 m_clientDataList.Append( (wxObject*) clientData );
214 m_clientObjectList.Append( (wxObject*)NULL );
215
216 AppendCommon( item );
217 }
218
219 void wxComboBox::Append( const wxString &item, wxClientData *clientData )
220 {
221 m_clientDataList.Append( (wxObject*) NULL );
222 m_clientObjectList.Append( (wxObject*) clientData );
223
224 AppendCommon( item );
225 }
226
227 void wxComboBox::SetClientData( int n, void* clientData )
228 {
229 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
230
231 wxNode *node = m_clientDataList.Nth( n );
232 if (!node) return;
233
234 node->SetData( (wxObject*) clientData );
235 }
236
237 void* wxComboBox::GetClientData( int n )
238 {
239 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
240
241 wxNode *node = m_clientDataList.Nth( n );
242 if (!node) return NULL;
243
244 return node->Data();
245 }
246
247 void wxComboBox::SetClientObject( int n, wxClientData* clientData )
248 {
249 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
250
251 wxNode *node = m_clientObjectList.Nth( n );
252 if (!node) return;
253
254 wxClientData *cd = (wxClientData*) node->Data();
255 if (cd) delete cd;
256
257 node->SetData( (wxObject*) clientData );
258 }
259
260 wxClientData* wxComboBox::GetClientObject( int n )
261 {
262 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, _T("invalid combobox") );
263
264 wxNode *node = m_clientDataList.Nth( n );
265 if (!node) return (wxClientData*) NULL;
266
267 return (wxClientData*) node->Data();
268 }
269
270 void wxComboBox::Clear()
271 {
272 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
273
274 GtkWidget *list = GTK_COMBO(m_widget)->list;
275 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
276
277 wxNode *node = m_clientObjectList.First();
278 while (node)
279 {
280 wxClientData *cd = (wxClientData*)node->Data();
281 if (cd) delete cd;
282 node = node->Next();
283 }
284 m_clientObjectList.Clear();
285
286 m_clientDataList.Clear();
287 }
288
289 void wxComboBox::Delete( int n )
290 {
291 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
292
293 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
294
295 GList *child = g_list_nth( listbox->children, n );
296
297 if (!child)
298 {
299 wxFAIL_MSG(_T("wrong index"));
300 return;
301 }
302
303 GList *list = g_list_append( (GList*) NULL, child->data );
304 gtk_list_remove_items( listbox, list );
305 g_list_free( list );
306
307 wxNode *node = m_clientObjectList.Nth( n );
308 if (node)
309 {
310 wxClientData *cd = (wxClientData*)node->Data();
311 if (cd) delete cd;
312 m_clientObjectList.DeleteNode( node );
313 }
314
315 node = m_clientDataList.Nth( n );
316 if (node)
317 {
318 m_clientDataList.DeleteNode( node );
319 }
320 }
321
322 int wxComboBox::FindString( const wxString &item )
323 {
324 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid combobox") );
325
326 GtkWidget *list = GTK_COMBO(m_widget)->list;
327
328 GList *child = GTK_LIST(list)->children;
329 int count = 0;
330 while (child)
331 {
332 GtkBin *bin = GTK_BIN( child->data );
333 GtkLabel *label = GTK_LABEL( bin->child );
334 if (item == wxString(label->label,*wxConvCurrent))
335 return count;
336 count++;
337 child = child->next;
338 }
339
340 return wxNOT_FOUND;
341 }
342
343 int wxComboBox::GetSelection() const
344 {
345 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid combobox") );
346
347 GtkWidget *list = GTK_COMBO(m_widget)->list;
348
349 GList *selection = GTK_LIST(list)->selection;
350 if (selection)
351 {
352 GList *child = GTK_LIST(list)->children;
353 int count = 0;
354 while (child)
355 {
356 if (child->data == selection->data) return count;
357 count++;
358 child = child->next;
359 }
360 }
361
362 wxFAIL_MSG( _T("wxComboBox: no selection") );
363
364 return -1;
365 }
366
367 wxString wxComboBox::GetString( int n ) const
368 {
369 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
370
371 GtkWidget *list = GTK_COMBO(m_widget)->list;
372
373 wxString str;
374 GList *child = g_list_nth( GTK_LIST(list)->children, n );
375 if (child)
376 {
377 GtkBin *bin = GTK_BIN( child->data );
378 GtkLabel *label = GTK_LABEL( bin->child );
379 str = wxString(label->label,*wxConvCurrent);
380 }
381 else
382 {
383 wxFAIL_MSG( _T("wxComboBox: wrong index") );
384 }
385
386 return str;
387 }
388
389 wxString wxComboBox::GetStringSelection() const
390 {
391 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
392
393 GtkWidget *list = GTK_COMBO(m_widget)->list;
394
395 GList *selection = GTK_LIST(list)->selection;
396 if (selection)
397 {
398 GtkBin *bin = GTK_BIN( selection->data );
399 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
400 return tmp;
401 }
402
403 wxFAIL_MSG( _T("wxComboBox: no selection") );
404
405 return _T("");
406 }
407
408 int wxComboBox::Number() const
409 {
410 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid combobox") );
411
412 GtkWidget *list = GTK_COMBO(m_widget)->list;
413
414 GList *child = GTK_LIST(list)->children;
415 int count = 0;
416 while (child) { count++; child = child->next; }
417 return count;
418 }
419
420 void wxComboBox::SetSelection( int n )
421 {
422 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
423
424 GtkWidget *list = GTK_COMBO(m_widget)->list;
425 gtk_list_select_item( GTK_LIST(list), n );
426 }
427
428 void wxComboBox::SetStringSelection( const wxString &string )
429 {
430 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
431
432 int res = FindString( string );
433 if (res == -1) return;
434 SetSelection( res );
435 }
436
437 wxString wxComboBox::GetValue() const
438 {
439 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
440 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
441 return tmp;
442 }
443
444 void wxComboBox::SetValue( const wxString& value )
445 {
446 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
447
448 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
449 wxString tmp = _T("");
450 if (!value.IsNull()) tmp = value;
451 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
452 }
453
454 void wxComboBox::Copy()
455 {
456 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
457
458 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
459 #if (GTK_MINOR_VERSION > 0)
460 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
461 #else
462 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
463 #endif
464 }
465
466 void wxComboBox::Cut()
467 {
468 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
469
470 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
471 #if (GTK_MINOR_VERSION > 0)
472 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
473 #else
474 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
475 #endif
476 }
477
478 void wxComboBox::Paste()
479 {
480 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
481
482 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
483 #if (GTK_MINOR_VERSION > 0)
484 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
485 #else
486 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
487 #endif
488 }
489
490 void wxComboBox::SetInsertionPoint( long pos )
491 {
492 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
493
494 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
495 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
496 }
497
498 void wxComboBox::SetInsertionPointEnd()
499 {
500 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
501
502 SetInsertionPoint( -1 );
503 }
504
505 long wxComboBox::GetInsertionPoint() const
506 {
507 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
508 return (long) GTK_EDITABLE(entry)->current_pos;
509 }
510
511 long wxComboBox::GetLastPosition() const
512 {
513 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
514 int pos = GTK_ENTRY(entry)->text_length;
515 return (long) pos-1;
516 }
517
518 void wxComboBox::Replace( long from, long to, const wxString& value )
519 {
520 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
521 // FIXME: not quite sure how to do this method right in multibyte mode
522
523 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
524 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
525 if (value.IsNull()) return;
526 gint pos = (gint)to;
527 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
528 }
529
530 void wxComboBox::Remove(long from, long to)
531 {
532 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
533
534 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
535 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
536 }
537
538 void wxComboBox::SetSelection( long from, long to )
539 {
540 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
541 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
542 }
543
544 void wxComboBox::SetEditable( bool editable )
545 {
546 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
547 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
548 }
549
550 void wxComboBox::OnChar( wxKeyEvent &event )
551 {
552 if ( event.KeyCode() == WXK_RETURN )
553 {
554 wxString value = GetValue();
555
556 if ( Number() == 0 )
557 {
558 // make Enter generate "selected" event if there is only one item
559 // in the combobox - without it, it's impossible to select it at
560 // all!
561 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
562 event.SetInt( 0 );
563 event.SetString( value );
564 event.SetEventObject( this );
565 GetEventHandler()->ProcessEvent( event );
566 }
567 else
568 {
569 // add the item to the list if it's not there yet
570 if ( FindString(value) == wxNOT_FOUND )
571 {
572 Append(value);
573
574 // and generate the selected event for it
575 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
576 event.SetInt( Number() - 1 );
577 event.SetString( value );
578 event.SetEventObject( this );
579 GetEventHandler()->ProcessEvent( event );
580 }
581 //else: do nothing, this will open the listbox
582 }
583 }
584
585 event.Skip();
586 }
587
588 void wxComboBox::OnSize( wxSizeEvent &event )
589 {
590 event.Skip();
591
592 return;
593
594 int w = 21;
595 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
596
597 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
598 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
599 }
600
601 void wxComboBox::ApplyWidgetStyle()
602 {
603 SetWidgetStyle();
604
605 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
606 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
607 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
608
609 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
610 GList *child = list->children;
611 while (child)
612 {
613 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
614
615 GtkBin *bin = GTK_BIN(child->data);
616 gtk_widget_set_style( bin->child, m_widgetStyle );
617
618 child = child->next;
619 }
620 }
621
622 GtkWidget* wxComboBox::GetConnectWidget()
623 {
624 return GTK_COMBO(m_widget)->entry;
625 }
626
627 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
628 {
629 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
630 (window == GTK_COMBO(m_widget)->button->window ) );
631 }
632
633 #endif