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"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
34 if (!combo
->HasVMT()) return;
35 if (g_blockEventsOnDrag
) return;
37 if (combo
->m_alreadySent
)
39 combo
->m_alreadySent
= FALSE
;
43 combo
->m_alreadySent
= TRUE
;
45 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId());
46 event
.SetInt( combo
->GetSelection() );
47 wxString
tmp( combo
->GetStringSelection() );
48 event
.SetString( WXSTRINGCAST(tmp
) );
49 event
.SetEventObject(combo
);
50 combo
->GetEventHandler()->ProcessEvent(event
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
58 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
60 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->m_windowId
);
61 event
.SetString( copystring(combo
->GetValue()) );
62 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
64 delete[] event
.GetString();
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
73 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
74 EVT_SIZE(wxComboBox::OnSize
)
77 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
78 const wxPoint
& pos
, const wxSize
& size
,
79 int n
, const wxString choices
[],
80 long style
, const wxValidator
& validator
,
81 const wxString
& name
)
83 m_alreadySent
= FALSE
;
85 m_acceptsFocus
= TRUE
;
87 PreCreation( parent
, id
, pos
, size
, style
, name
);
89 SetValidator( validator
);
91 m_widget
= gtk_combo_new();
93 wxSize newSize
= size
;
94 if (newSize
.x
== -1) newSize
.x
= 100;
95 if (newSize
.y
== -1) newSize
.y
= 26;
96 SetSize( newSize
.x
, newSize
.y
);
98 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
100 for (int i
= 0; i
< n
; i
++)
102 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
104 m_clientDataList
.Append( (wxObject
*)NULL
);
105 m_clientObjectList
.Append( (wxObject
*)NULL
);
107 gtk_container_add( GTK_CONTAINER(list
), list_item
);
109 gtk_widget_realize( list_item
);
110 gtk_widget_realize( GTK_BIN(list_item
)->child
);
112 gtk_widget_show( list_item
);
114 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
115 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
118 m_parent
->AddChild( this );
120 (m_parent
->m_insertCallback
)( m_parent
, this );
124 ConnectWidget( GTK_COMBO(m_widget
)->button
);
126 if (!value
.IsNull()) SetValue( value
);
128 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
129 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
130 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
132 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
133 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
135 SetBackgroundColour( parent
->GetBackgroundColour() );
136 SetForegroundColour( parent
->GetForegroundColour() );
137 SetFont( parent
->GetFont() );
144 wxComboBox::~wxComboBox()
146 wxNode
*node
= m_clientDataList
.First();
149 wxClientData
*cd
= (wxClientData
*)node
->Data();
153 m_clientDataList
.Clear();
156 void wxComboBox::AppendCommon( const wxString
&item
)
158 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
160 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
162 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
164 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
165 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
167 gtk_container_add( GTK_CONTAINER(list
), list_item
);
169 if (m_widgetStyle
) ApplyWidgetStyle();
171 gtk_widget_show( list_item
);
174 void wxComboBox::Append( const wxString
&item
)
176 m_clientDataList
.Append( (wxObject
*) NULL
);
177 m_clientObjectList
.Append( (wxObject
*) NULL
);
179 AppendCommon( item
);
182 void wxComboBox::Append( const wxString
&item
, void *clientData
)
184 m_clientDataList
.Append( (wxObject
*) clientData
);
185 m_clientObjectList
.Append( (wxObject
*)NULL
);
187 AppendCommon( item
);
190 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
192 m_clientDataList
.Append( (wxObject
*) NULL
);
193 m_clientObjectList
.Append( (wxObject
*) clientData
);
195 AppendCommon( item
);
198 void wxComboBox::SetClientData( int n
, void* clientData
)
200 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
202 wxNode
*node
= m_clientDataList
.Nth( n
);
205 node
->SetData( (wxObject
*) clientData
);
208 void* wxComboBox::GetClientData( int n
)
210 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
212 wxNode
*node
= m_clientDataList
.Nth( n
);
213 if (!node
) return NULL
;
218 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
220 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
222 wxNode
*node
= m_clientObjectList
.Nth( n
);
225 wxClientData
*cd
= (wxClientData
*) node
->Data();
228 node
->SetData( (wxObject
*) clientData
);
231 wxClientData
* wxComboBox::GetClientObject( int n
)
233 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
235 wxNode
*node
= m_clientDataList
.Nth( n
);
236 if (!node
) return (wxClientData
*) NULL
;
238 return (wxClientData
*) node
->Data();
241 void wxComboBox::Clear()
243 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
245 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
246 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
248 wxNode
*node
= m_clientObjectList
.First();
251 wxClientData
*cd
= (wxClientData
*)node
->Data();
255 m_clientObjectList
.Clear();
257 m_clientDataList
.Clear();
260 void wxComboBox::Delete( int n
)
262 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
264 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
266 GList
*child
= g_list_nth( listbox
->children
, n
);
270 wxFAIL_MSG("wrong index");
274 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
275 gtk_list_remove_items( listbox
, list
);
278 wxNode
*node
= m_clientObjectList
.Nth( n
);
281 wxClientData
*cd
= (wxClientData
*)node
->Data();
283 m_clientObjectList
.DeleteNode( node
);
286 node
= m_clientDataList
.Nth( n
);
289 m_clientDataList
.DeleteNode( node
);
293 int wxComboBox::FindString( const wxString
&item
)
295 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
297 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
299 GList
*child
= GTK_LIST(list
)->children
;
303 GtkBin
*bin
= GTK_BIN( child
->data
);
304 GtkLabel
*label
= GTK_LABEL( bin
->child
);
305 if (item
== label
->label
) return count
;
310 wxFAIL_MSG( "wxComboBox: string not found" );
315 int wxComboBox::GetSelection() const
317 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
319 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
321 GList
*selection
= GTK_LIST(list
)->selection
;
324 GList
*child
= GTK_LIST(list
)->children
;
328 if (child
->data
== selection
->data
) return count
;
334 wxFAIL_MSG( "wxComboBox: no selection" );
339 wxString
wxComboBox::GetString( int n
) const
341 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
343 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
345 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
348 GtkBin
*bin
= GTK_BIN( child
->data
);
349 GtkLabel
*label
= GTK_LABEL( bin
->child
);
353 wxFAIL_MSG( "wxComboBox: wrong index" );
358 wxString
wxComboBox::GetStringSelection() const
360 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
362 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
364 GList
*selection
= GTK_LIST(list
)->selection
;
367 GtkBin
*bin
= GTK_BIN( selection
->data
);
368 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
372 wxFAIL_MSG( "wxComboBox: no selection" );
377 int wxComboBox::Number() const
379 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
381 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
383 GList
*child
= GTK_LIST(list
)->children
;
385 while (child
) { count
++; child
= child
->next
; }
389 void wxComboBox::SetSelection( int n
)
391 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
393 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
394 gtk_list_select_item( GTK_LIST(list
), n
);
397 void wxComboBox::SetStringSelection( const wxString
&string
)
399 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
401 int res
= FindString( string
);
402 if (res
== -1) return;
406 wxString
wxComboBox::GetValue() const
408 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
409 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
413 void wxComboBox::SetValue( const wxString
& value
)
415 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
417 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
419 if (!value
.IsNull()) tmp
= value
;
420 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
423 void wxComboBox::Copy()
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_copy_clipboard( GTK_EDITABLE(entry
) );
431 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
435 void wxComboBox::Cut()
437 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
439 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
440 #if (GTK_MINOR_VERSION == 1)
441 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
443 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
447 void wxComboBox::Paste()
449 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
451 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
452 #if (GTK_MINOR_VERSION == 1)
453 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
455 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
459 void wxComboBox::SetInsertionPoint( long pos
)
461 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
463 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
467 void wxComboBox::SetInsertionPointEnd()
469 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
471 SetInsertionPoint( -1 );
474 long wxComboBox::GetInsertionPoint() const
476 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
477 return (long) GTK_EDITABLE(entry
)->current_pos
;
480 long wxComboBox::GetLastPosition() const
482 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
483 int pos
= GTK_ENTRY(entry
)->text_length
;
487 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
489 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
491 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
492 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
493 if (value
.IsNull()) return;
495 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
498 void wxComboBox::Remove(long from
, long to
)
500 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
502 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
503 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
506 void wxComboBox::SetSelection( long from
, long to
)
508 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
509 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
512 void wxComboBox::SetEditable( bool editable
)
514 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
515 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
518 void wxComboBox::OnSize( wxSizeEvent
&event
)
520 wxControl::OnSize( event
);
523 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
525 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
526 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
529 void wxComboBox::ApplyWidgetStyle()
533 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
534 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
535 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
537 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
538 GList
*child
= list
->children
;
541 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
543 GtkBin
*bin
= GTK_BIN(child
->data
);
544 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
550 GtkWidget
* wxComboBox::GetConnectWidget()
552 return GTK_COMBO(m_widget
)->entry
;
555 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
557 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
558 (window
== GTK_COMBO(m_widget
)->button
->window
) );