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 int sel
= listbox
->GetIndex( widget
);
52 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
54 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
56 clb
->Check( sel
, !clb
->IsChecked(sel
) );
58 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
59 event
.SetEventObject( listbox
);
61 listbox
->GetEventHandler()->ProcessEvent( event
);
64 if (gdk_event
->type
== GDK_2BUTTON_PRESS
)
66 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
67 event
.SetEventObject( listbox
);
69 listbox
->GetEventHandler()->ProcessEvent( event
);
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
80 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
82 if (g_blockEventsOnDrag
) return FALSE
;
84 if (!listbox
->HasVMT()) return FALSE
;
86 if (gdk_event
->keyval
!= ' ') return FALSE
;
88 int sel
= listbox
->GetIndex( widget
);
90 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
92 clb
->Check( sel
, !clb
->IsChecked(sel
) );
94 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
95 event
.SetEventObject( listbox
);
97 listbox
->GetEventHandler()->ProcessEvent( event
);
102 //-----------------------------------------------------------------------------
103 // "select" and "deselect"
104 //-----------------------------------------------------------------------------
106 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
108 if (!listbox
->HasVMT()) return;
109 if (g_blockEventsOnDrag
) return;
111 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
113 wxArrayInt aSelections
;
114 int count
= listbox
->GetSelections(aSelections
);
117 event
.m_commandInt
= aSelections
[0] ;
118 event
.m_clientData
= listbox
->GetClientData( event
.m_commandInt
);
119 wxString
str(listbox
->GetString(event
.m_commandInt
));
120 if (str
!= "") event
.m_commandString
= copystring((char *)(const char *)str
);
124 event
.m_commandInt
= -1 ;
125 event
.m_commandString
= copystring("") ;
128 event
.SetEventObject( listbox
);
130 listbox
->GetEventHandler()->ProcessEvent( event
);
131 if (event
.m_commandString
) delete[] event
.m_commandString
;
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
140 wxListBox::wxListBox()
142 m_list
= (GtkList
*) NULL
;
143 m_hasCheckBoxes
= FALSE
;
146 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
147 const wxPoint
&pos
, const wxSize
&size
,
148 int n
, const wxString choices
[],
149 long style
, const wxValidator
& validator
, const wxString
&name
)
152 m_acceptsFocus
= TRUE
;
154 PreCreation( parent
, id
, pos
, size
, style
, name
);
156 SetValidator( validator
);
158 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
159 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
160 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
162 m_list
= GTK_LIST( gtk_list_new() );
164 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
165 if (style
& wxLB_MULTIPLE
)
166 mode
= GTK_SELECTION_MULTIPLE
;
167 else if (style
& wxLB_EXTENDED
)
168 mode
= GTK_SELECTION_EXTENDED
;
170 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
172 #ifdef NEW_GTK_SCROLL_CODE
173 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
175 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
179 debug_focus_in( m_widget
, "wxListBox::m_widget", name
);
181 debug_focus_in( GTK_WIDGET(m_list
), "wxListBox::m_list", name
);
183 GtkScrolledWindow
*s_window
= GTK_SCROLLED_WINDOW(m_widget
);
185 debug_focus_in( s_window
->hscrollbar
, "wxWindow::hsrcollbar", name
);
186 debug_focus_in( s_window
->vscrollbar
, "wxWindow::vsrcollbar", name
);
188 #ifdef NEW_GTK_SCROLL_CODE
189 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->child
);
191 GtkViewport
*viewport
= GTK_VIEWPORT(s_window
->viewport
);
194 debug_focus_in( GTK_WIDGET(viewport
), "wxWindow::viewport", name
);
197 gtk_widget_show( GTK_WIDGET(m_list
) );
199 wxSize newSize
= size
;
200 if (newSize
.x
== -1) newSize
.x
= 100;
201 if (newSize
.y
== -1) newSize
.y
= 110;
202 SetSize( newSize
.x
, newSize
.y
);
204 for (int i
= 0; i
< n
; i
++)
206 m_clientDataList
.Append( (wxObject
*) NULL
);
207 m_clientObjectList
.Append( (wxObject
*) NULL
);
209 GtkWidget
*list_item
;
213 wxString str
= "[-] ";
215 list_item
= gtk_list_item_new_with_label( str
);
219 list_item
= gtk_list_item_new_with_label( choices
[i
] );
223 debug_focus_in( list_item
, "wxListBox::list_item", name
);
226 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
228 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
229 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
231 if (style
& wxLB_MULTIPLE
)
232 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
233 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
237 gtk_signal_connect( GTK_OBJECT(list_item
),
238 "button_press_event",
239 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
243 gtk_signal_connect( GTK_OBJECT(list_item
),
245 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
248 ConnectWidget( list_item
);
250 gtk_widget_show( list_item
);
253 m_parent
->AddChild( this );
255 (m_parent
->m_insertCallback
)( m_parent
, this );
259 gtk_widget_realize( GTK_WIDGET(m_list
) );
261 SetBackgroundColour( parent
->GetBackgroundColour() );
262 SetForegroundColour( parent
->GetForegroundColour() );
269 wxListBox::~wxListBox()
274 void wxListBox::AppendCommon( const wxString
&item
)
276 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
278 GtkWidget
*list_item
;
282 wxString str
= "[-] ";
284 list_item
= gtk_list_item_new_with_label( str
);
288 list_item
= gtk_list_item_new_with_label( item
);
291 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
292 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
294 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
295 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
296 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
298 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
300 if (m_widgetStyle
) ApplyWidgetStyle();
304 gtk_signal_connect( GTK_OBJECT(list_item
),
305 "button_press_event",
306 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
310 gtk_signal_connect( GTK_OBJECT(list_item
),
312 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
315 gtk_widget_show( list_item
);
317 ConnectWidget( list_item
);
319 #ifndef NEW_GTK_DND_CODE
320 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
324 void wxListBox::Append( const wxString
&item
)
326 m_clientDataList
.Append( (wxObject
*) NULL
);
327 m_clientObjectList
.Append( (wxObject
*) NULL
);
329 AppendCommon( item
);
332 void wxListBox::Append( const wxString
&item
, void *clientData
)
334 m_clientDataList
.Append( (wxObject
*) clientData
);
335 m_clientObjectList
.Append( (wxObject
*) NULL
);
337 AppendCommon( item
);
340 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
342 m_clientObjectList
.Append( (wxObject
*) clientData
);
343 m_clientDataList
.Append( (wxObject
*) NULL
);
345 AppendCommon( item
);
348 void wxListBox::SetClientData( int n
, void* clientData
)
350 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
352 wxNode
*node
= m_clientDataList
.Nth( n
);
355 node
->SetData( (wxObject
*) clientData
);
358 void* wxListBox::GetClientData( int n
)
360 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
362 wxNode
*node
= m_clientDataList
.Nth( n
);
363 if (!node
) return NULL
;
368 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
370 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
372 wxNode
*node
= m_clientObjectList
.Nth( n
);
375 wxClientData
*cd
= (wxClientData
*) node
->Data();
378 node
->SetData( (wxObject
*) clientData
);
381 wxClientData
* wxListBox::GetClientObject( int n
)
383 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
385 wxNode
*node
= m_clientObjectList
.Nth( n
);
386 if (!node
) return (wxClientData
*) NULL
;
388 return (wxClientData
*) node
->Data();
391 void wxListBox::Clear()
393 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
395 gtk_list_clear_items( m_list
, 0, Number() );
397 wxNode
*node
= m_clientObjectList
.First();
400 wxClientData
*cd
= (wxClientData
*)node
->Data();
404 m_clientObjectList
.Clear();
406 m_clientDataList
.Clear();
409 void wxListBox::Delete( int n
)
411 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
413 GList
*child
= g_list_nth( m_list
->children
, n
);
415 wxCHECK_RET( child
, "wrong listbox index" );
417 GList
*list
= g_list_append( NULL
, child
->data
);
418 gtk_list_remove_items( m_list
, list
);
421 wxNode
*node
= m_clientObjectList
.Nth( n
);
424 wxClientData
*cd
= (wxClientData
*)node
->Data();
426 m_clientObjectList
.DeleteNode( node
);
429 node
= m_clientDataList
.Nth( n
);
432 m_clientDataList
.DeleteNode( node
);
436 void wxListBox::Deselect( int n
)
438 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
440 gtk_list_unselect_item( m_list
, n
);
443 int wxListBox::FindString( const wxString
&item
) const
445 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
447 GList
*child
= m_list
->children
;
451 GtkBin
*bin
= GTK_BIN( child
->data
);
452 GtkLabel
*label
= GTK_LABEL( bin
->child
);
454 wxString str
= label
->label
;
455 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
457 if (str
== item
) return count
;
463 // it's not an error if the string is not found -> no wxCHECK
468 int wxListBox::GetSelection() const
470 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
472 GList
*child
= m_list
->children
;
476 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
483 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
485 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
487 // get the number of selected items first
488 GList
*child
= m_list
->children
;
490 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
492 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
501 aSelections
.Alloc(count
); // optimization attempt
503 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
505 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
513 wxString
wxListBox::GetString( int n
) const
515 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
517 GList
*child
= g_list_nth( m_list
->children
, n
);
520 GtkBin
*bin
= GTK_BIN( child
->data
);
521 GtkLabel
*label
= GTK_LABEL( bin
->child
);
523 wxString str
= label
->label
;
524 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
528 wxFAIL_MSG("wrong listbox index");
532 wxString
wxListBox::GetStringSelection() const
534 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
536 GList
*selection
= m_list
->selection
;
539 GtkBin
*bin
= GTK_BIN( selection
->data
);
540 GtkLabel
*label
= GTK_LABEL( bin
->child
);
542 wxString str
= label
->label
;
543 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
548 wxFAIL_MSG("no listbox selection available");
552 int wxListBox::Number()
554 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
556 GList
*child
= m_list
->children
;
558 while (child
) { count
++; child
= child
->next
; }
562 bool wxListBox::Selected( int n
)
564 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
566 GList
*target
= g_list_nth( m_list
->children
, n
);
569 GList
*child
= m_list
->selection
;
572 if (child
->data
== target
->data
) return TRUE
;
576 wxFAIL_MSG("wrong listbox index");
580 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
582 wxFAIL_MSG("wxListBox::Set not implemented");
585 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
587 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
590 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
592 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
595 void wxListBox::SetSelection( int n
, bool select
)
597 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
600 gtk_list_select_item( m_list
, n
);
602 gtk_list_unselect_item( m_list
, n
);
605 void wxListBox::SetString( int n
, const wxString
&string
)
607 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
609 GList
*child
= g_list_nth( m_list
->children
, n
);
612 GtkBin
*bin
= GTK_BIN( child
->data
);
613 GtkLabel
*label
= GTK_LABEL( bin
->child
);
616 if (m_hasCheckBoxes
) str
+= "[-] ";
619 gtk_label_set( label
, str
);
623 wxFAIL_MSG("wrong listbox index");
627 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
629 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
631 SetSelection( FindString(string
), select
);
634 int wxListBox::GetIndex( GtkWidget
*item
) const
638 GList
*child
= m_list
->children
;
642 if (GTK_WIDGET(child
->data
) == item
) return count
;
650 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
652 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
654 #ifndef NEW_GTK_DND_CODE
657 GList
*child
= m_list
->children
;
660 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
666 wxWindow::SetDropTarget( dropTarget
);
668 #ifndef NEW_GTK_DND_CODE
671 GList
*child
= m_list
->children
;
674 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
681 GtkWidget
*wxListBox::GetConnectWidget()
683 return GTK_WIDGET(m_list
);
686 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
688 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
690 GList
*child
= m_list
->children
;
693 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
694 if (bin
->window
== window
) return TRUE
;
701 void wxListBox::ApplyWidgetStyle()
705 if (m_backgroundColour
.Ok())
707 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
708 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
709 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
710 gdk_window_clear( window
);
713 GList
*child
= m_list
->children
;
716 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
718 GtkBin
*bin
= GTK_BIN( child
->data
);
719 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
720 gtk_widget_set_style( label
, m_widgetStyle
);