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