1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "listbox.h"
15 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
22 #include "wx/checklst.h"
23 #include "wx/settings.h"
26 #include "wx/tooltip.h"
29 #if wxUSE_DRAG_AND_DROP
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-------------------------------------------------------------------------
44 // conditional compilation
45 //-------------------------------------------------------------------------
47 #if (GTK_MINOR_VERSION > 0)
48 #define NEW_GTK_SCROLL_CODE
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 #if wxUSE_CHECKLISTBOX
57 #define CHECKBOX_STRING "[-] "
59 // checklistboxes have "[±] " prepended to their lables, this macro removes it
60 // (NB: 4 below is the length of CHECKBOX_STRING above)
62 // the argument to it is a "const char *" pointer
63 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
65 #else // !wxUSE_CHECKLISTBOX
67 #define GET_REAL_LABEL(label) (label)
69 #endif // wxUSE_CHECKLISTBOX
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 extern bool g_blockEventsOnDrag
;
76 extern bool g_blockEventsOnScroll
;
77 extern wxCursor g_globalCursor
;
79 static bool g_hasDoubleClicked
= FALSE
;
81 //-----------------------------------------------------------------------------
82 // "button_release_event"
83 //-----------------------------------------------------------------------------
85 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
86 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
87 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
88 this can lead to race conditions so that we emit the dclick event
89 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
92 gtk_listbox_button_release_callback( GtkWidget
* WXUNUSED(widget
),
93 GdkEventButton
* WXUNUSED(gdk_event
),
96 if (g_isIdle
) wxapp_install_idle_handler();
98 if (g_blockEventsOnDrag
) return FALSE
;
99 if (g_blockEventsOnScroll
) return FALSE
;
101 if (!listbox
->m_hasVMT
) return FALSE
;
103 if (!g_hasDoubleClicked
) return FALSE
;
105 wxCommandEvent
event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
106 event
.SetEventObject( listbox
);
108 wxArrayInt aSelections
;
109 int n
, count
= listbox
->GetSelections(aSelections
);
113 if ( listbox
->HasClientObjectData() )
114 event
.SetClientObject( listbox
->GetClientObject(n
) );
115 else if ( listbox
->HasClientUntypedData() )
116 event
.SetClientData( listbox
->GetClientData(n
) );
117 event
.SetString( listbox
->GetString(n
) );
124 event
.m_commandInt
= n
;
126 listbox
->GetEventHandler()->ProcessEvent( event
);
131 //-----------------------------------------------------------------------------
132 // "button_press_event"
133 //-----------------------------------------------------------------------------
136 gtk_listbox_button_press_callback( GtkWidget
*widget
,
137 GdkEventButton
*gdk_event
,
140 if (g_isIdle
) wxapp_install_idle_handler();
142 if (g_blockEventsOnDrag
) return FALSE
;
143 if (g_blockEventsOnScroll
) return FALSE
;
145 if (!listbox
->m_hasVMT
) return FALSE
;
147 int sel
= listbox
->GetIndex( widget
);
149 #if wxUSE_CHECKLISTBOX
150 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
152 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
154 clb
->Check( sel
, !clb
->IsChecked(sel
) );
156 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
157 event
.SetEventObject( listbox
);
159 listbox
->GetEventHandler()->ProcessEvent( event
);
161 #endif // wxUSE_CHECKLISTBOX
163 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
164 g_hasDoubleClicked
= (gdk_event
->type
== GDK_2BUTTON_PRESS
);
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 #if wxUSE_CHECKLISTBOX
175 gtk_listbox_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxListBox
*listbox
)
177 if (g_isIdle
) wxapp_install_idle_handler();
179 if (g_blockEventsOnDrag
) return FALSE
;
181 if (!listbox
->m_hasVMT
) return FALSE
;
183 if (gdk_event
->keyval
!= ' ') return FALSE
;
185 int sel
= listbox
->GetIndex( widget
);
187 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
189 clb
->Check( sel
, !clb
->IsChecked(sel
) );
191 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
192 event
.SetEventObject( listbox
);
194 listbox
->GetEventHandler()->ProcessEvent( event
);
198 #endif // wxUSE_CHECKLISTBOX
200 //-----------------------------------------------------------------------------
201 // "select" and "deselect"
202 //-----------------------------------------------------------------------------
204 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
);
206 static void gtk_listitem_deselect_callback( GtkWidget
*widget
, wxListBox
*listbox
)
208 gtk_listitem_select_callback( widget
, listbox
);
211 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
213 if (g_isIdle
) wxapp_install_idle_handler();
215 if (!listbox
->m_hasVMT
) return;
216 if (g_blockEventsOnDrag
) return;
218 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
219 event
.SetEventObject( listbox
);
221 wxArrayInt aSelections
;
222 int n
, count
= listbox
->GetSelections(aSelections
);
226 if ( listbox
->HasClientObjectData() )
227 event
.SetClientObject( listbox
->GetClientObject(n
) );
228 else if ( listbox
->HasClientUntypedData() )
229 event
.SetClientData( listbox
->GetClientData(n
) );
230 event
.SetString( listbox
->GetString(n
) );
237 event
.m_commandInt
= n
;
239 listbox
->GetEventHandler()->ProcessEvent( event
);
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 wxListBox::wxListBox()
254 m_list
= (GtkList
*) NULL
;
255 #if wxUSE_CHECKLISTBOX
256 m_hasCheckBoxes
= FALSE
;
257 #endif // wxUSE_CHECKLISTBOX
260 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
261 const wxPoint
&pos
, const wxSize
&size
,
262 int n
, const wxString choices
[],
263 long style
, const wxValidator
& validator
,
264 const wxString
&name
)
267 m_acceptsFocus
= TRUE
;
269 if (!PreCreation( parent
, pos
, size
) ||
270 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
272 wxFAIL_MSG( wxT("wxListBox creation failed") );
276 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
277 if (style
& wxLB_ALWAYS_SB
)
279 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
280 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
284 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
285 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
288 m_list
= GTK_LIST( gtk_list_new() );
290 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
291 if (style
& wxLB_MULTIPLE
)
292 mode
= GTK_SELECTION_MULTIPLE
;
293 else if (style
& wxLB_EXTENDED
)
294 mode
= GTK_SELECTION_EXTENDED
;
296 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
298 #ifdef NEW_GTK_SCROLL_CODE
299 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
), GTK_WIDGET(m_list
) );
301 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
304 /* make list scroll when moving the focus down using cursor keys */
305 gtk_container_set_focus_vadjustment(
306 GTK_CONTAINER(m_list
),
307 gtk_scrolled_window_get_vadjustment(
308 GTK_SCROLLED_WINDOW(m_widget
)));
310 gtk_widget_show( GTK_WIDGET(m_list
) );
312 wxSize newSize
= size
;
313 if (newSize
.x
== -1) newSize
.x
= 100;
314 if (newSize
.y
== -1) newSize
.y
= 110;
315 SetSize( newSize
.x
, newSize
.y
);
317 if ( style
& wxLB_SORT
)
319 // this will change DoAppend() behaviour
320 m_strings
= new wxSortedArrayString
;
323 for (int i
= 0; i
< n
; i
++)
325 DoAppend(choices
[i
]);
328 wxString
str(choices
[i
]);
330 #if wxUSE_CHECKLISTBOX
333 str
.Prepend(CHECKBOX_STRING
);
335 #endif // wxUSE_CHECKLISTBOX
337 GtkWidget
*list_item
= gtk_list_item_new_with_label( str
.mbc_str() );
339 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
341 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
342 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
344 if (style
& wxLB_MULTIPLE
)
345 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
346 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
348 gtk_signal_connect( GTK_OBJECT(list_item
),
349 "button_press_event",
350 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
353 gtk_signal_connect_after( GTK_OBJECT(list_item
),
354 "button_release_event",
355 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
358 #if wxUSE_CHECKLISTBOX
361 gtk_signal_connect( GTK_OBJECT(list_item
),
363 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
366 #endif // wxUSE_CHECKLISTBOX
368 ConnectWidget( list_item
);
370 gtk_widget_show( list_item
);
374 m_parent
->DoAddChild( this );
378 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
379 SetForegroundColour( parent
->GetForegroundColour() );
380 SetFont( parent
->GetFont() );
387 wxListBox::~wxListBox()
392 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
394 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
396 // code elsewhere supposes we have as many items in m_clientData as items
398 wxASSERT_MSG( m_clientData
.GetCount() == (size_t)GetCount(),
399 wxT("bug in client data management") );
401 GList
*children
= m_list
->children
;
402 int length
= g_list_length(children
);
403 wxCHECK_RET( pos
<= length
, wxT("invalid index in wxListBox::InsertItems") );
405 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
406 // into a listbox at the given position, this is why we first delete
407 // all items after this position, then append these items and then
408 // reappend back the old ones.
410 // VZ: notice that InsertItems knows nothing about sorting, so calling it
411 // from outside (and not from our own Append) is likely to break
414 size_t nItems
= items
.GetCount();
416 size_t n
; // loop var
418 // optimise for this trivial case
421 // no need to do anything complicated
422 for ( n
= 0; n
< nItems
; n
++ )
424 AppendWithoutSorting(items
[n
]);
426 m_clientData
.Append((wxObject
*)NULL
);
429 wxASSERT_MSG( m_clientData
.GetCount() == (size_t)GetCount(),
430 wxT("bug in client data management") );
435 // remove the old items
436 wxArrayString deletedLabels
;
437 #if wxUSE_CHECKLISTBOX
438 wxArrayInt deletedChecks
;
441 GList
*child
= g_list_nth( children
, pos
);
442 for ( n
= 0; child
!= NULL
; n
++, child
= child
->next
)
445 GtkBin
*bin
= GTK_BIN( child
->data
);
446 GtkLabel
*label
= GTK_LABEL( bin
->child
);
448 wxString
str(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
449 deletedLabels
.Add(str
);
451 #if wxUSE_CHECKLISTBOX
453 if ( m_hasCheckBoxes
)
455 deletedChecks
.Add(((wxCheckListBox
*)this)->IsChecked(pos
+ n
));
457 #endif // wxUSE_CHECKLISTBOX
460 size_t nDeletedCount
= n
;
462 gtk_list_clear_items( m_list
, pos
, length
);
464 // now append the new items
465 wxNode
*node
= m_clientData
.Item(pos
);
466 for ( n
= 0; n
< nItems
; n
++ )
468 AppendWithoutSorting(items
[n
]);
470 // make sure we have the correct number of items in this list
471 m_clientData
.Insert(node
, (wxObject
*)NULL
);
474 // and append the old items too
475 pos
+= nItems
; // now the indices are shifted
476 for ( n
= 0; n
< nDeletedCount
; n
++ )
478 AppendWithoutSorting(deletedLabels
[n
]);
480 // the data is already correct - the indices have been rearranged in
481 // such manner that we now correspond to the same node as before
483 #if wxUSE_CHECKLISTBOX
484 if ( m_hasCheckBoxes
)
486 ((wxCheckListBox
*)this)->Check(pos
+ n
, (bool)deletedChecks
[n
]);
488 #endif // wxUSE_CHECKLISTBOX
491 wxASSERT_MSG( m_clientData
.GetCount() == (size_t)GetCount(),
492 wxT("bug in client data management") );
495 int wxListBox::DoAppend( const wxString
& item
)
500 // need to determine the index
501 index
= m_strings
->Add(item
);
503 InsertItems(1, &item
, index
);
507 // not sorted, just append
508 AppendWithoutSorting(item
);
510 m_clientData
.Append((wxObject
*)NULL
);
512 index
= GetCount() - 1;
518 void wxListBox::AppendWithoutSorting( const wxString
&item
)
520 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
522 GtkWidget
*list_item
;
524 wxString
label(item
);
525 #if wxUSE_CHECKLISTBOX
528 label
.Prepend(CHECKBOX_STRING
);
530 #endif // wxUSE_CHECKLISTBOX
532 list_item
= gtk_list_item_new_with_label( label
.mbc_str() );
534 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
536 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
537 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
539 if (HasFlag(wxLB_MULTIPLE
))
540 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
541 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
543 gtk_signal_connect( GTK_OBJECT(list_item
),
544 "button_press_event",
545 (GtkSignalFunc
)gtk_listbox_button_press_callback
,
548 gtk_signal_connect_after( GTK_OBJECT(list_item
),
549 "button_release_event",
550 (GtkSignalFunc
)gtk_listbox_button_release_callback
,
553 #if wxUSE_CHECKLISTBOX
556 gtk_signal_connect( GTK_OBJECT(list_item
),
558 (GtkSignalFunc
)gtk_listbox_key_press_callback
,
561 #endif // wxUSE_CHECKLISTBOX
563 gtk_widget_show( list_item
);
565 ConnectWidget( list_item
);
567 if (GTK_WIDGET_REALIZED(m_widget
))
569 gtk_widget_realize( list_item
);
570 gtk_widget_realize( GTK_BIN(list_item
)->child
);
572 //if (m_widgetStyle) ApplyWidgetStyle();
574 // Apply current widget style to the new list_item
575 gtk_widget_set_style( GTK_WIDGET( list_item
), m_widgetStyle
);
576 GtkBin
*bin
= GTK_BIN( list_item
);
577 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
578 gtk_widget_set_style( label
, m_widgetStyle
);
581 #if wxUSE_DRAG_AND_DROP
582 #ifndef NEW_GTK_DND_CODE
583 if (m_dropTarget
) m_dropTarget
->RegisterWidget( list_item
);
588 if (m_tooltip
) m_tooltip
->Apply( this );
593 void wxListBox::DoSetItems( const wxArrayString
& items
,
598 DoInsertItems(items
, 0);
602 size_t count
= items
.GetCount();
603 for ( size_t n
= 0; n
< count
; n
++ )
605 SetClientData(n
, clientData
[n
]);
610 // ----------------------------------------------------------------------------
612 // ----------------------------------------------------------------------------
614 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
616 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
618 wxNode
*node
= m_clientData
.Nth( n
);
619 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientData") );
621 node
->SetData( (wxObject
*) clientData
);
624 void* wxListBox::DoGetItemClientData( int n
) const
626 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid listbox control") );
628 wxNode
*node
= m_clientData
.Nth( n
);
629 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxListBox::DoGetItemClientData") );
634 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
636 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid listbox control") );
638 wxNode
*node
= m_clientData
.Nth( n
);
639 wxCHECK_RET( node
, wxT("invalid index in wxListBox::DoSetItemClientObject") );
641 wxClientData
*cd
= (wxClientData
*) node
->Data();
644 node
->SetData( (wxObject
*) clientData
);
647 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
649 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*) NULL
, wxT("invalid listbox control") );
651 wxNode
*node
= m_clientData
.Nth( n
);
652 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
653 wxT("invalid index in wxListBox::DoGetItemClientObject") );
655 return (wxClientData
*) node
->Data();
658 void wxListBox::Clear()
660 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
662 gtk_list_clear_items( m_list
, 0, Number() );
664 if ( HasClientObjectData() )
666 // destroy the data (due to Robert's idea of using wxList<wxObject>
667 // and not wxList<wxClientData> we can't just say
668 // m_clientList.DeleteContents(TRUE) - this would crash!
669 wxNode
*node
= m_clientData
.First();
672 delete (wxClientData
*)node
->Data();
676 m_clientData
.Clear();
682 void wxListBox::Delete( int n
)
684 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
686 GList
*child
= g_list_nth( m_list
->children
, n
);
688 wxCHECK_RET( child
, wxT("wrong listbox index") );
690 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
691 gtk_list_remove_items( m_list
, list
);
694 wxNode
*node
= m_clientData
.Nth( n
);
697 if ( m_clientDataItemsType
== ClientData_Object
)
699 wxClientData
*cd
= (wxClientData
*)node
->Data();
703 m_clientData
.DeleteNode( node
);
707 m_strings
->Remove(n
);
710 // ----------------------------------------------------------------------------
711 // string list access
712 // ----------------------------------------------------------------------------
714 void wxListBox::SetString( int n
, const wxString
&string
)
716 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
718 GList
*child
= g_list_nth( m_list
->children
, n
);
721 GtkBin
*bin
= GTK_BIN( child
->data
);
722 GtkLabel
*label
= GTK_LABEL( bin
->child
);
725 #if wxUSE_CHECKLISTBOX
727 str
+= CHECKBOX_STRING
;
728 #endif // wxUSE_CHECKLISTBOX
731 gtk_label_set( label
, str
.mbc_str() );
735 wxFAIL_MSG(wxT("wrong listbox index"));
739 wxString
wxListBox::GetString( int n
) const
741 wxCHECK_MSG( m_list
!= NULL
, wxT(""), wxT("invalid listbox") );
743 GList
*child
= g_list_nth( m_list
->children
, n
);
746 GtkBin
*bin
= GTK_BIN( child
->data
);
747 GtkLabel
*label
= GTK_LABEL( bin
->child
);
749 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
754 wxFAIL_MSG(wxT("wrong listbox index"));
759 int wxListBox::GetCount() const
761 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
763 GList
*child
= m_list
->children
;
774 int wxListBox::FindString( const wxString
&item
) const
776 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
778 GList
*child
= m_list
->children
;
782 GtkBin
*bin
= GTK_BIN( child
->data
);
783 GtkLabel
*label
= GTK_LABEL( bin
->child
);
785 wxString str
= wxString(GET_REAL_LABEL(label
->label
),*wxConvCurrent
);
794 // it's not an error if the string is not found -> no wxCHECK
799 // ----------------------------------------------------------------------------
801 // ----------------------------------------------------------------------------
803 int wxListBox::GetSelection() const
805 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
807 GList
*child
= m_list
->children
;
811 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
818 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
820 wxCHECK_MSG( m_list
!= NULL
, -1, wxT("invalid listbox") );
822 // get the number of selected items first
823 GList
*child
= m_list
->children
;
825 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
827 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
836 aSelections
.Alloc(count
); // optimization attempt
838 for (child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++)
840 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
848 bool wxListBox::IsSelected( int n
) const
850 wxCHECK_MSG( m_list
!= NULL
, FALSE
, wxT("invalid listbox") );
852 GList
*target
= g_list_nth( m_list
->children
, n
);
855 GList
*child
= m_list
->selection
;
858 if (child
->data
== target
->data
) return TRUE
;
863 wxFAIL_MSG(wxT("wrong listbox index"));
868 void wxListBox::SetSelection( int n
, bool select
)
870 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
875 gtk_list_select_item( m_list
, n
);
877 gtk_list_unselect_item( m_list
, n
);
882 void wxListBox::DoSetFirstItem( int WXUNUSED(n
) )
884 wxFAIL_MSG(wxT("wxListBox::SetFirstItem not implemented"));
887 // ----------------------------------------------------------------------------
889 // ----------------------------------------------------------------------------
891 int wxListBox::GetIndex( GtkWidget
*item
) const
895 GList
*child
= m_list
->children
;
899 if (GTK_WIDGET(child
->data
) == item
) return count
;
908 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
910 GList
*child
= m_list
->children
;
913 gtk_tooltips_set_tip( tips
, GTK_WIDGET( child
->data
), wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
917 #endif // wxUSE_TOOLTIPS
919 #if wxUSE_DRAG_AND_DROP
920 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
922 wxCHECK_RET( m_list
!= NULL
, wxT("invalid listbox") );
924 #ifndef NEW_GTK_DND_CODE
927 GList
*child
= m_list
->children
;
930 m_dropTarget
->UnregisterWidget( GTK_WIDGET( child
->data
) );
936 wxWindow::SetDropTarget( dropTarget
);
938 #ifndef NEW_GTK_DND_CODE
941 GList
*child
= m_list
->children
;
944 m_dropTarget
->RegisterWidget( GTK_WIDGET( child
->data
) );
952 void wxListBox::DisableEvents()
954 GList
*child
= m_list
->children
;
957 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
958 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
960 if (HasFlag(wxLB_MULTIPLE
))
961 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
962 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
968 void wxListBox::EnableEvents()
970 GList
*child
= m_list
->children
;
973 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
974 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
976 if (HasFlag(wxLB_MULTIPLE
))
977 gtk_signal_connect( GTK_OBJECT(child
->data
), "deselect",
978 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback
), (gpointer
)this );
984 GtkWidget
*wxListBox::GetConnectWidget()
986 return GTK_WIDGET(m_list
);
989 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
991 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
993 GList
*child
= m_list
->children
;
996 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
997 if (bin
->window
== window
) return TRUE
;
1004 void wxListBox::ApplyWidgetStyle()
1008 if (m_backgroundColour
.Ok())
1010 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
1013 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1014 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1015 gdk_window_clear( window
);
1019 GList
*child
= m_list
->children
;
1022 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
1024 GtkBin
*bin
= GTK_BIN( child
->data
);
1025 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1026 gtk_widget_set_style( label
, m_widgetStyle
);
1028 child
= child
->next
;
1032 void wxListBox::OnInternalIdle()
1034 wxCursor cursor
= m_cursor
;
1035 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1037 if (GTK_WIDGET(m_list
)->window
&& cursor
.Ok())
1039 /* I now set the cursor the anew in every OnInternalIdle call
1040 as setting the cursor in a parent window also effects the
1041 windows above so that checking for the current cursor is
1044 gdk_window_set_cursor( GTK_WIDGET(m_list
)->window
, cursor
.GetCursor() );
1046 GList
*child
= m_list
->children
;
1049 GtkBin
*bin
= GTK_BIN( child
->data
);
1050 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
1055 gdk_window_set_cursor( label
->window
, cursor
.GetCursor() );
1057 child
= child
->next
;