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 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
55 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
56 EVT_SIZE(wxComboBox::OnSize
)
59 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
60 const wxPoint
& pos
, const wxSize
& size
,
61 int n
, const wxString choices
[],
62 long style
, const wxValidator
& validator
, const wxString
& name
)
64 m_alreadySent
= FALSE
;
67 PreCreation( parent
, id
, pos
, size
, style
, name
);
69 SetValidator( validator
);
71 m_widget
= gtk_combo_new();
73 wxSize newSize
= size
;
74 if (newSize
.x
== -1) newSize
.x
= 100;
75 if (newSize
.y
== -1) newSize
.y
= 26;
76 SetSize( newSize
.x
, newSize
.y
);
78 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
80 for (int i
= 0; i
< n
; i
++)
83 list_item
= gtk_list_item_new_with_label( choices
[i
] );
85 gtk_container_add( GTK_CONTAINER(list
), list_item
);
87 m_clientData
.Append( (wxObject
*)NULL
);
89 gtk_widget_show( list_item
);
91 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
92 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
97 ConnectWidget( GTK_COMBO(m_widget
)->button
);
99 if (!value
.IsNull()) SetValue( value
);
106 void wxComboBox::Clear(void)
108 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
109 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
111 m_clientData
.Clear();
114 void wxComboBox::Append( const wxString
&item
)
116 Append( item
, (char*)NULL
);
119 void wxComboBox::Append( const wxString
&item
, char *clientData
)
121 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
123 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
127 GtkBin
*bin
= GTK_BIN( list_item
);
128 gtk_widget_set_style( bin
->child
,
130 gtk_widget_get_style( m_widget
) ) );
133 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
134 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
136 m_clientData
.Append( (wxObject
*)clientData
);
138 gtk_container_add( GTK_CONTAINER(list
), list_item
);
140 gtk_widget_show( list_item
);
143 void wxComboBox::Delete( int n
)
145 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
147 GList
*child
= g_list_nth( listbox
->children
, n
);
151 wxFAIL_MSG("wrong index");
155 GList
*list
= g_list_append( NULL
, child
->data
);
156 gtk_list_remove_items( listbox
, list
);
159 wxNode
*node
= m_clientData
.Nth( n
);
162 wxFAIL_MSG( "wrong index" );
165 m_clientData
.DeleteNode( node
);
168 int wxComboBox::FindString( const wxString
&item
)
170 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
172 GList
*child
= GTK_LIST(list
)->children
;
176 GtkBin
*bin
= GTK_BIN( child
->data
);
177 GtkLabel
*label
= GTK_LABEL( bin
->child
);
178 if (item
== label
->label
) return count
;
183 wxFAIL_MSG( "wxComboBox: string not found" );
188 char* wxComboBox::GetClientData( int n
)
190 wxNode
*node
= m_clientData
.Nth( n
);
191 if (node
) return (char*)node
->Data();
193 wxFAIL_MSG( "wxComboBox: wrong index" );
195 return (char *) NULL
;
198 void wxComboBox::SetClientData( int n
, char * clientData
)
200 wxNode
*node
= m_clientData
.Nth( n
);
201 if (node
) node
->SetData( (wxObject
*) clientData
);
203 wxFAIL_MSG( "wxComboBox: wrong index" );
206 int wxComboBox::GetSelection(void) const
208 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
210 GList
*selection
= GTK_LIST(list
)->selection
;
213 GList
*child
= GTK_LIST(list
)->children
;
217 if (child
->data
== selection
->data
) return count
;
223 wxFAIL_MSG( "wxComboBox: no selection" );
228 wxString
wxComboBox::GetString( int n
) const
230 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
232 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
235 GtkBin
*bin
= GTK_BIN( child
->data
);
236 GtkLabel
*label
= GTK_LABEL( bin
->child
);
240 wxFAIL_MSG( "wxComboBox: wrong index" );
245 wxString
wxComboBox::GetStringSelection(void) const
247 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
249 GList
*selection
= GTK_LIST(list
)->selection
;
252 GtkBin
*bin
= GTK_BIN( selection
->data
);
253 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
257 wxFAIL_MSG( "wxComboBox: no selection" );
262 int wxComboBox::Number(void) const
264 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
266 GList
*child
= GTK_LIST(list
)->children
;
268 while (child
) { count
++; child
= child
->next
; }
272 void wxComboBox::SetSelection( int n
)
274 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
275 gtk_list_select_item( GTK_LIST(list
), n
);
278 void wxComboBox::SetStringSelection( const wxString
&string
)
280 int res
= FindString( string
);
281 if (res
== -1) return;
285 wxString
wxComboBox::GetValue(void) const
287 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
288 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
292 void wxComboBox::SetValue( const wxString
& value
)
294 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
296 if (!value
.IsNull()) tmp
= value
;
297 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
300 void wxComboBox::Copy(void)
302 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
303 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
306 void wxComboBox::Cut(void)
308 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
309 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
312 void wxComboBox::Paste(void)
314 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
315 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
318 void wxComboBox::SetInsertionPoint( long pos
)
320 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
322 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
325 void wxComboBox::SetInsertionPointEnd(void)
327 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
328 int pos
= GTK_ENTRY(entry
)->text_length
;
329 SetInsertionPoint( pos
-1 );
332 long wxComboBox::GetInsertionPoint(void) const
334 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
335 return (long) GTK_EDITABLE(entry
)->current_pos
;
338 long wxComboBox::GetLastPosition(void) const
340 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
341 int pos
= GTK_ENTRY(entry
)->text_length
;
345 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
347 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
348 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
349 if (value
.IsNull()) return;
351 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
354 void wxComboBox::Remove(long from
, long to
)
356 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
357 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
360 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
362 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
365 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
367 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
370 void wxComboBox::OnSize( wxSizeEvent
&event
)
372 wxControl::OnSize( event
);
376 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
378 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
379 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
382 void wxComboBox::SetFont( const wxFont
&font
)
384 wxWindow::SetFont( font
);
386 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
388 gtk_widget_set_style( entry
,
390 gtk_widget_get_style( m_widget
) ) );
392 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
394 GList
*child
= GTK_LIST(list
)->children
;
397 GtkBin
*bin
= (GtkBin
*) child
->data
;
398 gtk_widget_set_style( bin
->child
,
400 gtk_widget_get_style( m_widget
) ) );
406 GtkWidget
* wxComboBox::GetConnectWidget(void)
408 return GTK_COMBO(m_widget
)->entry
;
411 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
413 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
414 (window
== GTK_COMBO(m_widget
)->button
->window
) );