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