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_clientDataList
.First();
172 wxClientData
*cd
= (wxClientData
*)node
->Data();
176 m_clientDataList
.Clear();
179 void wxComboBox::AppendCommon( const wxString
&item
)
181 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
183 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
185 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
187 gtk_container_add( GTK_CONTAINER(list
), list_item
);
189 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
190 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
192 if (GTK_WIDGET_REALIZED(m_widget
))
194 gtk_widget_realize( list_item
);
195 gtk_widget_realize( GTK_BIN(list_item
)->child
);
197 if (m_widgetStyle
) ApplyWidgetStyle();
200 gtk_widget_show( list_item
);
203 void wxComboBox::Append( const wxString
&item
)
205 m_clientDataList
.Append( (wxObject
*) NULL
);
206 m_clientObjectList
.Append( (wxObject
*) NULL
);
208 AppendCommon( item
);
211 void wxComboBox::Append( const wxString
&item
, void *clientData
)
213 m_clientDataList
.Append( (wxObject
*) clientData
);
214 m_clientObjectList
.Append( (wxObject
*)NULL
);
216 AppendCommon( item
);
219 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
221 m_clientDataList
.Append( (wxObject
*) NULL
);
222 m_clientObjectList
.Append( (wxObject
*) clientData
);
224 AppendCommon( item
);
227 void wxComboBox::SetClientData( int n
, void* clientData
)
229 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
231 wxNode
*node
= m_clientDataList
.Nth( n
);
234 node
->SetData( (wxObject
*) clientData
);
237 void* wxComboBox::GetClientData( int n
)
239 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
241 wxNode
*node
= m_clientDataList
.Nth( n
);
242 if (!node
) return NULL
;
247 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
249 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
251 wxNode
*node
= m_clientObjectList
.Nth( n
);
254 wxClientData
*cd
= (wxClientData
*) node
->Data();
257 node
->SetData( (wxObject
*) clientData
);
260 wxClientData
* wxComboBox::GetClientObject( int n
)
262 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
264 wxNode
*node
= m_clientDataList
.Nth( n
);
265 if (!node
) return (wxClientData
*) NULL
;
267 return (wxClientData
*) node
->Data();
270 void wxComboBox::Clear()
272 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
274 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
275 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
277 wxNode
*node
= m_clientObjectList
.First();
280 wxClientData
*cd
= (wxClientData
*)node
->Data();
284 m_clientObjectList
.Clear();
286 m_clientDataList
.Clear();
289 void wxComboBox::Delete( int n
)
291 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
293 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
295 GList
*child
= g_list_nth( listbox
->children
, n
);
299 wxFAIL_MSG(_T("wrong index"));
303 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
304 gtk_list_remove_items( listbox
, list
);
307 wxNode
*node
= m_clientObjectList
.Nth( n
);
310 wxClientData
*cd
= (wxClientData
*)node
->Data();
312 m_clientObjectList
.DeleteNode( node
);
315 node
= m_clientDataList
.Nth( n
);
318 m_clientDataList
.DeleteNode( node
);
322 int wxComboBox::FindString( const wxString
&item
)
324 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
326 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
328 GList
*child
= GTK_LIST(list
)->children
;
332 GtkBin
*bin
= GTK_BIN( child
->data
);
333 GtkLabel
*label
= GTK_LABEL( bin
->child
);
334 if (item
== wxString(label
->label
,*wxConvCurrent
))
343 int wxComboBox::GetSelection() const
345 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
347 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
349 GList
*selection
= GTK_LIST(list
)->selection
;
352 GList
*child
= GTK_LIST(list
)->children
;
356 if (child
->data
== selection
->data
) return count
;
362 wxFAIL_MSG( _T("wxComboBox: no selection") );
367 wxString
wxComboBox::GetString( int n
) const
369 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
371 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
374 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
377 GtkBin
*bin
= GTK_BIN( child
->data
);
378 GtkLabel
*label
= GTK_LABEL( bin
->child
);
379 str
= wxString(label
->label
,*wxConvCurrent
);
383 wxFAIL_MSG( _T("wxComboBox: wrong index") );
389 wxString
wxComboBox::GetStringSelection() const
391 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
393 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
395 GList
*selection
= GTK_LIST(list
)->selection
;
398 GtkBin
*bin
= GTK_BIN( selection
->data
);
399 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
403 wxFAIL_MSG( _T("wxComboBox: no selection") );
408 int wxComboBox::Number() const
410 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
412 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
414 GList
*child
= GTK_LIST(list
)->children
;
416 while (child
) { count
++; child
= child
->next
; }
420 void wxComboBox::SetSelection( int n
)
422 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
424 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
425 gtk_list_select_item( GTK_LIST(list
), n
);
428 void wxComboBox::SetStringSelection( const wxString
&string
)
430 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
432 int res
= FindString( string
);
433 if (res
== -1) return;
437 wxString
wxComboBox::GetValue() const
439 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
440 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
444 void wxComboBox::SetValue( const wxString
& value
)
446 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
448 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 wxString tmp
= _T("");
450 if (!value
.IsNull()) tmp
= value
;
451 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
454 void wxComboBox::Copy()
456 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
458 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
459 #if (GTK_MINOR_VERSION > 0)
460 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
462 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
466 void wxComboBox::Cut()
468 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
470 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
471 #if (GTK_MINOR_VERSION > 0)
472 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
474 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
478 void wxComboBox::Paste()
480 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
482 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
483 #if (GTK_MINOR_VERSION > 0)
484 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
486 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
490 void wxComboBox::SetInsertionPoint( long pos
)
492 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
494 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
495 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
498 void wxComboBox::SetInsertionPointEnd()
500 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
502 SetInsertionPoint( -1 );
505 long wxComboBox::GetInsertionPoint() const
507 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
508 return (long) GTK_EDITABLE(entry
)->current_pos
;
511 long wxComboBox::GetLastPosition() const
513 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
514 int pos
= GTK_ENTRY(entry
)->text_length
;
518 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
520 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
521 // FIXME: not quite sure how to do this method right in multibyte mode
523 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
524 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
525 if (value
.IsNull()) return;
527 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
530 void wxComboBox::Remove(long from
, long to
)
532 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
534 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
535 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
538 void wxComboBox::SetSelection( long from
, long to
)
540 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
541 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
544 void wxComboBox::SetEditable( bool editable
)
546 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
547 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
550 void wxComboBox::OnChar( wxKeyEvent
&event
)
552 if ( event
.KeyCode() == WXK_RETURN
)
554 wxString value
= GetValue();
558 // make Enter generate "selected" event if there is only one item
559 // in the combobox - without it, it's impossible to select it at
561 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
563 event
.SetString( value
);
564 event
.SetEventObject( this );
565 GetEventHandler()->ProcessEvent( event
);
569 // add the item to the list if it's not there yet
570 if ( FindString(value
) == wxNOT_FOUND
)
574 // and generate the selected event for it
575 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
576 event
.SetInt( Number() - 1 );
577 event
.SetString( value
);
578 event
.SetEventObject( this );
579 GetEventHandler()->ProcessEvent( event
);
581 //else: do nothing, this will open the listbox
588 void wxComboBox::OnSize( wxSizeEvent
&event
)
595 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
597 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
598 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
601 void wxComboBox::ApplyWidgetStyle()
605 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
606 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
607 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
609 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
610 GList
*child
= list
->children
;
613 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
615 GtkBin
*bin
= GTK_BIN(child
->data
);
616 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
622 GtkWidget
* wxComboBox::GetConnectWidget()
624 return GTK_COMBO(m_widget
)->entry
;
627 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
629 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
630 (window
== GTK_COMBO(m_widget
)->button
->window
) );