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