Applied a few patches,
[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 count = listbox->GetSelections(aSelections);
110 if ( count > 0 )
111 {
112 event.m_commandInt = aSelections[0] ;
113 event.m_clientData = listbox->GetClientData( event.m_commandInt );
114 wxString str(listbox->GetString(event.m_commandInt));
115 if (!str.IsEmpty()) event.m_commandString = str;
116 }
117 else
118 {
119 event.m_commandInt = -1 ;
120 event.m_commandString.Empty();
121 }
122
123 listbox->GetEventHandler()->ProcessEvent( event );
124
125 return FALSE;
126 }
127
128 //-----------------------------------------------------------------------------
129 // "button_press_event"
130 //-----------------------------------------------------------------------------
131
132 static gint
133 gtk_listbox_button_press_callback( GtkWidget *widget,
134 GdkEventButton *gdk_event,
135 wxListBox *listbox )
136 {
137 if (g_isIdle) wxapp_install_idle_handler();
138
139 if (g_blockEventsOnDrag) return FALSE;
140 if (g_blockEventsOnScroll) return FALSE;
141
142 if (!listbox->m_hasVMT) return FALSE;
143
144 int sel = listbox->GetIndex( widget );
145
146 #if wxUSE_CHECKLISTBOX
147 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
148 {
149 wxCheckListBox *clb = (wxCheckListBox *)listbox;
150
151 clb->Check( sel, !clb->IsChecked(sel) );
152
153 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
154 event.SetEventObject( listbox );
155 event.SetInt( sel );
156 listbox->GetEventHandler()->ProcessEvent( event );
157 }
158 #endif // wxUSE_CHECKLISTBOX
159
160 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
161 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
162
163 return FALSE;
164 }
165
166 //-----------------------------------------------------------------------------
167 // "key_press_event"
168 //-----------------------------------------------------------------------------
169
170 #if wxUSE_CHECKLISTBOX
171 static gint
172 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
173 {
174 if (g_isIdle) wxapp_install_idle_handler();
175
176 if (g_blockEventsOnDrag) return FALSE;
177
178 if (!listbox->m_hasVMT) return FALSE;
179
180 if (gdk_event->keyval != ' ') return FALSE;
181
182 int sel = listbox->GetIndex( widget );
183
184 wxCheckListBox *clb = (wxCheckListBox *)listbox;
185
186 clb->Check( sel, !clb->IsChecked(sel) );
187
188 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
189 event.SetEventObject( listbox );
190 event.SetInt( sel );
191 listbox->GetEventHandler()->ProcessEvent( event );
192
193 return FALSE;
194 }
195 #endif // wxUSE_CHECKLISTBOX
196
197 //-----------------------------------------------------------------------------
198 // "select" and "deselect"
199 //-----------------------------------------------------------------------------
200
201 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox );
202
203 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
204 {
205 gtk_listitem_select_callback( widget, listbox );
206 }
207
208 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
209 {
210 if (g_isIdle) wxapp_install_idle_handler();
211
212 if (!listbox->m_hasVMT) return;
213 if (g_blockEventsOnDrag) return;
214
215 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
216
217 wxArrayInt aSelections;
218 int count = listbox->GetSelections(aSelections);
219 if ( count > 0 )
220 {
221 event.m_commandInt = aSelections[0] ;
222 event.m_clientData = listbox->GetClientData( event.m_commandInt );
223 wxString str(listbox->GetString(event.m_commandInt));
224 if (!str.IsEmpty()) event.m_commandString = str;
225 }
226 else
227 {
228 event.m_commandInt = -1 ;
229 event.m_commandString.Empty();
230 }
231
232 event.SetEventObject( listbox );
233
234 listbox->GetEventHandler()->ProcessEvent( event );
235 }
236
237 //-----------------------------------------------------------------------------
238 // wxListBox
239 //-----------------------------------------------------------------------------
240
241 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
242
243 wxListBox::wxListBox()
244 {
245 m_list = (GtkList *) NULL;
246 #if wxUSE_CHECKLISTBOX
247 m_hasCheckBoxes = FALSE;
248 #endif // wxUSE_CHECKLISTBOX
249 }
250
251 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
252 const wxPoint &pos, const wxSize &size,
253 int n, const wxString choices[],
254 long style, const wxValidator& validator, const wxString &name )
255 {
256 m_needParent = TRUE;
257 m_acceptsFocus = TRUE;
258
259 if (!PreCreation( parent, pos, size ) ||
260 !CreateBase( parent, id, pos, size, style, validator, name ))
261 {
262 wxFAIL_MSG( wxT("wxListBox creation failed") );
263 return FALSE;
264 }
265
266 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
267 if (style & wxLB_ALWAYS_SB)
268 {
269 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
270 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
271 }
272 else
273 {
274 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
275 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
276 }
277
278 m_list = GTK_LIST( gtk_list_new() );
279
280 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
281 if (style & wxLB_MULTIPLE)
282 mode = GTK_SELECTION_MULTIPLE;
283 else if (style & wxLB_EXTENDED)
284 mode = GTK_SELECTION_EXTENDED;
285
286 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
287
288 #ifdef NEW_GTK_SCROLL_CODE
289 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
290 #else
291 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
292 #endif
293
294 /* make list scroll when moving the focus down using cursor keys */
295 gtk_container_set_focus_vadjustment(
296 GTK_CONTAINER(m_list),
297 gtk_scrolled_window_get_vadjustment(
298 GTK_SCROLLED_WINDOW(m_widget)));
299
300 gtk_widget_show( GTK_WIDGET(m_list) );
301
302 wxSize newSize = size;
303 if (newSize.x == -1) newSize.x = 100;
304 if (newSize.y == -1) newSize.y = 110;
305 SetSize( newSize.x, newSize.y );
306
307 for (int i = 0; i < n; i++)
308 {
309 m_clientDataList.Append( (wxObject*) NULL );
310 m_clientObjectList.Append( (wxObject*) NULL );
311
312 GtkWidget *list_item;
313
314 wxString str(choices[i]);
315 #if wxUSE_CHECKLISTBOX
316 if (m_hasCheckBoxes)
317 {
318 str.Prepend(CHECKBOX_STRING);
319 }
320 #endif // wxUSE_CHECKLISTBOX
321
322 list_item = gtk_list_item_new_with_label( str.mbc_str() );
323
324 gtk_container_add( GTK_CONTAINER(m_list), list_item );
325
326 gtk_signal_connect( GTK_OBJECT(list_item), "select",
327 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
328
329 if (style & wxLB_MULTIPLE)
330 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
331 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
332
333 gtk_signal_connect( GTK_OBJECT(list_item),
334 "button_press_event",
335 (GtkSignalFunc)gtk_listbox_button_press_callback,
336 (gpointer) this );
337
338 gtk_signal_connect_after( GTK_OBJECT(list_item),
339 "button_release_event",
340 (GtkSignalFunc)gtk_listbox_button_release_callback,
341 (gpointer) this );
342
343 #if wxUSE_CHECKLISTBOX
344 if (m_hasCheckBoxes)
345 {
346 gtk_signal_connect( GTK_OBJECT(list_item),
347 "key_press_event",
348 (GtkSignalFunc)gtk_listbox_key_press_callback,
349 (gpointer)this );
350 }
351 #endif // wxUSE_CHECKLISTBOX
352
353 ConnectWidget( list_item );
354
355 gtk_widget_show( list_item );
356 }
357
358 m_parent->DoAddChild( this );
359
360 PostCreation();
361
362 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) );
363 SetForegroundColour( parent->GetForegroundColour() );
364 SetFont( parent->GetFont() );
365
366 Show( TRUE );
367
368 return TRUE;
369 }
370
371 wxListBox::~wxListBox()
372 {
373 Clear();
374 }
375
376 void wxListBox::InsertItems(int nItems, const wxString items[], int pos)
377 {
378 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
379
380 GList *children = m_list->children;
381 int length = g_list_length(children);
382 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
383
384 // VZ: it seems that GTK 1.0.6 doesn't has a function to insert an item
385 // into a listbox at the given position, this is why we first delete
386 // all items after this position, then append these items and then
387 // reappend back the old ones.
388
389 // first detach the old items
390 int n; // loop var
391
392 if ( pos == length )
393 {
394 // no need to do anything complicated
395 for ( n = 0; n < nItems; n++ )
396 {
397 Append(items[n]);
398 }
399
400 return;
401 }
402
403 wxArrayString deletedLabels;
404 wxArrayPtrVoid deletedData;
405 wxArrayInt deletedChecks; // only for check list boxes
406
407 GList *child = g_list_nth( children, pos );
408 for ( n = 0; child != NULL; n++, child = child->next )
409 {
410 // save label
411 GtkBin *bin = GTK_BIN( child->data );
412 GtkLabel *label = GTK_LABEL( bin->child );
413
414 wxString str(GET_REAL_LABEL(label->label),*wxConvCurrent);
415 deletedLabels.Add(str);
416
417 // save data
418 void *clientData = NULL;
419 wxNode *node = NULL;
420
421 if ( n < (int)m_clientObjectList.GetCount() )
422 node = m_clientObjectList.Nth( n );
423
424 if ( node )
425 {
426 clientData = node->GetData();
427 m_clientObjectList.DeleteNode( node );
428 }
429
430 if ( !clientData )
431 {
432 if ( n < (int)m_clientDataList.GetCount() )
433 node = m_clientDataList.Nth( n );
434
435 if ( node )
436 {
437 clientData = node->GetData();
438 node = m_clientDataList.Nth( n );
439 }
440 }
441
442 deletedData.Add(clientData);
443
444 #if wxUSE_CHECKLISTBOX
445 // save check state
446 if ( m_hasCheckBoxes )
447 {
448 deletedChecks.Add(((wxCheckListBox *)this)->IsChecked(pos + n));
449 }
450 #endif // wxUSE_CHECKLISTBOX
451 }
452
453 int nDeletedCount = n;
454
455 gtk_list_clear_items( m_list, pos, length );
456
457 // now append the new items
458 for ( n = 0; n < nItems; n++ )
459 {
460 Append(items[n]);
461 }
462
463 // and append the old items too
464 pos += nItems; // now the indices are shifter
465 for ( n = 0; n < nDeletedCount; n++ )
466 {
467 Append(deletedLabels[n], deletedData[n]);
468
469 #if wxUSE_CHECKLISTBOX
470 if ( m_hasCheckBoxes )
471 {
472 ((wxCheckListBox *)this)->Check(pos + n, (bool)deletedChecks[n]);
473 }
474 #endif // wxUSE_CHECKLISTBOX
475 }
476 }
477
478 void wxListBox::AppendCommon( const wxString &item )
479 {
480 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
481
482 GtkWidget *list_item;
483
484 wxString label(item);
485 #if wxUSE_CHECKLISTBOX
486 if (m_hasCheckBoxes)
487 {
488 label.Prepend(CHECKBOX_STRING);
489 }
490 #endif // wxUSE_CHECKLISTBOX
491
492 list_item = gtk_list_item_new_with_label( label.mbc_str() );
493
494 gtk_container_add( GTK_CONTAINER(m_list), list_item );
495
496 gtk_signal_connect( GTK_OBJECT(list_item), "select",
497 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
498
499 if (HasFlag(wxLB_MULTIPLE))
500 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
501 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
502
503 gtk_signal_connect( GTK_OBJECT(list_item),
504 "button_press_event",
505 (GtkSignalFunc)gtk_listbox_button_press_callback,
506 (gpointer) this );
507
508 gtk_signal_connect_after( GTK_OBJECT(list_item),
509 "button_release_event",
510 (GtkSignalFunc)gtk_listbox_button_release_callback,
511 (gpointer) this );
512
513 #if wxUSE_CHECKLISTBOX
514 if (m_hasCheckBoxes)
515 {
516 gtk_signal_connect( GTK_OBJECT(list_item),
517 "key_press_event",
518 (GtkSignalFunc)gtk_listbox_key_press_callback,
519 (gpointer)this );
520 }
521 #endif // wxUSE_CHECKLISTBOX
522
523 gtk_widget_show( list_item );
524
525 ConnectWidget( list_item );
526
527 if (GTK_WIDGET_REALIZED(m_widget))
528 {
529 gtk_widget_realize( list_item );
530 gtk_widget_realize( GTK_BIN(list_item)->child );
531
532 //if (m_widgetStyle) ApplyWidgetStyle();
533 if (m_widgetStyle) {
534 // Apply current widget style to the new list_item
535 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
536 GtkBin *bin = GTK_BIN( list_item );
537 GtkWidget *label = GTK_WIDGET( bin->child );
538 gtk_widget_set_style( label, m_widgetStyle );
539 }
540
541 #if wxUSE_DRAG_AND_DROP
542 #ifndef NEW_GTK_DND_CODE
543 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
544 #endif
545 #endif
546
547 #if wxUSE_TOOLTIPS
548 if (m_tooltip) m_tooltip->Apply( this );
549 #endif
550 }
551 }
552
553 void wxListBox::Append( const wxString &item )
554 {
555 m_clientDataList.Append( (wxObject*) NULL );
556 m_clientObjectList.Append( (wxObject*) NULL );
557
558 AppendCommon( item );
559 }
560
561 void wxListBox::Append( const wxString &item, void *clientData )
562 {
563 m_clientDataList.Append( (wxObject*) clientData );
564 m_clientObjectList.Append( (wxObject*) NULL );
565
566 AppendCommon( item );
567 }
568
569 void wxListBox::Append( const wxString &item, wxClientData *clientData )
570 {
571 m_clientObjectList.Append( (wxObject*) clientData );
572 m_clientDataList.Append( (wxObject*) NULL );
573
574 AppendCommon( item );
575 }
576
577 void wxListBox::SetClientData( int n, void* clientData )
578 {
579 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
580
581 wxNode *node = m_clientDataList.Nth( n );
582 if (!node) return;
583
584 node->SetData( (wxObject*) clientData );
585 }
586
587 void* wxListBox::GetClientData( int n )
588 {
589 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
590
591 wxNode *node = m_clientDataList.Nth( n );
592 if (!node) return NULL;
593
594 return node->Data();
595 }
596
597 void wxListBox::SetClientObject( int n, wxClientData* clientData )
598 {
599 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
600
601 wxNode *node = m_clientObjectList.Nth( n );
602 if (!node) return;
603
604 wxClientData *cd = (wxClientData*) node->Data();
605 if (cd) delete cd;
606
607 node->SetData( (wxObject*) clientData );
608 }
609
610 wxClientData* wxListBox::GetClientObject( int n )
611 {
612 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
613
614 wxNode *node = m_clientObjectList.Nth( n );
615 if (!node) return (wxClientData*) NULL;
616
617 return (wxClientData*) node->Data();
618 }
619
620 void wxListBox::Clear()
621 {
622 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
623
624 gtk_list_clear_items( m_list, 0, Number() );
625
626 wxNode *node = m_clientObjectList.First();
627 while (node)
628 {
629 wxClientData *cd = (wxClientData*)node->Data();
630 if (cd) delete cd;
631 node = node->Next();
632 }
633 m_clientObjectList.Clear();
634
635 m_clientDataList.Clear();
636 }
637
638 void wxListBox::Delete( int n )
639 {
640 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
641
642 GList *child = g_list_nth( m_list->children, n );
643
644 wxCHECK_RET( child, wxT("wrong listbox index") );
645
646 GList *list = g_list_append( (GList*) NULL, child->data );
647 gtk_list_remove_items( m_list, list );
648 g_list_free( list );
649
650 wxNode *node = m_clientObjectList.Nth( n );
651 if (node)
652 {
653 wxClientData *cd = (wxClientData*)node->Data();
654 if (cd) delete cd;
655 m_clientObjectList.DeleteNode( node );
656 }
657
658 node = m_clientDataList.Nth( n );
659 if (node)
660 {
661 m_clientDataList.DeleteNode( node );
662 }
663 }
664
665 void wxListBox::Deselect( int n )
666 {
667 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
668
669 DisableEvents();
670
671 gtk_list_unselect_item( m_list, n );
672
673 EnableEvents();
674 }
675
676 int wxListBox::FindString( const wxString &item ) const
677 {
678 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
679
680 GList *child = m_list->children;
681 int count = 0;
682 while (child)
683 {
684 GtkBin *bin = GTK_BIN( child->data );
685 GtkLabel *label = GTK_LABEL( bin->child );
686
687 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
688
689 if (str == item)
690 return count;
691
692 count++;
693 child = child->next;
694 }
695
696 // it's not an error if the string is not found -> no wxCHECK
697
698 return -1;
699 }
700
701 int wxListBox::GetSelection() const
702 {
703 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
704
705 GList *child = m_list->children;
706 int count = 0;
707 while (child)
708 {
709 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
710 count++;
711 child = child->next;
712 }
713 return -1;
714 }
715
716 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
717 {
718 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
719
720 // get the number of selected items first
721 GList *child = m_list->children;
722 int count = 0;
723 for (child = m_list->children; child != NULL; child = child->next)
724 {
725 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
726 count++;
727 }
728
729 aSelections.Empty();
730
731 if (count > 0)
732 {
733 // now fill the list
734 aSelections.Alloc(count); // optimization attempt
735 int i = 0;
736 for (child = m_list->children; child != NULL; child = child->next, i++)
737 {
738 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
739 aSelections.Add(i);
740 }
741 }
742
743 return count;
744 }
745
746 wxString wxListBox::GetString( int n ) const
747 {
748 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
749
750 GList *child = g_list_nth( m_list->children, n );
751 if (child)
752 {
753 GtkBin *bin = GTK_BIN( child->data );
754 GtkLabel *label = GTK_LABEL( bin->child );
755
756 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
757
758 return str;
759 }
760
761 wxFAIL_MSG(wxT("wrong listbox index"));
762
763 return wxT("");
764 }
765
766 wxString wxListBox::GetStringSelection() const
767 {
768 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
769
770 GList *selection = m_list->selection;
771 if (selection)
772 {
773 GtkBin *bin = GTK_BIN( selection->data );
774 GtkLabel *label = GTK_LABEL( bin->child );
775
776 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
777
778 return str;
779 }
780
781 wxFAIL_MSG(wxT("no listbox selection available"));
782 return wxT("");
783 }
784
785 int wxListBox::Number()
786 {
787 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
788
789 GList *child = m_list->children;
790 int count = 0;
791 while (child) { count++; child = child->next; }
792 return count;
793 }
794
795 bool wxListBox::Selected( int n )
796 {
797 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
798
799 GList *target = g_list_nth( m_list->children, n );
800 if (target)
801 {
802 GList *child = m_list->selection;
803 while (child)
804 {
805 if (child->data == target->data) return TRUE;
806 child = child->next;
807 }
808 }
809 wxFAIL_MSG(wxT("wrong listbox index"));
810 return FALSE;
811 }
812
813 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
814 {
815 wxFAIL_MSG(wxT("wxListBox::Set not implemented"));
816 }
817
818 void wxListBox::SetFirstItem( int WXUNUSED(n) )
819 {
820 wxFAIL_MSG(wxT("wxListBox::SetFirstItem not implemented"));
821 }
822
823 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
824 {
825 wxFAIL_MSG(wxT("wxListBox::SetFirstItem not implemented"));
826 }
827
828 void wxListBox::SetSelection( int n, bool select )
829 {
830 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
831
832 DisableEvents();
833
834 if (select)
835 gtk_list_select_item( m_list, n );
836 else
837 gtk_list_unselect_item( m_list, n );
838
839 EnableEvents();
840 }
841
842 void wxListBox::SetString( int n, const wxString &string )
843 {
844 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
845
846 GList *child = g_list_nth( m_list->children, n );
847 if (child)
848 {
849 GtkBin *bin = GTK_BIN( child->data );
850 GtkLabel *label = GTK_LABEL( bin->child );
851
852 wxString str;
853 #if wxUSE_CHECKLISTBOX
854 if (m_hasCheckBoxes)
855 str += CHECKBOX_STRING;
856 #endif // wxUSE_CHECKLISTBOX
857 str += string;
858
859 gtk_label_set( label, str.mbc_str() );
860 }
861 else
862 {
863 wxFAIL_MSG(wxT("wrong listbox index"));
864 }
865 }
866
867 void wxListBox::SetStringSelection( const wxString &string, bool select )
868 {
869 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
870
871 SetSelection( FindString(string), select );
872 }
873
874 int wxListBox::GetIndex( GtkWidget *item ) const
875 {
876 if (item)
877 {
878 GList *child = m_list->children;
879 int count = 0;
880 while (child)
881 {
882 if (GTK_WIDGET(child->data) == item) return count;
883 count++;
884 child = child->next;
885 }
886 }
887 return -1;
888 }
889
890 #if wxUSE_TOOLTIPS
891 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
892 {
893 GList *child = m_list->children;
894 while (child)
895 {
896 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
897 child = child->next;
898 }
899 }
900 #endif // wxUSE_TOOLTIPS
901
902 #if wxUSE_DRAG_AND_DROP
903 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
904 {
905 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
906
907 #ifndef NEW_GTK_DND_CODE
908 if (m_dropTarget)
909 {
910 GList *child = m_list->children;
911 while (child)
912 {
913 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
914 child = child->next;
915 }
916 }
917 #endif
918
919 wxWindow::SetDropTarget( dropTarget );
920
921 #ifndef NEW_GTK_DND_CODE
922 if (m_dropTarget)
923 {
924 GList *child = m_list->children;
925 while (child)
926 {
927 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
928 child = child->next;
929 }
930 }
931 #endif
932 }
933 #endif
934
935 void wxListBox::DisableEvents()
936 {
937 GList *child = m_list->children;
938 while (child)
939 {
940 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
941 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
942
943 if (HasFlag(wxLB_MULTIPLE))
944 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
945 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
946
947 child = child->next;
948 }
949 }
950
951 void wxListBox::EnableEvents()
952 {
953 GList *child = m_list->children;
954 while (child)
955 {
956 gtk_signal_connect( GTK_OBJECT(child->data), "select",
957 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
958
959 if (HasFlag(wxLB_MULTIPLE))
960 gtk_signal_connect( GTK_OBJECT(child->data), "deselect",
961 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
962
963 child = child->next;
964 }
965 }
966
967 GtkWidget *wxListBox::GetConnectWidget()
968 {
969 return GTK_WIDGET(m_list);
970 }
971
972 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
973 {
974 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
975
976 GList *child = m_list->children;
977 while (child)
978 {
979 GtkWidget *bin = GTK_WIDGET( child->data );
980 if (bin->window == window) return TRUE;
981 child = child->next;
982 }
983
984 return FALSE;
985 }
986
987 void wxListBox::ApplyWidgetStyle()
988 {
989 SetWidgetStyle();
990
991 if (m_backgroundColour.Ok())
992 {
993 GdkWindow *window = GTK_WIDGET(m_list)->window;
994 if ( window )
995 {
996 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
997 gdk_window_set_background( window, m_backgroundColour.GetColor() );
998 gdk_window_clear( window );
999 }
1000 }
1001
1002 GList *child = m_list->children;
1003 while (child)
1004 {
1005 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
1006
1007 GtkBin *bin = GTK_BIN( child->data );
1008 GtkWidget *label = GTK_WIDGET( bin->child );
1009 gtk_widget_set_style( label, m_widgetStyle );
1010
1011 child = child->next;
1012 }
1013 }
1014
1015 void wxListBox::OnInternalIdle()
1016 {
1017 wxCursor cursor = m_cursor;
1018 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1019
1020 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1021 {
1022 /* I now set the cursor the anew in every OnInternalIdle call
1023 as setting the cursor in a parent window also effects the
1024 windows above so that checking for the current cursor is
1025 not possible. */
1026
1027 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1028
1029 GList *child = m_list->children;
1030 while (child)
1031 {
1032 GtkBin *bin = GTK_BIN( child->data );
1033 GtkWidget *label = GTK_WIDGET( bin->child );
1034
1035 if (!label->window)
1036 break;
1037 else
1038 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1039
1040 child = child->next;
1041 }
1042 }
1043
1044 UpdateWindowUI();
1045 }
1046
1047 #endif