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 );
242 gtk_signal_connect( GTK_OBJECT(list_item
),
243 "button_press_event",
244 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
247 gtk_signal_connect( GTK_OBJECT(list_item
),
249 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
252 ConnectWidget( list_item
);
254 gtk_widget_show( list_item
);
257 m_parent
->AddChild( this );
259 (m_parent
->m_insertCallback
)( m_parent
, this );
263 gtk_widget_realize( GTK_WIDGET(m_list
) );
265 SetBackgroundColour( parent
->GetBackgroundColour() );
266 SetForegroundColour( parent
->GetForegroundColour() );
273 wxListBox::~wxListBox()
278 void wxListBox::AppendCommon( const wxString
&item
)
280 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
282 GtkWidget
*list_item
;
286 wxString str
= "[-] ";
288 list_item
= gtk_list_item_new_with_label( str
);
292 list_item
= gtk_list_item_new_with_label( item
);
295 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
296 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
298 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
299 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
300 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
302 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
304 if (m_widgetStyle
) ApplyWidgetStyle();
306 gtk_signal_connect( GTK_OBJECT(list_item
),
307 "button_press_event",
308 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
311 gtk_signal_connect( GTK_OBJECT(list_item
),
313 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
316 gtk_widget_show( list_item
);
318 ConnectWidget( list_item
);
320 #if wxUSE_DRAG_AND_DROP
321 #ifndef NEW_GTK_DND_CODE
322 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
327 void wxListBox::Append( const wxString
&item
)
329 m_clientDataList
.Append( (wxObject
*) NULL
);
330 m_clientObjectList
.Append( (wxObject
*) NULL
);
332 AppendCommon( item
);
335 void wxListBox::Append( const wxString
&item
, void *clientData
)
337 m_clientDataList
.Append( (wxObject
*) clientData
);
338 m_clientObjectList
.Append( (wxObject
*) NULL
);
340 AppendCommon( item
);
343 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
345 m_clientObjectList
.Append( (wxObject
*) clientData
);
346 m_clientDataList
.Append( (wxObject
*) NULL
);
348 AppendCommon( item
);
351 void wxListBox::SetClientData( int n
, void* clientData
)
353 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
355 wxNode
*node
= m_clientDataList
.Nth( n
);
358 node
->SetData( (wxObject
*) clientData
);
361 void* wxListBox::GetClientData( int n
)
363 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
365 wxNode
*node
= m_clientDataList
.Nth( n
);
366 if (!node
) return NULL
;
371 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
373 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
375 wxNode
*node
= m_clientObjectList
.Nth( n
);
378 wxClientData
*cd
= (wxClientData
*) node
->Data();
381 node
->SetData( (wxObject
*) clientData
);
384 wxClientData
* wxListBox::GetClientObject( int n
)
386 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
388 wxNode
*node
= m_clientObjectList
.Nth( n
);
389 if (!node
) return (wxClientData
*) NULL
;
391 return (wxClientData
*) node
->Data();
394 void wxListBox::Clear()
396 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
398 gtk_list_clear_items( m_list
, 0, Number() );
400 wxNode
*node
= m_clientObjectList
.First();
403 wxClientData
*cd
= (wxClientData
*)node
->Data();
407 m_clientObjectList
.Clear();
409 m_clientDataList
.Clear();
412 void wxListBox::Delete( int n
)
414 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
416 GList
*child
= g_list_nth( m_list
->children
, n
);
418 wxCHECK_RET( child
, "wrong listbox index" );
420 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
421 gtk_list_remove_items( m_list
, list
);
424 wxNode
*node
= m_clientObjectList
.Nth( n
);
427 wxClientData
*cd
= (wxClientData
*)node
->Data();
429 m_clientObjectList
.DeleteNode( node
);
432 node
= m_clientDataList
.Nth( n
);
435 m_clientDataList
.DeleteNode( node
);
439 void wxListBox::Deselect( int n
)
441 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
443 gtk_list_unselect_item( m_list
, n
);
446 int wxListBox::FindString( const wxString
&item
) const
448 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
450 GList
*child
= m_list
->children
;
454 GtkBin
*bin
= GTK_BIN( child
->data
);
455 GtkLabel
*label
= GTK_LABEL( bin
->child
);
457 wxString str
= label
->label
;
458 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
460 if (str
== item
) return count
;
466 // it's not an error if the string is not found -> no wxCHECK
471 int wxListBox::GetSelection() const
473 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
475 GList
*child
= m_list
->children
;
479 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
486 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
488 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
490 // get the number of selected items first
491 GList
*child
= m_list
->children
;
493 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
495 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
504 aSelections
.Alloc(count
); // optimization attempt
506 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
508 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
516 wxString
wxListBox::GetString( int n
) const
518 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
520 GList
*child
= g_list_nth( m_list
->children
, n
);
523 GtkBin
*bin
= GTK_BIN( child
->data
);
524 GtkLabel
*label
= GTK_LABEL( bin
->child
);
526 wxString str
= label
->label
;
527 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
531 wxFAIL_MSG("wrong listbox index");
535 wxString
wxListBox::GetStringSelection() const
537 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
539 GList
*selection
= m_list
->selection
;
542 GtkBin
*bin
= GTK_BIN( selection
->data
);
543 GtkLabel
*label
= GTK_LABEL( bin
->child
);
545 wxString str
= label
->label
;
546 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
551 wxFAIL_MSG("no listbox selection available");
555 int wxListBox::Number()
557 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
559 GList
*child
= m_list
->children
;
561 while (child
) { count
++; child
= child
->next
; }
565 bool wxListBox::Selected( int n
)
567 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
569 GList
*target
= g_list_nth( m_list
->children
, n
);
572 GList
*child
= m_list
->selection
;
575 if (child
->data
== target
->data
) return TRUE
;
579 wxFAIL_MSG("wrong listbox index");
583 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
585 wxFAIL_MSG("wxListBox::Set not implemented");
588 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
590 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
593 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
595 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
598 void wxListBox::SetSelection( int n
, bool select
)
600 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
603 gtk_list_select_item( m_list
, n
);
605 gtk_list_unselect_item( m_list
, n
);
608 void wxListBox::SetString( int n
, const wxString
&string
)
610 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
612 GList
*child
= g_list_nth( m_list
->children
, n
);
615 GtkBin
*bin
= GTK_BIN( child
->data
);
616 GtkLabel
*label
= GTK_LABEL( bin
->child
);
619 if (m_hasCheckBoxes
) str
+= "[-] ";
622 gtk_label_set( label
, str
);
626 wxFAIL_MSG("wrong listbox index");
630 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
632 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
634 SetSelection( FindString(string
), select
);
637 int wxListBox::GetIndex( GtkWidget
*item
) const
641 GList
*child
= m_list
->children
;
645 if (GTK_WIDGET(child
->data
) == item
) return count
;
653 #if wxUSE_DRAG_AND_DROP
654 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
656 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
658 #ifndef NEW_GTK_DND_CODE
661 GList
*child
= m_list
->children
;
664 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
670 wxWindow::SetDropTarget( dropTarget
);
672 #ifndef NEW_GTK_DND_CODE
675 GList
*child
= m_list
->children
;
678 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
686 GtkWidget
*wxListBox::GetConnectWidget()
688 return GTK_WIDGET(m_list
);
691 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
693 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
695 GList
*child
= m_list
->children
;
698 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
699 if (bin
->window
== window
) return TRUE
;
706 void wxListBox::ApplyWidgetStyle()
710 if (m_backgroundColour
.Ok())
712 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
713 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
714 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
715 gdk_window_clear( window
);
718 GList
*child
= m_list
->children
;
721 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
723 GtkBin
*bin
= GTK_BIN( child
->data
);
724 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
725 gtk_widget_set_style( label
, m_widgetStyle
);