]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Corrected wxlistBox, wxRadioBox, wxComboBox and
[wxWidgets.git] / src / gtk / 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
16 #if wxUSE_COMBOBOX
17
18 #include "wx/settings.h"
19 #include "wx/intl.h"
20
21 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23
24 //-----------------------------------------------------------------------------
25 // idle system
26 //-----------------------------------------------------------------------------
27
28 extern void wxapp_install_idle_handler();
29 extern bool g_isIdle;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern bool g_blockEventsOnDrag;
36
37 //-----------------------------------------------------------------------------
38 // "select"
39 //-----------------------------------------------------------------------------
40
41 static void
42 gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!combo->m_hasVMT) return;
47
48 if (g_blockEventsOnDrag) return;
49
50 if (combo->m_alreadySent)
51 {
52 combo->m_alreadySent = FALSE;
53 return;
54 }
55
56 combo->m_alreadySent = TRUE;
57
58 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
59 event.SetInt( combo->GetSelection() );
60 event.SetString( combo->GetStringSelection() );
61 event.SetEventObject( combo );
62
63 combo->GetEventHandler()->ProcessEvent( event );
64 }
65
66 //-----------------------------------------------------------------------------
67 // "changed"
68 //-----------------------------------------------------------------------------
69
70 static void
71 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
72 {
73 if (g_isIdle) wxapp_install_idle_handler();
74
75 if (!combo->m_hasVMT) return;
76
77 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
78 event.SetString( combo->GetValue() );
79 event.SetEventObject( combo );
80 combo->GetEventHandler()->ProcessEvent( event );
81 }
82
83 //-----------------------------------------------------------------------------
84 // wxComboBox
85 //-----------------------------------------------------------------------------
86
87 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
88
89 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
90 EVT_SIZE(wxComboBox::OnSize)
91 EVT_CHAR(wxComboBox::OnChar)
92 END_EVENT_TABLE()
93
94 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
95 const wxPoint& pos, const wxSize& size,
96 int n, const wxString choices[],
97 long style, const wxValidator& validator,
98 const wxString& name )
99 {
100 m_alreadySent = FALSE;
101 m_needParent = TRUE;
102 m_acceptsFocus = TRUE;
103
104 PreCreation( parent, id, pos, size, style, name );
105
106 #if wxUSE_VALIDATORS
107 SetValidator( validator );
108 #endif
109
110 m_widget = gtk_combo_new();
111
112 // make it more useable
113 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget), TRUE);
114
115 wxSize newSize = size;
116 if (newSize.x == -1)
117 newSize.x = 100;
118 if (newSize.y == -1)
119 newSize.y = 26;
120 SetSize( newSize.x, newSize.y );
121
122 GtkWidget *list = GTK_COMBO(m_widget)->list;
123
124 for (int i = 0; i < n; i++)
125 {
126 /* don't send first event, which GTK sends aways when
127 inserting the first item */
128 m_alreadySent = TRUE;
129
130 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i].mbc_str() );
131
132 m_clientDataList.Append( (wxObject*)NULL );
133 m_clientObjectList.Append( (wxObject*)NULL );
134
135 gtk_container_add( GTK_CONTAINER(list), list_item );
136
137 gtk_signal_connect( GTK_OBJECT(list_item), "select",
138 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
139
140 gtk_widget_show( list_item );
141 }
142
143 m_parent->DoAddChild( this );
144
145 PostCreation();
146
147 ConnectWidget( GTK_COMBO(m_widget)->button );
148
149 if (!value.IsNull()) SetValue( value );
150
151 if (style & wxCB_READONLY)
152 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget)->entry ), FALSE );
153
154 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
155 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
156
157 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
158 SetForegroundColour( parent->GetForegroundColour() );
159 SetFont( parent->GetFont() );
160
161 Show( TRUE );
162
163 return TRUE;
164 }
165
166 wxComboBox::~wxComboBox()
167 {
168 wxNode *node = m_clientObjectList.First();
169 while (node)
170 {
171 wxClientData *cd = (wxClientData*)node->Data();
172 if (cd) delete cd;
173 node = node->Next();
174 }
175 m_clientObjectList.Clear();
176
177 m_clientDataList.Clear();
178 }
179
180 void wxComboBox::AppendCommon( const wxString &item )
181 {
182 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
183
184 GtkWidget *list = GTK_COMBO(m_widget)->list;
185
186 GtkWidget *list_item = gtk_list_item_new_with_label( item.mbc_str() );
187
188 gtk_container_add( GTK_CONTAINER(list), list_item );
189
190 gtk_signal_connect( GTK_OBJECT(list_item), "select",
191 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
192
193 if (GTK_WIDGET_REALIZED(m_widget))
194 {
195 gtk_widget_realize( list_item );
196 gtk_widget_realize( GTK_BIN(list_item)->child );
197
198 if (m_widgetStyle) ApplyWidgetStyle();
199 }
200
201 gtk_widget_show( list_item );
202 }
203
204 void wxComboBox::Append( const wxString &item )
205 {
206 m_clientDataList.Append( (wxObject*) NULL );
207 m_clientObjectList.Append( (wxObject*) NULL );
208
209 AppendCommon( item );
210 }
211
212 void wxComboBox::Append( const wxString &item, void *clientData )
213 {
214 m_clientDataList.Append( (wxObject*) clientData );
215 m_clientObjectList.Append( (wxObject*)NULL );
216
217 AppendCommon( item );
218 }
219
220 void wxComboBox::Append( const wxString &item, wxClientData *clientData )
221 {
222 m_clientDataList.Append( (wxObject*) NULL );
223 m_clientObjectList.Append( (wxObject*) clientData );
224
225 AppendCommon( item );
226 }
227
228 void wxComboBox::SetClientData( int n, void* clientData )
229 {
230 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
231
232 wxNode *node = m_clientDataList.Nth( n );
233 if (!node) return;
234
235 node->SetData( (wxObject*) clientData );
236 }
237
238 void* wxComboBox::GetClientData( int n )
239 {
240 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
241
242 wxNode *node = m_clientDataList.Nth( n );
243 if (!node) return NULL;
244
245 return node->Data();
246 }
247
248 void wxComboBox::SetClientObject( int n, wxClientData* clientData )
249 {
250 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
251
252 wxNode *node = m_clientObjectList.Nth( n );
253 if (!node) return;
254
255 wxClientData *cd = (wxClientData*) node->Data();
256 if (cd) delete cd;
257
258 node->SetData( (wxObject*) clientData );
259 }
260
261 wxClientData* wxComboBox::GetClientObject( int n )
262 {
263 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, _T("invalid combobox") );
264
265 wxNode *node = m_clientDataList.Nth( n );
266 if (!node) return (wxClientData*) NULL;
267
268 return (wxClientData*) node->Data();
269 }
270
271 void wxComboBox::Clear()
272 {
273 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
274
275 GtkWidget *list = GTK_COMBO(m_widget)->list;
276 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
277
278 wxNode *node = m_clientObjectList.First();
279 while (node)
280 {
281 wxClientData *cd = (wxClientData*)node->Data();
282 if (cd) delete cd;
283 node = node->Next();
284 }
285 m_clientObjectList.Clear();
286
287 m_clientDataList.Clear();
288 }
289
290 void wxComboBox::Delete( int n )
291 {
292 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
293
294 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
295
296 GList *child = g_list_nth( listbox->children, n );
297
298 if (!child)
299 {
300 wxFAIL_MSG(_T("wrong index"));
301 return;
302 }
303
304 GList *list = g_list_append( (GList*) NULL, child->data );
305 gtk_list_remove_items( listbox, list );
306 g_list_free( list );
307
308 wxNode *node = m_clientObjectList.Nth( n );
309 if (node)
310 {
311 wxClientData *cd = (wxClientData*)node->Data();
312 if (cd) delete cd;
313 m_clientObjectList.DeleteNode( node );
314 }
315
316 node = m_clientDataList.Nth( n );
317 if (node)
318 {
319 m_clientDataList.DeleteNode( node );
320 }
321 }
322
323 int wxComboBox::FindString( const wxString &item )
324 {
325 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid combobox") );
326
327 GtkWidget *list = GTK_COMBO(m_widget)->list;
328
329 GList *child = GTK_LIST(list)->children;
330 int count = 0;
331 while (child)
332 {
333 GtkBin *bin = GTK_BIN( child->data );
334 GtkLabel *label = GTK_LABEL( bin->child );
335 if (item == wxString(label->label,*wxConvCurrent))
336 return count;
337 count++;
338 child = child->next;
339 }
340
341 return wxNOT_FOUND;
342 }
343
344 int wxComboBox::GetSelection() const
345 {
346 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid combobox") );
347
348 GtkWidget *list = GTK_COMBO(m_widget)->list;
349
350 GList *selection = GTK_LIST(list)->selection;
351 if (selection)
352 {
353 GList *child = GTK_LIST(list)->children;
354 int count = 0;
355 while (child)
356 {
357 if (child->data == selection->data) return count;
358 count++;
359 child = child->next;
360 }
361 }
362
363 wxFAIL_MSG( _T("wxComboBox: no selection") );
364
365 return -1;
366 }
367
368 wxString wxComboBox::GetString( int n ) const
369 {
370 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
371
372 GtkWidget *list = GTK_COMBO(m_widget)->list;
373
374 wxString str;
375 GList *child = g_list_nth( GTK_LIST(list)->children, n );
376 if (child)
377 {
378 GtkBin *bin = GTK_BIN( child->data );
379 GtkLabel *label = GTK_LABEL( bin->child );
380 str = wxString(label->label,*wxConvCurrent);
381 }
382 else
383 {
384 wxFAIL_MSG( _T("wxComboBox: wrong index") );
385 }
386
387 return str;
388 }
389
390 wxString wxComboBox::GetStringSelection() const
391 {
392 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
393
394 GtkWidget *list = GTK_COMBO(m_widget)->list;
395
396 GList *selection = GTK_LIST(list)->selection;
397 if (selection)
398 {
399 GtkBin *bin = GTK_BIN( selection->data );
400 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
401 return tmp;
402 }
403
404 wxFAIL_MSG( _T("wxComboBox: no selection") );
405
406 return _T("");
407 }
408
409 int wxComboBox::Number() const
410 {
411 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid combobox") );
412
413 GtkWidget *list = GTK_COMBO(m_widget)->list;
414
415 GList *child = GTK_LIST(list)->children;
416 int count = 0;
417 while (child) { count++; child = child->next; }
418 return count;
419 }
420
421 void wxComboBox::SetSelection( int n )
422 {
423 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
424
425 DisableEvents();
426
427 GtkWidget *list = GTK_COMBO(m_widget)->list;
428 gtk_list_select_item( GTK_LIST(list), n );
429
430 EnableEvents();
431 }
432
433 void wxComboBox::SetStringSelection( const wxString &string )
434 {
435 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
436
437 int res = FindString( string );
438 if (res == -1) return;
439 SetSelection( res );
440 }
441
442 wxString wxComboBox::GetValue() const
443 {
444 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
445 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
446 return tmp;
447 }
448
449 void wxComboBox::SetValue( const wxString& value )
450 {
451 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
452
453 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
454 wxString tmp = _T("");
455 if (!value.IsNull()) tmp = value;
456 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
457 }
458
459 void wxComboBox::Copy()
460 {
461 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
462
463 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
464 #if (GTK_MINOR_VERSION > 0)
465 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
466 #else
467 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
468 #endif
469 }
470
471 void wxComboBox::Cut()
472 {
473 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
474
475 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
476 #if (GTK_MINOR_VERSION > 0)
477 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
478 #else
479 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
480 #endif
481 }
482
483 void wxComboBox::Paste()
484 {
485 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
486
487 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
488 #if (GTK_MINOR_VERSION > 0)
489 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
490 #else
491 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
492 #endif
493 }
494
495 void wxComboBox::SetInsertionPoint( long pos )
496 {
497 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
498
499 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
500 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
501 }
502
503 void wxComboBox::SetInsertionPointEnd()
504 {
505 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
506
507 SetInsertionPoint( -1 );
508 }
509
510 long wxComboBox::GetInsertionPoint() const
511 {
512 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
513 return (long) GTK_EDITABLE(entry)->current_pos;
514 }
515
516 long wxComboBox::GetLastPosition() const
517 {
518 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
519 int pos = GTK_ENTRY(entry)->text_length;
520 return (long) pos-1;
521 }
522
523 void wxComboBox::Replace( long from, long to, const wxString& value )
524 {
525 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
526 // FIXME: not quite sure how to do this method right in multibyte mode
527
528 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
529 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
530 if (value.IsNull()) return;
531 gint pos = (gint)to;
532 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
533 }
534
535 void wxComboBox::Remove(long from, long to)
536 {
537 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
538
539 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
540 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
541 }
542
543 void wxComboBox::SetSelection( long from, long to )
544 {
545 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
546 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
547 }
548
549 void wxComboBox::SetEditable( bool editable )
550 {
551 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
552 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
553 }
554
555 void wxComboBox::OnChar( wxKeyEvent &event )
556 {
557 if ( event.KeyCode() == WXK_RETURN )
558 {
559 wxString value = GetValue();
560
561 if ( Number() == 0 )
562 {
563 // make Enter generate "selected" event if there is only one item
564 // in the combobox - without it, it's impossible to select it at
565 // all!
566 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
567 event.SetInt( 0 );
568 event.SetString( value );
569 event.SetEventObject( this );
570 GetEventHandler()->ProcessEvent( event );
571 }
572 else
573 {
574 // add the item to the list if it's not there yet
575 if ( FindString(value) == wxNOT_FOUND )
576 {
577 Append(value);
578
579 // and generate the selected event for it
580 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
581 event.SetInt( Number() - 1 );
582 event.SetString( value );
583 event.SetEventObject( this );
584 GetEventHandler()->ProcessEvent( event );
585 }
586 //else: do nothing, this will open the listbox
587 }
588 }
589
590 event.Skip();
591 }
592
593 void wxComboBox::DisableEvents()
594 {
595 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
596 GList *child = list->children;
597 while (child)
598 {
599 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
600 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
601
602 child = child->next;
603 }
604 }
605
606 void wxComboBox::EnableEvents()
607 {
608 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
609 GList *child = list->children;
610 while (child)
611 {
612 gtk_signal_connect( GTK_OBJECT(child->data), "select",
613 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
614
615 child = child->next;
616 }
617 }
618
619 void wxComboBox::OnSize( wxSizeEvent &event )
620 {
621 event.Skip();
622
623 return;
624
625 int w = 21;
626 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
627
628 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
629 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
630 }
631
632 void wxComboBox::ApplyWidgetStyle()
633 {
634 SetWidgetStyle();
635
636 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
637 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
638 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
639
640 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
641 GList *child = list->children;
642 while (child)
643 {
644 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
645
646 GtkBin *bin = GTK_BIN(child->data);
647 gtk_widget_set_style( bin->child, m_widgetStyle );
648
649 child = child->next;
650 }
651 }
652
653 GtkWidget* wxComboBox::GetConnectWidget()
654 {
655 return GTK_COMBO(m_widget)->entry;
656 }
657
658 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
659 {
660 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
661 (window == GTK_COMBO(m_widget)->button->window ) );
662 }
663
664 #endif