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 extern bool g_blockEventsOnDrag
;
47 extern bool g_blockEventsOnScroll
;
49 //-----------------------------------------------------------------------------
50 // "button_press_event"
51 //-----------------------------------------------------------------------------
54 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
56 if (g_blockEventsOnDrag
) return FALSE
;
57 if (g_blockEventsOnScroll
) return FALSE
;
59 if (!listbox
->HasVMT()) return FALSE
;
61 int sel
= listbox
->GetIndex( widget
);
63 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
65 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
67 clb
->Check( sel
, !clb
->IsChecked(sel
) );
69 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
70 event
.SetEventObject( listbox
);
72 listbox
->GetEventHandler()->ProcessEvent( event
);
75 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
77 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
78 event
.SetEventObject( listbox
);
80 wxArrayInt aSelections
;
81 int count
= listbox
->GetSelections(aSelections
);
84 event
.m_commandInt
= aSelections
[0] ;
85 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
86 wxString
str(listbox
->GetString(event
.m_commandInt
));
87 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
91 event
.m_commandInt
= -1 ;
92 event
.m_commandString
= copystring("") ;
95 listbox
->GetEventHandler()->ProcessEvent( event
);
97 if (event
.m_commandString
) delete[] event
.m_commandString
;
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
108 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
110 if (g_blockEventsOnDrag
) return FALSE
;
112 if (!listbox
->HasVMT()) return FALSE
;
114 if (gdk_event
->keyval
!= ' ') return FALSE
;
116 int sel
= listbox
->GetIndex( widget
);
118 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
120 clb
->Check( sel
, !clb
->IsChecked(sel
) );
122 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
123 event
.SetEventObject( listbox
);
125 listbox
->GetEventHandler()->ProcessEvent( event
);
130 //-----------------------------------------------------------------------------
131 // "select" and "deselect"
132 //-----------------------------------------------------------------------------
134 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
136 if (!listbox
->HasVMT()) return;
137 if (g_blockEventsOnDrag
) return;
139 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
141 wxArrayInt aSelections
;
142 int count
= listbox
->GetSelections(aSelections
);
145 event
.m_commandInt
= aSelections
[0] ;
146 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
147 wxString
str(listbox
->GetString(event
.m_commandInt
));
148 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
152 event
.m_commandInt
= -1 ;
153 event
.m_commandString
= copystring("") ;
156 event
.SetEventObject( listbox
);
158 listbox
->GetEventHandler()->ProcessEvent( event
);
159 if (event
.m_commandString
) delete[] event
.m_commandString
;
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
168 wxListBox::wxListBox()
170 m_list
= (GtkList
*) NULL
;
171 m_hasCheckBoxes
= FALSE
;
174 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
,
176 int n
, const wxString choices
[],
177 long style
, const wxValidator
& validator
, const wxString
&name
)
180 m_acceptsFocus
= TRUE
;
182 PreCreation( parent
, id
, pos
, size
, style
, name
);
184 SetValidator( validator
);
186 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
187 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
188 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
190 m_list
= GTK_LIST( gtk_list_new() );
192 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
193 if (style
& wxLB_MULTIPLE
)
194 mode
= GTK_SELECTION_MULTIPLE
;
195 else if (style
& wxLB_EXTENDED
)
196 mode
= GTK_SELECTION_EXTENDED
;
198 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
200 #ifdef NEW_GTK_SCROLL_CODE
201 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
203 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
207 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
209 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
211 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
213 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
214 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
216 #ifdef NEW_GTK_SCROLL_CODE
217 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
219 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
222 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
225 gtk_widget_show( GTK_WIDGET(m_list
) );
227 wxSize newSize
= size
;
228 if (newSize
.x
== -1) newSize
.x
= 100;
229 if (newSize
.y
== -1) newSize
.y
= 110;
230 SetSize( newSize
.x
, newSize
.y
);
232 for (int i
= 0; i
< n
; i
++)
234 m_clientDataList
.Append( (wxObject
*) NULL
);
235 m_clientObjectList
.Append( (wxObject
*) NULL
);
237 GtkWidget
*list_item
;
241 wxString str
= "[-] ";
243 list_item
= gtk_list_item_new_with_label( str
);
247 list_item
= gtk_list_item_new_with_label( choices
[i
] );
251 debug_focus_in( list_item
, "wxListBox::list_item", name
);
254 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
256 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
257 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
259 if (style
& wxLB_MULTIPLE
)
260 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
261 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
263 gtk_signal_connect( GTK_OBJECT(list_item
),
264 "button_press_event",
265 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
270 gtk_signal_connect( GTK_OBJECT(list_item
),
272 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
276 ConnectWidget( list_item
);
278 gtk_widget_show( list_item
);
281 m_parent
->AddChild( this );
283 (m_parent
->m_insertCallback
)( m_parent
, this );
287 gtk_widget_realize( GTK_WIDGET(m_list
) );
289 SetBackgroundColour( parent
->GetBackgroundColour() );
290 SetForegroundColour( parent
->GetForegroundColour() );
297 wxListBox::~wxListBox()
302 void wxListBox::AppendCommon( const wxString
&item
)
304 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
306 GtkWidget
*list_item
;
310 wxString str
= "[-] ";
312 list_item
= gtk_list_item_new_with_label( str
);
316 list_item
= gtk_list_item_new_with_label( item
);
319 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
320 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
322 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
323 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
324 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
326 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
328 if (m_widgetStyle
) ApplyWidgetStyle();
330 gtk_signal_connect( GTK_OBJECT(list_item
),
331 "button_press_event",
332 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
337 gtk_signal_connect( GTK_OBJECT(list_item
),
339 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
343 gtk_widget_show( list_item
);
345 ConnectWidget( list_item
);
347 #if wxUSE_DRAG_AND_DROP
348 #ifndef NEW_GTK_DND_CODE
349 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
354 if (m_toolTip
) m_toolTip
->Apply( this );
358 void wxListBox::Append( const wxString
&item
)
360 m_clientDataList
.Append( (wxObject
*) NULL
);
361 m_clientObjectList
.Append( (wxObject
*) NULL
);
363 AppendCommon( item
);
366 void wxListBox::Append( const wxString
&item
, void *clientData
)
368 m_clientDataList
.Append( (wxObject
*) clientData
);
369 m_clientObjectList
.Append( (wxObject
*) NULL
);
371 AppendCommon( item
);
374 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
376 m_clientObjectList
.Append( (wxObject
*) clientData
);
377 m_clientDataList
.Append( (wxObject
*) NULL
);
379 AppendCommon( item
);
382 void wxListBox::SetClientData( int n
, void* clientData
)
384 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
386 wxNode
*node
= m_clientDataList
.Nth( n
);
389 node
->SetData( (wxObject
*) clientData
);
392 void* wxListBox::GetClientData( int n
)
394 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
396 wxNode
*node
= m_clientDataList
.Nth( n
);
397 if (!node
) return NULL
;
402 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
404 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
406 wxNode
*node
= m_clientObjectList
.Nth( n
);
409 wxClientData
*cd
= (wxClientData
*) node
->Data();
412 node
->SetData( (wxObject
*) clientData
);
415 wxClientData
* wxListBox::GetClientObject( int n
)
417 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
419 wxNode
*node
= m_clientObjectList
.Nth( n
);
420 if (!node
) return (wxClientData
*) NULL
;
422 return (wxClientData
*) node
->Data();
425 void wxListBox::Clear()
427 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
429 gtk_list_clear_items( m_list
, 0, Number() );
431 wxNode
*node
= m_clientObjectList
.First();
434 wxClientData
*cd
= (wxClientData
*)node
->Data();
438 m_clientObjectList
.Clear();
440 m_clientDataList
.Clear();
443 void wxListBox::Delete( int n
)
445 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
447 GList
*child
= g_list_nth( m_list
->children
, n
);
449 wxCHECK_RET( child
, "wrong listbox index" );
451 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
452 gtk_list_remove_items( m_list
, list
);
455 wxNode
*node
= m_clientObjectList
.Nth( n
);
458 wxClientData
*cd
= (wxClientData
*)node
->Data();
460 m_clientObjectList
.DeleteNode( node
);
463 node
= m_clientDataList
.Nth( n
);
466 m_clientDataList
.DeleteNode( node
);
470 void wxListBox::Deselect( int n
)
472 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
474 gtk_list_unselect_item( m_list
, n
);
477 int wxListBox::FindString( const wxString
&item
) const
479 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
481 GList
*child
= m_list
->children
;
485 GtkBin
*bin
= GTK_BIN( child
->data
);
486 GtkLabel
*label
= GTK_LABEL( bin
->child
);
488 wxString str
= label
->label
;
489 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
491 if (str
== item
) return count
;
497 // it's not an error if the string is not found -> no wxCHECK
502 int wxListBox::GetSelection() const
504 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
506 GList
*child
= m_list
->children
;
510 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
517 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
519 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
521 // get the number of selected items first
522 GList
*child
= m_list
->children
;
524 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
526 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
535 aSelections
.Alloc(count
); // optimization attempt
537 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
539 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
547 wxString
wxListBox::GetString( int n
) const
549 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
551 GList
*child
= g_list_nth( m_list
->children
, n
);
554 GtkBin
*bin
= GTK_BIN( child
->data
);
555 GtkLabel
*label
= GTK_LABEL( bin
->child
);
557 wxString str
= label
->label
;
558 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
562 wxFAIL_MSG("wrong listbox index");
566 wxString
wxListBox::GetStringSelection() const
568 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
570 GList
*selection
= m_list
->selection
;
573 GtkBin
*bin
= GTK_BIN( selection
->data
);
574 GtkLabel
*label
= GTK_LABEL( bin
->child
);
576 wxString str
= label
->label
;
577 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
582 wxFAIL_MSG("no listbox selection available");
586 int wxListBox::Number()
588 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
590 GList
*child
= m_list
->children
;
592 while (child
) { count
++; child
= child
->next
; }
596 bool wxListBox::Selected( int n
)
598 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
600 GList
*target
= g_list_nth( m_list
->children
, n
);
603 GList
*child
= m_list
->selection
;
606 if (child
->data
== target
->data
) return TRUE
;
610 wxFAIL_MSG("wrong listbox index");
614 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
616 wxFAIL_MSG("wxListBox::Set not implemented");
619 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
621 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
624 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
626 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
629 void wxListBox::SetSelection( int n
, bool select
)
631 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
634 gtk_list_select_item( m_list
, n
);
636 gtk_list_unselect_item( m_list
, n
);
639 void wxListBox::SetString( int n
, const wxString
&string
)
641 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
643 GList
*child
= g_list_nth( m_list
->children
, n
);
646 GtkBin
*bin
= GTK_BIN( child
->data
);
647 GtkLabel
*label
= GTK_LABEL( bin
->child
);
650 if (m_hasCheckBoxes
) str
+= "[-] ";
653 gtk_label_set( label
, str
);
657 wxFAIL_MSG("wrong listbox index");
661 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
663 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
665 SetSelection( FindString(string
), select
);
668 int wxListBox::GetIndex( GtkWidget
*item
) const
672 GList
*child
= m_list
->children
;
676 if (GTK_WIDGET(child
->data
) == item
) return count
;
685 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const char *tip
)
687 GList
*child
= m_list
->children
;
690 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
694 #endif // wxUSE_TOOLTIPS
696 #if wxUSE_DRAG_AND_DROP
697 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
699 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
701 #ifndef NEW_GTK_DND_CODE
704 GList
*child
= m_list
->children
;
707 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
713 wxWindow::SetDropTarget( dropTarget
);
715 #ifndef NEW_GTK_DND_CODE
718 GList
*child
= m_list
->children
;
721 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
729 GtkWidget
*wxListBox::GetConnectWidget()
731 return GTK_WIDGET(m_list
);
734 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
736 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
738 GList
*child
= m_list
->children
;
741 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
742 if (bin
->window
== window
) return TRUE
;
749 void wxListBox::ApplyWidgetStyle()
753 if (m_backgroundColour
.Ok())
755 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
756 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
757 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
758 gdk_window_clear( window
);
761 GList
*child
= m_list
->children
;
764 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
766 GtkBin
*bin
= GTK_BIN( child
->data
);
767 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
768 gtk_widget_set_style( label
, m_widgetStyle
);