]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
wxX11 compilation fixes.
[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_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 bool wxComboBox::SetStringSelection( const wxString &string )
648 {
649 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") );
650
651 int res = FindString( string );
652 if (res == -1) return false;
653 SetSelection( res );
654 return true;
655 }
656
657 wxString wxComboBox::GetValue() const
658 {
659 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
660 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
661
662 #if 0
663 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
664 {
665 wxChar c = tmp[i];
666 printf( "%d ", (int) (c) );
667 }
668 printf( "\n" );
669 #endif
670
671 return tmp;
672 }
673
674 void wxComboBox::SetValue( const wxString& value )
675 {
676 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
677
678 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
679 wxString tmp = wxT("");
680 if (!value.IsNull()) tmp = value;
681 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
682
683 InvalidateBestSize();
684 }
685
686 void wxComboBox::Copy()
687 {
688 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
689
690 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
691 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
692 }
693
694 void wxComboBox::Cut()
695 {
696 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
697
698 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
699 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
700 }
701
702 void wxComboBox::Paste()
703 {
704 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
705
706 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
707 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
708 }
709
710 void wxComboBox::Undo()
711 {
712 // TODO
713 }
714
715 void wxComboBox::Redo()
716 {
717 // TODO
718 }
719
720 void wxComboBox::SelectAll()
721 {
722 SetSelection(0, GetLastPosition());
723 }
724
725 bool wxComboBox::CanUndo() const
726 {
727 // TODO
728 return false;
729 }
730
731 bool wxComboBox::CanRedo() const
732 {
733 // TODO
734 return false;
735 }
736
737 bool wxComboBox::HasSelection() const
738 {
739 long from, to;
740 GetSelection(&from, &to);
741 return from != to;
742 }
743
744 bool wxComboBox::CanCopy() const
745 {
746 // Can copy if there's a selection
747 return HasSelection();
748 }
749
750 bool wxComboBox::CanCut() const
751 {
752 return CanCopy() && IsEditable();
753 }
754
755 bool wxComboBox::CanPaste() const
756 {
757 // TODO: check for text on the clipboard
758 return IsEditable() ;
759 }
760
761 bool wxComboBox::IsEditable() const
762 {
763 return !HasFlag(wxCB_READONLY);
764 }
765
766
767 void wxComboBox::SetInsertionPoint( long pos )
768 {
769 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
770
771 if ( pos == GetLastPosition() )
772 pos = -1;
773
774 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
775 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
776 }
777
778 long wxComboBox::GetInsertionPoint() const
779 {
780 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
781 }
782
783 wxTextPos wxComboBox::GetLastPosition() const
784 {
785 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
786 int pos = GTK_ENTRY(entry)->text_length;
787 return (long) pos-1;
788 }
789
790 void wxComboBox::Replace( long from, long to, const wxString& value )
791 {
792 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
793
794 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
795 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
796 if (value.IsNull()) return;
797 gint pos = (gint)to;
798
799 #if wxUSE_UNICODE
800 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
801 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
802 #else
803 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos );
804 #endif
805 }
806
807 void wxComboBox::SetSelection( long from, long to )
808 {
809 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
810 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
811 }
812
813 void wxComboBox::GetSelection( long* from, long* to ) const
814 {
815 if (IsEditable())
816 {
817 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
818 #ifdef __WXGTK20__
819 gint start, end;
820 gtk_editable_get_selection_bounds(editable, & start, & end);
821 *from = start;
822 *to = end;
823 #else
824 *from = (long) editable->selection_start_pos;
825 *to = (long) editable->selection_end_pos;
826 #endif
827 }
828 }
829
830 void wxComboBox::SetEditable( bool editable )
831 {
832 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
833 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
834 }
835
836 void wxComboBox::OnChar( wxKeyEvent &event )
837 {
838 if ( event.GetKeyCode() == WXK_RETURN )
839 {
840 // GTK automatically selects an item if its in the list
841 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
842 event.SetString( GetValue() );
843 event.SetInt( GetSelection() );
844 event.SetEventObject( this );
845
846 if (!GetEventHandler()->ProcessEvent( event ))
847 {
848 // This will invoke the dialog default action, such
849 // as the clicking the default button.
850
851 wxWindow *top_frame = m_parent;
852 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
853 top_frame = top_frame->GetParent();
854
855 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
856 {
857 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
858
859 if (window->default_widget)
860 gtk_widget_activate (window->default_widget);
861 }
862 }
863
864 // Catch GTK event so that GTK doesn't open the drop
865 // down list upon RETURN.
866 return;
867 }
868
869 event.Skip();
870 }
871
872 void wxComboBox::DisableEvents()
873 {
874 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
875 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
876 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
877 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
878 }
879
880 void wxComboBox::EnableEvents()
881 {
882 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
883 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
884 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
885 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
886 }
887
888 void wxComboBox::OnSize( wxSizeEvent &event )
889 {
890 // NB: In some situations (e.g. on non-first page of a wizard, if the
891 // size used is default size), GtkCombo widget is resized correctly,
892 // but it's look is not updated, it's rendered as if it was much wider.
893 // No other widgets are affected, so it looks like a bug in GTK+.
894 // Manually requesting resize calculation (as gtk_pizza_set_size does)
895 // fixes it.
896 if (GTK_WIDGET_VISIBLE(m_widget))
897 gtk_widget_queue_resize(m_widget);
898
899 event.Skip();
900 }
901
902 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
903 {
904 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
905
906 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
907 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
908
909 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
910 GList *child = list->children;
911 while (child)
912 {
913 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
914
915 GtkBin *bin = GTK_BIN(child->data);
916 gtk_widget_modify_style( bin->child, style );
917
918 child = child->next;
919 }
920 }
921
922 GtkWidget* wxComboBox::GetConnectWidget()
923 {
924 return GTK_COMBO(m_widget)->entry;
925 }
926
927 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
928 {
929 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
930 (window == GTK_COMBO(m_widget)->button->window ) );
931 }
932
933 wxSize wxComboBox::DoGetBestSize() const
934 {
935 wxSize ret( wxControl::DoGetBestSize() );
936
937 // we know better our horizontal extent: it depends on the longest string
938 // in the combobox
939 if ( m_widget )
940 {
941 int width;
942 size_t count = GetCount();
943 for ( size_t n = 0; n < count; n++ )
944 {
945 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
946 if ( width > ret.x )
947 ret.x = width;
948 }
949 }
950
951 // empty combobox should have some reasonable default size too
952 if ( ret.x < 100 )
953 ret.x = 100;
954
955 CacheBestSize(ret);
956 return ret;
957 }
958
959 // static
960 wxVisualAttributes
961 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
962 {
963 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
964 }
965
966 // ----------------------------------------------------------------------------
967 // standard event handling
968 // ----------------------------------------------------------------------------
969
970 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
971 {
972 Cut();
973 }
974
975 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
976 {
977 Copy();
978 }
979
980 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
981 {
982 Paste();
983 }
984
985 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
986 {
987 Undo();
988 }
989
990 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
991 {
992 Redo();
993 }
994
995 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
996 {
997 long from, to;
998 GetSelection(& from, & to);
999 if (from != -1 && to != -1)
1000 Remove(from, to);
1001 }
1002
1003 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1004 {
1005 SetSelection(-1, -1);
1006 }
1007
1008 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1009 {
1010 event.Enable( CanCut() );
1011 }
1012
1013 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1014 {
1015 event.Enable( CanCopy() );
1016 }
1017
1018 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1019 {
1020 event.Enable( CanPaste() );
1021 }
1022
1023 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1024 {
1025 event.Enable( CanUndo() );
1026 }
1027
1028 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1029 {
1030 event.Enable( CanRedo() );
1031 }
1032
1033 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1034 {
1035 event.Enable(HasSelection() && IsEditable()) ;
1036 }
1037
1038 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1039 {
1040 event.Enable(GetLastPosition() > 0);
1041 }
1042
1043 #endif
1044