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
->DoAddChild( this );
289 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
290 SetForegroundColour( parent
->GetForegroundColour() );
291 SetFont( parent
->GetFont() );
298 wxListBox::~wxListBox()
303 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
305 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
307 GList
*children
= m_list
->children
;
308 int length
= g_list_length(children
);
309 wxCHECK_RET( pos
<= length
, _T("invalid index in wxListBox::InsertItems") );
311 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
312 // into a listbox at the given position, this is why we first delete
313 // all items after this position, then append these items and then
314 // reappend back the old ones.
316 // first detach the old items
321 // no need to do anything complicated
322 for ( n
= 0; n
< nItems
; n
++ )
330 wxArrayString deletedLabels
;
331 wxArrayPtrVoid deletedData
;
332 wxArrayInt deletedChecks
; // only for check list boxes
334 GList
*child
= g_list_nth( children
, pos
);
335 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
338 GtkBin
*bin
= GTK_BIN( child
->data
);
339 GtkLabel
*label
= GTK_LABEL( bin
->child
);
341 wxString
str(GET_REAL_LABEL(label
->label
));
342 deletedLabels
.Add(str
);
345 void *clientData
= NULL
;
348 if ( n
< (int)m_clientObjectList
.GetCount() )
349 node
= m_clientObjectList
.Nth( n
);
353 clientData
= node
->GetData();
354 m_clientObjectList
.DeleteNode( node
);
359 if ( n
< (int)m_clientDataList
.GetCount() )
360 node
= m_clientDataList
.Nth( n
);
364 clientData
= node
->GetData();
365 node
= m_clientDataList
.Nth( n
);
369 deletedData
.Add(clientData
);
372 if ( m_hasCheckBoxes
)
374 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
378 int nDeletedCount
= n
;
380 gtk_list_clear_items( m_list
, pos
, length
);
382 // now append the new items
383 for ( n
= 0; n
< nItems
; n
++ )
388 // and append the old items too
389 pos
+= nItems
; // now the indices are shifter
390 for ( n
= 0; n
< nDeletedCount
; n
++ )
392 Append(deletedLabels
[n
], deletedData
[n
]);
394 if ( m_hasCheckBoxes
)
396 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
401 void wxListBox::AppendCommon( const wxString
&item
)
403 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
405 GtkWidget
*list_item
;
407 wxString
label(item
);
410 label
.Prepend(CHECKBOX_STRING
);
413 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
415 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
417 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
418 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
420 if (HasFlag(wxLB_MULTIPLE
))
421 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
422 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
424 gtk_signal_connect( GTK_OBJECT(list_item
),
425 "button_press_event",
426 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
431 gtk_signal_connect( GTK_OBJECT(list_item
),
433 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
437 gtk_widget_show( list_item
);
439 ConnectWidget( list_item
);
441 if (GTK_WIDGET_REALIZED(m_widget
))
443 gtk_widget_realize( list_item
);
444 gtk_widget_realize( GTK_BIN(list_item
)->child
);
446 if (m_widgetStyle
) ApplyWidgetStyle();
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 );
460 void wxListBox::Append( const wxString
&item
)
462 m_clientDataList
.Append( (wxObject
*) NULL
);
463 m_clientObjectList
.Append( (wxObject
*) NULL
);
465 AppendCommon( item
);
468 void wxListBox::Append( const wxString
&item
, void *clientData
)
470 m_clientDataList
.Append( (wxObject
*) clientData
);
471 m_clientObjectList
.Append( (wxObject
*) NULL
);
473 AppendCommon( item
);
476 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
478 m_clientObjectList
.Append( (wxObject
*) clientData
);
479 m_clientDataList
.Append( (wxObject
*) NULL
);
481 AppendCommon( item
);
484 void wxListBox::SetClientData( int n
, void* clientData
)
486 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
488 wxNode
*node
= m_clientDataList
.Nth( n
);
491 node
->SetData( (wxObject
*) clientData
);
494 void* wxListBox::GetClientData( int n
)
496 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
498 wxNode
*node
= m_clientDataList
.Nth( n
);
499 if (!node
) return NULL
;
504 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
506 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
508 wxNode
*node
= m_clientObjectList
.Nth( n
);
511 wxClientData
*cd
= (wxClientData
*) node
->Data();
514 node
->SetData( (wxObject
*) clientData
);
517 wxClientData
* wxListBox::GetClientObject( int n
)
519 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
521 wxNode
*node
= m_clientObjectList
.Nth( n
);
522 if (!node
) return (wxClientData
*) NULL
;
524 return (wxClientData
*) node
->Data();
527 void wxListBox::Clear()
529 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
531 gtk_list_clear_items( m_list
, 0, Number() );
533 wxNode
*node
= m_clientObjectList
.First();
536 wxClientData
*cd
= (wxClientData
*)node
->Data();
540 m_clientObjectList
.Clear();
542 m_clientDataList
.Clear();
545 void wxListBox::Delete( int n
)
547 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
549 GList
*child
= g_list_nth( m_list
->children
, n
);
551 wxCHECK_RET( child
, _T("wrong listbox index") );
553 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
554 gtk_list_remove_items( m_list
, list
);
557 wxNode
*node
= m_clientObjectList
.Nth( n
);
560 wxClientData
*cd
= (wxClientData
*)node
->Data();
562 m_clientObjectList
.DeleteNode( node
);
565 node
= m_clientDataList
.Nth( n
);
568 m_clientDataList
.DeleteNode( node
);
572 void wxListBox::Deselect( int n
)
574 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
576 gtk_list_unselect_item( m_list
, n
);
579 int wxListBox::FindString( const wxString
&item
) const
581 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
583 GList
*child
= m_list
->children
;
587 GtkBin
*bin
= GTK_BIN( child
->data
);
588 GtkLabel
*label
= GTK_LABEL( bin
->child
);
590 wxString str
= GET_REAL_LABEL(label
->label
);
599 // it's not an error if the string is not found -> no wxCHECK
604 int wxListBox::GetSelection() const
606 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
608 GList
*child
= m_list
->children
;
612 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
619 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
621 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
623 // get the number of selected items first
624 GList
*child
= m_list
->children
;
626 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
628 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
637 aSelections
.Alloc(count
); // optimization attempt
639 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
641 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
649 wxString
wxListBox::GetString( int n
) const
651 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
653 GList
*child
= g_list_nth( m_list
->children
, n
);
656 GtkBin
*bin
= GTK_BIN( child
->data
);
657 GtkLabel
*label
= GTK_LABEL( bin
->child
);
659 wxString str
= GET_REAL_LABEL(label
->label
);
664 wxFAIL_MSG(_T("wrong listbox index"));
669 wxString
wxListBox::GetStringSelection() const
671 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
673 GList
*selection
= m_list
->selection
;
676 GtkBin
*bin
= GTK_BIN( selection
->data
);
677 GtkLabel
*label
= GTK_LABEL( bin
->child
);
679 wxString str
= GET_REAL_LABEL(label
->label
);
684 wxFAIL_MSG(_T("no listbox selection available"));
688 int wxListBox::Number()
690 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
692 GList
*child
= m_list
->children
;
694 while (child
) { count
++; child
= child
->next
; }
698 bool wxListBox::Selected( int n
)
700 wxCHECK_MSG( m_list
!= NULL
, FALSE
, _T("invalid listbox") );
702 GList
*target
= g_list_nth( m_list
->children
, n
);
705 GList
*child
= m_list
->selection
;
708 if (child
->data
== target
->data
) return TRUE
;
712 wxFAIL_MSG(_T("wrong listbox index"));
716 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
718 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
721 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
723 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
726 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
728 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
731 void wxListBox::SetSelection( int n
, bool select
)
733 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
736 gtk_list_select_item( m_list
, n
);
738 gtk_list_unselect_item( m_list
, n
);
741 void wxListBox::SetString( int n
, const wxString
&string
)
743 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
745 GList
*child
= g_list_nth( m_list
->children
, n
);
748 GtkBin
*bin
= GTK_BIN( child
->data
);
749 GtkLabel
*label
= GTK_LABEL( bin
->child
);
753 str
+= CHECKBOX_STRING
;
756 gtk_label_set( label
, str
.mbc_str() );
760 wxFAIL_MSG(_T("wrong listbox index"));
764 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
766 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
768 SetSelection( FindString(string
), select
);
771 int wxListBox::GetIndex( GtkWidget
*item
) const
775 GList
*child
= m_list
->children
;
779 if (GTK_WIDGET(child
->data
) == item
) return count
;
788 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
790 GList
*child
= m_list
->children
;
793 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConv_local
.cWX2MB(tip
), (gchar
*) NULL
);
797 #endif // wxUSE_TOOLTIPS
799 #if wxUSE_DRAG_AND_DROP
800 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
802 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
804 #ifndef NEW_GTK_DND_CODE
807 GList
*child
= m_list
->children
;
810 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
816 wxWindow::SetDropTarget( dropTarget
);
818 #ifndef NEW_GTK_DND_CODE
821 GList
*child
= m_list
->children
;
824 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
832 GtkWidget
*wxListBox::GetConnectWidget()
834 return GTK_WIDGET(m_list
);
837 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
839 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
841 GList
*child
= m_list
->children
;
844 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
845 if (bin
->window
== window
) return TRUE
;
852 void wxListBox::ApplyWidgetStyle()
856 if (m_backgroundColour
.Ok())
858 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
);
867 GList
*child
= m_list
->children
;
870 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
872 GtkBin
*bin
= GTK_BIN( child
->data
);
873 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
874 gtk_widget_set_style( label
, m_widgetStyle
);