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