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