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"
18 #include "wx/settings.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!combo
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (combo
->m_alreadySent
)
53 combo
->m_alreadySent
= FALSE
;
57 combo
->m_alreadySent
= TRUE
;
59 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
60 event
.SetInt( combo
->GetSelection() );
61 event
.SetString( combo
->GetStringSelection() );
62 event
.SetEventObject( combo
);
64 combo
->GetEventHandler()->ProcessEvent( event
);
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
72 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
74 if (g_isIdle
) wxapp_install_idle_handler();
76 if (!combo
->m_hasVMT
) return;
78 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
79 event
.SetString( combo
->GetValue() );
80 event
.SetEventObject( combo
);
81 combo
->GetEventHandler()->ProcessEvent( event
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
90 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
91 EVT_SIZE(wxComboBox::OnSize
)
92 EVT_CHAR(wxComboBox::OnChar
)
95 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 int n
, const wxString choices
[],
98 long style
, const wxValidator
& validator
,
99 const wxString
& name
)
101 m_alreadySent
= FALSE
;
103 m_acceptsFocus
= TRUE
;
105 PreCreation( parent
, id
, pos
, size
, style
, name
);
108 SetValidator( validator
);
111 m_widget
= gtk_combo_new();
113 // make it more useable
114 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
116 wxSize newSize
= size
;
121 SetSize( newSize
.x
, newSize
.y
);
123 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
125 for (int i
= 0; i
< n
; i
++)
127 /* don't send first event, which GTK sends aways when
128 inserting the first item */
129 m_alreadySent
= TRUE
;
131 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
133 m_clientDataList
.Append( (wxObject
*)NULL
);
134 m_clientObjectList
.Append( (wxObject
*)NULL
);
136 gtk_container_add( GTK_CONTAINER(list
), list_item
);
138 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
139 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
141 gtk_widget_show( list_item
);
144 m_parent
->DoAddChild( this );
148 ConnectWidget( GTK_COMBO(m_widget
)->button
);
150 if (!value
.IsNull()) SetValue( value
);
152 if (style
& wxCB_READONLY
)
153 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
155 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
156 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
158 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
159 SetForegroundColour( parent
->GetForegroundColour() );
160 SetFont( parent
->GetFont() );
167 wxComboBox::~wxComboBox()
169 wxNode
*node
= m_clientObjectList
.First();
172 wxClientData
*cd
= (wxClientData
*)node
->Data();
176 m_clientObjectList
.Clear();
178 m_clientDataList
.Clear();
181 void wxComboBox::AppendCommon( const wxString
&item
)
183 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
185 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
187 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
189 gtk_container_add( GTK_CONTAINER(list
), list_item
);
191 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
192 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
194 if (GTK_WIDGET_REALIZED(m_widget
))
196 gtk_widget_realize( list_item
);
197 gtk_widget_realize( GTK_BIN(list_item
)->child
);
199 if (m_widgetStyle
) ApplyWidgetStyle();
202 gtk_widget_show( list_item
);
205 void wxComboBox::Append( const wxString
&item
)
207 m_clientDataList
.Append( (wxObject
*) NULL
);
208 m_clientObjectList
.Append( (wxObject
*) NULL
);
210 AppendCommon( item
);
213 void wxComboBox::Append( const wxString
&item
, void *clientData
)
215 m_clientDataList
.Append( (wxObject
*) clientData
);
216 m_clientObjectList
.Append( (wxObject
*)NULL
);
218 AppendCommon( item
);
221 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
223 m_clientDataList
.Append( (wxObject
*) NULL
);
224 m_clientObjectList
.Append( (wxObject
*) clientData
);
226 AppendCommon( item
);
229 void wxComboBox::SetClientData( int n
, void* clientData
)
231 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
233 wxNode
*node
= m_clientDataList
.Nth( n
);
236 node
->SetData( (wxObject
*) clientData
);
239 void* wxComboBox::GetClientData( int n
)
241 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
243 wxNode
*node
= m_clientDataList
.Nth( n
);
244 if (!node
) return NULL
;
249 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
251 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
253 wxNode
*node
= m_clientObjectList
.Nth( n
);
256 wxClientData
*cd
= (wxClientData
*) node
->Data();
259 node
->SetData( (wxObject
*) clientData
);
262 wxClientData
* wxComboBox::GetClientObject( int n
)
264 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
266 wxNode
*node
= m_clientDataList
.Nth( n
);
267 if (!node
) return (wxClientData
*) NULL
;
269 return (wxClientData
*) node
->Data();
272 void wxComboBox::Clear()
274 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
276 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
277 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
279 wxNode
*node
= m_clientObjectList
.First();
282 wxClientData
*cd
= (wxClientData
*)node
->Data();
286 m_clientObjectList
.Clear();
288 m_clientDataList
.Clear();
291 void wxComboBox::Delete( int n
)
293 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
295 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
297 GList
*child
= g_list_nth( listbox
->children
, n
);
301 wxFAIL_MSG(_T("wrong index"));
305 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
306 gtk_list_remove_items( listbox
, list
);
309 wxNode
*node
= m_clientObjectList
.Nth( n
);
312 wxClientData
*cd
= (wxClientData
*)node
->Data();
314 m_clientObjectList
.DeleteNode( node
);
317 node
= m_clientDataList
.Nth( n
);
320 m_clientDataList
.DeleteNode( node
);
324 int wxComboBox::FindString( const wxString
&item
)
326 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
328 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
330 GList
*child
= GTK_LIST(list
)->children
;
334 GtkBin
*bin
= GTK_BIN( child
->data
);
335 GtkLabel
*label
= GTK_LABEL( bin
->child
);
336 if (item
== wxString(label
->label
,*wxConvCurrent
))
345 int wxComboBox::GetSelection() const
347 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
349 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
351 GList
*selection
= GTK_LIST(list
)->selection
;
354 GList
*child
= GTK_LIST(list
)->children
;
358 if (child
->data
== selection
->data
) return count
;
364 wxFAIL_MSG( _T("wxComboBox: no selection") );
369 wxString
wxComboBox::GetString( int n
) const
371 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
373 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
376 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
379 GtkBin
*bin
= GTK_BIN( child
->data
);
380 GtkLabel
*label
= GTK_LABEL( bin
->child
);
381 str
= wxString(label
->label
,*wxConvCurrent
);
385 wxFAIL_MSG( _T("wxComboBox: wrong index") );
391 wxString
wxComboBox::GetStringSelection() const
393 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
395 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
397 GList
*selection
= GTK_LIST(list
)->selection
;
400 GtkBin
*bin
= GTK_BIN( selection
->data
);
401 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
405 wxFAIL_MSG( _T("wxComboBox: no selection") );
410 int wxComboBox::Number() const
412 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
414 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
416 GList
*child
= GTK_LIST(list
)->children
;
418 while (child
) { count
++; child
= child
->next
; }
422 void wxComboBox::SetSelection( int n
)
424 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
426 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
427 gtk_list_select_item( GTK_LIST(list
), n
);
430 void wxComboBox::SetStringSelection( const wxString
&string
)
432 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
434 int res
= FindString( string
);
435 if (res
== -1) return;
439 wxString
wxComboBox::GetValue() const
441 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
442 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
446 void wxComboBox::SetValue( const wxString
& value
)
448 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
450 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
451 wxString tmp
= _T("");
452 if (!value
.IsNull()) tmp
= value
;
453 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
456 void wxComboBox::Copy()
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_copy_clipboard( GTK_EDITABLE(entry
) );
464 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
468 void wxComboBox::Cut()
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_cut_clipboard( GTK_EDITABLE(entry
) );
476 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
480 void wxComboBox::Paste()
482 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
484 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
485 #if (GTK_MINOR_VERSION > 0)
486 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
488 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
492 void wxComboBox::SetInsertionPoint( long pos
)
494 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
496 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
497 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
500 void wxComboBox::SetInsertionPointEnd()
502 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
504 SetInsertionPoint( -1 );
507 long wxComboBox::GetInsertionPoint() const
509 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
510 return (long) GTK_EDITABLE(entry
)->current_pos
;
513 long wxComboBox::GetLastPosition() const
515 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
516 int pos
= GTK_ENTRY(entry
)->text_length
;
520 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
522 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
523 // FIXME: not quite sure how to do this method right in multibyte mode
525 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
526 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
527 if (value
.IsNull()) return;
529 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
532 void wxComboBox::Remove(long from
, long to
)
534 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
536 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
537 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
540 void wxComboBox::SetSelection( long from
, long to
)
542 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
543 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
546 void wxComboBox::SetEditable( bool editable
)
548 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
549 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
552 void wxComboBox::OnChar( wxKeyEvent
&event
)
554 if ( event
.KeyCode() == WXK_RETURN
)
556 wxString value
= GetValue();
560 // make Enter generate "selected" event if there is only one item
561 // in the combobox - without it, it's impossible to select it at
563 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
565 event
.SetString( value
);
566 event
.SetEventObject( this );
567 GetEventHandler()->ProcessEvent( event
);
571 // add the item to the list if it's not there yet
572 if ( FindString(value
) == wxNOT_FOUND
)
576 // and generate the selected event for it
577 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
578 event
.SetInt( Number() - 1 );
579 event
.SetString( value
);
580 event
.SetEventObject( this );
581 GetEventHandler()->ProcessEvent( event
);
583 //else: do nothing, this will open the listbox
590 void wxComboBox::OnSize( wxSizeEvent
&event
)
597 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
599 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
600 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
603 void wxComboBox::ApplyWidgetStyle()
607 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
608 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
609 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
611 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
612 GList
*child
= list
->children
;
615 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
617 GtkBin
*bin
= GTK_BIN(child
->data
);
618 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
624 GtkWidget
* wxComboBox::GetConnectWidget()
626 return GTK_COMBO(m_widget
)->entry
;
629 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
631 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
632 (window
== GTK_COMBO(m_widget
)->button
->window
) );