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