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 listbox
->GetEventHandler()->ProcessEvent( event
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
87 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
89 if (g_blockEventsOnDrag
) return FALSE
;
91 if (!listbox
->HasVMT()) return FALSE
;
93 if (gdk_event
->keyval
!= ' ') return FALSE
;
95 int sel
= listbox
->GetIndex( widget
);
97 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
99 clb
->Check( sel
, !clb
->IsChecked(sel
) );
101 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
102 event
.SetEventObject( listbox
);
104 listbox
->GetEventHandler()->ProcessEvent( event
);
109 //-----------------------------------------------------------------------------
110 // "select" and "deselect"
111 //-----------------------------------------------------------------------------
113 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
115 if (!listbox
->HasVMT()) return;
116 if (g_blockEventsOnDrag
) return;
118 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
120 wxArrayInt aSelections
;
121 int count
= listbox
->GetSelections(aSelections
);
124 event
.m_commandInt
= aSelections
[0] ;
125 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
126 wxString
str(listbox
->GetString(event
.m_commandInt
));
127 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
131 event
.m_commandInt
= -1 ;
132 event
.m_commandString
= copystring("") ;
135 event
.SetEventObject( listbox
);
137 listbox
->GetEventHandler()->ProcessEvent( event
);
138 if (event
.m_commandString
) delete[] event
.m_commandString
;
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
147 wxListBox::wxListBox()
149 m_list
= (GtkList
*) NULL
;
150 m_hasCheckBoxes
= FALSE
;
153 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
154 const wxPoint
&pos
, const wxSize
&size
,
155 int n
, const wxString choices
[],
156 long style
, const wxValidator
& validator
, const wxString
&name
)
159 m_acceptsFocus
= TRUE
;
161 PreCreation( parent
, id
, pos
, size
, style
, name
);
163 SetValidator( validator
);
165 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
166 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
167 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
169 m_list
= GTK_LIST( gtk_list_new() );
171 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
172 if (style
& wxLB_MULTIPLE
)
173 mode
= GTK_SELECTION_MULTIPLE
;
174 else if (style
& wxLB_EXTENDED
)
175 mode
= GTK_SELECTION_EXTENDED
;
177 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
179 #ifdef NEW_GTK_SCROLL_CODE
180 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
182 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
186 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
188 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
190 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
192 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
193 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
195 #ifdef NEW_GTK_SCROLL_CODE
196 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
198 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
201 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
204 gtk_widget_show( GTK_WIDGET(m_list
) );
206 wxSize newSize
= size
;
207 if (newSize
.x
== -1) newSize
.x
= 100;
208 if (newSize
.y
== -1) newSize
.y
= 110;
209 SetSize( newSize
.x
, newSize
.y
);
211 for (int i
= 0; i
< n
; i
++)
213 m_clientDataList
.Append( (wxObject
*) NULL
);
214 m_clientObjectList
.Append( (wxObject
*) NULL
);
216 GtkWidget
*list_item
;
220 wxString str
= "[-] ";
222 list_item
= gtk_list_item_new_with_label( str
);
226 list_item
= gtk_list_item_new_with_label( choices
[i
] );
230 debug_focus_in( list_item
, "wxListBox::list_item", name
);
233 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
235 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
236 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
238 if (style
& wxLB_MULTIPLE
)
239 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
240 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
244 gtk_signal_connect( GTK_OBJECT(list_item
),
245 "button_press_event",
246 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
250 gtk_signal_connect( GTK_OBJECT(list_item
),
252 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
255 ConnectWidget( list_item
);
257 gtk_widget_show( list_item
);
260 m_parent
->AddChild( this );
262 (m_parent
->m_insertCallback
)( m_parent
, this );
266 gtk_widget_realize( GTK_WIDGET(m_list
) );
268 SetBackgroundColour( parent
->GetBackgroundColour() );
269 SetForegroundColour( parent
->GetForegroundColour() );
276 wxListBox::~wxListBox()
281 void wxListBox::AppendCommon( const wxString
&item
)
283 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
285 GtkWidget
*list_item
;
289 wxString str
= "[-] ";
291 list_item
= gtk_list_item_new_with_label( str
);
295 list_item
= gtk_list_item_new_with_label( item
);
298 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
299 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
301 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
302 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
303 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
305 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
307 if (m_widgetStyle
) ApplyWidgetStyle();
311 gtk_signal_connect( GTK_OBJECT(list_item
),
312 "button_press_event",
313 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
317 gtk_signal_connect( GTK_OBJECT(list_item
),
319 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
322 gtk_widget_show( list_item
);
324 ConnectWidget( list_item
);
326 #if wxUSE_DRAG_AND_DROP
327 #ifndef NEW_GTK_DND_CODE
328 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
333 void wxListBox::Append( const wxString
&item
)
335 m_clientDataList
.Append( (wxObject
*) NULL
);
336 m_clientObjectList
.Append( (wxObject
*) NULL
);
338 AppendCommon( item
);
341 void wxListBox::Append( const wxString
&item
, void *clientData
)
343 m_clientDataList
.Append( (wxObject
*) clientData
);
344 m_clientObjectList
.Append( (wxObject
*) NULL
);
346 AppendCommon( item
);
349 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
351 m_clientObjectList
.Append( (wxObject
*) clientData
);
352 m_clientDataList
.Append( (wxObject
*) NULL
);
354 AppendCommon( item
);
357 void wxListBox::SetClientData( int n
, void* clientData
)
359 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
361 wxNode
*node
= m_clientDataList
.Nth( n
);
364 node
->SetData( (wxObject
*) clientData
);
367 void* wxListBox::GetClientData( int n
)
369 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
371 wxNode
*node
= m_clientDataList
.Nth( n
);
372 if (!node
) return NULL
;
377 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
379 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
381 wxNode
*node
= m_clientObjectList
.Nth( n
);
384 wxClientData
*cd
= (wxClientData
*) node
->Data();
387 node
->SetData( (wxObject
*) clientData
);
390 wxClientData
* wxListBox::GetClientObject( int n
)
392 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
394 wxNode
*node
= m_clientObjectList
.Nth( n
);
395 if (!node
) return (wxClientData
*) NULL
;
397 return (wxClientData
*) node
->Data();
400 void wxListBox::Clear()
402 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
404 gtk_list_clear_items( m_list
, 0, Number() );
406 wxNode
*node
= m_clientObjectList
.First();
409 wxClientData
*cd
= (wxClientData
*)node
->Data();
413 m_clientObjectList
.Clear();
415 m_clientDataList
.Clear();
418 void wxListBox::Delete( int n
)
420 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
422 GList
*child
= g_list_nth( m_list
->children
, n
);
424 wxCHECK_RET( child
, "wrong listbox index" );
426 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
427 gtk_list_remove_items( m_list
, list
);
430 wxNode
*node
= m_clientObjectList
.Nth( n
);
433 wxClientData
*cd
= (wxClientData
*)node
->Data();
435 m_clientObjectList
.DeleteNode( node
);
438 node
= m_clientDataList
.Nth( n
);
441 m_clientDataList
.DeleteNode( node
);
445 void wxListBox::Deselect( int n
)
447 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
449 gtk_list_unselect_item( m_list
, n
);
452 int wxListBox::FindString( const wxString
&item
) const
454 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
456 GList
*child
= m_list
->children
;
460 GtkBin
*bin
= GTK_BIN( child
->data
);
461 GtkLabel
*label
= GTK_LABEL( bin
->child
);
463 wxString str
= label
->label
;
464 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
466 if (str
== item
) return count
;
472 // it's not an error if the string is not found -> no wxCHECK
477 int wxListBox::GetSelection() const
479 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
481 GList
*child
= m_list
->children
;
485 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
492 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
494 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
496 // get the number of selected items first
497 GList
*child
= m_list
->children
;
499 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
501 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
510 aSelections
.Alloc(count
); // optimization attempt
512 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
514 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
522 wxString
wxListBox::GetString( int n
) const
524 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
526 GList
*child
= g_list_nth( m_list
->children
, n
);
529 GtkBin
*bin
= GTK_BIN( child
->data
);
530 GtkLabel
*label
= GTK_LABEL( bin
->child
);
532 wxString str
= label
->label
;
533 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
537 wxFAIL_MSG("wrong listbox index");
541 wxString
wxListBox::GetStringSelection() const
543 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
545 GList
*selection
= m_list
->selection
;
548 GtkBin
*bin
= GTK_BIN( selection
->data
);
549 GtkLabel
*label
= GTK_LABEL( bin
->child
);
551 wxString str
= label
->label
;
552 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
557 wxFAIL_MSG("no listbox selection available");
561 int wxListBox::Number()
563 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
565 GList
*child
= m_list
->children
;
567 while (child
) { count
++; child
= child
->next
; }
571 bool wxListBox::Selected( int n
)
573 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
575 GList
*target
= g_list_nth( m_list
->children
, n
);
578 GList
*child
= m_list
->selection
;
581 if (child
->data
== target
->data
) return TRUE
;
585 wxFAIL_MSG("wrong listbox index");
589 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
591 wxFAIL_MSG("wxListBox::Set not implemented");
594 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
596 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
599 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
601 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
604 void wxListBox::SetSelection( int n
, bool select
)
606 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
609 gtk_list_select_item( m_list
, n
);
611 gtk_list_unselect_item( m_list
, n
);
614 void wxListBox::SetString( int n
, const wxString
&string
)
616 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
618 GList
*child
= g_list_nth( m_list
->children
, n
);
621 GtkBin
*bin
= GTK_BIN( child
->data
);
622 GtkLabel
*label
= GTK_LABEL( bin
->child
);
625 if (m_hasCheckBoxes
) str
+= "[-] ";
628 gtk_label_set( label
, str
);
632 wxFAIL_MSG("wrong listbox index");
636 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
638 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
640 SetSelection( FindString(string
), select
);
643 int wxListBox::GetIndex( GtkWidget
*item
) const
647 GList
*child
= m_list
->children
;
651 if (GTK_WIDGET(child
->data
) == item
) return count
;
659 #if wxUSE_DRAG_AND_DROP
660 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
662 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
664 #ifndef NEW_GTK_DND_CODE
667 GList
*child
= m_list
->children
;
670 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
676 wxWindow::SetDropTarget( dropTarget
);
678 #ifndef NEW_GTK_DND_CODE
681 GList
*child
= m_list
->children
;
684 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
692 GtkWidget
*wxListBox::GetConnectWidget()
694 return GTK_WIDGET(m_list
);
697 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
699 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
701 GList
*child
= m_list
->children
;
704 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
705 if (bin
->window
== window
) return TRUE
;
712 void wxListBox::ApplyWidgetStyle()
716 if (m_backgroundColour
.Ok())
718 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
719 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
720 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
721 gdk_window_clear( window
);
724 GList
*child
= m_list
->children
;
727 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
729 GtkBin
*bin
= GTK_BIN( child
->data
);
730 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
731 gtk_widget_set_style( label
, m_widgetStyle
);