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
] );
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 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
140 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
142 SetBackgroundColour( parent
->GetBackgroundColour() );
143 SetForegroundColour( parent
->GetForegroundColour() );
144 SetFont( parent
->GetFont() );
151 wxComboBox::~wxComboBox()
153 wxNode
*node
= m_clientDataList
.First();
156 wxClientData
*cd
= (wxClientData
*)node
->Data();
160 m_clientDataList
.Clear();
163 void wxComboBox::AppendCommon( const wxString
&item
)
165 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
167 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
169 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
171 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
172 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
174 gtk_container_add( GTK_CONTAINER(list
), list_item
);
176 if (m_widgetStyle
) ApplyWidgetStyle();
178 gtk_widget_show( list_item
);
181 void wxComboBox::Append( const wxString
&item
)
183 m_clientDataList
.Append( (wxObject
*) NULL
);
184 m_clientObjectList
.Append( (wxObject
*) NULL
);
186 AppendCommon( item
);
189 void wxComboBox::Append( const wxString
&item
, void *clientData
)
191 m_clientDataList
.Append( (wxObject
*) clientData
);
192 m_clientObjectList
.Append( (wxObject
*)NULL
);
194 AppendCommon( item
);
197 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
199 m_clientDataList
.Append( (wxObject
*) NULL
);
200 m_clientObjectList
.Append( (wxObject
*) clientData
);
202 AppendCommon( item
);
205 void wxComboBox::SetClientData( int n
, void* clientData
)
207 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
209 wxNode
*node
= m_clientDataList
.Nth( n
);
212 node
->SetData( (wxObject
*) clientData
);
215 void* wxComboBox::GetClientData( int n
)
217 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
219 wxNode
*node
= m_clientDataList
.Nth( n
);
220 if (!node
) return NULL
;
225 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
227 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
229 wxNode
*node
= m_clientObjectList
.Nth( n
);
232 wxClientData
*cd
= (wxClientData
*) node
->Data();
235 node
->SetData( (wxObject
*) clientData
);
238 wxClientData
* wxComboBox::GetClientObject( int n
)
240 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
242 wxNode
*node
= m_clientDataList
.Nth( n
);
243 if (!node
) return (wxClientData
*) NULL
;
245 return (wxClientData
*) node
->Data();
248 void wxComboBox::Clear()
250 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
252 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
253 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
255 wxNode
*node
= m_clientObjectList
.First();
258 wxClientData
*cd
= (wxClientData
*)node
->Data();
262 m_clientObjectList
.Clear();
264 m_clientDataList
.Clear();
267 void wxComboBox::Delete( int n
)
269 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
271 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
273 GList
*child
= g_list_nth( listbox
->children
, n
);
277 wxFAIL_MSG("wrong index");
281 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
282 gtk_list_remove_items( listbox
, list
);
285 wxNode
*node
= m_clientObjectList
.Nth( n
);
288 wxClientData
*cd
= (wxClientData
*)node
->Data();
290 m_clientObjectList
.DeleteNode( node
);
293 node
= m_clientDataList
.Nth( n
);
296 m_clientDataList
.DeleteNode( node
);
300 int wxComboBox::FindString( const wxString
&item
)
302 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
304 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
306 GList
*child
= GTK_LIST(list
)->children
;
310 GtkBin
*bin
= GTK_BIN( child
->data
);
311 GtkLabel
*label
= GTK_LABEL( bin
->child
);
312 if (item
== label
->label
)
321 int wxComboBox::GetSelection() const
323 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
325 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
327 GList
*selection
= GTK_LIST(list
)->selection
;
330 GList
*child
= GTK_LIST(list
)->children
;
334 if (child
->data
== selection
->data
) return count
;
340 wxFAIL_MSG( "wxComboBox: no selection" );
345 wxString
wxComboBox::GetString( int n
) const
347 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
349 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
352 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
355 GtkBin
*bin
= GTK_BIN( child
->data
);
356 GtkLabel
*label
= GTK_LABEL( bin
->child
);
361 wxFAIL_MSG( "wxComboBox: wrong index" );
367 wxString
wxComboBox::GetStringSelection() const
369 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
371 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
373 GList
*selection
= GTK_LIST(list
)->selection
;
376 GtkBin
*bin
= GTK_BIN( selection
->data
);
377 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
381 wxFAIL_MSG( "wxComboBox: no selection" );
386 int wxComboBox::Number() const
388 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
390 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
392 GList
*child
= GTK_LIST(list
)->children
;
394 while (child
) { count
++; child
= child
->next
; }
398 void wxComboBox::SetSelection( int n
)
400 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
402 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
403 gtk_list_select_item( GTK_LIST(list
), n
);
406 void wxComboBox::SetStringSelection( const wxString
&string
)
408 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
410 int res
= FindString( string
);
411 if (res
== -1) return;
415 wxString
wxComboBox::GetValue() const
417 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
418 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
422 void wxComboBox::SetValue( const wxString
& value
)
424 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
426 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
428 if (!value
.IsNull()) tmp
= value
;
429 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
432 void wxComboBox::Copy()
434 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
436 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
437 #if (GTK_MINOR_VERSION > 0)
438 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
440 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
444 void wxComboBox::Cut()
446 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
448 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 #if (GTK_MINOR_VERSION > 0)
450 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
452 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
456 void wxComboBox::Paste()
458 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
460 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
461 #if (GTK_MINOR_VERSION > 0)
462 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
464 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
468 void wxComboBox::SetInsertionPoint( long pos
)
470 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
472 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
473 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
476 void wxComboBox::SetInsertionPointEnd()
478 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
480 SetInsertionPoint( -1 );
483 long wxComboBox::GetInsertionPoint() const
485 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
486 return (long) GTK_EDITABLE(entry
)->current_pos
;
489 long wxComboBox::GetLastPosition() const
491 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
492 int pos
= GTK_ENTRY(entry
)->text_length
;
496 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
498 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
500 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
501 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
502 if (value
.IsNull()) return;
504 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
507 void wxComboBox::Remove(long from
, long to
)
509 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
511 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
512 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
515 void wxComboBox::SetSelection( long from
, long to
)
517 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
518 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
521 void wxComboBox::SetEditable( bool editable
)
523 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
524 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
527 void wxComboBox::OnChar( wxKeyEvent
&event
)
529 if ( event
.KeyCode() == WXK_RETURN
)
531 wxString value
= GetValue();
535 // make Enter generate "selected" event if there is only one item
536 // in the combobox - without it, it's impossible to select it at
538 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
540 event
.SetString( value
);
541 event
.SetEventObject( this );
542 GetEventHandler()->ProcessEvent( event
);
546 // add the item to the list if it's not there yet
547 if ( FindString(value
) == wxNOT_FOUND
)
551 // and generate the selected event for it
552 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
553 event
.SetInt( Number() - 1 );
554 event
.SetString( value
);
555 event
.SetEventObject( this );
556 GetEventHandler()->ProcessEvent( event
);
558 //else: do nothing, this will open the listbox
565 void wxComboBox::OnSize( wxSizeEvent
&event
)
567 wxControl::OnSize( event
);
570 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
572 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
573 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
576 void wxComboBox::ApplyWidgetStyle()
580 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
581 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
582 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
584 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
585 GList
*child
= list
->children
;
588 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
590 GtkBin
*bin
= GTK_BIN(child
->data
);
591 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
597 GtkWidget
* wxComboBox::GetConnectWidget()
599 return GTK_COMBO(m_widget
)->entry
;
602 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
604 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
605 (window
== GTK_COMBO(m_widget
)->button
->window
) );