1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "listbox.h"
15 #include "wx/dynarray.h"
16 #include "wx/listbox.h"
19 #include "wx/checklst.h"
22 #include "wx/tooltip.h"
25 #if wxUSE_DRAG_AND_DROP
32 //-------------------------------------------------------------------------
33 // conditional compilation
34 //-------------------------------------------------------------------------
36 #if (GTK_MINOR_VERSION > 0)
37 #define NEW_GTK_SCROLL_CODE
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 #define CHECKBOX_STRING "[-] "
46 // checklistboxes have "[±] " prepended to their lables, this macro removes it
47 // (NB: 4 below is the length of CHECKBOX_STRING above)
49 // the argument to it is a "const char *" pointer
50 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 extern bool g_blockEventsOnDrag
;
57 extern bool g_blockEventsOnScroll
;
59 //-----------------------------------------------------------------------------
60 // "button_press_event"
61 //-----------------------------------------------------------------------------
64 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
66 if (g_blockEventsOnDrag
) return FALSE
;
67 if (g_blockEventsOnScroll
) return FALSE
;
69 if (!listbox
->HasVMT()) return FALSE
;
71 int sel
= listbox
->GetIndex( widget
);
73 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
75 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
77 clb
->Check( sel
, !clb
->IsChecked(sel
) );
79 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
80 event
.SetEventObject( listbox
);
82 listbox
->GetEventHandler()->ProcessEvent( event
);
85 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
87 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
88 event
.SetEventObject( listbox
);
90 wxArrayInt aSelections
;
91 int count
= listbox
->GetSelections(aSelections
);
94 event
.m_commandInt
= aSelections
[0] ;
95 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
96 wxString
str(listbox
->GetString(event
.m_commandInt
));
97 if (!str
.IsEmpty()) event
.m_commandString
= str
;
101 event
.m_commandInt
= -1 ;
102 event
.m_commandString
.Empty();
105 listbox
->GetEventHandler()->ProcessEvent( event
);
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
117 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
119 if (g_blockEventsOnDrag
) return FALSE
;
121 if (!listbox
->HasVMT()) return FALSE
;
123 if (gdk_event
->keyval
!= ' ') return FALSE
;
125 int sel
= listbox
->GetIndex( widget
);
127 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
129 clb
->Check( sel
, !clb
->IsChecked(sel
) );
131 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
132 event
.SetEventObject( listbox
);
134 listbox
->GetEventHandler()->ProcessEvent( event
);
139 //-----------------------------------------------------------------------------
140 // "select" and "deselect"
141 //-----------------------------------------------------------------------------
143 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
145 if (!listbox
->HasVMT()) return;
146 if (g_blockEventsOnDrag
) return;
148 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
150 wxArrayInt aSelections
;
151 int count
= listbox
->GetSelections(aSelections
);
154 event
.m_commandInt
= aSelections
[0] ;
155 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
156 wxString
str(listbox
->GetString(event
.m_commandInt
));
157 if (!str
.IsEmpty()) event
.m_commandString
= str
;
161 event
.m_commandInt
= -1 ;
162 event
.m_commandString
.Empty();
165 event
.SetEventObject( listbox
);
167 listbox
->GetEventHandler()->ProcessEvent( event
);
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
174 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
176 wxListBox::wxListBox()
178 m_list
= (GtkList
*) NULL
;
179 m_hasCheckBoxes
= FALSE
;
182 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
183 const wxPoint
&pos
, const wxSize
&size
,
184 int n
, const wxString choices
[],
185 long style
, const wxValidator
& validator
, const wxString
&name
)
188 m_acceptsFocus
= TRUE
;
190 PreCreation( parent
, id
, pos
, size
, style
, name
);
192 SetValidator( validator
);
194 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
195 if (style
& wxLB_ALWAYS_SB
)
197 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
198 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
202 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
203 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
206 m_list
= GTK_LIST( gtk_list_new() );
208 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
209 if (style
& wxLB_MULTIPLE
)
210 mode
= GTK_SELECTION_MULTIPLE
;
211 else if (style
& wxLB_EXTENDED
)
212 mode
= GTK_SELECTION_EXTENDED
;
214 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
216 #ifdef NEW_GTK_SCROLL_CODE
217 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
219 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
222 gtk_widget_show( GTK_WIDGET(m_list
) );
224 wxSize newSize
= size
;
225 if (newSize
.x
== -1) newSize
.x
= 100;
226 if (newSize
.y
== -1) newSize
.y
= 110;
227 SetSize( newSize
.x
, newSize
.y
);
229 for (int i
= 0; i
< n
; i
++)
231 m_clientDataList
.Append( (wxObject
*) NULL
);
232 m_clientObjectList
.Append( (wxObject
*) NULL
);
234 GtkWidget
*list_item
;
236 wxString
str(choices
[i
]);
239 str
.Prepend(CHECKBOX_STRING
);
242 list_item
= gtk_list_item_new_with_label( str
.mbc_str() );
244 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
246 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
247 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
249 if (style
& wxLB_MULTIPLE
)
250 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
251 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
253 gtk_signal_connect( GTK_OBJECT(list_item
),
254 "button_press_event",
255 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
260 gtk_signal_connect( GTK_OBJECT(list_item
),
262 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
266 ConnectWidget( list_item
);
268 gtk_widget_show( list_item
);
271 m_parent
->AddChild( this );
273 (m_parent
->m_insertCallback
)( m_parent
, this );
277 SetBackgroundColour( parent
->GetBackgroundColour() );
278 SetForegroundColour( parent
->GetForegroundColour() );
279 SetFont( parent
->GetFont() );
286 wxListBox::~wxListBox()
291 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
293 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
295 GList
*children
= m_list
->children
;
296 int length
= g_list_length(children
);
297 wxCHECK_RET( pos
<= length
, _T("invalid index in wxListBox::InsertItems") );
299 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
300 // into a listbox at the given position, this is why we first delete
301 // all items after this position, then append these items and then
302 // reappend back the old ones.
304 // first detach the old items
309 // no need to do anything complicated
310 for ( n
= 0; n
< nItems
; n
++ )
318 wxArrayString deletedLabels
;
319 wxArrayPtrVoid deletedData
;
320 wxArrayInt deletedChecks
; // only for check list boxes
322 GList
*child
= g_list_nth( children
, pos
);
323 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
326 GtkBin
*bin
= GTK_BIN( child
->data
);
327 GtkLabel
*label
= GTK_LABEL( bin
->child
);
329 wxString
str(GET_REAL_LABEL(label
->label
));
330 deletedLabels
.Add(str
);
333 void *clientData
= NULL
;
336 if ( n
< (int)m_clientObjectList
.GetCount() )
337 node
= m_clientObjectList
.Nth( n
);
341 clientData
= node
->GetData();
342 m_clientObjectList
.DeleteNode( node
);
347 if ( n
< (int)m_clientDataList
.GetCount() )
348 node
= m_clientDataList
.Nth( n
);
352 clientData
= node
->GetData();
353 node
= m_clientDataList
.Nth( n
);
357 deletedData
.Add(clientData
);
360 if ( m_hasCheckBoxes
)
362 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
366 int nDeletedCount
= n
;
368 gtk_list_clear_items( m_list
, pos
, length
);
370 // now append the new items
371 for ( n
= 0; n
< nItems
; n
++ )
376 // and append the old items too
377 pos
+= nItems
; // now the indices are shifter
378 for ( n
= 0; n
< nDeletedCount
; n
++ )
380 Append(deletedLabels
[n
], deletedData
[n
]);
382 if ( m_hasCheckBoxes
)
384 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
389 void wxListBox::AppendCommon( const wxString
&item
)
391 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
393 GtkWidget
*list_item
;
395 wxString
label(item
);
398 label
.Prepend(CHECKBOX_STRING
);
401 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
403 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
405 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
406 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
408 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
409 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
410 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
412 gtk_signal_connect( GTK_OBJECT(list_item
),
413 "button_press_event",
414 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
419 gtk_signal_connect( GTK_OBJECT(list_item
),
421 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
425 gtk_widget_show( list_item
);
427 ConnectWidget( list_item
);
429 if (GTK_WIDGET_REALIZED(m_widget
))
431 gtk_widget_realize( list_item
);
432 gtk_widget_realize( GTK_BIN(list_item
)->child
);
434 if (m_widgetStyle
) ApplyWidgetStyle();
436 #if wxUSE_DRAG_AND_DROP
437 #ifndef NEW_GTK_DND_CODE
438 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
443 if (m_toolTip
) m_toolTip
->Apply( this );
448 void wxListBox::Append( const wxString
&item
)
450 m_clientDataList
.Append( (wxObject
*) NULL
);
451 m_clientObjectList
.Append( (wxObject
*) NULL
);
453 AppendCommon( item
);
456 void wxListBox::Append( const wxString
&item
, void *clientData
)
458 m_clientDataList
.Append( (wxObject
*) clientData
);
459 m_clientObjectList
.Append( (wxObject
*) NULL
);
461 AppendCommon( item
);
464 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
466 m_clientObjectList
.Append( (wxObject
*) clientData
);
467 m_clientDataList
.Append( (wxObject
*) NULL
);
469 AppendCommon( item
);
472 void wxListBox::SetClientData( int n
, void* clientData
)
474 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
476 wxNode
*node
= m_clientDataList
.Nth( n
);
479 node
->SetData( (wxObject
*) clientData
);
482 void* wxListBox::GetClientData( int n
)
484 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
486 wxNode
*node
= m_clientDataList
.Nth( n
);
487 if (!node
) return NULL
;
492 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
494 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
496 wxNode
*node
= m_clientObjectList
.Nth( n
);
499 wxClientData
*cd
= (wxClientData
*) node
->Data();
502 node
->SetData( (wxObject
*) clientData
);
505 wxClientData
* wxListBox::GetClientObject( int n
)
507 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
509 wxNode
*node
= m_clientObjectList
.Nth( n
);
510 if (!node
) return (wxClientData
*) NULL
;
512 return (wxClientData
*) node
->Data();
515 void wxListBox::Clear()
517 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
519 gtk_list_clear_items( m_list
, 0, Number() );
521 wxNode
*node
= m_clientObjectList
.First();
524 wxClientData
*cd
= (wxClientData
*)node
->Data();
528 m_clientObjectList
.Clear();
530 m_clientDataList
.Clear();
533 void wxListBox::Delete( int n
)
535 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
537 GList
*child
= g_list_nth( m_list
->children
, n
);
539 wxCHECK_RET( child
, _T("wrong listbox index") );
541 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
542 gtk_list_remove_items( m_list
, list
);
545 wxNode
*node
= m_clientObjectList
.Nth( n
);
548 wxClientData
*cd
= (wxClientData
*)node
->Data();
550 m_clientObjectList
.DeleteNode( node
);
553 node
= m_clientDataList
.Nth( n
);
556 m_clientDataList
.DeleteNode( node
);
560 void wxListBox::Deselect( int n
)
562 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
564 gtk_list_unselect_item( m_list
, n
);
567 int wxListBox::FindString( const wxString
&item
) const
569 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
571 GList
*child
= m_list
->children
;
575 GtkBin
*bin
= GTK_BIN( child
->data
);
576 GtkLabel
*label
= GTK_LABEL( bin
->child
);
578 wxString str
= GET_REAL_LABEL(label
->label
);
587 // it's not an error if the string is not found -> no wxCHECK
592 int wxListBox::GetSelection() const
594 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
596 GList
*child
= m_list
->children
;
600 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
607 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
609 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
611 // get the number of selected items first
612 GList
*child
= m_list
->children
;
614 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
616 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
625 aSelections
.Alloc(count
); // optimization attempt
627 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
629 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
637 wxString
wxListBox::GetString( int n
) const
639 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
641 GList
*child
= g_list_nth( m_list
->children
, n
);
644 GtkBin
*bin
= GTK_BIN( child
->data
);
645 GtkLabel
*label
= GTK_LABEL( bin
->child
);
647 wxString str
= GET_REAL_LABEL(label
->label
);
652 wxFAIL_MSG(_T("wrong listbox index"));
657 wxString
wxListBox::GetStringSelection() const
659 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
661 GList
*selection
= m_list
->selection
;
664 GtkBin
*bin
= GTK_BIN( selection
->data
);
665 GtkLabel
*label
= GTK_LABEL( bin
->child
);
667 wxString str
= GET_REAL_LABEL(label
->label
);
672 wxFAIL_MSG(_T("no listbox selection available"));
676 int wxListBox::Number()
678 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
680 GList
*child
= m_list
->children
;
682 while (child
) { count
++; child
= child
->next
; }
686 bool wxListBox::Selected( int n
)
688 wxCHECK_MSG( m_list
!= NULL
, FALSE
, _T("invalid listbox") );
690 GList
*target
= g_list_nth( m_list
->children
, n
);
693 GList
*child
= m_list
->selection
;
696 if (child
->data
== target
->data
) return TRUE
;
700 wxFAIL_MSG(_T("wrong listbox index"));
704 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
706 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
709 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
711 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
714 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
716 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
719 void wxListBox::SetSelection( int n
, bool select
)
721 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
724 gtk_list_select_item( m_list
, n
);
726 gtk_list_unselect_item( m_list
, n
);
729 void wxListBox::SetString( int n
, const wxString
&string
)
731 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
733 GList
*child
= g_list_nth( m_list
->children
, n
);
736 GtkBin
*bin
= GTK_BIN( child
->data
);
737 GtkLabel
*label
= GTK_LABEL( bin
->child
);
741 str
+= CHECKBOX_STRING
;
744 gtk_label_set( label
, str
.mbc_str() );
748 wxFAIL_MSG(_T("wrong listbox index"));
752 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
754 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
756 SetSelection( FindString(string
), select
);
759 int wxListBox::GetIndex( GtkWidget
*item
) const
763 GList
*child
= m_list
->children
;
767 if (GTK_WIDGET(child
->data
) == item
) return count
;
776 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
778 GList
*child
= m_list
->children
;
781 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConv_local
.cWX2MB(tip
), (gchar
*) NULL
);
785 #endif // wxUSE_TOOLTIPS
787 #if wxUSE_DRAG_AND_DROP
788 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
790 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
792 #ifndef NEW_GTK_DND_CODE
795 GList
*child
= m_list
->children
;
798 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
804 wxWindow::SetDropTarget( dropTarget
);
806 #ifndef NEW_GTK_DND_CODE
809 GList
*child
= m_list
->children
;
812 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
820 GtkWidget
*wxListBox::GetConnectWidget()
822 return GTK_WIDGET(m_list
);
825 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
827 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
829 GList
*child
= m_list
->children
;
832 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
833 if (bin
->window
== window
) return TRUE
;
840 void wxListBox::ApplyWidgetStyle()
844 if (m_backgroundColour
.Ok())
846 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
847 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
848 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
849 gdk_window_clear( window
);
852 GList
*child
= m_list
->children
;
855 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
857 GtkBin
*bin
= GTK_BIN( child
->data
);
858 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
859 gtk_widget_set_style( label
, m_widgetStyle
);