1. sorted wxListBox and wxComboBox seem to work under wxGTK
[wxWidgets.git] / src / gtk1 / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "listbox.h"
13 #endif
14
15 #include "wx/listbox.h"
16
17 #if wxUSE_LISTBOX
18
19 #include "wx/dynarray.h"
20 #include "wx/utils.h"
21 #include "wx/intl.h"
22 #include "wx/checklst.h"
23 #include "wx/settings.h"
24
25 #if wxUSE_TOOLTIPS
26 #include "wx/tooltip.h"
27 #endif
28
29 #if wxUSE_DRAG_AND_DROP
30 #include "wx/dnd.h"
31 #endif
32
33 #include "gdk/gdk.h"
34 #include "gtk/gtk.h"
35
36 //-----------------------------------------------------------------------------
37 // idle system
38 //-----------------------------------------------------------------------------
39
40 extern void wxapp_install_idle_handler();
41 extern bool g_isIdle;
42
43 //-------------------------------------------------------------------------
44 // conditional compilation
45 //-------------------------------------------------------------------------
46
47 #if (GTK_MINOR_VERSION > 0)
48 #define NEW_GTK_SCROLL_CODE
49 #endif
50
51 //-----------------------------------------------------------------------------
52 // private functions
53 //-----------------------------------------------------------------------------
54
55 #if wxUSE_CHECKLISTBOX
56
57 #define CHECKBOX_STRING "[-] "
58
59 // checklistboxes have "[±] " prepended to their lables, this macro removes it
60 // (NB: 4 below is the length of CHECKBOX_STRING above)
61 //
62 // the argument to it is a "const char *" pointer
63 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
64
65 #else // !wxUSE_CHECKLISTBOX
66
67 #define GET_REAL_LABEL(label) (label)
68
69 #endif // wxUSE_CHECKLISTBOX
70
71 //-----------------------------------------------------------------------------
72 // data
73 //-----------------------------------------------------------------------------
74
75 extern bool g_blockEventsOnDrag;
76 extern bool g_blockEventsOnScroll;
77 extern wxCursor g_globalCursor;
78
79 static bool g_hasDoubleClicked = FALSE;
80
81 //-----------------------------------------------------------------------------
82 // "button_release_event"
83 //-----------------------------------------------------------------------------
84
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 */
90
91 static gint
92 gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
93 GdkEventButton * WXUNUSED(gdk_event),
94 wxListBox *listbox )
95 {
96 if (g_isIdle) wxapp_install_idle_handler();
97
98 if (g_blockEventsOnDrag) return FALSE;
99 if (g_blockEventsOnScroll) return FALSE;
100
101 if (!listbox->m_hasVMT) return FALSE;
102
103 if (!g_hasDoubleClicked) return FALSE;
104
105 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
106 event.SetEventObject( listbox );
107
108 wxArrayInt aSelections;
109 int n, count = listbox->GetSelections(aSelections);
110 if ( count > 0 )
111 {
112 n = aSelections[0];
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) );
118 }
119 else
120 {
121 n = -1;
122 }
123
124 event.m_commandInt = n;
125
126 listbox->GetEventHandler()->ProcessEvent( event );
127
128 return FALSE;
129 }
130
131 //-----------------------------------------------------------------------------
132 // "button_press_event"
133 //-----------------------------------------------------------------------------
134
135 static gint
136 gtk_listbox_button_press_callback( GtkWidget *widget,
137 GdkEventButton *gdk_event,
138 wxListBox *listbox )
139 {
140 if (g_isIdle) wxapp_install_idle_handler();
141
142 if (g_blockEventsOnDrag) return FALSE;
143 if (g_blockEventsOnScroll) return FALSE;
144
145 if (!listbox->m_hasVMT) return FALSE;
146
147 int sel = listbox->GetIndex( widget );
148
149 #if wxUSE_CHECKLISTBOX
150 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
151 {
152 wxCheckListBox *clb = (wxCheckListBox *)listbox;
153
154 clb->Check( sel, !clb->IsChecked(sel) );
155
156 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
157 event.SetEventObject( listbox );
158 event.SetInt( sel );
159 listbox->GetEventHandler()->ProcessEvent( event );
160 }
161 #endif // wxUSE_CHECKLISTBOX
162
163 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
164 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
165
166 return FALSE;
167 }
168
169 //-----------------------------------------------------------------------------
170 // "key_press_event"
171 //-----------------------------------------------------------------------------
172
173 #if wxUSE_CHECKLISTBOX
174 static gint
175 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
176 {
177 if (g_isIdle) wxapp_install_idle_handler();
178
179 if (g_blockEventsOnDrag) return FALSE;
180
181 if (!listbox->m_hasVMT) return FALSE;
182
183 if (gdk_event->keyval != ' ') return FALSE;
184
185 int sel = listbox->GetIndex( widget );
186
187 wxCheckListBox *clb = (wxCheckListBox *)listbox;
188
189 clb->Check( sel, !clb->IsChecked(sel) );
190
191 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
192 event.SetEventObject( listbox );
193 event.SetInt( sel );
194 listbox->GetEventHandler()->ProcessEvent( event );
195
196 return FALSE;
197 }
198 #endif // wxUSE_CHECKLISTBOX
199
200 //-----------------------------------------------------------------------------
201 // "select" and "deselect"
202 //-----------------------------------------------------------------------------
203
204 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox );
205
206 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
207 {
208 gtk_listitem_select_callback( widget, listbox );
209 }
210
211 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
212 {
213 if (g_isIdle) wxapp_install_idle_handler();
214
215 if (!listbox->m_hasVMT) return;
216 if (g_blockEventsOnDrag) return;
217
218 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
219 event.SetEventObject( listbox );
220
221 wxArrayInt aSelections;
222 int n, count = listbox->GetSelections(aSelections);
223 if ( count > 0 )
224 {
225 n = aSelections[0];
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) );
231 }
232 else
233 {
234 n = -1;
235 }
236
237 event.m_commandInt = n;
238
239 listbox->GetEventHandler()->ProcessEvent( event );
240 }
241
242 //-----------------------------------------------------------------------------
243 // wxListBox
244 //-----------------------------------------------------------------------------
245
246 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
247
248 // ----------------------------------------------------------------------------
249 // construction
250 // ----------------------------------------------------------------------------
251
252 wxListBox::wxListBox()
253 {
254 m_list = (GtkList *) NULL;
255 #if wxUSE_CHECKLISTBOX
256 m_hasCheckBoxes = FALSE;
257 #endif // wxUSE_CHECKLISTBOX
258 }
259
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 )
265 {
266 m_needParent = TRUE;
267 m_acceptsFocus = TRUE;
268
269 if (!PreCreation( parent, pos, size ) ||
270 !CreateBase( parent, id, pos, size, style, validator, name ))
271 {
272 wxFAIL_MSG( wxT("wxListBox creation failed") );
273 return FALSE;
274 }
275
276 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
277 if (style & wxLB_ALWAYS_SB)
278 {
279 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
280 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
281 }
282 else
283 {
284 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
285 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
286 }
287
288 m_list = GTK_LIST( gtk_list_new() );
289
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;
295
296 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
297
298 #ifdef NEW_GTK_SCROLL_CODE
299 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
300 #else
301 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
302 #endif
303
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)));
309
310 gtk_widget_show( GTK_WIDGET(m_list) );
311
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 );
316
317 if ( style & wxLB_SORT )
318 {
319 // this will change DoAppend() behaviour
320 m_strings = new wxSortedArrayString;
321 }
322
323 for (int i = 0; i < n; i++)
324 {
325 DoAppend(choices[i]);
326
327 #if 0
328 wxString str(choices[i]);
329
330 #if wxUSE_CHECKLISTBOX
331 if (m_hasCheckBoxes)
332 {
333 str.Prepend(CHECKBOX_STRING);
334 }
335 #endif // wxUSE_CHECKLISTBOX
336
337 GtkWidget *list_item = gtk_list_item_new_with_label( str.mbc_str() );
338
339 gtk_container_add( GTK_CONTAINER(m_list), list_item );
340
341 gtk_signal_connect( GTK_OBJECT(list_item), "select",
342 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
343
344 if (style & wxLB_MULTIPLE)
345 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
346 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
347
348 gtk_signal_connect( GTK_OBJECT(list_item),
349 "button_press_event",
350 (GtkSignalFunc)gtk_listbox_button_press_callback,
351 (gpointer) this );
352
353 gtk_signal_connect_after( GTK_OBJECT(list_item),
354 "button_release_event",
355 (GtkSignalFunc)gtk_listbox_button_release_callback,
356 (gpointer) this );
357
358 #if wxUSE_CHECKLISTBOX
359 if (m_hasCheckBoxes)
360 {
361 gtk_signal_connect( GTK_OBJECT(list_item),
362 "key_press_event",
363 (GtkSignalFunc)gtk_listbox_key_press_callback,
364 (gpointer)this );
365 }
366 #endif // wxUSE_CHECKLISTBOX
367
368 ConnectWidget( list_item );
369
370 gtk_widget_show( list_item );
371 #endif // 0
372 }
373
374 m_parent->DoAddChild( this );
375
376 PostCreation();
377
378 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
379 SetForegroundColour( parent->GetForegroundColour() );
380 SetFont( parent->GetFont() );
381
382 Show( TRUE );
383
384 return TRUE;
385 }
386
387 wxListBox::~wxListBox()
388 {
389 Clear();
390 }
391
392 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
393 {
394 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
395
396 // code elsewhere supposes we have as many items in m_clientData as items
397 // in the listbox
398 wxASSERT_MSG( m_clientData.GetCount() == (size_t)GetCount(),
399 wxT("bug in client data management") );
400
401 GList *children = m_list->children;
402 int length = g_list_length(children);
403 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
404
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.
409
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
412 // everything
413
414 size_t nItems = items.GetCount();
415
416 size_t n; // loop var
417
418 // optimise for this trivial case
419 if ( pos == length )
420 {
421 // no need to do anything complicated
422 for ( n = 0; n < nItems; n++ )
423 {
424 AppendWithoutSorting(items[n]);
425
426 m_clientData.Append((wxObject *)NULL);
427 }
428
429 wxASSERT_MSG( m_clientData.GetCount() == (size_t)GetCount(),
430 wxT("bug in client data management") );
431
432 return;
433 }
434
435 // remove the old items
436 wxArrayString deletedLabels;
437 #if wxUSE_CHECKLISTBOX
438 wxArrayInt deletedChecks;
439 #endif
440
441 GList *child = g_list_nth( children, pos );
442 for ( n = 0; child != NULL; n++, child = child->next )
443 {
444 // save label
445 GtkBin *bin = GTK_BIN( child->data );
446 GtkLabel *label = GTK_LABEL( bin->child );
447
448 wxString str(GET_REAL_LABEL(label->label),*wxConvCurrent);
449 deletedLabels.Add(str);
450
451 #if wxUSE_CHECKLISTBOX
452 // save check state
453 if ( m_hasCheckBoxes )
454 {
455 deletedChecks.Add(((wxCheckListBox *)this)->IsChecked(pos + n));
456 }
457 #endif // wxUSE_CHECKLISTBOX
458 }
459
460 size_t nDeletedCount = n;
461
462 gtk_list_clear_items( m_list, pos, length );
463
464 // now append the new items
465 wxNode *node = m_clientData.Item(pos);
466 for ( n = 0; n < nItems; n++ )
467 {
468 AppendWithoutSorting(items[n]);
469
470 // make sure we have the correct number of items in this list
471 m_clientData.Insert(node, (wxObject *)NULL);
472 }
473
474 // and append the old items too
475 pos += nItems; // now the indices are shifted
476 for ( n = 0; n < nDeletedCount; n++ )
477 {
478 AppendWithoutSorting(deletedLabels[n]);
479
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
482
483 #if wxUSE_CHECKLISTBOX
484 if ( m_hasCheckBoxes )
485 {
486 ((wxCheckListBox *)this)->Check(pos + n, (bool)deletedChecks[n]);
487 }
488 #endif // wxUSE_CHECKLISTBOX
489 }
490
491 wxASSERT_MSG( m_clientData.GetCount() == (size_t)GetCount(),
492 wxT("bug in client data management") );
493 }
494
495 int wxListBox::DoAppend( const wxString& item )
496 {
497 int index;
498 if ( m_strings )
499 {
500 // need to determine the index
501 index = m_strings->Add(item);
502
503 InsertItems(1, &item, index);
504 }
505 else
506 {
507 // not sorted, just append
508 AppendWithoutSorting(item);
509
510 m_clientData.Append((wxObject *)NULL);
511
512 index = GetCount() - 1;
513 }
514
515 return index;
516 }
517
518 void wxListBox::AppendWithoutSorting( const wxString &item )
519 {
520 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
521
522 GtkWidget *list_item;
523
524 wxString label(item);
525 #if wxUSE_CHECKLISTBOX
526 if (m_hasCheckBoxes)
527 {
528 label.Prepend(CHECKBOX_STRING);
529 }
530 #endif // wxUSE_CHECKLISTBOX
531
532 list_item = gtk_list_item_new_with_label( label.mbc_str() );
533
534 gtk_container_add( GTK_CONTAINER(m_list), list_item );
535
536 gtk_signal_connect( GTK_OBJECT(list_item), "select",
537 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
538
539 if (HasFlag(wxLB_MULTIPLE))
540 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
541 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
542
543 gtk_signal_connect( GTK_OBJECT(list_item),
544 "button_press_event",
545 (GtkSignalFunc)gtk_listbox_button_press_callback,
546 (gpointer) this );
547
548 gtk_signal_connect_after( GTK_OBJECT(list_item),
549 "button_release_event",
550 (GtkSignalFunc)gtk_listbox_button_release_callback,
551 (gpointer) this );
552
553 #if wxUSE_CHECKLISTBOX
554 if (m_hasCheckBoxes)
555 {
556 gtk_signal_connect( GTK_OBJECT(list_item),
557 "key_press_event",
558 (GtkSignalFunc)gtk_listbox_key_press_callback,
559 (gpointer)this );
560 }
561 #endif // wxUSE_CHECKLISTBOX
562
563 gtk_widget_show( list_item );
564
565 ConnectWidget( list_item );
566
567 if (GTK_WIDGET_REALIZED(m_widget))
568 {
569 gtk_widget_realize( list_item );
570 gtk_widget_realize( GTK_BIN(list_item)->child );
571
572 //if (m_widgetStyle) ApplyWidgetStyle();
573 if (m_widgetStyle) {
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 );
579 }
580
581 #if wxUSE_DRAG_AND_DROP
582 #ifndef NEW_GTK_DND_CODE
583 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
584 #endif
585 #endif
586
587 #if wxUSE_TOOLTIPS
588 if (m_tooltip) m_tooltip->Apply( this );
589 #endif
590 }
591 }
592
593 void wxListBox::DoSetItems( const wxArrayString& items,
594 void **clientData)
595 {
596 Clear();
597
598 DoInsertItems(items, 0);
599
600 if ( clientData )
601 {
602 size_t count = items.GetCount();
603 for ( size_t n = 0; n < count; n++ )
604 {
605 SetClientData(n, clientData[n]);
606 }
607 }
608 }
609
610 // ----------------------------------------------------------------------------
611 // client data
612 // ----------------------------------------------------------------------------
613
614 void wxListBox::DoSetItemClientData( int n, void* clientData )
615 {
616 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
617
618 wxNode *node = m_clientData.Nth( n );
619 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
620
621 node->SetData( (wxObject*) clientData );
622 }
623
624 void* wxListBox::DoGetItemClientData( int n ) const
625 {
626 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
627
628 wxNode *node = m_clientData.Nth( n );
629 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
630
631 return node->Data();
632 }
633
634 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
635 {
636 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
637
638 wxNode *node = m_clientData.Nth( n );
639 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
640
641 wxClientData *cd = (wxClientData*) node->Data();
642 delete cd;
643
644 node->SetData( (wxObject*) clientData );
645 }
646
647 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
648 {
649 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
650
651 wxNode *node = m_clientData.Nth( n );
652 wxCHECK_MSG( node, (wxClientData *)NULL,
653 wxT("invalid index in wxListBox::DoGetItemClientObject") );
654
655 return (wxClientData*) node->Data();
656 }
657
658 void wxListBox::Clear()
659 {
660 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
661
662 gtk_list_clear_items( m_list, 0, Number() );
663
664 if ( HasClientObjectData() )
665 {
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();
670 while ( node )
671 {
672 delete (wxClientData *)node->Data();
673 node = node->Next();
674 }
675 }
676 m_clientData.Clear();
677
678 if ( m_strings )
679 m_strings->Clear();
680 }
681
682 void wxListBox::Delete( int n )
683 {
684 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
685
686 GList *child = g_list_nth( m_list->children, n );
687
688 wxCHECK_RET( child, wxT("wrong listbox index") );
689
690 GList *list = g_list_append( (GList*) NULL, child->data );
691 gtk_list_remove_items( m_list, list );
692 g_list_free( list );
693
694 wxNode *node = m_clientData.Nth( n );
695 if ( node )
696 {
697 if ( m_clientDataItemsType == ClientData_Object )
698 {
699 wxClientData *cd = (wxClientData*)node->Data();
700 delete cd;
701 }
702
703 m_clientData.DeleteNode( node );
704 }
705
706 if ( m_strings )
707 m_strings->Remove(n);
708 }
709
710 // ----------------------------------------------------------------------------
711 // string list access
712 // ----------------------------------------------------------------------------
713
714 void wxListBox::SetString( int n, const wxString &string )
715 {
716 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
717
718 GList *child = g_list_nth( m_list->children, n );
719 if (child)
720 {
721 GtkBin *bin = GTK_BIN( child->data );
722 GtkLabel *label = GTK_LABEL( bin->child );
723
724 wxString str;
725 #if wxUSE_CHECKLISTBOX
726 if (m_hasCheckBoxes)
727 str += CHECKBOX_STRING;
728 #endif // wxUSE_CHECKLISTBOX
729 str += string;
730
731 gtk_label_set( label, str.mbc_str() );
732 }
733 else
734 {
735 wxFAIL_MSG(wxT("wrong listbox index"));
736 }
737 }
738
739 wxString wxListBox::GetString( int n ) const
740 {
741 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
742
743 GList *child = g_list_nth( m_list->children, n );
744 if (child)
745 {
746 GtkBin *bin = GTK_BIN( child->data );
747 GtkLabel *label = GTK_LABEL( bin->child );
748
749 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
750
751 return str;
752 }
753
754 wxFAIL_MSG(wxT("wrong listbox index"));
755
756 return wxT("");
757 }
758
759 int wxListBox::GetCount() const
760 {
761 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
762
763 GList *child = m_list->children;
764 int count = 0;
765 while (child)
766 {
767 count++;
768 child = child->next;
769 }
770
771 return count;
772 }
773
774 int wxListBox::FindString( const wxString &item ) const
775 {
776 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
777
778 GList *child = m_list->children;
779 int count = 0;
780 while (child)
781 {
782 GtkBin *bin = GTK_BIN( child->data );
783 GtkLabel *label = GTK_LABEL( bin->child );
784
785 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
786
787 if (str == item)
788 return count;
789
790 count++;
791 child = child->next;
792 }
793
794 // it's not an error if the string is not found -> no wxCHECK
795
796 return wxNOT_FOUND;
797 }
798
799 // ----------------------------------------------------------------------------
800 // selection
801 // ----------------------------------------------------------------------------
802
803 int wxListBox::GetSelection() const
804 {
805 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
806
807 GList *child = m_list->children;
808 int count = 0;
809 while (child)
810 {
811 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
812 count++;
813 child = child->next;
814 }
815 return -1;
816 }
817
818 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
819 {
820 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
821
822 // get the number of selected items first
823 GList *child = m_list->children;
824 int count = 0;
825 for (child = m_list->children; child != NULL; child = child->next)
826 {
827 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
828 count++;
829 }
830
831 aSelections.Empty();
832
833 if (count > 0)
834 {
835 // now fill the list
836 aSelections.Alloc(count); // optimization attempt
837 int i = 0;
838 for (child = m_list->children; child != NULL; child = child->next, i++)
839 {
840 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
841 aSelections.Add(i);
842 }
843 }
844
845 return count;
846 }
847
848 bool wxListBox::IsSelected( int n ) const
849 {
850 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
851
852 GList *target = g_list_nth( m_list->children, n );
853 if (target)
854 {
855 GList *child = m_list->selection;
856 while (child)
857 {
858 if (child->data == target->data) return TRUE;
859 child = child->next;
860 }
861 }
862
863 wxFAIL_MSG(wxT("wrong listbox index"));
864
865 return FALSE;
866 }
867
868 void wxListBox::SetSelection( int n, bool select )
869 {
870 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
871
872 DisableEvents();
873
874 if (select)
875 gtk_list_select_item( m_list, n );
876 else
877 gtk_list_unselect_item( m_list, n );
878
879 EnableEvents();
880 }
881
882 void wxListBox::DoSetFirstItem( int WXUNUSED(n) )
883 {
884 wxFAIL_MSG(wxT("wxListBox::SetFirstItem not implemented"));
885 }
886
887 // ----------------------------------------------------------------------------
888 // helpers
889 // ----------------------------------------------------------------------------
890
891 int wxListBox::GetIndex( GtkWidget *item ) const
892 {
893 if (item)
894 {
895 GList *child = m_list->children;
896 int count = 0;
897 while (child)
898 {
899 if (GTK_WIDGET(child->data) == item) return count;
900 count++;
901 child = child->next;
902 }
903 }
904 return -1;
905 }
906
907 #if wxUSE_TOOLTIPS
908 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
909 {
910 GList *child = m_list->children;
911 while (child)
912 {
913 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
914 child = child->next;
915 }
916 }
917 #endif // wxUSE_TOOLTIPS
918
919 #if wxUSE_DRAG_AND_DROP
920 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
921 {
922 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
923
924 #ifndef NEW_GTK_DND_CODE
925 if (m_dropTarget)
926 {
927 GList *child = m_list->children;
928 while (child)
929 {
930 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
931 child = child->next;
932 }
933 }
934 #endif
935
936 wxWindow::SetDropTarget( dropTarget );
937
938 #ifndef NEW_GTK_DND_CODE
939 if (m_dropTarget)
940 {
941 GList *child = m_list->children;
942 while (child)
943 {
944 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
945 child = child->next;
946 }
947 }
948 #endif
949 }
950 #endif
951
952 void wxListBox::DisableEvents()
953 {
954 GList *child = m_list->children;
955 while (child)
956 {
957 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
958 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
959
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 );
963
964 child = child->next;
965 }
966 }
967
968 void wxListBox::EnableEvents()
969 {
970 GList *child = m_list->children;
971 while (child)
972 {
973 gtk_signal_connect( GTK_OBJECT(child->data), "select",
974 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
975
976 if (HasFlag(wxLB_MULTIPLE))
977 gtk_signal_connect( GTK_OBJECT(child->data), "deselect",
978 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
979
980 child = child->next;
981 }
982 }
983
984 GtkWidget *wxListBox::GetConnectWidget()
985 {
986 return GTK_WIDGET(m_list);
987 }
988
989 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
990 {
991 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
992
993 GList *child = m_list->children;
994 while (child)
995 {
996 GtkWidget *bin = GTK_WIDGET( child->data );
997 if (bin->window == window) return TRUE;
998 child = child->next;
999 }
1000
1001 return FALSE;
1002 }
1003
1004 void wxListBox::ApplyWidgetStyle()
1005 {
1006 SetWidgetStyle();
1007
1008 if (m_backgroundColour.Ok())
1009 {
1010 GdkWindow *window = GTK_WIDGET(m_list)->window;
1011 if ( window )
1012 {
1013 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1014 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1015 gdk_window_clear( window );
1016 }
1017 }
1018
1019 GList *child = m_list->children;
1020 while (child)
1021 {
1022 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
1023
1024 GtkBin *bin = GTK_BIN( child->data );
1025 GtkWidget *label = GTK_WIDGET( bin->child );
1026 gtk_widget_set_style( label, m_widgetStyle );
1027
1028 child = child->next;
1029 }
1030 }
1031
1032 void wxListBox::OnInternalIdle()
1033 {
1034 wxCursor cursor = m_cursor;
1035 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1036
1037 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1038 {
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
1042 not possible. */
1043
1044 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1045
1046 GList *child = m_list->children;
1047 while (child)
1048 {
1049 GtkBin *bin = GTK_BIN( child->data );
1050 GtkWidget *label = GTK_WIDGET( bin->child );
1051
1052 if (!label->window)
1053 break;
1054 else
1055 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1056
1057 child = child->next;
1058 }
1059 }
1060
1061 UpdateWindowUI();
1062 }
1063
1064 #endif