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
,
62 const wxString
& name
)
64 m_alreadySent
= FALSE
;
66 m_acceptsFocus
= TRUE
;
68 PreCreation( parent
, id
, pos
, size
, style
, name
);
70 SetValidator( validator
);
72 m_widget
= gtk_combo_new();
74 wxSize newSize
= size
;
75 if (newSize
.x
== -1) newSize
.x
= 100;
76 if (newSize
.y
== -1) newSize
.y
= 26;
77 SetSize( newSize
.x
, newSize
.y
);
79 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
81 for (int i
= 0; i
< n
; i
++)
83 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
] );
85 m_clientDataList
.Append( (wxObject
*)NULL
);
86 m_clientObjectList
.Append( (wxObject
*)NULL
);
88 gtk_container_add( GTK_CONTAINER(list
), list_item
);
90 gtk_widget_realize( list_item
);
91 gtk_widget_realize( GTK_BIN(list_item
)->child
);
93 gtk_widget_show( list_item
);
95 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
96 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
99 m_parent
->AddChild( this );
101 (m_parent
->m_insertCallback
)( m_parent
, this );
105 ConnectWidget( GTK_COMBO(m_widget
)->button
);
107 if (!value
.IsNull()) SetValue( value
);
109 gtk_widget_realize( GTK_COMBO(m_widget
)->list
);
110 gtk_widget_realize( GTK_COMBO(m_widget
)->entry
);
111 gtk_widget_realize( GTK_COMBO(m_widget
)->button
);
113 SetBackgroundColour( parent
->GetBackgroundColour() );
114 SetForegroundColour( parent
->GetForegroundColour() );
121 wxComboBox::~wxComboBox()
123 wxNode
*node
= m_clientDataList
.First();
126 wxClientData
*cd
= (wxClientData
*)node
->Data();
130 m_clientDataList
.Clear();
133 void wxComboBox::AppendCommon( const wxString
&item
)
135 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
137 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
139 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
141 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
142 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
144 gtk_container_add( GTK_CONTAINER(list
), list_item
);
146 if (m_widgetStyle
) ApplyWidgetStyle();
148 gtk_widget_show( list_item
);
151 void wxComboBox::Append( const wxString
&item
)
153 m_clientDataList
.Append( (wxObject
*) NULL
);
154 m_clientObjectList
.Append( (wxObject
*) NULL
);
156 AppendCommon( item
);
159 void wxComboBox::Append( const wxString
&item
, void *clientData
)
161 m_clientDataList
.Append( (wxObject
*) clientData
);
162 m_clientObjectList
.Append( (wxObject
*)NULL
);
164 AppendCommon( item
);
167 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
169 m_clientDataList
.Append( (wxObject
*) NULL
);
170 m_clientObjectList
.Append( (wxObject
*) clientData
);
172 AppendCommon( item
);
175 void wxComboBox::SetClientData( int n
, void* clientData
)
177 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
179 wxNode
*node
= m_clientDataList
.Nth( n
);
182 node
->SetData( (wxObject
*) clientData
);
185 void* wxComboBox::GetClientData( int n
)
187 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
189 wxNode
*node
= m_clientDataList
.Nth( n
);
190 if (!node
) return NULL
;
195 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
197 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
199 wxNode
*node
= m_clientObjectList
.Nth( n
);
202 wxClientData
*cd
= (wxClientData
*) node
->Data();
205 node
->SetData( (wxObject
*) clientData
);
208 wxClientData
* wxComboBox::GetClientObject( int n
)
210 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
212 wxNode
*node
= m_clientDataList
.Nth( n
);
213 if (!node
) return (wxClientData
*) NULL
;
215 return (wxClientData
*) node
->Data();
218 void wxComboBox::Clear()
220 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
222 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
223 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
225 wxNode
*node
= m_clientObjectList
.First();
228 wxClientData
*cd
= (wxClientData
*)node
->Data();
232 m_clientObjectList
.Clear();
234 m_clientDataList
.Clear();
237 void wxComboBox::Delete( int n
)
239 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
241 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
243 GList
*child
= g_list_nth( listbox
->children
, n
);
247 wxFAIL_MSG("wrong index");
251 GList
*list
= g_list_append( NULL
, child
->data
);
252 gtk_list_remove_items( listbox
, list
);
255 wxNode
*node
= m_clientObjectList
.Nth( n
);
258 wxClientData
*cd
= (wxClientData
*)node
->Data();
260 m_clientObjectList
.DeleteNode( node
);
263 node
= m_clientDataList
.Nth( n
);
266 m_clientDataList
.DeleteNode( node
);
270 int wxComboBox::FindString( const wxString
&item
)
272 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
274 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
276 GList
*child
= GTK_LIST(list
)->children
;
280 GtkBin
*bin
= GTK_BIN( child
->data
);
281 GtkLabel
*label
= GTK_LABEL( bin
->child
);
282 if (item
== label
->label
) return count
;
287 wxFAIL_MSG( "wxComboBox: string not found" );
292 int wxComboBox::GetSelection() const
294 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
296 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
298 GList
*selection
= GTK_LIST(list
)->selection
;
301 GList
*child
= GTK_LIST(list
)->children
;
305 if (child
->data
== selection
->data
) return count
;
311 wxFAIL_MSG( "wxComboBox: no selection" );
316 wxString
wxComboBox::GetString( int n
) const
318 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
320 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
322 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
325 GtkBin
*bin
= GTK_BIN( child
->data
);
326 GtkLabel
*label
= GTK_LABEL( bin
->child
);
330 wxFAIL_MSG( "wxComboBox: wrong index" );
335 wxString
wxComboBox::GetStringSelection() const
337 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
339 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
341 GList
*selection
= GTK_LIST(list
)->selection
;
344 GtkBin
*bin
= GTK_BIN( selection
->data
);
345 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
349 wxFAIL_MSG( "wxComboBox: no selection" );
354 int wxComboBox::Number() const
356 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
358 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
360 GList
*child
= GTK_LIST(list
)->children
;
362 while (child
) { count
++; child
= child
->next
; }
366 void wxComboBox::SetSelection( int n
)
368 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
370 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
371 gtk_list_select_item( GTK_LIST(list
), n
);
374 void wxComboBox::SetStringSelection( const wxString
&string
)
376 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
378 int res
= FindString( string
);
379 if (res
== -1) return;
383 wxString
wxComboBox::GetValue() const
385 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
386 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
390 void wxComboBox::SetValue( const wxString
& value
)
392 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
394 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
396 if (!value
.IsNull()) tmp
= value
;
397 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
400 void wxComboBox::Copy()
402 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
404 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
405 #if (GTK_MINOR_VERSION == 1)
406 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
408 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
412 void wxComboBox::Cut()
414 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
416 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
417 #if (GTK_MINOR_VERSION == 1)
418 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
420 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
424 void wxComboBox::Paste()
426 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
428 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
429 #if (GTK_MINOR_VERSION == 1)
430 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
432 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
436 void wxComboBox::SetInsertionPoint( long pos
)
438 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
440 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
442 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
445 void wxComboBox::SetInsertionPointEnd()
447 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
449 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
450 int pos
= GTK_ENTRY(entry
)->text_length
;
451 SetInsertionPoint( pos
-1 );
454 long wxComboBox::GetInsertionPoint() const
456 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
457 return (long) GTK_EDITABLE(entry
)->current_pos
;
460 long wxComboBox::GetLastPosition() const
462 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
463 int pos
= GTK_ENTRY(entry
)->text_length
;
467 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
469 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
471 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
472 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
473 if (value
.IsNull()) return;
475 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
478 void wxComboBox::Remove(long from
, long to
)
480 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
482 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
483 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
486 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
488 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
491 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
493 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
496 void wxComboBox::OnSize( wxSizeEvent
&event
)
498 wxControl::OnSize( event
);
501 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
503 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
504 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
507 void wxComboBox::ApplyWidgetStyle()
511 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, m_widgetStyle
);
512 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
513 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
515 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
516 GList
*child
= list
->children
;
519 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
521 GtkBin
*bin
= GTK_BIN(child
->data
);
522 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
528 GtkWidget
* wxComboBox::GetConnectWidget()
530 return GTK_COMBO(m_widget
)->entry
;
533 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
535 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
536 (window
== GTK_COMBO(m_widget
)->button
->window
) );