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