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