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 gtk_widget_realize( GTK_WIDGET(m_list
) );
279 SetBackgroundColour( parent
->GetBackgroundColour() );
280 SetForegroundColour( parent
->GetForegroundColour() );
281 SetFont( parent
->GetFont() );
288 wxListBox::~wxListBox()
293 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
295 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
297 GList
*children
= m_list
->children
;
298 int length
= g_list_length(children
);
299 wxCHECK_RET( pos
<= length
, _T("invalid index in wxListBox::InsertItems") );
301 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
302 // into a listbox at the given position, this is why we first delete
303 // all items after this position, then append these items and then
304 // reappend back the old ones.
306 // first detach the old items
311 // no need to do anything complicated
312 for ( n
= 0; n
< nItems
; n
++ )
320 wxArrayString deletedLabels
;
321 wxArrayPtrVoid deletedData
;
322 wxArrayInt deletedChecks
; // only for check list boxes
324 GList
*child
= g_list_nth( children
, pos
);
325 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
328 GtkBin
*bin
= GTK_BIN( child
->data
);
329 GtkLabel
*label
= GTK_LABEL( bin
->child
);
331 wxString
str(GET_REAL_LABEL(label
->label
));
332 deletedLabels
.Add(str
);
335 void *clientData
= NULL
;
338 if ( n
< (int)m_clientObjectList
.GetCount() )
339 node
= m_clientObjectList
.Nth( n
);
343 clientData
= node
->GetData();
344 m_clientObjectList
.DeleteNode( node
);
349 if ( n
< (int)m_clientDataList
.GetCount() )
350 node
= m_clientDataList
.Nth( n
);
354 clientData
= node
->GetData();
355 node
= m_clientDataList
.Nth( n
);
359 deletedData
.Add(clientData
);
362 if ( m_hasCheckBoxes
)
364 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
368 int nDeletedCount
= n
;
370 gtk_list_clear_items( m_list
, pos
, length
);
372 // now append the new items
373 for ( n
= 0; n
< nItems
; n
++ )
378 // and append the old items too
379 pos
+= nItems
; // now the indices are shifter
380 for ( n
= 0; n
< nDeletedCount
; n
++ )
382 Append(deletedLabels
[n
], deletedData
[n
]);
384 if ( m_hasCheckBoxes
)
386 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
391 void wxListBox::AppendCommon( const wxString
&item
)
393 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
395 GtkWidget
*list_item
;
397 wxString
label(item
);
400 label
.Prepend(CHECKBOX_STRING
);
403 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
405 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
407 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
408 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
410 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
411 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
412 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
414 if (m_widgetStyle
) ApplyWidgetStyle();
416 gtk_signal_connect( GTK_OBJECT(list_item
),
417 "button_press_event",
418 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
423 gtk_signal_connect( GTK_OBJECT(list_item
),
425 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
429 gtk_widget_show( list_item
);
431 ConnectWidget( list_item
);
433 #if wxUSE_DRAG_AND_DROP
434 #ifndef NEW_GTK_DND_CODE
435 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
440 if (m_toolTip
) m_toolTip
->Apply( this );
444 void wxListBox::Append( const wxString
&item
)
446 m_clientDataList
.Append( (wxObject
*) NULL
);
447 m_clientObjectList
.Append( (wxObject
*) NULL
);
449 AppendCommon( item
);
452 void wxListBox::Append( const wxString
&item
, void *clientData
)
454 m_clientDataList
.Append( (wxObject
*) clientData
);
455 m_clientObjectList
.Append( (wxObject
*) NULL
);
457 AppendCommon( item
);
460 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
462 m_clientObjectList
.Append( (wxObject
*) clientData
);
463 m_clientDataList
.Append( (wxObject
*) NULL
);
465 AppendCommon( item
);
468 void wxListBox::SetClientData( int n
, void* clientData
)
470 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
472 wxNode
*node
= m_clientDataList
.Nth( n
);
475 node
->SetData( (wxObject
*) clientData
);
478 void* wxListBox::GetClientData( int n
)
480 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
482 wxNode
*node
= m_clientDataList
.Nth( n
);
483 if (!node
) return NULL
;
488 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
490 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
492 wxNode
*node
= m_clientObjectList
.Nth( n
);
495 wxClientData
*cd
= (wxClientData
*) node
->Data();
498 node
->SetData( (wxObject
*) clientData
);
501 wxClientData
* wxListBox::GetClientObject( int n
)
503 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
505 wxNode
*node
= m_clientObjectList
.Nth( n
);
506 if (!node
) return (wxClientData
*) NULL
;
508 return (wxClientData
*) node
->Data();
511 void wxListBox::Clear()
513 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
515 gtk_list_clear_items( m_list
, 0, Number() );
517 wxNode
*node
= m_clientObjectList
.First();
520 wxClientData
*cd
= (wxClientData
*)node
->Data();
524 m_clientObjectList
.Clear();
526 m_clientDataList
.Clear();
529 void wxListBox::Delete( int n
)
531 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
533 GList
*child
= g_list_nth( m_list
->children
, n
);
535 wxCHECK_RET( child
, _T("wrong listbox index") );
537 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
538 gtk_list_remove_items( m_list
, list
);
541 wxNode
*node
= m_clientObjectList
.Nth( n
);
544 wxClientData
*cd
= (wxClientData
*)node
->Data();
546 m_clientObjectList
.DeleteNode( node
);
549 node
= m_clientDataList
.Nth( n
);
552 m_clientDataList
.DeleteNode( node
);
556 void wxListBox::Deselect( int n
)
558 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
560 gtk_list_unselect_item( m_list
, n
);
563 int wxListBox::FindString( const wxString
&item
) const
565 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
567 GList
*child
= m_list
->children
;
571 GtkBin
*bin
= GTK_BIN( child
->data
);
572 GtkLabel
*label
= GTK_LABEL( bin
->child
);
574 wxString str
= GET_REAL_LABEL(label
->label
);
583 // it's not an error if the string is not found -> no wxCHECK
588 int wxListBox::GetSelection() const
590 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
592 GList
*child
= m_list
->children
;
596 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
603 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
605 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
607 // get the number of selected items first
608 GList
*child
= m_list
->children
;
610 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
612 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
621 aSelections
.Alloc(count
); // optimization attempt
623 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
625 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
633 wxString
wxListBox::GetString( int n
) const
635 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
637 GList
*child
= g_list_nth( m_list
->children
, n
);
640 GtkBin
*bin
= GTK_BIN( child
->data
);
641 GtkLabel
*label
= GTK_LABEL( bin
->child
);
643 wxString str
= GET_REAL_LABEL(label
->label
);
648 wxFAIL_MSG(_T("wrong listbox index"));
653 wxString
wxListBox::GetStringSelection() const
655 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
657 GList
*selection
= m_list
->selection
;
660 GtkBin
*bin
= GTK_BIN( selection
->data
);
661 GtkLabel
*label
= GTK_LABEL( bin
->child
);
663 wxString str
= GET_REAL_LABEL(label
->label
);
668 wxFAIL_MSG(_T("no listbox selection available"));
672 int wxListBox::Number()
674 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
676 GList
*child
= m_list
->children
;
678 while (child
) { count
++; child
= child
->next
; }
682 bool wxListBox::Selected( int n
)
684 wxCHECK_MSG( m_list
!= NULL
, FALSE
, _T("invalid listbox") );
686 GList
*target
= g_list_nth( m_list
->children
, n
);
689 GList
*child
= m_list
->selection
;
692 if (child
->data
== target
->data
) return TRUE
;
696 wxFAIL_MSG(_T("wrong listbox index"));
700 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
702 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
705 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
707 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
710 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
712 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
715 void wxListBox::SetSelection( int n
, bool select
)
717 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
720 gtk_list_select_item( m_list
, n
);
722 gtk_list_unselect_item( m_list
, n
);
725 void wxListBox::SetString( int n
, const wxString
&string
)
727 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
729 GList
*child
= g_list_nth( m_list
->children
, n
);
732 GtkBin
*bin
= GTK_BIN( child
->data
);
733 GtkLabel
*label
= GTK_LABEL( bin
->child
);
737 str
+= CHECKBOX_STRING
;
740 gtk_label_set( label
, str
.mbc_str() );
744 wxFAIL_MSG(_T("wrong listbox index"));
748 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
750 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
752 SetSelection( FindString(string
), select
);
755 int wxListBox::GetIndex( GtkWidget
*item
) const
759 GList
*child
= m_list
->children
;
763 if (GTK_WIDGET(child
->data
) == item
) return count
;
772 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
774 GList
*child
= m_list
->children
;
777 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
781 #endif // wxUSE_TOOLTIPS
783 #if wxUSE_DRAG_AND_DROP
784 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
786 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
788 #ifndef NEW_GTK_DND_CODE
791 GList
*child
= m_list
->children
;
794 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
800 wxWindow::SetDropTarget( dropTarget
);
802 #ifndef NEW_GTK_DND_CODE
805 GList
*child
= m_list
->children
;
808 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
816 GtkWidget
*wxListBox::GetConnectWidget()
818 return GTK_WIDGET(m_list
);
821 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
823 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
825 GList
*child
= m_list
->children
;
828 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
829 if (bin
->window
== window
) return TRUE
;
836 void wxListBox::ApplyWidgetStyle()
840 if (m_backgroundColour
.Ok())
842 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
843 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
844 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
845 gdk_window_clear( window
);
848 GList
*child
= m_list
->children
;
851 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
853 GtkBin
*bin
= GTK_BIN( child
->data
);
854 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
855 gtk_widget_set_style( label
, m_widgetStyle
);