1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
15 #include "wx/combobox.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
33 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
38 if (g_blockEventsOnDrag
)
41 if (combo
->m_alreadySent
)
43 combo
->m_alreadySent
= FALSE
;
47 combo
->m_alreadySent
= TRUE
;
49 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
50 event
.SetInt( combo
->GetSelection() );
51 event
.SetString( copystring(combo
->GetStringSelection()) );
52 event
.SetEventObject( combo
);
53 combo
->GetEventHandler()->ProcessEvent( event
);
55 delete [] event
.GetString();
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
63 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
65 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
66 event
.SetString( copystring(combo
->GetValue()) );
67 event
.SetEventObject( combo
);
68 combo
->GetEventHandler()->ProcessEvent( event
);
70 delete [] event
.GetString();
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
79 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
80 EVT_SIZE(wxComboBox::OnSize
)
81 EVT_CHAR(wxComboBox::OnChar
)
84 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
85 const wxPoint
& pos
, const wxSize
& size
,
86 int n
, const wxString choices
[],
87 long style
, const wxValidator
& validator
,
88 const wxString
& name
)
90 m_alreadySent
= FALSE
;
92 m_acceptsFocus
= TRUE
;
94 PreCreation( parent
, id
, pos
, size
, style
, name
);
96 SetValidator( validator
);
98 m_widget
= gtk_combo_new();
100 // make it more useable
101 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
103 wxSize newSize
= size
;
108 SetSize( newSize
.x
, newSize
.y
);
110 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
112 for (int i
= 0; i
< n
; i
++)
114 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
116 m_clientDataList
.Append( (wxObject
*)NULL
);
117 m_clientObjectList
.Append( (wxObject
*)NULL
);
119 gtk_container_add( GTK_CONTAINER(list
), list_item
);
121 gtk_widget_realize( list_item
);
122 gtk_widget_realize( GTK_BIN(list_item
)->child
);
124 gtk_widget_show( list_item
);
126 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
127 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
130 m_parent
->AddChild( this );
132 (m_parent
->m_insertCallback
)( m_parent
, this );
136 ConnectWidget( GTK_COMBO(m_widget
)->button
);
138 if (!value
.IsNull()) SetValue( value
);
140 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
141 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
142 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
144 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
145 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
147 SetBackgroundColour( parent
->GetBackgroundColour() );
148 SetForegroundColour( parent
->GetForegroundColour() );
149 SetFont( parent
->GetFont() );
156 wxComboBox::~wxComboBox()
158 wxNode
*node
= m_clientDataList
.First();
161 wxClientData
*cd
= (wxClientData
*)node
->Data();
165 m_clientDataList
.Clear();
168 void wxComboBox::AppendCommon( const wxString
&item
)
170 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
172 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
174 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
176 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
177 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
179 gtk_container_add( GTK_CONTAINER(list
), list_item
);
181 if (m_widgetStyle
) ApplyWidgetStyle();
183 gtk_widget_show( list_item
);
186 void wxComboBox::Append( const wxString
&item
)
188 m_clientDataList
.Append( (wxObject
*) NULL
);
189 m_clientObjectList
.Append( (wxObject
*) NULL
);
191 AppendCommon( item
);
194 void wxComboBox::Append( const wxString
&item
, void *clientData
)
196 m_clientDataList
.Append( (wxObject
*) clientData
);
197 m_clientObjectList
.Append( (wxObject
*)NULL
);
199 AppendCommon( item
);
202 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
204 m_clientDataList
.Append( (wxObject
*) NULL
);
205 m_clientObjectList
.Append( (wxObject
*) clientData
);
207 AppendCommon( item
);
210 void wxComboBox::SetClientData( int n
, void* clientData
)
212 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
214 wxNode
*node
= m_clientDataList
.Nth( n
);
217 node
->SetData( (wxObject
*) clientData
);
220 void* wxComboBox::GetClientData( int n
)
222 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
224 wxNode
*node
= m_clientDataList
.Nth( n
);
225 if (!node
) return NULL
;
230 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
232 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
234 wxNode
*node
= m_clientObjectList
.Nth( n
);
237 wxClientData
*cd
= (wxClientData
*) node
->Data();
240 node
->SetData( (wxObject
*) clientData
);
243 wxClientData
* wxComboBox::GetClientObject( int n
)
245 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
247 wxNode
*node
= m_clientDataList
.Nth( n
);
248 if (!node
) return (wxClientData
*) NULL
;
250 return (wxClientData
*) node
->Data();
253 void wxComboBox::Clear()
255 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
257 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
258 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
260 wxNode
*node
= m_clientObjectList
.First();
263 wxClientData
*cd
= (wxClientData
*)node
->Data();
267 m_clientObjectList
.Clear();
269 m_clientDataList
.Clear();
272 void wxComboBox::Delete( int n
)
274 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
276 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
278 GList
*child
= g_list_nth( listbox
->children
, n
);
282 wxFAIL_MSG("wrong index");
286 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
287 gtk_list_remove_items( listbox
, list
);
290 wxNode
*node
= m_clientObjectList
.Nth( n
);
293 wxClientData
*cd
= (wxClientData
*)node
->Data();
295 m_clientObjectList
.DeleteNode( node
);
298 node
= m_clientDataList
.Nth( n
);
301 m_clientDataList
.DeleteNode( node
);
305 int wxComboBox::FindString( const wxString
&item
)
307 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
309 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
311 GList
*child
= GTK_LIST(list
)->children
;
315 GtkBin
*bin
= GTK_BIN( child
->data
);
316 GtkLabel
*label
= GTK_LABEL( bin
->child
);
317 if (item
== label
->label
)
326 int wxComboBox::GetSelection() const
328 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
330 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
332 GList
*selection
= GTK_LIST(list
)->selection
;
335 GList
*child
= GTK_LIST(list
)->children
;
339 if (child
->data
== selection
->data
) return count
;
345 wxFAIL_MSG( "wxComboBox: no selection" );
350 wxString
wxComboBox::GetString( int n
) const
352 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
354 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
357 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
360 GtkBin
*bin
= GTK_BIN( child
->data
);
361 GtkLabel
*label
= GTK_LABEL( bin
->child
);
366 wxFAIL_MSG( "wxComboBox: wrong index" );
372 wxString
wxComboBox::GetStringSelection() const
374 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
376 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
378 GList
*selection
= GTK_LIST(list
)->selection
;
381 GtkBin
*bin
= GTK_BIN( selection
->data
);
382 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
386 wxFAIL_MSG( "wxComboBox: no selection" );
391 int wxComboBox::Number() const
393 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
395 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
397 GList
*child
= GTK_LIST(list
)->children
;
399 while (child
) { count
++; child
= child
->next
; }
403 void wxComboBox::SetSelection( int n
)
405 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
407 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
408 gtk_list_select_item( GTK_LIST(list
), n
);
411 void wxComboBox::SetStringSelection( const wxString
&string
)
413 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
415 int res
= FindString( string
);
416 if (res
== -1) return;
420 wxString
wxComboBox::GetValue() const
422 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
423 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
427 void wxComboBox::SetValue( const wxString
& value
)
429 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
431 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
433 if (!value
.IsNull()) tmp
= value
;
434 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
437 void wxComboBox::Copy()
439 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
441 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
442 #if (GTK_MINOR_VERSION == 1)
443 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
445 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
449 void wxComboBox::Cut()
451 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
453 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
454 #if (GTK_MINOR_VERSION == 1)
455 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
457 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
461 void wxComboBox::Paste()
463 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
465 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
466 #if (GTK_MINOR_VERSION == 1)
467 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
469 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
473 void wxComboBox::SetInsertionPoint( long pos
)
475 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
477 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
478 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
481 void wxComboBox::SetInsertionPointEnd()
483 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
485 SetInsertionPoint( -1 );
488 long wxComboBox::GetInsertionPoint() const
490 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
491 return (long) GTK_EDITABLE(entry
)->current_pos
;
494 long wxComboBox::GetLastPosition() const
496 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
497 int pos
= GTK_ENTRY(entry
)->text_length
;
501 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
503 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
505 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
506 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
507 if (value
.IsNull()) return;
509 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
512 void wxComboBox::Remove(long from
, long to
)
514 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
516 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
517 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
520 void wxComboBox::SetSelection( long from
, long to
)
522 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
523 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
526 void wxComboBox::SetEditable( bool editable
)
528 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
529 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
532 void wxComboBox::OnChar( wxKeyEvent
&event
)
534 if ( event
.KeyCode() == WXK_RETURN
)
536 wxString value
= GetValue();
540 // make Enter generate "selected" event if there is only one item
541 // in the combobox - without it, it's impossible to select it at
543 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
545 event
.SetString( (char *)value
.c_str() );
546 event
.SetEventObject( this );
547 GetEventHandler()->ProcessEvent( event
);
551 // add the item to the list if it's not there yet
552 if ( FindString(value
) == wxNOT_FOUND
)
556 // and generate the selected event for it
557 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
558 event
.SetInt( Number() - 1 );
559 event
.SetString( (char *)value
.c_str() );
560 event
.SetEventObject( this );
561 GetEventHandler()->ProcessEvent( event
);
563 //else: do nothing, this will open the listbox
570 void wxComboBox::OnSize( wxSizeEvent
&event
)
572 wxControl::OnSize( event
);
575 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
577 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
578 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
581 void wxComboBox::ApplyWidgetStyle()
585 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
586 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
587 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
589 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
590 GList
*child
= list
->children
;
593 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
595 GtkBin
*bin
= GTK_BIN(child
->data
);
596 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
602 GtkWidget
* wxComboBox::GetConnectWidget()
604 return GTK_COMBO(m_widget
)->entry
;
607 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
609 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
610 (window
== GTK_COMBO(m_widget
)->button
->window
) );