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
++)
81 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
83 m_clientData
.Append( (wxObject
*)NULL
);
85 gtk_container_add( GTK_CONTAINER(list
), list_item
);
87 gtk_widget_realize( list_item
);
88 gtk_widget_realize( GTK_BIN(list_item
)->child
);
90 gtk_widget_show( list_item
);
92 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
93 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
98 ConnectWidget( GTK_COMBO(m_widget
)->button
);
100 if (!value
.IsNull()) SetValue( value
);
102 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
103 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
104 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
106 SetBackgroundColour( parent
->GetBackgroundColour() );
113 void wxComboBox::Clear(void)
115 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
117 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
118 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
120 m_clientData
.Clear();
123 void wxComboBox::Append( const wxString
&item
)
125 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
127 Append( item
, (char*)NULL
);
130 void wxComboBox::Append( const wxString
&item
, char *clientData
)
132 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
134 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
136 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
140 gtk_widget_set_style( list_item
, m_widgetStyle
);
141 gtk_widget_set_style( GTK_BIN( list_item
)->child
, m_widgetStyle
);
144 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
145 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
147 m_clientData
.Append( (wxObject
*)clientData
);
149 gtk_container_add( GTK_CONTAINER(list
), list_item
);
151 gtk_widget_show( list_item
);
154 void wxComboBox::Delete( int n
)
156 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
158 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
160 GList
*child
= g_list_nth( listbox
->children
, n
);
164 wxFAIL_MSG("wrong index");
168 GList
*list
= g_list_append( NULL
, child
->data
);
169 gtk_list_remove_items( listbox
, list
);
172 wxNode
*node
= m_clientData
.Nth( n
);
175 wxFAIL_MSG( "wrong index" );
178 m_clientData
.DeleteNode( node
);
181 int wxComboBox::FindString( const wxString
&item
)
183 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
185 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
187 GList
*child
= GTK_LIST(list
)->children
;
191 GtkBin
*bin
= GTK_BIN( child
->data
);
192 GtkLabel
*label
= GTK_LABEL( bin
->child
);
193 if (item
== label
->label
) return count
;
198 wxFAIL_MSG( "wxComboBox: string not found" );
203 char* wxComboBox::GetClientData( int n
)
205 wxCHECK_MSG( m_widget
!= NULL
, (char*)NULL
, "invalid combobox" );
207 wxNode
*node
= m_clientData
.Nth( n
);
208 if (node
) return (char*)node
->Data();
210 wxFAIL_MSG( "wxComboBox: wrong index" );
212 return (char *) NULL
;
215 void wxComboBox::SetClientData( int n
, char * clientData
)
217 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
219 wxNode
*node
= m_clientData
.Nth( n
);
220 if (node
) node
->SetData( (wxObject
*) clientData
);
222 wxFAIL_MSG( "wxComboBox: wrong index" );
225 int wxComboBox::GetSelection(void) const
227 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
229 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
231 GList
*selection
= GTK_LIST(list
)->selection
;
234 GList
*child
= GTK_LIST(list
)->children
;
238 if (child
->data
== selection
->data
) return count
;
244 wxFAIL_MSG( "wxComboBox: no selection" );
249 wxString
wxComboBox::GetString( int n
) const
251 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
253 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
255 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
258 GtkBin
*bin
= GTK_BIN( child
->data
);
259 GtkLabel
*label
= GTK_LABEL( bin
->child
);
263 wxFAIL_MSG( "wxComboBox: wrong index" );
268 wxString
wxComboBox::GetStringSelection(void) const
270 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
272 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
274 GList
*selection
= GTK_LIST(list
)->selection
;
277 GtkBin
*bin
= GTK_BIN( selection
->data
);
278 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
282 wxFAIL_MSG( "wxComboBox: no selection" );
287 int wxComboBox::Number(void) const
289 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
291 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
293 GList
*child
= GTK_LIST(list
)->children
;
295 while (child
) { count
++; child
= child
->next
; }
299 void wxComboBox::SetSelection( int n
)
301 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
303 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
304 gtk_list_select_item( GTK_LIST(list
), n
);
307 void wxComboBox::SetStringSelection( const wxString
&string
)
309 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
311 int res
= FindString( string
);
312 if (res
== -1) return;
316 wxString
wxComboBox::GetValue(void) const
318 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
319 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
323 void wxComboBox::SetValue( const wxString
& value
)
325 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
327 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
329 if (!value
.IsNull()) tmp
= value
;
330 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
333 void wxComboBox::Copy(void)
335 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
337 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
338 #if (GTK_MINOR_VERSION == 1)
339 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
341 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
345 void wxComboBox::Cut(void)
347 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
349 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
350 #if (GTK_MINOR_VERSION == 1)
351 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
353 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
357 void wxComboBox::Paste(void)
359 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
361 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
362 #if (GTK_MINOR_VERSION == 1)
363 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
365 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
369 void wxComboBox::SetInsertionPoint( long pos
)
371 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
373 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
375 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
378 void wxComboBox::SetInsertionPointEnd(void)
380 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
382 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
383 int pos
= GTK_ENTRY(entry
)->text_length
;
384 SetInsertionPoint( pos
-1 );
387 long wxComboBox::GetInsertionPoint(void) const
389 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
390 return (long) GTK_EDITABLE(entry
)->current_pos
;
393 long wxComboBox::GetLastPosition(void) const
395 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
396 int pos
= GTK_ENTRY(entry
)->text_length
;
400 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
402 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
404 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
405 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
406 if (value
.IsNull()) return;
408 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
411 void wxComboBox::Remove(long from
, long to
)
413 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
415 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
416 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
419 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
421 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
424 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
426 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
429 void wxComboBox::OnSize( wxSizeEvent
&event
)
431 wxControl::OnSize( event
);
435 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
437 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
438 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
441 void wxComboBox::SetFont( const wxFont
&font
)
443 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
445 wxWindow::SetFont( font
);
447 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
449 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
451 GList
*child
= list
->children
;
454 GtkBin
*bin
= (GtkBin
*) child
->data
;
456 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
462 GtkWidget
* wxComboBox::GetConnectWidget(void)
464 return GTK_COMBO(m_widget
)->entry
;
467 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
469 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
470 (window
== GTK_COMBO(m_widget
)->button
->window
) );
473 void wxComboBox::SetBackgroundColour( const wxColour
&colour
)
475 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
477 wxControl::SetBackgroundColour( colour
);
479 if (!m_backgroundColour
.Ok()) return;
481 // gtk_widget_set_style( m_widget, m_widgetStyle );
483 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
484 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
485 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
487 GList
*child
= GTK_LIST( GTK_COMBO(m_widget
)->list
)->children
;
490 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);