]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/combobox.cpp
Fix for bug [ 1351659 ] wxTextValidator::Validate not Unicode safe
[wxWidgets.git] / src / gtk / combobox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/combobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#include "wx/combobox.h"
14
15#if wxUSE_COMBOBOX
16
17#include "wx/settings.h"
18#include "wx/arrstr.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
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
38
39//-----------------------------------------------------------------------------
40// "changed" - typing and list item matches get changed, select-child
41// if it doesn't match an item then just get a single changed
42//-----------------------------------------------------------------------------
43
44extern "C" {
45static void
46gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
47{
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (combo->m_ignoreNextUpdate)
51 {
52 combo->m_ignoreNextUpdate = false;
53 return;
54 }
55
56 if (!combo->m_hasVMT) return;
57
58 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
59 event.SetString( combo->GetValue() );
60 event.SetEventObject( combo );
61 combo->GetEventHandler()->ProcessEvent( event );
62}
63}
64
65extern "C" {
66static void
67gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
68{
69}
70}
71
72extern "C" {
73static void
74gtk_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
75{
76 // when the popup is hidden, throw a SELECTED event only if the combobox
77 // selection changed.
78 const int curSelection = combo->GetCurrentSelection();
79
80 const bool hasChanged = curSelection != g_SelectionBeforePopup;
81
82 // reset the selection flag to value meaning that it is hidden and do it
83 // now, before generating the events, so that GetSelection() returns the
84 // new value from the event handler
85 g_SelectionBeforePopup = wxID_NONE;
86
87 if ( hasChanged )
88 {
89 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
90 event.SetInt( curSelection );
91 event.SetString( combo->GetStringSelection() );
92 event.SetEventObject( combo );
93 combo->GetEventHandler()->ProcessEvent( event );
94
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
97 event2.SetString( combo->GetStringSelection() );
98 event2.SetEventObject( combo );
99 combo->GetEventHandler()->ProcessEvent( event2 );
100 }
101}
102}
103
104extern "C" {
105static void
106gtk_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
107{
108 // store the combobox selection value before the popup is shown
109 g_SelectionBeforePopup = combo->GetCurrentSelection();
110}
111}
112
113//-----------------------------------------------------------------------------
114// "select-child" - click/cursor get select-child, changed, select-child
115//-----------------------------------------------------------------------------
116
117extern "C" {
118static void
119gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
120{
121 if (g_isIdle) wxapp_install_idle_handler();
122
123 if (!combo->m_hasVMT) return;
124
125 if (g_blockEventsOnDrag) return;
126
127 int curSelection = combo->GetCurrentSelection();
128
129 if (combo->m_prevSelection == curSelection) return;
130
131 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
132 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
133
134 combo->m_prevSelection = curSelection;
135
136 // Quickly set the value of the combo box
137 // as GTK+ does that only AFTER the event
138 // is sent.
139 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry),
140 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
141 combo->SetValue( combo->GetStringSelection() );
142 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
143 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
144
145 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
146 // because when combobox popup is shown, gtk_combo_select_child_callback is
147 // called each times the mouse is over an item with a pressed button so a lot
148 // of SELECTED event could be generated if the user keep the mouse button down
149 // and select other items ...
150 if (g_SelectionBeforePopup == wxID_NONE)
151 {
152 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
153 event.SetInt( curSelection );
154 event.SetString( combo->GetStringSelection() );
155 event.SetEventObject( combo );
156 combo->GetEventHandler()->ProcessEvent( event );
157
158 // for consistency with the other ports, don't generate text update
159 // events while the user is browsing the combobox neither
160 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
161 event2.SetString( combo->GetValue() );
162 event2.SetEventObject( combo );
163 combo->GetEventHandler()->ProcessEvent( event2 );
164 }
165}
166}
167
168//-----------------------------------------------------------------------------
169// wxComboBox
170//-----------------------------------------------------------------------------
171
172IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
173
174BEGIN_EVENT_TABLE(wxComboBox, wxControl)
175 EVT_SIZE(wxComboBox::OnSize)
176 EVT_CHAR(wxComboBox::OnChar)
177
178 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
179 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
180 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
181 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
182 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
183 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
184 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
185
186 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
187 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
188 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
189 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
190 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
191 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
192 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
193END_EVENT_TABLE()
194
195bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
196 const wxString& value,
197 const wxPoint& pos, const wxSize& size,
198 const wxArrayString& choices,
199 long style, const wxValidator& validator,
200 const wxString& name )
201{
202 wxCArrayString chs(choices);
203
204 return Create( parent, id, value, pos, size, chs.GetCount(),
205 chs.GetStrings(), style, validator, name );
206}
207
208bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
209 const wxPoint& pos, const wxSize& size,
210 int n, const wxString choices[],
211 long style, const wxValidator& validator,
212 const wxString& name )
213{
214 m_ignoreNextUpdate = false;
215 m_needParent = true;
216 m_acceptsFocus = true;
217 m_prevSelection = 0;
218
219 if (!PreCreation( parent, pos, size ) ||
220 !CreateBase( parent, id, pos, size, style, validator, name ))
221 {
222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
223 return false;
224 }
225
226 m_widget = gtk_combo_new();
227 GtkCombo *combo = GTK_COMBO(m_widget);
228
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
231 // ... and add surrogate handler.
232 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
233 (GtkSignalFunc) gtk_dummy_callback, combo);
234
235 // make it more useable
236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
237
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
240
241#ifdef __WXGTK20__
242 if (style & wxNO_BORDER)
243 g_object_set( GTK_ENTRY( combo->entry ), "has-frame", FALSE, NULL );
244#endif
245
246 GtkWidget *list = GTK_COMBO(m_widget)->list;
247
248#ifndef __WXGTK20__
249 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
250#endif
251
252 for (int i = 0; i < n; i++)
253 {
254 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
255
256 m_clientDataList.Append( (wxObject*)NULL );
257 m_clientObjectList.Append( (wxObject*)NULL );
258
259 gtk_container_add( GTK_CONTAINER(list), list_item );
260
261 gtk_widget_show( list_item );
262 }
263
264 m_parent->DoAddChild( this );
265
266 m_focusWidget = combo->entry;
267
268 PostCreation(size);
269
270 ConnectWidget( combo->button );
271
272 // MSW's combo box shows the value and the selection is -1
273 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
274 gtk_list_unselect_all( GTK_LIST(combo->list) );
275
276 if (style & wxCB_READONLY)
277 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
278
279 // "show" and "hide" events are generated when user click on the combobox button which popups a list
280 // this list is the "popwin" gtk widget
281 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
282 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
283 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
284 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
285
286 gtk_signal_connect_after( GTK_OBJECT(combo->entry), "changed",
287 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
288
289 gtk_signal_connect_after( GTK_OBJECT(combo->list), "select-child",
290 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
291
292 SetBestSize(size); // need this too because this is a wxControlWithItems
293
294 // This is required for tool bar support
295// wxSize setsize = GetSize();
296// gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
297
298 return true;
299}
300
301wxComboBox::~wxComboBox()
302{
303 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
304 while (node)
305 {
306 wxClientData *cd = (wxClientData*)node->GetData();
307 if (cd) delete cd;
308 node = node->GetNext();
309 }
310 m_clientObjectList.Clear();
311
312 m_clientDataList.Clear();
313}
314
315void wxComboBox::SetFocus()
316{
317 if ( m_hasFocus )
318 {
319 // don't do anything if we already have focus
320 return;
321 }
322
323 gtk_widget_grab_focus( m_focusWidget );
324}
325
326int wxComboBox::DoAppend( const wxString &item )
327{
328 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
329
330 DisableEvents();
331
332 GtkWidget *list = GTK_COMBO(m_widget)->list;
333
334 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
335
336 gtk_container_add( GTK_CONTAINER(list), list_item );
337
338 if (GTK_WIDGET_REALIZED(m_widget))
339 {
340 gtk_widget_realize( list_item );
341 gtk_widget_realize( GTK_BIN(list_item)->child );
342 }
343
344 // Apply current widget style to the new list_item
345 GtkRcStyle *style = CreateWidgetStyle();
346 if (style)
347 {
348 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
349 GtkBin *bin = GTK_BIN( list_item );
350 GtkWidget *label = GTK_WIDGET( bin->child );
351 gtk_widget_modify_style( label, style );
352 gtk_rc_style_unref( style );
353 }
354
355 gtk_widget_show( list_item );
356
357 const int count = GetCount();
358
359 if ( (int)m_clientDataList.GetCount() < count )
360 m_clientDataList.Append( (wxObject*) NULL );
361 if ( (int)m_clientObjectList.GetCount() < count )
362 m_clientObjectList.Append( (wxObject*) NULL );
363
364 EnableEvents();
365
366 InvalidateBestSize();
367
368 return count - 1;
369}
370
371int wxComboBox::DoInsert( const wxString &item, int pos )
372{
373 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
374 wxT("can't insert into sorted list"));
375
376 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
377
378 int count = GetCount();
379 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
380
381 if (pos == count)
382 return Append(item);
383
384 DisableEvents();
385
386 GtkWidget *list = GTK_COMBO(m_widget)->list;
387
388 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
389
390 GList *gitem_list = g_list_alloc ();
391 gitem_list->data = list_item;
392 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
393
394 if (GTK_WIDGET_REALIZED(m_widget))
395 {
396 gtk_widget_realize( list_item );
397 gtk_widget_realize( GTK_BIN(list_item)->child );
398
399 ApplyWidgetStyle();
400 }
401
402 gtk_widget_show( list_item );
403
404 count = GetCount();
405
406 if ( (int)m_clientDataList.GetCount() < count )
407 m_clientDataList.Insert( pos, (wxObject*) NULL );
408 if ( (int)m_clientObjectList.GetCount() < count )
409 m_clientObjectList.Insert( pos, (wxObject*) NULL );
410
411 EnableEvents();
412
413 InvalidateBestSize();
414
415 return pos;
416}
417
418void wxComboBox::DoSetItemClientData( int n, void* clientData )
419{
420 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
421
422 wxList::compatibility_iterator node = m_clientDataList.Item( n );
423 if (!node) return;
424
425 node->SetData( (wxObject*) clientData );
426}
427
428void* wxComboBox::DoGetItemClientData( int n ) const
429{
430 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
431
432 wxList::compatibility_iterator node = m_clientDataList.Item( n );
433
434 return node ? node->GetData() : NULL;
435}
436
437void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
438{
439 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
440
441 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
442 if (!node) return;
443
444 // wxItemContainer already deletes data for us
445
446 node->SetData( (wxObject*) clientData );
447}
448
449wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
450{
451 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
452
453 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
454
455 return node ? (wxClientData*) node->GetData() : NULL;
456}
457
458void wxComboBox::Clear()
459{
460 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
461
462 DisableEvents();
463
464 GtkWidget *list = GTK_COMBO(m_widget)->list;
465 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
466
467 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
468 while (node)
469 {
470 wxClientData *cd = (wxClientData*)node->GetData();
471 if (cd) delete cd;
472 node = node->GetNext();
473 }
474 m_clientObjectList.Clear();
475
476 m_clientDataList.Clear();
477
478 EnableEvents();
479
480 InvalidateBestSize();
481}
482
483void wxComboBox::Delete( int n )
484{
485 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
486
487 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
488
489 GList *child = g_list_nth( listbox->children, n );
490
491 if (!child)
492 {
493 wxFAIL_MSG(wxT("wrong index"));
494 return;
495 }
496
497 DisableEvents();
498
499 GList *list = g_list_append( (GList*) NULL, child->data );
500 gtk_list_remove_items( listbox, list );
501 g_list_free( list );
502
503 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
504 if (node)
505 {
506 wxClientData *cd = (wxClientData*)node->GetData();
507 if (cd) delete cd;
508 m_clientObjectList.Erase( node );
509 }
510
511 node = m_clientDataList.Item( n );
512 if (node)
513 m_clientDataList.Erase( node );
514
515 EnableEvents();
516
517 InvalidateBestSize();
518}
519
520void wxComboBox::SetString(int n, const wxString &text)
521{
522 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
523
524 GtkWidget *list = GTK_COMBO(m_widget)->list;
525
526 GList *child = g_list_nth( GTK_LIST(list)->children, n );
527 if (child)
528 {
529 GtkBin *bin = GTK_BIN( child->data );
530 GtkLabel *label = GTK_LABEL( bin->child );
531 gtk_label_set_text(label, wxGTK_CONV(text));
532 }
533 else
534 {
535 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
536 }
537
538 InvalidateBestSize();
539}
540
541int wxComboBox::FindString( const wxString &item, bool bCase ) const
542{
543 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
544
545 GtkWidget *list = GTK_COMBO(m_widget)->list;
546
547 GList *child = GTK_LIST(list)->children;
548 int count = 0;
549 while (child)
550 {
551 GtkBin *bin = GTK_BIN( child->data );
552 GtkLabel *label = GTK_LABEL( bin->child );
553#ifdef __WXGTK20__
554 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
555#else
556 wxString str( label->label );
557#endif
558 if (item.IsSameAs( str , bCase ) )
559 return count;
560
561 count++;
562 child = child->next;
563 }
564
565 return wxNOT_FOUND;
566}
567
568int wxComboBox::GetSelection() const
569{
570 // if the popup is currently opened, use the selection as it had been
571 // before it dropped down
572 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
573 : g_SelectionBeforePopup;
574}
575
576int wxComboBox::GetCurrentSelection() const
577{
578 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
579
580 GtkWidget *list = GTK_COMBO(m_widget)->list;
581
582 GList *selection = GTK_LIST(list)->selection;
583 if (selection)
584 {
585 GList *child = GTK_LIST(list)->children;
586 int count = 0;
587 while (child)
588 {
589 if (child->data == selection->data) return count;
590 count++;
591 child = child->next;
592 }
593 }
594
595 return -1;
596}
597
598wxString wxComboBox::GetString( int n ) const
599{
600 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
601
602 GtkWidget *list = GTK_COMBO(m_widget)->list;
603
604 wxString str;
605 GList *child = g_list_nth( GTK_LIST(list)->children, n );
606 if (child)
607 {
608 GtkBin *bin = GTK_BIN( child->data );
609 GtkLabel *label = GTK_LABEL( bin->child );
610#ifdef __WXGTK20__
611 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
612#else
613 str = wxString( label->label );
614#endif
615 }
616 else
617 {
618 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
619 }
620
621 return str;
622}
623
624wxString wxComboBox::GetStringSelection() const
625{
626 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
627
628 GtkWidget *list = GTK_COMBO(m_widget)->list;
629
630 GList *selection = GTK_LIST(list)->selection;
631 if (selection)
632 {
633 GtkBin *bin = GTK_BIN( selection->data );
634 GtkLabel *label = GTK_LABEL( bin->child );
635#ifdef __WXGTK20__
636 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
637#else
638 wxString tmp( label->label );
639#endif
640 return tmp;
641 }
642
643 wxFAIL_MSG( wxT("wxComboBox: no selection") );
644
645 return wxEmptyString;
646}
647
648int wxComboBox::GetCount() const
649{
650 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
651
652 GtkWidget *list = GTK_COMBO(m_widget)->list;
653
654 GList *child = GTK_LIST(list)->children;
655 int count = 0;
656 while (child) { count++; child = child->next; }
657 return count;
658}
659
660void wxComboBox::SetSelection( int n )
661{
662 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
663
664 DisableEvents();
665
666 GtkWidget *list = GTK_COMBO(m_widget)->list;
667 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
668 gtk_list_select_item( GTK_LIST(list), n );
669 m_prevSelection = n;
670
671 EnableEvents();
672}
673
674wxString wxComboBox::GetValue() const
675{
676 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
677 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
678
679#if 0
680 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
681 {
682 wxChar c = tmp[i];
683 printf( "%d ", (int) (c) );
684 }
685 printf( "\n" );
686#endif
687
688 return tmp;
689}
690
691void wxComboBox::SetValue( const wxString& value )
692{
693 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
694
695 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
696 wxString tmp;
697 if (!value.IsNull()) tmp = value;
698 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
699
700 InvalidateBestSize();
701}
702
703void wxComboBox::Copy()
704{
705 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
706
707 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
708 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
709}
710
711void wxComboBox::Cut()
712{
713 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
714
715 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
716 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
717}
718
719void wxComboBox::Paste()
720{
721 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
722
723 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
724 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
725}
726
727void wxComboBox::Undo()
728{
729 // TODO
730}
731
732void wxComboBox::Redo()
733{
734 // TODO
735}
736
737void wxComboBox::SelectAll()
738{
739 SetSelection(0, GetLastPosition());
740}
741
742bool wxComboBox::CanUndo() const
743{
744 // TODO
745 return false;
746}
747
748bool wxComboBox::CanRedo() const
749{
750 // TODO
751 return false;
752}
753
754bool wxComboBox::HasSelection() const
755{
756 long from, to;
757 GetSelection(&from, &to);
758 return from != to;
759}
760
761bool wxComboBox::CanCopy() const
762{
763 // Can copy if there's a selection
764 return HasSelection();
765}
766
767bool wxComboBox::CanCut() const
768{
769 return CanCopy() && IsEditable();
770}
771
772bool wxComboBox::CanPaste() const
773{
774 // TODO: check for text on the clipboard
775 return IsEditable() ;
776}
777
778bool wxComboBox::IsEditable() const
779{
780 return !HasFlag(wxCB_READONLY);
781}
782
783
784void wxComboBox::SetInsertionPoint( long pos )
785{
786 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
787
788 if ( pos == GetLastPosition() )
789 pos = -1;
790
791 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
792 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
793}
794
795long wxComboBox::GetInsertionPoint() const
796{
797 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
798}
799
800wxTextPos wxComboBox::GetLastPosition() const
801{
802 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
803 int pos = GTK_ENTRY(entry)->text_length;
804 return (long) pos-1;
805}
806
807void wxComboBox::Replace( long from, long to, const wxString& value )
808{
809 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
810
811 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
812 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
813 if (value.IsNull()) return;
814 gint pos = (gint)to;
815
816#if wxUSE_UNICODE
817 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
818 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
819#else
820 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
821#endif
822}
823
824void wxComboBox::SetSelection( long from, long to )
825{
826 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
827 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
828}
829
830void wxComboBox::GetSelection( long* from, long* to ) const
831{
832 if (IsEditable())
833 {
834 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
835#ifdef __WXGTK20__
836 gint start, end;
837 gtk_editable_get_selection_bounds(editable, & start, & end);
838 *from = start;
839 *to = end;
840#else
841 *from = (long) editable->selection_start_pos;
842 *to = (long) editable->selection_end_pos;
843#endif
844 }
845}
846
847void wxComboBox::SetEditable( bool editable )
848{
849 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
850 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
851}
852
853void wxComboBox::OnChar( wxKeyEvent &event )
854{
855 if ( event.GetKeyCode() == WXK_RETURN )
856 {
857 // GTK automatically selects an item if its in the list
858 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
859 eventEnter.SetString( GetValue() );
860 eventEnter.SetInt( GetSelection() );
861 eventEnter.SetEventObject( this );
862
863 if (!GetEventHandler()->ProcessEvent( eventEnter ))
864 {
865 // This will invoke the dialog default action, such
866 // as the clicking the default button.
867
868 wxWindow *top_frame = m_parent;
869 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
870 top_frame = top_frame->GetParent();
871
872 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
873 {
874 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
875
876 if (window->default_widget)
877 gtk_widget_activate (window->default_widget);
878 }
879 }
880
881 // Catch GTK event so that GTK doesn't open the drop
882 // down list upon RETURN.
883 return;
884 }
885
886 event.Skip();
887}
888
889void wxComboBox::DisableEvents()
890{
891 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
892 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
893 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
894 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
895}
896
897void wxComboBox::EnableEvents()
898{
899 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
900 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
901 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
902 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
903}
904
905void wxComboBox::OnSize( wxSizeEvent &event )
906{
907 // NB: In some situations (e.g. on non-first page of a wizard, if the
908 // size used is default size), GtkCombo widget is resized correctly,
909 // but it's look is not updated, it's rendered as if it was much wider.
910 // No other widgets are affected, so it looks like a bug in GTK+.
911 // Manually requesting resize calculation (as gtk_pizza_set_size does)
912 // fixes it.
913 if (GTK_WIDGET_VISIBLE(m_widget))
914 gtk_widget_queue_resize(m_widget);
915
916 event.Skip();
917}
918
919void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
920{
921// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
922
923 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
924 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
925
926 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
927 GList *child = list->children;
928 while (child)
929 {
930 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
931
932 GtkBin *bin = GTK_BIN(child->data);
933 gtk_widget_modify_style( bin->child, style );
934
935 child = child->next;
936 }
937}
938
939GtkWidget* wxComboBox::GetConnectWidget()
940{
941 return GTK_COMBO(m_widget)->entry;
942}
943
944bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
945{
946 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
947 (window == GTK_COMBO(m_widget)->button->window ) );
948}
949
950wxSize wxComboBox::DoGetBestSize() const
951{
952 wxSize ret( wxControl::DoGetBestSize() );
953
954 // we know better our horizontal extent: it depends on the longest string
955 // in the combobox
956 if ( m_widget )
957 {
958 int width;
959 size_t count = GetCount();
960 for ( size_t n = 0; n < count; n++ )
961 {
962 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
963 if ( width > ret.x )
964 ret.x = width;
965 }
966 }
967
968 // empty combobox should have some reasonable default size too
969 if ( ret.x < 100 )
970 ret.x = 100;
971
972 CacheBestSize(ret);
973 return ret;
974}
975
976// static
977wxVisualAttributes
978wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
979{
980 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
981}
982
983// ----------------------------------------------------------------------------
984// standard event handling
985// ----------------------------------------------------------------------------
986
987void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
988{
989 Cut();
990}
991
992void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
993{
994 Copy();
995}
996
997void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
998{
999 Paste();
1000}
1001
1002void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1003{
1004 Undo();
1005}
1006
1007void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1008{
1009 Redo();
1010}
1011
1012void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1013{
1014 long from, to;
1015 GetSelection(& from, & to);
1016 if (from != -1 && to != -1)
1017 Remove(from, to);
1018}
1019
1020void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1021{
1022 SetSelection(-1, -1);
1023}
1024
1025void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1026{
1027 event.Enable( CanCut() );
1028}
1029
1030void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1031{
1032 event.Enable( CanCopy() );
1033}
1034
1035void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1036{
1037 event.Enable( CanPaste() );
1038}
1039
1040void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1041{
1042 event.Enable( CanUndo() );
1043}
1044
1045void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1046{
1047 event.Enable( CanRedo() );
1048}
1049
1050void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1051{
1052 event.Enable(HasSelection() && IsEditable()) ;
1053}
1054
1055void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1056{
1057 event.Enable(GetLastPosition() > 0);
1058}
1059
1060#endif