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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!combo
->HasVMT()) return;
45 if (g_blockEventsOnDrag
) return;
47 if (combo
->m_alreadySent
)
49 combo
->m_alreadySent
= FALSE
;
53 combo
->m_alreadySent
= TRUE
;
55 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
56 event
.SetInt( combo
->GetSelection() );
57 event
.SetString( combo
->GetStringSelection() );
58 event
.SetEventObject( combo
);
60 combo
->GetEventHandler()->ProcessEvent( event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
68 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
70 if (g_isIdle
) wxapp_install_idle_handler();
72 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
73 event
.SetString( combo
->GetValue() );
74 event
.SetEventObject( combo
);
75 combo
->GetEventHandler()->ProcessEvent( event
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
84 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
85 EVT_SIZE(wxComboBox::OnSize
)
86 EVT_CHAR(wxComboBox::OnChar
)
89 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
90 const wxPoint
& pos
, const wxSize
& size
,
91 int n
, const wxString choices
[],
92 long style
, const wxValidator
& validator
,
93 const wxString
& name
)
95 m_alreadySent
= FALSE
;
97 m_acceptsFocus
= TRUE
;
99 PreCreation( parent
, id
, pos
, size
, style
, name
);
101 SetValidator( validator
);
103 m_widget
= gtk_combo_new();
105 // make it more useable
106 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
108 wxSize newSize
= size
;
113 SetSize( newSize
.x
, newSize
.y
);
115 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
117 for (int i
= 0; i
< n
; i
++)
119 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
121 m_clientDataList
.Append( (wxObject
*)NULL
);
122 m_clientObjectList
.Append( (wxObject
*)NULL
);
124 gtk_container_add( GTK_CONTAINER(list
), list_item
);
126 gtk_widget_show( list_item
);
128 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
129 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
132 m_parent
->AddChild( this );
134 (m_parent
->m_insertCallback
)( m_parent
, this );
138 ConnectWidget( GTK_COMBO(m_widget
)->button
);
140 if (!value
.IsNull()) SetValue( value
);
142 if (style
& wxCB_READONLY
)
143 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
145 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
146 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
148 SetBackgroundColour( parent
->GetBackgroundColour() );
149 SetForegroundColour( parent
->GetForegroundColour() );
150 SetFont( parent
->GetFont() );
157 wxComboBox::~wxComboBox()
159 wxNode
*node
= m_clientDataList
.First();
162 wxClientData
*cd
= (wxClientData
*)node
->Data();
166 m_clientDataList
.Clear();
169 void wxComboBox::AppendCommon( const wxString
&item
)
171 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
173 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
175 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
177 gtk_container_add( GTK_CONTAINER(list
), list_item
);
179 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
180 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
182 if (GTK_WIDGET_REALIZED(m_widget
))
184 gtk_widget_realize( list_item
);
185 gtk_widget_realize( GTK_BIN(list_item
)->child
);
187 if (m_widgetStyle
) ApplyWidgetStyle();
190 gtk_widget_show( list_item
);
193 void wxComboBox::Append( const wxString
&item
)
195 m_clientDataList
.Append( (wxObject
*) NULL
);
196 m_clientObjectList
.Append( (wxObject
*) NULL
);
198 AppendCommon( item
);
201 void wxComboBox::Append( const wxString
&item
, void *clientData
)
203 m_clientDataList
.Append( (wxObject
*) clientData
);
204 m_clientObjectList
.Append( (wxObject
*)NULL
);
206 AppendCommon( item
);
209 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
211 m_clientDataList
.Append( (wxObject
*) NULL
);
212 m_clientObjectList
.Append( (wxObject
*) clientData
);
214 AppendCommon( item
);
217 void wxComboBox::SetClientData( int n
, void* clientData
)
219 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
221 wxNode
*node
= m_clientDataList
.Nth( n
);
224 node
->SetData( (wxObject
*) clientData
);
227 void* wxComboBox::GetClientData( int n
)
229 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
231 wxNode
*node
= m_clientDataList
.Nth( n
);
232 if (!node
) return NULL
;
237 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
239 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
241 wxNode
*node
= m_clientObjectList
.Nth( n
);
244 wxClientData
*cd
= (wxClientData
*) node
->Data();
247 node
->SetData( (wxObject
*) clientData
);
250 wxClientData
* wxComboBox::GetClientObject( int n
)
252 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
254 wxNode
*node
= m_clientDataList
.Nth( n
);
255 if (!node
) return (wxClientData
*) NULL
;
257 return (wxClientData
*) node
->Data();
260 void wxComboBox::Clear()
262 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
264 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
265 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
267 wxNode
*node
= m_clientObjectList
.First();
270 wxClientData
*cd
= (wxClientData
*)node
->Data();
274 m_clientObjectList
.Clear();
276 m_clientDataList
.Clear();
279 void wxComboBox::Delete( int n
)
281 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
283 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
285 GList
*child
= g_list_nth( listbox
->children
, n
);
289 wxFAIL_MSG(_T("wrong index"));
293 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
294 gtk_list_remove_items( listbox
, list
);
297 wxNode
*node
= m_clientObjectList
.Nth( n
);
300 wxClientData
*cd
= (wxClientData
*)node
->Data();
302 m_clientObjectList
.DeleteNode( node
);
305 node
= m_clientDataList
.Nth( n
);
308 m_clientDataList
.DeleteNode( node
);
312 int wxComboBox::FindString( const wxString
&item
)
314 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
316 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
318 GList
*child
= GTK_LIST(list
)->children
;
322 GtkBin
*bin
= GTK_BIN( child
->data
);
323 GtkLabel
*label
= GTK_LABEL( bin
->child
);
324 if (item
== label
->label
)
333 int wxComboBox::GetSelection() const
335 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
337 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
339 GList
*selection
= GTK_LIST(list
)->selection
;
342 GList
*child
= GTK_LIST(list
)->children
;
346 if (child
->data
== selection
->data
) return count
;
352 wxFAIL_MSG( _T("wxComboBox: no selection") );
357 wxString
wxComboBox::GetString( int n
) const
359 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
361 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
364 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
367 GtkBin
*bin
= GTK_BIN( child
->data
);
368 GtkLabel
*label
= GTK_LABEL( bin
->child
);
373 wxFAIL_MSG( _T("wxComboBox: wrong index") );
379 wxString
wxComboBox::GetStringSelection() const
381 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
383 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
385 GList
*selection
= GTK_LIST(list
)->selection
;
388 GtkBin
*bin
= GTK_BIN( selection
->data
);
389 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
393 wxFAIL_MSG( _T("wxComboBox: no selection") );
398 int wxComboBox::Number() const
400 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
402 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
404 GList
*child
= GTK_LIST(list
)->children
;
406 while (child
) { count
++; child
= child
->next
; }
410 void wxComboBox::SetSelection( int n
)
412 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
414 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
415 gtk_list_select_item( GTK_LIST(list
), n
);
418 void wxComboBox::SetStringSelection( const wxString
&string
)
420 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
422 int res
= FindString( string
);
423 if (res
== -1) return;
427 wxString
wxComboBox::GetValue() const
429 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
430 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
434 void wxComboBox::SetValue( const wxString
& value
)
436 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
438 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
439 wxString tmp
= _T("");
440 if (!value
.IsNull()) tmp
= value
;
441 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
444 void wxComboBox::Copy()
446 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
448 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 #if (GTK_MINOR_VERSION > 0)
450 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
452 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
456 void wxComboBox::Cut()
458 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
460 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
461 #if (GTK_MINOR_VERSION > 0)
462 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
464 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
468 void wxComboBox::Paste()
470 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
472 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
473 #if (GTK_MINOR_VERSION > 0)
474 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
476 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
480 void wxComboBox::SetInsertionPoint( long pos
)
482 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
484 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
485 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
488 void wxComboBox::SetInsertionPointEnd()
490 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
492 SetInsertionPoint( -1 );
495 long wxComboBox::GetInsertionPoint() const
497 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
498 return (long) GTK_EDITABLE(entry
)->current_pos
;
501 long wxComboBox::GetLastPosition() const
503 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
504 int pos
= GTK_ENTRY(entry
)->text_length
;
508 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
510 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
511 // FIXME: not quite sure how to do this method right in multibyte mode
513 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
514 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
515 if (value
.IsNull()) return;
517 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
520 void wxComboBox::Remove(long from
, long to
)
522 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
524 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
525 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
528 void wxComboBox::SetSelection( long from
, long to
)
530 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
531 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
534 void wxComboBox::SetEditable( bool editable
)
536 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
537 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
540 void wxComboBox::OnChar( wxKeyEvent
&event
)
542 if ( event
.KeyCode() == WXK_RETURN
)
544 wxString value
= GetValue();
548 // make Enter generate "selected" event if there is only one item
549 // in the combobox - without it, it's impossible to select it at
551 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
553 event
.SetString( value
);
554 event
.SetEventObject( this );
555 GetEventHandler()->ProcessEvent( event
);
559 // add the item to the list if it's not there yet
560 if ( FindString(value
) == wxNOT_FOUND
)
564 // and generate the selected event for it
565 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
566 event
.SetInt( Number() - 1 );
567 event
.SetString( value
);
568 event
.SetEventObject( this );
569 GetEventHandler()->ProcessEvent( event
);
571 //else: do nothing, this will open the listbox
578 void wxComboBox::OnSize( wxSizeEvent
&event
)
580 wxControl::OnSize( event
);
583 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
585 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
586 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
589 void wxComboBox::ApplyWidgetStyle()
593 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
594 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
595 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
597 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
598 GList
*child
= list
->children
;
601 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
603 GtkBin
*bin
= GTK_BIN(child
->data
);
604 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
610 GtkWidget
* wxComboBox::GetConnectWidget()
612 return GTK_COMBO(m_widget
)->entry
;
615 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
617 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
618 (window
== GTK_COMBO(m_widget
)->button
->window
) );