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