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