]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/combobox.cpp
1. restored wxEvent::operator=()
[wxWidgets.git] / src / gtk / 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 m_focusWidget = GTK_COMBO(m_widget)->entry;
159
160 PostCreation();
161
162 ConnectWidget( GTK_COMBO(m_widget)->button );
163
164 if (!value.IsNull()) SetValue( value );
165
166 if (style & wxCB_READONLY)
167 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE );
168
169 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
170 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
171
172 wxSize size_best( DoGetBestSize() );
173 wxSize new_size( size );
174 if (new_size.x == -1)
175 new_size.x = size_best.x;
176 if (new_size.y == -1)
177 new_size.y = size_best.y;
178 if (new_size.y > size_best.y)
179 new_size.y = size_best.y;
180 if ((new_size.x != size.x) || (new_size.y != size.y))
181 {
182 SetSize( new_size.x, new_size.y );
183
184 // This is required for tool bar support
185 gtk_widget_set_usize( m_widget, new_size.x, new_size.y );
186 }
187
188
189 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
190 SetForegroundColour( parent->GetForegroundColour() );
191
192 Show( TRUE );
193
194 return TRUE;
195}
196
197wxComboBox::~wxComboBox()
198{
199 wxNode *node = m_clientObjectList.First();
200 while (node)
201 {
202 wxClientData *cd = (wxClientData*)node->Data();
203 if (cd) delete cd;
204 node = node->Next();
205 }
206 m_clientObjectList.Clear();
207
208 m_clientDataList.Clear();
209}
210
211void wxComboBox::AppendCommon( const wxString &item )
212{
213 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
214
215 GtkWidget *list = GTK_COMBO(m_widget)->list;
216
217 GtkWidget *list_item = gtk_list_item_new_with_label( item.mbc_str() );
218
219 gtk_container_add( GTK_CONTAINER(list), list_item );
220
221 gtk_signal_connect( GTK_OBJECT(list_item), "select",
222 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
223
224 if (GTK_WIDGET_REALIZED(m_widget))
225 {
226 gtk_widget_realize( list_item );
227 gtk_widget_realize( GTK_BIN(list_item)->child );
228
229 if (m_widgetStyle) ApplyWidgetStyle();
230 }
231
232 gtk_widget_show( list_item );
233}
234
235void wxComboBox::Append( const wxString &item )
236{
237 m_clientDataList.Append( (wxObject*) NULL );
238 m_clientObjectList.Append( (wxObject*) NULL );
239
240 AppendCommon( item );
241}
242
243void wxComboBox::Append( const wxString &item, void *clientData )
244{
245 m_clientDataList.Append( (wxObject*) clientData );
246 m_clientObjectList.Append( (wxObject*)NULL );
247
248 AppendCommon( item );
249}
250
251void wxComboBox::Append( const wxString &item, wxClientData *clientData )
252{
253 m_clientDataList.Append( (wxObject*) NULL );
254 m_clientObjectList.Append( (wxObject*) clientData );
255
256 AppendCommon( item );
257}
258
259void wxComboBox::SetClientData( int n, void* clientData )
260{
261 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
262
263 wxNode *node = m_clientDataList.Nth( n );
264 if (!node) return;
265
266 node->SetData( (wxObject*) clientData );
267}
268
269void* wxComboBox::GetClientData( int n )
270{
271 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
272
273 wxNode *node = m_clientDataList.Nth( n );
274 if (!node) return NULL;
275
276 return node->Data();
277}
278
279void wxComboBox::SetClientObject( int n, wxClientData* clientData )
280{
281 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
282
283 wxNode *node = m_clientObjectList.Nth( n );
284 if (!node) return;
285
286 wxClientData *cd = (wxClientData*) node->Data();
287 if (cd) delete cd;
288
289 node->SetData( (wxObject*) clientData );
290}
291
292wxClientData* wxComboBox::GetClientObject( int n )
293{
294 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
295
296 wxNode *node = m_clientDataList.Nth( n );
297 if (!node) return (wxClientData*) NULL;
298
299 return (wxClientData*) node->Data();
300}
301
302void wxComboBox::Clear()
303{
304 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
305
306 GtkWidget *list = GTK_COMBO(m_widget)->list;
307 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
308
309 wxNode *node = m_clientObjectList.First();
310 while (node)
311 {
312 wxClientData *cd = (wxClientData*)node->Data();
313 if (cd) delete cd;
314 node = node->Next();
315 }
316 m_clientObjectList.Clear();
317
318 m_clientDataList.Clear();
319}
320
321void wxComboBox::Delete( int n )
322{
323 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
324
325 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
326
327 GList *child = g_list_nth( listbox->children, n );
328
329 if (!child)
330 {
331 wxFAIL_MSG(wxT("wrong index"));
332 return;
333 }
334
335 GList *list = g_list_append( (GList*) NULL, child->data );
336 gtk_list_remove_items( listbox, list );
337 g_list_free( list );
338
339 wxNode *node = m_clientObjectList.Nth( n );
340 if (node)
341 {
342 wxClientData *cd = (wxClientData*)node->Data();
343 if (cd) delete cd;
344 m_clientObjectList.DeleteNode( node );
345 }
346
347 node = m_clientDataList.Nth( n );
348 if (node)
349 {
350 m_clientDataList.DeleteNode( node );
351 }
352}
353
354int wxComboBox::FindString( const wxString &item )
355{
356 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
357
358 GtkWidget *list = GTK_COMBO(m_widget)->list;
359
360 GList *child = GTK_LIST(list)->children;
361 int count = 0;
362 while (child)
363 {
364 GtkBin *bin = GTK_BIN( child->data );
365 GtkLabel *label = GTK_LABEL( bin->child );
366 if (item == wxString(label->label,*wxConvCurrent))
367 return count;
368 count++;
369 child = child->next;
370 }
371
372 return wxNOT_FOUND;
373}
374
375int wxComboBox::GetSelection() const
376{
377 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
378
379 GtkWidget *list = GTK_COMBO(m_widget)->list;
380
381 GList *selection = GTK_LIST(list)->selection;
382 if (selection)
383 {
384 GList *child = GTK_LIST(list)->children;
385 int count = 0;
386 while (child)
387 {
388 if (child->data == selection->data) return count;
389 count++;
390 child = child->next;
391 }
392 }
393
394 return -1;
395}
396
397wxString wxComboBox::GetString( int n ) const
398{
399 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
400
401 GtkWidget *list = GTK_COMBO(m_widget)->list;
402
403 wxString str;
404 GList *child = g_list_nth( GTK_LIST(list)->children, n );
405 if (child)
406 {
407 GtkBin *bin = GTK_BIN( child->data );
408 GtkLabel *label = GTK_LABEL( bin->child );
409 str = wxString(label->label,*wxConvCurrent);
410 }
411 else
412 {
413 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
414 }
415
416 return str;
417}
418
419wxString wxComboBox::GetStringSelection() const
420{
421 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
422
423 GtkWidget *list = GTK_COMBO(m_widget)->list;
424
425 GList *selection = GTK_LIST(list)->selection;
426 if (selection)
427 {
428 GtkBin *bin = GTK_BIN( selection->data );
429 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
430 return tmp;
431 }
432
433 wxFAIL_MSG( wxT("wxComboBox: no selection") );
434
435 return wxT("");
436}
437
438int wxComboBox::Number() const
439{
440 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
441
442 GtkWidget *list = GTK_COMBO(m_widget)->list;
443
444 GList *child = GTK_LIST(list)->children;
445 int count = 0;
446 while (child) { count++; child = child->next; }
447 return count;
448}
449
450void wxComboBox::SetSelection( int n )
451{
452 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
453
454 DisableEvents();
455
456 GtkWidget *list = GTK_COMBO(m_widget)->list;
457 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
458 gtk_list_select_item( GTK_LIST(list), n );
459 m_prevSelection = n;
460
461 EnableEvents();
462}
463
464void wxComboBox::SetStringSelection( const wxString &string )
465{
466 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
467
468 int res = FindString( string );
469 if (res == -1) return;
470 SetSelection( res );
471}
472
473wxString wxComboBox::GetValue() const
474{
475 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
476 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
477 return tmp;
478}
479
480void wxComboBox::SetValue( const wxString& value )
481{
482 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
483
484 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
485 wxString tmp = wxT("");
486 if (!value.IsNull()) tmp = value;
487 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
488}
489
490void wxComboBox::Copy()
491{
492 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
493
494 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
495#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
496 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
497#else
498 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
499#endif
500}
501
502void wxComboBox::Cut()
503{
504 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
505
506 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
507#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
508 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
509#else
510 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
511#endif
512}
513
514void wxComboBox::Paste()
515{
516 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
517
518 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
519#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
520 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
521#else
522 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
523#endif
524}
525
526void wxComboBox::SetInsertionPoint( long pos )
527{
528 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
529
530 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
531 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
532}
533
534void wxComboBox::SetInsertionPointEnd()
535{
536 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
537
538 SetInsertionPoint( -1 );
539}
540
541long wxComboBox::GetInsertionPoint() const
542{
543 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
544 return (long) GTK_EDITABLE(entry)->current_pos;
545}
546
547long wxComboBox::GetLastPosition() const
548{
549 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
550 int pos = GTK_ENTRY(entry)->text_length;
551 return (long) pos-1;
552}
553
554void wxComboBox::Replace( long from, long to, const wxString& value )
555{
556 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
557 // FIXME: not quite sure how to do this method right in multibyte mode
558
559 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
560 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
561 if (value.IsNull()) return;
562 gint pos = (gint)to;
563 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
564}
565
566void wxComboBox::Remove(long from, long to)
567{
568 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
569
570 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
571 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
572}
573
574void wxComboBox::SetSelection( long from, long to )
575{
576 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
577 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
578}
579
580void wxComboBox::SetEditable( bool editable )
581{
582 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
583 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
584}
585
586void wxComboBox::OnChar( wxKeyEvent &event )
587{
588 if ( event.KeyCode() == WXK_RETURN )
589 {
590 wxString value = GetValue();
591
592 if ( Number() == 0 )
593 {
594 // make Enter generate "selected" event if there is only one item
595 // in the combobox - without it, it's impossible to select it at
596 // all!
597 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
598 event.SetInt( 0 );
599 event.SetString( value );
600 event.SetEventObject( this );
601 GetEventHandler()->ProcessEvent( event );
602 }
603 else
604 {
605 // add the item to the list if it's not there yet
606 if ( FindString(value) == wxNOT_FOUND )
607 {
608 Append(value);
609 SetStringSelection(value);
610
611 // and generate the selected event for it
612 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
613 event.SetInt( Number() - 1 );
614 event.SetString( value );
615 event.SetEventObject( this );
616 GetEventHandler()->ProcessEvent( event );
617 }
618 //else: do nothing, this will open the listbox
619 }
620 }
621
622 event.Skip();
623}
624
625void wxComboBox::DisableEvents()
626{
627 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
628 GList *child = list->children;
629 while (child)
630 {
631 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
632 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
633
634 child = child->next;
635 }
636}
637
638void wxComboBox::EnableEvents()
639{
640 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
641 GList *child = list->children;
642 while (child)
643 {
644 gtk_signal_connect( GTK_OBJECT(child->data), "select",
645 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
646
647 child = child->next;
648 }
649}
650
651void wxComboBox::OnSize( wxSizeEvent &event )
652{
653 event.Skip();
654
655#if 0
656 int w = 21;
657 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
658
659 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
660 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
661#endif // 0
662}
663
664void wxComboBox::ApplyWidgetStyle()
665{
666 SetWidgetStyle();
667
668// gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
669 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
670 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
671
672 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
673 GList *child = list->children;
674 while (child)
675 {
676 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
677
678 GtkBin *bin = GTK_BIN(child->data);
679 gtk_widget_set_style( bin->child, m_widgetStyle );
680
681 child = child->next;
682 }
683}
684
685GtkWidget* wxComboBox::GetConnectWidget()
686{
687 return GTK_COMBO(m_widget)->entry;
688}
689
690bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
691{
692 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
693 (window == GTK_COMBO(m_widget)->button->window ) );
694}
695
696wxSize wxComboBox::DoGetBestSize() const
697{
698 wxSize ret( wxControl::DoGetBestSize() );
699
700 // we know better our horizontal extent: it depends on the longest string
701 // in the combobox
702 ret.x = 0;
703 if ( m_widget )
704 {
705 GdkFont *font = m_font.GetInternalFont();
706
707 wxCoord width;
708 size_t count = Number();
709 for ( size_t n = 0; n < count; n++ )
710 {
711 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
712 if ( width > ret.x )
713 ret.x = width;
714 }
715 }
716
717 // empty combobox should have some reasonable default size too
718 if ( ret.x < 100 )
719 ret.x = 100;
720 return ret;
721}
722
723#endif