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 bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
32 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
37 if (g_blockEventsOnDrag
)
40 if (combo
->m_alreadySent
)
42 combo
->m_alreadySent
= FALSE
;
46 combo
->m_alreadySent
= TRUE
;
48 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
49 event
.SetInt( combo
->GetSelection() );
50 event
.SetString( combo
->GetStringSelection() );
51 event
.SetEventObject( combo
);
52 combo
->GetEventHandler()->ProcessEvent( event
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
60 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
62 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
63 event
.SetString( combo
->GetValue() );
64 event
.SetEventObject( combo
);
65 combo
->GetEventHandler()->ProcessEvent( event
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
74 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
75 EVT_SIZE(wxComboBox::OnSize
)
76 EVT_CHAR(wxComboBox::OnChar
)
79 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
80 const wxPoint
& pos
, const wxSize
& size
,
81 int n
, const wxString choices
[],
82 long style
, const wxValidator
& validator
,
83 const wxString
& name
)
85 m_alreadySent
= FALSE
;
87 m_acceptsFocus
= TRUE
;
89 PreCreation( parent
, id
, pos
, size
, style
, name
);
91 SetValidator( validator
);
93 m_widget
= gtk_combo_new();
95 // make it more useable
96 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
98 wxSize newSize
= size
;
103 SetSize( newSize
.x
, newSize
.y
);
105 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
107 for (int i
= 0; i
< n
; i
++)
109 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
111 m_clientDataList
.Append( (wxObject
*)NULL
);
112 m_clientObjectList
.Append( (wxObject
*)NULL
);
114 gtk_container_add( GTK_CONTAINER(list
), list_item
);
116 gtk_widget_realize( list_item
);
117 gtk_widget_realize( GTK_BIN(list_item
)->child
);
119 gtk_widget_show( list_item
);
121 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
122 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
125 m_parent
->AddChild( this );
127 (m_parent
->m_insertCallback
)( m_parent
, this );
131 ConnectWidget( GTK_COMBO(m_widget
)->button
);
133 if (!value
.IsNull()) SetValue( value
);
135 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
136 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
137 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
139 if (style
& wxCB_READONLY
)
140 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
142 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
143 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
145 SetBackgroundColour( parent
->GetBackgroundColour() );
146 SetForegroundColour( parent
->GetForegroundColour() );
147 SetFont( parent
->GetFont() );
154 wxComboBox::~wxComboBox()
156 wxNode
*node
= m_clientDataList
.First();
159 wxClientData
*cd
= (wxClientData
*)node
->Data();
163 m_clientDataList
.Clear();
166 void wxComboBox::AppendCommon( const wxString
&item
)
168 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
170 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
172 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
174 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
175 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
177 gtk_container_add( GTK_CONTAINER(list
), list_item
);
179 if (m_widgetStyle
) ApplyWidgetStyle();
181 gtk_widget_show( list_item
);
184 void wxComboBox::Append( const wxString
&item
)
186 m_clientDataList
.Append( (wxObject
*) NULL
);
187 m_clientObjectList
.Append( (wxObject
*) NULL
);
189 AppendCommon( item
);
192 void wxComboBox::Append( const wxString
&item
, void *clientData
)
194 m_clientDataList
.Append( (wxObject
*) clientData
);
195 m_clientObjectList
.Append( (wxObject
*)NULL
);
197 AppendCommon( item
);
200 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
202 m_clientDataList
.Append( (wxObject
*) NULL
);
203 m_clientObjectList
.Append( (wxObject
*) clientData
);
205 AppendCommon( item
);
208 void wxComboBox::SetClientData( int n
, void* clientData
)
210 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
212 wxNode
*node
= m_clientDataList
.Nth( n
);
215 node
->SetData( (wxObject
*) clientData
);
218 void* wxComboBox::GetClientData( int n
)
220 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
222 wxNode
*node
= m_clientDataList
.Nth( n
);
223 if (!node
) return NULL
;
228 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
230 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
232 wxNode
*node
= m_clientObjectList
.Nth( n
);
235 wxClientData
*cd
= (wxClientData
*) node
->Data();
238 node
->SetData( (wxObject
*) clientData
);
241 wxClientData
* wxComboBox::GetClientObject( int n
)
243 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
245 wxNode
*node
= m_clientDataList
.Nth( n
);
246 if (!node
) return (wxClientData
*) NULL
;
248 return (wxClientData
*) node
->Data();
251 void wxComboBox::Clear()
253 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
255 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
256 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
258 wxNode
*node
= m_clientObjectList
.First();
261 wxClientData
*cd
= (wxClientData
*)node
->Data();
265 m_clientObjectList
.Clear();
267 m_clientDataList
.Clear();
270 void wxComboBox::Delete( int n
)
272 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
274 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
276 GList
*child
= g_list_nth( listbox
->children
, n
);
280 wxFAIL_MSG(_T("wrong index"));
284 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
285 gtk_list_remove_items( listbox
, list
);
288 wxNode
*node
= m_clientObjectList
.Nth( n
);
291 wxClientData
*cd
= (wxClientData
*)node
->Data();
293 m_clientObjectList
.DeleteNode( node
);
296 node
= m_clientDataList
.Nth( n
);
299 m_clientDataList
.DeleteNode( node
);
303 int wxComboBox::FindString( const wxString
&item
)
305 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
307 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
309 GList
*child
= GTK_LIST(list
)->children
;
313 GtkBin
*bin
= GTK_BIN( child
->data
);
314 GtkLabel
*label
= GTK_LABEL( bin
->child
);
315 if (item
== label
->label
)
324 int wxComboBox::GetSelection() const
326 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
328 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
330 GList
*selection
= GTK_LIST(list
)->selection
;
333 GList
*child
= GTK_LIST(list
)->children
;
337 if (child
->data
== selection
->data
) return count
;
343 wxFAIL_MSG( _T("wxComboBox: no selection") );
348 wxString
wxComboBox::GetString( int n
) const
350 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
352 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
355 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
358 GtkBin
*bin
= GTK_BIN( child
->data
);
359 GtkLabel
*label
= GTK_LABEL( bin
->child
);
364 wxFAIL_MSG( _T("wxComboBox: wrong index") );
370 wxString
wxComboBox::GetStringSelection() const
372 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
374 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
376 GList
*selection
= GTK_LIST(list
)->selection
;
379 GtkBin
*bin
= GTK_BIN( selection
->data
);
380 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
384 wxFAIL_MSG( _T("wxComboBox: no selection") );
389 int wxComboBox::Number() const
391 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
393 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
395 GList
*child
= GTK_LIST(list
)->children
;
397 while (child
) { count
++; child
= child
->next
; }
401 void wxComboBox::SetSelection( int n
)
403 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
405 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
406 gtk_list_select_item( GTK_LIST(list
), n
);
409 void wxComboBox::SetStringSelection( const wxString
&string
)
411 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
413 int res
= FindString( string
);
414 if (res
== -1) return;
418 wxString
wxComboBox::GetValue() const
420 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
421 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
425 void wxComboBox::SetValue( const wxString
& value
)
427 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
429 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
430 wxString tmp
= _T("");
431 if (!value
.IsNull()) tmp
= value
;
432 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
435 void wxComboBox::Copy()
437 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
439 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
440 #if (GTK_MINOR_VERSION > 0)
441 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
443 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
447 void wxComboBox::Cut()
449 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
451 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
452 #if (GTK_MINOR_VERSION > 0)
453 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
455 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
459 void wxComboBox::Paste()
461 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
463 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 #if (GTK_MINOR_VERSION > 0)
465 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
467 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
471 void wxComboBox::SetInsertionPoint( long pos
)
473 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
475 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
476 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
479 void wxComboBox::SetInsertionPointEnd()
481 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
483 SetInsertionPoint( -1 );
486 long wxComboBox::GetInsertionPoint() const
488 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
489 return (long) GTK_EDITABLE(entry
)->current_pos
;
492 long wxComboBox::GetLastPosition() const
494 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
495 int pos
= GTK_ENTRY(entry
)->text_length
;
499 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
501 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
502 // FIXME: not quite sure how to do this method right in multibyte mode
504 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
505 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
506 if (value
.IsNull()) return;
508 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
511 void wxComboBox::Remove(long from
, long to
)
513 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
515 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
516 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
519 void wxComboBox::SetSelection( long from
, long to
)
521 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
522 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
525 void wxComboBox::SetEditable( bool editable
)
527 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
528 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
531 void wxComboBox::OnChar( wxKeyEvent
&event
)
533 if ( event
.KeyCode() == WXK_RETURN
)
535 wxString value
= GetValue();
539 // make Enter generate "selected" event if there is only one item
540 // in the combobox - without it, it's impossible to select it at
542 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
544 event
.SetString( value
);
545 event
.SetEventObject( this );
546 GetEventHandler()->ProcessEvent( event
);
550 // add the item to the list if it's not there yet
551 if ( FindString(value
) == wxNOT_FOUND
)
555 // and generate the selected event for it
556 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
557 event
.SetInt( Number() - 1 );
558 event
.SetString( value
);
559 event
.SetEventObject( this );
560 GetEventHandler()->ProcessEvent( event
);
562 //else: do nothing, this will open the listbox
569 void wxComboBox::OnSize( wxSizeEvent
&event
)
571 wxControl::OnSize( event
);
574 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
576 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
577 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
580 void wxComboBox::ApplyWidgetStyle()
584 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
585 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
586 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
588 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
589 GList
*child
= list
->children
;
592 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
594 GtkBin
*bin
= GTK_BIN(child
->data
);
595 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
601 GtkWidget
* wxComboBox::GetConnectWidget()
603 return GTK_COMBO(m_widget
)->entry
;
606 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
608 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
609 (window
== GTK_COMBO(m_widget
)->button
->window
) );