1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "combobox.h"
15 #include "wx/combobox.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 static void gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
30 if (!combo
->HasVMT()) return;
31 if (g_blockEventsOnDrag
) return;
33 if (combo
->m_alreadySent
)
35 combo
->m_alreadySent
= FALSE
;
39 combo
->m_alreadySent
= TRUE
;
41 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, combo
->GetId());
42 event
.SetInt( combo
->GetSelection() );
43 wxString
tmp( combo
->GetStringSelection() );
44 event
.SetString( WXSTRINGCAST(tmp
) );
45 event
.SetEventObject(combo
);
46 combo
->GetEventHandler()->ProcessEvent(event
);
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
55 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
56 EVT_SIZE(wxComboBox::OnSize
)
59 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
60 const wxPoint
& pos
, const wxSize
& size
,
61 int n
, const wxString choices
[],
62 long style
, const wxValidator
& validator
, const wxString
& name
)
64 m_alreadySent
= FALSE
;
67 PreCreation( parent
, id
, pos
, size
, style
, name
);
69 SetValidator( validator
);
71 m_widget
= gtk_combo_new();
73 wxSize newSize
= size
;
74 if (newSize
.x
== -1) newSize
.x
= 100;
75 if (newSize
.y
== -1) newSize
.y
= 26;
76 SetSize( newSize
.x
, newSize
.y
);
78 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
80 for (int i
= 0; i
< n
; i
++)
83 list_item
= gtk_list_item_new_with_label( choices
[i
] );
85 gtk_container_add( GTK_CONTAINER(list
), list_item
);
87 m_clientData
.Append( (wxObject
*)NULL
);
89 gtk_widget_show( list_item
);
91 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
92 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
97 ConnectWidget( GTK_COMBO(m_widget
)->button
);
99 if (!value
.IsNull()) SetValue( value
);
106 void wxComboBox::Clear(void)
108 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
109 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
111 m_clientData
.Clear();
114 void wxComboBox::Append( const wxString
&item
)
116 Append( item
, (char*)NULL
);
119 void wxComboBox::Append( const wxString
&item
, char *clientData
)
121 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
123 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
127 GtkBin
*bin
= GTK_BIN( list_item
);
128 gtk_widget_set_style( bin
->child
,
130 gtk_widget_get_style( m_widget
) ) );
133 if (m_backgroundColour
!= wxNullColour
)
135 GtkBin
*bin
= GTK_BIN( list_item
);
136 SetBackgroundColourHelper( bin
->child
->window
);
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 gtk_widget_show( list_item
);
149 void wxComboBox::Delete( int n
)
151 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
153 GList
*child
= g_list_nth( listbox
->children
, n
);
157 wxFAIL_MSG("wrong index");
161 GList
*list
= g_list_append( NULL
, child
->data
);
162 gtk_list_remove_items( listbox
, list
);
165 wxNode
*node
= m_clientData
.Nth( n
);
168 wxFAIL_MSG( "wrong index" );
171 m_clientData
.DeleteNode( node
);
174 int wxComboBox::FindString( const wxString
&item
)
176 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
178 GList
*child
= GTK_LIST(list
)->children
;
182 GtkBin
*bin
= GTK_BIN( child
->data
);
183 GtkLabel
*label
= GTK_LABEL( bin
->child
);
184 if (item
== label
->label
) return count
;
189 wxFAIL_MSG( "wxComboBox: string not found" );
194 char* wxComboBox::GetClientData( int n
)
196 wxNode
*node
= m_clientData
.Nth( n
);
197 if (node
) return (char*)node
->Data();
199 wxFAIL_MSG( "wxComboBox: wrong index" );
201 return (char *) NULL
;
204 void wxComboBox::SetClientData( int n
, char * clientData
)
206 wxNode
*node
= m_clientData
.Nth( n
);
207 if (node
) node
->SetData( (wxObject
*) clientData
);
209 wxFAIL_MSG( "wxComboBox: wrong index" );
212 int wxComboBox::GetSelection(void) const
214 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
216 GList
*selection
= GTK_LIST(list
)->selection
;
219 GList
*child
= GTK_LIST(list
)->children
;
223 if (child
->data
== selection
->data
) return count
;
229 wxFAIL_MSG( "wxComboBox: no selection" );
234 wxString
wxComboBox::GetString( int n
) const
236 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
238 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
241 GtkBin
*bin
= GTK_BIN( child
->data
);
242 GtkLabel
*label
= GTK_LABEL( bin
->child
);
246 wxFAIL_MSG( "wxComboBox: wrong index" );
251 wxString
wxComboBox::GetStringSelection(void) const
253 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
255 GList
*selection
= GTK_LIST(list
)->selection
;
258 GtkBin
*bin
= GTK_BIN( selection
->data
);
259 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
263 wxFAIL_MSG( "wxComboBox: no selection" );
268 int wxComboBox::Number(void) const
270 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
272 GList
*child
= GTK_LIST(list
)->children
;
274 while (child
) { count
++; child
= child
->next
; }
278 void wxComboBox::SetSelection( int n
)
280 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
281 gtk_list_select_item( GTK_LIST(list
), n
);
284 void wxComboBox::SetStringSelection( const wxString
&string
)
286 int res
= FindString( string
);
287 if (res
== -1) return;
291 wxString
wxComboBox::GetValue(void) const
293 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
294 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
298 void wxComboBox::SetValue( const wxString
& value
)
300 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
302 if (!value
.IsNull()) tmp
= value
;
303 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
306 void wxComboBox::Copy(void)
308 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
309 #if (GTK_MINOR_VERSION == 1)
310 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
312 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
316 void wxComboBox::Cut(void)
318 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
319 #if (GTK_MINOR_VERSION == 1)
320 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
322 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
326 void wxComboBox::Paste(void)
328 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
329 #if (GTK_MINOR_VERSION == 1)
330 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
332 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
336 void wxComboBox::SetInsertionPoint( long pos
)
338 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
340 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
343 void wxComboBox::SetInsertionPointEnd(void)
345 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
346 int pos
= GTK_ENTRY(entry
)->text_length
;
347 SetInsertionPoint( pos
-1 );
350 long wxComboBox::GetInsertionPoint(void) const
352 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
353 return (long) GTK_EDITABLE(entry
)->current_pos
;
356 long wxComboBox::GetLastPosition(void) const
358 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
359 int pos
= GTK_ENTRY(entry
)->text_length
;
363 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
365 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
366 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
367 if (value
.IsNull()) return;
369 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
372 void wxComboBox::Remove(long from
, long to
)
374 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
375 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
378 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
380 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
383 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
385 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
388 void wxComboBox::OnSize( wxSizeEvent
&event
)
390 wxControl::OnSize( event
);
394 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
396 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
397 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
400 void wxComboBox::SetFont( const wxFont
&font
)
402 wxWindow::SetFont( font
);
404 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
406 gtk_widget_set_style( entry
,
408 gtk_widget_get_style( m_widget
) ) );
410 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
412 GList
*child
= GTK_LIST(list
)->children
;
415 GtkBin
*bin
= (GtkBin
*) child
->data
;
416 gtk_widget_set_style( bin
->child
,
418 gtk_widget_get_style( m_widget
) ) );
424 GtkWidget
* wxComboBox::GetConnectWidget(void)
426 return GTK_COMBO(m_widget
)->entry
;
429 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
431 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
432 (window
== GTK_COMBO(m_widget
)->button
->window
) );
435 void wxComboBox::SetBackgroundColour( const wxColour
&colour
)
437 wxWindow::SetBackgroundColour( colour
);
439 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
441 GList
*child
= GTK_LIST(list
)->children
;
444 GtkBin
*bin
= (GtkBin
*) child
->data
;
445 SetBackgroundColourHelper( bin
->child
->window
);