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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
32 if (!combo
->HasVMT()) return;
33 if (g_blockEventsOnDrag
) return;
35 if (combo
->m_alreadySent
)
37 combo
->m_alreadySent
= FALSE
;
41 combo
->m_alreadySent
= TRUE
;
43 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
44 event
.SetInt( combo
->GetSelection() );
45 wxString
tmp( combo
->GetStringSelection() );
46 event
.SetString( WXSTRINGCAST(tmp
) );
47 event
.SetEventObject(combo
);
48 combo
->GetEventHandler()->ProcessEvent(event
);
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
57 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
58 EVT_SIZE(wxComboBox::OnSize
)
61 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
62 const wxPoint
& pos
, const wxSize
& size
,
63 int n
, const wxString choices
[],
64 long style
, const wxValidator
& validator
,
65 const wxString
& name
)
67 m_alreadySent
= FALSE
;
69 m_acceptsFocus
= TRUE
;
71 PreCreation( parent
, id
, pos
, size
, style
, name
);
73 SetValidator( validator
);
75 m_widget
= gtk_combo_new();
77 wxSize newSize
= size
;
78 if (newSize
.x
== -1) newSize
.x
= 100;
79 if (newSize
.y
== -1) newSize
.y
= 26;
80 SetSize( newSize
.x
, newSize
.y
);
82 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
84 for (int i
= 0; i
< n
; i
++)
86 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
88 m_clientDataList
.Append( (wxObject
*)NULL
);
89 m_clientObjectList
.Append( (wxObject
*)NULL
);
91 gtk_container_add( GTK_CONTAINER(list
), list_item
);
93 gtk_widget_realize( list_item
);
94 gtk_widget_realize( GTK_BIN(list_item
)->child
);
96 gtk_widget_show( list_item
);
98 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
99 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
102 m_parent
->AddChild( this );
104 (m_parent
->m_insertCallback
)( m_parent
, this );
108 ConnectWidget( GTK_COMBO(m_widget
)->button
);
110 if (!value
.IsNull()) SetValue( value
);
112 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
113 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
114 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
116 SetBackgroundColour( parent
->GetBackgroundColour() );
117 SetForegroundColour( parent
->GetForegroundColour() );
124 wxComboBox::~wxComboBox()
126 wxNode
*node
= m_clientDataList
.First();
129 wxClientData
*cd
= (wxClientData
*)node
->Data();
133 m_clientDataList
.Clear();
136 void wxComboBox::AppendCommon( const wxString
&item
)
138 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
140 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
142 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
144 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
145 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
147 gtk_container_add( GTK_CONTAINER(list
), list_item
);
149 if (m_widgetStyle
) ApplyWidgetStyle();
151 gtk_widget_show( list_item
);
154 void wxComboBox::Append( const wxString
&item
)
156 m_clientDataList
.Append( (wxObject
*) NULL
);
157 m_clientObjectList
.Append( (wxObject
*) NULL
);
159 AppendCommon( item
);
162 void wxComboBox::Append( const wxString
&item
, void *clientData
)
164 m_clientDataList
.Append( (wxObject
*) clientData
);
165 m_clientObjectList
.Append( (wxObject
*)NULL
);
167 AppendCommon( item
);
170 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
172 m_clientDataList
.Append( (wxObject
*) NULL
);
173 m_clientObjectList
.Append( (wxObject
*) clientData
);
175 AppendCommon( item
);
178 void wxComboBox::SetClientData( int n
, void* clientData
)
180 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
182 wxNode
*node
= m_clientDataList
.Nth( n
);
185 node
->SetData( (wxObject
*) clientData
);
188 void* wxComboBox::GetClientData( int n
)
190 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
192 wxNode
*node
= m_clientDataList
.Nth( n
);
193 if (!node
) return NULL
;
198 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
200 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
202 wxNode
*node
= m_clientObjectList
.Nth( n
);
205 wxClientData
*cd
= (wxClientData
*) node
->Data();
208 node
->SetData( (wxObject
*) clientData
);
211 wxClientData
* wxComboBox::GetClientObject( int n
)
213 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
215 wxNode
*node
= m_clientDataList
.Nth( n
);
216 if (!node
) return (wxClientData
*) NULL
;
218 return (wxClientData
*) node
->Data();
221 void wxComboBox::Clear()
223 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
225 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
226 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
228 wxNode
*node
= m_clientObjectList
.First();
231 wxClientData
*cd
= (wxClientData
*)node
->Data();
235 m_clientObjectList
.Clear();
237 m_clientDataList
.Clear();
240 void wxComboBox::Delete( int n
)
242 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
244 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
246 GList
*child
= g_list_nth( listbox
->children
, n
);
250 wxFAIL_MSG("wrong index");
254 GList
*list
= g_list_append( NULL
, child
->data
);
255 gtk_list_remove_items( listbox
, list
);
258 wxNode
*node
= m_clientObjectList
.Nth( n
);
261 wxClientData
*cd
= (wxClientData
*)node
->Data();
263 m_clientObjectList
.DeleteNode( node
);
266 node
= m_clientDataList
.Nth( n
);
269 m_clientDataList
.DeleteNode( node
);
273 int wxComboBox::FindString( const wxString
&item
)
275 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
277 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
279 GList
*child
= GTK_LIST(list
)->children
;
283 GtkBin
*bin
= GTK_BIN( child
->data
);
284 GtkLabel
*label
= GTK_LABEL( bin
->child
);
285 if (item
== label
->label
) return count
;
290 wxFAIL_MSG( "wxComboBox: string not found" );
295 int wxComboBox::GetSelection() const
297 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
299 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
301 GList
*selection
= GTK_LIST(list
)->selection
;
304 GList
*child
= GTK_LIST(list
)->children
;
308 if (child
->data
== selection
->data
) return count
;
314 wxFAIL_MSG( "wxComboBox: no selection" );
319 wxString
wxComboBox::GetString( int n
) const
321 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
323 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
325 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
328 GtkBin
*bin
= GTK_BIN( child
->data
);
329 GtkLabel
*label
= GTK_LABEL( bin
->child
);
333 wxFAIL_MSG( "wxComboBox: wrong index" );
338 wxString
wxComboBox::GetStringSelection() const
340 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
342 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
344 GList
*selection
= GTK_LIST(list
)->selection
;
347 GtkBin
*bin
= GTK_BIN( selection
->data
);
348 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
352 wxFAIL_MSG( "wxComboBox: no selection" );
357 int wxComboBox::Number() const
359 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
361 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
363 GList
*child
= GTK_LIST(list
)->children
;
365 while (child
) { count
++; child
= child
->next
; }
369 void wxComboBox::SetSelection( int n
)
371 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
373 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
374 gtk_list_select_item( GTK_LIST(list
), n
);
377 void wxComboBox::SetStringSelection( const wxString
&string
)
379 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
381 int res
= FindString( string
);
382 if (res
== -1) return;
386 wxString
wxComboBox::GetValue() const
388 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
389 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
393 void wxComboBox::SetValue( const wxString
& value
)
395 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
397 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
399 if (!value
.IsNull()) tmp
= value
;
400 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
403 void wxComboBox::Copy()
405 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
407 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
408 #if (GTK_MINOR_VERSION == 1)
409 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
411 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
415 void wxComboBox::Cut()
417 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
419 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
420 #if (GTK_MINOR_VERSION == 1)
421 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
423 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
427 void wxComboBox::Paste()
429 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
431 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
432 #if (GTK_MINOR_VERSION == 1)
433 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
435 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
439 void wxComboBox::SetInsertionPoint( long pos
)
441 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
443 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
445 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
448 void wxComboBox::SetInsertionPointEnd()
450 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
452 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
453 int pos
= GTK_ENTRY(entry
)->text_length
;
454 SetInsertionPoint( pos
-1 );
457 long wxComboBox::GetInsertionPoint() const
459 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
460 return (long) GTK_EDITABLE(entry
)->current_pos
;
463 long wxComboBox::GetLastPosition() const
465 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
466 int pos
= GTK_ENTRY(entry
)->text_length
;
470 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
472 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
474 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
475 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
476 if (value
.IsNull()) return;
478 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
481 void wxComboBox::Remove(long from
, long to
)
483 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
485 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
486 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
489 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
491 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
494 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
496 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
499 void wxComboBox::OnSize( wxSizeEvent
&event
)
501 wxControl::OnSize( event
);
504 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
506 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
507 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
510 void wxComboBox::ApplyWidgetStyle()
514 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
515 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
516 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
518 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
519 GList
*child
= list
->children
;
522 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
524 GtkBin
*bin
= GTK_BIN(child
->data
);
525 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
531 GtkWidget
* wxComboBox::GetConnectWidget()
533 return GTK_COMBO(m_widget
)->entry
;
536 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
538 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
539 (window
== GTK_COMBO(m_widget
)->button
->window
) );