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