1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
14 #include "wx/combobox.h"
18 #include "wx/settings.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!combo
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (combo
->m_alreadySent
)
52 combo
->m_alreadySent
= FALSE
;
56 combo
->m_alreadySent
= TRUE
;
58 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
59 event
.SetInt( combo
->GetSelection() );
60 event
.SetString( combo
->GetStringSelection() );
61 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
71 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
73 if (g_isIdle
) wxapp_install_idle_handler();
75 if (!combo
->m_hasVMT
) return;
77 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
78 event
.SetString( combo
->GetValue() );
79 event
.SetEventObject( combo
);
80 combo
->GetEventHandler()->ProcessEvent( event
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
89 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
90 EVT_SIZE(wxComboBox::OnSize
)
91 EVT_CHAR(wxComboBox::OnChar
)
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
)
100 m_alreadySent
= FALSE
;
102 m_acceptsFocus
= TRUE
;
104 wxSize
newSize( size
);
112 if (!PreCreation( parent
, pos
, newSize
) ||
113 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
115 wxFAIL_MSG( wxT("wxComboBox creation failed") );
119 m_widget
= gtk_combo_new();
121 // make it more useable
122 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
124 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
126 for (int i
= 0; i
< n
; i
++)
128 /* don't send first event, which GTK sends aways when
129 inserting the first item */
130 m_alreadySent
= TRUE
;
132 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
134 m_clientDataList
.Append( (wxObject
*)NULL
);
135 m_clientObjectList
.Append( (wxObject
*)NULL
);
137 gtk_container_add( GTK_CONTAINER(list
), list_item
);
139 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
140 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
142 gtk_widget_show( list_item
);
145 m_parent
->DoAddChild( this );
149 ConnectWidget( GTK_COMBO(m_widget
)->button
);
151 if (!value
.IsNull()) SetValue( value
);
153 if (style
& wxCB_READONLY
)
154 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
156 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
157 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
159 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
160 SetForegroundColour( parent
->GetForegroundColour() );
161 SetFont( parent
->GetFont() );
168 wxComboBox::~wxComboBox()
170 wxNode
*node
= m_clientObjectList
.First();
173 wxClientData
*cd
= (wxClientData
*)node
->Data();
177 m_clientObjectList
.Clear();
179 m_clientDataList
.Clear();
182 void wxComboBox::AppendCommon( const wxString
&item
)
184 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
186 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
188 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
190 gtk_container_add( GTK_CONTAINER(list
), list_item
);
192 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
193 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
195 if (GTK_WIDGET_REALIZED(m_widget
))
197 gtk_widget_realize( list_item
);
198 gtk_widget_realize( GTK_BIN(list_item
)->child
);
200 if (m_widgetStyle
) ApplyWidgetStyle();
203 gtk_widget_show( list_item
);
206 void wxComboBox::Append( const wxString
&item
)
208 m_clientDataList
.Append( (wxObject
*) NULL
);
209 m_clientObjectList
.Append( (wxObject
*) NULL
);
211 AppendCommon( item
);
214 void wxComboBox::Append( const wxString
&item
, void *clientData
)
216 m_clientDataList
.Append( (wxObject
*) clientData
);
217 m_clientObjectList
.Append( (wxObject
*)NULL
);
219 AppendCommon( item
);
222 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
224 m_clientDataList
.Append( (wxObject
*) NULL
);
225 m_clientObjectList
.Append( (wxObject
*) clientData
);
227 AppendCommon( item
);
230 void wxComboBox::SetClientData( int n
, void* clientData
)
232 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
234 wxNode
*node
= m_clientDataList
.Nth( n
);
237 node
->SetData( (wxObject
*) clientData
);
240 void* wxComboBox::GetClientData( int n
)
242 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
244 wxNode
*node
= m_clientDataList
.Nth( n
);
245 if (!node
) return NULL
;
250 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
252 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
254 wxNode
*node
= m_clientObjectList
.Nth( n
);
257 wxClientData
*cd
= (wxClientData
*) node
->Data();
260 node
->SetData( (wxObject
*) clientData
);
263 wxClientData
* wxComboBox::GetClientObject( int n
)
265 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
267 wxNode
*node
= m_clientDataList
.Nth( n
);
268 if (!node
) return (wxClientData
*) NULL
;
270 return (wxClientData
*) node
->Data();
273 void wxComboBox::Clear()
275 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
277 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
278 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
280 wxNode
*node
= m_clientObjectList
.First();
283 wxClientData
*cd
= (wxClientData
*)node
->Data();
287 m_clientObjectList
.Clear();
289 m_clientDataList
.Clear();
292 void wxComboBox::Delete( int n
)
294 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
296 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
298 GList
*child
= g_list_nth( listbox
->children
, n
);
302 wxFAIL_MSG(wxT("wrong index"));
306 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
307 gtk_list_remove_items( listbox
, list
);
310 wxNode
*node
= m_clientObjectList
.Nth( n
);
313 wxClientData
*cd
= (wxClientData
*)node
->Data();
315 m_clientObjectList
.DeleteNode( node
);
318 node
= m_clientDataList
.Nth( n
);
321 m_clientDataList
.DeleteNode( node
);
325 int wxComboBox::FindString( const wxString
&item
)
327 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
329 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
331 GList
*child
= GTK_LIST(list
)->children
;
335 GtkBin
*bin
= GTK_BIN( child
->data
);
336 GtkLabel
*label
= GTK_LABEL( bin
->child
);
337 if (item
== wxString(label
->label
,*wxConvCurrent
))
346 int wxComboBox::GetSelection() const
348 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
350 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
352 GList
*selection
= GTK_LIST(list
)->selection
;
355 GList
*child
= GTK_LIST(list
)->children
;
359 if (child
->data
== selection
->data
) return count
;
368 wxString
wxComboBox::GetString( int n
) const
370 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
372 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
375 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
378 GtkBin
*bin
= GTK_BIN( child
->data
);
379 GtkLabel
*label
= GTK_LABEL( bin
->child
);
380 str
= wxString(label
->label
,*wxConvCurrent
);
384 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
390 wxString
wxComboBox::GetStringSelection() const
392 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
394 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
396 GList
*selection
= GTK_LIST(list
)->selection
;
399 GtkBin
*bin
= GTK_BIN( selection
->data
);
400 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
404 wxFAIL_MSG( wxT("wxComboBox: no selection") );
409 int wxComboBox::Number() const
411 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
413 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
415 GList
*child
= GTK_LIST(list
)->children
;
417 while (child
) { count
++; child
= child
->next
; }
421 void wxComboBox::SetSelection( int n
)
423 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
427 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
428 gtk_list_select_item( GTK_LIST(list
), n
);
433 void wxComboBox::SetStringSelection( const wxString
&string
)
435 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
437 int res
= FindString( string
);
438 if (res
== -1) return;
442 wxString
wxComboBox::GetValue() const
444 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
445 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
449 void wxComboBox::SetValue( const wxString
& value
)
451 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
453 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
454 wxString tmp
= wxT("");
455 if (!value
.IsNull()) tmp
= value
;
456 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
459 void wxComboBox::Copy()
461 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
463 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 #if (GTK_MINOR_VERSION > 0)
465 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
467 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
471 void wxComboBox::Cut()
473 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
475 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
476 #if (GTK_MINOR_VERSION > 0)
477 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
479 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
483 void wxComboBox::Paste()
485 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
487 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
488 #if (GTK_MINOR_VERSION > 0)
489 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
491 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
495 void wxComboBox::SetInsertionPoint( long pos
)
497 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
499 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
500 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
503 void wxComboBox::SetInsertionPointEnd()
505 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
507 SetInsertionPoint( -1 );
510 long wxComboBox::GetInsertionPoint() const
512 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
513 return (long) GTK_EDITABLE(entry
)->current_pos
;
516 long wxComboBox::GetLastPosition() const
518 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
519 int pos
= GTK_ENTRY(entry
)->text_length
;
523 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
525 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
526 // FIXME: not quite sure how to do this method right in multibyte mode
528 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
529 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
530 if (value
.IsNull()) return;
532 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
535 void wxComboBox::Remove(long from
, long to
)
537 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
539 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
540 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
543 void wxComboBox::SetSelection( long from
, long to
)
545 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
546 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
549 void wxComboBox::SetEditable( bool editable
)
551 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
552 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
555 void wxComboBox::OnChar( wxKeyEvent
&event
)
557 if ( event
.KeyCode() == WXK_RETURN
)
559 wxString value
= GetValue();
563 // make Enter generate "selected" event if there is only one item
564 // in the combobox - without it, it's impossible to select it at
566 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
568 event
.SetString( value
);
569 event
.SetEventObject( this );
570 GetEventHandler()->ProcessEvent( event
);
574 // add the item to the list if it's not there yet
575 if ( FindString(value
) == wxNOT_FOUND
)
579 // and generate the selected event for it
580 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
581 event
.SetInt( Number() - 1 );
582 event
.SetString( value
);
583 event
.SetEventObject( this );
584 GetEventHandler()->ProcessEvent( event
);
586 //else: do nothing, this will open the listbox
593 void wxComboBox::DisableEvents()
595 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
596 GList
*child
= list
->children
;
599 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
600 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
606 void wxComboBox::EnableEvents()
608 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
609 GList
*child
= list
->children
;
612 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
613 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
619 void wxComboBox::OnSize( wxSizeEvent
&event
)
625 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
627 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
628 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
632 void wxComboBox::ApplyWidgetStyle()
636 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
637 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
638 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
640 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
641 GList
*child
= list
->children
;
644 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
646 GtkBin
*bin
= GTK_BIN(child
->data
);
647 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
653 GtkWidget
* wxComboBox::GetConnectWidget()
655 return GTK_COMBO(m_widget
)->entry
;
658 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
660 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
661 (window
== GTK_COMBO(m_widget
)->button
->window
) );
664 wxSize
wxComboBox::DoGetBestSize() const
666 return wxSize(100, 26);