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