]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
calling insert("") would provoke an assert - now it's just ignored
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
53010e52
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "combobox.h"
13#endif
14
15#include "wx/combobox.h"
1a5a8367 16#include <wx/intl.h>
53010e52 17
47908e25
RR
18//-----------------------------------------------------------------------------
19// data
20//-----------------------------------------------------------------------------
21
22extern bool g_blockEventsOnDrag;
23
53010e52
RR
24//-----------------------------------------------------------------------------
25// wxComboBox
26//-----------------------------------------------------------------------------
27
47908e25
RR
28//-----------------------------------------------------------------------------
29// clicked
30
31static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 32{
66bd6b93
RR
33 if (!combo->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
47908e25
RR
36 if (combo->m_alreadySent)
37 {
38 combo->m_alreadySent = FALSE;
39 return;
40 }
41
42 combo->m_alreadySent = TRUE;
43
53010e52
RR
44 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
45 event.SetInt( combo->GetSelection() );
46 wxString tmp( combo->GetStringSelection() );
47 event.SetString( WXSTRINGCAST(tmp) );
48 event.SetEventObject(combo);
47908e25 49 combo->GetEventHandler()->ProcessEvent(event);
6de97a3b 50}
47908e25
RR
51
52//-----------------------------------------------------------------------------
53// size
54
55/*
56static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
57{
58 if (!win->HasVMT()) return FALSE;
59 if (g_blockEventsOnDrag) return FALSE;
60 if (!widget->button) return FALSE;
61
62 widget->button->allocation.x =
63 alloc->width - widget->button->allocation.width;
64
65 return FALSE;
6de97a3b 66}
47908e25 67*/
53010e52
RR
68
69//-----------------------------------------------------------------------------
70
71IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
72
debe6624 73bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
53010e52 74 const wxPoint& pos, const wxSize& size,
debe6624 75 int n, const wxString choices[],
6de97a3b 76 long style, const wxValidator& validator, const wxString& name )
53010e52 77{
47908e25 78 m_alreadySent = FALSE;
53010e52
RR
79 m_needParent = TRUE;
80
81 PreCreation( parent, id, pos, size, style, name );
82
6de97a3b
RR
83 SetValidator( validator );
84
53010e52
RR
85 m_widget = gtk_combo_new();
86
87 wxSize newSize = size;
88 if (newSize.x == -1) newSize.x = 100;
89 if (newSize.y == -1) newSize.y = 26;
90 SetSize( newSize.x, newSize.y );
91
92 GtkWidget *list = GTK_COMBO(m_widget)->list;
93
94 for (int i = 0; i < n; i++)
95 {
96 GtkWidget *list_item;
97 list_item = gtk_list_item_new_with_label( choices[i] );
98
53010e52
RR
99 gtk_container_add( GTK_CONTAINER(list), list_item );
100
47908e25
RR
101 m_clientData.Append( (wxObject*)NULL );
102
53010e52 103 gtk_widget_show( list_item );
66bd6b93
RR
104
105 gtk_signal_connect( GTK_OBJECT(list_item), "select",
106 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
6de97a3b 107 }
53010e52
RR
108
109 PostCreation();
110
47908e25
RR
111/*
112 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
113 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
114*/
115
53010e52
RR
116 if (!value.IsNull()) SetValue( value );
117
118 Show( TRUE );
119
120 return TRUE;
6de97a3b 121}
53010e52
RR
122
123void wxComboBox::Clear(void)
124{
125 GtkWidget *list = GTK_COMBO(m_widget)->list;
126 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
47908e25
RR
127
128 m_clientData.Clear();
6de97a3b 129}
53010e52
RR
130
131void wxComboBox::Append( const wxString &item )
47908e25
RR
132{
133 Append( item, (char*)NULL );
6de97a3b 134}
47908e25
RR
135
136void wxComboBox::Append( const wxString &item, char *clientData )
53010e52
RR
137{
138 GtkWidget *list = GTK_COMBO(m_widget)->list;
139
140 GtkWidget *list_item;
141 list_item = gtk_list_item_new_with_label( item );
142
47908e25 143 gtk_signal_connect( GTK_OBJECT(list_item), "select",
53010e52
RR
144 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
145
146 gtk_container_add( GTK_CONTAINER(list), list_item );
147
148 gtk_widget_show( list_item );
47908e25
RR
149
150 m_clientData.Append( (wxObject*)clientData );
6de97a3b 151}
53010e52 152
debe6624 153void wxComboBox::Delete( int n )
53010e52
RR
154{
155 GtkWidget *list = GTK_COMBO(m_widget)->list;
156 gtk_list_clear_items( GTK_LIST(list), n, n );
47908e25
RR
157
158 wxNode *node = m_clientData.Nth( n );
159 if (!node)
160 {
b6af8d80 161 wxFAIL_MSG( "wxComboBox: wrong index" );
47908e25
RR
162 }
163 else
164 m_clientData.DeleteNode( node );
6de97a3b 165}
53010e52
RR
166
167int wxComboBox::FindString( const wxString &item )
168{
169 GtkWidget *list = GTK_COMBO(m_widget)->list;
170
171 GList *child = GTK_LIST(list)->children;
172 int count = 0;
173 while (child)
174 {
175 GtkBin *bin = GTK_BIN( child->data );
176 GtkLabel *label = GTK_LABEL( bin->child );
177 if (item == label->label) return count;
178 count++;
179 child = child->next;
6de97a3b 180 }
b6af8d80
RR
181
182 wxFAIL_MSG( "wxComboBox: string not found" );
183
53010e52 184 return -1;
6de97a3b 185}
53010e52 186
debe6624 187char* wxComboBox::GetClientData( int n )
53010e52
RR
188{
189 wxNode *node = m_clientData.Nth( n );
190 if (node) return (char*)node->Data();
b6af8d80
RR
191
192 wxFAIL_MSG( "wxComboBox: wrong index" );
193
53010e52 194 return NULL;
6de97a3b 195}
53010e52 196
debe6624 197void wxComboBox::SetClientData( int n, char * clientData )
53010e52
RR
198{
199 wxNode *node = m_clientData.Nth( n );
200 if (node) node->SetData( (wxObject*) clientData );
b6af8d80
RR
201
202 wxFAIL_MSG( "wxComboBox: wrong index" );
6de97a3b 203}
53010e52
RR
204
205int wxComboBox::GetSelection(void) const
206{
207 GtkWidget *list = GTK_COMBO(m_widget)->list;
208
209 GList *selection = GTK_LIST(list)->selection;
210 if (selection)
211 {
212 GList *child = GTK_LIST(list)->children;
213 int count = 0;
214 while (child)
215 {
216 if (child->data == selection->data) return count;
217 count++;
218 child = child->next;
6de97a3b
RR
219 }
220 }
b6af8d80
RR
221
222 wxFAIL_MSG( "wxComboBox: no selection" );
223
53010e52 224 return -1;
6de97a3b 225}
53010e52 226
debe6624 227wxString wxComboBox::GetString( int n ) const
53010e52
RR
228{
229 GtkWidget *list = GTK_COMBO(m_widget)->list;
230
231 GList *child = g_list_nth( GTK_LIST(list)->children, n );
232 if (child)
233 {
234 GtkBin *bin = GTK_BIN( child->data );
235 GtkLabel *label = GTK_LABEL( bin->child );
236 return label->label;
6de97a3b 237 }
b6af8d80
RR
238
239 wxFAIL_MSG( "wxComboBox: wrong index" );
240
53010e52 241 return "";
6de97a3b 242}
53010e52
RR
243
244wxString wxComboBox::GetStringSelection(void) const
245{
246 GtkWidget *list = GTK_COMBO(m_widget)->list;
247
248 GList *selection = GTK_LIST(list)->selection;
249 if (selection)
250 {
251 GtkBin *bin = GTK_BIN( selection->data );
252 wxString tmp = GTK_LABEL( bin->child )->label;
253 return tmp;
6de97a3b 254 }
b6af8d80
RR
255
256 wxFAIL_MSG( "wxComboBox: no selection" );
257
53010e52 258 return "";
6de97a3b 259}
53010e52
RR
260
261int wxComboBox::Number(void) const
262{
263 GtkWidget *list = GTK_COMBO(m_widget)->list;
264
265 GList *child = GTK_LIST(list)->children;
266 int count = 0;
6de97a3b 267 while (child) { count++; child = child->next; }
53010e52 268 return count;
6de97a3b 269}
53010e52 270
debe6624 271void wxComboBox::SetSelection( int n )
53010e52
RR
272{
273 GtkWidget *list = GTK_COMBO(m_widget)->list;
274 gtk_list_select_item( GTK_LIST(list), n );
6de97a3b 275}
53010e52 276
47908e25
RR
277void wxComboBox::SetStringSelection( const wxString &string )
278{
279 int res = FindString( string );
280 if (res == -1) return;
281 SetSelection( res );
6de97a3b 282}
47908e25 283
53010e52
RR
284wxString wxComboBox::GetValue(void) const
285{
286 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
287 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
288 return tmp;
6de97a3b 289}
53010e52
RR
290
291void wxComboBox::SetValue( const wxString& value )
292{
293 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
294 wxString tmp = "";
295 if (!value.IsNull()) tmp = value;
296 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
6de97a3b 297}
53010e52
RR
298
299void wxComboBox::Copy(void)
300{
301 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
302 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 303}
53010e52
RR
304
305void wxComboBox::Cut(void)
306{
307 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
308 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 309}
53010e52
RR
310
311void wxComboBox::Paste(void)
312{
313 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
314 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 315}
53010e52 316
debe6624 317void wxComboBox::SetInsertionPoint( long pos )
53010e52
RR
318{
319 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
320 int tmp = (int) pos;
321 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
6de97a3b 322}
53010e52
RR
323
324void wxComboBox::SetInsertionPointEnd(void)
325{
326 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
327 int pos = GTK_ENTRY(entry)->text_length;
328 SetInsertionPoint( pos-1 );
6de97a3b 329}
53010e52
RR
330
331long wxComboBox::GetInsertionPoint(void) const
332{
333 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
334 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 335}
53010e52
RR
336
337long wxComboBox::GetLastPosition(void) const
338{
339 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
340 int pos = GTK_ENTRY(entry)->text_length;
341 return (long) pos-1;
6de97a3b 342}
53010e52 343
debe6624 344void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52
RR
345{
346 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
347 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
348 if (value.IsNull()) return;
349 gint pos = (gint)to;
350 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
6de97a3b 351}
53010e52 352
debe6624 353void wxComboBox::Remove(long from, long to)
53010e52
RR
354{
355 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
356 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 357}
53010e52 358
debe6624 359void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52 360{
6de97a3b 361}
53010e52 362
debe6624 363void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52 364{
6de97a3b 365}
53010e52
RR
366
367