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 //-----------------------------------------------------------------------------
32 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
34 if (!combo
->HasVMT()) return;
35 if (g_blockEventsOnDrag
) return;
37 if (combo
->m_alreadySent
)
39 combo
->m_alreadySent
= FALSE
;
43 combo
->m_alreadySent
= TRUE
;
45 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
46 event
.SetInt( combo
->GetSelection() );
47 wxString
tmp( combo
->GetStringSelection() );
48 event
.SetString( WXSTRINGCAST(tmp
) );
49 event
.SetEventObject(combo
);
50 combo
->GetEventHandler()->ProcessEvent(event
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
58 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
60 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->m_windowId
);
61 event
.SetString( copystring(combo
->GetValue()) );
62 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
64 delete[] event
.GetString();
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
73 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
74 EVT_SIZE(wxComboBox::OnSize
)
77 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
78 const wxPoint
& pos
, const wxSize
& size
,
79 int n
, const wxString choices
[],
80 long style
, const wxValidator
& validator
,
81 const wxString
& name
)
83 m_alreadySent
= FALSE
;
85 m_acceptsFocus
= TRUE
;
87 PreCreation( parent
, id
, pos
, size
, style
, name
);
89 SetValidator( validator
);
91 m_widget
= gtk_combo_new();
93 wxSize newSize
= size
;
94 if (newSize
.x
== -1) newSize
.x
= 100;
95 if (newSize
.y
== -1) newSize
.y
= 26;
96 SetSize( newSize
.x
, newSize
.y
);
98 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
100 for (int i
= 0; i
< n
; i
++)
102 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
104 m_clientDataList
.Append( (wxObject
*)NULL
);
105 m_clientObjectList
.Append( (wxObject
*)NULL
);
107 gtk_container_add( GTK_CONTAINER(list
), list_item
);
109 gtk_widget_realize( list_item
);
110 gtk_widget_realize( GTK_BIN(list_item
)->child
);
112 gtk_widget_show( list_item
);
114 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
115 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
118 m_parent
->AddChild( this );
120 (m_parent
->m_insertCallback
)( m_parent
, this );
124 ConnectWidget( GTK_COMBO(m_widget
)->button
);
126 if (!value
.IsNull()) SetValue( value
);
128 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
129 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
130 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
132 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
133 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
135 SetBackgroundColour( parent
->GetBackgroundColour() );
136 SetForegroundColour( parent
->GetForegroundColour() );
143 wxComboBox::~wxComboBox()
145 wxNode
*node
= m_clientDataList
.First();
148 wxClientData
*cd
= (wxClientData
*)node
->Data();
152 m_clientDataList
.Clear();
155 void wxComboBox::AppendCommon( const wxString
&item
)
157 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
159 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
161 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
163 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
164 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
166 gtk_container_add( GTK_CONTAINER(list
), list_item
);
168 if (m_widgetStyle
) ApplyWidgetStyle();
170 gtk_widget_show( list_item
);
173 void wxComboBox::Append( const wxString
&item
)
175 m_clientDataList
.Append( (wxObject
*) NULL
);
176 m_clientObjectList
.Append( (wxObject
*) NULL
);
178 AppendCommon( item
);
181 void wxComboBox::Append( const wxString
&item
, void *clientData
)
183 m_clientDataList
.Append( (wxObject
*) clientData
);
184 m_clientObjectList
.Append( (wxObject
*)NULL
);
186 AppendCommon( item
);
189 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
191 m_clientDataList
.Append( (wxObject
*) NULL
);
192 m_clientObjectList
.Append( (wxObject
*) clientData
);
194 AppendCommon( item
);
197 void wxComboBox::SetClientData( int n
, void* clientData
)
199 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
201 wxNode
*node
= m_clientDataList
.Nth( n
);
204 node
->SetData( (wxObject
*) clientData
);
207 void* wxComboBox::GetClientData( int n
)
209 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
211 wxNode
*node
= m_clientDataList
.Nth( n
);
212 if (!node
) return NULL
;
217 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
219 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
221 wxNode
*node
= m_clientObjectList
.Nth( n
);
224 wxClientData
*cd
= (wxClientData
*) node
->Data();
227 node
->SetData( (wxObject
*) clientData
);
230 wxClientData
* wxComboBox::GetClientObject( int n
)
232 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
234 wxNode
*node
= m_clientDataList
.Nth( n
);
235 if (!node
) return (wxClientData
*) NULL
;
237 return (wxClientData
*) node
->Data();
240 void wxComboBox::Clear()
242 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
244 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
245 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
247 wxNode
*node
= m_clientObjectList
.First();
250 wxClientData
*cd
= (wxClientData
*)node
->Data();
254 m_clientObjectList
.Clear();
256 m_clientDataList
.Clear();
259 void wxComboBox::Delete( int n
)
261 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
263 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
265 GList
*child
= g_list_nth( listbox
->children
, n
);
269 wxFAIL_MSG("wrong index");
273 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
274 gtk_list_remove_items( listbox
, list
);
277 wxNode
*node
= m_clientObjectList
.Nth( n
);
280 wxClientData
*cd
= (wxClientData
*)node
->Data();
282 m_clientObjectList
.DeleteNode( node
);
285 node
= m_clientDataList
.Nth( n
);
288 m_clientDataList
.DeleteNode( node
);
292 int wxComboBox::FindString( const wxString
&item
)
294 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
296 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
298 GList
*child
= GTK_LIST(list
)->children
;
302 GtkBin
*bin
= GTK_BIN( child
->data
);
303 GtkLabel
*label
= GTK_LABEL( bin
->child
);
304 if (item
== label
->label
) return count
;
309 wxFAIL_MSG( "wxComboBox: string not found" );
314 int wxComboBox::GetSelection() const
316 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
318 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
320 GList
*selection
= GTK_LIST(list
)->selection
;
323 GList
*child
= GTK_LIST(list
)->children
;
327 if (child
->data
== selection
->data
) return count
;
333 wxFAIL_MSG( "wxComboBox: no selection" );
338 wxString
wxComboBox::GetString( int n
) const
340 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
342 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
344 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
347 GtkBin
*bin
= GTK_BIN( child
->data
);
348 GtkLabel
*label
= GTK_LABEL( bin
->child
);
352 wxFAIL_MSG( "wxComboBox: wrong index" );
357 wxString
wxComboBox::GetStringSelection() const
359 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
361 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
363 GList
*selection
= GTK_LIST(list
)->selection
;
366 GtkBin
*bin
= GTK_BIN( selection
->data
);
367 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
371 wxFAIL_MSG( "wxComboBox: no selection" );
376 int wxComboBox::Number() const
378 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
380 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
382 GList
*child
= GTK_LIST(list
)->children
;
384 while (child
) { count
++; child
= child
->next
; }
388 void wxComboBox::SetSelection( int n
)
390 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
392 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
393 gtk_list_select_item( GTK_LIST(list
), n
);
396 void wxComboBox::SetStringSelection( const wxString
&string
)
398 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
400 int res
= FindString( string
);
401 if (res
== -1) return;
405 wxString
wxComboBox::GetValue() const
407 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
408 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
412 void wxComboBox::SetValue( const wxString
& value
)
414 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
416 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
418 if (!value
.IsNull()) tmp
= value
;
419 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
422 void wxComboBox::Copy()
424 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
426 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
427 #if (GTK_MINOR_VERSION == 1)
428 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
430 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
434 void wxComboBox::Cut()
436 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
438 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
439 #if (GTK_MINOR_VERSION == 1)
440 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
442 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
446 void wxComboBox::Paste()
448 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
450 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
451 #if (GTK_MINOR_VERSION == 1)
452 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
454 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
458 void wxComboBox::SetInsertionPoint( long pos
)
460 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
462 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
467 void wxComboBox::SetInsertionPointEnd()
469 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
471 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
472 int pos
= GTK_ENTRY(entry
)->text_length
;
473 SetInsertionPoint( pos
-1 );
476 long wxComboBox::GetInsertionPoint() const
478 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
479 return (long) GTK_EDITABLE(entry
)->current_pos
;
482 long wxComboBox::GetLastPosition() const
484 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
485 int pos
= GTK_ENTRY(entry
)->text_length
;
489 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
491 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
493 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
494 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
495 if (value
.IsNull()) return;
497 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
500 void wxComboBox::Remove(long from
, long to
)
502 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
504 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
505 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
508 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
510 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
513 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
515 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
518 void wxComboBox::OnSize( wxSizeEvent
&event
)
520 wxControl::OnSize( event
);
523 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
525 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
526 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
529 void wxComboBox::ApplyWidgetStyle()
533 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
534 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
535 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
537 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
538 GList
*child
= list
->children
;
541 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
543 GtkBin
*bin
= GTK_BIN(child
->data
);
544 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
550 GtkWidget
* wxComboBox::GetConnectWidget()
552 return GTK_COMBO(m_widget
)->entry
;
555 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
557 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
558 (window
== GTK_COMBO(m_widget
)->button
->window
) );