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