]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/listbox.cpp
fixed fl makefiles that Julian didn't fix correctly
[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 #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_prevSelection = 0; // or -1 ??
337
338 if (!PreCreation( parent, pos, size ) ||
339 !CreateBase( parent, id, pos, size, style, validator, name ))
340 {
341 wxFAIL_MSG( wxT("wxListBox creation failed") );
342 return FALSE;
343 }
344
345 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
346 if (style & wxLB_ALWAYS_SB)
347 {
348 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
349 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
350 }
351 else
352 {
353 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
354 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
355 }
356
357 m_list = GTK_LIST( gtk_list_new() );
358
359 GtkSelectionMode mode;
360 if (style & wxLB_MULTIPLE)
361 {
362 mode = GTK_SELECTION_MULTIPLE;
363 }
364 else if (style & wxLB_EXTENDED)
365 {
366 mode = GTK_SELECTION_EXTENDED;
367 }
368 else
369 {
370 // if style was 0 set single mode
371 m_windowStyle |= wxLB_SINGLE;
372 mode = GTK_SELECTION_MULTIPLE;
373 }
374
375 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
376
377 #ifdef NEW_GTK_SCROLL_CODE
378 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
379 #else
380 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
381 #endif
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 SetBestSize( size );
392
393 if ( style & wxLB_SORT )
394 {
395 // this will change DoAppend() behaviour
396 m_strings = new wxSortedArrayString;
397 }
398 else
399 {
400 m_strings = (wxSortedArrayString *)NULL;
401 }
402
403 for (int i = 0; i < n; i++)
404 {
405 // add one by one
406 DoAppend(choices[i]);
407 }
408
409 m_parent->DoAddChild( this );
410
411 PostCreation();
412
413 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
414 SetForegroundColour( parent->GetForegroundColour() );
415 SetFont( parent->GetFont() );
416
417 Show( TRUE );
418
419 return TRUE;
420 }
421
422 wxListBox::~wxListBox()
423 {
424 m_hasVMT = FALSE;
425
426 Clear();
427
428 if (m_strings)
429 delete m_strings;
430 }
431
432 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
433 {
434 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
435
436 // VZ: notice that InsertItems knows nothing about sorting, so calling it
437 // from outside (and not from our own Append) is likely to break
438 // everything
439
440 // code elsewhere supposes we have as many items in m_clientList as items
441 // in the listbox
442 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
443 wxT("bug in client data management") );
444
445 GList *children = m_list->children;
446 int length = g_list_length(children);
447
448 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
449
450 size_t nItems = items.GetCount();
451 int index;
452
453 if (m_strings)
454 {
455 for (size_t n = 0; n < nItems; n++)
456 {
457 index = m_strings->Add( items[n] );
458
459 if (index != GetCount())
460 {
461 GtkAddItem( items[n], index );
462 wxNode *node = m_clientList.Nth( index );
463 m_clientList.Insert( node, (wxObject*) NULL );
464 }
465 else
466 {
467 GtkAddItem( items[n] );
468 m_clientList.Append( (wxObject*) NULL );
469 }
470 }
471 }
472 else
473 {
474 if (pos == length)
475 {
476 for ( size_t n = 0; n < nItems; n++ )
477 {
478 GtkAddItem( items[n] );
479
480 m_clientList.Append((wxObject *)NULL);
481 }
482 }
483 else
484 {
485 wxNode *node = m_clientList.Nth( pos );
486 for ( size_t n = 0; n < nItems; n++ )
487 {
488 GtkAddItem( items[n], pos+n );
489
490 m_clientList.Insert( node, (wxObject *)NULL );
491 }
492 }
493 }
494
495 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
496 wxT("bug in client data management") );
497 }
498
499 int wxListBox::DoAppend( const wxString& item )
500 {
501 if (m_strings)
502 {
503 // need to determine the index
504 int index = m_strings->Add( item );
505
506 // only if not at the end anyway
507 if (index != GetCount())
508 {
509 GtkAddItem( item, index );
510
511 wxNode *node = m_clientList.Nth( index );
512 m_clientList.Insert( node, (wxObject *)NULL );
513
514 return index;
515 }
516 }
517
518 GtkAddItem(item);
519
520 m_clientList.Append((wxObject *)NULL);
521
522 return GetCount() - 1;
523 }
524
525 void wxListBox::GtkAddItem( const wxString &item, int pos )
526 {
527 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
528
529 GtkWidget *list_item;
530
531 wxString label(item);
532 #if wxUSE_CHECKLISTBOX
533 if (m_hasCheckBoxes)
534 {
535 label.Prepend(wxCHECKLBOX_STRING);
536 }
537 #endif // wxUSE_CHECKLISTBOX
538
539 list_item = gtk_list_item_new_with_label( label.mbc_str() );
540
541 GList *gitem_list = g_list_alloc ();
542 gitem_list->data = list_item;
543
544 if (pos == -1)
545 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
546 else
547 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
548
549 gtk_signal_connect( GTK_OBJECT(list_item), "select",
550 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
551
552 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
553 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
554 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
555
556 gtk_signal_connect( GTK_OBJECT(list_item),
557 "button_press_event",
558 (GtkSignalFunc)gtk_listbox_button_press_callback,
559 (gpointer) this );
560
561 gtk_signal_connect_after( GTK_OBJECT(list_item),
562 "button_release_event",
563 (GtkSignalFunc)gtk_listbox_button_release_callback,
564 (gpointer) this );
565
566 gtk_signal_connect( GTK_OBJECT(list_item),
567 "key_press_event",
568 (GtkSignalFunc)gtk_listbox_key_press_callback,
569 (gpointer)this );
570
571 ConnectWidget( list_item );
572
573 gtk_widget_show( list_item );
574
575 if (GTK_WIDGET_REALIZED(m_widget))
576 {
577 gtk_widget_realize( list_item );
578 gtk_widget_realize( GTK_BIN(list_item)->child );
579
580 // Apply current widget style to the new list_item
581 if (m_widgetStyle)
582 {
583 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
584 GtkBin *bin = GTK_BIN( list_item );
585 GtkWidget *label = GTK_WIDGET( bin->child );
586 gtk_widget_set_style( label, m_widgetStyle );
587 }
588
589 #if wxUSE_TOOLTIPS
590 if (m_tooltip) m_tooltip->Apply( this );
591 #endif
592 }
593 }
594
595 void wxListBox::DoSetItems( const wxArrayString& items,
596 void **clientData)
597 {
598 Clear();
599
600 DoInsertItems(items, 0);
601
602 if ( clientData )
603 {
604 size_t count = items.GetCount();
605 for ( size_t n = 0; n < count; n++ )
606 {
607 SetClientData(n, clientData[n]);
608 }
609 }
610 }
611
612 // ----------------------------------------------------------------------------
613 // client data
614 // ----------------------------------------------------------------------------
615
616 void wxListBox::DoSetItemClientData( int n, void* clientData )
617 {
618 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
619
620 wxNode *node = m_clientList.Nth( n );
621 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
622
623 node->SetData( (wxObject*) clientData );
624 }
625
626 void* wxListBox::DoGetItemClientData( int n ) const
627 {
628 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
629
630 wxNode *node = m_clientList.Nth( n );
631 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
632
633 return node->Data();
634 }
635
636 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
637 {
638 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
639
640 wxNode *node = m_clientList.Nth( n );
641 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
642
643 wxClientData *cd = (wxClientData*) node->Data();
644 delete cd;
645
646 node->SetData( (wxObject*) clientData );
647 }
648
649 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
650 {
651 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
652
653 wxNode *node = m_clientList.Nth( n );
654 wxCHECK_MSG( node, (wxClientData *)NULL,
655 wxT("invalid index in wxListBox::DoGetItemClientObject") );
656
657 return (wxClientData*) node->Data();
658 }
659
660 void wxListBox::Clear()
661 {
662 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
663
664 gtk_list_clear_items( m_list, 0, GetCount() );
665
666 if ( HasClientObjectData() )
667 {
668 // destroy the data (due to Robert's idea of using wxList<wxObject>
669 // and not wxList<wxClientData> we can't just say
670 // m_clientList.DeleteContents(TRUE) - this would crash!
671 wxNode *node = m_clientList.First();
672 while ( node )
673 {
674 delete (wxClientData *)node->Data();
675 node = node->Next();
676 }
677 }
678 m_clientList.Clear();
679
680 if ( m_strings )
681 m_strings->Clear();
682 }
683
684 void wxListBox::Delete( int n )
685 {
686 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
687
688 GList *child = g_list_nth( m_list->children, n );
689
690 wxCHECK_RET( child, wxT("wrong listbox index") );
691
692 GList *list = g_list_append( (GList*) NULL, child->data );
693 gtk_list_remove_items( m_list, list );
694 g_list_free( list );
695
696 wxNode *node = m_clientList.Nth( n );
697 if ( node )
698 {
699 if ( m_clientDataItemsType == wxClientData_Object )
700 {
701 wxClientData *cd = (wxClientData*)node->Data();
702 delete cd;
703 }
704
705 m_clientList.DeleteNode( node );
706 }
707
708 if ( m_strings )
709 m_strings->Remove(n);
710 }
711
712 // ----------------------------------------------------------------------------
713 // string list access
714 // ----------------------------------------------------------------------------
715
716 void wxListBox::SetString( int n, const wxString &string )
717 {
718 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
719
720 GList *child = g_list_nth( m_list->children, n );
721 if (child)
722 {
723 GtkBin *bin = GTK_BIN( child->data );
724 GtkLabel *label = GTK_LABEL( bin->child );
725
726 wxString str;
727 #if wxUSE_CHECKLISTBOX
728 if (m_hasCheckBoxes)
729 str += wxCHECKLBOX_STRING;
730 #endif // wxUSE_CHECKLISTBOX
731 str += string;
732
733 gtk_label_set( label, str.mbc_str() );
734 }
735 else
736 {
737 wxFAIL_MSG(wxT("wrong listbox index"));
738 }
739 }
740
741 wxString wxListBox::GetString( int n ) const
742 {
743 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
744
745 GList *child = g_list_nth( m_list->children, n );
746 if (child)
747 {
748 GtkBin *bin = GTK_BIN( child->data );
749 GtkLabel *label = GTK_LABEL( bin->child );
750
751 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
752
753 return str;
754 }
755
756 wxFAIL_MSG(wxT("wrong listbox index"));
757
758 return wxT("");
759 }
760
761 int wxListBox::GetCount() const
762 {
763 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
764
765 GList *children = m_list->children;
766 return g_list_length(children);
767 }
768
769 int wxListBox::FindString( const wxString &item ) const
770 {
771 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
772
773 GList *child = m_list->children;
774 int count = 0;
775 while (child)
776 {
777 GtkBin *bin = GTK_BIN( child->data );
778 GtkLabel *label = GTK_LABEL( bin->child );
779
780 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
781
782 if (str == item)
783 return count;
784
785 count++;
786 child = child->next;
787 }
788
789 // it's not an error if the string is not found -> no wxCHECK
790
791 return wxNOT_FOUND;
792 }
793
794 // ----------------------------------------------------------------------------
795 // selection
796 // ----------------------------------------------------------------------------
797
798 int wxListBox::GetSelection() const
799 {
800 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
801
802 GList *child = m_list->children;
803 int count = 0;
804 while (child)
805 {
806 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
807 count++;
808 child = child->next;
809 }
810 return -1;
811 }
812
813 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
814 {
815 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
816
817 // get the number of selected items first
818 GList *child = m_list->children;
819 int count = 0;
820 for (child = m_list->children; child != NULL; child = child->next)
821 {
822 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
823 count++;
824 }
825
826 aSelections.Empty();
827
828 if (count > 0)
829 {
830 // now fill the list
831 aSelections.Alloc(count); // optimization attempt
832 int i = 0;
833 for (child = m_list->children; child != NULL; child = child->next, i++)
834 {
835 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
836 aSelections.Add(i);
837 }
838 }
839
840 return count;
841 }
842
843 bool wxListBox::IsSelected( int n ) const
844 {
845 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
846
847 GList *target = g_list_nth( m_list->children, n );
848
849 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
850
851 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
852 }
853
854 void wxListBox::SetSelection( int n, bool select )
855 {
856 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
857
858 GtkDisableEvents();
859
860 if (select)
861 {
862 if ((m_windowStyle & wxLB_SINGLE) != 0)
863 gtk_list_unselect_item( m_list, m_prevSelection );
864 gtk_list_select_item( m_list, n );
865 m_prevSelection = n;
866 }
867 else
868 gtk_list_unselect_item( m_list, n );
869
870 GtkEnableEvents();
871 }
872
873 void wxListBox::DoSetFirstItem( int n )
874 {
875 wxCHECK_RET( m_list, wxT("invalid listbox") );
876
877 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
878 return;
879
880 // terribly efficient
881 const gchar *vadjustment_key = "gtk-vadjustment";
882 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
883
884 GtkAdjustment *adjustment =
885 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
886 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
887
888 GList *target = g_list_nth( m_list->children, n );
889 wxCHECK_RET( target, wxT("invalid listbox index") );
890
891 GtkWidget *item = GTK_WIDGET(target->data);
892 wxCHECK_RET( item, wxT("invalid listbox code") );
893
894 if (item->allocation.y == -1)
895 {
896 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
897 data->m_listbox = this;
898 data->m_item = n;
899 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
900
901 return;
902 }
903
904 float y = item->allocation.y;
905 if (y > adjustment->upper - adjustment->page_size)
906 y = adjustment->upper - adjustment->page_size;
907 gtk_adjustment_set_value( adjustment, y );
908 }
909
910 // ----------------------------------------------------------------------------
911 // helpers
912 // ----------------------------------------------------------------------------
913
914 int wxListBox::GtkGetIndex( GtkWidget *item ) const
915 {
916 if (item)
917 {
918 GList *child = m_list->children;
919 int count = 0;
920 while (child)
921 {
922 if (GTK_WIDGET(child->data) == item) return count;
923 count++;
924 child = child->next;
925 }
926 }
927 return -1;
928 }
929
930 #if wxUSE_TOOLTIPS
931 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
932 {
933 GList *child = m_list->children;
934 while (child)
935 {
936 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
937 child = child->next;
938 }
939 }
940 #endif // wxUSE_TOOLTIPS
941
942 void wxListBox::GtkDisableEvents()
943 {
944 GList *child = m_list->children;
945 while (child)
946 {
947 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
948 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
949
950 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
951 gtk_signal_disconnect_by_func( GTK_OBJECT(child->data),
952 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
953
954 child = child->next;
955 }
956 }
957
958 void wxListBox::GtkEnableEvents()
959 {
960 GList *child = m_list->children;
961 while (child)
962 {
963 gtk_signal_connect( GTK_OBJECT(child->data), "select",
964 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
965
966 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
967 gtk_signal_connect( GTK_OBJECT(child->data), "deselect",
968 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
969
970 child = child->next;
971 }
972 }
973
974 GtkWidget *wxListBox::GetConnectWidget()
975 {
976 return GTK_WIDGET(m_list);
977 }
978
979 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
980 {
981 if (GTK_WIDGET(m_list)->window == window) return TRUE;
982
983 GList *child = m_list->children;
984 while (child)
985 {
986 GtkWidget *bin = GTK_WIDGET( child->data );
987 if (bin->window == window) return TRUE;
988 child = child->next;
989 }
990
991 return FALSE;
992 }
993
994 void wxListBox::ApplyWidgetStyle()
995 {
996 SetWidgetStyle();
997
998 if (m_backgroundColour.Ok())
999 {
1000 GdkWindow *window = GTK_WIDGET(m_list)->window;
1001 if ( window )
1002 {
1003 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1004 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1005 gdk_window_clear( window );
1006 }
1007 }
1008
1009 GList *child = m_list->children;
1010 while (child)
1011 {
1012 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
1013
1014 GtkBin *bin = GTK_BIN( child->data );
1015 GtkWidget *label = GTK_WIDGET( bin->child );
1016 gtk_widget_set_style( label, m_widgetStyle );
1017
1018 child = child->next;
1019 }
1020 }
1021
1022 void wxListBox::OnInternalIdle()
1023 {
1024 wxCursor cursor = m_cursor;
1025 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1026
1027 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1028 {
1029 /* I now set the cursor the anew in every OnInternalIdle call
1030 as setting the cursor in a parent window also effects the
1031 windows above so that checking for the current cursor is
1032 not possible. */
1033
1034 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1035
1036 GList *child = m_list->children;
1037 while (child)
1038 {
1039 GtkBin *bin = GTK_BIN( child->data );
1040 GtkWidget *label = GTK_WIDGET( bin->child );
1041
1042 if (!label->window)
1043 break;
1044 else
1045 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1046
1047 child = child->next;
1048 }
1049 }
1050
1051 UpdateWindowUI();
1052 }
1053
1054 wxSize wxListBox::DoGetBestSize() const
1055 {
1056 return wxSize(100, 110);
1057 }
1058
1059 #endif