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 //-------------------------------------------------------------------------
22 // conditional compilation
23 //-------------------------------------------------------------------------
25 #if (GTK_MINOR_VERSION == 1)
26 #if (GTK_MICRO_VERSION >= 5)
27 #define NEW_GTK_SCROLL_CODE
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern bool g_blockEventsOnScroll
;
38 //-----------------------------------------------------------------------------
39 // "button_press_event"
40 //-----------------------------------------------------------------------------
43 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
45 if (g_blockEventsOnDrag
) return FALSE
;
46 if (g_blockEventsOnScroll
) return FALSE
;
48 if (!listbox
->HasVMT()) return FALSE
;
50 if (gdk_event
->x
> 15) return FALSE
;
52 int sel
= listbox
->GetIndex( widget
);
54 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
56 clb
->Check( sel
, !clb
->IsChecked(sel
) );
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
66 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
68 if (g_blockEventsOnDrag
) return FALSE
;
70 if (!listbox
->HasVMT()) return FALSE
;
72 if (gdk_event
->keyval
!= ' ') return FALSE
;
74 int sel
= listbox
->GetIndex( widget
);
76 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
78 clb
->Check( sel
, !clb
->IsChecked(sel
) );
83 //-----------------------------------------------------------------------------
84 // "select" and "deselect"
85 //-----------------------------------------------------------------------------
87 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
89 if (!listbox
->HasVMT()) return;
90 if (g_blockEventsOnDrag
) return;
92 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
94 wxArrayInt aSelections
;
95 int count
= listbox
->GetSelections(aSelections
);
98 event
.m_commandInt
= aSelections
[0] ;
99 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
100 wxString
str(listbox
->GetString(event
.m_commandInt
));
101 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
105 event
.m_commandInt
= -1 ;
106 event
.m_commandString
= copystring("") ;
109 event
.SetEventObject( listbox
);
111 listbox
->GetEventHandler()->ProcessEvent( event
);
112 if (event
.m_commandString
) delete[] event
.m_commandString
;
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
121 wxListBox::wxListBox()
123 m_list
= (GtkList
*) NULL
;
124 m_hasCheckBoxes
= FALSE
;
127 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
,
129 int n
, const wxString choices
[],
130 long style
, const wxValidator
& validator
, const wxString
&name
)
133 m_acceptsFocus
= TRUE
;
135 PreCreation( parent
, id
, pos
, size
, style
, name
);
137 SetValidator( validator
);
139 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
140 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
141 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
143 m_list
= GTK_LIST( gtk_list_new() );
145 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
146 if (style
& wxLB_MULTIPLE
)
147 mode
= GTK_SELECTION_MULTIPLE
;
148 else if (style
& wxLB_EXTENDED
)
149 mode
= GTK_SELECTION_EXTENDED
;
151 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
153 #ifdef NEW_GTK_SCROLL_CODE
154 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
156 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
160 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
162 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
164 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
166 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
167 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
169 #ifdef NEW_GTK_SCROLL_CODE
170 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
172 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
175 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
178 gtk_widget_show( GTK_WIDGET(m_list
) );
180 wxSize newSize
= size
;
181 if (newSize
.x
== -1) newSize
.x
= 100;
182 if (newSize
.y
== -1) newSize
.y
= 110;
183 SetSize( newSize
.x
, newSize
.y
);
185 for (int i
= 0; i
< n
; i
++)
187 m_clientDataList
.Append( (wxObject
*) NULL
);
188 m_clientObjectList
.Append( (wxObject
*) NULL
);
190 GtkWidget
*list_item
;
194 wxString str
= "[-] ";
196 list_item
= gtk_list_item_new_with_label( str
);
200 list_item
= gtk_list_item_new_with_label( choices
[i
] );
204 debug_focus_in( list_item
, "wxListBox::list_item", name
);
207 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
209 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
210 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
212 if (style
& wxLB_MULTIPLE
)
213 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
214 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
218 gtk_signal_connect( GTK_OBJECT(list_item
),
219 "button_press_event",
220 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
223 gtk_signal_connect( GTK_OBJECT(list_item
),
225 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
229 ConnectWidget( list_item
);
231 gtk_widget_show( list_item
);
234 m_parent
->AddChild( this );
236 (m_parent
->m_insertCallback
)( m_parent
, this );
240 gtk_widget_realize( GTK_WIDGET(m_list
) );
242 SetBackgroundColour( parent
->GetBackgroundColour() );
243 SetForegroundColour( parent
->GetForegroundColour() );
250 wxListBox::~wxListBox()
255 void wxListBox::AppendCommon( const wxString
&item
)
257 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
259 GtkWidget
*list_item
;
263 wxString str
= "[-] ";
265 list_item
= gtk_list_item_new_with_label( str
);
269 list_item
= gtk_list_item_new_with_label( item
);
272 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
273 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
275 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
276 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
277 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
279 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
281 if (m_widgetStyle
) ApplyWidgetStyle();
285 gtk_signal_connect( GTK_OBJECT(list_item
),
286 "button_press_event",
287 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
290 gtk_signal_connect( GTK_OBJECT(list_item
),
292 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
296 gtk_widget_show( list_item
);
298 ConnectWidget( list_item
);
300 #ifndef NEW_GTK_DND_CODE
301 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
305 void wxListBox::Append( const wxString
&item
)
307 m_clientDataList
.Append( (wxObject
*) NULL
);
308 m_clientObjectList
.Append( (wxObject
*) NULL
);
310 AppendCommon( item
);
313 void wxListBox::Append( const wxString
&item
, void *clientData
)
315 m_clientDataList
.Append( (wxObject
*) clientData
);
316 m_clientObjectList
.Append( (wxObject
*) NULL
);
318 AppendCommon( item
);
321 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
323 m_clientObjectList
.Append( (wxObject
*) clientData
);
324 m_clientDataList
.Append( (wxObject
*) NULL
);
326 AppendCommon( item
);
329 void wxListBox::SetClientData( int n
, void* clientData
)
331 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
333 wxNode
*node
= m_clientDataList
.Nth( n
);
336 node
->SetData( (wxObject
*) clientData
);
339 void* wxListBox::GetClientData( int n
)
341 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
343 wxNode
*node
= m_clientDataList
.Nth( n
);
344 if (!node
) return NULL
;
349 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
351 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
353 wxNode
*node
= m_clientObjectList
.Nth( n
);
356 wxClientData
*cd
= (wxClientData
*) node
->Data();
359 node
->SetData( (wxObject
*) clientData
);
362 wxClientData
* wxListBox::GetClientObject( int n
)
364 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
366 wxNode
*node
= m_clientObjectList
.Nth( n
);
367 if (!node
) return (wxClientData
*) NULL
;
369 return (wxClientData
*) node
->Data();
372 void wxListBox::Clear()
374 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
376 gtk_list_clear_items( m_list
, 0, Number() );
378 wxNode
*node
= m_clientObjectList
.First();
381 wxClientData
*cd
= (wxClientData
*)node
->Data();
385 m_clientObjectList
.Clear();
387 m_clientDataList
.Clear();
390 void wxListBox::Delete( int n
)
392 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
394 GList
*child
= g_list_nth( m_list
->children
, n
);
396 wxCHECK_RET( child
, "wrong listbox index" );
398 GList
*list
= g_list_append( NULL
, child
->data
);
399 gtk_list_remove_items( m_list
, list
);
402 wxNode
*node
= m_clientObjectList
.Nth( n
);
405 wxClientData
*cd
= (wxClientData
*)node
->Data();
407 m_clientObjectList
.DeleteNode( node
);
410 node
= m_clientDataList
.Nth( n
);
413 m_clientDataList
.DeleteNode( node
);
417 void wxListBox::Deselect( int n
)
419 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
421 gtk_list_unselect_item( m_list
, n
);
424 int wxListBox::FindString( const wxString
&item
) const
426 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
428 GList
*child
= m_list
->children
;
432 GtkBin
*bin
= GTK_BIN( child
->data
);
433 GtkLabel
*label
= GTK_LABEL( bin
->child
);
435 wxString str
= label
->label
;
436 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
438 if (str
== item
) return count
;
444 // it's not an error if the string is not found -> no wxCHECK
449 int wxListBox::GetSelection() const
451 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
453 GList
*child
= m_list
->children
;
457 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
464 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
466 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
468 // get the number of selected items first
469 GList
*child
= m_list
->children
;
471 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
473 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
482 aSelections
.Alloc(count
); // optimization attempt
484 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
486 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
494 wxString
wxListBox::GetString( int n
) const
496 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
498 GList
*child
= g_list_nth( m_list
->children
, n
);
501 GtkBin
*bin
= GTK_BIN( child
->data
);
502 GtkLabel
*label
= GTK_LABEL( bin
->child
);
504 wxString str
= label
->label
;
505 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
509 wxFAIL_MSG("wrong listbox index");
513 wxString
wxListBox::GetStringSelection() const
515 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
517 GList
*selection
= m_list
->selection
;
520 GtkBin
*bin
= GTK_BIN( selection
->data
);
521 GtkLabel
*label
= GTK_LABEL( bin
->child
);
523 wxString str
= label
->label
;
524 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
529 wxFAIL_MSG("no listbox selection available");
533 int wxListBox::Number()
535 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
537 GList
*child
= m_list
->children
;
539 while (child
) { count
++; child
= child
->next
; }
543 bool wxListBox::Selected( int n
)
545 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
547 GList
*target
= g_list_nth( m_list
->children
, n
);
550 GList
*child
= m_list
->selection
;
553 if (child
->data
== target
->data
) return TRUE
;
557 wxFAIL_MSG("wrong listbox index");
561 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
563 wxFAIL_MSG("wxListBox::Set not implemented");
566 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
568 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
571 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
573 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
576 void wxListBox::SetSelection( int n
, bool select
)
578 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
581 gtk_list_select_item( m_list
, n
);
583 gtk_list_unselect_item( m_list
, n
);
586 void wxListBox::SetString( int n
, const wxString
&string
)
588 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
590 GList
*child
= g_list_nth( m_list
->children
, n
);
593 GtkBin
*bin
= GTK_BIN( child
->data
);
594 GtkLabel
*label
= GTK_LABEL( bin
->child
);
597 if (m_hasCheckBoxes
) str
+= "[-] ";
600 gtk_label_set( label
, str
);
604 wxFAIL_MSG("wrong listbox index");
608 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
610 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
612 SetSelection( FindString(string
), select
);
615 int wxListBox::GetIndex( GtkWidget
*item
) const
619 GList
*child
= m_list
->children
;
623 if (GTK_WIDGET(child
->data
) == item
) return count
;
631 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
633 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
635 #ifndef NEW_GTK_DND_CODE
638 GList
*child
= m_list
->children
;
641 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
647 wxWindow::SetDropTarget( dropTarget
);
649 #ifndef NEW_GTK_DND_CODE
652 GList
*child
= m_list
->children
;
655 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
662 GtkWidget
*wxListBox::GetConnectWidget()
664 return GTK_WIDGET(m_list
);
667 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
669 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
671 GList
*child
= m_list
->children
;
674 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
675 if (bin
->window
== window
) return TRUE
;
682 void wxListBox::ApplyWidgetStyle()
686 if (m_backgroundColour
.Ok())
688 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
689 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
690 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
691 gdk_window_clear( window
);
694 GList
*child
= m_list
->children
;
697 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
699 GtkBin
*bin
= GTK_BIN( child
->data
);
700 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
701 gtk_widget_set_style( label
, m_widgetStyle
);