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