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 );
96 m_parent
->AddChild( this );
98 (m_parent
->m_insertCallback
)( m_parent
, this );
102 ConnectWidget( GTK_COMBO(m_widget
)->button
);
104 if (!value
.IsNull()) SetValue( value
);
106 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
107 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
108 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
110 SetBackgroundColour( parent
->GetBackgroundColour() );
111 SetForegroundColour( parent
->GetForegroundColour() );
118 void wxComboBox::Clear(void)
120 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
122 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
123 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
125 m_clientData
.Clear();
128 void wxComboBox::Append( const wxString
&item
)
130 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
132 Append( item
, (char*)NULL
);
135 void wxComboBox::Append( const wxString
&item
, char *clientData
)
137 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
139 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
141 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
143 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
144 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
146 m_clientData
.Append( (wxObject
*)clientData
);
148 gtk_container_add( GTK_CONTAINER(list
), list_item
);
150 if (m_widgetStyle
) ApplyWidgetStyle();
152 gtk_widget_show( list_item
);
155 void wxComboBox::Delete( int n
)
157 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
159 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
161 GList
*child
= g_list_nth( listbox
->children
, n
);
165 wxFAIL_MSG("wrong index");
169 GList
*list
= g_list_append( NULL
, child
->data
);
170 gtk_list_remove_items( listbox
, list
);
173 wxNode
*node
= m_clientData
.Nth( n
);
176 wxFAIL_MSG( "wrong index" );
179 m_clientData
.DeleteNode( node
);
182 int wxComboBox::FindString( const wxString
&item
)
184 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
186 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
188 GList
*child
= GTK_LIST(list
)->children
;
192 GtkBin
*bin
= GTK_BIN( child
->data
);
193 GtkLabel
*label
= GTK_LABEL( bin
->child
);
194 if (item
== label
->label
) return count
;
199 wxFAIL_MSG( "wxComboBox: string not found" );
204 char* wxComboBox::GetClientData( int n
)
206 wxCHECK_MSG( m_widget
!= NULL
, (char*)NULL
, "invalid combobox" );
208 wxNode
*node
= m_clientData
.Nth( n
);
209 if (node
) return (char*)node
->Data();
211 wxFAIL_MSG( "wxComboBox: wrong index" );
213 return (char *) NULL
;
216 void wxComboBox::SetClientData( int n
, char * clientData
)
218 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
220 wxNode
*node
= m_clientData
.Nth( n
);
221 if (node
) node
->SetData( (wxObject
*) clientData
);
223 wxFAIL_MSG( "wxComboBox: wrong index" );
226 int wxComboBox::GetSelection(void) const
228 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
230 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
232 GList
*selection
= GTK_LIST(list
)->selection
;
235 GList
*child
= GTK_LIST(list
)->children
;
239 if (child
->data
== selection
->data
) return count
;
245 wxFAIL_MSG( "wxComboBox: no selection" );
250 wxString
wxComboBox::GetString( int n
) const
252 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
254 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
256 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
259 GtkBin
*bin
= GTK_BIN( child
->data
);
260 GtkLabel
*label
= GTK_LABEL( bin
->child
);
264 wxFAIL_MSG( "wxComboBox: wrong index" );
269 wxString
wxComboBox::GetStringSelection(void) const
271 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
273 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
275 GList
*selection
= GTK_LIST(list
)->selection
;
278 GtkBin
*bin
= GTK_BIN( selection
->data
);
279 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
283 wxFAIL_MSG( "wxComboBox: no selection" );
288 int wxComboBox::Number(void) const
290 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
292 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
294 GList
*child
= GTK_LIST(list
)->children
;
296 while (child
) { count
++; child
= child
->next
; }
300 void wxComboBox::SetSelection( int n
)
302 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
304 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
305 gtk_list_select_item( GTK_LIST(list
), n
);
308 void wxComboBox::SetStringSelection( const wxString
&string
)
310 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
312 int res
= FindString( string
);
313 if (res
== -1) return;
317 wxString
wxComboBox::GetValue(void) const
319 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
320 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
324 void wxComboBox::SetValue( const wxString
& value
)
326 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
328 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
330 if (!value
.IsNull()) tmp
= value
;
331 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
334 void wxComboBox::Copy(void)
336 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
338 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
339 #if (GTK_MINOR_VERSION == 1)
340 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
342 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
346 void wxComboBox::Cut(void)
348 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
350 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
351 #if (GTK_MINOR_VERSION == 1)
352 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
354 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
358 void wxComboBox::Paste(void)
360 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
362 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
363 #if (GTK_MINOR_VERSION == 1)
364 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
366 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
370 void wxComboBox::SetInsertionPoint( long pos
)
372 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
374 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
376 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
379 void wxComboBox::SetInsertionPointEnd(void)
381 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
383 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
384 int pos
= GTK_ENTRY(entry
)->text_length
;
385 SetInsertionPoint( pos
-1 );
388 long wxComboBox::GetInsertionPoint(void) const
390 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
391 return (long) GTK_EDITABLE(entry
)->current_pos
;
394 long wxComboBox::GetLastPosition(void) const
396 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
397 int pos
= GTK_ENTRY(entry
)->text_length
;
401 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
403 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
405 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
406 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
407 if (value
.IsNull()) return;
409 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
412 void wxComboBox::Remove(long from
, long to
)
414 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
416 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
417 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
420 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
422 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
425 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
427 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
430 void wxComboBox::OnSize( wxSizeEvent
&event
)
432 wxControl::OnSize( event
);
436 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
438 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
439 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
442 void wxComboBox::ApplyWidgetStyle()
446 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
447 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
448 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
450 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
451 GList
*child
= list
->children
;
454 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
456 GtkBin
*bin
= GTK_BIN(child
->data
);
457 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
463 GtkWidget
* wxComboBox::GetConnectWidget(void)
465 return GTK_COMBO(m_widget
)->entry
;
468 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
470 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
471 (window
== GTK_COMBO(m_widget
)->button
->window
) );