1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
15 #include "wx/combobox.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
34 if (!combo
->HasVMT()) return;
35 if (g_blockEventsOnDrag
) return;
37 if (combo
->m_alreadySent
)
39 combo
->m_alreadySent
= FALSE
;
43 combo
->m_alreadySent
= TRUE
;
45 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
46 event
.SetInt( combo
->GetSelection() );
47 wxString
tmp( combo
->GetStringSelection() );
48 event
.SetString( WXSTRINGCAST(tmp
) );
49 event
.SetEventObject(combo
);
50 combo
->GetEventHandler()->ProcessEvent(event
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
59 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
60 EVT_SIZE(wxComboBox::OnSize
)
63 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
64 const wxPoint
& pos
, const wxSize
& size
,
65 int n
, const wxString choices
[],
66 long style
, const wxValidator
& validator
,
67 const wxString
& name
)
69 m_alreadySent
= FALSE
;
71 m_acceptsFocus
= TRUE
;
73 PreCreation( parent
, id
, pos
, size
, style
, name
);
75 SetValidator( validator
);
77 m_widget
= gtk_combo_new();
79 wxSize newSize
= size
;
80 if (newSize
.x
== -1) newSize
.x
= 100;
81 if (newSize
.y
== -1) newSize
.y
= 26;
82 SetSize( newSize
.x
, newSize
.y
);
84 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
86 for (int i
= 0; i
< n
; i
++)
88 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
90 m_clientDataList
.Append( (wxObject
*)NULL
);
91 m_clientObjectList
.Append( (wxObject
*)NULL
);
93 gtk_container_add( GTK_CONTAINER(list
), list_item
);
95 gtk_widget_realize( list_item
);
96 gtk_widget_realize( GTK_BIN(list_item
)->child
);
98 gtk_widget_show( list_item
);
100 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
101 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
104 m_parent
->AddChild( this );
106 (m_parent
->m_insertCallback
)( m_parent
, this );
110 ConnectWidget( GTK_COMBO(m_widget
)->button
);
112 if (!value
.IsNull()) SetValue( value
);
114 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
115 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
116 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
118 SetBackgroundColour( parent
->GetBackgroundColour() );
119 SetForegroundColour( parent
->GetForegroundColour() );
126 wxComboBox::~wxComboBox()
128 wxNode
*node
= m_clientDataList
.First();
131 wxClientData
*cd
= (wxClientData
*)node
->Data();
135 m_clientDataList
.Clear();
138 void wxComboBox::AppendCommon( const wxString
&item
)
140 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
142 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
144 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
146 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
147 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
149 gtk_container_add( GTK_CONTAINER(list
), list_item
);
151 if (m_widgetStyle
) ApplyWidgetStyle();
153 gtk_widget_show( list_item
);
156 void wxComboBox::Append( const wxString
&item
)
158 m_clientDataList
.Append( (wxObject
*) NULL
);
159 m_clientObjectList
.Append( (wxObject
*) NULL
);
161 AppendCommon( item
);
164 void wxComboBox::Append( const wxString
&item
, void *clientData
)
166 m_clientDataList
.Append( (wxObject
*) clientData
);
167 m_clientObjectList
.Append( (wxObject
*)NULL
);
169 AppendCommon( item
);
172 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
174 m_clientDataList
.Append( (wxObject
*) NULL
);
175 m_clientObjectList
.Append( (wxObject
*) clientData
);
177 AppendCommon( item
);
180 void wxComboBox::SetClientData( int n
, void* clientData
)
182 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
184 wxNode
*node
= m_clientDataList
.Nth( n
);
187 node
->SetData( (wxObject
*) clientData
);
190 void* wxComboBox::GetClientData( int n
)
192 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
194 wxNode
*node
= m_clientDataList
.Nth( n
);
195 if (!node
) return NULL
;
200 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
202 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
204 wxNode
*node
= m_clientObjectList
.Nth( n
);
207 wxClientData
*cd
= (wxClientData
*) node
->Data();
210 node
->SetData( (wxObject
*) clientData
);
213 wxClientData
* wxComboBox::GetClientObject( int n
)
215 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
217 wxNode
*node
= m_clientDataList
.Nth( n
);
218 if (!node
) return (wxClientData
*) NULL
;
220 return (wxClientData
*) node
->Data();
223 void wxComboBox::Clear()
225 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
227 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
228 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
230 wxNode
*node
= m_clientObjectList
.First();
233 wxClientData
*cd
= (wxClientData
*)node
->Data();
237 m_clientObjectList
.Clear();
239 m_clientDataList
.Clear();
242 void wxComboBox::Delete( int n
)
244 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
246 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
248 GList
*child
= g_list_nth( listbox
->children
, n
);
252 wxFAIL_MSG("wrong index");
256 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
257 gtk_list_remove_items( listbox
, list
);
260 wxNode
*node
= m_clientObjectList
.Nth( n
);
263 wxClientData
*cd
= (wxClientData
*)node
->Data();
265 m_clientObjectList
.DeleteNode( node
);
268 node
= m_clientDataList
.Nth( n
);
271 m_clientDataList
.DeleteNode( node
);
275 int wxComboBox::FindString( const wxString
&item
)
277 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
279 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
281 GList
*child
= GTK_LIST(list
)->children
;
285 GtkBin
*bin
= GTK_BIN( child
->data
);
286 GtkLabel
*label
= GTK_LABEL( bin
->child
);
287 if (item
== label
->label
) return count
;
292 wxFAIL_MSG( "wxComboBox: string not found" );
297 int wxComboBox::GetSelection() const
299 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
301 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
303 GList
*selection
= GTK_LIST(list
)->selection
;
306 GList
*child
= GTK_LIST(list
)->children
;
310 if (child
->data
== selection
->data
) return count
;
316 wxFAIL_MSG( "wxComboBox: no selection" );
321 wxString
wxComboBox::GetString( int n
) const
323 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
325 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
327 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
330 GtkBin
*bin
= GTK_BIN( child
->data
);
331 GtkLabel
*label
= GTK_LABEL( bin
->child
);
335 wxFAIL_MSG( "wxComboBox: wrong index" );
340 wxString
wxComboBox::GetStringSelection() const
342 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
344 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
346 GList
*selection
= GTK_LIST(list
)->selection
;
349 GtkBin
*bin
= GTK_BIN( selection
->data
);
350 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
354 wxFAIL_MSG( "wxComboBox: no selection" );
359 int wxComboBox::Number() const
361 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
363 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
365 GList
*child
= GTK_LIST(list
)->children
;
367 while (child
) { count
++; child
= child
->next
; }
371 void wxComboBox::SetSelection( int n
)
373 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
375 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
376 gtk_list_select_item( GTK_LIST(list
), n
);
379 void wxComboBox::SetStringSelection( const wxString
&string
)
381 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
383 int res
= FindString( string
);
384 if (res
== -1) return;
388 wxString
wxComboBox::GetValue() const
390 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
391 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
395 void wxComboBox::SetValue( const wxString
& value
)
397 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
399 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
401 if (!value
.IsNull()) tmp
= value
;
402 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
405 void wxComboBox::Copy()
407 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
409 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
410 #if (GTK_MINOR_VERSION == 1)
411 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
413 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
417 void wxComboBox::Cut()
419 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
421 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
422 #if (GTK_MINOR_VERSION == 1)
423 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
425 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
429 void wxComboBox::Paste()
431 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
433 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
434 #if (GTK_MINOR_VERSION == 1)
435 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
437 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
441 void wxComboBox::SetInsertionPoint( long pos
)
443 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
445 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
447 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
450 void wxComboBox::SetInsertionPointEnd()
452 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
454 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
455 int pos
= GTK_ENTRY(entry
)->text_length
;
456 SetInsertionPoint( pos
-1 );
459 long wxComboBox::GetInsertionPoint() const
461 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
462 return (long) GTK_EDITABLE(entry
)->current_pos
;
465 long wxComboBox::GetLastPosition() const
467 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
468 int pos
= GTK_ENTRY(entry
)->text_length
;
472 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
474 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
476 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
477 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
478 if (value
.IsNull()) return;
480 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
483 void wxComboBox::Remove(long from
, long to
)
485 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
487 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
488 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
491 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
493 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
496 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
498 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
501 void wxComboBox::OnSize( wxSizeEvent
&event
)
503 wxControl::OnSize( event
);
506 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
508 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
509 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
512 void wxComboBox::ApplyWidgetStyle()
516 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
517 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
518 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
520 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
521 GList
*child
= list
->children
;
524 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
526 GtkBin
*bin
= GTK_BIN(child
->data
);
527 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
533 GtkWidget
* wxComboBox::GetConnectWidget()
535 return GTK_COMBO(m_widget
)->entry
;
538 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
540 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
541 (window
== GTK_COMBO(m_widget
)->button
->window
) );