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
,
62 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
++)
82 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
84 m_clientDataList
.Append( (wxObject
*)NULL
);
85 m_clientObjectList
.Append( (wxObject
*)NULL
);
87 gtk_container_add( GTK_CONTAINER(list
), list_item
);
89 gtk_widget_realize( list_item
);
90 gtk_widget_realize( GTK_BIN(list_item
)->child
);
92 gtk_widget_show( list_item
);
94 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
95 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
98 m_parent
->AddChild( this );
100 (m_parent
->m_insertCallback
)( m_parent
, this );
104 ConnectWidget( GTK_COMBO(m_widget
)->button
);
106 if (!value
.IsNull()) SetValue( value
);
108 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
109 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
110 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
112 SetBackgroundColour( parent
->GetBackgroundColour() );
113 SetForegroundColour( parent
->GetForegroundColour() );
120 wxComboBox::~wxComboBox()
122 wxNode
*node
= m_clientDataList
.First();
125 wxClientData
*cd
= (wxClientData
*)node
->Data();
129 m_clientDataList
.Clear();
132 void wxComboBox::AppendCommon( const wxString
&item
)
134 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
136 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
138 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
140 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
141 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
143 gtk_container_add( GTK_CONTAINER(list
), list_item
);
145 if (m_widgetStyle
) ApplyWidgetStyle();
147 gtk_widget_show( list_item
);
150 void wxComboBox::Append( const wxString
&item
)
152 m_clientDataList
.Append( (wxObject
*) NULL
);
153 m_clientObjectList
.Append( (wxObject
*) NULL
);
155 AppendCommon( item
);
158 void wxComboBox::Append( const wxString
&item
, void *clientData
)
160 m_clientDataList
.Append( (wxObject
*) clientData
);
161 m_clientObjectList
.Append( (wxObject
*)NULL
);
163 AppendCommon( item
);
166 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
168 m_clientDataList
.Append( (wxObject
*) NULL
);
169 m_clientObjectList
.Append( (wxObject
*) clientData
);
171 AppendCommon( item
);
174 void wxComboBox::SetClientData( int n
, void* clientData
)
176 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
178 wxNode
*node
= m_clientDataList
.Nth( n
);
181 node
->SetData( (wxObject
*) clientData
);
184 void* wxComboBox::GetClientData( int n
)
186 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
188 wxNode
*node
= m_clientDataList
.Nth( n
);
189 if (!node
) return NULL
;
194 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
196 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
198 wxNode
*node
= m_clientObjectList
.Nth( n
);
201 wxClientData
*cd
= (wxClientData
*) node
->Data();
204 node
->SetData( (wxObject
*) clientData
);
207 wxClientData
* wxComboBox::GetClientObject( int n
)
209 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
211 wxNode
*node
= m_clientDataList
.Nth( n
);
212 if (!node
) return (wxClientData
*) NULL
;
214 return (wxClientData
*) node
->Data();
217 void wxComboBox::Clear()
219 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
221 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
222 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
224 wxNode
*node
= m_clientObjectList
.First();
227 wxClientData
*cd
= (wxClientData
*)node
->Data();
231 m_clientObjectList
.Clear();
233 m_clientDataList
.Clear();
236 void wxComboBox::Delete( int n
)
238 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
240 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
242 GList
*child
= g_list_nth( listbox
->children
, n
);
246 wxFAIL_MSG("wrong index");
250 GList
*list
= g_list_append( NULL
, child
->data
);
251 gtk_list_remove_items( listbox
, list
);
254 wxNode
*node
= m_clientObjectList
.Nth( n
);
257 wxClientData
*cd
= (wxClientData
*)node
->Data();
259 m_clientObjectList
.DeleteNode( node
);
262 node
= m_clientDataList
.Nth( n
);
265 m_clientDataList
.DeleteNode( node
);
269 int wxComboBox::FindString( const wxString
&item
)
271 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
273 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
275 GList
*child
= GTK_LIST(list
)->children
;
279 GtkBin
*bin
= GTK_BIN( child
->data
);
280 GtkLabel
*label
= GTK_LABEL( bin
->child
);
281 if (item
== label
->label
) return count
;
286 wxFAIL_MSG( "wxComboBox: string not found" );
291 int wxComboBox::GetSelection() const
293 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
295 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
297 GList
*selection
= GTK_LIST(list
)->selection
;
300 GList
*child
= GTK_LIST(list
)->children
;
304 if (child
->data
== selection
->data
) return count
;
310 wxFAIL_MSG( "wxComboBox: no selection" );
315 wxString
wxComboBox::GetString( int n
) const
317 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
319 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
321 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
324 GtkBin
*bin
= GTK_BIN( child
->data
);
325 GtkLabel
*label
= GTK_LABEL( bin
->child
);
329 wxFAIL_MSG( "wxComboBox: wrong index" );
334 wxString
wxComboBox::GetStringSelection() const
336 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
338 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
340 GList
*selection
= GTK_LIST(list
)->selection
;
343 GtkBin
*bin
= GTK_BIN( selection
->data
);
344 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
348 wxFAIL_MSG( "wxComboBox: no selection" );
353 int wxComboBox::Number() const
355 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
357 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
359 GList
*child
= GTK_LIST(list
)->children
;
361 while (child
) { count
++; child
= child
->next
; }
365 void wxComboBox::SetSelection( int n
)
367 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
369 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
370 gtk_list_select_item( GTK_LIST(list
), n
);
373 void wxComboBox::SetStringSelection( const wxString
&string
)
375 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
377 int res
= FindString( string
);
378 if (res
== -1) return;
382 wxString
wxComboBox::GetValue() const
384 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
385 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
389 void wxComboBox::SetValue( const wxString
& value
)
391 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
393 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
395 if (!value
.IsNull()) tmp
= value
;
396 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
399 void wxComboBox::Copy()
401 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
403 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
404 #if (GTK_MINOR_VERSION == 1)
405 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
407 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
411 void wxComboBox::Cut()
413 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
415 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
416 #if (GTK_MINOR_VERSION == 1)
417 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
419 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
423 void wxComboBox::Paste()
425 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
427 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
428 #if (GTK_MINOR_VERSION == 1)
429 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
431 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
435 void wxComboBox::SetInsertionPoint( long pos
)
437 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
439 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
441 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
444 void wxComboBox::SetInsertionPointEnd()
446 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
448 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 int pos
= GTK_ENTRY(entry
)->text_length
;
450 SetInsertionPoint( pos
-1 );
453 long wxComboBox::GetInsertionPoint() const
455 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
456 return (long) GTK_EDITABLE(entry
)->current_pos
;
459 long wxComboBox::GetLastPosition() const
461 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
462 int pos
= GTK_ENTRY(entry
)->text_length
;
466 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
468 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
470 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
471 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
472 if (value
.IsNull()) return;
474 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
477 void wxComboBox::Remove(long from
, long to
)
479 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
481 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
482 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
485 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
487 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
490 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
492 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
495 void wxComboBox::OnSize( wxSizeEvent
&event
)
497 wxControl::OnSize( event
);
500 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
502 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
503 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
506 void wxComboBox::ApplyWidgetStyle()
510 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
511 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
512 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
514 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
515 GList
*child
= list
->children
;
518 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
520 GtkBin
*bin
= GTK_BIN(child
->data
);
521 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
527 GtkWidget
* wxComboBox::GetConnectWidget()
529 return GTK_COMBO(m_widget
)->entry
;
532 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
534 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
535 (window
== GTK_COMBO(m_widget
)->button
->window
) );