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"
21 #if wxUSE_DRAG_AND_DROP
28 //-------------------------------------------------------------------------
29 // conditional compilation
30 //-------------------------------------------------------------------------
32 #if (GTK_MINOR_VERSION == 1)
33 #if (GTK_MICRO_VERSION >= 5)
34 #define NEW_GTK_SCROLL_CODE
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
43 extern bool g_blockEventsOnScroll
;
45 //-----------------------------------------------------------------------------
46 // "button_press_event"
47 //-----------------------------------------------------------------------------
50 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
52 if (g_blockEventsOnDrag
) return FALSE
;
53 if (g_blockEventsOnScroll
) return FALSE
;
55 if (!listbox
->HasVMT()) return FALSE
;
57 int sel
= listbox
->GetIndex( widget
);
59 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
61 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
63 clb
->Check( sel
, !clb
->IsChecked(sel
) );
65 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
66 event
.SetEventObject( listbox
);
68 listbox
->GetEventHandler()->ProcessEvent( event
);
71 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
73 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
74 event
.SetEventObject( listbox
);
76 wxArrayInt aSelections
;
77 int count
= listbox
->GetSelections(aSelections
);
80 event
.m_commandInt
= aSelections
[0] ;
81 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
82 wxString
str(listbox
->GetString(event
.m_commandInt
));
83 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
87 event
.m_commandInt
= -1 ;
88 event
.m_commandString
= copystring("") ;
91 listbox
->GetEventHandler()->ProcessEvent( event
);
93 if (event
.m_commandString
) delete[] event
.m_commandString
;
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
104 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
106 if (g_blockEventsOnDrag
) return FALSE
;
108 if (!listbox
->HasVMT()) return FALSE
;
110 if (gdk_event
->keyval
!= ' ') return FALSE
;
112 int sel
= listbox
->GetIndex( widget
);
114 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
116 clb
->Check( sel
, !clb
->IsChecked(sel
) );
118 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
119 event
.SetEventObject( listbox
);
121 listbox
->GetEventHandler()->ProcessEvent( event
);
126 //-----------------------------------------------------------------------------
127 // "select" and "deselect"
128 //-----------------------------------------------------------------------------
130 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
132 if (!listbox
->HasVMT()) return;
133 if (g_blockEventsOnDrag
) return;
135 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
137 wxArrayInt aSelections
;
138 int count
= listbox
->GetSelections(aSelections
);
141 event
.m_commandInt
= aSelections
[0] ;
142 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
143 wxString
str(listbox
->GetString(event
.m_commandInt
));
144 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
148 event
.m_commandInt
= -1 ;
149 event
.m_commandString
= copystring("") ;
152 event
.SetEventObject( listbox
);
154 listbox
->GetEventHandler()->ProcessEvent( event
);
155 if (event
.m_commandString
) delete[] event
.m_commandString
;
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
164 wxListBox::wxListBox()
166 m_list
= (GtkList
*) NULL
;
167 m_hasCheckBoxes
= FALSE
;
170 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
171 const wxPoint
&pos
, const wxSize
&size
,
172 int n
, const wxString choices
[],
173 long style
, const wxValidator
& validator
, const wxString
&name
)
176 m_acceptsFocus
= TRUE
;
178 PreCreation( parent
, id
, pos
, size
, style
, name
);
180 SetValidator( validator
);
182 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
183 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
184 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
186 m_list
= GTK_LIST( gtk_list_new() );
188 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
189 if (style
& wxLB_MULTIPLE
)
190 mode
= GTK_SELECTION_MULTIPLE
;
191 else if (style
& wxLB_EXTENDED
)
192 mode
= GTK_SELECTION_EXTENDED
;
194 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
196 #ifdef NEW_GTK_SCROLL_CODE
197 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
199 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
203 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
205 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
207 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
209 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
210 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
212 #ifdef NEW_GTK_SCROLL_CODE
213 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
215 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
218 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
221 gtk_widget_show( GTK_WIDGET(m_list
) );
223 wxSize newSize
= size
;
224 if (newSize
.x
== -1) newSize
.x
= 100;
225 if (newSize
.y
== -1) newSize
.y
= 110;
226 SetSize( newSize
.x
, newSize
.y
);
228 for (int i
= 0; i
< n
; i
++)
230 m_clientDataList
.Append( (wxObject
*) NULL
);
231 m_clientObjectList
.Append( (wxObject
*) NULL
);
233 GtkWidget
*list_item
;
237 wxString str
= "[-] ";
239 list_item
= gtk_list_item_new_with_label( str
);
243 list_item
= gtk_list_item_new_with_label( choices
[i
] );
247 debug_focus_in( list_item
, "wxListBox::list_item", name
);
250 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
252 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
253 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
255 if (style
& wxLB_MULTIPLE
)
256 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
257 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
259 gtk_signal_connect( GTK_OBJECT(list_item
),
260 "button_press_event",
261 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
266 gtk_signal_connect( GTK_OBJECT(list_item
),
268 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
272 ConnectWidget( list_item
);
274 gtk_widget_show( list_item
);
277 m_parent
->AddChild( this );
279 (m_parent
->m_insertCallback
)( m_parent
, this );
283 gtk_widget_realize( GTK_WIDGET(m_list
) );
285 SetBackgroundColour( parent
->GetBackgroundColour() );
286 SetForegroundColour( parent
->GetForegroundColour() );
293 wxListBox::~wxListBox()
298 void wxListBox::AppendCommon( const wxString
&item
)
300 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
302 GtkWidget
*list_item
;
306 wxString str
= "[-] ";
308 list_item
= gtk_list_item_new_with_label( str
);
312 list_item
= gtk_list_item_new_with_label( item
);
315 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
316 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
318 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
319 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
320 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
322 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
324 if (m_widgetStyle
) ApplyWidgetStyle();
326 gtk_signal_connect( GTK_OBJECT(list_item
),
327 "button_press_event",
328 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
333 gtk_signal_connect( GTK_OBJECT(list_item
),
335 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
339 gtk_widget_show( list_item
);
341 ConnectWidget( list_item
);
343 #if wxUSE_DRAG_AND_DROP
344 #ifndef NEW_GTK_DND_CODE
345 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
350 void wxListBox::Append( const wxString
&item
)
352 m_clientDataList
.Append( (wxObject
*) NULL
);
353 m_clientObjectList
.Append( (wxObject
*) NULL
);
355 AppendCommon( item
);
358 void wxListBox::Append( const wxString
&item
, void *clientData
)
360 m_clientDataList
.Append( (wxObject
*) clientData
);
361 m_clientObjectList
.Append( (wxObject
*) NULL
);
363 AppendCommon( item
);
366 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
368 m_clientObjectList
.Append( (wxObject
*) clientData
);
369 m_clientDataList
.Append( (wxObject
*) NULL
);
371 AppendCommon( item
);
374 void wxListBox::SetClientData( int n
, void* clientData
)
376 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
378 wxNode
*node
= m_clientDataList
.Nth( n
);
381 node
->SetData( (wxObject
*) clientData
);
384 void* wxListBox::GetClientData( int n
)
386 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
388 wxNode
*node
= m_clientDataList
.Nth( n
);
389 if (!node
) return NULL
;
394 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
396 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
398 wxNode
*node
= m_clientObjectList
.Nth( n
);
401 wxClientData
*cd
= (wxClientData
*) node
->Data();
404 node
->SetData( (wxObject
*) clientData
);
407 wxClientData
* wxListBox::GetClientObject( int n
)
409 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
411 wxNode
*node
= m_clientObjectList
.Nth( n
);
412 if (!node
) return (wxClientData
*) NULL
;
414 return (wxClientData
*) node
->Data();
417 void wxListBox::Clear()
419 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
421 gtk_list_clear_items( m_list
, 0, Number() );
423 wxNode
*node
= m_clientObjectList
.First();
426 wxClientData
*cd
= (wxClientData
*)node
->Data();
430 m_clientObjectList
.Clear();
432 m_clientDataList
.Clear();
435 void wxListBox::Delete( int n
)
437 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
439 GList
*child
= g_list_nth( m_list
->children
, n
);
441 wxCHECK_RET( child
, "wrong listbox index" );
443 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
444 gtk_list_remove_items( m_list
, list
);
447 wxNode
*node
= m_clientObjectList
.Nth( n
);
450 wxClientData
*cd
= (wxClientData
*)node
->Data();
452 m_clientObjectList
.DeleteNode( node
);
455 node
= m_clientDataList
.Nth( n
);
458 m_clientDataList
.DeleteNode( node
);
462 void wxListBox::Deselect( int n
)
464 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
466 gtk_list_unselect_item( m_list
, n
);
469 int wxListBox::FindString( const wxString
&item
) const
471 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
473 GList
*child
= m_list
->children
;
477 GtkBin
*bin
= GTK_BIN( child
->data
);
478 GtkLabel
*label
= GTK_LABEL( bin
->child
);
480 wxString str
= label
->label
;
481 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
483 if (str
== item
) return count
;
489 // it's not an error if the string is not found -> no wxCHECK
494 int wxListBox::GetSelection() const
496 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
498 GList
*child
= m_list
->children
;
502 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
509 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
511 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
513 // get the number of selected items first
514 GList
*child
= m_list
->children
;
516 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
518 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
527 aSelections
.Alloc(count
); // optimization attempt
529 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
531 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
539 wxString
wxListBox::GetString( int n
) const
541 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
543 GList
*child
= g_list_nth( m_list
->children
, n
);
546 GtkBin
*bin
= GTK_BIN( child
->data
);
547 GtkLabel
*label
= GTK_LABEL( bin
->child
);
549 wxString str
= label
->label
;
550 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
554 wxFAIL_MSG("wrong listbox index");
558 wxString
wxListBox::GetStringSelection() const
560 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
562 GList
*selection
= m_list
->selection
;
565 GtkBin
*bin
= GTK_BIN( selection
->data
);
566 GtkLabel
*label
= GTK_LABEL( bin
->child
);
568 wxString str
= label
->label
;
569 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
574 wxFAIL_MSG("no listbox selection available");
578 int wxListBox::Number()
580 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
582 GList
*child
= m_list
->children
;
584 while (child
) { count
++; child
= child
->next
; }
588 bool wxListBox::Selected( int n
)
590 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
592 GList
*target
= g_list_nth( m_list
->children
, n
);
595 GList
*child
= m_list
->selection
;
598 if (child
->data
== target
->data
) return TRUE
;
602 wxFAIL_MSG("wrong listbox index");
606 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
608 wxFAIL_MSG("wxListBox::Set not implemented");
611 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
613 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
616 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
618 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
621 void wxListBox::SetSelection( int n
, bool select
)
623 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
626 gtk_list_select_item( m_list
, n
);
628 gtk_list_unselect_item( m_list
, n
);
631 void wxListBox::SetString( int n
, const wxString
&string
)
633 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
635 GList
*child
= g_list_nth( m_list
->children
, n
);
638 GtkBin
*bin
= GTK_BIN( child
->data
);
639 GtkLabel
*label
= GTK_LABEL( bin
->child
);
642 if (m_hasCheckBoxes
) str
+= "[-] ";
645 gtk_label_set( label
, str
);
649 wxFAIL_MSG("wrong listbox index");
653 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
655 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
657 SetSelection( FindString(string
), select
);
660 int wxListBox::GetIndex( GtkWidget
*item
) const
664 GList
*child
= m_list
->children
;
668 if (GTK_WIDGET(child
->data
) == item
) return count
;
676 #if wxUSE_DRAG_AND_DROP
677 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
679 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
681 #ifndef NEW_GTK_DND_CODE
684 GList
*child
= m_list
->children
;
687 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
693 wxWindow::SetDropTarget( dropTarget
);
695 #ifndef NEW_GTK_DND_CODE
698 GList
*child
= m_list
->children
;
701 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
709 GtkWidget
*wxListBox::GetConnectWidget()
711 return GTK_WIDGET(m_list
);
714 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
716 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
718 GList
*child
= m_list
->children
;
721 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
722 if (bin
->window
== window
) return TRUE
;
729 void wxListBox::ApplyWidgetStyle()
733 if (m_backgroundColour
.Ok())
735 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
736 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
737 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
738 gdk_window_clear( window
);
741 GList
*child
= m_list
->children
;
744 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
746 GtkBin
*bin
= GTK_BIN( child
->data
);
747 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
748 gtk_widget_set_style( label
, m_widgetStyle
);