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"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!combo
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (combo
->m_alreadySent
)
52 combo
->m_alreadySent
= FALSE
;
56 combo
->m_alreadySent
= TRUE
;
58 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
59 event
.SetInt( combo
->GetSelection() );
60 event
.SetString( combo
->GetStringSelection() );
61 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
71 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
73 if (g_isIdle
) wxapp_install_idle_handler();
75 if (!combo
->m_hasVMT
) return;
77 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
78 event
.SetString( combo
->GetValue() );
79 event
.SetEventObject( combo
);
80 combo
->GetEventHandler()->ProcessEvent( event
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
89 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
90 EVT_SIZE(wxComboBox::OnSize
)
91 EVT_CHAR(wxComboBox::OnChar
)
94 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 int n
, const wxString choices
[],
97 long style
, const wxValidator
& validator
,
98 const wxString
& name
)
100 m_alreadySent
= FALSE
;
102 m_acceptsFocus
= TRUE
;
104 if (!PreCreation( parent
, pos
, size
) ||
105 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
107 wxFAIL_MSG( wxT("wxComboBox creation failed") );
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
, wxT("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
, wxT("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
, wxT("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
, wxT("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
, wxT("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
, wxT("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
, wxT("invalid combobox") );
295 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
297 GList
*child
= g_list_nth( listbox
->children
, n
);
301 wxFAIL_MSG(wxT("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, wxT("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, wxT("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
;
367 wxString
wxComboBox::GetString( int n
) const
369 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("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( wxT("wxComboBox: wrong index") );
389 wxString
wxComboBox::GetStringSelection() const
391 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("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( wxT("wxComboBox: no selection") );
408 int wxComboBox::Number() const
410 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("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
, wxT("invalid combobox") );
426 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
427 gtk_list_select_item( GTK_LIST(list
), n
);
432 void wxComboBox::SetStringSelection( const wxString
&string
)
434 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
436 int res
= FindString( string
);
437 if (res
== -1) return;
441 wxString
wxComboBox::GetValue() const
443 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
444 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
448 void wxComboBox::SetValue( const wxString
& value
)
450 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
452 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
453 wxString tmp
= wxT("");
454 if (!value
.IsNull()) tmp
= value
;
455 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
458 void wxComboBox::Copy()
460 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
462 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
463 #if (GTK_MINOR_VERSION > 0)
464 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
466 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
470 void wxComboBox::Cut()
472 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
474 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
475 #if (GTK_MINOR_VERSION > 0)
476 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
478 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
482 void wxComboBox::Paste()
484 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
486 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
487 #if (GTK_MINOR_VERSION > 0)
488 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
490 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
494 void wxComboBox::SetInsertionPoint( long pos
)
496 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
498 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
499 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
502 void wxComboBox::SetInsertionPointEnd()
504 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
506 SetInsertionPoint( -1 );
509 long wxComboBox::GetInsertionPoint() const
511 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
512 return (long) GTK_EDITABLE(entry
)->current_pos
;
515 long wxComboBox::GetLastPosition() const
517 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
518 int pos
= GTK_ENTRY(entry
)->text_length
;
522 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
524 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
525 // FIXME: not quite sure how to do this method right in multibyte mode
527 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
528 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
529 if (value
.IsNull()) return;
531 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
534 void wxComboBox::Remove(long from
, long to
)
536 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
538 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
539 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
542 void wxComboBox::SetSelection( long from
, long to
)
544 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
545 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
548 void wxComboBox::SetEditable( bool editable
)
550 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
551 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
554 void wxComboBox::OnChar( wxKeyEvent
&event
)
556 if ( event
.KeyCode() == WXK_RETURN
)
558 wxString value
= GetValue();
562 // make Enter generate "selected" event if there is only one item
563 // in the combobox - without it, it's impossible to select it at
565 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
567 event
.SetString( value
);
568 event
.SetEventObject( this );
569 GetEventHandler()->ProcessEvent( event
);
573 // add the item to the list if it's not there yet
574 if ( FindString(value
) == wxNOT_FOUND
)
578 // and generate the selected event for it
579 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
580 event
.SetInt( Number() - 1 );
581 event
.SetString( value
);
582 event
.SetEventObject( this );
583 GetEventHandler()->ProcessEvent( event
);
585 //else: do nothing, this will open the listbox
592 void wxComboBox::DisableEvents()
594 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
595 GList
*child
= list
->children
;
598 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
599 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
605 void wxComboBox::EnableEvents()
607 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
608 GList
*child
= list
->children
;
611 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
612 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
618 void wxComboBox::OnSize( wxSizeEvent
&event
)
625 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
627 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
628 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
631 void wxComboBox::ApplyWidgetStyle()
635 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
636 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
637 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
639 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
640 GList
*child
= list
->children
;
643 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
645 GtkBin
*bin
= GTK_BIN(child
->data
);
646 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
652 GtkWidget
* wxComboBox::GetConnectWidget()
654 return GTK_COMBO(m_widget
)->entry
;
657 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
659 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
660 (window
== GTK_COMBO(m_widget
)->button
->window
) );