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