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
)
134 PreCreation( parent
, id
, pos
, size
, style
, name
);
136 SetValidator( validator
);
138 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
139 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
140 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
142 m_list
= GTK_LIST( gtk_list_new() );
144 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
145 if (style
& wxLB_MULTIPLE
)
146 mode
= GTK_SELECTION_MULTIPLE
;
147 else if (style
& wxLB_EXTENDED
)
148 mode
= GTK_SELECTION_EXTENDED
;
150 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
152 #ifdef NEW_GTK_SCROLL_CODE
153 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
155 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
158 gtk_widget_show( GTK_WIDGET(m_list
) );
160 wxSize newSize
= size
;
161 if (newSize
.x
== -1) newSize
.x
= 100;
162 if (newSize
.y
== -1) newSize
.y
= 110;
163 SetSize( newSize
.x
, newSize
.y
);
165 for (int i
= 0; i
< n
; i
++)
167 m_clientDataList
.Append( (wxObject
*) NULL
);
168 m_clientObjectList
.Append( (wxObject
*) NULL
);
170 GtkWidget
*list_item
;
174 wxString str
= "[-] ";
176 list_item
= gtk_list_item_new_with_label( str
);
180 list_item
= gtk_list_item_new_with_label( choices
[i
] );
183 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
185 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
186 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
188 if (style
& wxLB_MULTIPLE
)
189 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
190 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
194 gtk_signal_connect( GTK_OBJECT(list_item
),
195 "button_press_event",
196 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
199 gtk_signal_connect( GTK_OBJECT(list_item
),
201 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
205 ConnectWidget( list_item
);
207 gtk_widget_show( list_item
);
210 m_parent
->AddChild( this );
212 (m_parent
->m_insertCallback
)( m_parent
, this );
216 gtk_widget_realize( GTK_WIDGET(m_list
) );
218 SetBackgroundColour( parent
->GetBackgroundColour() );
219 SetForegroundColour( parent
->GetForegroundColour() );
226 wxListBox::~wxListBox()
231 void wxListBox::AppendCommon( const wxString
&item
)
233 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
235 GtkWidget
*list_item
;
239 wxString str
= "[-] ";
241 list_item
= gtk_list_item_new_with_label( str
);
245 list_item
= gtk_list_item_new_with_label( item
);
248 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
249 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
251 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
252 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
253 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
255 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
257 if (m_widgetStyle
) ApplyWidgetStyle();
261 gtk_signal_connect( GTK_OBJECT(list_item
),
262 "button_press_event",
263 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
266 gtk_signal_connect( GTK_OBJECT(list_item
),
268 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
272 gtk_widget_show( list_item
);
274 ConnectWidget( list_item
);
276 #ifndef NEW_GTK_DND_CODE
277 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
281 void wxListBox::Append( const wxString
&item
)
283 m_clientDataList
.Append( (wxObject
*) NULL
);
284 m_clientObjectList
.Append( (wxObject
*) NULL
);
286 AppendCommon( item
);
289 void wxListBox::Append( const wxString
&item
, void *clientData
)
291 m_clientDataList
.Append( (wxObject
*) clientData
);
292 m_clientObjectList
.Append( (wxObject
*) NULL
);
294 AppendCommon( item
);
297 void wxListBox::Append( const wxString
&item
, wxClientData
*clientData
)
299 m_clientObjectList
.Append( (wxObject
*) clientData
);
300 m_clientDataList
.Append( (wxObject
*) NULL
);
302 AppendCommon( item
);
305 void wxListBox::SetClientData( int n
, void* clientData
)
307 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
309 wxNode
*node
= m_clientDataList
.Nth( n
);
312 node
->SetData( (wxObject
*) clientData
);
315 void* wxListBox::GetClientData( int n
)
317 wxCHECK_MSG( m_widget
!= NULL
, NULL
, "invalid combobox" );
319 wxNode
*node
= m_clientDataList
.Nth( n
);
320 if (!node
) return NULL
;
325 void wxListBox::SetClientObject( int n
, wxClientData
* clientData
)
327 wxCHECK_RET( m_widget
!= NULL
, "invalid combobox" );
329 wxNode
*node
= m_clientObjectList
.Nth( n
);
332 wxClientData
*cd
= (wxClientData
*) node
->Data();
335 node
->SetData( (wxObject
*) clientData
);
338 wxClientData
* wxListBox::GetClientObject( int n
)
340 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, "invalid combobox" );
342 wxNode
*node
= m_clientObjectList
.Nth( n
);
343 if (!node
) return (wxClientData
*) NULL
;
345 return (wxClientData
*) node
->Data();
348 void wxListBox::Clear()
350 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
352 gtk_list_clear_items( m_list
, 0, Number() );
354 wxNode
*node
= m_clientObjectList
.First();
357 wxClientData
*cd
= (wxClientData
*)node
->Data();
361 m_clientObjectList
.Clear();
363 m_clientDataList
.Clear();
366 void wxListBox::Delete( int n
)
368 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
370 GList
*child
= g_list_nth( m_list
->children
, n
);
372 wxCHECK_RET( child
, "wrong listbox index" );
374 GList
*list
= g_list_append( NULL
, child
->data
);
375 gtk_list_remove_items( m_list
, list
);
378 wxNode
*node
= m_clientObjectList
.Nth( n
);
381 wxClientData
*cd
= (wxClientData
*)node
->Data();
383 m_clientObjectList
.DeleteNode( node
);
386 node
= m_clientDataList
.Nth( n
);
389 m_clientDataList
.DeleteNode( node
);
393 void wxListBox::Deselect( int n
)
395 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
397 gtk_list_unselect_item( m_list
, n
);
400 int wxListBox::FindString( const wxString
&item
) const
402 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
404 GList
*child
= m_list
->children
;
408 GtkBin
*bin
= GTK_BIN( child
->data
);
409 GtkLabel
*label
= GTK_LABEL( bin
->child
);
411 wxString str
= label
->label
;
412 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
414 if (str
== item
) return count
;
420 // it's not an error if the string is not found -> no wxCHECK
425 int wxListBox::GetSelection() const
427 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
429 GList
*child
= m_list
->children
;
433 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
440 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
442 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
444 // get the number of selected items first
445 GList
*child
= m_list
->children
;
447 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
449 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
458 aSelections
.Alloc(count
); // optimization attempt
460 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
462 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
470 wxString
wxListBox::GetString( int n
) const
472 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
474 GList
*child
= g_list_nth( m_list
->children
, n
);
477 GtkBin
*bin
= GTK_BIN( child
->data
);
478 GtkLabel
*label
= GTK_LABEL( bin
->child
);
480 wxString str
= label
->label
;
481 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
485 wxFAIL_MSG("wrong listbox index");
489 wxString
wxListBox::GetStringSelection() const
491 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
493 GList
*selection
= m_list
->selection
;
496 GtkBin
*bin
= GTK_BIN( selection
->data
);
497 GtkLabel
*label
= GTK_LABEL( bin
->child
);
499 wxString str
= label
->label
;
500 if (m_hasCheckBoxes
) str
.Remove( 0, 4 );
505 wxFAIL_MSG("no listbox selection available");
509 int wxListBox::Number()
511 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
513 GList
*child
= m_list
->children
;
515 while (child
) { count
++; child
= child
->next
; }
519 bool wxListBox::Selected( int n
)
521 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
523 GList
*target
= g_list_nth( m_list
->children
, n
);
526 GList
*child
= m_list
->selection
;
529 if (child
->data
== target
->data
) return TRUE
;
533 wxFAIL_MSG("wrong listbox index");
537 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
539 wxFAIL_MSG("wxListBox::Set not implemented");
542 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
544 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
547 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
549 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
552 void wxListBox::SetSelection( int n
, bool select
)
554 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
557 gtk_list_select_item( m_list
, n
);
559 gtk_list_unselect_item( m_list
, n
);
562 void wxListBox::SetString( int n
, const wxString
&string
)
564 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
566 GList
*child
= g_list_nth( m_list
->children
, n
);
569 GtkBin
*bin
= GTK_BIN( child
->data
);
570 GtkLabel
*label
= GTK_LABEL( bin
->child
);
573 if (m_hasCheckBoxes
) str
+= "[-] ";
576 gtk_label_set( label
, str
);
580 wxFAIL_MSG("wrong listbox index");
584 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
586 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
588 SetSelection( FindString(string
), select
);
591 int wxListBox::GetIndex( GtkWidget
*item
) const
595 GList
*child
= m_list
->children
;
599 if (GTK_WIDGET(child
->data
) == item
) return count
;
607 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
609 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
611 #ifndef NEW_GTK_DND_CODE
614 GList
*child
= m_list
->children
;
617 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
623 wxWindow::SetDropTarget( dropTarget
);
625 #ifndef NEW_GTK_DND_CODE
628 GList
*child
= m_list
->children
;
631 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
638 GtkWidget
*wxListBox::GetConnectWidget()
640 return GTK_WIDGET(m_list
);
643 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
645 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
647 GList
*child
= m_list
->children
;
650 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
651 if (bin
->window
== window
) return TRUE
;
658 void wxListBox::ApplyWidgetStyle()
662 if (m_backgroundColour
.Ok())
664 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
665 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
666 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
667 gdk_window_clear( window
);
670 GList
*child
= m_list
->children
;
673 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
675 GtkBin
*bin
= GTK_BIN( child
->data
);
676 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
677 gtk_widget_set_style( label
, m_widgetStyle
);