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 == 1)
37 #if (GTK_MICRO_VERSION >= 5)
38 #define NEW_GTK_SCROLL_CODE
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 #define CHECKBOX_STRING "[-] "
48 // checklistboxes have "[±] " prepended to their lables, this macro removes it
49 // (NB: 4 below is the length of CHECKBOX_STRING above)
51 // the argument to it is a "const char *" pointer
52 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 extern bool g_blockEventsOnDrag
;
59 extern bool g_blockEventsOnScroll
;
61 //-----------------------------------------------------------------------------
62 // "button_press_event"
63 //-----------------------------------------------------------------------------
66 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
68 if (g_blockEventsOnDrag
) return FALSE
;
69 if (g_blockEventsOnScroll
) return FALSE
;
71 if (!listbox
->HasVMT()) return FALSE
;
73 int sel
= listbox
->GetIndex( widget
);
75 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
77 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
79 clb
->Check( sel
, !clb
->IsChecked(sel
) );
81 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
82 event
.SetEventObject( listbox
);
84 listbox
->GetEventHandler()->ProcessEvent( event
);
87 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
89 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
90 event
.SetEventObject( listbox
);
92 wxArrayInt aSelections
;
93 int count
= listbox
->GetSelections(aSelections
);
96 event
.m_commandInt
= aSelections
[0] ;
97 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
98 wxString
str(listbox
->GetString(event
.m_commandInt
));
99 if (!str
.IsEmpty()) event
.m_commandString
= str
;
103 event
.m_commandInt
= -1 ;
104 event
.m_commandString
.Empty();
107 listbox
->GetEventHandler()->ProcessEvent( event
);
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
119 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
121 if (g_blockEventsOnDrag
) return FALSE
;
123 if (!listbox
->HasVMT()) return FALSE
;
125 if (gdk_event
->keyval
!= ' ') return FALSE
;
127 int sel
= listbox
->GetIndex( widget
);
129 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
131 clb
->Check( sel
, !clb
->IsChecked(sel
) );
133 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
134 event
.SetEventObject( listbox
);
136 listbox
->GetEventHandler()->ProcessEvent( event
);
141 //-----------------------------------------------------------------------------
142 // "select" and "deselect"
143 //-----------------------------------------------------------------------------
145 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
147 if (!listbox
->HasVMT()) return;
148 if (g_blockEventsOnDrag
) return;
150 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
152 wxArrayInt aSelections
;
153 int count
= listbox
->GetSelections(aSelections
);
156 event
.m_commandInt
= aSelections
[0] ;
157 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
158 wxString
str(listbox
->GetString(event
.m_commandInt
));
159 if (!str
.IsEmpty()) event
.m_commandString
= str
;
163 event
.m_commandInt
= -1 ;
164 event
.m_commandString
.Empty();
167 event
.SetEventObject( listbox
);
169 listbox
->GetEventHandler()->ProcessEvent( event
);
172 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
176 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
178 wxListBox::wxListBox()
180 m_list
= (GtkList
*) NULL
;
181 m_hasCheckBoxes
= FALSE
;
184 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
185 const wxPoint
&pos
, const wxSize
&size
,
186 int n
, const wxString choices
[],
187 long style
, const wxValidator
& validator
, const wxString
&name
)
190 m_acceptsFocus
= TRUE
;
192 PreCreation( parent
, id
, pos
, size
, style
, name
);
194 SetValidator( validator
);
196 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
197 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
198 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
200 m_list
= GTK_LIST( gtk_list_new() );
202 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
203 if (style
& wxLB_MULTIPLE
)
204 mode
= GTK_SELECTION_MULTIPLE
;
205 else if (style
& wxLB_EXTENDED
)
206 mode
= GTK_SELECTION_EXTENDED
;
208 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
210 #ifdef NEW_GTK_SCROLL_CODE
211 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
213 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
217 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
219 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
221 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
223 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
224 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
226 #ifdef NEW_GTK_SCROLL_CODE
227 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
229 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
232 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
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
);
258 debug_focus_in( list_item
, "wxListBox::list_item", name
);
261 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
263 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
264 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
266 if (style
& wxLB_MULTIPLE
)
267 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
268 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
270 gtk_signal_connect( GTK_OBJECT(list_item
),
271 "button_press_event",
272 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
277 gtk_signal_connect( GTK_OBJECT(list_item
),
279 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
283 ConnectWidget( list_item
);
285 gtk_widget_show( list_item
);
288 m_parent
->AddChild( this );
290 (m_parent
->m_insertCallback
)( m_parent
, this );
294 gtk_widget_realize( GTK_WIDGET(m_list
) );
296 SetBackgroundColour( parent
->GetBackgroundColour() );
297 SetForegroundColour( parent
->GetForegroundColour() );
298 SetFont( parent
->GetFont() );
305 wxListBox::~wxListBox()
310 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
312 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
314 GList
*children
= m_list
->children
;
315 int length
= g_list_length(children
);
316 wxCHECK_RET( pos
<= length
, "invalid index in wxListBox::InsertItems" );
318 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
319 // into a listbox at the given position, this is why we first delete
320 // all items after this position, then append these items and then
321 // reappend back the old ones.
323 // first detach the old items
328 // no need to do anything complicated
329 for ( n
= 0; n
< nItems
; n
++ )
337 wxArrayString deletedLabels
;
338 wxArrayPtrVoid deletedData
;
339 wxArrayInt deletedChecks
; // only for check list boxes
341 GList
*child
= g_list_nth( children
, pos
);
342 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
345 GtkBin
*bin
= GTK_BIN( child
->data
);
346 GtkLabel
*label
= GTK_LABEL( bin
->child
);
348 wxString
str(GET_REAL_LABEL(label
->label
));
349 deletedLabels
.Add(str
);
352 void *clientData
= NULL
;
355 if ( n
< (int)m_clientObjectList
.GetCount() )
356 node
= m_clientObjectList
.Nth( n
);
360 clientData
= node
->GetData();
361 m_clientObjectList
.DeleteNode( node
);
366 if ( n
< (int)m_clientDataList
.GetCount() )
367 node
= m_clientDataList
.Nth( n
);
371 clientData
= node
->GetData();
372 node
= m_clientDataList
.Nth( n
);
376 deletedData
.Add(clientData
);
379 if ( m_hasCheckBoxes
)
381 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
385 int nDeletedCount
= n
;
387 gtk_list_clear_items( m_list
, pos
, length
);
389 // now append the new items
390 for ( n
= 0; n
< nItems
; n
++ )
395 // and append the old items too
396 pos
+= nItems
; // now the indices are shifter
397 for ( n
= 0; n
< nDeletedCount
; n
++ )
399 Append(deletedLabels
[n
], deletedData
[n
]);
401 if ( m_hasCheckBoxes
)
403 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
408 void wxListBox::AppendCommon( const wxString
&item
)
410 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
412 GtkWidget
*list_item
;
414 wxString
label(item
);
417 label
.Prepend(CHECKBOX_STRING
);
420 list_item
= gtk_list_item_new_with_label( label
);
422 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
424 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
425 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
427 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
428 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
429 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
431 if (m_widgetStyle
) ApplyWidgetStyle();
433 gtk_signal_connect( GTK_OBJECT(list_item
),
434 "button_press_event",
435 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
440 gtk_signal_connect( GTK_OBJECT(list_item
),
442 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
446 gtk_widget_show( list_item
);
448 ConnectWidget( list_item
);
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 );
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
, "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
, "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
, "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
, "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
, "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
, "invalid listbox" );
550 GList
*child
= g_list_nth( m_list
->children
, n
);
552 wxCHECK_RET( child
, "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
, "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, "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, "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, "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
, "", "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("wrong listbox index");
670 wxString
wxListBox::GetStringSelection() const
672 wxCHECK_MSG( m_list
!= NULL
, "", "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("no listbox selection available");
689 int wxListBox::Number()
691 wxCHECK_MSG( m_list
!= NULL
, -1, "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
, "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("wrong listbox index");
717 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
719 wxFAIL_MSG("wxListBox::Set not implemented");
722 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
724 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
727 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
729 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
732 void wxListBox::SetSelection( int n
, bool select
)
734 wxCHECK_RET( m_list
!= NULL
, "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
, "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
);
761 wxFAIL_MSG("wrong listbox index");
765 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
767 wxCHECK_RET( m_list
!= NULL
, "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 char *tip
)
791 GList
*child
= m_list
->children
;
794 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), 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
, "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
);