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"
20 #include "wx/settings.h"
23 #include "wx/tooltip.h"
26 #if wxUSE_DRAG_AND_DROP
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern void wxapp_install_idle_handler();
40 //-------------------------------------------------------------------------
41 // conditional compilation
42 //-------------------------------------------------------------------------
44 #if (GTK_MINOR_VERSION > 0)
45 #define NEW_GTK_SCROLL_CODE
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 #define CHECKBOX_STRING "[-] "
54 // checklistboxes have "[±] " prepended to their lables, this macro removes it
55 // (NB: 4 below is the length of CHECKBOX_STRING above)
57 // the argument to it is a "const char *" pointer
58 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 extern bool g_blockEventsOnDrag
;
65 extern bool g_blockEventsOnScroll
;
67 //-----------------------------------------------------------------------------
68 // "button_press_event"
69 //-----------------------------------------------------------------------------
72 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
74 if (g_isIdle
) wxapp_install_idle_handler();
76 if (g_blockEventsOnDrag
) return FALSE
;
77 if (g_blockEventsOnScroll
) return FALSE
;
79 if (!listbox
->HasVMT()) return FALSE
;
81 int sel
= listbox
->GetIndex( widget
);
83 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
85 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
87 clb
->Check( sel
, !clb
->IsChecked(sel
) );
89 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
90 event
.SetEventObject( listbox
);
92 listbox
->GetEventHandler()->ProcessEvent( event
);
95 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
97 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
98 event
.SetEventObject( listbox
);
100 wxArrayInt aSelections
;
101 int count
= listbox
->GetSelections(aSelections
);
104 event
.m_commandInt
= aSelections
[0] ;
105 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
106 wxString
str(listbox
->GetString(event
.m_commandInt
));
107 if (!str
.IsEmpty()) event
.m_commandString
= str
;
111 event
.m_commandInt
= -1 ;
112 event
.m_commandString
.Empty();
115 listbox
->GetEventHandler()->ProcessEvent( event
);
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
127 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
129 if (g_isIdle
) wxapp_install_idle_handler();
131 if (g_blockEventsOnDrag
) return FALSE
;
133 if (!listbox
->HasVMT()) return FALSE
;
135 if (gdk_event
->keyval
!= ' ') return FALSE
;
137 int sel
= listbox
->GetIndex( widget
);
139 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
141 clb
->Check( sel
, !clb
->IsChecked(sel
) );
143 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
144 event
.SetEventObject( listbox
);
146 listbox
->GetEventHandler()->ProcessEvent( event
);
151 //-----------------------------------------------------------------------------
152 // "select" and "deselect"
153 //-----------------------------------------------------------------------------
155 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
157 if (g_isIdle
) wxapp_install_idle_handler();
159 if (!listbox
->HasVMT()) return;
160 if (g_blockEventsOnDrag
) return;
162 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
164 wxArrayInt aSelections
;
165 int count
= listbox
->GetSelections(aSelections
);
168 event
.m_commandInt
= aSelections
[0] ;
169 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
170 wxString
str(listbox
->GetString(event
.m_commandInt
));
171 if (!str
.IsEmpty()) event
.m_commandString
= str
;
175 event
.m_commandInt
= -1 ;
176 event
.m_commandString
.Empty();
179 event
.SetEventObject( listbox
);
181 listbox
->GetEventHandler()->ProcessEvent( event
);
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
190 wxListBox::wxListBox()
192 m_list
= (GtkList
*) NULL
;
193 m_hasCheckBoxes
= FALSE
;
196 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
197 const wxPoint
&pos
, const wxSize
&size
,
198 int n
, const wxString choices
[],
199 long style
, const wxValidator
& validator
, const wxString
&name
)
202 m_acceptsFocus
= TRUE
;
204 PreCreation( parent
, id
, pos
, size
, style
, name
);
206 SetValidator( validator
);
208 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
209 if (style
& wxLB_ALWAYS_SB
)
211 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
212 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
216 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
217 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
220 m_list
= GTK_LIST( gtk_list_new() );
222 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
223 if (style
& wxLB_MULTIPLE
)
224 mode
= GTK_SELECTION_MULTIPLE
;
225 else if (style
& wxLB_EXTENDED
)
226 mode
= GTK_SELECTION_EXTENDED
;
228 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
230 #ifdef NEW_GTK_SCROLL_CODE
231 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
233 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
236 gtk_widget_show( GTK_WIDGET(m_list
) );
238 wxSize newSize
= size
;
239 if (newSize
.x
== -1) newSize
.x
= 100;
240 if (newSize
.y
== -1) newSize
.y
= 110;
241 SetSize( newSize
.x
, newSize
.y
);
243 for (int i
= 0; i
< n
; i
++)
245 m_clientDataList
.Append( (wxObject
*) NULL
);
246 m_clientObjectList
.Append( (wxObject
*) NULL
);
248 GtkWidget
*list_item
;
250 wxString
str(choices
[i
]);
253 str
.Prepend(CHECKBOX_STRING
);
256 list_item
= gtk_list_item_new_with_label( str
.mbc_str() );
258 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
260 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
261 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
263 if (style
& wxLB_MULTIPLE
)
264 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
265 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
267 gtk_signal_connect( GTK_OBJECT(list_item
),
268 "button_press_event",
269 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
274 gtk_signal_connect( GTK_OBJECT(list_item
),
276 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
280 ConnectWidget( list_item
);
282 gtk_widget_show( list_item
);
285 m_parent
->AddChild( this );
287 (m_parent
->m_insertCallback
)( m_parent
, this );
291 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
292 SetForegroundColour( parent
->GetForegroundColour() );
293 SetFont( parent
->GetFont() );
300 wxListBox::~wxListBox()
305 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
307 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
309 GList
*children
= m_list
->children
;
310 int length
= g_list_length(children
);
311 wxCHECK_RET( pos
<= length
, _T("invalid index in wxListBox::InsertItems") );
313 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
314 // into a listbox at the given position, this is why we first delete
315 // all items after this position, then append these items and then
316 // reappend back the old ones.
318 // first detach the old items
323 // no need to do anything complicated
324 for ( n
= 0; n
< nItems
; n
++ )
332 wxArrayString deletedLabels
;
333 wxArrayPtrVoid deletedData
;
334 wxArrayInt deletedChecks
; // only for check list boxes
336 GList
*child
= g_list_nth( children
, pos
);
337 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
340 GtkBin
*bin
= GTK_BIN( child
->data
);
341 GtkLabel
*label
= GTK_LABEL( bin
->child
);
343 wxString
str(GET_REAL_LABEL(label
->label
));
344 deletedLabels
.Add(str
);
347 void *clientData
= NULL
;
350 if ( n
< (int)m_clientObjectList
.GetCount() )
351 node
= m_clientObjectList
.Nth( n
);
355 clientData
= node
->GetData();
356 m_clientObjectList
.DeleteNode( node
);
361 if ( n
< (int)m_clientDataList
.GetCount() )
362 node
= m_clientDataList
.Nth( n
);
366 clientData
= node
->GetData();
367 node
= m_clientDataList
.Nth( n
);
371 deletedData
.Add(clientData
);
374 if ( m_hasCheckBoxes
)
376 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
380 int nDeletedCount
= n
;
382 gtk_list_clear_items( m_list
, pos
, length
);
384 // now append the new items
385 for ( n
= 0; n
< nItems
; n
++ )
390 // and append the old items too
391 pos
+= nItems
; // now the indices are shifter
392 for ( n
= 0; n
< nDeletedCount
; n
++ )
394 Append(deletedLabels
[n
], deletedData
[n
]);
396 if ( m_hasCheckBoxes
)
398 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
403 void wxListBox::AppendCommon( const wxString
&item
)
405 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
407 GtkWidget
*list_item
;
409 wxString
label(item
);
412 label
.Prepend(CHECKBOX_STRING
);
415 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
417 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
419 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
420 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
422 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
423 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
424 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
426 gtk_signal_connect( GTK_OBJECT(list_item
),
427 "button_press_event",
428 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
433 gtk_signal_connect( GTK_OBJECT(list_item
),
435 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
439 gtk_widget_show( list_item
);
441 ConnectWidget( list_item
);
443 if (GTK_WIDGET_REALIZED(m_widget
))
445 gtk_widget_realize( list_item
);
446 gtk_widget_realize( GTK_BIN(list_item
)->child
);
448 if (m_widgetStyle
) ApplyWidgetStyle();
450 #if wxUSE_DRAG_AND_DROP
451 #ifndef NEW_GTK_DND_CODE
452 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
457 if (m_toolTip
) m_toolTip
->Apply( this );
462 void wxListBox::Append( const wxString
&item
)
464 m_clientDataList
.Append( (wxObject
*) NULL
);
465 m_clientObjectList
.Append( (wxObject
*) NULL
);
467 AppendCommon( item
);
470 void wxListBox::Append( const wxString
&item
, void *clientData
)
472 m_clientDataList
.Append( (wxObject
*) clientData
);
473 m_clientObjectList
.Append( (wxObject
*) NULL
);
475 AppendCommon( item
);
478 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
480 m_clientObjectList
.Append( (wxObject
*) clientData
);
481 m_clientDataList
.Append( (wxObject
*) NULL
);
483 AppendCommon( item
);
486 void wxListBox::SetClientData( int n
, void* clientData
)
488 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
490 wxNode
*node
= m_clientDataList
.Nth( n
);
493 node
->SetData( (wxObject
*) clientData
);
496 void* wxListBox::GetClientData( int n
)
498 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
500 wxNode
*node
= m_clientDataList
.Nth( n
);
501 if (!node
) return NULL
;
506 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
508 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
510 wxNode
*node
= m_clientObjectList
.Nth( n
);
513 wxClientData
*cd
= (wxClientData
*) node
->Data();
516 node
->SetData( (wxObject
*) clientData
);
519 wxClientData
* wxListBox::GetClientObject( int n
)
521 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
523 wxNode
*node
= m_clientObjectList
.Nth( n
);
524 if (!node
) return (wxClientData
*) NULL
;
526 return (wxClientData
*) node
->Data();
529 void wxListBox::Clear()
531 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
533 gtk_list_clear_items( m_list
, 0, Number() );
535 wxNode
*node
= m_clientObjectList
.First();
538 wxClientData
*cd
= (wxClientData
*)node
->Data();
542 m_clientObjectList
.Clear();
544 m_clientDataList
.Clear();
547 void wxListBox::Delete( int n
)
549 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
551 GList
*child
= g_list_nth( m_list
->children
, n
);
553 wxCHECK_RET( child
, _T("wrong listbox index") );
555 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
556 gtk_list_remove_items( m_list
, list
);
559 wxNode
*node
= m_clientObjectList
.Nth( n
);
562 wxClientData
*cd
= (wxClientData
*)node
->Data();
564 m_clientObjectList
.DeleteNode( node
);
567 node
= m_clientDataList
.Nth( n
);
570 m_clientDataList
.DeleteNode( node
);
574 void wxListBox::Deselect( int n
)
576 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
578 gtk_list_unselect_item( m_list
, n
);
581 int wxListBox::FindString( const wxString
&item
) const
583 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
585 GList
*child
= m_list
->children
;
589 GtkBin
*bin
= GTK_BIN( child
->data
);
590 GtkLabel
*label
= GTK_LABEL( bin
->child
);
592 wxString str
= GET_REAL_LABEL(label
->label
);
601 // it's not an error if the string is not found -> no wxCHECK
606 int wxListBox::GetSelection() const
608 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
610 GList
*child
= m_list
->children
;
614 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
621 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
623 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
625 // get the number of selected items first
626 GList
*child
= m_list
->children
;
628 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
630 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
639 aSelections
.Alloc(count
); // optimization attempt
641 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
643 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
651 wxString
wxListBox::GetString( int n
) const
653 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
655 GList
*child
= g_list_nth( m_list
->children
, n
);
658 GtkBin
*bin
= GTK_BIN( child
->data
);
659 GtkLabel
*label
= GTK_LABEL( bin
->child
);
661 wxString str
= GET_REAL_LABEL(label
->label
);
666 wxFAIL_MSG(_T("wrong listbox index"));
671 wxString
wxListBox::GetStringSelection() const
673 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
675 GList
*selection
= m_list
->selection
;
678 GtkBin
*bin
= GTK_BIN( selection
->data
);
679 GtkLabel
*label
= GTK_LABEL( bin
->child
);
681 wxString str
= GET_REAL_LABEL(label
->label
);
686 wxFAIL_MSG(_T("no listbox selection available"));
690 int wxListBox::Number()
692 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
694 GList
*child
= m_list
->children
;
696 while (child
) { count
++; child
= child
->next
; }
700 bool wxListBox::Selected( int n
)
702 wxCHECK_MSG( m_list
!= NULL
, FALSE
, _T("invalid listbox") );
704 GList
*target
= g_list_nth( m_list
->children
, n
);
707 GList
*child
= m_list
->selection
;
710 if (child
->data
== target
->data
) return TRUE
;
714 wxFAIL_MSG(_T("wrong listbox index"));
718 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
720 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
723 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
725 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
728 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
730 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
733 void wxListBox::SetSelection( int n
, bool select
)
735 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
738 gtk_list_select_item( m_list
, n
);
740 gtk_list_unselect_item( m_list
, n
);
743 void wxListBox::SetString( int n
, const wxString
&string
)
745 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
747 GList
*child
= g_list_nth( m_list
->children
, n
);
750 GtkBin
*bin
= GTK_BIN( child
->data
);
751 GtkLabel
*label
= GTK_LABEL( bin
->child
);
755 str
+= CHECKBOX_STRING
;
758 gtk_label_set( label
, str
.mbc_str() );
762 wxFAIL_MSG(_T("wrong listbox index"));
766 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
768 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
770 SetSelection( FindString(string
), select
);
773 int wxListBox::GetIndex( GtkWidget
*item
) const
777 GList
*child
= m_list
->children
;
781 if (GTK_WIDGET(child
->data
) == item
) return count
;
790 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
792 GList
*child
= m_list
->children
;
795 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConv_local
.cWX2MB(tip
), (gchar
*) NULL
);
799 #endif // wxUSE_TOOLTIPS
801 #if wxUSE_DRAG_AND_DROP
802 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
804 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
806 #ifndef NEW_GTK_DND_CODE
809 GList
*child
= m_list
->children
;
812 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
818 wxWindow::SetDropTarget( dropTarget
);
820 #ifndef NEW_GTK_DND_CODE
823 GList
*child
= m_list
->children
;
826 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
834 GtkWidget
*wxListBox::GetConnectWidget()
836 return GTK_WIDGET(m_list
);
839 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
841 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
843 GList
*child
= m_list
->children
;
846 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
847 if (bin
->window
== window
) return TRUE
;
854 void wxListBox::ApplyWidgetStyle()
858 if (m_backgroundColour
.Ok())
860 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
861 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
862 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
863 gdk_window_clear( window
);
866 GList
*child
= m_list
->children
;
869 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
871 GtkBin
*bin
= GTK_BIN( child
->data
);
872 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
873 gtk_widget_set_style( label
, m_widgetStyle
);