]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/listbox.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / src / gtk1 / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if wxUSE_LISTBOX
13
14 #include "wx/listbox.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/dynarray.h"
18 #include "wx/intl.h"
19 #include "wx/utils.h"
20 #include "wx/settings.h"
21 #include "wx/checklst.h"
22 #include "wx/arrstr.h"
23 #endif
24
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 *WXUNUSED(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->HandleWindowEvent(eventChildFocus);
113
114 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
115 eventFocus.SetEventObject(win);
116
117 (void)win->HandleWindowEvent(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 *WXUNUSED(widget),
130 GdkEventFocus *WXUNUSED(gdk_event),
131 wxWindowGTK *win )
132 {
133 if (g_isIdle)
134 wxapp_install_idle_handler();
135
136 g_focusWindow = NULL;
137
138 // don't send the window a kill focus event if it thinks that it doesn't
139 // have focus already
140 if ( win->m_hasFocus )
141 {
142 win->m_hasFocus = false;
143
144 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
145 event.SetEventObject( win );
146
147 // even if we did process the event in wx code, still let GTK itself
148 // process it too as otherwise bad things happen, especially in GTK2
149 // where the text control simply aborts the program if it doesn't get
150 // the matching focus out event
151 (void)win->HandleWindowEvent( event );
152 }
153
154 return FALSE;
155 }
156 }
157
158 //-----------------------------------------------------------------------------
159 // "button_release_event"
160 //-----------------------------------------------------------------------------
161
162 /* we would normally emit a wxEVT_LISTBOX_DCLICK event once
163 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
164 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
165 this can lead to race conditions so that we emit the dclick event
166 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
167
168 extern "C" {
169 static gint
170 gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
171 GdkEventButton * WXUNUSED(gdk_event),
172 wxListBox *listbox )
173 {
174 if (g_isIdle) wxapp_install_idle_handler();
175
176 if (g_blockEventsOnDrag) return FALSE;
177 if (g_blockEventsOnScroll) return FALSE;
178
179 if (!listbox->m_hasVMT) return FALSE;
180
181 if (!g_hasDoubleClicked) return FALSE;
182
183 wxCommandEvent event( wxEVT_LISTBOX_DCLICK, listbox->GetId() );
184 event.SetEventObject( listbox );
185
186 wxArrayInt aSelections;
187 int n, count = listbox->GetSelections(aSelections);
188 if ( count > 0 )
189 {
190 n = aSelections[0];
191 if ( listbox->HasClientObjectData() )
192 event.SetClientObject( listbox->GetClientObject(n) );
193 else if ( listbox->HasClientUntypedData() )
194 event.SetClientData( listbox->GetClientData(n) );
195 event.SetString( listbox->GetString(n) );
196 }
197 else
198 {
199 n = -1;
200 }
201
202 event.SetInt(n);
203
204 listbox->HandleWindowEvent( event );
205
206 return FALSE;
207 }
208 }
209
210 //-----------------------------------------------------------------------------
211 // "button_press_event"
212 //-----------------------------------------------------------------------------
213
214 extern "C" {
215 static gint
216 gtk_listbox_button_press_callback( GtkWidget *widget,
217 GdkEventButton *gdk_event,
218 wxListBox *listbox )
219 {
220 if (g_isIdle) wxapp_install_idle_handler();
221
222 if (g_blockEventsOnDrag) return FALSE;
223 if (g_blockEventsOnScroll) return FALSE;
224
225 if (!listbox->m_hasVMT) return FALSE;
226
227 int sel = listbox->GtkGetIndex( widget );
228
229 #if wxUSE_CHECKLISTBOX
230 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
231 {
232 wxCheckListBox *clb = (wxCheckListBox *)listbox;
233
234 clb->Check( sel, !clb->IsChecked(sel) );
235
236 wxCommandEvent event( wxEVT_CHECKLISTBOX, listbox->GetId() );
237 event.SetEventObject( listbox );
238 event.SetInt( sel );
239 listbox->HandleWindowEvent( event );
240 }
241 #endif // wxUSE_CHECKLISTBOX
242
243 if ((gdk_event->state == 0) &&
244 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
245 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
246 {
247 listbox->m_blockEvent = true;
248
249 int i;
250 for (i = 0; i < (int)listbox->GetCount(); i++)
251 if (i != sel)
252 gtk_list_unselect_item( GTK_LIST(listbox->m_list), i );
253
254 listbox->m_blockEvent = false;
255
256 return false;
257 }
258
259 /* emit wxEVT_LISTBOX_DCLICK later */
260 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
261
262 return FALSE;
263 }
264 }
265
266 //-----------------------------------------------------------------------------
267 // "key_press_event"
268 //-----------------------------------------------------------------------------
269
270 extern "C" {
271 static gint
272 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
273 {
274 if (g_isIdle)
275 wxapp_install_idle_handler();
276
277 if (g_blockEventsOnDrag)
278 return FALSE;
279
280 bool ret = false;
281
282 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
283 {
284 wxNavigationKeyEvent new_event;
285 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
286 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
287 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
288 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
289 new_event.SetCurrentFocus( listbox );
290 ret = listbox->HandleWindowEvent( new_event );
291 }
292
293 if ((gdk_event->keyval == GDK_Return) && (!ret))
294 {
295 // eat return in all modes
296 ret = true;
297 }
298
299 #if wxUSE_CHECKLISTBOX
300 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
301 {
302 int sel = listbox->GtkGetIndex( widget );
303
304 wxCheckListBox *clb = (wxCheckListBox *)listbox;
305
306 clb->Check( sel, !clb->IsChecked(sel) );
307
308 wxCommandEvent new_event( wxEVT_CHECKLISTBOX, listbox->GetId() );
309 new_event.SetEventObject( listbox );
310 new_event.SetInt( sel );
311 ret = listbox->HandleWindowEvent( new_event );
312 }
313 #endif // wxUSE_CHECKLISTBOX
314
315 // Check or uncheck item with SPACE
316 if ((gdk_event->keyval == ' ') && (!ret) &&
317 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
318 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
319 {
320 int sel = listbox->GtkGetIndex( widget );
321
322 if (sel != -1)
323 {
324 ret = true;
325
326 if (listbox->IsSelected( sel ))
327 gtk_list_unselect_item( listbox->m_list, sel );
328 else
329 gtk_list_select_item( listbox->m_list, sel );
330
331 wxCommandEvent new_event(wxEVT_LISTBOX, listbox->GetId() );
332 new_event.SetEventObject( listbox );
333 wxArrayInt aSelections;
334 int n, count = listbox->GetSelections(aSelections);
335 if ( count > 0 )
336 {
337 n = aSelections[0];
338 if ( listbox->HasClientObjectData() )
339 new_event.SetClientObject( listbox->GetClientObject(n) );
340 else if ( listbox->HasClientUntypedData() )
341 new_event.SetClientData( listbox->GetClientData(n) );
342 new_event.SetString( listbox->GetString(n) );
343 }
344 else
345 {
346 n = -1;
347 }
348 new_event.SetInt(n);
349 listbox->HandleWindowEvent( new_event );
350 }
351 }
352
353 if (ret)
354 {
355 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
356 return TRUE;
357 }
358
359 return FALSE;
360 }
361 }
362
363 //-----------------------------------------------------------------------------
364 // "select" and "deselect"
365 //-----------------------------------------------------------------------------
366
367 static void gtk_listitem_select_cb( GtkWidget *widget,
368 wxListBox *listbox,
369 bool is_selection )
370 {
371 if (g_isIdle) wxapp_install_idle_handler();
372
373 if (!listbox->m_hasVMT) return;
374 if (g_blockEventsOnDrag) return;
375
376 if (listbox->m_blockEvent) return;
377
378 wxCommandEvent event(wxEVT_LISTBOX, listbox->GetId() );
379 event.SetEventObject( listbox );
380
381 // indicate whether this is a selection or a deselection
382 event.SetExtraLong( is_selection );
383
384 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
385 {
386 int sel = listbox->GtkGetIndex( widget );
387
388 if (listbox->m_prevSelection != sel)
389 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
390
391 listbox->m_prevSelection = sel;
392 }
393
394 wxArrayInt aSelections;
395 int n, count = listbox->GetSelections(aSelections);
396 if ( count > 0 )
397 {
398 n = aSelections[0];
399 if ( listbox->HasClientObjectData() )
400 event.SetClientObject( listbox->GetClientObject(n) );
401 else if ( listbox->HasClientUntypedData() )
402 event.SetClientData( listbox->GetClientData(n) );
403 event.SetString( listbox->GetString(n) );
404 }
405 else
406 {
407 n = -1;
408 }
409
410 event.SetInt(n);
411
412 // No longer required with new code in wxLB_SINGLE
413 // listbox->GetEventHandler()->AddPendingEvent( event );
414 listbox->HandleWindowEvent( event );
415 }
416
417 extern "C" {
418 static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
419 {
420 gtk_listitem_select_cb( widget, listbox, TRUE );
421 }
422 }
423
424 extern "C" {
425 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
426 {
427 gtk_listitem_select_cb( widget, listbox, FALSE );
428 }
429 }
430
431 //-----------------------------------------------------------------------------
432 // wxListBox
433 //-----------------------------------------------------------------------------
434
435 extern "C" {
436 static gint
437 gtk_listbox_realized_callback( GtkWidget *WXUNUSED(widget), wxListBox *win )
438 {
439 if (g_isIdle)
440 wxapp_install_idle_handler();
441
442 GList *child = win->m_list->children;
443 for (child = win->m_list->children; child != NULL; child = child->next)
444 gtk_widget_show( GTK_WIDGET(child->data) );
445
446 return false;
447 }
448 }
449
450 //-----------------------------------------------------------------------------
451 // wxListBox
452 //-----------------------------------------------------------------------------
453
454 // ----------------------------------------------------------------------------
455 // construction
456 // ----------------------------------------------------------------------------
457
458 wxListBox::wxListBox()
459 {
460 m_list = 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( NULL, 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 Append() behaviour
544 m_strings = new wxSortedArrayString;
545 }
546 else
547 {
548 m_strings = NULL;
549 }
550
551 Append(n, choices);
552
553 m_parent->DoAddChild( this );
554
555 PostCreation(size);
556 SetInitialSize(size); // need this too because this is a wxControlWithItems
557
558 return true;
559 }
560
561 wxListBox::~wxListBox()
562 {
563 m_hasVMT = false;
564
565 Clear();
566
567 delete m_strings;
568 }
569
570 // ----------------------------------------------------------------------------
571 // adding items
572 // ----------------------------------------------------------------------------
573
574 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
575 unsigned int pos,
576 void **clientData,
577 wxClientDataType type)
578 {
579 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
580
581 const unsigned count = GetCount();
582 wxCHECK_MSG( pos <= count, wxNOT_FOUND,
583 wxT("invalid index in wxListBox::InsertItems") );
584
585 // code elsewhere supposes we have as many items in m_clientList as items
586 // in the listbox
587 wxASSERT_MSG( m_clientList.GetCount() == count,
588 wxT("bug in client data management") );
589
590 InvalidateBestSize();
591
592 const unsigned numItems = items.GetCount();
593
594 for ( unsigned int n = 0; n < numItems; ++n, ++pos )
595 {
596 const wxString& item = items[n];
597
598 const unsigned idx = m_strings ? m_strings->Add(item)
599 : pos;
600
601 GtkAddItem(item, idx == GetCount() ? (unsigned) -1 : idx);
602
603 m_clientList.Insert(idx, NULL);
604
605 AssignNewItemClientData(idx, clientData, n, type);
606 }
607
608 wxASSERT_MSG( m_clientList.GetCount() == GetCount(),
609 wxT("bug in client data management") );
610
611 return pos - 1;
612 }
613
614 void wxListBox::GtkAddItem( const wxString &item, int pos )
615 {
616 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
617
618 GtkWidget *list_item;
619
620 wxString label(item);
621 #if wxUSE_CHECKLISTBOX
622 if (m_hasCheckBoxes)
623 {
624 label.Prepend(wxCHECKLBOX_STRING);
625 }
626 #endif // wxUSE_CHECKLISTBOX
627
628 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
629
630 GList *gitem_list = g_list_alloc ();
631 gitem_list->data = list_item;
632
633 if (pos == -1)
634 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
635 else
636 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
637
638 gtk_signal_connect_after( GTK_OBJECT(list_item), "select",
639 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
640
641 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
642 gtk_signal_connect_after( GTK_OBJECT(list_item), "deselect",
643 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
644
645 gtk_signal_connect( GTK_OBJECT(list_item),
646 "button_press_event",
647 (GtkSignalFunc)gtk_listbox_button_press_callback,
648 (gpointer) this );
649
650 gtk_signal_connect_after( GTK_OBJECT(list_item),
651 "button_release_event",
652 (GtkSignalFunc)gtk_listbox_button_release_callback,
653 (gpointer) this );
654
655 gtk_signal_connect( GTK_OBJECT(list_item),
656 "key_press_event",
657 (GtkSignalFunc)gtk_listbox_key_press_callback,
658 (gpointer)this );
659
660
661 gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event",
662 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this );
663
664 gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event",
665 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this );
666
667 ConnectWidget( list_item );
668
669 if (GTK_WIDGET_REALIZED(m_widget))
670 {
671 gtk_widget_show( list_item );
672
673 gtk_widget_realize( list_item );
674 gtk_widget_realize( GTK_BIN(list_item)->child );
675
676 #if wxUSE_TOOLTIPS
677 if (m_tooltip) m_tooltip->Apply( this );
678 #endif
679 }
680
681 // Apply current widget style to the new list_item
682 GtkRcStyle *style = CreateWidgetStyle();
683 if (style)
684 {
685 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
686 GtkBin *bin = GTK_BIN( list_item );
687 gtk_widget_modify_style( GTK_WIDGET( bin->child ), style );
688 gtk_rc_style_unref( style );
689 }
690 }
691
692 // ----------------------------------------------------------------------------
693 // deleting items
694 // ----------------------------------------------------------------------------
695
696 void wxListBox::DoClear()
697 {
698 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
699
700 gtk_list_clear_items( m_list, 0, (int)GetCount() );
701
702 if ( GTK_LIST(m_list)->last_focus_child != NULL )
703 {
704 // This should be NULL, I think.
705 GTK_LIST(m_list)->last_focus_child = NULL;
706 }
707
708 m_clientList.Clear();
709
710 if ( m_strings )
711 m_strings->Clear();
712 }
713
714 void wxListBox::DoDeleteOneItem(unsigned int n)
715 {
716 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
717
718 GList *child = g_list_nth( m_list->children, n );
719
720 wxCHECK_RET( child, wxT("wrong listbox index") );
721
722 GList *list = g_list_append( NULL, child->data );
723 gtk_list_remove_items( m_list, list );
724 g_list_free( list );
725
726 wxList::compatibility_iterator node = m_clientList.Item( n );
727 if ( node )
728 {
729 m_clientList.Erase( node );
730 }
731
732 if ( m_strings )
733 m_strings->RemoveAt(n);
734 }
735
736 // ----------------------------------------------------------------------------
737 // client data
738 // ----------------------------------------------------------------------------
739
740 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
741 {
742 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
743
744 wxList::compatibility_iterator node = m_clientList.Item( n );
745 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
746
747 node->SetData( (wxObject*) clientData );
748 }
749
750 void* wxListBox::DoGetItemClientData(unsigned int n) const
751 {
752 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
753
754 wxList::compatibility_iterator node = m_clientList.Item( n );
755 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
756
757 return node->GetData();
758 }
759
760 // ----------------------------------------------------------------------------
761 // string list access
762 // ----------------------------------------------------------------------------
763
764 wxString wxListBox::GetRealLabel(GList *item) const
765 {
766 GtkBin *bin = GTK_BIN( item->data );
767 GtkLabel *label = GTK_LABEL( bin->child );
768
769 wxString str;
770
771 str = wxString( label->label );
772
773 #if wxUSE_CHECKLISTBOX
774 // checklistboxes have "[±] " prepended to their lables, remove it
775 //
776 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
777 if ( m_hasCheckBoxes )
778 str.erase(0, 4);
779 #endif // wxUSE_CHECKLISTBOX
780
781 return str;
782 }
783
784 void wxListBox::SetString(unsigned int n, const wxString &string)
785 {
786 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
787
788 GList *child = g_list_nth( m_list->children, n );
789 if (child)
790 {
791 GtkBin *bin = GTK_BIN( child->data );
792 GtkLabel *label = GTK_LABEL( bin->child );
793
794 wxString str;
795 #if wxUSE_CHECKLISTBOX
796 if (m_hasCheckBoxes)
797 str += wxCHECKLBOX_STRING;
798 #endif // wxUSE_CHECKLISTBOX
799 str += string;
800
801 gtk_label_set( label, wxGTK_CONV( str ) );
802 }
803 else
804 {
805 wxFAIL_MSG(wxT("wrong listbox index"));
806 }
807 }
808
809 wxString wxListBox::GetString(unsigned int n) const
810 {
811 wxCHECK_MSG( m_list != NULL, wxEmptyString, wxT("invalid listbox") );
812
813 GList *child = g_list_nth( m_list->children, n );
814 if (child)
815 {
816 return GetRealLabel(child);
817 }
818
819 wxFAIL_MSG(wxT("wrong listbox index"));
820
821 return wxEmptyString;
822 }
823
824 unsigned int wxListBox::GetCount() const
825 {
826 wxCHECK_MSG( m_list != NULL, 0, wxT("invalid listbox") );
827
828 GList *children = m_list->children;
829 return g_list_length(children);
830 }
831
832 int wxListBox::FindString( const wxString &item, bool bCase ) const
833 {
834 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
835
836 GList *child = m_list->children;
837 int count = 0;
838 while (child)
839 {
840 if ( item.IsSameAs( GetRealLabel(child), bCase ) )
841 return count;
842
843 count++;
844 child = child->next;
845 }
846
847 // it's not an error if the string is not found -> no wxCHECK
848
849 return wxNOT_FOUND;
850 }
851
852 // ----------------------------------------------------------------------------
853 // selection
854 // ----------------------------------------------------------------------------
855
856 int wxListBox::GetSelection() const
857 {
858 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
859
860 GList *child = m_list->children;
861 int count = 0;
862 while (child)
863 {
864 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
865 count++;
866 child = child->next;
867 }
868 return wxNOT_FOUND;
869 }
870
871 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
872 {
873 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
874
875 // get the number of selected items first
876 GList *child = m_list->children;
877 int count = 0;
878 for (child = m_list->children; child != NULL; child = child->next)
879 {
880 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
881 count++;
882 }
883
884 aSelections.Empty();
885
886 if (count > 0)
887 {
888 // now fill the list
889 aSelections.Alloc(count); // optimization attempt
890 int i = 0;
891 for (child = m_list->children; child != NULL; child = child->next, i++)
892 {
893 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
894 aSelections.Add(i);
895 }
896 }
897
898 return count;
899 }
900
901 bool wxListBox::IsSelected( int n ) const
902 {
903 wxCHECK_MSG( m_list != NULL, false, wxT("invalid listbox") );
904
905 GList *target = g_list_nth( m_list->children, n );
906
907 wxCHECK_MSG( target, false, wxT("invalid listbox index") );
908
909 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
910 }
911
912 void wxListBox::DoSetSelection( int n, bool select )
913 {
914 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
915
916 m_blockEvent = true;
917
918 if (select)
919 {
920 if ((m_windowStyle & wxLB_SINGLE) != 0)
921 gtk_list_unselect_item( m_list, m_prevSelection );
922 gtk_list_select_item( m_list, n );
923 m_prevSelection = n;
924 }
925 else
926 gtk_list_unselect_item( m_list, n );
927
928 m_blockEvent = false;
929 }
930
931 void wxListBox::DoSetFirstItem( int n )
932 {
933 wxCHECK_RET( m_list, wxT("invalid listbox") );
934
935 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
936 return;
937
938 // terribly efficient
939 const gchar *vadjustment_key = "gtk-vadjustment";
940 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
941
942 GtkAdjustment *adjustment =
943 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
944 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
945
946 GList *target = g_list_nth( m_list->children, n );
947 wxCHECK_RET( target, wxT("invalid listbox index") );
948
949 GtkWidget *item = GTK_WIDGET(target->data);
950 wxCHECK_RET( item, wxT("invalid listbox code") );
951
952 if (item->allocation.y == -1)
953 {
954 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
955 data->m_listbox = this;
956 data->m_item = n;
957 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
958
959 return;
960 }
961
962 float y = item->allocation.y;
963 if (y > adjustment->upper - adjustment->page_size)
964 y = adjustment->upper - adjustment->page_size;
965 gtk_adjustment_set_value( adjustment, y );
966 }
967
968 // ----------------------------------------------------------------------------
969 // helpers
970 // ----------------------------------------------------------------------------
971
972 int wxListBox::GtkGetIndex( GtkWidget *item ) const
973 {
974 if (item)
975 {
976 GList *child = m_list->children;
977 int count = 0;
978 while (child)
979 {
980 if (GTK_WIDGET(child->data) == item) return count;
981 count++;
982 child = child->next;
983 }
984 }
985 return -1;
986 }
987
988 #if wxUSE_TOOLTIPS
989 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
990 {
991 GList *child = m_list->children;
992 while (child)
993 {
994 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), NULL );
995 child = child->next;
996 }
997 }
998 #endif // wxUSE_TOOLTIPS
999
1000 GtkWidget *wxListBox::GetConnectWidget()
1001 {
1002 // return GTK_WIDGET(m_list);
1003 return m_widget;
1004 }
1005
1006 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
1007 {
1008 return m_widget->window == window ||
1009 GTK_WIDGET(m_list)->window == window;
1010 }
1011
1012 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
1013 {
1014 if (m_hasBgCol && m_backgroundColour.IsOk())
1015 {
1016 GdkWindow *window = GTK_WIDGET(m_list)->window;
1017 if ( window )
1018 {
1019 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1020 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1021 gdk_window_clear( window );
1022 }
1023 }
1024
1025 GList *child = m_list->children;
1026 while (child)
1027 {
1028 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
1029
1030 GtkBin *bin = GTK_BIN( child->data );
1031 GtkWidget *label = GTK_WIDGET( bin->child );
1032 gtk_widget_modify_style( label, style );
1033
1034 child = child->next;
1035 }
1036 }
1037
1038 void wxListBox::OnInternalIdle()
1039 {
1040 wxCursor cursor = m_cursor;
1041 if (g_globalCursor.IsOk()) cursor = g_globalCursor;
1042
1043 if (GTK_WIDGET(m_list)->window && cursor.IsOk())
1044 {
1045 /* I now set the cursor the anew in every OnInternalIdle call
1046 as setting the cursor in a parent window also effects the
1047 windows above so that checking for the current cursor is
1048 not possible. */
1049
1050 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1051
1052 GList *child = m_list->children;
1053 while (child)
1054 {
1055 GtkBin *bin = GTK_BIN( child->data );
1056 GtkWidget *label = GTK_WIDGET( bin->child );
1057
1058 if (!label->window)
1059 break;
1060 else
1061 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1062
1063 child = child->next;
1064 }
1065 }
1066
1067 if (g_delayedFocus == this)
1068 {
1069 if (GTK_WIDGET_REALIZED(m_widget))
1070 {
1071 gtk_widget_grab_focus( m_widget );
1072 g_delayedFocus = NULL;
1073 }
1074 }
1075
1076 if (wxUpdateUIEvent::CanUpdate(this))
1077 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1078 }
1079
1080 wxSize wxListBox::DoGetBestSize() const
1081 {
1082 int lbWidth = 100; // some defaults
1083 int lbHeight = 110;
1084 int wLine;
1085
1086 // Find the widest line
1087 for(unsigned int i = 0; i < GetCount(); i++) {
1088 wxString str(GetString(i));
1089 GetTextExtent(str, &wLine, NULL);
1090 lbWidth = wxMax(lbWidth, wLine);
1091 }
1092
1093 // Add room for the scrollbar
1094 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1095
1096 // And just a bit more
1097 int cx, cy;
1098 GetTextExtent( wxT("X"), &cx, &cy);
1099 lbWidth += 3 * cx;
1100
1101 // don't make the listbox too tall (limit height to around 10 items) but don't
1102 // make it too small neither
1103 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1104
1105 wxSize best(lbWidth, lbHeight);
1106 CacheBestSize(best);
1107 return best;
1108 }
1109
1110 void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y)
1111 {
1112 // the mouse event coords are relative to the listbox items, we need to
1113 // translate them to the normal client coords
1114 x += widget->allocation.x;
1115 y += widget->allocation.y;
1116 }
1117
1118
1119 // static
1120 wxVisualAttributes
1121 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1122 {
1123 return GetDefaultAttributesFromGTKWidget(gtk_list_new, true);
1124 }
1125
1126 #endif // wxUSE_LISTBOX