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 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
55 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
57 if (!win->HasVMT()) return FALSE;
58 if (g_blockEventsOnDrag) return FALSE;
59 if (!widget->button) return FALSE;
61 widget->button->allocation.x =
62 alloc->width - widget->button->allocation.width;
68 //-----------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
72 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
73 const wxPoint
& pos
, const wxSize
& size
,
74 int n
, const wxString choices
[],
75 long style
, const wxString
& name
)
77 m_alreadySent
= FALSE
;
80 PreCreation( parent
, id
, pos
, size
, style
, name
);
82 m_widget
= gtk_combo_new();
84 wxSize newSize
= size
;
85 if (newSize
.x
== -1) newSize
.x
= 100;
86 if (newSize
.y
== -1) newSize
.y
= 26;
87 SetSize( newSize
.x
, newSize
.y
);
89 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
91 for (int i
= 0; i
< n
; i
++)
94 list_item
= gtk_list_item_new_with_label( choices
[i
] );
96 gtk_container_add( GTK_CONTAINER(list
), list_item
);
98 m_clientData
.Append( (wxObject
*)NULL
);
100 gtk_widget_show( list_item
);
102 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
103 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
109 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
110 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
113 if (!value
.IsNull()) SetValue( value
);
120 void wxComboBox::Clear(void)
122 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
123 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
125 m_clientData
.Clear();
128 void wxComboBox::Append( const wxString
&item
)
130 Append( item
, (char*)NULL
);
133 void wxComboBox::Append( const wxString
&item
, char *clientData
)
135 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
137 GtkWidget
*list_item
;
138 list_item
= gtk_list_item_new_with_label( item
);
140 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
141 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
143 gtk_container_add( GTK_CONTAINER(list
), list_item
);
145 gtk_widget_show( list_item
);
147 m_clientData
.Append( (wxObject
*)clientData
);
150 void wxComboBox::Delete( int n
)
152 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
153 gtk_list_clear_items( GTK_LIST(list
), n
, n
);
155 wxNode
*node
= m_clientData
.Nth( n
);
158 wxFAIL_MSG("wxComboBox::Delete wrong index");
161 m_clientData
.DeleteNode( node
);
164 int wxComboBox::FindString( const wxString
&item
)
166 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
168 GList
*child
= GTK_LIST(list
)->children
;
172 GtkBin
*bin
= GTK_BIN( child
->data
);
173 GtkLabel
*label
= GTK_LABEL( bin
->child
);
174 if (item
== label
->label
) return count
;
181 char* wxComboBox::GetClientData( int n
)
183 wxNode
*node
= m_clientData
.Nth( n
);
184 if (node
) return (char*)node
->Data();
188 void wxComboBox::SetClientData( int n
, char * clientData
)
190 wxNode
*node
= m_clientData
.Nth( n
);
191 if (node
) node
->SetData( (wxObject
*) clientData
);
194 int wxComboBox::GetSelection(void) const
196 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
198 GList
*selection
= GTK_LIST(list
)->selection
;
201 GList
*child
= GTK_LIST(list
)->children
;
205 if (child
->data
== selection
->data
) return count
;
213 wxString
wxComboBox::GetString( int n
) const
215 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
217 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
220 GtkBin
*bin
= GTK_BIN( child
->data
);
221 GtkLabel
*label
= GTK_LABEL( bin
->child
);
227 wxString
wxComboBox::GetStringSelection(void) const
229 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
231 GList
*selection
= GTK_LIST(list
)->selection
;
234 GtkBin
*bin
= GTK_BIN( selection
->data
);
235 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
241 int wxComboBox::Number(void) const
243 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
245 GList
*child
= GTK_LIST(list
)->children
;
247 while (child
) { count
++; child
= child
->next
; };
251 void wxComboBox::SetSelection( int n
)
253 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
254 gtk_list_select_item( GTK_LIST(list
), n
);
257 void wxComboBox::SetStringSelection( const wxString
&string
)
259 int res
= FindString( string
);
260 if (res
== -1) return;
264 wxString
wxComboBox::GetValue(void) const
266 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
267 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
271 void wxComboBox::SetValue( const wxString
& value
)
273 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
275 if (!value
.IsNull()) tmp
= value
;
276 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
279 void wxComboBox::Copy(void)
281 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
282 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
285 void wxComboBox::Cut(void)
287 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
288 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
291 void wxComboBox::Paste(void)
293 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
294 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
297 void wxComboBox::SetInsertionPoint( long pos
)
299 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
301 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
304 void wxComboBox::SetInsertionPointEnd(void)
306 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
307 int pos
= GTK_ENTRY(entry
)->text_length
;
308 SetInsertionPoint( pos
-1 );
311 long wxComboBox::GetInsertionPoint(void) const
313 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
314 return (long) GTK_EDITABLE(entry
)->current_pos
;
317 long wxComboBox::GetLastPosition(void) const
319 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
320 int pos
= GTK_ENTRY(entry
)->text_length
;
324 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
326 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
327 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
328 if (value
.IsNull()) return;
330 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
333 void wxComboBox::Remove(long from
, long to
)
335 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
336 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
339 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
343 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )