]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/combobox.cpp
Fix for TextCtrl problem as reported by Vegh
[wxWidgets.git] / src / gtk1 / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "combobox.h"
12 #endif
13
14 #include "wx/combobox.h"
15 #include <wx/intl.h>
16
17 //-----------------------------------------------------------------------------
18 // data
19 //-----------------------------------------------------------------------------
20
21 extern bool g_blockEventsOnDrag;
22
23 //-----------------------------------------------------------------------------
24 // "select"
25 //-----------------------------------------------------------------------------
26
27 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
28 {
29 if (!combo->HasVMT()) return;
30 if (g_blockEventsOnDrag) return;
31
32 if (combo->m_alreadySent)
33 {
34 combo->m_alreadySent = FALSE;
35 return;
36 }
37
38 combo->m_alreadySent = TRUE;
39
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);
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxComboBox
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
53
54 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
55 EVT_SIZE(wxComboBox::OnSize)
56 END_EVENT_TABLE()
57
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 )
62 {
63 m_alreadySent = FALSE;
64 m_needParent = TRUE;
65
66 PreCreation( parent, id, pos, size, style, name );
67
68 SetValidator( validator );
69
70 m_widget = gtk_combo_new();
71
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 );
76
77 GtkWidget *list = GTK_COMBO(m_widget)->list;
78
79 for (int i = 0; i < n; i++)
80 {
81 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i] );
82
83 m_clientData.Append( (wxObject*)NULL );
84
85 gtk_container_add( GTK_CONTAINER(list), list_item );
86
87 gtk_widget_realize( list_item );
88 gtk_widget_realize( GTK_BIN(list_item)->child );
89
90 gtk_widget_show( list_item );
91
92 gtk_signal_connect( GTK_OBJECT(list_item), "select",
93 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
94 }
95
96 m_parent->AddChild( this );
97
98 (m_parent->m_insertCallback)( m_parent, this );
99
100 PostCreation();
101
102 ConnectWidget( GTK_COMBO(m_widget)->button );
103
104 if (!value.IsNull()) SetValue( value );
105
106 gtk_widget_realize( GTK_COMBO(m_widget)->list );
107 gtk_widget_realize( GTK_COMBO(m_widget)->entry );
108 gtk_widget_realize( GTK_COMBO(m_widget)->button );
109
110 SetBackgroundColour( parent->GetBackgroundColour() );
111 SetForegroundColour( parent->GetForegroundColour() );
112
113 Show( TRUE );
114
115 return TRUE;
116 }
117
118 void wxComboBox::Clear(void)
119 {
120 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
121
122 GtkWidget *list = GTK_COMBO(m_widget)->list;
123 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
124
125 m_clientData.Clear();
126 }
127
128 void wxComboBox::Append( const wxString &item )
129 {
130 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
131
132 Append( item, (char*)NULL );
133 }
134
135 void wxComboBox::Append( const wxString &item, char *clientData )
136 {
137 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
138
139 GtkWidget *list = GTK_COMBO(m_widget)->list;
140
141 GtkWidget *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 if (m_widgetStyle) ApplyWidgetStyle();
151
152 gtk_widget_show( list_item );
153 }
154
155 void wxComboBox::Delete( int n )
156 {
157 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
158
159 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
160
161 GList *child = g_list_nth( listbox->children, n );
162
163 if (!child)
164 {
165 wxFAIL_MSG("wrong index");
166 return;
167 }
168
169 GList *list = g_list_append( NULL, child->data );
170 gtk_list_remove_items( listbox, list );
171 g_list_free( list );
172
173 wxNode *node = m_clientData.Nth( n );
174 if (!node)
175 {
176 wxFAIL_MSG( "wrong index" );
177 }
178 else
179 m_clientData.DeleteNode( node );
180 }
181
182 int wxComboBox::FindString( const wxString &item )
183 {
184 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
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 wxCHECK_MSG( m_widget != NULL, (char*)NULL, "invalid combobox" );
207
208 wxNode *node = m_clientData.Nth( n );
209 if (node) return (char*)node->Data();
210
211 wxFAIL_MSG( "wxComboBox: wrong index" );
212
213 return (char *) NULL;
214 }
215
216 void wxComboBox::SetClientData( int n, char * clientData )
217 {
218 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
219
220 wxNode *node = m_clientData.Nth( n );
221 if (node) node->SetData( (wxObject*) clientData );
222
223 wxFAIL_MSG( "wxComboBox: wrong index" );
224 }
225
226 int wxComboBox::GetSelection(void) const
227 {
228 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
229
230 GtkWidget *list = GTK_COMBO(m_widget)->list;
231
232 GList *selection = GTK_LIST(list)->selection;
233 if (selection)
234 {
235 GList *child = GTK_LIST(list)->children;
236 int count = 0;
237 while (child)
238 {
239 if (child->data == selection->data) return count;
240 count++;
241 child = child->next;
242 }
243 }
244
245 wxFAIL_MSG( "wxComboBox: no selection" );
246
247 return -1;
248 }
249
250 wxString wxComboBox::GetString( int n ) const
251 {
252 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
253
254 GtkWidget *list = GTK_COMBO(m_widget)->list;
255
256 GList *child = g_list_nth( GTK_LIST(list)->children, n );
257 if (child)
258 {
259 GtkBin *bin = GTK_BIN( child->data );
260 GtkLabel *label = GTK_LABEL( bin->child );
261 return label->label;
262 }
263
264 wxFAIL_MSG( "wxComboBox: wrong index" );
265
266 return "";
267 }
268
269 wxString wxComboBox::GetStringSelection(void) const
270 {
271 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
272
273 GtkWidget *list = GTK_COMBO(m_widget)->list;
274
275 GList *selection = GTK_LIST(list)->selection;
276 if (selection)
277 {
278 GtkBin *bin = GTK_BIN( selection->data );
279 wxString tmp = GTK_LABEL( bin->child )->label;
280 return tmp;
281 }
282
283 wxFAIL_MSG( "wxComboBox: no selection" );
284
285 return "";
286 }
287
288 int wxComboBox::Number(void) const
289 {
290 wxCHECK_MSG( m_widget != NULL, 0, "invalid combobox" );
291
292 GtkWidget *list = GTK_COMBO(m_widget)->list;
293
294 GList *child = GTK_LIST(list)->children;
295 int count = 0;
296 while (child) { count++; child = child->next; }
297 return count;
298 }
299
300 void wxComboBox::SetSelection( int n )
301 {
302 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
303
304 GtkWidget *list = GTK_COMBO(m_widget)->list;
305 gtk_list_select_item( GTK_LIST(list), n );
306 }
307
308 void wxComboBox::SetStringSelection( const wxString &string )
309 {
310 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
311
312 int res = FindString( string );
313 if (res == -1) return;
314 SetSelection( res );
315 }
316
317 wxString wxComboBox::GetValue(void) const
318 {
319 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
320 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
321 return tmp;
322 }
323
324 void wxComboBox::SetValue( const wxString& value )
325 {
326 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
327
328 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
329 wxString tmp = "";
330 if (!value.IsNull()) tmp = value;
331 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
332 }
333
334 void wxComboBox::Copy(void)
335 {
336 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
337
338 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
339 #if (GTK_MINOR_VERSION == 1)
340 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
341 #else
342 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
343 #endif
344 }
345
346 void wxComboBox::Cut(void)
347 {
348 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
349
350 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
351 #if (GTK_MINOR_VERSION == 1)
352 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
353 #else
354 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
355 #endif
356 }
357
358 void wxComboBox::Paste(void)
359 {
360 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
361
362 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
363 #if (GTK_MINOR_VERSION == 1)
364 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
365 #else
366 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
367 #endif
368 }
369
370 void wxComboBox::SetInsertionPoint( long pos )
371 {
372 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
373
374 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
375 int tmp = (int) pos;
376 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
377 }
378
379 void wxComboBox::SetInsertionPointEnd(void)
380 {
381 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
382
383 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
384 int pos = GTK_ENTRY(entry)->text_length;
385 SetInsertionPoint( pos-1 );
386 }
387
388 long wxComboBox::GetInsertionPoint(void) const
389 {
390 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
391 return (long) GTK_EDITABLE(entry)->current_pos;
392 }
393
394 long wxComboBox::GetLastPosition(void) const
395 {
396 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
397 int pos = GTK_ENTRY(entry)->text_length;
398 return (long) pos-1;
399 }
400
401 void wxComboBox::Replace( long from, long to, const wxString& value )
402 {
403 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
404
405 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
406 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
407 if (value.IsNull()) return;
408 gint pos = (gint)to;
409 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
410 }
411
412 void wxComboBox::Remove(long from, long to)
413 {
414 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
415
416 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
417 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
418 }
419
420 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
421 {
422 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
423 }
424
425 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
426 {
427 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
428 }
429
430 void wxComboBox::OnSize( wxSizeEvent &event )
431 {
432 wxControl::OnSize( event );
433
434 int w = 21;
435
436 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
437
438 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
439 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
440 }
441
442 void wxComboBox::ApplyWidgetStyle()
443 {
444 SetWidgetStyle();
445
446 gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
447 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
448 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
449
450 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
451 GList *child = list->children;
452 while (child)
453 {
454 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
455
456 GtkBin *bin = GTK_BIN(child->data);
457 gtk_widget_set_style( bin->child, m_widgetStyle );
458
459 child = child->next;
460 }
461 }
462
463 GtkWidget* wxComboBox::GetConnectWidget(void)
464 {
465 return GTK_COMBO(m_widget)->entry;
466 }
467
468 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
469 {
470 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
471 (window == GTK_COMBO(m_widget)->button->window ) );
472 }