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
->m_alreadySent
)
34 combo
->m_alreadySent
= FALSE
;
38 combo
->m_alreadySent
= TRUE
;
40 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
41 event
.SetInt( combo
->GetSelection() );
42 wxString
tmp( combo
->GetStringSelection() );
43 event
.SetString( WXSTRINGCAST(tmp
) );
44 event
.SetEventObject(combo
);
45 combo
->GetEventHandler()->ProcessEvent(event
);
48 //-----------------------------------------------------------------------------
52 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
54 if (!win->HasVMT()) return FALSE;
55 if (g_blockEventsOnDrag) return FALSE;
56 if (!widget->button) return FALSE;
58 widget->button->allocation.x =
59 alloc->width - widget->button->allocation.width;
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
69 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
70 const wxPoint
& pos
, const wxSize
& size
,
71 int n
, const wxString choices
[],
72 long style
, const wxString
& name
)
74 m_alreadySent
= FALSE
;
77 PreCreation( parent
, id
, pos
, size
, style
, name
);
79 m_widget
= gtk_combo_new();
81 wxSize newSize
= size
;
82 if (newSize
.x
== -1) newSize
.x
= 100;
83 if (newSize
.y
== -1) newSize
.y
= 26;
84 SetSize( newSize
.x
, newSize
.y
);
86 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
88 for (int i
= 0; i
< n
; i
++)
91 list_item
= gtk_list_item_new_with_label( choices
[i
] );
93 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
94 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
96 gtk_container_add( GTK_CONTAINER(list
), list_item
);
98 m_clientData
.Append( (wxObject
*)NULL
);
100 gtk_widget_show( list_item
);
106 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
107 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
110 if (!value
.IsNull()) SetValue( value
);
117 void wxComboBox::Clear(void)
119 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
120 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
122 m_clientData
.Clear();
125 void wxComboBox::Append( const wxString
&item
)
127 Append( item
, (char*)NULL
);
130 void wxComboBox::Append( const wxString
&item
, char *clientData
)
132 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
134 GtkWidget
*list_item
;
135 list_item
= gtk_list_item_new_with_label( item
);
137 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
138 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
140 gtk_container_add( GTK_CONTAINER(list
), list_item
);
142 gtk_widget_show( list_item
);
144 m_clientData
.Append( (wxObject
*)clientData
);
147 void wxComboBox::Delete( int n
)
149 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
150 gtk_list_clear_items( GTK_LIST(list
), n
, n
);
152 wxNode
*node
= m_clientData
.Nth( n
);
155 wxFAIL_MSG("wxComboBox::Delete wrong index");
158 m_clientData
.DeleteNode( node
);
161 int wxComboBox::FindString( const wxString
&item
)
163 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
165 GList
*child
= GTK_LIST(list
)->children
;
169 GtkBin
*bin
= GTK_BIN( child
->data
);
170 GtkLabel
*label
= GTK_LABEL( bin
->child
);
171 if (item
== label
->label
) return count
;
178 char* wxComboBox::GetClientData( int n
)
180 wxNode
*node
= m_clientData
.Nth( n
);
181 if (node
) return (char*)node
->Data();
185 void wxComboBox::SetClientData( int n
, char * clientData
)
187 wxNode
*node
= m_clientData
.Nth( n
);
188 if (node
) node
->SetData( (wxObject
*) clientData
);
191 int wxComboBox::GetSelection(void) const
193 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
195 GList
*selection
= GTK_LIST(list
)->selection
;
198 GList
*child
= GTK_LIST(list
)->children
;
202 if (child
->data
== selection
->data
) return count
;
210 wxString
wxComboBox::GetString( int n
) const
212 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
214 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
217 GtkBin
*bin
= GTK_BIN( child
->data
);
218 GtkLabel
*label
= GTK_LABEL( bin
->child
);
224 wxString
wxComboBox::GetStringSelection(void) const
226 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
228 GList
*selection
= GTK_LIST(list
)->selection
;
231 GtkBin
*bin
= GTK_BIN( selection
->data
);
232 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
238 int wxComboBox::Number(void) const
240 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
242 GList
*child
= GTK_LIST(list
)->children
;
244 while (child
) { count
++; child
= child
->next
; };
248 void wxComboBox::SetSelection( int n
)
250 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
251 gtk_list_select_item( GTK_LIST(list
), n
);
254 void wxComboBox::SetStringSelection( const wxString
&string
)
256 int res
= FindString( string
);
257 if (res
== -1) return;
261 wxString
wxComboBox::GetValue(void) const
263 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
264 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
268 void wxComboBox::SetValue( const wxString
& value
)
270 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
272 if (!value
.IsNull()) tmp
= value
;
273 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
276 void wxComboBox::Copy(void)
278 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
279 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
282 void wxComboBox::Cut(void)
284 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
285 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
288 void wxComboBox::Paste(void)
290 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
291 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
294 void wxComboBox::SetInsertionPoint( long pos
)
296 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
298 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
301 void wxComboBox::SetInsertionPointEnd(void)
303 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
304 int pos
= GTK_ENTRY(entry
)->text_length
;
305 SetInsertionPoint( pos
-1 );
308 long wxComboBox::GetInsertionPoint(void) const
310 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
311 return (long) GTK_EDITABLE(entry
)->current_pos
;
314 long wxComboBox::GetLastPosition(void) const
316 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
317 int pos
= GTK_ENTRY(entry
)->text_length
;
321 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
323 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
324 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
325 if (value
.IsNull()) return;
327 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
330 void wxComboBox::Remove(long from
, long to
)
332 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
333 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
336 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
340 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )