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