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 m_clientData
.Append( (wxObject
*)clientData
);
148 gtk_container_add( GTK_CONTAINER(list
), list_item
);
150 gtk_widget_show( list_item
);
153 void wxComboBox::Delete( int n
)
155 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
157 GList
*child
= g_list_nth( listbox
->children
, n
);
161 wxFAIL_MSG("wrong index");
165 GList
*list
= g_list_append( NULL
, child
->data
);
166 gtk_list_remove_items( listbox
, list
);
169 wxNode
*node
= m_clientData
.Nth( n
);
172 wxFAIL_MSG( "wrong index" );
175 m_clientData
.DeleteNode( node
);
178 int wxComboBox::FindString( const wxString
&item
)
180 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
182 GList
*child
= GTK_LIST(list
)->children
;
186 GtkBin
*bin
= GTK_BIN( child
->data
);
187 GtkLabel
*label
= GTK_LABEL( bin
->child
);
188 if (item
== label
->label
) return count
;
193 wxFAIL_MSG( "wxComboBox: string not found" );
198 char* wxComboBox::GetClientData( int n
)
200 wxNode
*node
= m_clientData
.Nth( n
);
201 if (node
) return (char*)node
->Data();
203 wxFAIL_MSG( "wxComboBox: wrong index" );
205 return (char *) NULL
;
208 void wxComboBox::SetClientData( int n
, char * clientData
)
210 wxNode
*node
= m_clientData
.Nth( n
);
211 if (node
) node
->SetData( (wxObject
*) clientData
);
213 wxFAIL_MSG( "wxComboBox: wrong index" );
216 int wxComboBox::GetSelection(void) const
218 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
220 GList
*selection
= GTK_LIST(list
)->selection
;
223 GList
*child
= GTK_LIST(list
)->children
;
227 if (child
->data
== selection
->data
) return count
;
233 wxFAIL_MSG( "wxComboBox: no selection" );
238 wxString
wxComboBox::GetString( int n
) const
240 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
242 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
245 GtkBin
*bin
= GTK_BIN( child
->data
);
246 GtkLabel
*label
= GTK_LABEL( bin
->child
);
250 wxFAIL_MSG( "wxComboBox: wrong index" );
255 wxString
wxComboBox::GetStringSelection(void) const
257 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
259 GList
*selection
= GTK_LIST(list
)->selection
;
262 GtkBin
*bin
= GTK_BIN( selection
->data
);
263 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
267 wxFAIL_MSG( "wxComboBox: no selection" );
272 int wxComboBox::Number(void) const
274 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
276 GList
*child
= GTK_LIST(list
)->children
;
278 while (child
) { count
++; child
= child
->next
; }
282 void wxComboBox::SetSelection( int n
)
284 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
285 gtk_list_select_item( GTK_LIST(list
), n
);
288 void wxComboBox::SetStringSelection( const wxString
&string
)
290 int res
= FindString( string
);
291 if (res
== -1) return;
295 wxString
wxComboBox::GetValue(void) const
297 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
298 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
302 void wxComboBox::SetValue( const wxString
& value
)
304 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
306 if (!value
.IsNull()) tmp
= value
;
307 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
310 void wxComboBox::Copy(void)
312 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
313 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
316 void wxComboBox::Cut(void)
318 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
319 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
322 void wxComboBox::Paste(void)
324 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
325 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
328 void wxComboBox::SetInsertionPoint( long pos
)
330 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
332 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
335 void wxComboBox::SetInsertionPointEnd(void)
337 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
338 int pos
= GTK_ENTRY(entry
)->text_length
;
339 SetInsertionPoint( pos
-1 );
342 long wxComboBox::GetInsertionPoint(void) const
344 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
345 return (long) GTK_EDITABLE(entry
)->current_pos
;
348 long wxComboBox::GetLastPosition(void) const
350 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
351 int pos
= GTK_ENTRY(entry
)->text_length
;
355 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
357 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
358 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
359 if (value
.IsNull()) return;
361 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
364 void wxComboBox::Remove(long from
, long to
)
366 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
367 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
370 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
374 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )