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 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
196 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
198 m_list
= GTK_LIST( gtk_list_new() );
200 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
201 if (style
& wxLB_MULTIPLE
)
202 mode
= GTK_SELECTION_MULTIPLE
;
203 else if (style
& wxLB_EXTENDED
)
204 mode
= GTK_SELECTION_EXTENDED
;
206 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
208 #ifdef NEW_GTK_SCROLL_CODE
209 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
211 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
215 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
217 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
219 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
221 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
222 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
224 #ifdef NEW_GTK_SCROLL_CODE
225 GtkViewport
*viewport
= GTK_VIEWPORT( GTK_BIN(s_window
)->child
);
227 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
230 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
233 gtk_widget_show( GTK_WIDGET(m_list
) );
235 wxSize newSize
= size
;
236 if (newSize
.x
== -1) newSize
.x
= 100;
237 if (newSize
.y
== -1) newSize
.y
= 110;
238 SetSize( newSize
.x
, newSize
.y
);
240 for (int i
= 0; i
< n
; i
++)
242 m_clientDataList
.Append( (wxObject
*) NULL
);
243 m_clientObjectList
.Append( (wxObject
*) NULL
);
245 GtkWidget
*list_item
;
247 wxString
str(choices
[i
]);
250 str
.Prepend(CHECKBOX_STRING
);
253 list_item
= gtk_list_item_new_with_label( str
);
256 debug_focus_in( list_item
, "wxListBox::list_item", name
);
259 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
261 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
262 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
264 if (style
& wxLB_MULTIPLE
)
265 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
266 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
268 gtk_signal_connect( GTK_OBJECT(list_item
),
269 "button_press_event",
270 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
275 gtk_signal_connect( GTK_OBJECT(list_item
),
277 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
281 ConnectWidget( list_item
);
283 gtk_widget_show( list_item
);
286 m_parent
->AddChild( this );
288 (m_parent
->m_insertCallback
)( m_parent
, this );
292 gtk_widget_realize( GTK_WIDGET(m_list
) );
294 SetBackgroundColour( parent
->GetBackgroundColour() );
295 SetForegroundColour( parent
->GetForegroundColour() );
296 SetFont( parent
->GetFont() );
303 wxListBox::~wxListBox()
308 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
310 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
312 GList
*children
= m_list
->children
;
313 int length
= g_list_length(children
);
314 wxCHECK_RET( pos
<= length
, "invalid index in wxListBox::InsertItems" );
316 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
317 // into a listbox at the given position, this is why we first delete
318 // all items after this position, then append these items and then
319 // reappend back the old ones.
321 // first detach the old items
326 // no need to do anything complicated
327 for ( n
= 0; n
< nItems
; n
++ )
335 wxArrayString deletedLabels
;
336 wxArrayPtrVoid deletedData
;
337 wxArrayInt deletedChecks
; // only for check list boxes
339 GList
*child
= g_list_nth( children
, pos
);
340 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
343 GtkBin
*bin
= GTK_BIN( child
->data
);
344 GtkLabel
*label
= GTK_LABEL( bin
->child
);
346 wxString
str(GET_REAL_LABEL(label
->label
));
347 deletedLabels
.Add(str
);
350 void *clientData
= NULL
;
353 if ( n
< (int)m_clientObjectList
.GetCount() )
354 node
= m_clientObjectList
.Nth( n
);
358 clientData
= node
->GetData();
359 m_clientObjectList
.DeleteNode( node
);
364 if ( n
< (int)m_clientDataList
.GetCount() )
365 node
= m_clientDataList
.Nth( n
);
369 clientData
= node
->GetData();
370 node
= m_clientDataList
.Nth( n
);
374 deletedData
.Add(clientData
);
377 if ( m_hasCheckBoxes
)
379 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
383 int nDeletedCount
= n
;
385 gtk_list_clear_items( m_list
, pos
, length
);
387 // now append the new items
388 for ( n
= 0; n
< nItems
; n
++ )
393 // and append the old items too
394 pos
+= nItems
; // now the indices are shifter
395 for ( n
= 0; n
< nDeletedCount
; n
++ )
397 Append(deletedLabels
[n
], deletedData
[n
]);
399 if ( m_hasCheckBoxes
)
401 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
406 void wxListBox::AppendCommon( const wxString
&item
)
408 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
410 GtkWidget
*list_item
;
412 wxString
label(item
);
415 label
.Prepend(CHECKBOX_STRING
);
418 list_item
= gtk_list_item_new_with_label( label
);
420 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
422 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
423 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
425 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
426 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
427 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
429 if (m_widgetStyle
) ApplyWidgetStyle();
431 gtk_signal_connect( GTK_OBJECT(list_item
),
432 "button_press_event",
433 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
438 gtk_signal_connect( GTK_OBJECT(list_item
),
440 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
444 gtk_widget_show( list_item
);
446 ConnectWidget( list_item
);
448 #if wxUSE_DRAG_AND_DROP
449 #ifndef NEW_GTK_DND_CODE
450 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
455 if (m_toolTip
) m_toolTip
->Apply( this );
459 void wxListBox::Append( const wxString
&item
)
461 m_clientDataList
.Append( (wxObject
*) NULL
);
462 m_clientObjectList
.Append( (wxObject
*) NULL
);
464 AppendCommon( item
);
467 void wxListBox::Append( const wxString
&item
, void *clientData
)
469 m_clientDataList
.Append( (wxObject
*) clientData
);
470 m_clientObjectList
.Append( (wxObject
*) NULL
);
472 AppendCommon( item
);
475 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
477 m_clientObjectList
.Append( (wxObject
*) clientData
);
478 m_clientDataList
.Append( (wxObject
*) NULL
);
480 AppendCommon( item
);
483 void wxListBox::SetClientData( int n
, void* clientData
)
485 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
487 wxNode
*node
= m_clientDataList
.Nth( n
);
490 node
->SetData( (wxObject
*) clientData
);
493 void* wxListBox::GetClientData( int n
)
495 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
497 wxNode
*node
= m_clientDataList
.Nth( n
);
498 if (!node
) return NULL
;
503 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
505 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
507 wxNode
*node
= m_clientObjectList
.Nth( n
);
510 wxClientData
*cd
= (wxClientData
*) node
->Data();
513 node
->SetData( (wxObject
*) clientData
);
516 wxClientData
* wxListBox::GetClientObject( int n
)
518 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
520 wxNode
*node
= m_clientObjectList
.Nth( n
);
521 if (!node
) return (wxClientData
*) NULL
;
523 return (wxClientData
*) node
->Data();
526 void wxListBox::Clear()
528 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
530 gtk_list_clear_items( m_list
, 0, Number() );
532 wxNode
*node
= m_clientObjectList
.First();
535 wxClientData
*cd
= (wxClientData
*)node
->Data();
539 m_clientObjectList
.Clear();
541 m_clientDataList
.Clear();
544 void wxListBox::Delete( int n
)
546 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
548 GList
*child
= g_list_nth( m_list
->children
, n
);
550 wxCHECK_RET( child
, "wrong listbox index" );
552 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
553 gtk_list_remove_items( m_list
, list
);
556 wxNode
*node
= m_clientObjectList
.Nth( n
);
559 wxClientData
*cd
= (wxClientData
*)node
->Data();
561 m_clientObjectList
.DeleteNode( node
);
564 node
= m_clientDataList
.Nth( n
);
567 m_clientDataList
.DeleteNode( node
);
571 void wxListBox::Deselect( int n
)
573 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
575 gtk_list_unselect_item( m_list
, n
);
578 int wxListBox::FindString( const wxString
&item
) const
580 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
582 GList
*child
= m_list
->children
;
586 GtkBin
*bin
= GTK_BIN( child
->data
);
587 GtkLabel
*label
= GTK_LABEL( bin
->child
);
589 wxString str
= GET_REAL_LABEL(label
->label
);
598 // it's not an error if the string is not found -> no wxCHECK
603 int wxListBox::GetSelection() const
605 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
607 GList
*child
= m_list
->children
;
611 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
618 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
620 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
622 // get the number of selected items first
623 GList
*child
= m_list
->children
;
625 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
627 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
636 aSelections
.Alloc(count
); // optimization attempt
638 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
640 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
648 wxString
wxListBox::GetString( int n
) const
650 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
652 GList
*child
= g_list_nth( m_list
->children
, n
);
655 GtkBin
*bin
= GTK_BIN( child
->data
);
656 GtkLabel
*label
= GTK_LABEL( bin
->child
);
658 wxString str
= GET_REAL_LABEL(label
->label
);
663 wxFAIL_MSG("wrong listbox index");
668 wxString
wxListBox::GetStringSelection() const
670 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
672 GList
*selection
= m_list
->selection
;
675 GtkBin
*bin
= GTK_BIN( selection
->data
);
676 GtkLabel
*label
= GTK_LABEL( bin
->child
);
678 wxString str
= GET_REAL_LABEL(label
->label
);
683 wxFAIL_MSG("no listbox selection available");
687 int wxListBox::Number()
689 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
691 GList
*child
= m_list
->children
;
693 while (child
) { count
++; child
= child
->next
; }
697 bool wxListBox::Selected( int n
)
699 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
701 GList
*target
= g_list_nth( m_list
->children
, n
);
704 GList
*child
= m_list
->selection
;
707 if (child
->data
== target
->data
) return TRUE
;
711 wxFAIL_MSG("wrong listbox index");
715 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
717 wxFAIL_MSG("wxListBox::Set not implemented");
720 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
722 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
725 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
727 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
730 void wxListBox::SetSelection( int n
, bool select
)
732 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
735 gtk_list_select_item( m_list
, n
);
737 gtk_list_unselect_item( m_list
, n
);
740 void wxListBox::SetString( int n
, const wxString
&string
)
742 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
744 GList
*child
= g_list_nth( m_list
->children
, n
);
747 GtkBin
*bin
= GTK_BIN( child
->data
);
748 GtkLabel
*label
= GTK_LABEL( bin
->child
);
752 str
+= CHECKBOX_STRING
;
755 gtk_label_set( label
, str
);
759 wxFAIL_MSG("wrong listbox index");
763 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
765 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
767 SetSelection( FindString(string
), select
);
770 int wxListBox::GetIndex( GtkWidget
*item
) const
774 GList
*child
= m_list
->children
;
778 if (GTK_WIDGET(child
->data
) == item
) return count
;
787 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const char *tip
)
789 GList
*child
= m_list
->children
;
792 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
796 #endif // wxUSE_TOOLTIPS
798 #if wxUSE_DRAG_AND_DROP
799 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
801 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
803 #ifndef NEW_GTK_DND_CODE
806 GList
*child
= m_list
->children
;
809 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
815 wxWindow::SetDropTarget( dropTarget
);
817 #ifndef NEW_GTK_DND_CODE
820 GList
*child
= m_list
->children
;
823 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
831 GtkWidget
*wxListBox::GetConnectWidget()
833 return GTK_WIDGET(m_list
);
836 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
838 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
840 GList
*child
= m_list
->children
;
843 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
844 if (bin
->window
== window
) return TRUE
;
851 void wxListBox::ApplyWidgetStyle()
855 if (m_backgroundColour
.Ok())
857 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
858 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
859 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
860 gdk_window_clear( window
);
863 GList
*child
= m_list
->children
;
866 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
868 GtkBin
*bin
= GTK_BIN( child
->data
);
869 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
870 gtk_widget_set_style( label
, m_widgetStyle
);