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