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