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() );
113 void wxComboBox::Clear(void)
115 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
117 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
118 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
120 m_clientData
.Clear();
123 void wxComboBox::Append( const wxString
&item
)
125 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
127 Append( item
, (char*)NULL
);
130 void wxComboBox::Append( const wxString
&item
, char *clientData
)
132 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
134 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
136 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
140 GtkBin
*bin
= GTK_BIN( list_item
);
141 gtk_widget_set_style( bin
->child
,
143 gtk_widget_get_style( m_widget
) ) );
144 gtk_widget_set_style( GTK_WIDGET(bin
),
146 gtk_widget_get_style( m_widget
) ) );
149 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
150 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
152 m_clientData
.Append( (wxObject
*)clientData
);
154 gtk_container_add( GTK_CONTAINER(list
), list_item
);
156 gtk_widget_show( list_item
);
159 void wxComboBox::Delete( int n
)
161 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
163 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
165 GList
*child
= g_list_nth( listbox
->children
, n
);
169 wxFAIL_MSG("wrong index");
173 GList
*list
= g_list_append( NULL
, child
->data
);
174 gtk_list_remove_items( listbox
, list
);
177 wxNode
*node
= m_clientData
.Nth( n
);
180 wxFAIL_MSG( "wrong index" );
183 m_clientData
.DeleteNode( node
);
186 int wxComboBox::FindString( const wxString
&item
)
188 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
190 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
192 GList
*child
= GTK_LIST(list
)->children
;
196 GtkBin
*bin
= GTK_BIN( child
->data
);
197 GtkLabel
*label
= GTK_LABEL( bin
->child
);
198 if (item
== label
->label
) return count
;
203 wxFAIL_MSG( "wxComboBox: string not found" );
208 char* wxComboBox::GetClientData( int n
)
210 wxCHECK_MSG( m_widget
!= NULL
, (char*)NULL
, "invalid combobox" );
212 wxNode
*node
= m_clientData
.Nth( n
);
213 if (node
) return (char*)node
->Data();
215 wxFAIL_MSG( "wxComboBox: wrong index" );
217 return (char *) NULL
;
220 void wxComboBox::SetClientData( int n
, char * clientData
)
222 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
224 wxNode
*node
= m_clientData
.Nth( n
);
225 if (node
) node
->SetData( (wxObject
*) clientData
);
227 wxFAIL_MSG( "wxComboBox: wrong index" );
230 int wxComboBox::GetSelection(void) const
232 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid combobox" );
234 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
236 GList
*selection
= GTK_LIST(list
)->selection
;
239 GList
*child
= GTK_LIST(list
)->children
;
243 if (child
->data
== selection
->data
) return count
;
249 wxFAIL_MSG( "wxComboBox: no selection" );
254 wxString
wxComboBox::GetString( int n
) const
256 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
258 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
260 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
263 GtkBin
*bin
= GTK_BIN( child
->data
);
264 GtkLabel
*label
= GTK_LABEL( bin
->child
);
268 wxFAIL_MSG( "wxComboBox: wrong index" );
273 wxString
wxComboBox::GetStringSelection(void) const
275 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid combobox" );
277 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
279 GList
*selection
= GTK_LIST(list
)->selection
;
282 GtkBin
*bin
= GTK_BIN( selection
->data
);
283 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
287 wxFAIL_MSG( "wxComboBox: no selection" );
292 int wxComboBox::Number(void) const
294 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid combobox" );
296 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
298 GList
*child
= GTK_LIST(list
)->children
;
300 while (child
) { count
++; child
= child
->next
; }
304 void wxComboBox::SetSelection( int n
)
306 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
308 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
309 gtk_list_select_item( GTK_LIST(list
), n
);
312 void wxComboBox::SetStringSelection( const wxString
&string
)
314 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
316 int res
= FindString( string
);
317 if (res
== -1) return;
321 wxString
wxComboBox::GetValue(void) const
323 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
324 wxString tmp
= gtk_entry_get_text( GTK_ENTRY(entry
) );
328 void wxComboBox::SetValue( const wxString
& value
)
330 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
332 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
334 if (!value
.IsNull()) tmp
= value
;
335 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
);
338 void wxComboBox::Copy(void)
340 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
342 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
343 #if (GTK_MINOR_VERSION == 1)
344 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
346 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
350 void wxComboBox::Cut(void)
352 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
354 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
355 #if (GTK_MINOR_VERSION == 1)
356 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
358 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
362 void wxComboBox::Paste(void)
364 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
366 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
367 #if (GTK_MINOR_VERSION == 1)
368 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
370 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
374 void wxComboBox::SetInsertionPoint( long pos
)
376 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
378 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
380 gtk_entry_set_position( GTK_ENTRY(entry
), tmp
);
383 void wxComboBox::SetInsertionPointEnd(void)
385 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
387 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
388 int pos
= GTK_ENTRY(entry
)->text_length
;
389 SetInsertionPoint( pos
-1 );
392 long wxComboBox::GetInsertionPoint(void) const
394 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
395 return (long) GTK_EDITABLE(entry
)->current_pos
;
398 long wxComboBox::GetLastPosition(void) const
400 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
401 int pos
= GTK_ENTRY(entry
)->text_length
;
405 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
407 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
409 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
410 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
411 if (value
.IsNull()) return;
413 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
, value
.Length(), &pos
);
416 void wxComboBox::Remove(long from
, long to
)
418 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
420 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
421 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
424 void wxComboBox::SetSelection( long WXUNUSED(from
), long WXUNUSED(to
) )
426 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
429 void wxComboBox::SetEditable( bool WXUNUSED(editable
) )
431 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
434 void wxComboBox::OnSize( wxSizeEvent
&event
)
436 wxControl::OnSize( event
);
440 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
442 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
443 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
446 void wxComboBox::SetFont( const wxFont
&font
)
448 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
450 wxWindow::SetFont( font
);
452 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
454 gtk_widget_set_style( entry
,
456 gtk_widget_get_style( m_widget
) ) );
458 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
460 GList
*child
= GTK_LIST(list
)->children
;
463 GtkBin
*bin
= (GtkBin
*) child
->data
;
464 gtk_widget_set_style( bin
->child
,
466 gtk_widget_get_style( m_widget
) ) );
472 GtkWidget
* wxComboBox::GetConnectWidget(void)
474 return GTK_COMBO(m_widget
)->entry
;
477 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
479 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
480 (window
== GTK_COMBO(m_widget
)->button
->window
) );
483 void wxComboBox::SetBackgroundColour( const wxColour
&colour
)
487 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
489 m_backgroundColour
= colour
;
490 if (!m_backgroundColour
.Ok()) return;
492 GtkStyle
*style
= gtk_widget_get_style( m_widget
);
495 m_hasOwnStyle
= TRUE
;
496 style
= gtk_style_copy( gtk_widget_get_style( m_widget
) );
499 style
->base
[GTK_STATE_NORMAL
] = *m_backgroundColour
.GetColor();
500 style
->bg
[GTK_STATE_NORMAL
] = *m_backgroundColour
.GetColor();
502 gtk_widget_set_style( m_widget
, style
);
504 gtk_widget_set_style( GTK_COMBO(m_widget
)->button
, gtk_style_ref( style
) );
505 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, gtk_style_ref( style
) );
506 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, gtk_style_ref( style
) );
508 GList
*child
= GTK_LIST( GTK_COMBO(m_widget
)->list
)->children
;
511 GtkWidget
*item
= GTK_WIDGET(child
->data
);
512 gtk_widget_set_style( item
, gtk_style_ref( style
) );