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"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
29 if (!combo
->HasVMT()) return;
30 if (g_blockEventsOnDrag
) return;
32 if (combo
->m_alreadySent
)
34 combo
->m_alreadySent
= FALSE
;
38 combo
->m_alreadySent
= TRUE
;
40 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
41 event
.SetInt( combo
->GetSelection() );
42 wxString
tmp( combo
->GetStringSelection() );
43 event
.SetString( WXSTRINGCAST(tmp
) );
44 event
.SetEventObject(combo
);
45 combo
->GetEventHandler()->ProcessEvent(event
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
54 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
55 EVT_SIZE(wxComboBox::OnSize
)
58 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int n
, const wxString choices
[],
61 long style
, const wxValidator
& validator
, const wxString
& name
)
63 m_alreadySent
= FALSE
;
66 PreCreation( parent
, id
, pos
, size
, style
, name
);
68 SetValidator( validator
);
70 m_widget
= gtk_combo_new();
72 wxSize newSize
= size
;
73 if (newSize
.x
== -1) newSize
.x
= 100;
74 if (newSize
.y
== -1) newSize
.y
= 26;
75 SetSize( newSize
.x
, newSize
.y
);
77 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
79 for (int i
= 0; i
< n
; i
++)
81 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
83 m_clientData
.Append( (wxObject
*)NULL
);
85 gtk_container_add( GTK_CONTAINER(list
), list_item
);
87 gtk_widget_realize( list_item
);
88 gtk_widget_realize( GTK_BIN(list_item
)->child
);
90 gtk_widget_show( list_item
);
92 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
93 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
98 ConnectWidget( GTK_COMBO(m_widget
)->button
);
100 if (!value
.IsNull()) SetValue( value
);
102 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
103 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
104 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
106 SetBackgroundColour( parent
->GetBackgroundColour() );
107 SetForegroundColour( parent
->GetForegroundColour() );
114 void wxComboBox::Clear(void)
116 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
118 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
119 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
121 m_clientData
.Clear();
124 void wxComboBox::Append( const wxString
&item
)
126 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
128 Append( item
, (char*)NULL
);
131 void wxComboBox::Append( const wxString
&item
, char *clientData
)
133 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
135 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
137 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
139 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
140 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
142 m_clientData
.Append( (wxObject
*)clientData
);
144 gtk_container_add( GTK_CONTAINER(list
), list_item
);
146 if (m_widgetStyle
) ApplyWidgetStyle();
148 gtk_widget_show( list_item
);
151 void wxComboBox::Delete( int n
)
153 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
155 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
157 GList
*child
= g_list_nth( listbox
->children
, n
);
161 wxFAIL_MSG("wrong index");
165 GList
*list
= g_list_append( NULL
, child
->data
);
166 gtk_list_remove_items( listbox
, list
);
169 wxNode
*node
= m_clientData
.Nth( n
);
172 wxFAIL_MSG( "wrong index" );
175 m_clientData
.DeleteNode( node
);
178 int wxComboBox::FindString( const wxString
&item
)
180 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
182 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
184 GList
*child
= GTK_LIST(list
)->children
;
188 GtkBin
*bin
= GTK_BIN( child
->data
);
189 GtkLabel
*label
= GTK_LABEL( bin
->child
);
190 if (item
== label
->label
) return count
;
195 wxFAIL_MSG( "wxComboBox: string not found" );
200 char* wxComboBox::GetClientData( int n
)
202 wxCHECK_MSG( m_widget
!= NULL
, (char*)NULL
, "invalid combobox" );
204 wxNode
*node
= m_clientData
.Nth( n
);
205 if (node
) return (char*)node
->Data();
207 wxFAIL_MSG( "wxComboBox: wrong index" );
209 return (char *) NULL
;
212 void wxComboBox::SetClientData( int n
, char * clientData
)
214 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
216 wxNode
*node
= m_clientData
.Nth( n
);
217 if (node
) node
->SetData( (wxObject
*) clientData
);
219 wxFAIL_MSG( "wxComboBox: wrong index" );
222 int wxComboBox::GetSelection(void) const
224 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
226 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
228 GList
*selection
= GTK_LIST(list
)->selection
;
231 GList
*child
= GTK_LIST(list
)->children
;
235 if (child
->data
== selection
->data
) return count
;
241 wxFAIL_MSG( "wxComboBox: no selection" );
246 wxString
wxComboBox::GetString( int n
) const
248 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
250 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
252 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
255 GtkBin
*bin
= GTK_BIN( child
->data
);
256 GtkLabel
*label
= GTK_LABEL( bin
->child
);
260 wxFAIL_MSG( "wxComboBox: wrong index" );
265 wxString
wxComboBox::GetStringSelection(void) const
267 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
269 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
271 GList
*selection
= GTK_LIST(list
)->selection
;
274 GtkBin
*bin
= GTK_BIN( selection
->data
);
275 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
279 wxFAIL_MSG( "wxComboBox: no selection" );
284 int wxComboBox::Number(void) const
286 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
288 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
290 GList
*child
= GTK_LIST(list
)->children
;
292 while (child
) { count
++; child
= child
->next
; }
296 void wxComboBox::SetSelection( int n
)
298 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
300 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
301 gtk_list_select_item( GTK_LIST(list
), n
);
304 void wxComboBox::SetStringSelection( const wxString
&string
)
306 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
308 int res
= FindString( string
);
309 if (res
== -1) return;
313 wxString
wxComboBox::GetValue(void) const
315 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
316 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
320 void wxComboBox::SetValue( const wxString
& value
)
322 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
324 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
326 if (!value
.IsNull()) tmp
= value
;
327 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
330 void wxComboBox::Copy(void)
332 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
334 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
335 #if (GTK_MINOR_VERSION == 1)
336 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
338 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
342 void wxComboBox::Cut(void)
344 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
346 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
347 #if (GTK_MINOR_VERSION == 1)
348 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
350 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
354 void wxComboBox::Paste(void)
356 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
358 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
359 #if (GTK_MINOR_VERSION == 1)
360 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
362 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
366 void wxComboBox::SetInsertionPoint( long pos
)
368 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
370 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
372 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
375 void wxComboBox::SetInsertionPointEnd(void)
377 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
379 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
380 int pos
= GTK_ENTRY(entry
)->text_length
;
381 SetInsertionPoint( pos
-1 );
384 long wxComboBox::GetInsertionPoint(void) const
386 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
387 return (long) GTK_EDITABLE(entry
)->current_pos
;
390 long wxComboBox::GetLastPosition(void) const
392 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
393 int pos
= GTK_ENTRY(entry
)->text_length
;
397 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
399 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
401 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
402 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
403 if (value
.IsNull()) return;
405 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
408 void wxComboBox::Remove(long from
, long to
)
410 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
412 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
413 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
416 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
418 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
421 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
423 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
426 void wxComboBox::OnSize( wxSizeEvent
&event
)
428 wxControl::OnSize( event
);
432 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
434 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
435 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
438 void wxComboBox::ApplyWidgetStyle()
442 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
443 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
444 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
446 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
447 GList
*child
= list
->children
;
450 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
452 GtkBin
*bin
= GTK_BIN(child
->data
);
453 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
459 GtkWidget
* wxComboBox::GetConnectWidget(void)
461 return GTK_COMBO(m_widget
)->entry
;
464 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
466 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
467 (window
== GTK_COMBO(m_widget
)->button
->window
) );