]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/listbox.cpp
added dummy <style> handler in order to hide CSS code
[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 extern "C" 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 *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 if (listbox->m_blockEvent) return;
272
273 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
274 event.SetEventObject( listbox );
275
276 // MSW doesn't do that either
277 // event.SetExtraLong( (long) is_selection );
278
279
280 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
281 {
282 int sel = listbox->GtkGetIndex( widget );
283
284 if (listbox->m_prevSelection != sel)
285 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
286
287 listbox->m_prevSelection = sel;
288 }
289
290 wxArrayInt aSelections;
291 int n, count = listbox->GetSelections(aSelections);
292 if ( count > 0 )
293 {
294 n = aSelections[0];
295 if ( listbox->HasClientObjectData() )
296 event.SetClientObject( listbox->GetClientObject(n) );
297 else if ( listbox->HasClientUntypedData() )
298 event.SetClientData( listbox->GetClientData(n) );
299 event.SetString( listbox->GetString(n) );
300 }
301 else
302 {
303 n = -1;
304 }
305
306 event.m_commandInt = n;
307
308 // No longer required with new code in wxLB_SINGLE
309 // listbox->GetEventHandler()->AddPendingEvent( event );
310 listbox->GetEventHandler()->ProcessEvent( event );
311 }
312
313 //-----------------------------------------------------------------------------
314 // wxListBox
315 //-----------------------------------------------------------------------------
316
317 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
318
319 // ----------------------------------------------------------------------------
320 // construction
321 // ----------------------------------------------------------------------------
322
323 wxListBox::wxListBox()
324 {
325 m_list = (GtkList *) NULL;
326 #if wxUSE_CHECKLISTBOX
327 m_hasCheckBoxes = FALSE;
328 #endif // wxUSE_CHECKLISTBOX
329 }
330
331 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
332 const wxPoint &pos, const wxSize &size,
333 int n, const wxString choices[],
334 long style, const wxValidator& validator,
335 const wxString &name )
336 {
337 m_needParent = TRUE;
338 m_acceptsFocus = TRUE;
339 m_isListBox = TRUE;
340 m_prevSelection = 0; // or -1 ??
341 m_blockEvent = FALSE;
342
343 if (!PreCreation( parent, pos, size ) ||
344 !CreateBase( parent, id, pos, size, style, validator, name ))
345 {
346 wxFAIL_MSG( wxT("wxListBox creation failed") );
347 return FALSE;
348 }
349
350 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
351 if (style & wxLB_ALWAYS_SB)
352 {
353 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
354 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
355 }
356 else
357 {
358 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
359 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
360 }
361
362 m_list = GTK_LIST( gtk_list_new() );
363
364 GtkSelectionMode mode;
365 if (style & wxLB_MULTIPLE)
366 {
367 mode = GTK_SELECTION_MULTIPLE;
368 }
369 else if (style & wxLB_EXTENDED)
370 {
371 mode = GTK_SELECTION_EXTENDED;
372 }
373 else
374 {
375 // if style was 0 set single mode
376 m_windowStyle |= wxLB_SINGLE;
377 mode = GTK_SELECTION_MULTIPLE;
378 }
379
380 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
381
382 #ifdef NEW_GTK_SCROLL_CODE
383 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
384 #else
385 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
386 #endif
387
388 /* make list scroll when moving the focus down using cursor keys */
389 gtk_container_set_focus_vadjustment(
390 GTK_CONTAINER(m_list),
391 gtk_scrolled_window_get_vadjustment(
392 GTK_SCROLLED_WINDOW(m_widget)));
393
394 gtk_widget_show( GTK_WIDGET(m_list) );
395
396 if ( style & wxLB_SORT )
397 {
398 // this will change DoAppend() behaviour
399 m_strings = new wxSortedArrayString;
400 }
401 else
402 {
403 m_strings = (wxSortedArrayString *)NULL;
404 }
405
406 for (int i = 0; i < n; i++)
407 {
408 // add one by one
409 DoAppend(choices[i]);
410 }
411
412 // call it after appending the strings to the listbox, otherwise it doesn't
413 // work correctly
414 SetBestSize( size );
415
416 m_parent->DoAddChild( this );
417
418 PostCreation();
419
420 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ) );
421 SetForegroundColour( parent->GetForegroundColour() );
422 SetFont( parent->GetFont() );
423
424 Show( TRUE );
425
426 return TRUE;
427 }
428
429 wxListBox::~wxListBox()
430 {
431 m_hasVMT = FALSE;
432
433 Clear();
434
435 if (m_strings)
436 delete m_strings;
437 }
438
439 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
440 {
441 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
442
443 // VZ: notice that InsertItems knows nothing about sorting, so calling it
444 // from outside (and not from our own Append) is likely to break
445 // everything
446
447 // code elsewhere supposes we have as many items in m_clientList as items
448 // in the listbox
449 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
450 wxT("bug in client data management") );
451
452 GList *children = m_list->children;
453 int length = g_list_length(children);
454
455 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
456
457 size_t nItems = items.GetCount();
458 int index;
459
460 if (m_strings)
461 {
462 for (size_t n = 0; n < nItems; n++)
463 {
464 index = m_strings->Add( items[n] );
465
466 if (index != GetCount())
467 {
468 GtkAddItem( items[n], index );
469 wxNode *node = m_clientList.Nth( index );
470 m_clientList.Insert( node, (wxObject*) NULL );
471 }
472 else
473 {
474 GtkAddItem( items[n] );
475 m_clientList.Append( (wxObject*) NULL );
476 }
477 }
478 }
479 else
480 {
481 if (pos == length)
482 {
483 for ( size_t n = 0; n < nItems; n++ )
484 {
485 GtkAddItem( items[n] );
486
487 m_clientList.Append((wxObject *)NULL);
488 }
489 }
490 else
491 {
492 wxNode *node = m_clientList.Nth( pos );
493 for ( size_t n = 0; n < nItems; n++ )
494 {
495 GtkAddItem( items[n], pos+n );
496
497 m_clientList.Insert( node, (wxObject *)NULL );
498 }
499 }
500 }
501
502 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
503 wxT("bug in client data management") );
504 }
505
506 int wxListBox::DoAppend( const wxString& item )
507 {
508 if (m_strings)
509 {
510 // need to determine the index
511 int index = m_strings->Add( item );
512
513 // only if not at the end anyway
514 if (index != GetCount())
515 {
516 GtkAddItem( item, index );
517
518 wxNode *node = m_clientList.Nth( index );
519 m_clientList.Insert( node, (wxObject *)NULL );
520
521 return index;
522 }
523 }
524
525 GtkAddItem(item);
526
527 m_clientList.Append((wxObject *)NULL);
528
529 return GetCount() - 1;
530 }
531
532 void wxListBox::GtkAddItem( const wxString &item, int pos )
533 {
534 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
535
536 GtkWidget *list_item;
537
538 wxString label(item);
539 #if wxUSE_CHECKLISTBOX
540 if (m_hasCheckBoxes)
541 {
542 label.Prepend(wxCHECKLBOX_STRING);
543 }
544 #endif // wxUSE_CHECKLISTBOX
545
546 list_item = gtk_list_item_new_with_label( label.mbc_str() );
547
548 GList *gitem_list = g_list_alloc ();
549 gitem_list->data = list_item;
550
551 if (pos == -1)
552 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
553 else
554 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
555
556 gtk_signal_connect( GTK_OBJECT(list_item), "select",
557 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
558
559 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
560 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
561 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
562
563 gtk_signal_connect( GTK_OBJECT(list_item),
564 "button_press_event",
565 (GtkSignalFunc)gtk_listbox_button_press_callback,
566 (gpointer) this );
567
568 gtk_signal_connect_after( GTK_OBJECT(list_item),
569 "button_release_event",
570 (GtkSignalFunc)gtk_listbox_button_release_callback,
571 (gpointer) this );
572
573 gtk_signal_connect( GTK_OBJECT(list_item),
574 "key_press_event",
575 (GtkSignalFunc)gtk_listbox_key_press_callback,
576 (gpointer)this );
577
578 ConnectWidget( list_item );
579
580 gtk_widget_show( list_item );
581
582 if (GTK_WIDGET_REALIZED(m_widget))
583 {
584 gtk_widget_realize( list_item );
585 gtk_widget_realize( GTK_BIN(list_item)->child );
586
587 // Apply current widget style to the new list_item
588 if (m_widgetStyle)
589 {
590 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
591 GtkBin *bin = GTK_BIN( list_item );
592 GtkWidget *label = GTK_WIDGET( bin->child );
593 gtk_widget_set_style( label, m_widgetStyle );
594 }
595
596 #if wxUSE_TOOLTIPS
597 if (m_tooltip) m_tooltip->Apply( this );
598 #endif
599 }
600 }
601
602 void wxListBox::DoSetItems( const wxArrayString& items,
603 void **clientData)
604 {
605 Clear();
606
607 DoInsertItems(items, 0);
608
609 if ( clientData )
610 {
611 size_t count = items.GetCount();
612 for ( size_t n = 0; n < count; n++ )
613 {
614 SetClientData(n, clientData[n]);
615 }
616 }
617 }
618
619 // ----------------------------------------------------------------------------
620 // client data
621 // ----------------------------------------------------------------------------
622
623 void wxListBox::DoSetItemClientData( int n, void* clientData )
624 {
625 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
626
627 wxNode *node = m_clientList.Nth( n );
628 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
629
630 node->SetData( (wxObject*) clientData );
631 }
632
633 void* wxListBox::DoGetItemClientData( int n ) const
634 {
635 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
636
637 wxNode *node = m_clientList.Nth( n );
638 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
639
640 return node->Data();
641 }
642
643 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
644 {
645 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
646
647 wxNode *node = m_clientList.Nth( n );
648 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
649
650 wxClientData *cd = (wxClientData*) node->Data();
651 delete cd;
652
653 node->SetData( (wxObject*) clientData );
654 }
655
656 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
657 {
658 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
659
660 wxNode *node = m_clientList.Nth( n );
661 wxCHECK_MSG( node, (wxClientData *)NULL,
662 wxT("invalid index in wxListBox::DoGetItemClientObject") );
663
664 return (wxClientData*) node->Data();
665 }
666
667 void wxListBox::Clear()
668 {
669 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
670
671 gtk_list_clear_items( m_list, 0, GetCount() );
672
673 if ( GTK_LIST(m_list)->last_focus_child != NULL )
674 {
675 // This should be NULL, I think.
676 GTK_LIST(m_list)->last_focus_child = NULL;
677 }
678
679 if ( HasClientObjectData() )
680 {
681 // destroy the data (due to Robert's idea of using wxList<wxObject>
682 // and not wxList<wxClientData> we can't just say
683 // m_clientList.DeleteContents(TRUE) - this would crash!
684 wxNode *node = m_clientList.First();
685 while ( node )
686 {
687 delete (wxClientData *)node->Data();
688 node = node->Next();
689 }
690 }
691 m_clientList.Clear();
692
693 if ( m_strings )
694 m_strings->Clear();
695 }
696
697 void wxListBox::Delete( int n )
698 {
699 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
700
701 GList *child = g_list_nth( m_list->children, n );
702
703 wxCHECK_RET( child, wxT("wrong listbox index") );
704
705 GList *list = g_list_append( (GList*) NULL, child->data );
706 gtk_list_remove_items( m_list, list );
707 g_list_free( list );
708
709 wxNode *node = m_clientList.Nth( n );
710 if ( node )
711 {
712 if ( m_clientDataItemsType == wxClientData_Object )
713 {
714 wxClientData *cd = (wxClientData*)node->Data();
715 delete cd;
716 }
717
718 m_clientList.DeleteNode( node );
719 }
720
721 if ( m_strings )
722 m_strings->Remove(n);
723 }
724
725 // ----------------------------------------------------------------------------
726 // string list access
727 // ----------------------------------------------------------------------------
728
729 void wxListBox::SetString( int n, const wxString &string )
730 {
731 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
732
733 GList *child = g_list_nth( m_list->children, n );
734 if (child)
735 {
736 GtkBin *bin = GTK_BIN( child->data );
737 GtkLabel *label = GTK_LABEL( bin->child );
738
739 wxString str;
740 #if wxUSE_CHECKLISTBOX
741 if (m_hasCheckBoxes)
742 str += wxCHECKLBOX_STRING;
743 #endif // wxUSE_CHECKLISTBOX
744 str += string;
745
746 gtk_label_set( label, str.mbc_str() );
747 }
748 else
749 {
750 wxFAIL_MSG(wxT("wrong listbox index"));
751 }
752 }
753
754 wxString wxListBox::GetString( int n ) const
755 {
756 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
757
758 GList *child = g_list_nth( m_list->children, n );
759 if (child)
760 {
761 GtkBin *bin = GTK_BIN( child->data );
762 GtkLabel *label = GTK_LABEL( bin->child );
763
764 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
765
766 return str;
767 }
768
769 wxFAIL_MSG(wxT("wrong listbox index"));
770
771 return wxT("");
772 }
773
774 int wxListBox::GetCount() const
775 {
776 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
777
778 GList *children = m_list->children;
779 return g_list_length(children);
780 }
781
782 int wxListBox::FindString( const wxString &item ) const
783 {
784 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
785
786 GList *child = m_list->children;
787 int count = 0;
788 while (child)
789 {
790 GtkBin *bin = GTK_BIN( child->data );
791 GtkLabel *label = GTK_LABEL( bin->child );
792
793 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
794
795 if (str == item)
796 return count;
797
798 count++;
799 child = child->next;
800 }
801
802 // it's not an error if the string is not found -> no wxCHECK
803
804 return wxNOT_FOUND;
805 }
806
807 // ----------------------------------------------------------------------------
808 // selection
809 // ----------------------------------------------------------------------------
810
811 int wxListBox::GetSelection() const
812 {
813 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
814
815 GList *child = m_list->children;
816 int count = 0;
817 while (child)
818 {
819 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
820 count++;
821 child = child->next;
822 }
823 return -1;
824 }
825
826 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
827 {
828 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
829
830 // get the number of selected items first
831 GList *child = m_list->children;
832 int count = 0;
833 for (child = m_list->children; child != NULL; child = child->next)
834 {
835 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
836 count++;
837 }
838
839 aSelections.Empty();
840
841 if (count > 0)
842 {
843 // now fill the list
844 aSelections.Alloc(count); // optimization attempt
845 int i = 0;
846 for (child = m_list->children; child != NULL; child = child->next, i++)
847 {
848 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
849 aSelections.Add(i);
850 }
851 }
852
853 return count;
854 }
855
856 bool wxListBox::IsSelected( int n ) const
857 {
858 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
859
860 GList *target = g_list_nth( m_list->children, n );
861
862 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
863
864 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
865 }
866
867 void wxListBox::SetSelection( int n, bool select )
868 {
869 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
870
871 m_blockEvent = TRUE;
872
873 if (select)
874 {
875 if ((m_windowStyle & wxLB_SINGLE) != 0)
876 gtk_list_unselect_item( m_list, m_prevSelection );
877 gtk_list_select_item( m_list, n );
878 m_prevSelection = n;
879 }
880 else
881 gtk_list_unselect_item( m_list, n );
882
883 m_blockEvent = FALSE;
884 }
885
886 void wxListBox::DoSetFirstItem( int n )
887 {
888 wxCHECK_RET( m_list, wxT("invalid listbox") );
889
890 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
891 return;
892
893 // terribly efficient
894 const gchar *vadjustment_key = "gtk-vadjustment";
895 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
896
897 GtkAdjustment *adjustment =
898 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
899 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
900
901 GList *target = g_list_nth( m_list->children, n );
902 wxCHECK_RET( target, wxT("invalid listbox index") );
903
904 GtkWidget *item = GTK_WIDGET(target->data);
905 wxCHECK_RET( item, wxT("invalid listbox code") );
906
907 if (item->allocation.y == -1)
908 {
909 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
910 data->m_listbox = this;
911 data->m_item = n;
912 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
913
914 return;
915 }
916
917 float y = item->allocation.y;
918 if (y > adjustment->upper - adjustment->page_size)
919 y = adjustment->upper - adjustment->page_size;
920 gtk_adjustment_set_value( adjustment, y );
921 }
922
923 // ----------------------------------------------------------------------------
924 // helpers
925 // ----------------------------------------------------------------------------
926
927 int wxListBox::GtkGetIndex( GtkWidget *item ) const
928 {
929 if (item)
930 {
931 GList *child = m_list->children;
932 int count = 0;
933 while (child)
934 {
935 if (GTK_WIDGET(child->data) == item) return count;
936 count++;
937 child = child->next;
938 }
939 }
940 return -1;
941 }
942
943 #if wxUSE_TOOLTIPS
944 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
945 {
946 GList *child = m_list->children;
947 while (child)
948 {
949 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
950 child = child->next;
951 }
952 }
953 #endif // wxUSE_TOOLTIPS
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 int lbWidth = 100; // some defaults
1038 int lbHeight = 110;
1039 int wLine;
1040
1041 // Find the widest line
1042 for(int i = 0; i < GetCount(); i++) {
1043 wxString str(GetString(i));
1044 GetTextExtent(str, &wLine, NULL);
1045 lbWidth = wxMax(lbWidth, wLine);
1046 }
1047
1048 // Add room for the scrollbar
1049 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1050
1051 // And just a bit more
1052 int cx, cy;
1053 GetTextExtent("X", &cx, &cy);
1054 lbWidth += 3 * cx;
1055
1056 // don't make the listbox too tall (limit height to around 10 items) but don't
1057 // make it too small neither
1058 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1059
1060 return wxSize(lbWidth, lbHeight);
1061 }
1062
1063 #endif