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