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