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"
22 #include "wx/tooltip.h"
25 #if wxUSE_DRAG_AND_DROP
32 //-------------------------------------------------------------------------
33 // conditional compilation
34 //-------------------------------------------------------------------------
36 #if (GTK_MINOR_VERSION == 1)
37 #if (GTK_MICRO_VERSION >= 5)
38 #define NEW_GTK_SCROLL_CODE
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern bool g_blockEventsOnDrag
;
47 extern bool g_blockEventsOnScroll
;
49 //-----------------------------------------------------------------------------
50 // "button_press_event"
51 //-----------------------------------------------------------------------------
54 gtk_listbox_button_press_callback( GtkWidget
*widget
, GdkEventButton
*gdk_event
, wxListBox
*listbox
)
56 if (g_blockEventsOnDrag
) return FALSE
;
57 if (g_blockEventsOnScroll
) return FALSE
;
59 if (!listbox
->HasVMT()) return FALSE
;
61 int sel
= listbox
->GetIndex( widget
);
63 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
65 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
67 clb
->Check( sel
, !clb
->IsChecked(sel
) );
69 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
70 event
.SetEventObject( listbox
);
72 listbox
->GetEventHandler()->ProcessEvent( event
);
75 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
77 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
78 event
.SetEventObject( listbox
);
80 wxArrayInt aSelections
;
81 int count
= listbox
->GetSelections(aSelections
);
84 event
.m_commandInt
= aSelections
[0] ;
85 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
86 wxString
str(listbox
->GetString(event
.m_commandInt
));
87 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
91 event
.m_commandInt
= -1 ;
92 event
.m_commandString
= copystring("") ;
95 listbox
->GetEventHandler()->ProcessEvent( event
);
97 if (event
.m_commandString
) delete[] event
.m_commandString
;
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
108 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
110 if (g_blockEventsOnDrag
) return FALSE
;
112 if (!listbox
->HasVMT()) return FALSE
;
114 if (gdk_event
->keyval
!= ' ') return FALSE
;
116 int sel
= listbox
->GetIndex( widget
);
118 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
120 clb
->Check( sel
, !clb
->IsChecked(sel
) );
122 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
123 event
.SetEventObject( listbox
);
125 listbox
->GetEventHandler()->ProcessEvent( event
);
130 //-----------------------------------------------------------------------------
131 // "select" and "deselect"
132 //-----------------------------------------------------------------------------
134 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
136 if (!listbox
->HasVMT()) return;
137 if (g_blockEventsOnDrag
) return;
139 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
141 wxArrayInt aSelections
;
142 int count
= listbox
->GetSelections(aSelections
);
145 event
.m_commandInt
= aSelections
[0] ;
146 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
147 wxString
str(listbox
->GetString(event
.m_commandInt
));
148 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
152 event
.m_commandInt
= -1 ;
153 event
.m_commandString
= copystring("") ;
156 event
.SetEventObject( listbox
);
158 listbox
->GetEventHandler()->ProcessEvent( event
);
159 if (event
.m_commandString
) delete[] event
.m_commandString
;
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
168 wxListBox::wxListBox()
170 m_list
= (GtkList
*) NULL
;
171 m_hasCheckBoxes
= FALSE
;
174 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
,
176 int n
, const wxString choices
[],
177 long style
, const wxValidator
& validator
, const wxString
&name
)
180 m_acceptsFocus
= TRUE
;
182 PreCreation( parent
, id
, pos
, size
, style
, name
);
184 SetValidator( validator
);
186 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
187 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
188 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
190 m_list
= GTK_LIST( gtk_list_new() );
192 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
193 if (style
& wxLB_MULTIPLE
)
194 mode
= GTK_SELECTION_MULTIPLE
;
195 else if (style
& wxLB_EXTENDED
)
196 mode
= GTK_SELECTION_EXTENDED
;
198 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
200 #ifdef NEW_GTK_SCROLL_CODE
201 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
203 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
207 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
209 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
211 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
213 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
214 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
216 #ifdef NEW_GTK_SCROLL_CODE
217 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
219 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
222 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
225 gtk_widget_show( GTK_WIDGET(m_list
) );
227 wxSize newSize
= size
;
228 if (newSize
.x
== -1) newSize
.x
= 100;
229 if (newSize
.y
== -1) newSize
.y
= 110;
230 SetSize( newSize
.x
, newSize
.y
);
232 for (int i
= 0; i
< n
; i
++)
234 m_clientDataList
.Append( (wxObject
*) NULL
);
235 m_clientObjectList
.Append( (wxObject
*) NULL
);
237 GtkWidget
*list_item
;
241 wxString str
= "[-] ";
243 list_item
= gtk_list_item_new_with_label( str
);
247 list_item
= gtk_list_item_new_with_label( choices
[i
] );
251 debug_focus_in( list_item
, "wxListBox::list_item", name
);
254 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
256 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
257 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
259 if (style
& wxLB_MULTIPLE
)
260 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
261 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
263 gtk_signal_connect( GTK_OBJECT(list_item
),
264 "button_press_event",
265 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
270 gtk_signal_connect( GTK_OBJECT(list_item
),
272 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
276 ConnectWidget( list_item
);
278 gtk_widget_show( list_item
);
281 m_parent
->AddChild( this );
283 (m_parent
->m_insertCallback
)( m_parent
, this );
287 gtk_widget_realize( GTK_WIDGET(m_list
) );
289 SetBackgroundColour( parent
->GetBackgroundColour() );
290 SetForegroundColour( parent
->GetForegroundColour() );
291 SetFont( parent
->GetFont() );
298 wxListBox::~wxListBox()
303 void wxListBox::AppendCommon( const wxString
&item
)
305 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
307 GtkWidget
*list_item
;
311 wxString str
= "[-] ";
313 list_item
= gtk_list_item_new_with_label( str
);
317 list_item
= gtk_list_item_new_with_label( item
);
320 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
322 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
323 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
325 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
326 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
327 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
329 if (m_widgetStyle
) ApplyWidgetStyle();
331 gtk_signal_connect( GTK_OBJECT(list_item
),
332 "button_press_event",
333 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
338 gtk_signal_connect( GTK_OBJECT(list_item
),
340 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
344 gtk_widget_show( list_item
);
346 ConnectWidget( list_item
);
348 #if wxUSE_DRAG_AND_DROP
349 #ifndef NEW_GTK_DND_CODE
350 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
355 if (m_toolTip
) m_toolTip
->Apply( this );
359 void wxListBox::Append( const wxString
&item
)
361 m_clientDataList
.Append( (wxObject
*) NULL
);
362 m_clientObjectList
.Append( (wxObject
*) NULL
);
364 AppendCommon( item
);
367 void wxListBox::Append( const wxString
&item
, void *clientData
)
369 m_clientDataList
.Append( (wxObject
*) clientData
);
370 m_clientObjectList
.Append( (wxObject
*) NULL
);
372 AppendCommon( item
);
375 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
377 m_clientObjectList
.Append( (wxObject
*) clientData
);
378 m_clientDataList
.Append( (wxObject
*) NULL
);
380 AppendCommon( item
);
383 void wxListBox::SetClientData( int n
, void* clientData
)
385 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
387 wxNode
*node
= m_clientDataList
.Nth( n
);
390 node
->SetData( (wxObject
*) clientData
);
393 void* wxListBox::GetClientData( int n
)
395 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
397 wxNode
*node
= m_clientDataList
.Nth( n
);
398 if (!node
) return NULL
;
403 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
405 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
407 wxNode
*node
= m_clientObjectList
.Nth( n
);
410 wxClientData
*cd
= (wxClientData
*) node
->Data();
413 node
->SetData( (wxObject
*) clientData
);
416 wxClientData
* wxListBox::GetClientObject( int n
)
418 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
420 wxNode
*node
= m_clientObjectList
.Nth( n
);
421 if (!node
) return (wxClientData
*) NULL
;
423 return (wxClientData
*) node
->Data();
426 void wxListBox::Clear()
428 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
430 gtk_list_clear_items( m_list
, 0, Number() );
432 wxNode
*node
= m_clientObjectList
.First();
435 wxClientData
*cd
= (wxClientData
*)node
->Data();
439 m_clientObjectList
.Clear();
441 m_clientDataList
.Clear();
444 void wxListBox::Delete( int n
)
446 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
448 GList
*child
= g_list_nth( m_list
->children
, n
);
450 wxCHECK_RET( child
, "wrong listbox index" );
452 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
453 gtk_list_remove_items( m_list
, list
);
456 wxNode
*node
= m_clientObjectList
.Nth( n
);
459 wxClientData
*cd
= (wxClientData
*)node
->Data();
461 m_clientObjectList
.DeleteNode( node
);
464 node
= m_clientDataList
.Nth( n
);
467 m_clientDataList
.DeleteNode( node
);
471 void wxListBox::Deselect( int n
)
473 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
475 gtk_list_unselect_item( m_list
, n
);
478 int wxListBox::FindString( const wxString
&item
) const
480 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
482 GList
*child
= m_list
->children
;
486 GtkBin
*bin
= GTK_BIN( child
->data
);
487 GtkLabel
*label
= GTK_LABEL( bin
->child
);
489 wxString str
= label
->label
;
490 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
492 if (str
== item
) return count
;
498 // it's not an error if the string is not found -> no wxCHECK
503 int wxListBox::GetSelection() const
505 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
507 GList
*child
= m_list
->children
;
511 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
518 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
520 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
522 // get the number of selected items first
523 GList
*child
= m_list
->children
;
525 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
527 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
536 aSelections
.Alloc(count
); // optimization attempt
538 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
540 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
548 wxString
wxListBox::GetString( int n
) const
550 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
552 GList
*child
= g_list_nth( m_list
->children
, n
);
555 GtkBin
*bin
= GTK_BIN( child
->data
);
556 GtkLabel
*label
= GTK_LABEL( bin
->child
);
558 wxString str
= label
->label
;
559 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
563 wxFAIL_MSG("wrong listbox index");
567 wxString
wxListBox::GetStringSelection() const
569 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
571 GList
*selection
= m_list
->selection
;
574 GtkBin
*bin
= GTK_BIN( selection
->data
);
575 GtkLabel
*label
= GTK_LABEL( bin
->child
);
577 wxString str
= label
->label
;
578 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
583 wxFAIL_MSG("no listbox selection available");
587 int wxListBox::Number()
589 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
591 GList
*child
= m_list
->children
;
593 while (child
) { count
++; child
= child
->next
; }
597 bool wxListBox::Selected( int n
)
599 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
601 GList
*target
= g_list_nth( m_list
->children
, n
);
604 GList
*child
= m_list
->selection
;
607 if (child
->data
== target
->data
) return TRUE
;
611 wxFAIL_MSG("wrong listbox index");
615 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
617 wxFAIL_MSG("wxListBox::Set not implemented");
620 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
622 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
625 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
627 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
630 void wxListBox::SetSelection( int n
, bool select
)
632 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
635 gtk_list_select_item( m_list
, n
);
637 gtk_list_unselect_item( m_list
, n
);
640 void wxListBox::SetString( int n
, const wxString
&string
)
642 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
644 GList
*child
= g_list_nth( m_list
->children
, n
);
647 GtkBin
*bin
= GTK_BIN( child
->data
);
648 GtkLabel
*label
= GTK_LABEL( bin
->child
);
651 if (m_hasCheckBoxes
) str
+= "[-] ";
654 gtk_label_set( label
, str
);
658 wxFAIL_MSG("wrong listbox index");
662 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
664 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
666 SetSelection( FindString(string
), select
);
669 int wxListBox::GetIndex( GtkWidget
*item
) const
673 GList
*child
= m_list
->children
;
677 if (GTK_WIDGET(child
->data
) == item
) return count
;
686 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const char *tip
)
688 GList
*child
= m_list
->children
;
691 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), tip
, (gchar
*) NULL
);
695 #endif // wxUSE_TOOLTIPS
697 #if wxUSE_DRAG_AND_DROP
698 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
700 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
702 #ifndef NEW_GTK_DND_CODE
705 GList
*child
= m_list
->children
;
708 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
714 wxWindow::SetDropTarget( dropTarget
);
716 #ifndef NEW_GTK_DND_CODE
719 GList
*child
= m_list
->children
;
722 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
730 GtkWidget
*wxListBox::GetConnectWidget()
732 return GTK_WIDGET(m_list
);
735 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
737 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
739 GList
*child
= m_list
->children
;
742 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
743 if (bin
->window
== window
) return TRUE
;
750 void wxListBox::ApplyWidgetStyle()
754 if (m_backgroundColour
.Ok())
756 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
757 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
758 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
759 gdk_window_clear( window
);
762 GList
*child
= m_list
->children
;
765 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
767 GtkBin
*bin
= GTK_BIN( child
->data
);
768 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
769 gtk_widget_set_style( label
, m_widgetStyle
);