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