Nuke GTK1 from src/gtk
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/combobox.h"
14
15 #if wxUSE_COMBOBOX
16
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
19 #include "wx/intl.h"
20
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
23 #include "wx/gtk/private.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
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_COMMAND_TEXT_UPDATED, combo->GetId() );
59 event.SetString( combo->GetValue() );
60 event.SetEventObject( combo );
61 combo->GetEventHandler()->ProcessEvent( 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_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
90 event.SetInt( curSelection );
91 event.SetString( combo->GetStringSelection() );
92 event.SetEventObject( combo );
93 combo->GetEventHandler()->ProcessEvent( event );
94
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
97 event2.SetString( combo->GetStringSelection() );
98 event2.SetEventObject( combo );
99 combo->GetEventHandler()->ProcessEvent( event2 );
100 }
101 }
102 }
103
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_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
153 event.SetInt( curSelection );
154 event.SetString( combo->GetStringSelection() );
155 event.SetEventObject( combo );
156 combo->GetEventHandler()->ProcessEvent( event );
157
158 // for consistency with the other ports, don't generate text update
159 // events while the user is browsing the combobox neither
160 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
161 event2.SetString( combo->GetValue() );
162 event2.SetEventObject( combo );
163 combo->GetEventHandler()->ProcessEvent( event2 );
164 }
165 }
166 }
167
168 //-----------------------------------------------------------------------------
169 // wxComboBox
170 //-----------------------------------------------------------------------------
171
172 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
173
174 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
175 EVT_SIZE(wxComboBox::OnSize)
176 EVT_CHAR(wxComboBox::OnChar)
177
178 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
179 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
180 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
181 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
182 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
183 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
184 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
185
186 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
187 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
188 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
189 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
190 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
191 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
192 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
193 END_EVENT_TABLE()
194
195 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
196 const wxString& value,
197 const wxPoint& pos, const wxSize& size,
198 const wxArrayString& choices,
199 long style, const wxValidator& validator,
200 const wxString& name )
201 {
202 wxCArrayString chs(choices);
203
204 return Create( parent, id, value, pos, size, chs.GetCount(),
205 chs.GetStrings(), style, validator, name );
206 }
207
208 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
209 const wxPoint& pos, const wxSize& size,
210 int n, const wxString choices[],
211 long style, const wxValidator& validator,
212 const wxString& name )
213 {
214 m_ignoreNextUpdate = false;
215 m_needParent = true;
216 m_acceptsFocus = true;
217 m_prevSelection = 0;
218
219 if (!PreCreation( parent, pos, size ) ||
220 !CreateBase( parent, id, pos, size, style, validator, name ))
221 {
222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
223 return false;
224 }
225
226 m_widget = gtk_combo_new();
227 GtkCombo *combo = GTK_COMBO(m_widget);
228
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
231 // ... and add surrogate handler.
232 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
233 (GtkSignalFunc) gtk_dummy_callback, combo);
234
235 // make it more useable
236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
237
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
240
241 if (style & wxNO_BORDER)
242 g_object_set( GTK_ENTRY( combo->entry ), "has-frame", FALSE, NULL );
243
244 GtkWidget *list = GTK_COMBO(m_widget)->list;
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 SetBestSize(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
295 wxComboBox::~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
309 void 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
320 int wxComboBox::DoAppend( const wxString &item )
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 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
329
330 gtk_container_add( GTK_CONTAINER(list), list_item );
331
332 if (GTK_WIDGET_REALIZED(m_widget))
333 {
334 gtk_widget_realize( list_item );
335 gtk_widget_realize( GTK_BIN(list_item)->child );
336 }
337
338 // Apply current widget style to the new list_item
339 GtkRcStyle *style = CreateWidgetStyle();
340 if (style)
341 {
342 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
343 GtkBin *bin = GTK_BIN( list_item );
344 GtkWidget *label = GTK_WIDGET( bin->child );
345 gtk_widget_modify_style( label, style );
346 gtk_rc_style_unref( style );
347 }
348
349 gtk_widget_show( list_item );
350
351 const int count = GetCount();
352
353 if ( (int)m_clientDataList.GetCount() < count )
354 m_clientDataList.Append( (wxObject*) NULL );
355 if ( (int)m_clientObjectList.GetCount() < count )
356 m_clientObjectList.Append( (wxObject*) NULL );
357
358 EnableEvents();
359
360 InvalidateBestSize();
361
362 return count - 1;
363 }
364
365 int wxComboBox::DoInsert( const wxString &item, int pos )
366 {
367 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
368 wxT("can't insert into sorted list"));
369
370 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
371
372 int count = GetCount();
373 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
374
375 if (pos == count)
376 return Append(item);
377
378 DisableEvents();
379
380 GtkWidget *list = GTK_COMBO(m_widget)->list;
381
382 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
383
384 GList *gitem_list = g_list_alloc ();
385 gitem_list->data = list_item;
386 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
387
388 if (GTK_WIDGET_REALIZED(m_widget))
389 {
390 gtk_widget_realize( list_item );
391 gtk_widget_realize( GTK_BIN(list_item)->child );
392
393 ApplyWidgetStyle();
394 }
395
396 gtk_widget_show( list_item );
397
398 count = GetCount();
399
400 if ( (int)m_clientDataList.GetCount() < count )
401 m_clientDataList.Insert( pos, (wxObject*) NULL );
402 if ( (int)m_clientObjectList.GetCount() < count )
403 m_clientObjectList.Insert( pos, (wxObject*) NULL );
404
405 EnableEvents();
406
407 InvalidateBestSize();
408
409 return pos;
410 }
411
412 void wxComboBox::DoSetItemClientData( int n, void* clientData )
413 {
414 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
415
416 wxList::compatibility_iterator node = m_clientDataList.Item( n );
417 if (!node) return;
418
419 node->SetData( (wxObject*) clientData );
420 }
421
422 void* wxComboBox::DoGetItemClientData( int n ) const
423 {
424 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
425
426 wxList::compatibility_iterator node = m_clientDataList.Item( n );
427
428 return node ? node->GetData() : NULL;
429 }
430
431 void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
432 {
433 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
434
435 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
436 if (!node) return;
437
438 // wxItemContainer already deletes data for us
439
440 node->SetData( (wxObject*) clientData );
441 }
442
443 wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
444 {
445 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
446
447 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
448
449 return node ? (wxClientData*) node->GetData() : NULL;
450 }
451
452 void wxComboBox::Clear()
453 {
454 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
455
456 DisableEvents();
457
458 GtkWidget *list = GTK_COMBO(m_widget)->list;
459 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
460
461 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
462 while (node)
463 {
464 wxClientData *cd = (wxClientData*)node->GetData();
465 if (cd) delete cd;
466 node = node->GetNext();
467 }
468 m_clientObjectList.Clear();
469
470 m_clientDataList.Clear();
471
472 EnableEvents();
473
474 InvalidateBestSize();
475 }
476
477 void wxComboBox::Delete( int n )
478 {
479 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
480
481 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
482
483 GList *child = g_list_nth( listbox->children, n );
484
485 if (!child)
486 {
487 wxFAIL_MSG(wxT("wrong index"));
488 return;
489 }
490
491 DisableEvents();
492
493 GList *list = g_list_append( (GList*) NULL, child->data );
494 gtk_list_remove_items( listbox, list );
495 g_list_free( list );
496
497 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
498 if (node)
499 {
500 wxClientData *cd = (wxClientData*)node->GetData();
501 if (cd) delete cd;
502 m_clientObjectList.Erase( node );
503 }
504
505 node = m_clientDataList.Item( n );
506 if (node)
507 m_clientDataList.Erase( node );
508
509 EnableEvents();
510
511 InvalidateBestSize();
512 }
513
514 void wxComboBox::SetString(int n, const wxString &text)
515 {
516 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
517
518 GtkWidget *list = GTK_COMBO(m_widget)->list;
519
520 GList *child = g_list_nth( GTK_LIST(list)->children, n );
521 if (child)
522 {
523 GtkBin *bin = GTK_BIN( child->data );
524 GtkLabel *label = GTK_LABEL( bin->child );
525 gtk_label_set_text(label, wxGTK_CONV(text));
526 }
527 else
528 {
529 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
530 }
531
532 InvalidateBestSize();
533 }
534
535 int wxComboBox::FindString( const wxString &item, bool bCase ) const
536 {
537 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
538
539 GtkWidget *list = GTK_COMBO(m_widget)->list;
540
541 GList *child = GTK_LIST(list)->children;
542 int count = 0;
543 while (child)
544 {
545 GtkBin *bin = GTK_BIN( child->data );
546 GtkLabel *label = GTK_LABEL( bin->child );
547 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
548
549 if (item.IsSameAs( str , bCase ) )
550 return count;
551
552 count++;
553 child = child->next;
554 }
555
556 return wxNOT_FOUND;
557 }
558
559 int wxComboBox::GetSelection() const
560 {
561 // if the popup is currently opened, use the selection as it had been
562 // before it dropped down
563 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
564 : g_SelectionBeforePopup;
565 }
566
567 int wxComboBox::GetCurrentSelection() const
568 {
569 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
570
571 GtkWidget *list = GTK_COMBO(m_widget)->list;
572
573 GList *selection = GTK_LIST(list)->selection;
574 if (selection)
575 {
576 GList *child = GTK_LIST(list)->children;
577 int count = 0;
578 while (child)
579 {
580 if (child->data == selection->data) return count;
581 count++;
582 child = child->next;
583 }
584 }
585
586 return -1;
587 }
588
589 wxString wxComboBox::GetString( int n ) const
590 {
591 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
592
593 GtkWidget *list = GTK_COMBO(m_widget)->list;
594
595 wxString str;
596 GList *child = g_list_nth( GTK_LIST(list)->children, n );
597 if (child)
598 {
599 GtkBin *bin = GTK_BIN( child->data );
600 GtkLabel *label = GTK_LABEL( bin->child );
601 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
602 }
603 else
604 {
605 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
606 }
607
608 return str;
609 }
610
611 wxString wxComboBox::GetStringSelection() const
612 {
613 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
614
615 GtkWidget *list = GTK_COMBO(m_widget)->list;
616
617 GList *selection = GTK_LIST(list)->selection;
618 if (selection)
619 {
620 GtkBin *bin = GTK_BIN( selection->data );
621 GtkLabel *label = GTK_LABEL( bin->child );
622 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
623 return tmp;
624 }
625
626 wxFAIL_MSG( wxT("wxComboBox: no selection") );
627
628 return wxEmptyString;
629 }
630
631 int wxComboBox::GetCount() const
632 {
633 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
634
635 GtkWidget *list = GTK_COMBO(m_widget)->list;
636
637 GList *child = GTK_LIST(list)->children;
638 int count = 0;
639 while (child) { count++; child = child->next; }
640 return count;
641 }
642
643 void wxComboBox::SetSelection( int n )
644 {
645 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
646
647 DisableEvents();
648
649 GtkWidget *list = GTK_COMBO(m_widget)->list;
650 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
651 gtk_list_select_item( GTK_LIST(list), n );
652 m_prevSelection = n;
653
654 EnableEvents();
655 }
656
657 wxString wxComboBox::GetValue() const
658 {
659 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
660 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
661
662 #if 0
663 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
664 {
665 wxChar c = tmp[i];
666 printf( "%d ", (int) (c) );
667 }
668 printf( "\n" );
669 #endif
670
671 return tmp;
672 }
673
674 void wxComboBox::SetValue( const wxString& value )
675 {
676 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
677
678 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
679 wxString tmp;
680 if (!value.IsNull()) tmp = value;
681 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
682
683 InvalidateBestSize();
684 }
685
686 void wxComboBox::Copy()
687 {
688 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
689
690 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
691 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
692 }
693
694 void wxComboBox::Cut()
695 {
696 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
697
698 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
699 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
700 }
701
702 void wxComboBox::Paste()
703 {
704 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
705
706 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
707 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
708 }
709
710 void wxComboBox::Undo()
711 {
712 // TODO
713 }
714
715 void wxComboBox::Redo()
716 {
717 // TODO
718 }
719
720 void wxComboBox::SelectAll()
721 {
722 SetSelection(0, GetLastPosition());
723 }
724
725 bool wxComboBox::CanUndo() const
726 {
727 // TODO
728 return false;
729 }
730
731 bool wxComboBox::CanRedo() const
732 {
733 // TODO
734 return false;
735 }
736
737 bool wxComboBox::HasSelection() const
738 {
739 long from, to;
740 GetSelection(&from, &to);
741 return from != to;
742 }
743
744 bool wxComboBox::CanCopy() const
745 {
746 // Can copy if there's a selection
747 return HasSelection();
748 }
749
750 bool wxComboBox::CanCut() const
751 {
752 return CanCopy() && IsEditable();
753 }
754
755 bool wxComboBox::CanPaste() const
756 {
757 // TODO: check for text on the clipboard
758 return IsEditable() ;
759 }
760
761 bool wxComboBox::IsEditable() const
762 {
763 return !HasFlag(wxCB_READONLY);
764 }
765
766
767 void wxComboBox::SetInsertionPoint( long pos )
768 {
769 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
770
771 if ( pos == GetLastPosition() )
772 pos = -1;
773
774 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
775 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
776 }
777
778 long wxComboBox::GetInsertionPoint() const
779 {
780 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
781 }
782
783 wxTextPos wxComboBox::GetLastPosition() const
784 {
785 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
786 int pos = GTK_ENTRY(entry)->text_length;
787 return (long) pos-1;
788 }
789
790 void wxComboBox::Replace( long from, long to, const wxString& value )
791 {
792 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
793
794 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
795 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
796 if (value.IsNull()) return;
797 gint pos = (gint)to;
798
799 #if wxUSE_UNICODE
800 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
801 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
802 #else
803 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
804 #endif
805 }
806
807 void wxComboBox::SetSelection( long from, long to )
808 {
809 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
810 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
811 }
812
813 void wxComboBox::GetSelection( long* from, long* to ) const
814 {
815 if (IsEditable())
816 {
817 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
818 gint start, end;
819 gtk_editable_get_selection_bounds(editable, & start, & end);
820 *from = start;
821 *to = end;
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 eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
837 eventEnter.SetString( GetValue() );
838 eventEnter.SetInt( GetSelection() );
839 eventEnter.SetEventObject( this );
840
841 if (!GetEventHandler()->ProcessEvent( eventEnter ))
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_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
878 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
879 gtk_signal_connect_after( 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