]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/combobox.cpp
Tries unsuccesfully to cure a few more problems
[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 = gtk_list_item_new_with_label( item );
140
141 if (m_hasOwnStyle)
142 {
143 GtkBin *bin = GTK_BIN( list_item );
144 gtk_widget_set_style( bin->child,
145 gtk_style_ref(
146 gtk_widget_get_style( m_widget ) ) );
147 }
148
149 gtk_signal_connect( GTK_OBJECT(list_item), "select",
150 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
151
152 m_clientData.Append( (wxObject*)clientData );
153
154 gtk_container_add( GTK_CONTAINER(list), list_item );
155
156 gtk_widget_show( list_item );
157 }
158
159 void wxComboBox::Delete( int n )
160 {
161 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
162
163 GList *child = g_list_nth( listbox->children, n );
164
165 if (!child)
166 {
167 wxFAIL_MSG("wrong index");
168 return;
169 }
170
171 GList *list = g_list_append( NULL, child->data );
172 gtk_list_remove_items( listbox, list );
173 g_list_free( list );
174
175 wxNode *node = m_clientData.Nth( n );
176 if (!node)
177 {
178 wxFAIL_MSG( "wrong index" );
179 }
180 else
181 m_clientData.DeleteNode( node );
182 }
183
184 int wxComboBox::FindString( const wxString &item )
185 {
186 GtkWidget *list = GTK_COMBO(m_widget)->list;
187
188 GList *child = GTK_LIST(list)->children;
189 int count = 0;
190 while (child)
191 {
192 GtkBin *bin = GTK_BIN( child->data );
193 GtkLabel *label = GTK_LABEL( bin->child );
194 if (item == label->label) return count;
195 count++;
196 child = child->next;
197 }
198
199 wxFAIL_MSG( "wxComboBox: string not found" );
200
201 return -1;
202 }
203
204 char* wxComboBox::GetClientData( int n )
205 {
206 wxNode *node = m_clientData.Nth( n );
207 if (node) return (char*)node->Data();
208
209 wxFAIL_MSG( "wxComboBox: wrong index" );
210
211 return (char *) NULL;
212 }
213
214 void wxComboBox::SetClientData( int n, char * clientData )
215 {
216 wxNode *node = m_clientData.Nth( n );
217 if (node) node->SetData( (wxObject*) clientData );
218
219 wxFAIL_MSG( "wxComboBox: wrong index" );
220 }
221
222 int wxComboBox::GetSelection(void) const
223 {
224 GtkWidget *list = GTK_COMBO(m_widget)->list;
225
226 GList *selection = GTK_LIST(list)->selection;
227 if (selection)
228 {
229 GList *child = GTK_LIST(list)->children;
230 int count = 0;
231 while (child)
232 {
233 if (child->data == selection->data) return count;
234 count++;
235 child = child->next;
236 }
237 }
238
239 wxFAIL_MSG( "wxComboBox: no selection" );
240
241 return -1;
242 }
243
244 wxString wxComboBox::GetString( int n ) const
245 {
246 GtkWidget *list = GTK_COMBO(m_widget)->list;
247
248 GList *child = g_list_nth( GTK_LIST(list)->children, n );
249 if (child)
250 {
251 GtkBin *bin = GTK_BIN( child->data );
252 GtkLabel *label = GTK_LABEL( bin->child );
253 return label->label;
254 }
255
256 wxFAIL_MSG( "wxComboBox: wrong index" );
257
258 return "";
259 }
260
261 wxString wxComboBox::GetStringSelection(void) const
262 {
263 GtkWidget *list = GTK_COMBO(m_widget)->list;
264
265 GList *selection = GTK_LIST(list)->selection;
266 if (selection)
267 {
268 GtkBin *bin = GTK_BIN( selection->data );
269 wxString tmp = GTK_LABEL( bin->child )->label;
270 return tmp;
271 }
272
273 wxFAIL_MSG( "wxComboBox: no selection" );
274
275 return "";
276 }
277
278 int wxComboBox::Number(void) const
279 {
280 GtkWidget *list = GTK_COMBO(m_widget)->list;
281
282 GList *child = GTK_LIST(list)->children;
283 int count = 0;
284 while (child) { count++; child = child->next; }
285 return count;
286 }
287
288 void wxComboBox::SetSelection( int n )
289 {
290 GtkWidget *list = GTK_COMBO(m_widget)->list;
291 gtk_list_select_item( GTK_LIST(list), n );
292 }
293
294 void wxComboBox::SetStringSelection( const wxString &string )
295 {
296 int res = FindString( string );
297 if (res == -1) return;
298 SetSelection( res );
299 }
300
301 wxString wxComboBox::GetValue(void) const
302 {
303 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
304 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
305 return tmp;
306 }
307
308 void wxComboBox::SetValue( const wxString& value )
309 {
310 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
311 wxString tmp = "";
312 if (!value.IsNull()) tmp = value;
313 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
314 }
315
316 void wxComboBox::Copy(void)
317 {
318 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
319 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
320 }
321
322 void wxComboBox::Cut(void)
323 {
324 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
325 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
326 }
327
328 void wxComboBox::Paste(void)
329 {
330 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
331 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
332 }
333
334 void wxComboBox::SetInsertionPoint( long pos )
335 {
336 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
337 int tmp = (int) pos;
338 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
339 }
340
341 void wxComboBox::SetInsertionPointEnd(void)
342 {
343 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
344 int pos = GTK_ENTRY(entry)->text_length;
345 SetInsertionPoint( pos-1 );
346 }
347
348 long wxComboBox::GetInsertionPoint(void) const
349 {
350 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
351 return (long) GTK_EDITABLE(entry)->current_pos;
352 }
353
354 long wxComboBox::GetLastPosition(void) const
355 {
356 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
357 int pos = GTK_ENTRY(entry)->text_length;
358 return (long) pos-1;
359 }
360
361 void wxComboBox::Replace( long from, long to, const wxString& value )
362 {
363 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
364 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
365 if (value.IsNull()) return;
366 gint pos = (gint)to;
367 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
368 }
369
370 void wxComboBox::Remove(long from, long to)
371 {
372 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
373 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
374 }
375
376 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
377 {
378 }
379
380 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
381 {
382 }
383
384 void wxComboBox::SetFont( const wxFont &font )
385 {
386 wxWindow::SetFont( font );
387
388 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
389
390 gtk_widget_set_style( entry,
391 gtk_style_ref(
392 gtk_widget_get_style( m_widget ) ) );
393
394 GtkWidget *list = GTK_COMBO(m_widget)->list;
395
396 GList *child = GTK_LIST(list)->children;
397 while (child)
398 {
399 GtkBin *bin = (GtkBin*) child->data;
400 gtk_widget_set_style( bin->child,
401 gtk_style_ref(
402 gtk_widget_get_style( m_widget ) ) );
403
404 child = child->next;
405 }
406 }
407
408 GtkWidget* wxComboBox::GetConnectWidget(void)
409 {
410 return GTK_COMBO(m_widget)->entry;
411 }
412
413