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