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 wxString
& name
)
78 m_alreadySent
= FALSE
;
81 PreCreation( parent
, id
, pos
, size
, style
, name
);
83 m_widget
= gtk_combo_new();
85 wxSize newSize
= size
;
86 if (newSize
.x
== -1) newSize
.x
= 100;
87 if (newSize
.y
== -1) newSize
.y
= 26;
88 SetSize( newSize
.x
, newSize
.y
);
90 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
92 for (int i
= 0; i
< n
; i
++)
95 list_item
= gtk_list_item_new_with_label( choices
[i
] );
97 gtk_container_add( GTK_CONTAINER(list
), list_item
);
99 m_clientData
.Append( (wxObject
*)NULL
);
101 gtk_widget_show( list_item
);
103 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
104 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
110 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
111 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
114 if (!value
.IsNull()) SetValue( value
);
121 void wxComboBox::Clear(void)
123 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
124 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
126 m_clientData
.Clear();
129 void wxComboBox::Append( const wxString
&item
)
131 Append( item
, (char*)NULL
);
134 void wxComboBox::Append( const wxString
&item
, char *clientData
)
136 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
138 GtkWidget
*list_item
;
139 list_item
= gtk_list_item_new_with_label( item
);
141 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
142 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
144 gtk_container_add( GTK_CONTAINER(list
), list_item
);
146 gtk_widget_show( list_item
);
148 m_clientData
.Append( (wxObject
*)clientData
);
151 void wxComboBox::Delete( int n
)
153 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
154 gtk_list_clear_items( GTK_LIST(list
), n
, n
);
156 wxNode
*node
= m_clientData
.Nth( n
);
159 wxFAIL_MSG( "wxComboBox: wrong index" );
162 m_clientData
.DeleteNode( node
);
165 int wxComboBox::FindString( const wxString
&item
)
167 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
169 GList
*child
= GTK_LIST(list
)->children
;
173 GtkBin
*bin
= GTK_BIN( child
->data
);
174 GtkLabel
*label
= GTK_LABEL( bin
->child
);
175 if (item
== label
->label
) return count
;
180 wxFAIL_MSG( "wxComboBox: string not found" );
185 char* wxComboBox::GetClientData( int n
)
187 wxNode
*node
= m_clientData
.Nth( n
);
188 if (node
) return (char*)node
->Data();
190 wxFAIL_MSG( "wxComboBox: wrong index" );
195 void wxComboBox::SetClientData( int n
, char * clientData
)
197 wxNode
*node
= m_clientData
.Nth( n
);
198 if (node
) node
->SetData( (wxObject
*) clientData
);
200 wxFAIL_MSG( "wxComboBox: wrong index" );
203 int wxComboBox::GetSelection(void) const
205 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
207 GList
*selection
= GTK_LIST(list
)->selection
;
210 GList
*child
= GTK_LIST(list
)->children
;
214 if (child
->data
== selection
->data
) return count
;
220 wxFAIL_MSG( "wxComboBox: no selection" );
225 wxString
wxComboBox::GetString( int n
) const
227 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
229 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
232 GtkBin
*bin
= GTK_BIN( child
->data
);
233 GtkLabel
*label
= GTK_LABEL( bin
->child
);
237 wxFAIL_MSG( "wxComboBox: wrong index" );
242 wxString
wxComboBox::GetStringSelection(void) const
244 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
246 GList
*selection
= GTK_LIST(list
)->selection
;
249 GtkBin
*bin
= GTK_BIN( selection
->data
);
250 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
254 wxFAIL_MSG( "wxComboBox: no selection" );
259 int wxComboBox::Number(void) const
261 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
263 GList
*child
= GTK_LIST(list
)->children
;
265 while (child
) { count
++; child
= child
->next
; };
269 void wxComboBox::SetSelection( int n
)
271 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
272 gtk_list_select_item( GTK_LIST(list
), n
);
275 void wxComboBox::SetStringSelection( const wxString
&string
)
277 int res
= FindString( string
);
278 if (res
== -1) return;
282 wxString
wxComboBox::GetValue(void) const
284 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
285 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
289 void wxComboBox::SetValue( const wxString
& value
)
291 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
293 if (!value
.IsNull()) tmp
= value
;
294 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
297 void wxComboBox::Copy(void)
299 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
300 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
303 void wxComboBox::Cut(void)
305 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
306 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
309 void wxComboBox::Paste(void)
311 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
312 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
315 void wxComboBox::SetInsertionPoint( long pos
)
317 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
319 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
322 void wxComboBox::SetInsertionPointEnd(void)
324 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
325 int pos
= GTK_ENTRY(entry
)->text_length
;
326 SetInsertionPoint( pos
-1 );
329 long wxComboBox::GetInsertionPoint(void) const
331 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
332 return (long) GTK_EDITABLE(entry
)->current_pos
;
335 long wxComboBox::GetLastPosition(void) const
337 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
338 int pos
= GTK_ENTRY(entry
)->text_length
;
342 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
344 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
345 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
346 if (value
.IsNull()) return;
348 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
351 void wxComboBox::Remove(long from
, long to
)
353 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
354 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
357 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
361 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )