]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
More OS/2 update to fix some owner drawn things
[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 #include "wx/gtk/private.h"
25
26 #if wxUSE_TOOLTIPS
27 #include "wx/tooltip.h"
28 #endif
29
30 #include <gdk/gdk.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33
34 //-----------------------------------------------------------------------------
35 // idle system
36 //-----------------------------------------------------------------------------
37
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 //-----------------------------------------------------------------------------
42 // private functions
43 //-----------------------------------------------------------------------------
44
45 #if wxUSE_CHECKLISTBOX
46
47 // checklistboxes have "[±] " prepended to their lables, this macro removes it
48 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
49 //
50 // the argument to it is a "const char *" pointer
51 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
52
53 #else // !wxUSE_CHECKLISTBOX
54
55 #define GET_REAL_LABEL(label) (label)
56
57 #endif // wxUSE_CHECKLISTBOX
58
59 //-----------------------------------------------------------------------------
60 // data
61 //-----------------------------------------------------------------------------
62
63 extern bool g_blockEventsOnDrag;
64 extern bool g_blockEventsOnScroll;
65 extern wxCursor g_globalCursor;
66 extern wxWindowGTK *g_delayedFocus;
67
68 static bool g_hasDoubleClicked = FALSE;
69
70 //-----------------------------------------------------------------------------
71 // idle callback for SetFirstItem
72 //-----------------------------------------------------------------------------
73
74 struct wxlistbox_idle_struct
75 {
76 wxListBox *m_listbox;
77 int m_item;
78 gint m_tag;
79 };
80
81 extern "C" gint wxlistbox_idle_callback( gpointer gdata )
82 {
83 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
84 gdk_threads_enter();
85
86 gtk_idle_remove( data->m_tag );
87
88 // check that the items haven't been deleted from the listbox since we had
89 // installed this callback
90 wxListBox *lbox = data->m_listbox;
91 if ( data->m_item < lbox->GetCount() )
92 {
93 lbox->SetFirstItem( data->m_item );
94 }
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 if (listbox->m_blockEvent) return;
272
273 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
274 event.SetEventObject( listbox );
275
276 // MSW doesn't do that either
277 // event.SetExtraLong( (long) is_selection );
278
279
280 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
281 {
282 int sel = listbox->GtkGetIndex( widget );
283
284 if (listbox->m_prevSelection != sel)
285 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
286
287 listbox->m_prevSelection = sel;
288 }
289
290 wxArrayInt aSelections;
291 int n, count = listbox->GetSelections(aSelections);
292 if ( count > 0 )
293 {
294 n = aSelections[0];
295 if ( listbox->HasClientObjectData() )
296 event.SetClientObject( listbox->GetClientObject(n) );
297 else if ( listbox->HasClientUntypedData() )
298 event.SetClientData( listbox->GetClientData(n) );
299 event.SetString( listbox->GetString(n) );
300 }
301 else
302 {
303 n = -1;
304 }
305
306 event.m_commandInt = n;
307
308 // No longer required with new code in wxLB_SINGLE
309 // listbox->GetEventHandler()->AddPendingEvent( event );
310 listbox->GetEventHandler()->ProcessEvent( event );
311 }
312
313 //-----------------------------------------------------------------------------
314 // wxListBox
315 //-----------------------------------------------------------------------------
316
317 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
318
319 // ----------------------------------------------------------------------------
320 // construction
321 // ----------------------------------------------------------------------------
322
323 wxListBox::wxListBox()
324 {
325 m_list = (GtkList *) NULL;
326 #if wxUSE_CHECKLISTBOX
327 m_hasCheckBoxes = FALSE;
328 #endif // wxUSE_CHECKLISTBOX
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 // call it after appending the strings to the listbox, otherwise it doesn't
408 // work correctly
409 SetBestSize( size );
410
411 m_parent->DoAddChild( this );
412
413 PostCreation();
414
415 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ) );
416 SetForegroundColour( parent->GetForegroundColour() );
417 SetFont( parent->GetFont() );
418
419 Show( TRUE );
420
421 return TRUE;
422 }
423
424 wxListBox::~wxListBox()
425 {
426 m_hasVMT = FALSE;
427
428 Clear();
429
430 if (m_strings)
431 delete m_strings;
432 }
433
434 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
435 {
436 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
437
438 // VZ: notice that InsertItems knows nothing about sorting, so calling it
439 // from outside (and not from our own Append) is likely to break
440 // everything
441
442 // code elsewhere supposes we have as many items in m_clientList as items
443 // in the listbox
444 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
445 wxT("bug in client data management") );
446
447 GList *children = m_list->children;
448 int length = g_list_length(children);
449
450 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
451
452 size_t nItems = items.GetCount();
453 int index;
454
455 if (m_strings)
456 {
457 for (size_t n = 0; n < nItems; n++)
458 {
459 index = m_strings->Add( items[n] );
460
461 if (index != GetCount())
462 {
463 GtkAddItem( items[n], index );
464 wxNode *node = m_clientList.Nth( index );
465 m_clientList.Insert( node, (wxObject*) NULL );
466 }
467 else
468 {
469 GtkAddItem( items[n] );
470 m_clientList.Append( (wxObject*) NULL );
471 }
472 }
473 }
474 else
475 {
476 if (pos == length)
477 {
478 for ( size_t n = 0; n < nItems; n++ )
479 {
480 GtkAddItem( items[n] );
481
482 m_clientList.Append((wxObject *)NULL);
483 }
484 }
485 else
486 {
487 wxNode *node = m_clientList.Nth( pos );
488 for ( size_t n = 0; n < nItems; n++ )
489 {
490 GtkAddItem( items[n], pos+n );
491
492 m_clientList.Insert( node, (wxObject *)NULL );
493 }
494 }
495 }
496
497 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
498 wxT("bug in client data management") );
499 }
500
501 int wxListBox::DoAppend( const wxString& item )
502 {
503 if (m_strings)
504 {
505 // need to determine the index
506 int index = m_strings->Add( item );
507
508 // only if not at the end anyway
509 if (index != GetCount())
510 {
511 GtkAddItem( item, index );
512
513 wxNode *node = m_clientList.Nth( index );
514 m_clientList.Insert( node, (wxObject *)NULL );
515
516 return index;
517 }
518 }
519
520 GtkAddItem(item);
521
522 m_clientList.Append((wxObject *)NULL);
523
524 return GetCount() - 1;
525 }
526
527 void wxListBox::GtkAddItem( const wxString &item, int pos )
528 {
529 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
530
531 GtkWidget *list_item;
532
533 wxString label(item);
534 #if wxUSE_CHECKLISTBOX
535 if (m_hasCheckBoxes)
536 {
537 label.Prepend(wxCHECKLBOX_STRING);
538 }
539 #endif // wxUSE_CHECKLISTBOX
540
541 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
542
543 GList *gitem_list = g_list_alloc ();
544 gitem_list->data = list_item;
545
546 if (pos == -1)
547 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
548 else
549 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
550
551 gtk_signal_connect( GTK_OBJECT(list_item), "select",
552 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
553
554 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
555 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
556 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
557
558 gtk_signal_connect( GTK_OBJECT(list_item),
559 "button_press_event",
560 (GtkSignalFunc)gtk_listbox_button_press_callback,
561 (gpointer) this );
562
563 gtk_signal_connect_after( GTK_OBJECT(list_item),
564 "button_release_event",
565 (GtkSignalFunc)gtk_listbox_button_release_callback,
566 (gpointer) this );
567
568 gtk_signal_connect( GTK_OBJECT(list_item),
569 "key_press_event",
570 (GtkSignalFunc)gtk_listbox_key_press_callback,
571 (gpointer)this );
572
573 ConnectWidget( list_item );
574
575 gtk_widget_show( list_item );
576
577 if (GTK_WIDGET_REALIZED(m_widget))
578 {
579 gtk_widget_realize( list_item );
580 gtk_widget_realize( GTK_BIN(list_item)->child );
581
582 // Apply current widget style to the new list_item
583 if (m_widgetStyle)
584 {
585 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
586 GtkBin *bin = GTK_BIN( list_item );
587 GtkWidget *label = GTK_WIDGET( bin->child );
588 gtk_widget_set_style( label, m_widgetStyle );
589 }
590
591 #if wxUSE_TOOLTIPS
592 if (m_tooltip) m_tooltip->Apply( this );
593 #endif
594 }
595 }
596
597 void wxListBox::DoSetItems( const wxArrayString& items,
598 void **clientData)
599 {
600 Clear();
601
602 DoInsertItems(items, 0);
603
604 if ( clientData )
605 {
606 size_t count = items.GetCount();
607 for ( size_t n = 0; n < count; n++ )
608 {
609 SetClientData(n, clientData[n]);
610 }
611 }
612 }
613
614 // ----------------------------------------------------------------------------
615 // client data
616 // ----------------------------------------------------------------------------
617
618 void wxListBox::DoSetItemClientData( int n, void* clientData )
619 {
620 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
621
622 wxNode *node = m_clientList.Nth( n );
623 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
624
625 node->SetData( (wxObject*) clientData );
626 }
627
628 void* wxListBox::DoGetItemClientData( int n ) const
629 {
630 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
631
632 wxNode *node = m_clientList.Nth( n );
633 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
634
635 return node->Data();
636 }
637
638 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
639 {
640 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
641
642 wxNode *node = m_clientList.Nth( n );
643 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
644
645 // wxItemContainer already deletes data for us
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 ( GTK_LIST(m_list)->last_focus_child != NULL )
668 {
669 // This should be NULL, I think.
670 GTK_LIST(m_list)->last_focus_child = NULL;
671 }
672
673 if ( HasClientObjectData() )
674 {
675 // destroy the data (due to Robert's idea of using wxList<wxObject>
676 // and not wxList<wxClientData> we can't just say
677 // m_clientList.DeleteContents(TRUE) - this would crash!
678 wxNode *node = m_clientList.First();
679 while ( node )
680 {
681 delete (wxClientData *)node->Data();
682 node = node->Next();
683 }
684 }
685 m_clientList.Clear();
686
687 if ( m_strings )
688 m_strings->Clear();
689 }
690
691 void wxListBox::Delete( int n )
692 {
693 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
694
695 GList *child = g_list_nth( m_list->children, n );
696
697 wxCHECK_RET( child, wxT("wrong listbox index") );
698
699 GList *list = g_list_append( (GList*) NULL, child->data );
700 gtk_list_remove_items( m_list, list );
701 g_list_free( list );
702
703 wxNode *node = m_clientList.Nth( n );
704 if ( node )
705 {
706 if ( m_clientDataItemsType == wxClientData_Object )
707 {
708 wxClientData *cd = (wxClientData*)node->Data();
709 delete cd;
710 }
711
712 m_clientList.DeleteNode( node );
713 }
714
715 if ( m_strings )
716 m_strings->Remove(n);
717 }
718
719 // ----------------------------------------------------------------------------
720 // string list access
721 // ----------------------------------------------------------------------------
722
723 void wxListBox::SetString( int n, const wxString &string )
724 {
725 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
726
727 GList *child = g_list_nth( m_list->children, n );
728 if (child)
729 {
730 GtkBin *bin = GTK_BIN( child->data );
731 GtkLabel *label = GTK_LABEL( bin->child );
732
733 wxString str;
734 #if wxUSE_CHECKLISTBOX
735 if (m_hasCheckBoxes)
736 str += wxCHECKLBOX_STRING;
737 #endif // wxUSE_CHECKLISTBOX
738 str += string;
739
740 gtk_label_set( label, wxGTK_CONV( str ) );
741 }
742 else
743 {
744 wxFAIL_MSG(wxT("wrong listbox index"));
745 }
746 }
747
748 wxString wxListBox::GetString( int n ) const
749 {
750 wxCHECK_MSG( m_list != NULL, wxT(""), wxT("invalid listbox") );
751
752 GList *child = g_list_nth( m_list->children, n );
753 if (child)
754 {
755 GtkBin *bin = GTK_BIN( child->data );
756 GtkLabel *label = GTK_LABEL( bin->child );
757
758 #ifdef __WXGTK20__
759 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
760 #else
761 wxString str = wxString( label->label );
762 #endif
763
764 return str;
765 }
766
767 wxFAIL_MSG(wxT("wrong listbox index"));
768
769 return wxT("");
770 }
771
772 int wxListBox::GetCount() const
773 {
774 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
775
776 GList *children = m_list->children;
777 return g_list_length(children);
778 }
779
780 int wxListBox::FindString( const wxString &item ) const
781 {
782 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
783
784 GList *child = m_list->children;
785 int count = 0;
786 while (child)
787 {
788 GtkBin *bin = GTK_BIN( child->data );
789 GtkLabel *label = GTK_LABEL( bin->child );
790
791 #ifdef __WXGTK20__
792 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
793 #else
794 wxString str = wxString( label->label );
795 #endif
796
797 if (str == item)
798 return count;
799
800 count++;
801 child = child->next;
802 }
803
804 // it's not an error if the string is not found -> no wxCHECK
805
806 return wxNOT_FOUND;
807 }
808
809 // ----------------------------------------------------------------------------
810 // selection
811 // ----------------------------------------------------------------------------
812
813 int wxListBox::GetSelection() const
814 {
815 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
816
817 GList *child = m_list->children;
818 int count = 0;
819 while (child)
820 {
821 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
822 count++;
823 child = child->next;
824 }
825 return -1;
826 }
827
828 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
829 {
830 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
831
832 // get the number of selected items first
833 GList *child = m_list->children;
834 int count = 0;
835 for (child = m_list->children; child != NULL; child = child->next)
836 {
837 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
838 count++;
839 }
840
841 aSelections.Empty();
842
843 if (count > 0)
844 {
845 // now fill the list
846 aSelections.Alloc(count); // optimization attempt
847 int i = 0;
848 for (child = m_list->children; child != NULL; child = child->next, i++)
849 {
850 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
851 aSelections.Add(i);
852 }
853 }
854
855 return count;
856 }
857
858 bool wxListBox::IsSelected( int n ) const
859 {
860 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
861
862 GList *target = g_list_nth( m_list->children, n );
863
864 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
865
866 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
867 }
868
869 void wxListBox::SetSelection( int n, bool select )
870 {
871 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
872
873 m_blockEvent = TRUE;
874
875 if (select)
876 {
877 if ((m_windowStyle & wxLB_SINGLE) != 0)
878 gtk_list_unselect_item( m_list, m_prevSelection );
879 gtk_list_select_item( m_list, n );
880 m_prevSelection = n;
881 }
882 else
883 gtk_list_unselect_item( m_list, n );
884
885 m_blockEvent = FALSE;
886 }
887
888 void wxListBox::DoSetFirstItem( int n )
889 {
890 wxCHECK_RET( m_list, wxT("invalid listbox") );
891
892 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
893 return;
894
895 // terribly efficient
896 const gchar *vadjustment_key = "gtk-vadjustment";
897 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
898
899 GtkAdjustment *adjustment =
900 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
901 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
902
903 GList *target = g_list_nth( m_list->children, n );
904 wxCHECK_RET( target, wxT("invalid listbox index") );
905
906 GtkWidget *item = GTK_WIDGET(target->data);
907 wxCHECK_RET( item, wxT("invalid listbox code") );
908
909 if (item->allocation.y == -1)
910 {
911 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
912 data->m_listbox = this;
913 data->m_item = n;
914 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
915
916 return;
917 }
918
919 float y = item->allocation.y;
920 if (y > adjustment->upper - adjustment->page_size)
921 y = adjustment->upper - adjustment->page_size;
922 gtk_adjustment_set_value( adjustment, y );
923 }
924
925 // ----------------------------------------------------------------------------
926 // helpers
927 // ----------------------------------------------------------------------------
928
929 int wxListBox::GtkGetIndex( GtkWidget *item ) const
930 {
931 if (item)
932 {
933 GList *child = m_list->children;
934 int count = 0;
935 while (child)
936 {
937 if (GTK_WIDGET(child->data) == item) return count;
938 count++;
939 child = child->next;
940 }
941 }
942 return -1;
943 }
944
945 #if wxUSE_TOOLTIPS
946 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
947 {
948 GList *child = m_list->children;
949 while (child)
950 {
951 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
952 child = child->next;
953 }
954 }
955 #endif // wxUSE_TOOLTIPS
956
957 GtkWidget *wxListBox::GetConnectWidget()
958 {
959 return GTK_WIDGET(m_list);
960 }
961
962 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
963 {
964 if (GTK_WIDGET(m_list)->window == window) return TRUE;
965
966 GList *child = m_list->children;
967 while (child)
968 {
969 GtkWidget *bin = GTK_WIDGET( child->data );
970 if (bin->window == window) return TRUE;
971 child = child->next;
972 }
973
974 return FALSE;
975 }
976
977 void wxListBox::ApplyWidgetStyle()
978 {
979 SetWidgetStyle();
980
981 if (m_backgroundColour.Ok())
982 {
983 GdkWindow *window = GTK_WIDGET(m_list)->window;
984 if ( window )
985 {
986 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
987 gdk_window_set_background( window, m_backgroundColour.GetColor() );
988 gdk_window_clear( window );
989 }
990 }
991
992 GList *child = m_list->children;
993 while (child)
994 {
995 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
996
997 GtkBin *bin = GTK_BIN( child->data );
998 GtkWidget *label = GTK_WIDGET( bin->child );
999 gtk_widget_set_style( label, m_widgetStyle );
1000
1001 child = child->next;
1002 }
1003 }
1004
1005 void wxListBox::OnInternalIdle()
1006 {
1007 wxCursor cursor = m_cursor;
1008 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1009
1010 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1011 {
1012 /* I now set the cursor the anew in every OnInternalIdle call
1013 as setting the cursor in a parent window also effects the
1014 windows above so that checking for the current cursor is
1015 not possible. */
1016
1017 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1018
1019 GList *child = m_list->children;
1020 while (child)
1021 {
1022 GtkBin *bin = GTK_BIN( child->data );
1023 GtkWidget *label = GTK_WIDGET( bin->child );
1024
1025 if (!label->window)
1026 break;
1027 else
1028 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1029
1030 child = child->next;
1031 }
1032 }
1033
1034 if (g_delayedFocus == this)
1035 {
1036 if (GTK_WIDGET_REALIZED(m_widget))
1037 {
1038 gtk_widget_grab_focus( m_widget );
1039 g_delayedFocus = NULL;
1040 }
1041 }
1042
1043 UpdateWindowUI();
1044 }
1045
1046 wxSize wxListBox::DoGetBestSize() const
1047 {
1048 int lbWidth = 100; // some defaults
1049 int lbHeight = 110;
1050 int wLine;
1051
1052 // Find the widest line
1053 for(int i = 0; i < GetCount(); i++) {
1054 wxString str(GetString(i));
1055 GetTextExtent(str, &wLine, NULL);
1056 lbWidth = wxMax(lbWidth, wLine);
1057 }
1058
1059 // Add room for the scrollbar
1060 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1061
1062 // And just a bit more
1063 int cx, cy;
1064 GetTextExtent("X", &cx, &cy);
1065 lbWidth += 3 * cx;
1066
1067 // don't make the listbox too tall (limit height to around 10 items) but don't
1068 // make it too small neither
1069 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1070
1071 return wxSize(lbWidth, lbHeight);
1072 }
1073
1074 void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y)
1075 {
1076 // the mouse event coords are relative to the listbox items, we need to
1077 // translate them to the normal client coords
1078 x += widget->allocation.x;
1079 y += widget->allocation.y;
1080 }
1081
1082 #endif // wxUSE_LISTBOX
1083