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