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