Applied wxNO_BORDER patch for text control and combo box.
[wxWidgets.git] / src / gtk / 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( 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 if (style & wxNO_BORDER)
223 g_object_set( GTK_ENTRY( combo->entry ), "has-frame", FALSE, NULL );
224
225 GtkWidget *list = GTK_COMBO(m_widget)->list;
226
227 #ifndef __WXGTK20__
228 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
229 #endif
230
231 for (int i = 0; i < n; i++)
232 {
233 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
234
235 m_clientDataList.Append( (wxObject*)NULL );
236 m_clientObjectList.Append( (wxObject*)NULL );
237
238 gtk_container_add( GTK_CONTAINER(list), list_item );
239
240 gtk_widget_show( list_item );
241 }
242
243 m_parent->DoAddChild( this );
244
245 m_focusWidget = combo->entry;
246
247 PostCreation(size);
248
249 ConnectWidget( combo->button );
250
251 // MSW's combo box shows the value and the selection is -1
252 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
253 gtk_list_unselect_all( GTK_LIST(combo->list) );
254
255 if (style & wxCB_READONLY)
256 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
257
258 // "show" and "hide" events are generated when user click on the combobox button which popups a list
259 // this list is the "popwin" gtk widget
260 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
261 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
262 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
263 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
264
265 gtk_signal_connect( GTK_OBJECT(combo->entry), "changed",
266 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
267
268 gtk_signal_connect( GTK_OBJECT(combo->list), "select-child",
269 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
270
271 SetBestSize(size); // need this too because this is a wxControlWithItems
272
273 // This is required for tool bar support
274 wxSize setsize = GetSize();
275 gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
276
277 return TRUE;
278 }
279
280 wxComboBox::~wxComboBox()
281 {
282 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
283 while (node)
284 {
285 wxClientData *cd = (wxClientData*)node->GetData();
286 if (cd) delete cd;
287 node = node->GetNext();
288 }
289 m_clientObjectList.Clear();
290
291 m_clientDataList.Clear();
292 }
293
294 void wxComboBox::SetFocus()
295 {
296 if ( m_hasFocus )
297 {
298 // don't do anything if we already have focus
299 return;
300 }
301
302 gtk_widget_grab_focus( m_focusWidget );
303 }
304
305 int wxComboBox::DoAppend( const wxString &item )
306 {
307 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
308
309 DisableEvents();
310
311 GtkWidget *list = GTK_COMBO(m_widget)->list;
312
313 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
314
315 gtk_container_add( GTK_CONTAINER(list), list_item );
316
317 if (GTK_WIDGET_REALIZED(m_widget))
318 {
319 gtk_widget_realize( list_item );
320 gtk_widget_realize( GTK_BIN(list_item)->child );
321 }
322
323 // Apply current widget style to the new list_item
324 GtkRcStyle *style = CreateWidgetStyle();
325 if (style)
326 {
327 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
328 GtkBin *bin = GTK_BIN( list_item );
329 GtkWidget *label = GTK_WIDGET( bin->child );
330 gtk_widget_modify_style( label, style );
331 gtk_rc_style_unref( style );
332 }
333
334 gtk_widget_show( list_item );
335
336 const int count = GetCount();
337
338 if ( (int)m_clientDataList.GetCount() < count )
339 m_clientDataList.Append( (wxObject*) NULL );
340 if ( (int)m_clientObjectList.GetCount() < count )
341 m_clientObjectList.Append( (wxObject*) NULL );
342
343 EnableEvents();
344
345 InvalidateBestSize();
346
347 return count - 1;
348 }
349
350 int wxComboBox::DoInsert( const wxString &item, int pos )
351 {
352 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
353 wxT("can't insert into sorted list"));
354
355 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
356
357 int count = GetCount();
358 wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") );
359
360 if (pos == count)
361 return Append(item);
362
363 DisableEvents();
364
365 GtkWidget *list = GTK_COMBO(m_widget)->list;
366
367 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
368
369 GList *gitem_list = g_list_alloc ();
370 gitem_list->data = list_item;
371 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
372
373 if (GTK_WIDGET_REALIZED(m_widget))
374 {
375 gtk_widget_realize( list_item );
376 gtk_widget_realize( GTK_BIN(list_item)->child );
377
378 ApplyWidgetStyle();
379 }
380
381 gtk_widget_show( list_item );
382
383 count = GetCount();
384
385 if ( (int)m_clientDataList.GetCount() < count )
386 m_clientDataList.Insert( pos, (wxObject*) NULL );
387 if ( (int)m_clientObjectList.GetCount() < count )
388 m_clientObjectList.Insert( pos, (wxObject*) NULL );
389
390 EnableEvents();
391
392 InvalidateBestSize();
393
394 return pos;
395 }
396
397 void wxComboBox::DoSetItemClientData( int n, void* clientData )
398 {
399 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
400
401 wxList::compatibility_iterator node = m_clientDataList.Item( n );
402 if (!node) return;
403
404 node->SetData( (wxObject*) clientData );
405 }
406
407 void* wxComboBox::DoGetItemClientData( int n ) const
408 {
409 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
410
411 wxList::compatibility_iterator node = m_clientDataList.Item( n );
412
413 return node ? node->GetData() : NULL;
414 }
415
416 void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData )
417 {
418 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
419
420 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
421 if (!node) return;
422
423 // wxItemContainer already deletes data for us
424
425 node->SetData( (wxObject*) clientData );
426 }
427
428 wxClientData* wxComboBox::DoGetItemClientObject( int n ) const
429 {
430 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
431
432 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
433
434 return node ? (wxClientData*) node->GetData() : NULL;
435 }
436
437 void wxComboBox::Clear()
438 {
439 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
440
441 DisableEvents();
442
443 GtkWidget *list = GTK_COMBO(m_widget)->list;
444 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
445
446 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
447 while (node)
448 {
449 wxClientData *cd = (wxClientData*)node->GetData();
450 if (cd) delete cd;
451 node = node->GetNext();
452 }
453 m_clientObjectList.Clear();
454
455 m_clientDataList.Clear();
456
457 EnableEvents();
458
459 InvalidateBestSize();
460 }
461
462 void wxComboBox::Delete( int n )
463 {
464 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
465
466 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
467
468 GList *child = g_list_nth( listbox->children, n );
469
470 if (!child)
471 {
472 wxFAIL_MSG(wxT("wrong index"));
473 return;
474 }
475
476 DisableEvents();
477
478 GList *list = g_list_append( (GList*) NULL, child->data );
479 gtk_list_remove_items( listbox, list );
480 g_list_free( list );
481
482 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
483 if (node)
484 {
485 wxClientData *cd = (wxClientData*)node->GetData();
486 if (cd) delete cd;
487 m_clientObjectList.Erase( node );
488 }
489
490 node = m_clientDataList.Item( n );
491 if (node)
492 m_clientDataList.Erase( node );
493
494 EnableEvents();
495
496 InvalidateBestSize();
497 }
498
499 void wxComboBox::SetString(int n, const wxString &text)
500 {
501 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
502
503 GtkWidget *list = GTK_COMBO(m_widget)->list;
504
505 GList *child = g_list_nth( GTK_LIST(list)->children, n );
506 if (child)
507 {
508 GtkBin *bin = GTK_BIN( child->data );
509 GtkLabel *label = GTK_LABEL( bin->child );
510 gtk_label_set_text(label, wxGTK_CONV(text));
511 }
512 else
513 {
514 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
515 }
516
517 InvalidateBestSize();
518 }
519
520 int wxComboBox::FindString( const wxString &item ) const
521 {
522 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
523
524 GtkWidget *list = GTK_COMBO(m_widget)->list;
525
526 GList *child = GTK_LIST(list)->children;
527 int count = 0;
528 while (child)
529 {
530 GtkBin *bin = GTK_BIN( child->data );
531 GtkLabel *label = GTK_LABEL( bin->child );
532 #ifdef __WXGTK20__
533 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
534 #else
535 wxString str( label->label );
536 #endif
537 if (item == str)
538 return count;
539
540 count++;
541 child = child->next;
542 }
543
544 return wxNOT_FOUND;
545 }
546
547 int wxComboBox::GetSelection() const
548 {
549 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
550
551 GtkWidget *list = GTK_COMBO(m_widget)->list;
552
553 GList *selection = GTK_LIST(list)->selection;
554 if (selection)
555 {
556 GList *child = GTK_LIST(list)->children;
557 int count = 0;
558 while (child)
559 {
560 if (child->data == selection->data) return count;
561 count++;
562 child = child->next;
563 }
564 }
565
566 return -1;
567 }
568
569 wxString wxComboBox::GetString( int n ) const
570 {
571 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
572
573 GtkWidget *list = GTK_COMBO(m_widget)->list;
574
575 wxString str;
576 GList *child = g_list_nth( GTK_LIST(list)->children, n );
577 if (child)
578 {
579 GtkBin *bin = GTK_BIN( child->data );
580 GtkLabel *label = GTK_LABEL( bin->child );
581 #ifdef __WXGTK20__
582 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
583 #else
584 str = wxString( label->label );
585 #endif
586 }
587 else
588 {
589 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
590 }
591
592 return str;
593 }
594
595 wxString wxComboBox::GetStringSelection() const
596 {
597 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") );
598
599 GtkWidget *list = GTK_COMBO(m_widget)->list;
600
601 GList *selection = GTK_LIST(list)->selection;
602 if (selection)
603 {
604 GtkBin *bin = GTK_BIN( selection->data );
605 GtkLabel *label = GTK_LABEL( bin->child );
606 #ifdef __WXGTK20__
607 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
608 #else
609 wxString tmp( label->label );
610 #endif
611 return tmp;
612 }
613
614 wxFAIL_MSG( wxT("wxComboBox: no selection") );
615
616 return wxT("");
617 }
618
619 int wxComboBox::GetCount() const
620 {
621 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
622
623 GtkWidget *list = GTK_COMBO(m_widget)->list;
624
625 GList *child = GTK_LIST(list)->children;
626 int count = 0;
627 while (child) { count++; child = child->next; }
628 return count;
629 }
630
631 void wxComboBox::SetSelection( int n )
632 {
633 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
634
635 DisableEvents();
636
637 GtkWidget *list = GTK_COMBO(m_widget)->list;
638 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
639 gtk_list_select_item( GTK_LIST(list), n );
640 m_prevSelection = n;
641
642 EnableEvents();
643 }
644
645 bool wxComboBox::SetStringSelection( const wxString &string )
646 {
647 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") );
648
649 int res = FindString( string );
650 if (res == -1) return false;
651 SetSelection( res );
652 return true;
653 }
654
655 wxString wxComboBox::GetValue() const
656 {
657 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
658 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
659
660 #if 0
661 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
662 {
663 wxChar c = tmp[i];
664 printf( "%d ", (int) (c) );
665 }
666 printf( "\n" );
667 #endif
668
669 return tmp;
670 }
671
672 void wxComboBox::SetValue( const wxString& value )
673 {
674 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
675
676 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
677 wxString tmp = wxT("");
678 if (!value.IsNull()) tmp = value;
679 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
680
681 InvalidateBestSize();
682 }
683
684 void wxComboBox::Copy()
685 {
686 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
687
688 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
689 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
690 }
691
692 void wxComboBox::Cut()
693 {
694 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
695
696 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
697 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
698 }
699
700 void wxComboBox::Paste()
701 {
702 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
703
704 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
705 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
706 }
707
708 void wxComboBox::Undo()
709 {
710 // TODO
711 }
712
713 void wxComboBox::Redo()
714 {
715 // TODO
716 }
717
718 void wxComboBox::SelectAll()
719 {
720 SetSelection(0, GetLastPosition());
721 }
722
723 bool wxComboBox::CanUndo() const
724 {
725 // TODO
726 return false;
727 }
728
729 bool wxComboBox::CanRedo() const
730 {
731 // TODO
732 return false;
733 }
734
735 bool wxComboBox::HasSelection() const
736 {
737 long from, to;
738 GetSelection(&from, &to);
739 return from != to;
740 }
741
742 bool wxComboBox::CanCopy() const
743 {
744 // Can copy if there's a selection
745 return HasSelection();
746 }
747
748 bool wxComboBox::CanCut() const
749 {
750 return CanCopy() && IsEditable();
751 }
752
753 bool wxComboBox::CanPaste() const
754 {
755 // TODO: check for text on the clipboard
756 return IsEditable() ;
757 }
758
759 bool wxComboBox::IsEditable() const
760 {
761 return !HasFlag(wxCB_READONLY);
762 }
763
764
765 void wxComboBox::SetInsertionPoint( long pos )
766 {
767 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
768
769 if ( pos == GetLastPosition() )
770 pos = -1;
771
772 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
773 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
774 }
775
776 long wxComboBox::GetInsertionPoint() const
777 {
778 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
779 }
780
781 long wxComboBox::GetLastPosition() const
782 {
783 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
784 int pos = GTK_ENTRY(entry)->text_length;
785 return (long) pos-1;
786 }
787
788 void wxComboBox::Replace( long from, long to, const wxString& value )
789 {
790 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
791
792 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
793 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
794 if (value.IsNull()) return;
795 gint pos = (gint)to;
796
797 #if wxUSE_UNICODE
798 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
799 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
800 #else
801 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
802 #endif
803 }
804
805 void wxComboBox::SetSelection( long from, long to )
806 {
807 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
808 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
809 }
810
811 void wxComboBox::GetSelection( long* from, long* to ) const
812 {
813 if (IsEditable())
814 {
815 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
816 #ifdef __WXGTK20__
817 gint start, end;
818 gtk_editable_get_selection_bounds(editable, & start, & end);
819 *from = start;
820 *to = end;
821 #else
822 *from = (long) editable->selection_start_pos;
823 *to = (long) editable->selection_end_pos;
824 #endif
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 event(wxEVT_COMMAND_TEXT_ENTER, GetId());
840 event.SetString( GetValue() );
841 event.SetInt( GetSelection() );
842 event.SetEventObject( this );
843
844 if (!GetEventHandler()->ProcessEvent( event ))
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 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
873 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
874 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
875 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
876 }
877
878 void wxComboBox::EnableEvents()
879 {
880 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
881 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
882 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
883 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
884 }
885
886 void wxComboBox::OnSize( wxSizeEvent &event )
887 {
888 // NB: In some situations (e.g. on non-first page of a wizard, if the
889 // size used is default size), GtkCombo widget is resized correctly,
890 // but it's look is not updated, it's rendered as if it was much wider.
891 // No other widgets are affected, so it looks like a bug in GTK+.
892 // Manually requesting resize calculation (as gtk_pizza_set_size does)
893 // fixes it.
894 if (GTK_WIDGET_VISIBLE(m_widget))
895 gtk_widget_queue_resize(m_widget);
896
897 event.Skip();
898 }
899
900 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
901 {
902 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
903
904 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
905 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
906
907 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
908 GList *child = list->children;
909 while (child)
910 {
911 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
912
913 GtkBin *bin = GTK_BIN(child->data);
914 gtk_widget_modify_style( bin->child, style );
915
916 child = child->next;
917 }
918 }
919
920 GtkWidget* wxComboBox::GetConnectWidget()
921 {
922 return GTK_COMBO(m_widget)->entry;
923 }
924
925 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
926 {
927 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
928 (window == GTK_COMBO(m_widget)->button->window ) );
929 }
930
931 wxSize wxComboBox::DoGetBestSize() const
932 {
933 wxSize ret( wxControl::DoGetBestSize() );
934
935 // we know better our horizontal extent: it depends on the longest string
936 // in the combobox
937 if ( m_widget )
938 {
939 int width;
940 size_t count = GetCount();
941 for ( size_t n = 0; n < count; n++ )
942 {
943 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
944 if ( width > ret.x )
945 ret.x = width;
946 }
947 }
948
949 // empty combobox should have some reasonable default size too
950 if ( ret.x < 100 )
951 ret.x = 100;
952
953 CacheBestSize(ret);
954 return ret;
955 }
956
957 // static
958 wxVisualAttributes
959 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
960 {
961 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
962 }
963
964 // ----------------------------------------------------------------------------
965 // standard event handling
966 // ----------------------------------------------------------------------------
967
968 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
969 {
970 Cut();
971 }
972
973 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
974 {
975 Copy();
976 }
977
978 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
979 {
980 Paste();
981 }
982
983 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
984 {
985 Undo();
986 }
987
988 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
989 {
990 Redo();
991 }
992
993 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
994 {
995 long from, to;
996 GetSelection(& from, & to);
997 if (from != -1 && to != -1)
998 Remove(from, to);
999 }
1000
1001 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1002 {
1003 SetSelection(-1, -1);
1004 }
1005
1006 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1007 {
1008 event.Enable( CanCut() );
1009 }
1010
1011 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1012 {
1013 event.Enable( CanCopy() );
1014 }
1015
1016 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1017 {
1018 event.Enable( CanPaste() );
1019 }
1020
1021 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1022 {
1023 event.Enable( CanUndo() );
1024 }
1025
1026 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1027 {
1028 event.Enable( CanRedo() );
1029 }
1030
1031 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1032 {
1033 event.Enable(HasSelection() && IsEditable()) ;
1034 }
1035
1036 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1037 {
1038 event.Enable(GetLastPosition() > 0);
1039 }
1040
1041 #endif
1042