1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
15 #include "wx/combobox.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern bool g_blockEventsOnDrag
;
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
36 if (!combo
->HasVMT()) return;
37 if (g_blockEventsOnDrag
) return;
39 if (combo
->m_alreadySent
)
41 combo
->m_alreadySent
= FALSE
;
45 combo
->m_alreadySent
= TRUE
;
47 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
48 event
.SetInt( combo
->GetSelection() );
49 wxString
tmp( combo
->GetStringSelection() );
50 event
.SetString( WXSTRINGCAST(tmp
) );
51 event
.SetEventObject(combo
);
52 combo
->GetEventHandler()->ProcessEvent(event
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
61 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
62 EVT_SIZE(wxComboBox::OnSize
)
65 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
66 const wxPoint
& pos
, const wxSize
& size
,
67 int n
, const wxString choices
[],
68 long style
, const wxValidator
& validator
,
69 const wxString
& name
)
71 m_alreadySent
= FALSE
;
73 m_acceptsFocus
= TRUE
;
75 PreCreation( parent
, id
, pos
, size
, style
, name
);
77 SetValidator( validator
);
79 m_widget
= gtk_combo_new();
81 wxSize newSize
= size
;
82 if (newSize
.x
== -1) newSize
.x
= 100;
83 if (newSize
.y
== -1) newSize
.y
= 26;
84 SetSize( newSize
.x
, newSize
.y
);
86 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
88 for (int i
= 0; i
< n
; i
++)
90 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
92 m_clientDataList
.Append( (wxObject
*)NULL
);
93 m_clientObjectList
.Append( (wxObject
*)NULL
);
95 gtk_container_add( GTK_CONTAINER(list
), list_item
);
97 gtk_widget_realize( list_item
);
98 gtk_widget_realize( GTK_BIN(list_item
)->child
);
100 gtk_widget_show( list_item
);
102 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
103 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
106 m_parent
->AddChild( this );
108 (m_parent
->m_insertCallback
)( m_parent
, this );
112 ConnectWidget( GTK_COMBO(m_widget
)->button
);
114 if (!value
.IsNull()) SetValue( value
);
116 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
117 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
118 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
120 SetBackgroundColour( parent
->GetBackgroundColour() );
121 SetForegroundColour( parent
->GetForegroundColour() );
128 wxComboBox::~wxComboBox()
130 wxNode
*node
= m_clientDataList
.First();
133 wxClientData
*cd
= (wxClientData
*)node
->Data();
137 m_clientDataList
.Clear();
140 void wxComboBox::AppendCommon( const wxString
&item
)
142 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
144 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
146 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
148 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
149 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
151 gtk_container_add( GTK_CONTAINER(list
), list_item
);
153 if (m_widgetStyle
) ApplyWidgetStyle();
155 gtk_widget_show( list_item
);
158 void wxComboBox::Append( const wxString
&item
)
160 m_clientDataList
.Append( (wxObject
*) NULL
);
161 m_clientObjectList
.Append( (wxObject
*) NULL
);
163 AppendCommon( item
);
166 void wxComboBox::Append( const wxString
&item
, void *clientData
)
168 m_clientDataList
.Append( (wxObject
*) clientData
);
169 m_clientObjectList
.Append( (wxObject
*)NULL
);
171 AppendCommon( item
);
174 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
176 m_clientDataList
.Append( (wxObject
*) NULL
);
177 m_clientObjectList
.Append( (wxObject
*) clientData
);
179 AppendCommon( item
);
182 void wxComboBox::SetClientData( int n
, void* clientData
)
184 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
186 wxNode
*node
= m_clientDataList
.Nth( n
);
189 node
->SetData( (wxObject
*) clientData
);
192 void* wxComboBox::GetClientData( int n
)
194 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
196 wxNode
*node
= m_clientDataList
.Nth( n
);
197 if (!node
) return NULL
;
202 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
204 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
206 wxNode
*node
= m_clientObjectList
.Nth( n
);
209 wxClientData
*cd
= (wxClientData
*) node
->Data();
212 node
->SetData( (wxObject
*) clientData
);
215 wxClientData
* wxComboBox::GetClientObject( int n
)
217 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
219 wxNode
*node
= m_clientDataList
.Nth( n
);
220 if (!node
) return (wxClientData
*) NULL
;
222 return (wxClientData
*) node
->Data();
225 void wxComboBox::Clear()
227 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
229 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
230 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
232 wxNode
*node
= m_clientObjectList
.First();
235 wxClientData
*cd
= (wxClientData
*)node
->Data();
239 m_clientObjectList
.Clear();
241 m_clientDataList
.Clear();
244 void wxComboBox::Delete( int n
)
246 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
248 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
250 GList
*child
= g_list_nth( listbox
->children
, n
);
254 wxFAIL_MSG("wrong index");
258 GList
*list
= g_list_append( NULL
, child
->data
);
259 gtk_list_remove_items( listbox
, list
);
262 wxNode
*node
= m_clientObjectList
.Nth( n
);
265 wxClientData
*cd
= (wxClientData
*)node
->Data();
267 m_clientObjectList
.DeleteNode( node
);
270 node
= m_clientDataList
.Nth( n
);
273 m_clientDataList
.DeleteNode( node
);
277 int wxComboBox::FindString( const wxString
&item
)
279 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
281 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
283 GList
*child
= GTK_LIST(list
)->children
;
287 GtkBin
*bin
= GTK_BIN( child
->data
);
288 GtkLabel
*label
= GTK_LABEL( bin
->child
);
289 if (item
== label
->label
) return count
;
294 wxFAIL_MSG( "wxComboBox: string not found" );
299 int wxComboBox::GetSelection() const
301 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
303 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
305 GList
*selection
= GTK_LIST(list
)->selection
;
308 GList
*child
= GTK_LIST(list
)->children
;
312 if (child
->data
== selection
->data
) return count
;
318 wxFAIL_MSG( "wxComboBox: no selection" );
323 wxString
wxComboBox::GetString( int n
) const
325 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
327 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
329 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
332 GtkBin
*bin
= GTK_BIN( child
->data
);
333 GtkLabel
*label
= GTK_LABEL( bin
->child
);
337 wxFAIL_MSG( "wxComboBox: wrong index" );
342 wxString
wxComboBox::GetStringSelection() const
344 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
346 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
348 GList
*selection
= GTK_LIST(list
)->selection
;
351 GtkBin
*bin
= GTK_BIN( selection
->data
);
352 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
356 wxFAIL_MSG( "wxComboBox: no selection" );
361 int wxComboBox::Number() const
363 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
365 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
367 GList
*child
= GTK_LIST(list
)->children
;
369 while (child
) { count
++; child
= child
->next
; }
373 void wxComboBox::SetSelection( int n
)
375 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
377 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
378 gtk_list_select_item( GTK_LIST(list
), n
);
381 void wxComboBox::SetStringSelection( const wxString
&string
)
383 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
385 int res
= FindString( string
);
386 if (res
== -1) return;
390 wxString
wxComboBox::GetValue() const
392 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
393 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
397 void wxComboBox::SetValue( const wxString
& value
)
399 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
401 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
403 if (!value
.IsNull()) tmp
= value
;
404 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
407 void wxComboBox::Copy()
409 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
411 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
412 #if (GTK_MINOR_VERSION == 1)
413 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
415 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
419 void wxComboBox::Cut()
421 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
423 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
424 #if (GTK_MINOR_VERSION == 1)
425 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
427 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
431 void wxComboBox::Paste()
433 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
435 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
436 #if (GTK_MINOR_VERSION == 1)
437 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
439 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
443 void wxComboBox::SetInsertionPoint( long pos
)
445 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
447 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
452 void wxComboBox::SetInsertionPointEnd()
454 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
456 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
457 int pos
= GTK_ENTRY(entry
)->text_length
;
458 SetInsertionPoint( pos
-1 );
461 long wxComboBox::GetInsertionPoint() const
463 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 return (long) GTK_EDITABLE(entry
)->current_pos
;
467 long wxComboBox::GetLastPosition() const
469 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
470 int pos
= GTK_ENTRY(entry
)->text_length
;
474 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
476 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
478 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
479 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
480 if (value
.IsNull()) return;
482 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
485 void wxComboBox::Remove(long from
, long to
)
487 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
489 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
490 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
493 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
495 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
498 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
500 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
503 void wxComboBox::OnSize( wxSizeEvent
&event
)
505 wxControl::OnSize( event
);
508 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
510 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
511 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
514 void wxComboBox::ApplyWidgetStyle()
518 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
519 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
520 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
522 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
523 GList
*child
= list
->children
;
526 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
528 GtkBin
*bin
= GTK_BIN(child
->data
);
529 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
535 GtkWidget
* wxComboBox::GetConnectWidget()
537 return GTK_COMBO(m_widget
)->entry
;
540 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
542 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
543 (window
== GTK_COMBO(m_widget
)->button
->window
) );