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