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
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
103 event
.m_commandInt
= -1 ;
104 event
.m_commandString
= copystring("") ;
107 listbox
->GetEventHandler()->ProcessEvent( event
);
109 if (event
.m_commandString
) delete[] event
.m_commandString
;
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
120 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
122 if (g_blockEventsOnDrag
) return FALSE
;
124 if (!listbox
->HasVMT()) return FALSE
;
126 if (gdk_event
->keyval
!= ' ') return FALSE
;
128 int sel
= listbox
->GetIndex( widget
);
130 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
132 clb
->Check( sel
, !clb
->IsChecked(sel
) );
134 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
135 event
.SetEventObject( listbox
);
137 listbox
->GetEventHandler()->ProcessEvent( event
);
142 //-----------------------------------------------------------------------------
143 // "select" and "deselect"
144 //-----------------------------------------------------------------------------
146 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
148 if (!listbox
->HasVMT()) return;
149 if (g_blockEventsOnDrag
) return;
151 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
153 wxArrayInt aSelections
;
154 int count
= listbox
->GetSelections(aSelections
);
157 event
.m_commandInt
= aSelections
[0] ;
158 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
159 wxString
str(listbox
->GetString(event
.m_commandInt
));
160 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
164 event
.m_commandInt
= -1 ;
165 event
.m_commandString
= copystring("") ;
168 event
.SetEventObject( listbox
);
170 listbox
->GetEventHandler()->ProcessEvent( event
);
171 if (event
.m_commandString
) delete[] event
.m_commandString
;
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
180 wxListBox::wxListBox()
182 m_list
= (GtkList
*) NULL
;
183 m_hasCheckBoxes
= FALSE
;
186 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
187 const wxPoint
&pos
, const wxSize
&size
,
188 int n
, const wxString choices
[],
189 long style
, const wxValidator
& validator
, const wxString
&name
)
192 m_acceptsFocus
= TRUE
;
194 PreCreation( parent
, id
, pos
, size
, style
, name
);
196 SetValidator( validator
);
198 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
199 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
200 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
202 m_list
= GTK_LIST( gtk_list_new() );
204 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
205 if (style
& wxLB_MULTIPLE
)
206 mode
= GTK_SELECTION_MULTIPLE
;
207 else if (style
& wxLB_EXTENDED
)
208 mode
= GTK_SELECTION_EXTENDED
;
210 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
212 #ifdef NEW_GTK_SCROLL_CODE
213 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
215 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
219 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
221 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
223 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
225 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
226 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
228 #ifdef NEW_GTK_SCROLL_CODE
229 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
231 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
234 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
237 gtk_widget_show( GTK_WIDGET(m_list
) );
239 wxSize newSize
= size
;
240 if (newSize
.x
== -1) newSize
.x
= 100;
241 if (newSize
.y
== -1) newSize
.y
= 110;
242 SetSize( newSize
.x
, newSize
.y
);
244 for (int i
= 0; i
< n
; i
++)
246 m_clientDataList
.Append( (wxObject
*) NULL
);
247 m_clientObjectList
.Append( (wxObject
*) NULL
);
249 GtkWidget
*list_item
;
251 wxString
str(choices
[i
]);
254 str
.Prepend(CHECKBOX_STRING
);
257 list_item
= gtk_list_item_new_with_label( str
);
260 debug_focus_in( list_item
, "wxListBox::list_item", name
);
263 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
265 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
266 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
268 if (style
& wxLB_MULTIPLE
)
269 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
270 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
272 gtk_signal_connect( GTK_OBJECT(list_item
),
273 "button_press_event",
274 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
279 gtk_signal_connect( GTK_OBJECT(list_item
),
281 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
285 ConnectWidget( list_item
);
287 gtk_widget_show( list_item
);
290 m_parent
->AddChild( this );
292 (m_parent
->m_insertCallback
)( m_parent
, this );
296 gtk_widget_realize( GTK_WIDGET(m_list
) );
298 SetBackgroundColour( parent
->GetBackgroundColour() );
299 SetForegroundColour( parent
->GetForegroundColour() );
300 SetFont( parent
->GetFont() );
307 wxListBox::~wxListBox()
312 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
314 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
316 GList
*children
= m_list
->children
;
317 int length
= g_list_length(children
);
318 wxCHECK_RET( pos
<= length
, "invalid index in wxListBox::InsertItems" );
320 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
321 // into a listbox at the given position, this is why we first delete
322 // all items after this position, then append these items and then
323 // reappend back the old ones.
325 // first detach the old items
330 // no need to do anything complicated
331 for ( n
= 0; n
< nItems
; n
++ )
339 wxArrayString deletedLabels
;
340 wxArrayPtrVoid deletedData
;
341 wxArrayInt deletedChecks
; // only for check list boxes
343 GList
*child
= g_list_nth( children
, pos
);
344 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
347 GtkBin
*bin
= GTK_BIN( child
->data
);
348 GtkLabel
*label
= GTK_LABEL( bin
->child
);
350 wxString
str(GET_REAL_LABEL(label
->label
));
351 deletedLabels
.Add(str
);
354 void *clientData
= NULL
;
357 if ( n
< (int)m_clientObjectList
.GetCount() )
358 node
= m_clientObjectList
.Nth( n
);
362 clientData
= node
->GetData();
363 m_clientObjectList
.DeleteNode( node
);
368 if ( n
< (int)m_clientDataList
.GetCount() )
369 node
= m_clientDataList
.Nth( n
);
373 clientData
= node
->GetData();
374 node
= m_clientDataList
.Nth( n
);
378 deletedData
.Add(clientData
);
381 if ( m_hasCheckBoxes
)
383 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
387 int nDeletedCount
= n
;
389 gtk_list_clear_items( m_list
, pos
, length
);
391 // now append the new items
392 for ( n
= 0; n
< nItems
; n
++ )
397 // and append the old items too
398 pos
+= nItems
; // now the indices are shifter
399 for ( n
= 0; n
< nDeletedCount
; n
++ )
401 Append(deletedLabels
[n
], deletedData
[n
]);
403 if ( m_hasCheckBoxes
)
405 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
410 void wxListBox::AppendCommon( const wxString
&item
)
412 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
414 GtkWidget
*list_item
;
416 wxString
label(item
);
419 label
.Prepend(CHECKBOX_STRING
);
422 list_item
= gtk_list_item_new_with_label( label
);
424 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
426 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
427 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
429 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
430 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
431 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
433 if (m_widgetStyle
) ApplyWidgetStyle();
435 gtk_signal_connect( GTK_OBJECT(list_item
),
436 "button_press_event",
437 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
442 gtk_signal_connect( GTK_OBJECT(list_item
),
444 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
448 gtk_widget_show( list_item
);
450 ConnectWidget( list_item
);
452 #if wxUSE_DRAG_AND_DROP
453 #ifndef NEW_GTK_DND_CODE
454 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
459 if (m_toolTip
) m_toolTip
->Apply( this );
463 void wxListBox::Append( const wxString
&item
)
465 m_clientDataList
.Append( (wxObject
*) NULL
);
466 m_clientObjectList
.Append( (wxObject
*) NULL
);
468 AppendCommon( item
);
471 void wxListBox::Append( const wxString
&item
, void *clientData
)
473 m_clientDataList
.Append( (wxObject
*) clientData
);
474 m_clientObjectList
.Append( (wxObject
*) NULL
);
476 AppendCommon( item
);
479 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
481 m_clientObjectList
.Append( (wxObject
*) clientData
);
482 m_clientDataList
.Append( (wxObject
*) NULL
);
484 AppendCommon( item
);
487 void wxListBox::SetClientData( int n
, void* clientData
)
489 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
491 wxNode
*node
= m_clientDataList
.Nth( n
);
494 node
->SetData( (wxObject
*) clientData
);
497 void* wxListBox::GetClientData( int n
)
499 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
501 wxNode
*node
= m_clientDataList
.Nth( n
);
502 if (!node
) return NULL
;
507 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
509 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
511 wxNode
*node
= m_clientObjectList
.Nth( n
);
514 wxClientData
*cd
= (wxClientData
*) node
->Data();
517 node
->SetData( (wxObject
*) clientData
);
520 wxClientData
* wxListBox::GetClientObject( int n
)
522 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
524 wxNode
*node
= m_clientObjectList
.Nth( n
);
525 if (!node
) return (wxClientData
*) NULL
;
527 return (wxClientData
*) node
->Data();
530 void wxListBox::Clear()
532 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
534 gtk_list_clear_items( m_list
, 0, Number() );
536 wxNode
*node
= m_clientObjectList
.First();
539 wxClientData
*cd
= (wxClientData
*)node
->Data();
543 m_clientObjectList
.Clear();
545 m_clientDataList
.Clear();
548 void wxListBox::Delete( int n
)
550 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
552 GList
*child
= g_list_nth( m_list
->children
, n
);
554 wxCHECK_RET( child
, "wrong listbox index" );
556 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
557 gtk_list_remove_items( m_list
, list
);
560 wxNode
*node
= m_clientObjectList
.Nth( n
);
563 wxClientData
*cd
= (wxClientData
*)node
->Data();
565 m_clientObjectList
.DeleteNode( node
);
568 node
= m_clientDataList
.Nth( n
);
571 m_clientDataList
.DeleteNode( node
);
575 void wxListBox::Deselect( int n
)
577 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
579 gtk_list_unselect_item( m_list
, n
);
582 int wxListBox::FindString( const wxString
&item
) const
584 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
586 GList
*child
= m_list
->children
;
590 GtkBin
*bin
= GTK_BIN( child
->data
);
591 GtkLabel
*label
= GTK_LABEL( bin
->child
);
593 wxString str
= GET_REAL_LABEL(label
->label
);
602 // it's not an error if the string is not found -> no wxCHECK
607 int wxListBox::GetSelection() const
609 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
611 GList
*child
= m_list
->children
;
615 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
622 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
624 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
626 // get the number of selected items first
627 GList
*child
= m_list
->children
;
629 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
631 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
640 aSelections
.Alloc(count
); // optimization attempt
642 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
644 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
652 wxString
wxListBox::GetString( int n
) const
654 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
656 GList
*child
= g_list_nth( m_list
->children
, n
);
659 GtkBin
*bin
= GTK_BIN( child
->data
);
660 GtkLabel
*label
= GTK_LABEL( bin
->child
);
662 wxString str
= GET_REAL_LABEL(label
->label
);
667 wxFAIL_MSG("wrong listbox index");
672 wxString
wxListBox::GetStringSelection() const
674 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
676 GList
*selection
= m_list
->selection
;
679 GtkBin
*bin
= GTK_BIN( selection
->data
);
680 GtkLabel
*label
= GTK_LABEL( bin
->child
);
682 wxString str
= GET_REAL_LABEL(label
->label
);
687 wxFAIL_MSG("no listbox selection available");
691 int wxListBox::Number()
693 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
695 GList
*child
= m_list
->children
;
697 while (child
) { count
++; child
= child
->next
; }
701 bool wxListBox::Selected( int n
)
703 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
705 GList
*target
= g_list_nth( m_list
->children
, n
);
708 GList
*child
= m_list
->selection
;
711 if (child
->data
== target
->data
) return TRUE
;
715 wxFAIL_MSG("wrong listbox index");
719 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
721 wxFAIL_MSG("wxListBox::Set not implemented");
724 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
726 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
729 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
731 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
734 void wxListBox::SetSelection( int n
, bool select
)
736 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
739 gtk_list_select_item( m_list
, n
);
741 gtk_list_unselect_item( m_list
, n
);
744 void wxListBox::SetString( int n
, const wxString
&string
)
746 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
748 GList
*child
= g_list_nth( m_list
->children
, n
);
751 GtkBin
*bin
= GTK_BIN( child
->data
);
752 GtkLabel
*label
= GTK_LABEL( bin
->child
);
756 str
+= CHECKBOX_STRING
;
759 gtk_label_set( label
, str
);
763 wxFAIL_MSG("wrong listbox index");
767 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
769 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
771 SetSelection( FindString(string
), select
);
774 int wxListBox::GetIndex( GtkWidget
*item
) const
778 GList
*child
= m_list
->children
;
782 if (GTK_WIDGET(child
->data
) == item
) return count
;
791 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const char *tip
)
793 GList
*child
= m_list
->children
;
796 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
800 #endif // wxUSE_TOOLTIPS
802 #if wxUSE_DRAG_AND_DROP
803 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
805 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
807 #ifndef NEW_GTK_DND_CODE
810 GList
*child
= m_list
->children
;
813 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
819 wxWindow::SetDropTarget( dropTarget
);
821 #ifndef NEW_GTK_DND_CODE
824 GList
*child
= m_list
->children
;
827 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
835 GtkWidget
*wxListBox::GetConnectWidget()
837 return GTK_WIDGET(m_list
);
840 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
842 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
844 GList
*child
= m_list
->children
;
847 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
848 if (bin
->window
== window
) return TRUE
;
855 void wxListBox::ApplyWidgetStyle()
859 if (m_backgroundColour
.Ok())
861 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
862 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
863 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
864 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
);