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