]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/listbox.cpp
Added wxCURSOR_ARROWWAIT which is the default arrow+hourglass cursor
[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 #include <gdk/gdk.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32
33 //-----------------------------------------------------------------------------
34 // idle system
35 //-----------------------------------------------------------------------------
36
37 extern void wxapp_install_idle_handler();
38 extern bool g_isIdle;
39
40 //-------------------------------------------------------------------------
41 // conditional compilation
42 //-------------------------------------------------------------------------
43
44 #if (GTK_MINOR_VERSION > 0)
45 #define NEW_GTK_SCROLL_CODE
46 #endif
47
48 //-----------------------------------------------------------------------------
49 // private functions
50 //-----------------------------------------------------------------------------
51
52 #if wxUSE_CHECKLISTBOX
53
54 // checklistboxes have "[±] " prepended to their lables, this macro removes it
55 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
56 //
57 // the argument to it is a "const char *" pointer
58 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
59
60 #else // !wxUSE_CHECKLISTBOX
61
62 #define GET_REAL_LABEL(label) (label)
63
64 #endif // wxUSE_CHECKLISTBOX
65
66 //-----------------------------------------------------------------------------
67 // data
68 //-----------------------------------------------------------------------------
69
70 extern bool g_blockEventsOnDrag;
71 extern bool g_blockEventsOnScroll;
72 extern wxCursor g_globalCursor;
73
74 static bool g_hasDoubleClicked = FALSE;
75
76 //-----------------------------------------------------------------------------
77 // idle callback for SetFirstItem
78 //-----------------------------------------------------------------------------
79
80 struct wxlistbox_idle_struct
81 {
82 wxListBox *m_listbox;
83 int m_item;
84 gint m_tag;
85 };
86
87 static gint wxlistbox_idle_callback( gpointer gdata )
88 {
89 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
90 gdk_threads_enter();
91
92 gtk_idle_remove( data->m_tag );
93
94 data->m_listbox->SetFirstItem( data->m_item );
95
96 delete data;
97
98 gdk_threads_leave();
99
100 return TRUE;
101 }
102
103 //-----------------------------------------------------------------------------
104 // "button_release_event"
105 //-----------------------------------------------------------------------------
106
107 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
108 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
109 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
110 this can lead to race conditions so that we emit the dclick event
111 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
112
113 static gint
114 gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
115 GdkEventButton * WXUNUSED(gdk_event),
116 wxListBox *listbox )
117 {
118 if (g_isIdle) wxapp_install_idle_handler();
119
120 if (g_blockEventsOnDrag) return FALSE;
121 if (g_blockEventsOnScroll) return FALSE;
122
123 if (!listbox->m_hasVMT) return FALSE;
124
125 if (!g_hasDoubleClicked) return FALSE;
126
127 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
128 event.SetEventObject( listbox );
129
130 wxArrayInt aSelections;
131 int n, count = listbox->GetSelections(aSelections);
132 if ( count > 0 )
133 {
134 n = aSelections[0];
135 if ( listbox->HasClientObjectData() )
136 event.SetClientObject( listbox->GetClientObject(n) );
137 else if ( listbox->HasClientUntypedData() )
138 event.SetClientData( listbox->GetClientData(n) );
139 event.SetString( listbox->GetString(n) );
140 }
141 else
142 {
143 n = -1;
144 }
145
146 event.m_commandInt = n;
147
148 listbox->GetEventHandler()->ProcessEvent( event );
149
150 return FALSE;
151 }
152
153 //-----------------------------------------------------------------------------
154 // "button_press_event"
155 //-----------------------------------------------------------------------------
156
157 static gint
158 gtk_listbox_button_press_callback( GtkWidget *widget,
159 GdkEventButton *gdk_event,
160 wxListBox *listbox )
161 {
162 if (g_isIdle) wxapp_install_idle_handler();
163
164 if (g_blockEventsOnDrag) return FALSE;
165 if (g_blockEventsOnScroll) return FALSE;
166
167 if (!listbox->m_hasVMT) return FALSE;
168
169 int sel = listbox->GtkGetIndex( widget );
170
171 #if wxUSE_CHECKLISTBOX
172 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
173 {
174 wxCheckListBox *clb = (wxCheckListBox *)listbox;
175
176 clb->Check( sel, !clb->IsChecked(sel) );
177
178 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
179 event.SetEventObject( listbox );
180 event.SetInt( sel );
181 listbox->GetEventHandler()->ProcessEvent( event );
182 }
183 #endif // wxUSE_CHECKLISTBOX
184
185 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
186 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
187
188 return FALSE;
189 }
190
191 //-----------------------------------------------------------------------------
192 // "key_press_event"
193 //-----------------------------------------------------------------------------
194
195 static gint
196 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
197 {
198 if (g_isIdle)
199 wxapp_install_idle_handler();
200
201 if (g_blockEventsOnDrag)
202 return FALSE;
203
204 bool ret = FALSE;
205
206 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
207 {
208 wxNavigationKeyEvent new_event;
209 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
210 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
211 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
212 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
213 new_event.SetCurrentFocus( listbox );
214 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
215 }
216
217 if ((gdk_event->keyval == GDK_Return) && (!ret))
218 {
219 // eat return in all modes
220 ret = TRUE;
221 }
222
223 #if wxUSE_CHECKLISTBOX
224 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
225 {
226 int sel = listbox->GtkGetIndex( widget );
227
228 wxCheckListBox *clb = (wxCheckListBox *)listbox;
229
230 clb->Check( sel, !clb->IsChecked(sel) );
231
232 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
233 new_event.SetEventObject( listbox );
234 new_event.SetInt( sel );
235 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
236 }
237 #endif // wxUSE_CHECKLISTBOX
238
239 if (ret)
240 {
241 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
242 return TRUE;
243 }
244
245 return FALSE;
246 }
247
248 //-----------------------------------------------------------------------------
249 // "select" and "deselect"
250 //-----------------------------------------------------------------------------
251
252 static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
253
254 static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
255 {
256 gtk_listitem_select_cb( widget, listbox, TRUE );
257 }
258
259 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
260 {
261 gtk_listitem_select_cb( widget, listbox, FALSE );
262 }
263
264 static void gtk_listitem_select_cb( GtkWidget *WXUNUSED(widget), wxListBox *listbox, bool is_selection )
265 {
266 if (g_isIdle) wxapp_install_idle_handler();
267
268 if (!listbox->m_hasVMT) return;
269 if (g_blockEventsOnDrag) return;
270
271 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
272 event.SetEventObject( listbox );
273 event.SetExtraLong( (long) is_selection );
274
275 wxArrayInt aSelections;
276 int n, count = listbox->GetSelections(aSelections);
277 if ( count > 0 )
278 {
279 n = aSelections[0];
280 if ( listbox->HasClientObjectData() )
281 event.SetClientObject( listbox->GetClientObject(n) );
282 else if ( listbox->HasClientUntypedData() )
283 event.SetClientData( listbox->GetClientData(n) );
284 event.SetString( listbox->GetString(n) );
285 }
286 else
287 {
288 n = -1;
289 }
290
291 event.m_commandInt = n;
292
293 listbox->GetEventHandler()->AddPendingEvent( event );
294 // listbox->GetEventHandler()->ProcessEvent( event );
295 }
296
297 //-----------------------------------------------------------------------------
298 // wxListBox
299 //-----------------------------------------------------------------------------
300
301 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
302
303 // ----------------------------------------------------------------------------
304 // construction
305 // ----------------------------------------------------------------------------
306
307 wxListBox::wxListBox()
308 {
309 m_list = (GtkList *) NULL;
310 #if wxUSE_CHECKLISTBOX
311 m_hasCheckBoxes = FALSE;
312 #endif // wxUSE_CHECKLISTBOX
313 }
314
315 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
316 const wxPoint &pos, const wxSize &size,
317 int n, const wxString choices[],
318 long style, const wxValidator& validator,
319 const wxString &name )
320 {
321 m_needParent = TRUE;
322 m_acceptsFocus = TRUE;
323
324 if (!PreCreation( parent, pos, size ) ||
325 !CreateBase( parent, id, pos, size, style, validator, name ))
326 {
327 wxFAIL_MSG( wxT("wxListBox creation failed") );
328 return FALSE;
329 }
330
331 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
332 if (style & wxLB_ALWAYS_SB)
333 {
334 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
335 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
336 }
337 else
338 {
339 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
340 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
341 }
342
343 m_list = GTK_LIST( gtk_list_new() );
344
345 GtkSelectionMode mode;
346 if (style & wxLB_MULTIPLE)
347 {
348 mode = GTK_SELECTION_MULTIPLE;
349 }
350 else if (style & wxLB_EXTENDED)
351 {
352 mode = GTK_SELECTION_EXTENDED;
353 }
354 else
355 {
356 // if style was 0 set single mode
357 m_windowStyle |= wxLB_SINGLE;
358 mode = GTK_SELECTION_BROWSE;
359 }
360
361 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
362
363 #ifdef NEW_GTK_SCROLL_CODE
364 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
365 #else
366 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
367 #endif
368
369 /* make list scroll when moving the focus down using cursor keys */
370 gtk_container_set_focus_vadjustment(
371 GTK_CONTAINER(m_list),
372 gtk_scrolled_window_get_vadjustment(
373 GTK_SCROLLED_WINDOW(m_widget)));
374
375 gtk_widget_show( GTK_WIDGET(m_list) );
376
377 SetSizeOrDefault( size );
378
379 if ( style & wxLB_SORT )
380 {
381 // this will change DoAppend() behaviour
382 m_strings = new wxSortedArrayString;
383 }
384 else
385 {
386 m_strings = (wxSortedArrayString *)NULL;
387 }
388
389 for (int i = 0; i < n; i++)
390 {
391 // add one by one
392 DoAppend(choices[i]);
393 }
394
395 m_parent->DoAddChild( this );
396
397 PostCreation();
398
399 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
400 SetForegroundColour( parent->GetForegroundColour() );
401 SetFont( parent->GetFont() );
402
403 Show( TRUE );
404
405 return TRUE;
406 }
407
408 wxListBox::~wxListBox()
409 {
410 m_hasVMT = FALSE;
411
412 Clear();
413
414 if (m_strings)
415 delete m_strings;
416 }
417
418 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
419 {
420 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
421
422 // VZ: notice that InsertItems knows nothing about sorting, so calling it
423 // from outside (and not from our own Append) is likely to break
424 // everything
425
426 // code elsewhere supposes we have as many items in m_clientList as items
427 // in the listbox
428 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
429 wxT("bug in client data management") );
430
431 GList *children = m_list->children;
432 int length = g_list_length(children);
433
434 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
435
436 size_t nItems = items.GetCount();
437 int index;
438
439 if (m_strings)
440 {
441 for (size_t n = 0; n < nItems; n++)
442 {
443 index = m_strings->Add( items[n] );
444
445 if (index != GetCount())
446 {
447 GtkAddItem( items[n], index );
448 wxNode *node = m_clientList.Nth( index );
449 m_clientList.Insert( node, (wxObject*) NULL );
450 }
451 else
452 {
453 GtkAddItem( items[n] );
454 m_clientList.Append( (wxObject*) NULL );
455 }
456 }
457 }
458 else
459 {
460 if (pos == length)
461 {
462 for ( size_t n = 0; n < nItems; n++ )
463 {
464 GtkAddItem( items[n] );
465
466 m_clientList.Append((wxObject *)NULL);
467 }
468 }
469 else
470 {
471 wxNode *node = m_clientList.Nth( pos );
472 for ( size_t n = 0; n < nItems; n++ )
473 {
474 GtkAddItem( items[n], pos+n );
475
476 m_clientList.Insert( node, (wxObject *)NULL );
477 }
478 }
479 }
480
481 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
482 wxT("bug in client data management") );
483 }
484
485 int wxListBox::DoAppend( const wxString& item )
486 {
487 if (m_strings)
488 {
489 // need to determine the index
490 int index = m_strings->Add( item );
491
492 // only if not at the end anyway
493 if (index != GetCount())
494 {
495 GtkAddItem( item, index );
496
497 wxNode *node = m_clientList.Nth( index );
498 m_clientList.Insert( node, (wxObject *)NULL );
499
500 return index;
501 }
502 }
503
504 GtkAddItem(item);
505
506 m_clientList.Append((wxObject *)NULL);
507
508 return GetCount() - 1;
509 }
510
511 void wxListBox::GtkAddItem( const wxString &item, int pos )
512 {
513 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
514
515 GtkWidget *list_item;
516
517 wxString label(item);
518 #if wxUSE_CHECKLISTBOX
519 if (m_hasCheckBoxes)
520 {
521 label.Prepend(wxCHECKLBOX_STRING);
522 }
523 #endif // wxUSE_CHECKLISTBOX
524
525 list_item = gtk_list_item_new_with_label( label.mbc_str() );
526
527 GList *gitem_list = g_list_alloc ();
528 gitem_list->data = list_item;
529
530 if (pos == -1)
531 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
532 else
533 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
534
535 gtk_signal_connect( GTK_OBJECT(list_item), "select",
536 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
537
538 if (HasFlag(wxLB_MULTIPLE))
539 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
540 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
541
542 gtk_signal_connect( GTK_OBJECT(list_item),
543 "button_press_event",
544 (GtkSignalFunc)gtk_listbox_button_press_callback,
545 (gpointer) this );
546
547 gtk_signal_connect_after( GTK_OBJECT(list_item),
548 "button_release_event",
549 (GtkSignalFunc)gtk_listbox_button_release_callback,
550 (gpointer) this );
551
552 gtk_signal_connect( GTK_OBJECT(list_item),
553 "key_press_event",
554 (GtkSignalFunc)gtk_listbox_key_press_callback,
555 (gpointer)this );
556
557 ConnectWidget( list_item );
558
559 gtk_widget_show( list_item );
560
561 if (GTK_WIDGET_REALIZED(m_widget))
562 {
563 gtk_widget_realize( list_item );
564 gtk_widget_realize( GTK_BIN(list_item)->child );
565
566 // Apply current widget style to the new list_item
567 if (m_widgetStyle)
568 {
569 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
570 GtkBin *bin = GTK_BIN( list_item );
571 GtkWidget *label = GTK_WIDGET( bin->child );
572 gtk_widget_set_style( label, m_widgetStyle );
573 }
574
575 #if wxUSE_TOOLTIPS
576 if (m_tooltip) m_tooltip->Apply( this );
577 #endif
578 }
579 }
580
581 void wxListBox::DoSetItems( const wxArrayString& items,
582 void **clientData)
583 {
584 Clear();
585
586 DoInsertItems(items, 0);
587
588 if ( clientData )
589 {
590 size_t count = items.GetCount();
591 for ( size_t n = 0; n < count; n++ )
592 {
593 SetClientData(n, clientData[n]);
594 }
595 }
596 }
597
598 // ----------------------------------------------------------------------------
599 // client data
600 // ----------------------------------------------------------------------------
601
602 void wxListBox::DoSetItemClientData( int n, void* clientData )
603 {
604 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
605
606 wxNode *node = m_clientList.Nth( n );
607 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
608
609 node->SetData( (wxObject*) clientData );
610 }
611
612 void* wxListBox::DoGetItemClientData( int n ) const
613 {
614 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
615
616 wxNode *node = m_clientList.Nth( n );
617 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
618
619 return node->Data();
620 }
621
622 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
623 {
624 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
625
626 wxNode *node = m_clientList.Nth( n );
627 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
628
629 wxClientData *cd = (wxClientData*) node->Data();
630 delete cd;
631
632 node->SetData( (wxObject*) clientData );
633 }
634
635 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
636 {
637 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
638
639 wxNode *node = m_clientList.Nth( n );
640 wxCHECK_MSG( node, (wxClientData *)NULL,
641 wxT("invalid index in wxListBox::DoGetItemClientObject") );
642
643 return (wxClientData*) node->Data();
644 }
645
646 void wxListBox::Clear()
647 {
648 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
649
650 gtk_list_clear_items( m_list, 0, Number() );
651
652 if ( HasClientObjectData() )
653 {
654 // destroy the data (due to Robert's idea of using wxList<wxObject>
655 // and not wxList<wxClientData> we can't just say
656 // m_clientList.DeleteContents(TRUE) - this would crash!
657 wxNode *node = m_clientList.First();
658 while ( node )
659 {
660 delete (wxClientData *)node->Data();
661 node = node->Next();
662 }
663 }
664 m_clientList.Clear();
665
666 if ( m_strings )
667 m_strings->Clear();
668 }
669
670 void wxListBox::Delete( int n )
671 {
672 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
673
674 GList *child = g_list_nth( m_list->children, n );
675
676 wxCHECK_RET( child, wxT("wrong listbox index") );
677
678 GList *list = g_list_append( (GList*) NULL, child->data );
679 gtk_list_remove_items( m_list, list );
680 g_list_free( list );
681
682 wxNode *node = m_clientList.Nth( n );
683 if ( node )
684 {
685 if ( m_clientDataItemsType == ClientData_Object )
686 {
687 wxClientData *cd = (wxClientData*)node->Data();
688 delete cd;
689 }
690
691 m_clientList.DeleteNode( node );
692 }
693
694 if ( m_strings )
695 m_strings->Remove(n);
696 }
697
698 // ----------------------------------------------------------------------------
699 // string list access
700 // ----------------------------------------------------------------------------
701
702 void wxListBox::SetString( int n, const wxString &string )
703 {
704 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
705
706 GList *child = g_list_nth( m_list->children, n );
707 if (child)
708 {
709 GtkBin *bin = GTK_BIN( child->data );
710 GtkLabel *label = GTK_LABEL( bin->child );
711
712 wxString str;
713 #if wxUSE_CHECKLISTBOX
714 if (m_hasCheckBoxes)
715 str += wxCHECKLBOX_STRING;
716 #endif // wxUSE_CHECKLISTBOX
717 str += string;
718
719 gtk_label_set( label, str.mbc_str() );
720 }
721 else
722 {
723 wxFAIL_MSG(wxT("wrong listbox index"));
724 }
725 }
726
727 wxString wxListBox::GetString( int n ) const
728 {
729 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
730
731 GList *child = g_list_nth( m_list->children, n );
732 if (child)
733 {
734 GtkBin *bin = GTK_BIN( child->data );
735 GtkLabel *label = GTK_LABEL( bin->child );
736
737 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
738
739 return str;
740 }
741
742 wxFAIL_MSG(wxT("wrong listbox index"));
743
744 return wxT("");
745 }
746
747 int wxListBox::GetCount() const
748 {
749 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
750
751 GList *children = m_list->children;
752 return g_list_length(children);
753 }
754
755 int wxListBox::FindString( const wxString &item ) const
756 {
757 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
758
759 GList *child = m_list->children;
760 int count = 0;
761 while (child)
762 {
763 GtkBin *bin = GTK_BIN( child->data );
764 GtkLabel *label = GTK_LABEL( bin->child );
765
766 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
767
768 if (str == item)
769 return count;
770
771 count++;
772 child = child->next;
773 }
774
775 // it's not an error if the string is not found -> no wxCHECK
776
777 return wxNOT_FOUND;
778 }
779
780 // ----------------------------------------------------------------------------
781 // selection
782 // ----------------------------------------------------------------------------
783
784 int wxListBox::GetSelection() const
785 {
786 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
787
788 GList *child = m_list->children;
789 int count = 0;
790 while (child)
791 {
792 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
793 count++;
794 child = child->next;
795 }
796 return -1;
797 }
798
799 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
800 {
801 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
802
803 // get the number of selected items first
804 GList *child = m_list->children;
805 int count = 0;
806 for (child = m_list->children; child != NULL; child = child->next)
807 {
808 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
809 count++;
810 }
811
812 aSelections.Empty();
813
814 if (count > 0)
815 {
816 // now fill the list
817 aSelections.Alloc(count); // optimization attempt
818 int i = 0;
819 for (child = m_list->children; child != NULL; child = child->next, i++)
820 {
821 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
822 aSelections.Add(i);
823 }
824 }
825
826 return count;
827 }
828
829 bool wxListBox::IsSelected( int n ) const
830 {
831 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
832
833 GList *target = g_list_nth( m_list->children, n );
834
835 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
836
837 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
838 }
839
840 void wxListBox::SetSelection( int n, bool select )
841 {
842 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
843
844 GtkDisableEvents();
845
846 if (select)
847 gtk_list_select_item( m_list, n );
848 else
849 gtk_list_unselect_item( m_list, n );
850
851 GtkEnableEvents();
852 }
853
854 void wxListBox::DoSetFirstItem( int n )
855 {
856 wxCHECK_RET( m_list, wxT("invalid listbox") );
857
858 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
859 return;
860
861 // terribly efficient
862 const gchar *vadjustment_key = "gtk-vadjustment";
863 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
864
865 GtkAdjustment *adjustment =
866 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
867 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
868
869 GList *target = g_list_nth( m_list->children, n );
870 wxCHECK_RET( target, wxT("invalid listbox index") );
871
872 GtkWidget *item = GTK_WIDGET(target->data);
873 wxCHECK_RET( item, wxT("invalid listbox code") );
874
875 if (item->allocation.y == -1)
876 {
877 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
878 data->m_listbox = this;
879 data->m_item = n;
880 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
881
882 return;
883 }
884
885 float y = item->allocation.y;
886 if (y > adjustment->upper - adjustment->page_size)
887 y = adjustment->upper - adjustment->page_size;
888 gtk_adjustment_set_value( adjustment, y );
889 }
890
891 // ----------------------------------------------------------------------------
892 // helpers
893 // ----------------------------------------------------------------------------
894
895 int wxListBox::GtkGetIndex( GtkWidget *item ) const
896 {
897 if (item)
898 {
899 GList *child = m_list->children;
900 int count = 0;
901 while (child)
902 {
903 if (GTK_WIDGET(child->data) == item) return count;
904 count++;
905 child = child->next;
906 }
907 }
908 return -1;
909 }
910
911 #if wxUSE_TOOLTIPS
912 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
913 {
914 GList *child = m_list->children;
915 while (child)
916 {
917 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
918 child = child->next;
919 }
920 }
921 #endif // wxUSE_TOOLTIPS
922
923 void wxListBox::GtkDisableEvents()
924 {
925 GList *child = m_list->children;
926 while (child)
927 {
928 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
929 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
930
931 if (HasFlag(wxLB_MULTIPLE))
932 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
933 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
934
935 child = child->next;
936 }
937 }
938
939 void wxListBox::GtkEnableEvents()
940 {
941 GList *child = m_list->children;
942 while (child)
943 {
944 gtk_signal_connect( GTK_OBJECT(child->data), "select",
945 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
946
947 if (HasFlag(wxLB_MULTIPLE))
948 gtk_signal_connect( GTK_OBJECT(child->data), "deselect",
949 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
950
951 child = child->next;
952 }
953 }
954
955 GtkWidget *wxListBox::GetConnectWidget()
956 {
957 return GTK_WIDGET(m_list);
958 }
959
960 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
961 {
962 if (GTK_WIDGET(m_list)->window == window) return TRUE;
963
964 GList *child = m_list->children;
965 while (child)
966 {
967 GtkWidget *bin = GTK_WIDGET( child->data );
968 if (bin->window == window) return TRUE;
969 child = child->next;
970 }
971
972 return FALSE;
973 }
974
975 void wxListBox::ApplyWidgetStyle()
976 {
977 SetWidgetStyle();
978
979 if (m_backgroundColour.Ok())
980 {
981 GdkWindow *window = GTK_WIDGET(m_list)->window;
982 if ( window )
983 {
984 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
985 gdk_window_set_background( window, m_backgroundColour.GetColor() );
986 gdk_window_clear( window );
987 }
988 }
989
990 GList *child = m_list->children;
991 while (child)
992 {
993 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
994
995 GtkBin *bin = GTK_BIN( child->data );
996 GtkWidget *label = GTK_WIDGET( bin->child );
997 gtk_widget_set_style( label, m_widgetStyle );
998
999 child = child->next;
1000 }
1001 }
1002
1003 void wxListBox::OnInternalIdle()
1004 {
1005 wxCursor cursor = m_cursor;
1006 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1007
1008 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1009 {
1010 /* I now set the cursor the anew in every OnInternalIdle call
1011 as setting the cursor in a parent window also effects the
1012 windows above so that checking for the current cursor is
1013 not possible. */
1014
1015 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1016
1017 GList *child = m_list->children;
1018 while (child)
1019 {
1020 GtkBin *bin = GTK_BIN( child->data );
1021 GtkWidget *label = GTK_WIDGET( bin->child );
1022
1023 if (!label->window)
1024 break;
1025 else
1026 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1027
1028 child = child->next;
1029 }
1030 }
1031
1032 UpdateWindowUI();
1033 }
1034
1035 wxSize wxListBox::DoGetBestSize() const
1036 {
1037 return wxSize(100, 110);
1038 }
1039
1040 #endif