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