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"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
33 if (!combo
->HasVMT()) return;
34 if (g_blockEventsOnDrag
) return;
36 if (combo
->m_alreadySent
)
38 combo
->m_alreadySent
= FALSE
;
42 combo
->m_alreadySent
= TRUE
;
44 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
45 event
.SetInt( combo
->GetSelection() );
46 wxString
tmp( combo
->GetStringSelection() );
47 event
.SetString( WXSTRINGCAST(tmp
) );
48 event
.SetEventObject(combo
);
49 combo
->GetEventHandler()->ProcessEvent(event
);
52 //-----------------------------------------------------------------------------
56 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
58 if (!win->HasVMT()) return FALSE;
59 if (g_blockEventsOnDrag) return FALSE;
60 if (!widget->button) return FALSE;
62 widget->button->allocation.x =
63 alloc->width - widget->button->allocation.width;
69 //-----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
73 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
74 const wxPoint
& pos
, const wxSize
& size
,
75 int n
, const wxString choices
[],
76 long style
, const wxValidator
& validator
, const wxString
& name
)
78 m_alreadySent
= FALSE
;
81 PreCreation( parent
, id
, pos
, size
, style
, name
);
83 SetValidator( validator
);
85 m_widget
= gtk_combo_new();
87 wxSize newSize
= size
;
88 if (newSize
.x
== -1) newSize
.x
= 100;
89 if (newSize
.y
== -1) newSize
.y
= 26;
90 SetSize( newSize
.x
, newSize
.y
);
92 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
94 for (int i
= 0; i
< n
; i
++)
97 list_item
= gtk_list_item_new_with_label( choices
[i
] );
99 gtk_container_add( GTK_CONTAINER(list
), list_item
);
101 m_clientData
.Append( (wxObject
*)NULL
);
103 gtk_widget_show( list_item
);
105 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
106 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
112 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
113 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
116 if (!value
.IsNull()) SetValue( value
);
123 void wxComboBox::Clear(void)
125 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
126 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
128 m_clientData
.Clear();
131 void wxComboBox::Append( const wxString
&item
)
133 Append( item
, (char*)NULL
);
136 void wxComboBox::Append( const wxString
&item
, char *clientData
)
138 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
140 GtkWidget
*list_item
;
141 list_item
= gtk_list_item_new_with_label( item
);
143 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
144 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
146 gtk_container_add( GTK_CONTAINER(list
), list_item
);
148 gtk_widget_show( list_item
);
150 m_clientData
.Append( (wxObject
*)clientData
);
153 void wxComboBox::Delete( int n
)
155 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
156 gtk_list_clear_items( GTK_LIST(list
), n
, n
);
158 wxNode
*node
= m_clientData
.Nth( n
);
161 wxFAIL_MSG( "wxComboBox: wrong index" );
164 m_clientData
.DeleteNode( node
);
167 int wxComboBox::FindString( const wxString
&item
)
169 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
171 GList
*child
= GTK_LIST(list
)->children
;
175 GtkBin
*bin
= GTK_BIN( child
->data
);
176 GtkLabel
*label
= GTK_LABEL( bin
->child
);
177 if (item
== label
->label
) return count
;
182 wxFAIL_MSG( "wxComboBox: string not found" );
187 char* wxComboBox::GetClientData( int n
)
189 wxNode
*node
= m_clientData
.Nth( n
);
190 if (node
) return (char*)node
->Data();
192 wxFAIL_MSG( "wxComboBox: wrong index" );
197 void wxComboBox::SetClientData( int n
, char * clientData
)
199 wxNode
*node
= m_clientData
.Nth( n
);
200 if (node
) node
->SetData( (wxObject
*) clientData
);
202 wxFAIL_MSG( "wxComboBox: wrong index" );
205 int wxComboBox::GetSelection(void) const
207 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
209 GList
*selection
= GTK_LIST(list
)->selection
;
212 GList
*child
= GTK_LIST(list
)->children
;
216 if (child
->data
== selection
->data
) return count
;
222 wxFAIL_MSG( "wxComboBox: no selection" );
227 wxString
wxComboBox::GetString( int n
) const
229 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
231 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
234 GtkBin
*bin
= GTK_BIN( child
->data
);
235 GtkLabel
*label
= GTK_LABEL( bin
->child
);
239 wxFAIL_MSG( "wxComboBox: wrong index" );
244 wxString
wxComboBox::GetStringSelection(void) const
246 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
248 GList
*selection
= GTK_LIST(list
)->selection
;
251 GtkBin
*bin
= GTK_BIN( selection
->data
);
252 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
256 wxFAIL_MSG( "wxComboBox: no selection" );
261 int wxComboBox::Number(void) const
263 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
265 GList
*child
= GTK_LIST(list
)->children
;
267 while (child
) { count
++; child
= child
->next
; }
271 void wxComboBox::SetSelection( int n
)
273 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
274 gtk_list_select_item( GTK_LIST(list
), n
);
277 void wxComboBox::SetStringSelection( const wxString
&string
)
279 int res
= FindString( string
);
280 if (res
== -1) return;
284 wxString
wxComboBox::GetValue(void) const
286 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
287 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
291 void wxComboBox::SetValue( const wxString
& value
)
293 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
295 if (!value
.IsNull()) tmp
= value
;
296 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
299 void wxComboBox::Copy(void)
301 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
302 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
305 void wxComboBox::Cut(void)
307 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
308 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
311 void wxComboBox::Paste(void)
313 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
314 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
317 void wxComboBox::SetInsertionPoint( long pos
)
319 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
321 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
324 void wxComboBox::SetInsertionPointEnd(void)
326 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
327 int pos
= GTK_ENTRY(entry
)->text_length
;
328 SetInsertionPoint( pos
-1 );
331 long wxComboBox::GetInsertionPoint(void) const
333 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
334 return (long) GTK_EDITABLE(entry
)->current_pos
;
337 long wxComboBox::GetLastPosition(void) const
339 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
340 int pos
= GTK_ENTRY(entry
)->text_length
;
344 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
346 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
347 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
348 if (value
.IsNull()) return;
350 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
353 void wxComboBox::Remove(long from
, long to
)
355 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
356 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
359 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
363 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )