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