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"
24 //-------------------------------------------------------------------------
25 // conditional compilation
26 //-------------------------------------------------------------------------
28 #if (GTK_MINOR_VERSION == 1)
29 #if (GTK_MICRO_VERSION >= 5)
30 #define NEW_GTK_SCROLL_CODE
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
39 extern bool g_blockEventsOnScroll
;
41 //-----------------------------------------------------------------------------
42 // "button_press_event"
43 //-----------------------------------------------------------------------------
46 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
48 if (g_blockEventsOnDrag
) return FALSE
;
49 if (g_blockEventsOnScroll
) return FALSE
;
51 if (!listbox
->HasVMT()) return FALSE
;
53 int sel
= listbox
->GetIndex( widget
);
55 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
57 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
59 clb
->Check( sel
, !clb
->IsChecked(sel
) );
61 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
62 event
.SetEventObject( listbox
);
64 listbox
->GetEventHandler()->ProcessEvent( event
);
67 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
69 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
70 event
.SetEventObject( listbox
);
72 listbox
->GetEventHandler()->ProcessEvent( event
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
85 if (g_blockEventsOnDrag
) return FALSE
;
87 if (!listbox
->HasVMT()) return FALSE
;
89 if (gdk_event
->keyval
!= ' ') return FALSE
;
91 int sel
= listbox
->GetIndex( widget
);
93 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
95 clb
->Check( sel
, !clb
->IsChecked(sel
) );
97 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
98 event
.SetEventObject( listbox
);
100 listbox
->GetEventHandler()->ProcessEvent( event
);
105 //-----------------------------------------------------------------------------
106 // "select" and "deselect"
107 //-----------------------------------------------------------------------------
109 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
111 if (!listbox
->HasVMT()) return;
112 if (g_blockEventsOnDrag
) return;
114 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
116 wxArrayInt aSelections
;
117 int count
= listbox
->GetSelections(aSelections
);
120 event
.m_commandInt
= aSelections
[0] ;
121 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
122 wxString
str(listbox
->GetString(event
.m_commandInt
));
123 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
127 event
.m_commandInt
= -1 ;
128 event
.m_commandString
= copystring("") ;
131 event
.SetEventObject( listbox
);
133 listbox
->GetEventHandler()->ProcessEvent( event
);
134 if (event
.m_commandString
) delete[] event
.m_commandString
;
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
143 wxListBox::wxListBox()
145 m_list
= (GtkList
*) NULL
;
146 m_hasCheckBoxes
= FALSE
;
149 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
150 const wxPoint
&pos
, const wxSize
&size
,
151 int n
, const wxString choices
[],
152 long style
, const wxValidator
& validator
, const wxString
&name
)
155 m_acceptsFocus
= TRUE
;
157 PreCreation( parent
, id
, pos
, size
, style
, name
);
159 SetValidator( validator
);
161 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
162 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
163 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
165 m_list
= GTK_LIST( gtk_list_new() );
167 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
168 if (style
& wxLB_MULTIPLE
)
169 mode
= GTK_SELECTION_MULTIPLE
;
170 else if (style
& wxLB_EXTENDED
)
171 mode
= GTK_SELECTION_EXTENDED
;
173 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
175 #ifdef NEW_GTK_SCROLL_CODE
176 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
178 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
182 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
184 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
186 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
188 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
189 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
191 #ifdef NEW_GTK_SCROLL_CODE
192 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
194 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
197 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
200 gtk_widget_show( GTK_WIDGET(m_list
) );
202 wxSize newSize
= size
;
203 if (newSize
.x
== -1) newSize
.x
= 100;
204 if (newSize
.y
== -1) newSize
.y
= 110;
205 SetSize( newSize
.x
, newSize
.y
);
207 for (int i
= 0; i
< n
; i
++)
209 m_clientDataList
.Append( (wxObject
*) NULL
);
210 m_clientObjectList
.Append( (wxObject
*) NULL
);
212 GtkWidget
*list_item
;
216 wxString str
= "[-] ";
218 list_item
= gtk_list_item_new_with_label( str
);
222 list_item
= gtk_list_item_new_with_label( choices
[i
] );
226 debug_focus_in( list_item
, "wxListBox::list_item", name
);
229 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
231 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
232 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
234 if (style
& wxLB_MULTIPLE
)
235 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
236 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
240 gtk_signal_connect( GTK_OBJECT(list_item
),
241 "button_press_event",
242 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
246 gtk_signal_connect( GTK_OBJECT(list_item
),
248 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
251 ConnectWidget( list_item
);
253 gtk_widget_show( list_item
);
256 m_parent
->AddChild( this );
258 (m_parent
->m_insertCallback
)( m_parent
, this );
262 gtk_widget_realize( GTK_WIDGET(m_list
) );
264 SetBackgroundColour( parent
->GetBackgroundColour() );
265 SetForegroundColour( parent
->GetForegroundColour() );
272 wxListBox::~wxListBox()
277 void wxListBox::AppendCommon( const wxString
&item
)
279 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
281 GtkWidget
*list_item
;
285 wxString str
= "[-] ";
287 list_item
= gtk_list_item_new_with_label( str
);
291 list_item
= gtk_list_item_new_with_label( item
);
294 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
295 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
297 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
298 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
299 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
301 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
303 if (m_widgetStyle
) ApplyWidgetStyle();
307 gtk_signal_connect( GTK_OBJECT(list_item
),
308 "button_press_event",
309 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
313 gtk_signal_connect( GTK_OBJECT(list_item
),
315 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
318 gtk_widget_show( list_item
);
320 ConnectWidget( list_item
);
322 #ifdef wxUSE_DRAG_AND_DROP
323 #ifndef NEW_GTK_DND_CODE
324 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
329 void wxListBox::Append( const wxString
&item
)
331 m_clientDataList
.Append( (wxObject
*) NULL
);
332 m_clientObjectList
.Append( (wxObject
*) NULL
);
334 AppendCommon( item
);
337 void wxListBox::Append( const wxString
&item
, void *clientData
)
339 m_clientDataList
.Append( (wxObject
*) clientData
);
340 m_clientObjectList
.Append( (wxObject
*) NULL
);
342 AppendCommon( item
);
345 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
347 m_clientObjectList
.Append( (wxObject
*) clientData
);
348 m_clientDataList
.Append( (wxObject
*) NULL
);
350 AppendCommon( item
);
353 void wxListBox::SetClientData( int n
, void* clientData
)
355 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
357 wxNode
*node
= m_clientDataList
.Nth( n
);
360 node
->SetData( (wxObject
*) clientData
);
363 void* wxListBox::GetClientData( int n
)
365 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
367 wxNode
*node
= m_clientDataList
.Nth( n
);
368 if (!node
) return NULL
;
373 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
375 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
377 wxNode
*node
= m_clientObjectList
.Nth( n
);
380 wxClientData
*cd
= (wxClientData
*) node
->Data();
383 node
->SetData( (wxObject
*) clientData
);
386 wxClientData
* wxListBox::GetClientObject( int n
)
388 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
390 wxNode
*node
= m_clientObjectList
.Nth( n
);
391 if (!node
) return (wxClientData
*) NULL
;
393 return (wxClientData
*) node
->Data();
396 void wxListBox::Clear()
398 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
400 gtk_list_clear_items( m_list
, 0, Number() );
402 wxNode
*node
= m_clientObjectList
.First();
405 wxClientData
*cd
= (wxClientData
*)node
->Data();
409 m_clientObjectList
.Clear();
411 m_clientDataList
.Clear();
414 void wxListBox::Delete( int n
)
416 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
418 GList
*child
= g_list_nth( m_list
->children
, n
);
420 wxCHECK_RET( child
, "wrong listbox index" );
422 GList
*list
= g_list_append( NULL
, child
->data
);
423 gtk_list_remove_items( m_list
, list
);
426 wxNode
*node
= m_clientObjectList
.Nth( n
);
429 wxClientData
*cd
= (wxClientData
*)node
->Data();
431 m_clientObjectList
.DeleteNode( node
);
434 node
= m_clientDataList
.Nth( n
);
437 m_clientDataList
.DeleteNode( node
);
441 void wxListBox::Deselect( int n
)
443 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
445 gtk_list_unselect_item( m_list
, n
);
448 int wxListBox::FindString( const wxString
&item
) const
450 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
452 GList
*child
= m_list
->children
;
456 GtkBin
*bin
= GTK_BIN( child
->data
);
457 GtkLabel
*label
= GTK_LABEL( bin
->child
);
459 wxString str
= label
->label
;
460 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
462 if (str
== item
) return count
;
468 // it's not an error if the string is not found -> no wxCHECK
473 int wxListBox::GetSelection() const
475 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
477 GList
*child
= m_list
->children
;
481 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
488 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
490 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
492 // get the number of selected items first
493 GList
*child
= m_list
->children
;
495 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
497 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
506 aSelections
.Alloc(count
); // optimization attempt
508 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
510 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
518 wxString
wxListBox::GetString( int n
) const
520 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
522 GList
*child
= g_list_nth( m_list
->children
, n
);
525 GtkBin
*bin
= GTK_BIN( child
->data
);
526 GtkLabel
*label
= GTK_LABEL( bin
->child
);
528 wxString str
= label
->label
;
529 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
533 wxFAIL_MSG("wrong listbox index");
537 wxString
wxListBox::GetStringSelection() const
539 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
541 GList
*selection
= m_list
->selection
;
544 GtkBin
*bin
= GTK_BIN( selection
->data
);
545 GtkLabel
*label
= GTK_LABEL( bin
->child
);
547 wxString str
= label
->label
;
548 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
553 wxFAIL_MSG("no listbox selection available");
557 int wxListBox::Number()
559 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
561 GList
*child
= m_list
->children
;
563 while (child
) { count
++; child
= child
->next
; }
567 bool wxListBox::Selected( int n
)
569 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
571 GList
*target
= g_list_nth( m_list
->children
, n
);
574 GList
*child
= m_list
->selection
;
577 if (child
->data
== target
->data
) return TRUE
;
581 wxFAIL_MSG("wrong listbox index");
585 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
587 wxFAIL_MSG("wxListBox::Set not implemented");
590 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
592 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
595 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
597 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
600 void wxListBox::SetSelection( int n
, bool select
)
602 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
605 gtk_list_select_item( m_list
, n
);
607 gtk_list_unselect_item( m_list
, n
);
610 void wxListBox::SetString( int n
, const wxString
&string
)
612 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
614 GList
*child
= g_list_nth( m_list
->children
, n
);
617 GtkBin
*bin
= GTK_BIN( child
->data
);
618 GtkLabel
*label
= GTK_LABEL( bin
->child
);
621 if (m_hasCheckBoxes
) str
+= "[-] ";
624 gtk_label_set( label
, str
);
628 wxFAIL_MSG("wrong listbox index");
632 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
634 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
636 SetSelection( FindString(string
), select
);
639 int wxListBox::GetIndex( GtkWidget
*item
) const
643 GList
*child
= m_list
->children
;
647 if (GTK_WIDGET(child
->data
) == item
) return count
;
655 #ifdef wxUSE_DRAG_AND_DROP
656 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
658 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
660 #ifndef NEW_GTK_DND_CODE
663 GList
*child
= m_list
->children
;
666 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
672 wxWindow::SetDropTarget( dropTarget
);
674 #ifndef NEW_GTK_DND_CODE
677 GList
*child
= m_list
->children
;
680 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
688 GtkWidget
*wxListBox::GetConnectWidget()
690 return GTK_WIDGET(m_list
);
693 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
695 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
697 GList
*child
= m_list
->children
;
700 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
701 if (bin
->window
== window
) return TRUE
;
708 void wxListBox::ApplyWidgetStyle()
712 if (m_backgroundColour
.Ok())
714 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
715 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
716 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
717 gdk_window_clear( window
);
720 GList
*child
= m_list
->children
;
723 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
725 GtkBin
*bin
= GTK_BIN( child
->data
);
726 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
727 gtk_widget_set_style( label
, m_widgetStyle
);