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