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 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern void wxapp_install_idle_handler();
39 //-------------------------------------------------------------------------
40 // conditional compilation
41 //-------------------------------------------------------------------------
43 #if (GTK_MINOR_VERSION > 0)
44 #define NEW_GTK_SCROLL_CODE
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 #define CHECKBOX_STRING "[-] "
53 // checklistboxes have "[±] " prepended to their lables, this macro removes it
54 // (NB: 4 below is the length of CHECKBOX_STRING above)
56 // the argument to it is a "const char *" pointer
57 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 extern bool g_blockEventsOnDrag
;
64 extern bool g_blockEventsOnScroll
;
66 //-----------------------------------------------------------------------------
67 // "button_press_event"
68 //-----------------------------------------------------------------------------
71 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
73 if (g_isIdle
) wxapp_install_idle_handler();
75 if (g_blockEventsOnDrag
) return FALSE
;
76 if (g_blockEventsOnScroll
) return FALSE
;
78 if (!listbox
->HasVMT()) return FALSE
;
80 int sel
= listbox
->GetIndex( widget
);
82 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
84 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
86 clb
->Check( sel
, !clb
->IsChecked(sel
) );
88 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
89 event
.SetEventObject( listbox
);
91 listbox
->GetEventHandler()->ProcessEvent( event
);
94 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
96 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
97 event
.SetEventObject( listbox
);
99 wxArrayInt aSelections
;
100 int count
= listbox
->GetSelections(aSelections
);
103 event
.m_commandInt
= aSelections
[0] ;
104 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
105 wxString
str(listbox
->GetString(event
.m_commandInt
));
106 if (!str
.IsEmpty()) event
.m_commandString
= str
;
110 event
.m_commandInt
= -1 ;
111 event
.m_commandString
.Empty();
114 listbox
->GetEventHandler()->ProcessEvent( event
);
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
126 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
128 if (g_isIdle
) wxapp_install_idle_handler();
130 if (g_blockEventsOnDrag
) return FALSE
;
132 if (!listbox
->HasVMT()) return FALSE
;
134 if (gdk_event
->keyval
!= ' ') return FALSE
;
136 int sel
= listbox
->GetIndex( widget
);
138 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
140 clb
->Check( sel
, !clb
->IsChecked(sel
) );
142 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
143 event
.SetEventObject( listbox
);
145 listbox
->GetEventHandler()->ProcessEvent( event
);
150 //-----------------------------------------------------------------------------
151 // "select" and "deselect"
152 //-----------------------------------------------------------------------------
154 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
156 if (g_isIdle
) wxapp_install_idle_handler();
158 if (!listbox
->HasVMT()) return;
159 if (g_blockEventsOnDrag
) return;
161 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
163 wxArrayInt aSelections
;
164 int count
= listbox
->GetSelections(aSelections
);
167 event
.m_commandInt
= aSelections
[0] ;
168 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
169 wxString
str(listbox
->GetString(event
.m_commandInt
));
170 if (!str
.IsEmpty()) event
.m_commandString
= str
;
174 event
.m_commandInt
= -1 ;
175 event
.m_commandString
.Empty();
178 event
.SetEventObject( listbox
);
180 listbox
->GetEventHandler()->ProcessEvent( event
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
189 wxListBox::wxListBox()
191 m_list
= (GtkList
*) NULL
;
192 m_hasCheckBoxes
= FALSE
;
195 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
196 const wxPoint
&pos
, const wxSize
&size
,
197 int n
, const wxString choices
[],
198 long style
, const wxValidator
& validator
, const wxString
&name
)
201 m_acceptsFocus
= TRUE
;
203 PreCreation( parent
, id
, pos
, size
, style
, name
);
205 SetValidator( validator
);
207 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
208 if (style
& wxLB_ALWAYS_SB
)
210 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
211 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
215 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
216 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
219 m_list
= GTK_LIST( gtk_list_new() );
221 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
222 if (style
& wxLB_MULTIPLE
)
223 mode
= GTK_SELECTION_MULTIPLE
;
224 else if (style
& wxLB_EXTENDED
)
225 mode
= GTK_SELECTION_EXTENDED
;
227 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
229 #ifdef NEW_GTK_SCROLL_CODE
230 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
232 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
235 gtk_widget_show( GTK_WIDGET(m_list
) );
237 wxSize newSize
= size
;
238 if (newSize
.x
== -1) newSize
.x
= 100;
239 if (newSize
.y
== -1) newSize
.y
= 110;
240 SetSize( newSize
.x
, newSize
.y
);
242 for (int i
= 0; i
< n
; i
++)
244 m_clientDataList
.Append( (wxObject
*) NULL
);
245 m_clientObjectList
.Append( (wxObject
*) NULL
);
247 GtkWidget
*list_item
;
249 wxString
str(choices
[i
]);
252 str
.Prepend(CHECKBOX_STRING
);
255 list_item
= gtk_list_item_new_with_label( str
.mbc_str() );
257 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
259 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
260 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
262 if (style
& wxLB_MULTIPLE
)
263 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
264 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
266 gtk_signal_connect( GTK_OBJECT(list_item
),
267 "button_press_event",
268 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
273 gtk_signal_connect( GTK_OBJECT(list_item
),
275 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
279 ConnectWidget( list_item
);
281 gtk_widget_show( list_item
);
284 m_parent
->AddChild( this );
286 (m_parent
->m_insertCallback
)( m_parent
, this );
290 SetBackgroundColour( parent
->GetBackgroundColour() );
291 SetForegroundColour( parent
->GetForegroundColour() );
292 SetFont( parent
->GetFont() );
299 wxListBox::~wxListBox()
304 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
306 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
308 GList
*children
= m_list
->children
;
309 int length
= g_list_length(children
);
310 wxCHECK_RET( pos
<= length
, _T("invalid index in wxListBox::InsertItems") );
312 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
313 // into a listbox at the given position, this is why we first delete
314 // all items after this position, then append these items and then
315 // reappend back the old ones.
317 // first detach the old items
322 // no need to do anything complicated
323 for ( n
= 0; n
< nItems
; n
++ )
331 wxArrayString deletedLabels
;
332 wxArrayPtrVoid deletedData
;
333 wxArrayInt deletedChecks
; // only for check list boxes
335 GList
*child
= g_list_nth( children
, pos
);
336 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
339 GtkBin
*bin
= GTK_BIN( child
->data
);
340 GtkLabel
*label
= GTK_LABEL( bin
->child
);
342 wxString
str(GET_REAL_LABEL(label
->label
));
343 deletedLabels
.Add(str
);
346 void *clientData
= NULL
;
349 if ( n
< (int)m_clientObjectList
.GetCount() )
350 node
= m_clientObjectList
.Nth( n
);
354 clientData
= node
->GetData();
355 m_clientObjectList
.DeleteNode( node
);
360 if ( n
< (int)m_clientDataList
.GetCount() )
361 node
= m_clientDataList
.Nth( n
);
365 clientData
= node
->GetData();
366 node
= m_clientDataList
.Nth( n
);
370 deletedData
.Add(clientData
);
373 if ( m_hasCheckBoxes
)
375 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
379 int nDeletedCount
= n
;
381 gtk_list_clear_items( m_list
, pos
, length
);
383 // now append the new items
384 for ( n
= 0; n
< nItems
; n
++ )
389 // and append the old items too
390 pos
+= nItems
; // now the indices are shifter
391 for ( n
= 0; n
< nDeletedCount
; n
++ )
393 Append(deletedLabels
[n
], deletedData
[n
]);
395 if ( m_hasCheckBoxes
)
397 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
402 void wxListBox::AppendCommon( const wxString
&item
)
404 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
406 GtkWidget
*list_item
;
408 wxString
label(item
);
411 label
.Prepend(CHECKBOX_STRING
);
414 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
416 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
418 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
419 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
421 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
422 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
423 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
425 gtk_signal_connect( GTK_OBJECT(list_item
),
426 "button_press_event",
427 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
432 gtk_signal_connect( GTK_OBJECT(list_item
),
434 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
438 gtk_widget_show( list_item
);
440 ConnectWidget( list_item
);
442 if (GTK_WIDGET_REALIZED(m_widget
))
444 gtk_widget_realize( list_item
);
445 gtk_widget_realize( GTK_BIN(list_item
)->child
);
447 if (m_widgetStyle
) ApplyWidgetStyle();
449 #if wxUSE_DRAG_AND_DROP
450 #ifndef NEW_GTK_DND_CODE
451 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
456 if (m_toolTip
) m_toolTip
->Apply( this );
461 void wxListBox::Append( const wxString
&item
)
463 m_clientDataList
.Append( (wxObject
*) NULL
);
464 m_clientObjectList
.Append( (wxObject
*) NULL
);
466 AppendCommon( item
);
469 void wxListBox::Append( const wxString
&item
, void *clientData
)
471 m_clientDataList
.Append( (wxObject
*) clientData
);
472 m_clientObjectList
.Append( (wxObject
*) NULL
);
474 AppendCommon( item
);
477 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
479 m_clientObjectList
.Append( (wxObject
*) clientData
);
480 m_clientDataList
.Append( (wxObject
*) NULL
);
482 AppendCommon( item
);
485 void wxListBox::SetClientData( int n
, void* clientData
)
487 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
489 wxNode
*node
= m_clientDataList
.Nth( n
);
492 node
->SetData( (wxObject
*) clientData
);
495 void* wxListBox::GetClientData( int n
)
497 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
499 wxNode
*node
= m_clientDataList
.Nth( n
);
500 if (!node
) return NULL
;
505 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
507 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
509 wxNode
*node
= m_clientObjectList
.Nth( n
);
512 wxClientData
*cd
= (wxClientData
*) node
->Data();
515 node
->SetData( (wxObject
*) clientData
);
518 wxClientData
* wxListBox::GetClientObject( int n
)
520 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
522 wxNode
*node
= m_clientObjectList
.Nth( n
);
523 if (!node
) return (wxClientData
*) NULL
;
525 return (wxClientData
*) node
->Data();
528 void wxListBox::Clear()
530 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
532 gtk_list_clear_items( m_list
, 0, Number() );
534 wxNode
*node
= m_clientObjectList
.First();
537 wxClientData
*cd
= (wxClientData
*)node
->Data();
541 m_clientObjectList
.Clear();
543 m_clientDataList
.Clear();
546 void wxListBox::Delete( int n
)
548 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
550 GList
*child
= g_list_nth( m_list
->children
, n
);
552 wxCHECK_RET( child
, _T("wrong listbox index") );
554 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
555 gtk_list_remove_items( m_list
, list
);
558 wxNode
*node
= m_clientObjectList
.Nth( n
);
561 wxClientData
*cd
= (wxClientData
*)node
->Data();
563 m_clientObjectList
.DeleteNode( node
);
566 node
= m_clientDataList
.Nth( n
);
569 m_clientDataList
.DeleteNode( node
);
573 void wxListBox::Deselect( int n
)
575 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
577 gtk_list_unselect_item( m_list
, n
);
580 int wxListBox::FindString( const wxString
&item
) const
582 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
584 GList
*child
= m_list
->children
;
588 GtkBin
*bin
= GTK_BIN( child
->data
);
589 GtkLabel
*label
= GTK_LABEL( bin
->child
);
591 wxString str
= GET_REAL_LABEL(label
->label
);
600 // it's not an error if the string is not found -> no wxCHECK
605 int wxListBox::GetSelection() const
607 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
609 GList
*child
= m_list
->children
;
613 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
620 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
622 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
624 // get the number of selected items first
625 GList
*child
= m_list
->children
;
627 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
629 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
638 aSelections
.Alloc(count
); // optimization attempt
640 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
642 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
650 wxString
wxListBox::GetString( int n
) const
652 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
654 GList
*child
= g_list_nth( m_list
->children
, n
);
657 GtkBin
*bin
= GTK_BIN( child
->data
);
658 GtkLabel
*label
= GTK_LABEL( bin
->child
);
660 wxString str
= GET_REAL_LABEL(label
->label
);
665 wxFAIL_MSG(_T("wrong listbox index"));
670 wxString
wxListBox::GetStringSelection() const
672 wxCHECK_MSG( m_list
!= NULL
, _T(""), _T("invalid listbox") );
674 GList
*selection
= m_list
->selection
;
677 GtkBin
*bin
= GTK_BIN( selection
->data
);
678 GtkLabel
*label
= GTK_LABEL( bin
->child
);
680 wxString str
= GET_REAL_LABEL(label
->label
);
685 wxFAIL_MSG(_T("no listbox selection available"));
689 int wxListBox::Number()
691 wxCHECK_MSG( m_list
!= NULL
, -1, _T("invalid listbox") );
693 GList
*child
= m_list
->children
;
695 while (child
) { count
++; child
= child
->next
; }
699 bool wxListBox::Selected( int n
)
701 wxCHECK_MSG( m_list
!= NULL
, FALSE
, _T("invalid listbox") );
703 GList
*target
= g_list_nth( m_list
->children
, n
);
706 GList
*child
= m_list
->selection
;
709 if (child
->data
== target
->data
) return TRUE
;
713 wxFAIL_MSG(_T("wrong listbox index"));
717 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
719 wxFAIL_MSG(_T("wxListBox::Set not implemented"));
722 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
724 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
727 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
729 wxFAIL_MSG(_T("wxListBox::SetFirstItem not implemented"));
732 void wxListBox::SetSelection( int n
, bool select
)
734 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
737 gtk_list_select_item( m_list
, n
);
739 gtk_list_unselect_item( m_list
, n
);
742 void wxListBox::SetString( int n
, const wxString
&string
)
744 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
746 GList
*child
= g_list_nth( m_list
->children
, n
);
749 GtkBin
*bin
= GTK_BIN( child
->data
);
750 GtkLabel
*label
= GTK_LABEL( bin
->child
);
754 str
+= CHECKBOX_STRING
;
757 gtk_label_set( label
, str
.mbc_str() );
761 wxFAIL_MSG(_T("wrong listbox index"));
765 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
767 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
769 SetSelection( FindString(string
), select
);
772 int wxListBox::GetIndex( GtkWidget
*item
) const
776 GList
*child
= m_list
->children
;
780 if (GTK_WIDGET(child
->data
) == item
) return count
;
789 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
791 GList
*child
= m_list
->children
;
794 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConv_local
.cWX2MB(tip
), (gchar
*) NULL
);
798 #endif // wxUSE_TOOLTIPS
800 #if wxUSE_DRAG_AND_DROP
801 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
803 wxCHECK_RET( m_list
!= NULL
, _T("invalid listbox") );
805 #ifndef NEW_GTK_DND_CODE
808 GList
*child
= m_list
->children
;
811 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
817 wxWindow::SetDropTarget( dropTarget
);
819 #ifndef NEW_GTK_DND_CODE
822 GList
*child
= m_list
->children
;
825 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
833 GtkWidget
*wxListBox::GetConnectWidget()
835 return GTK_WIDGET(m_list
);
838 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
840 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
842 GList
*child
= m_list
->children
;
845 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
846 if (bin
->window
== window
) return TRUE
;
853 void wxListBox::ApplyWidgetStyle()
857 if (m_backgroundColour
.Ok())
859 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
860 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
861 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
862 gdk_window_clear( window
);
865 GList
*child
= m_list
->children
;
868 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
870 GtkBin
*bin
= GTK_BIN( child
->data
);
871 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
872 gtk_widget_set_style( label
, m_widgetStyle
);