]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/combobox.cpp
Changed m_children to GetChildren() -- not all ports are the same!
[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,
62 const wxString& name )
63 {
64 m_alreadySent = FALSE;
65 m_needParent = TRUE;
66 m_acceptsFocus = TRUE;
67
68 PreCreation( parent, id, pos, size, style, name );
69
70 SetValidator( validator );
71
72 m_widget = gtk_combo_new();
73
74 wxSize newSize = size;
75 if (newSize.x == -1) newSize.x = 100;
76 if (newSize.y == -1) newSize.y = 26;
77 SetSize( newSize.x, newSize.y );
78
79 GtkWidget *list = GTK_COMBO(m_widget)->list;
80
81 for (int i = 0; i < n; i++)
82 {
83 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i] );
84
85 m_clientDataList.Append( (wxObject*)NULL );
86 m_clientObjectList.Append( (wxObject*)NULL );
87
88 gtk_container_add( GTK_CONTAINER(list), list_item );
89
90 gtk_widget_realize( list_item );
91 gtk_widget_realize( GTK_BIN(list_item)->child );
92
93 gtk_widget_show( list_item );
94
95 gtk_signal_connect( GTK_OBJECT(list_item), "select",
96 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
97 }
98
99 m_parent->AddChild( this );
100
101 (m_parent->m_insertCallback)( m_parent, this );
102
103 PostCreation();
104
105 ConnectWidget( GTK_COMBO(m_widget)->button );
106
107 if (!value.IsNull()) SetValue( value );
108
109 gtk_widget_realize( GTK_COMBO(m_widget)->list );
110 gtk_widget_realize( GTK_COMBO(m_widget)->entry );
111 gtk_widget_realize( GTK_COMBO(m_widget)->button );
112
113 SetBackgroundColour( parent->GetBackgroundColour() );
114 SetForegroundColour( parent->GetForegroundColour() );
115
116 Show( TRUE );
117
118 return TRUE;
119 }
120
121 wxComboBox::~wxComboBox()
122 {
123 wxNode *node = m_clientDataList.First();
124 while (node)
125 {
126 wxClientData *cd = (wxClientData*)node->Data();
127 if (cd) delete cd;
128 node = node->Next();
129 }
130 m_clientDataList.Clear();
131 }
132
133 void wxComboBox::AppendCommon( const wxString &item )
134 {
135 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
136
137 GtkWidget *list = GTK_COMBO(m_widget)->list;
138
139 GtkWidget *list_item = gtk_list_item_new_with_label( item );
140
141 gtk_signal_connect( GTK_OBJECT(list_item), "select",
142 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
143
144 gtk_container_add( GTK_CONTAINER(list), list_item );
145
146 if (m_widgetStyle) ApplyWidgetStyle();
147
148 gtk_widget_show( list_item );
149 }
150
151 void wxComboBox::Append( const wxString &item )
152 {
153 m_clientDataList.Append( (wxObject*) NULL );
154 m_clientObjectList.Append( (wxObject*) NULL );
155
156 AppendCommon( item );
157 }
158
159 void wxComboBox::Append( const wxString &item, void *clientData )
160 {
161 m_clientDataList.Append( (wxObject*) clientData );
162 m_clientObjectList.Append( (wxObject*)NULL );
163
164 AppendCommon( item );
165 }
166
167 void wxComboBox::Append( const wxString &item, wxClientData *clientData )
168 {
169 m_clientDataList.Append( (wxObject*) NULL );
170 m_clientObjectList.Append( (wxObject*) clientData );
171
172 AppendCommon( item );
173 }
174
175 void wxComboBox::SetClientData( int n, void* clientData )
176 {
177 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
178
179 wxNode *node = m_clientDataList.Nth( n );
180 if (!node) return;
181
182 node->SetData( (wxObject*) clientData );
183 }
184
185 void* wxComboBox::GetClientData( int n )
186 {
187 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
188
189 wxNode *node = m_clientDataList.Nth( n );
190 if (!node) return NULL;
191
192 return node->Data();
193 }
194
195 void wxComboBox::SetClientObject( int n, wxClientData* clientData )
196 {
197 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
198
199 wxNode *node = m_clientObjectList.Nth( n );
200 if (!node) return;
201
202 wxClientData *cd = (wxClientData*) node->Data();
203 if (cd) delete cd;
204
205 node->SetData( (wxObject*) clientData );
206 }
207
208 wxClientData* wxComboBox::GetClientObject( int n )
209 {
210 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
211
212 wxNode *node = m_clientDataList.Nth( n );
213 if (!node) return (wxClientData*) NULL;
214
215 return (wxClientData*) node->Data();
216 }
217
218 void wxComboBox::Clear()
219 {
220 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
221
222 GtkWidget *list = GTK_COMBO(m_widget)->list;
223 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
224
225 wxNode *node = m_clientObjectList.First();
226 while (node)
227 {
228 wxClientData *cd = (wxClientData*)node->Data();
229 if (cd) delete cd;
230 node = node->Next();
231 }
232 m_clientObjectList.Clear();
233
234 m_clientDataList.Clear();
235 }
236
237 void wxComboBox::Delete( int n )
238 {
239 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
240
241 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
242
243 GList *child = g_list_nth( listbox->children, n );
244
245 if (!child)
246 {
247 wxFAIL_MSG("wrong index");
248 return;
249 }
250
251 GList *list = g_list_append( NULL, child->data );
252 gtk_list_remove_items( listbox, list );
253 g_list_free( list );
254
255 wxNode *node = m_clientObjectList.Nth( n );
256 if (node)
257 {
258 wxClientData *cd = (wxClientData*)node->Data();
259 if (cd) delete cd;
260 m_clientObjectList.DeleteNode( node );
261 }
262
263 node = m_clientDataList.Nth( n );
264 if (node)
265 {
266 m_clientDataList.DeleteNode( node );
267 }
268 }
269
270 int wxComboBox::FindString( const wxString &item )
271 {
272 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
273
274 GtkWidget *list = GTK_COMBO(m_widget)->list;
275
276 GList *child = GTK_LIST(list)->children;
277 int count = 0;
278 while (child)
279 {
280 GtkBin *bin = GTK_BIN( child->data );
281 GtkLabel *label = GTK_LABEL( bin->child );
282 if (item == label->label) return count;
283 count++;
284 child = child->next;
285 }
286
287 wxFAIL_MSG( "wxComboBox: string not found" );
288
289 return -1;
290 }
291
292 int wxComboBox::GetSelection() const
293 {
294 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
295
296 GtkWidget *list = GTK_COMBO(m_widget)->list;
297
298 GList *selection = GTK_LIST(list)->selection;
299 if (selection)
300 {
301 GList *child = GTK_LIST(list)->children;
302 int count = 0;
303 while (child)
304 {
305 if (child->data == selection->data) return count;
306 count++;
307 child = child->next;
308 }
309 }
310
311 wxFAIL_MSG( "wxComboBox: no selection" );
312
313 return -1;
314 }
315
316 wxString wxComboBox::GetString( int n ) const
317 {
318 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
319
320 GtkWidget *list = GTK_COMBO(m_widget)->list;
321
322 GList *child = g_list_nth( GTK_LIST(list)->children, n );
323 if (child)
324 {
325 GtkBin *bin = GTK_BIN( child->data );
326 GtkLabel *label = GTK_LABEL( bin->child );
327 return label->label;
328 }
329
330 wxFAIL_MSG( "wxComboBox: wrong index" );
331
332 return "";
333 }
334
335 wxString wxComboBox::GetStringSelection() const
336 {
337 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
338
339 GtkWidget *list = GTK_COMBO(m_widget)->list;
340
341 GList *selection = GTK_LIST(list)->selection;
342 if (selection)
343 {
344 GtkBin *bin = GTK_BIN( selection->data );
345 wxString tmp = GTK_LABEL( bin->child )->label;
346 return tmp;
347 }
348
349 wxFAIL_MSG( "wxComboBox: no selection" );
350
351 return "";
352 }
353
354 int wxComboBox::Number() const
355 {
356 wxCHECK_MSG( m_widget != NULL, 0, "invalid combobox" );
357
358 GtkWidget *list = GTK_COMBO(m_widget)->list;
359
360 GList *child = GTK_LIST(list)->children;
361 int count = 0;
362 while (child) { count++; child = child->next; }
363 return count;
364 }
365
366 void wxComboBox::SetSelection( int n )
367 {
368 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
369
370 GtkWidget *list = GTK_COMBO(m_widget)->list;
371 gtk_list_select_item( GTK_LIST(list), n );
372 }
373
374 void wxComboBox::SetStringSelection( const wxString &string )
375 {
376 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
377
378 int res = FindString( string );
379 if (res == -1) return;
380 SetSelection( res );
381 }
382
383 wxString wxComboBox::GetValue() const
384 {
385 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
386 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
387 return tmp;
388 }
389
390 void wxComboBox::SetValue( const wxString& value )
391 {
392 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
393
394 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
395 wxString tmp = "";
396 if (!value.IsNull()) tmp = value;
397 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
398 }
399
400 void wxComboBox::Copy()
401 {
402 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
403
404 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
405 #if (GTK_MINOR_VERSION == 1)
406 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
407 #else
408 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
409 #endif
410 }
411
412 void wxComboBox::Cut()
413 {
414 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
415
416 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
417 #if (GTK_MINOR_VERSION == 1)
418 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
419 #else
420 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
421 #endif
422 }
423
424 void wxComboBox::Paste()
425 {
426 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
427
428 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
429 #if (GTK_MINOR_VERSION == 1)
430 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
431 #else
432 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
433 #endif
434 }
435
436 void wxComboBox::SetInsertionPoint( long pos )
437 {
438 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
439
440 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
441 int tmp = (int) pos;
442 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
443 }
444
445 void wxComboBox::SetInsertionPointEnd()
446 {
447 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
448
449 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
450 int pos = GTK_ENTRY(entry)->text_length;
451 SetInsertionPoint( pos-1 );
452 }
453
454 long wxComboBox::GetInsertionPoint() const
455 {
456 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
457 return (long) GTK_EDITABLE(entry)->current_pos;
458 }
459
460 long wxComboBox::GetLastPosition() const
461 {
462 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
463 int pos = GTK_ENTRY(entry)->text_length;
464 return (long) pos-1;
465 }
466
467 void wxComboBox::Replace( long from, long to, const wxString& value )
468 {
469 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
470
471 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
472 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
473 if (value.IsNull()) return;
474 gint pos = (gint)to;
475 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
476 }
477
478 void wxComboBox::Remove(long from, long to)
479 {
480 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
481
482 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
483 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
484 }
485
486 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
487 {
488 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
489 }
490
491 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
492 {
493 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
494 }
495
496 void wxComboBox::OnSize( wxSizeEvent &event )
497 {
498 wxControl::OnSize( event );
499
500 int w = 21;
501 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
502
503 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
504 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
505 }
506
507 void wxComboBox::ApplyWidgetStyle()
508 {
509 SetWidgetStyle();
510
511 gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
512 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
513 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
514
515 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
516 GList *child = list->children;
517 while (child)
518 {
519 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
520
521 GtkBin *bin = GTK_BIN(child->data);
522 gtk_widget_set_style( bin->child, m_widgetStyle );
523
524 child = child->next;
525 }
526 }
527
528 GtkWidget* wxComboBox::GetConnectWidget()
529 {
530 return GTK_COMBO(m_widget)->entry;
531 }
532
533 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
534 {
535 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
536 (window == GTK_COMBO(m_widget)->button->window ) );
537 }