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