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