1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
14 #include "wx/combobox.h"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
29 if (!combo
->HasVMT()) return;
30 if (g_blockEventsOnDrag
) return;
32 if (combo
->m_alreadySent
)
34 combo
->m_alreadySent
= FALSE
;
38 combo
->m_alreadySent
= TRUE
;
40 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
41 event
.SetInt( combo
->GetSelection() );
42 wxString
tmp( combo
->GetStringSelection() );
43 event
.SetString( WXSTRINGCAST(tmp
) );
44 event
.SetEventObject(combo
);
45 combo
->GetEventHandler()->ProcessEvent(event
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
54 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
55 EVT_SIZE(wxComboBox::OnSize
)
58 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int n
, const wxString choices
[],
61 long style
, const wxValidator
& validator
, const wxString
& name
)
63 m_alreadySent
= FALSE
;
66 PreCreation( parent
, id
, pos
, size
, style
, name
);
68 SetValidator( validator
);
70 m_widget
= gtk_combo_new();
72 wxSize newSize
= size
;
73 if (newSize
.x
== -1) newSize
.x
= 100;
74 if (newSize
.y
== -1) newSize
.y
= 26;
75 SetSize( newSize
.x
, newSize
.y
);
77 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
79 for (int i
= 0; i
< n
; i
++)
82 list_item
= gtk_list_item_new_with_label( choices
[i
] );
84 gtk_container_add( GTK_CONTAINER(list
), list_item
);
86 m_clientData
.Append( (wxObject
*)NULL
);
88 gtk_widget_show( list_item
);
90 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
91 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
96 ConnectWidget( GTK_COMBO(m_widget
)->button
);
98 if (!value
.IsNull()) SetValue( value
);
105 void wxComboBox::Clear(void)
107 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
108 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
110 m_clientData
.Clear();
113 void wxComboBox::Append( const wxString
&item
)
115 Append( item
, (char*)NULL
);
118 void wxComboBox::Append( const wxString
&item
, char *clientData
)
120 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
122 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
126 GtkBin
*bin
= GTK_BIN( list_item
);
127 gtk_widget_set_style( bin
->child
,
129 gtk_widget_get_style( m_widget
) ) );
132 if (m_backgroundColour
!= wxNullColour
)
134 GtkBin
*bin
= GTK_BIN( list_item
);
135 SetBackgroundColourHelper( bin
->child
->window
);
138 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
139 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
141 m_clientData
.Append( (wxObject
*)clientData
);
143 gtk_container_add( GTK_CONTAINER(list
), list_item
);
145 gtk_widget_show( list_item
);
148 void wxComboBox::Delete( int n
)
150 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
152 GList
*child
= g_list_nth( listbox
->children
, n
);
156 wxFAIL_MSG("wrong index");
160 GList
*list
= g_list_append( NULL
, child
->data
);
161 gtk_list_remove_items( listbox
, list
);
164 wxNode
*node
= m_clientData
.Nth( n
);
167 wxFAIL_MSG( "wrong index" );
170 m_clientData
.DeleteNode( node
);
173 int wxComboBox::FindString( const wxString
&item
)
175 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
177 GList
*child
= GTK_LIST(list
)->children
;
181 GtkBin
*bin
= GTK_BIN( child
->data
);
182 GtkLabel
*label
= GTK_LABEL( bin
->child
);
183 if (item
== label
->label
) return count
;
188 wxFAIL_MSG( "wxComboBox: string not found" );
193 char* wxComboBox::GetClientData( int n
)
195 wxNode
*node
= m_clientData
.Nth( n
);
196 if (node
) return (char*)node
->Data();
198 wxFAIL_MSG( "wxComboBox: wrong index" );
200 return (char *) NULL
;
203 void wxComboBox::SetClientData( int n
, char * clientData
)
205 wxNode
*node
= m_clientData
.Nth( n
);
206 if (node
) node
->SetData( (wxObject
*) clientData
);
208 wxFAIL_MSG( "wxComboBox: wrong index" );
211 int wxComboBox::GetSelection(void) const
213 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
215 GList
*selection
= GTK_LIST(list
)->selection
;
218 GList
*child
= GTK_LIST(list
)->children
;
222 if (child
->data
== selection
->data
) return count
;
228 wxFAIL_MSG( "wxComboBox: no selection" );
233 wxString
wxComboBox::GetString( int n
) const
235 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
237 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
240 GtkBin
*bin
= GTK_BIN( child
->data
);
241 GtkLabel
*label
= GTK_LABEL( bin
->child
);
245 wxFAIL_MSG( "wxComboBox: wrong index" );
250 wxString
wxComboBox::GetStringSelection(void) const
252 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
254 GList
*selection
= GTK_LIST(list
)->selection
;
257 GtkBin
*bin
= GTK_BIN( selection
->data
);
258 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
262 wxFAIL_MSG( "wxComboBox: no selection" );
267 int wxComboBox::Number(void) const
269 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
271 GList
*child
= GTK_LIST(list
)->children
;
273 while (child
) { count
++; child
= child
->next
; }
277 void wxComboBox::SetSelection( int n
)
279 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
280 gtk_list_select_item( GTK_LIST(list
), n
);
283 void wxComboBox::SetStringSelection( const wxString
&string
)
285 int res
= FindString( string
);
286 if (res
== -1) return;
290 wxString
wxComboBox::GetValue(void) const
292 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
293 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
297 void wxComboBox::SetValue( const wxString
& value
)
299 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
301 if (!value
.IsNull()) tmp
= value
;
302 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
305 void wxComboBox::Copy(void)
307 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
308 #if (GTK_MINOR_VERSION == 1)
309 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
311 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
315 void wxComboBox::Cut(void)
317 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
318 #if (GTK_MINOR_VERSION == 1)
319 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
321 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
325 void wxComboBox::Paste(void)
327 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
328 #if (GTK_MINOR_VERSION == 1)
329 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
331 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
335 void wxComboBox::SetInsertionPoint( long pos
)
337 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
339 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
342 void wxComboBox::SetInsertionPointEnd(void)
344 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
345 int pos
= GTK_ENTRY(entry
)->text_length
;
346 SetInsertionPoint( pos
-1 );
349 long wxComboBox::GetInsertionPoint(void) const
351 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
352 return (long) GTK_EDITABLE(entry
)->current_pos
;
355 long wxComboBox::GetLastPosition(void) const
357 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
358 int pos
= GTK_ENTRY(entry
)->text_length
;
362 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
364 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
365 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
366 if (value
.IsNull()) return;
368 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
371 void wxComboBox::Remove(long from
, long to
)
373 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
374 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
377 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
379 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
382 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
384 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
387 void wxComboBox::OnSize( wxSizeEvent
&event
)
389 wxControl::OnSize( event
);
393 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
395 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
396 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
399 void wxComboBox::SetFont( const wxFont
&font
)
401 wxWindow::SetFont( font
);
403 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
405 gtk_widget_set_style( entry
,
407 gtk_widget_get_style( m_widget
) ) );
409 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
411 GList
*child
= GTK_LIST(list
)->children
;
414 GtkBin
*bin
= (GtkBin
*) child
->data
;
415 gtk_widget_set_style( bin
->child
,
417 gtk_widget_get_style( m_widget
) ) );
423 GtkWidget
* wxComboBox::GetConnectWidget(void)
425 return GTK_COMBO(m_widget
)->entry
;
428 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
430 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
431 (window
== GTK_COMBO(m_widget
)->button
->window
) );
434 void wxComboBox::SetBackgroundColour( const wxColour
&colour
)
436 wxWindow::SetBackgroundColour( colour
);
438 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
440 GList
*child
= GTK_LIST(list
)->children
;
443 GtkBin
*bin
= (GtkBin
*) child
->data
;
444 SetBackgroundColourHelper( bin
->child
->window
);