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