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 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
30 if (!combo
->HasVMT()) return;
31 if (g_blockEventsOnDrag
) return;
33 if (combo
->m_alreadySent
)
35 combo
->m_alreadySent
= FALSE
;
39 combo
->m_alreadySent
= TRUE
;
41 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
42 event
.SetInt( combo
->GetSelection() );
43 wxString
tmp( combo
->GetStringSelection() );
44 event
.SetString( WXSTRINGCAST(tmp
) );
45 event
.SetEventObject(combo
);
46 combo
->GetEventHandler()->ProcessEvent(event
);
49 //-----------------------------------------------------------------------------
53 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
55 if (!win->HasVMT()) return FALSE;
56 if (g_blockEventsOnDrag) return FALSE;
57 if (!widget->button) return FALSE;
59 widget->button->allocation.x =
60 alloc->width - widget->button->allocation.width;
66 //-----------------------------------------------------------------------------
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 wxValidator
& validator
, const wxString
& name
)
77 m_alreadySent
= FALSE
;
80 PreCreation( parent
, id
, pos
, size
, style
, name
);
82 SetValidator( validator
);
84 m_widget
= gtk_combo_new();
86 wxSize newSize
= size
;
87 if (newSize
.x
== -1) newSize
.x
= 100;
88 if (newSize
.y
== -1) newSize
.y
= 26;
89 SetSize( newSize
.x
, newSize
.y
);
91 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
93 for (int i
= 0; i
< n
; i
++)
96 list_item
= gtk_list_item_new_with_label( choices
[i
] );
98 gtk_container_add( GTK_CONTAINER(list
), list_item
);
100 m_clientData
.Append( (wxObject
*)NULL
);
102 gtk_widget_show( list_item
);
104 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
105 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
111 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
112 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
115 if (!value
.IsNull()) SetValue( value
);
122 void wxComboBox::Clear(void)
124 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
125 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
127 m_clientData
.Clear();
130 void wxComboBox::Append( const wxString
&item
)
132 Append( item
, (char*)NULL
);
135 void wxComboBox::Append( const wxString
&item
, char *clientData
)
137 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
139 GtkWidget
*list_item
;
140 list_item
= gtk_list_item_new_with_label( item
);
142 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
143 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
145 m_clientData
.Append( (wxObject
*)clientData
);
147 gtk_container_add( GTK_CONTAINER(list
), list_item
);
149 gtk_widget_show( list_item
);
152 void wxComboBox::Delete( int n
)
154 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
156 GList
*child
= g_list_nth( listbox
->children
, n
);
160 wxFAIL_MSG("wrong index");
164 GList
*list
= g_list_append( NULL
, child
->data
);
165 gtk_list_remove_items( listbox
, list
);
168 wxNode
*node
= m_clientData
.Nth( n
);
171 wxFAIL_MSG( "wrong index" );
174 m_clientData
.DeleteNode( node
);
177 int wxComboBox::FindString( const wxString
&item
)
179 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
181 GList
*child
= GTK_LIST(list
)->children
;
185 GtkBin
*bin
= GTK_BIN( child
->data
);
186 GtkLabel
*label
= GTK_LABEL( bin
->child
);
187 if (item
== label
->label
) return count
;
192 wxFAIL_MSG( "wxComboBox: string not found" );
197 char* wxComboBox::GetClientData( int n
)
199 wxNode
*node
= m_clientData
.Nth( n
);
200 if (node
) return (char*)node
->Data();
202 wxFAIL_MSG( "wxComboBox: wrong index" );
204 return (char *) NULL
;
207 void wxComboBox::SetClientData( int n
, char * clientData
)
209 wxNode
*node
= m_clientData
.Nth( n
);
210 if (node
) node
->SetData( (wxObject
*) clientData
);
212 wxFAIL_MSG( "wxComboBox: wrong index" );
215 int wxComboBox::GetSelection(void) const
217 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
219 GList
*selection
= GTK_LIST(list
)->selection
;
222 GList
*child
= GTK_LIST(list
)->children
;
226 if (child
->data
== selection
->data
) return count
;
232 wxFAIL_MSG( "wxComboBox: no selection" );
237 wxString
wxComboBox::GetString( int n
) const
239 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
241 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
244 GtkBin
*bin
= GTK_BIN( child
->data
);
245 GtkLabel
*label
= GTK_LABEL( bin
->child
);
249 wxFAIL_MSG( "wxComboBox: wrong index" );
254 wxString
wxComboBox::GetStringSelection(void) const
256 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
258 GList
*selection
= GTK_LIST(list
)->selection
;
261 GtkBin
*bin
= GTK_BIN( selection
->data
);
262 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
266 wxFAIL_MSG( "wxComboBox: no selection" );
271 int wxComboBox::Number(void) const
273 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
275 GList
*child
= GTK_LIST(list
)->children
;
277 while (child
) { count
++; child
= child
->next
; }
281 void wxComboBox::SetSelection( int n
)
283 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
284 gtk_list_select_item( GTK_LIST(list
), n
);
287 void wxComboBox::SetStringSelection( const wxString
&string
)
289 int res
= FindString( string
);
290 if (res
== -1) return;
294 wxString
wxComboBox::GetValue(void) const
296 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
297 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
301 void wxComboBox::SetValue( const wxString
& value
)
303 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
305 if (!value
.IsNull()) tmp
= value
;
306 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
309 void wxComboBox::Copy(void)
311 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
312 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
315 void wxComboBox::Cut(void)
317 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
318 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
321 void wxComboBox::Paste(void)
323 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
324 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
327 void wxComboBox::SetInsertionPoint( long pos
)
329 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
331 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
334 void wxComboBox::SetInsertionPointEnd(void)
336 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
337 int pos
= GTK_ENTRY(entry
)->text_length
;
338 SetInsertionPoint( pos
-1 );
341 long wxComboBox::GetInsertionPoint(void) const
343 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
344 return (long) GTK_EDITABLE(entry
)->current_pos
;
347 long wxComboBox::GetLastPosition(void) const
349 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
350 int pos
= GTK_ENTRY(entry
)->text_length
;
354 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
356 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
357 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
358 if (value
.IsNull()) return;
360 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
363 void wxComboBox::Remove(long from
, long to
)
365 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
366 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
369 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
373 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )