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/tooltip.h"
22 #if wxUSE_DRAG_AND_DROP
29 //-------------------------------------------------------------------------
30 // conditional compilation
31 //-------------------------------------------------------------------------
33 #if (GTK_MINOR_VERSION == 1)
34 #if (GTK_MICRO_VERSION >= 5)
35 #define NEW_GTK_SCROLL_CODE
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
44 extern bool g_blockEventsOnScroll
;
46 //-----------------------------------------------------------------------------
47 // "button_press_event"
48 //-----------------------------------------------------------------------------
51 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
53 if (g_blockEventsOnDrag
) return FALSE
;
54 if (g_blockEventsOnScroll
) return FALSE
;
56 if (!listbox
->HasVMT()) return FALSE
;
58 int sel
= listbox
->GetIndex( widget
);
60 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
62 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
64 clb
->Check( sel
, !clb
->IsChecked(sel
) );
66 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
67 event
.SetEventObject( listbox
);
69 listbox
->GetEventHandler()->ProcessEvent( event
);
72 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
74 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
75 event
.SetEventObject( listbox
);
77 wxArrayInt aSelections
;
78 int count
= listbox
->GetSelections(aSelections
);
81 event
.m_commandInt
= aSelections
[0] ;
82 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
83 wxString
str(listbox
->GetString(event
.m_commandInt
));
84 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
88 event
.m_commandInt
= -1 ;
89 event
.m_commandString
= copystring("") ;
92 listbox
->GetEventHandler()->ProcessEvent( event
);
94 if (event
.m_commandString
) delete[] event
.m_commandString
;
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
105 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
107 if (g_blockEventsOnDrag
) return FALSE
;
109 if (!listbox
->HasVMT()) return FALSE
;
111 if (gdk_event
->keyval
!= ' ') return FALSE
;
113 int sel
= listbox
->GetIndex( widget
);
115 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
117 clb
->Check( sel
, !clb
->IsChecked(sel
) );
119 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
120 event
.SetEventObject( listbox
);
122 listbox
->GetEventHandler()->ProcessEvent( event
);
127 //-----------------------------------------------------------------------------
128 // "select" and "deselect"
129 //-----------------------------------------------------------------------------
131 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
133 if (!listbox
->HasVMT()) return;
134 if (g_blockEventsOnDrag
) return;
136 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
138 wxArrayInt aSelections
;
139 int count
= listbox
->GetSelections(aSelections
);
142 event
.m_commandInt
= aSelections
[0] ;
143 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
144 wxString
str(listbox
->GetString(event
.m_commandInt
));
145 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
149 event
.m_commandInt
= -1 ;
150 event
.m_commandString
= copystring("") ;
153 event
.SetEventObject( listbox
);
155 listbox
->GetEventHandler()->ProcessEvent( event
);
156 if (event
.m_commandString
) delete[] event
.m_commandString
;
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
163 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
165 wxListBox::wxListBox()
167 m_list
= (GtkList
*) NULL
;
168 m_hasCheckBoxes
= FALSE
;
171 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
172 const wxPoint
&pos
, const wxSize
&size
,
173 int n
, const wxString choices
[],
174 long style
, const wxValidator
& validator
, const wxString
&name
)
177 m_acceptsFocus
= TRUE
;
179 PreCreation( parent
, id
, pos
, size
, style
, name
);
181 SetValidator( validator
);
183 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
184 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
185 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
187 m_list
= GTK_LIST( gtk_list_new() );
189 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
190 if (style
& wxLB_MULTIPLE
)
191 mode
= GTK_SELECTION_MULTIPLE
;
192 else if (style
& wxLB_EXTENDED
)
193 mode
= GTK_SELECTION_EXTENDED
;
195 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
197 #ifdef NEW_GTK_SCROLL_CODE
198 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
200 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
204 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
206 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
208 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
210 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
211 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
213 #ifdef NEW_GTK_SCROLL_CODE
214 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
216 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
219 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
222 gtk_widget_show( GTK_WIDGET(m_list
) );
224 wxSize newSize
= size
;
225 if (newSize
.x
== -1) newSize
.x
= 100;
226 if (newSize
.y
== -1) newSize
.y
= 110;
227 SetSize( newSize
.x
, newSize
.y
);
229 for (int i
= 0; i
< n
; i
++)
231 m_clientDataList
.Append( (wxObject
*) NULL
);
232 m_clientObjectList
.Append( (wxObject
*) NULL
);
234 GtkWidget
*list_item
;
238 wxString str
= "[-] ";
240 list_item
= gtk_list_item_new_with_label( str
);
244 list_item
= gtk_list_item_new_with_label( choices
[i
] );
248 debug_focus_in( list_item
, "wxListBox::list_item", name
);
251 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
253 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
254 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
256 if (style
& wxLB_MULTIPLE
)
257 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
258 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
260 gtk_signal_connect( GTK_OBJECT(list_item
),
261 "button_press_event",
262 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
267 gtk_signal_connect( GTK_OBJECT(list_item
),
269 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
273 ConnectWidget( list_item
);
275 gtk_widget_show( list_item
);
278 m_parent
->AddChild( this );
280 (m_parent
->m_insertCallback
)( m_parent
, this );
284 gtk_widget_realize( GTK_WIDGET(m_list
) );
286 SetBackgroundColour( parent
->GetBackgroundColour() );
287 SetForegroundColour( parent
->GetForegroundColour() );
294 wxListBox::~wxListBox()
299 void wxListBox::AppendCommon( const wxString
&item
)
301 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
303 GtkWidget
*list_item
;
307 wxString str
= "[-] ";
309 list_item
= gtk_list_item_new_with_label( str
);
313 list_item
= gtk_list_item_new_with_label( item
);
316 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
317 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
319 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
320 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
321 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
323 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
325 if (m_widgetStyle
) ApplyWidgetStyle();
327 gtk_signal_connect( GTK_OBJECT(list_item
),
328 "button_press_event",
329 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
334 gtk_signal_connect( GTK_OBJECT(list_item
),
336 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
340 gtk_widget_show( list_item
);
342 ConnectWidget( list_item
);
344 #if wxUSE_DRAG_AND_DROP
345 #ifndef NEW_GTK_DND_CODE
346 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
350 if (m_toolTip
) m_toolTip
->Apply( this );
353 void wxListBox::Append( const wxString
&item
)
355 m_clientDataList
.Append( (wxObject
*) NULL
);
356 m_clientObjectList
.Append( (wxObject
*) NULL
);
358 AppendCommon( item
);
361 void wxListBox::Append( const wxString
&item
, void *clientData
)
363 m_clientDataList
.Append( (wxObject
*) clientData
);
364 m_clientObjectList
.Append( (wxObject
*) NULL
);
366 AppendCommon( item
);
369 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
371 m_clientObjectList
.Append( (wxObject
*) clientData
);
372 m_clientDataList
.Append( (wxObject
*) NULL
);
374 AppendCommon( item
);
377 void wxListBox::SetClientData( int n
, void* clientData
)
379 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
381 wxNode
*node
= m_clientDataList
.Nth( n
);
384 node
->SetData( (wxObject
*) clientData
);
387 void* wxListBox::GetClientData( int n
)
389 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
391 wxNode
*node
= m_clientDataList
.Nth( n
);
392 if (!node
) return NULL
;
397 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
399 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
401 wxNode
*node
= m_clientObjectList
.Nth( n
);
404 wxClientData
*cd
= (wxClientData
*) node
->Data();
407 node
->SetData( (wxObject
*) clientData
);
410 wxClientData
* wxListBox::GetClientObject( int n
)
412 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
414 wxNode
*node
= m_clientObjectList
.Nth( n
);
415 if (!node
) return (wxClientData
*) NULL
;
417 return (wxClientData
*) node
->Data();
420 void wxListBox::Clear()
422 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
424 gtk_list_clear_items( m_list
, 0, Number() );
426 wxNode
*node
= m_clientObjectList
.First();
429 wxClientData
*cd
= (wxClientData
*)node
->Data();
433 m_clientObjectList
.Clear();
435 m_clientDataList
.Clear();
438 void wxListBox::Delete( int n
)
440 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
442 GList
*child
= g_list_nth( m_list
->children
, n
);
444 wxCHECK_RET( child
, "wrong listbox index" );
446 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
447 gtk_list_remove_items( m_list
, list
);
450 wxNode
*node
= m_clientObjectList
.Nth( n
);
453 wxClientData
*cd
= (wxClientData
*)node
->Data();
455 m_clientObjectList
.DeleteNode( node
);
458 node
= m_clientDataList
.Nth( n
);
461 m_clientDataList
.DeleteNode( node
);
465 void wxListBox::Deselect( int n
)
467 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
469 gtk_list_unselect_item( m_list
, n
);
472 int wxListBox::FindString( const wxString
&item
) const
474 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
476 GList
*child
= m_list
->children
;
480 GtkBin
*bin
= GTK_BIN( child
->data
);
481 GtkLabel
*label
= GTK_LABEL( bin
->child
);
483 wxString str
= label
->label
;
484 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
486 if (str
== item
) return count
;
492 // it's not an error if the string is not found -> no wxCHECK
497 int wxListBox::GetSelection() const
499 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
501 GList
*child
= m_list
->children
;
505 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
512 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
514 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
516 // get the number of selected items first
517 GList
*child
= m_list
->children
;
519 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
521 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
530 aSelections
.Alloc(count
); // optimization attempt
532 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
534 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
542 wxString
wxListBox::GetString( int n
) const
544 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
546 GList
*child
= g_list_nth( m_list
->children
, n
);
549 GtkBin
*bin
= GTK_BIN( child
->data
);
550 GtkLabel
*label
= GTK_LABEL( bin
->child
);
552 wxString str
= label
->label
;
553 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
557 wxFAIL_MSG("wrong listbox index");
561 wxString
wxListBox::GetStringSelection() const
563 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
565 GList
*selection
= m_list
->selection
;
568 GtkBin
*bin
= GTK_BIN( selection
->data
);
569 GtkLabel
*label
= GTK_LABEL( bin
->child
);
571 wxString str
= label
->label
;
572 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
577 wxFAIL_MSG("no listbox selection available");
581 int wxListBox::Number()
583 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
585 GList
*child
= m_list
->children
;
587 while (child
) { count
++; child
= child
->next
; }
591 bool wxListBox::Selected( int n
)
593 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
595 GList
*target
= g_list_nth( m_list
->children
, n
);
598 GList
*child
= m_list
->selection
;
601 if (child
->data
== target
->data
) return TRUE
;
605 wxFAIL_MSG("wrong listbox index");
609 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
611 wxFAIL_MSG("wxListBox::Set not implemented");
614 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
616 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
619 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
621 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
624 void wxListBox::SetSelection( int n
, bool select
)
626 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
629 gtk_list_select_item( m_list
, n
);
631 gtk_list_unselect_item( m_list
, n
);
634 void wxListBox::SetString( int n
, const wxString
&string
)
636 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
638 GList
*child
= g_list_nth( m_list
->children
, n
);
641 GtkBin
*bin
= GTK_BIN( child
->data
);
642 GtkLabel
*label
= GTK_LABEL( bin
->child
);
645 if (m_hasCheckBoxes
) str
+= "[-] ";
648 gtk_label_set( label
, str
);
652 wxFAIL_MSG("wrong listbox index");
656 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
658 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
660 SetSelection( FindString(string
), select
);
663 int wxListBox::GetIndex( GtkWidget
*item
) const
667 GList
*child
= m_list
->children
;
671 if (GTK_WIDGET(child
->data
) == item
) return count
;
679 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const char *tip
)
681 GList
*child
= m_list
->children
;
684 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
689 #if wxUSE_DRAG_AND_DROP
690 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
692 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
694 #ifndef NEW_GTK_DND_CODE
697 GList
*child
= m_list
->children
;
700 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
706 wxWindow::SetDropTarget( dropTarget
);
708 #ifndef NEW_GTK_DND_CODE
711 GList
*child
= m_list
->children
;
714 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
722 GtkWidget
*wxListBox::GetConnectWidget()
724 return GTK_WIDGET(m_list
);
727 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
729 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
731 GList
*child
= m_list
->children
;
734 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
735 if (bin
->window
== window
) return TRUE
;
742 void wxListBox::ApplyWidgetStyle()
746 if (m_backgroundColour
.Ok())
748 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
749 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
750 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
751 gdk_window_clear( window
);
754 GList
*child
= m_list
->children
;
757 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
759 GtkBin
*bin
= GTK_BIN( child
->data
);
760 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
761 gtk_widget_set_style( label
, m_widgetStyle
);