1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "combobox.h"
15 #include "wx/combobox.h"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
23 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
24 event
.SetInt( combo
->GetSelection() );
25 wxString
tmp( combo
->GetStringSelection() );
26 event
.SetString( WXSTRINGCAST(tmp
) );
27 event
.SetEventObject(combo
);
28 combo
->ProcessEvent(event
);
31 //-----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
35 bool wxComboBox::Create(wxWindow
*parent
, const wxWindowID id
, const wxString
& value
,
36 const wxPoint
& pos
, const wxSize
& size
,
37 const int n
, const wxString choices
[],
38 const long style
, const wxString
& name
)
42 PreCreation( parent
, id
, pos
, size
, style
, name
);
44 m_widget
= gtk_combo_new();
46 wxSize newSize
= size
;
47 if (newSize
.x
== -1) newSize
.x
= 100;
48 if (newSize
.y
== -1) newSize
.y
= 26;
49 SetSize( newSize
.x
, newSize
.y
);
51 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
53 for (int i
= 0; i
< n
; i
++)
56 list_item
= gtk_list_item_new_with_label( choices
[i
] );
58 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
59 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
61 gtk_container_add( GTK_CONTAINER(list
), list_item
);
63 gtk_widget_show( list_item
);
68 if (!value
.IsNull()) SetValue( value
);
75 void wxComboBox::Clear(void)
77 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
78 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
81 void wxComboBox::Append( const wxString
&item
)
83 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
86 list_item
= gtk_list_item_new_with_label( item
);
88 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
89 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
91 gtk_container_add( GTK_CONTAINER(list
), list_item
);
93 gtk_widget_show( list_item
);
96 void wxComboBox::Append( const wxString
&WXUNUSED(item
), char* WXUNUSED(clientData
) )
100 void wxComboBox::Delete( const int n
)
102 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
103 gtk_list_clear_items( GTK_LIST(list
), n
, n
);
106 int wxComboBox::FindString( const wxString
&item
)
108 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
110 GList
*child
= GTK_LIST(list
)->children
;
114 GtkBin
*bin
= GTK_BIN( child
->data
);
115 GtkLabel
*label
= GTK_LABEL( bin
->child
);
116 if (item
== label
->label
) return count
;
123 char* wxComboBox::GetClientData( const int n
)
125 wxNode
*node
= m_clientData
.Nth( n
);
126 if (node
) return (char*)node
->Data();
130 void wxComboBox::SetClientData( const int n
, char * clientData
)
132 wxNode
*node
= m_clientData
.Nth( n
);
133 if (node
) node
->SetData( (wxObject
*) clientData
);
136 int wxComboBox::GetSelection(void) const
138 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
140 GList
*selection
= GTK_LIST(list
)->selection
;
143 GList
*child
= GTK_LIST(list
)->children
;
147 if (child
->data
== selection
->data
) return count
;
155 wxString
wxComboBox::GetString( const int n
) const
157 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
159 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
162 GtkBin
*bin
= GTK_BIN( child
->data
);
163 GtkLabel
*label
= GTK_LABEL( bin
->child
);
169 wxString
wxComboBox::GetStringSelection(void) const
171 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
173 GList
*selection
= GTK_LIST(list
)->selection
;
176 GtkBin
*bin
= GTK_BIN( selection
->data
);
177 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
183 int wxComboBox::Number(void) const
185 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
187 GList
*child
= GTK_LIST(list
)->children
;
189 while (child
) { count
++; child
= child
->next
; };
193 void wxComboBox::SetSelection( const int n
)
195 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
196 gtk_list_select_item( GTK_LIST(list
), n
);
199 wxString
wxComboBox::GetValue(void) const
201 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
202 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
206 void wxComboBox::SetValue( const wxString
& value
)
208 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
210 if (!value
.IsNull()) tmp
= value
;
211 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
214 void wxComboBox::Copy(void)
216 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
217 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
220 void wxComboBox::Cut(void)
222 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
223 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
226 void wxComboBox::Paste(void)
228 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
229 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
232 void wxComboBox::SetInsertionPoint( const long pos
)
234 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
236 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
239 void wxComboBox::SetInsertionPointEnd(void)
241 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
242 int pos
= GTK_ENTRY(entry
)->text_length
;
243 SetInsertionPoint( pos
-1 );
246 long wxComboBox::GetInsertionPoint(void) const
248 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
249 return (long) GTK_EDITABLE(entry
)->current_pos
;
252 long wxComboBox::GetLastPosition(void) const
254 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
255 int pos
= GTK_ENTRY(entry
)->text_length
;
259 void wxComboBox::Replace( const long from
, const long to
, const wxString
& value
)
261 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
262 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
263 if (value
.IsNull()) return;
265 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
268 void wxComboBox::Remove(const long from
, const long to
)
270 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
271 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
274 void wxComboBox::SetSelection( const long WXUNUSED(from
), const long WXUNUSED(to
) )
278 void wxComboBox::SetEditable( const bool WXUNUSED(editable
) )