]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
added test for wxNO_DEFAULT in wxMessageBox
[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 // private functions
42 //-----------------------------------------------------------------------------
43
44 #if wxUSE_CHECKLISTBOX
45
46 // checklistboxes have "[±] " prepended to their lables, this macro removes it
47 // (NB: 4 below is the length of wxCHECKLBOX_STRING above)
48 //
49 // the argument to it is a "const char *" pointer
50 #define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label))
51
52 #else // !wxUSE_CHECKLISTBOX
53
54 #define GET_REAL_LABEL(label) (label)
55
56 #endif // wxUSE_CHECKLISTBOX
57
58 //-----------------------------------------------------------------------------
59 // data
60 //-----------------------------------------------------------------------------
61
62 extern bool g_blockEventsOnDrag;
63 extern bool g_blockEventsOnScroll;
64 extern wxCursor g_globalCursor;
65
66 static bool g_hasDoubleClicked = FALSE;
67
68 //-----------------------------------------------------------------------------
69 // idle callback for SetFirstItem
70 //-----------------------------------------------------------------------------
71
72 struct wxlistbox_idle_struct
73 {
74 wxListBox *m_listbox;
75 int m_item;
76 gint m_tag;
77 };
78
79 extern "C" gint wxlistbox_idle_callback( gpointer gdata )
80 {
81 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
82 gdk_threads_enter();
83
84 gtk_idle_remove( data->m_tag );
85
86 // check that the items haven't been deleted from the listbox since we had
87 // installed this callback
88 wxListBox *lbox = data->m_listbox;
89 if ( data->m_item < lbox->GetCount() )
90 {
91 lbox->SetFirstItem( data->m_item );
92 }
93
94 delete data;
95
96 gdk_threads_leave();
97
98 return TRUE;
99 }
100
101 //-----------------------------------------------------------------------------
102 // "button_release_event"
103 //-----------------------------------------------------------------------------
104
105 /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
106 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
107 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
108 this can lead to race conditions so that we emit the dclick event
109 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
110
111 static gint
112 gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
113 GdkEventButton * WXUNUSED(gdk_event),
114 wxListBox *listbox )
115 {
116 if (g_isIdle) wxapp_install_idle_handler();
117
118 if (g_blockEventsOnDrag) return FALSE;
119 if (g_blockEventsOnScroll) return FALSE;
120
121 if (!listbox->m_hasVMT) return FALSE;
122
123 if (!g_hasDoubleClicked) return FALSE;
124
125 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
126 event.SetEventObject( listbox );
127
128 wxArrayInt aSelections;
129 int n, count = listbox->GetSelections(aSelections);
130 if ( count > 0 )
131 {
132 n = aSelections[0];
133 if ( listbox->HasClientObjectData() )
134 event.SetClientObject( listbox->GetClientObject(n) );
135 else if ( listbox->HasClientUntypedData() )
136 event.SetClientData( listbox->GetClientData(n) );
137 event.SetString( listbox->GetString(n) );
138 }
139 else
140 {
141 n = -1;
142 }
143
144 event.m_commandInt = n;
145
146 listbox->GetEventHandler()->ProcessEvent( event );
147
148 return FALSE;
149 }
150
151 //-----------------------------------------------------------------------------
152 // "button_press_event"
153 //-----------------------------------------------------------------------------
154
155 static gint
156 gtk_listbox_button_press_callback( GtkWidget *widget,
157 GdkEventButton *gdk_event,
158 wxListBox *listbox )
159 {
160 if (g_isIdle) wxapp_install_idle_handler();
161
162 if (g_blockEventsOnDrag) return FALSE;
163 if (g_blockEventsOnScroll) return FALSE;
164
165 if (!listbox->m_hasVMT) return FALSE;
166
167 int sel = listbox->GtkGetIndex( widget );
168
169 #if wxUSE_CHECKLISTBOX
170 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
171 {
172 wxCheckListBox *clb = (wxCheckListBox *)listbox;
173
174 clb->Check( sel, !clb->IsChecked(sel) );
175
176 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
177 event.SetEventObject( listbox );
178 event.SetInt( sel );
179 listbox->GetEventHandler()->ProcessEvent( event );
180 }
181 #endif // wxUSE_CHECKLISTBOX
182
183 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
184 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
185
186 return FALSE;
187 }
188
189 //-----------------------------------------------------------------------------
190 // "key_press_event"
191 //-----------------------------------------------------------------------------
192
193 static gint
194 gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
195 {
196 if (g_isIdle)
197 wxapp_install_idle_handler();
198
199 if (g_blockEventsOnDrag)
200 return FALSE;
201
202 bool ret = FALSE;
203
204 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
205 {
206 wxNavigationKeyEvent new_event;
207 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
208 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
209 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
210 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
211 new_event.SetCurrentFocus( listbox );
212 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
213 }
214
215 if ((gdk_event->keyval == GDK_Return) && (!ret))
216 {
217 // eat return in all modes
218 ret = TRUE;
219 }
220
221 #if wxUSE_CHECKLISTBOX
222 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
223 {
224 int sel = listbox->GtkGetIndex( widget );
225
226 wxCheckListBox *clb = (wxCheckListBox *)listbox;
227
228 clb->Check( sel, !clb->IsChecked(sel) );
229
230 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
231 new_event.SetEventObject( listbox );
232 new_event.SetInt( sel );
233 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
234 }
235 #endif // wxUSE_CHECKLISTBOX
236
237 if (ret)
238 {
239 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
240 return TRUE;
241 }
242
243 return FALSE;
244 }
245
246 //-----------------------------------------------------------------------------
247 // "select" and "deselect"
248 //-----------------------------------------------------------------------------
249
250 static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
251
252 static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
253 {
254 gtk_listitem_select_cb( widget, listbox, TRUE );
255 }
256
257 static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
258 {
259 gtk_listitem_select_cb( widget, listbox, FALSE );
260 }
261
262 static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection )
263 {
264 if (g_isIdle) wxapp_install_idle_handler();
265
266 if (!listbox->m_hasVMT) return;
267 if (g_blockEventsOnDrag) return;
268
269 if (listbox->m_blockEvent) return;
270
271 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
272 event.SetEventObject( listbox );
273
274 // MSW doesn't do that either
275 // event.SetExtraLong( (long) is_selection );
276
277
278 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
279 {
280 int sel = listbox->GtkGetIndex( widget );
281
282 if (listbox->m_prevSelection != sel)
283 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
284
285 listbox->m_prevSelection = sel;
286 }
287
288 wxArrayInt aSelections;
289 int n, count = listbox->GetSelections(aSelections);
290 if ( count > 0 )
291 {
292 n = aSelections[0];
293 if ( listbox->HasClientObjectData() )
294 event.SetClientObject( listbox->GetClientObject(n) );
295 else if ( listbox->HasClientUntypedData() )
296 event.SetClientData( listbox->GetClientData(n) );
297 event.SetString( listbox->GetString(n) );
298 }
299 else
300 {
301 n = -1;
302 }
303
304 event.m_commandInt = n;
305
306 // No longer required with new code in wxLB_SINGLE
307 // listbox->GetEventHandler()->AddPendingEvent( event );
308 listbox->GetEventHandler()->ProcessEvent( event );
309 }
310
311 //-----------------------------------------------------------------------------
312 // wxListBox
313 //-----------------------------------------------------------------------------
314
315 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
316
317 // ----------------------------------------------------------------------------
318 // construction
319 // ----------------------------------------------------------------------------
320
321 wxListBox::wxListBox()
322 {
323 m_list = (GtkList *) NULL;
324 #if wxUSE_CHECKLISTBOX
325 m_hasCheckBoxes = FALSE;
326 #endif // wxUSE_CHECKLISTBOX
327 }
328
329 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
330 const wxPoint &pos, const wxSize &size,
331 int n, const wxString choices[],
332 long style, const wxValidator& validator,
333 const wxString &name )
334 {
335 m_needParent = TRUE;
336 m_acceptsFocus = TRUE;
337 m_isListBox = TRUE;
338 m_prevSelection = 0; // or -1 ??
339 m_blockEvent = FALSE;
340
341 if (!PreCreation( parent, pos, size ) ||
342 !CreateBase( parent, id, pos, size, style, validator, name ))
343 {
344 wxFAIL_MSG( wxT("wxListBox creation failed") );
345 return FALSE;
346 }
347
348 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
349 if (style & wxLB_ALWAYS_SB)
350 {
351 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
352 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
353 }
354 else
355 {
356 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
357 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
358 }
359
360 m_list = GTK_LIST( gtk_list_new() );
361
362 GtkSelectionMode mode;
363 if (style & wxLB_MULTIPLE)
364 {
365 mode = GTK_SELECTION_MULTIPLE;
366 }
367 else if (style & wxLB_EXTENDED)
368 {
369 mode = GTK_SELECTION_EXTENDED;
370 }
371 else
372 {
373 // if style was 0 set single mode
374 m_windowStyle |= wxLB_SINGLE;
375 mode = GTK_SELECTION_MULTIPLE;
376 }
377
378 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
379
380 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
381
382 /* make list scroll when moving the focus down using cursor keys */
383 gtk_container_set_focus_vadjustment(
384 GTK_CONTAINER(m_list),
385 gtk_scrolled_window_get_vadjustment(
386 GTK_SCROLLED_WINDOW(m_widget)));
387
388 gtk_widget_show( GTK_WIDGET(m_list) );
389
390 if ( style & wxLB_SORT )
391 {
392 // this will change DoAppend() behaviour
393 m_strings = new wxSortedArrayString;
394 }
395 else
396 {
397 m_strings = (wxSortedArrayString *)NULL;
398 }
399
400 for (int i = 0; i < n; i++)
401 {
402 // add one by one
403 DoAppend(choices[i]);
404 }
405
406 // call it after appending the strings to the listbox, otherwise it doesn't
407 // work correctly
408 SetBestSize( size );
409
410 m_parent->DoAddChild( this );
411
412 PostCreation();
413
414 SetBackgroundColour( wxSystemSettings::GetColour( 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 ( 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, str.mbc_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 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
759
760 return str;
761 }
762
763 wxFAIL_MSG(wxT("wrong listbox index"));
764
765 return wxT("");
766 }
767
768 int wxListBox::GetCount() const
769 {
770 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
771
772 GList *children = m_list->children;
773 return g_list_length(children);
774 }
775
776 int wxListBox::FindString( const wxString &item ) const
777 {
778 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
779
780 GList *child = m_list->children;
781 int count = 0;
782 while (child)
783 {
784 GtkBin *bin = GTK_BIN( child->data );
785 GtkLabel *label = GTK_LABEL( bin->child );
786
787 wxString str = wxString(GET_REAL_LABEL(label->label),*wxConvCurrent);
788
789 if (str == item)
790 return count;
791
792 count++;
793 child = child->next;
794 }
795
796 // it's not an error if the string is not found -> no wxCHECK
797
798 return wxNOT_FOUND;
799 }
800
801 // ----------------------------------------------------------------------------
802 // selection
803 // ----------------------------------------------------------------------------
804
805 int wxListBox::GetSelection() const
806 {
807 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
808
809 GList *child = m_list->children;
810 int count = 0;
811 while (child)
812 {
813 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
814 count++;
815 child = child->next;
816 }
817 return -1;
818 }
819
820 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
821 {
822 wxCHECK_MSG( m_list != NULL, -1, wxT("invalid listbox") );
823
824 // get the number of selected items first
825 GList *child = m_list->children;
826 int count = 0;
827 for (child = m_list->children; child != NULL; child = child->next)
828 {
829 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
830 count++;
831 }
832
833 aSelections.Empty();
834
835 if (count > 0)
836 {
837 // now fill the list
838 aSelections.Alloc(count); // optimization attempt
839 int i = 0;
840 for (child = m_list->children; child != NULL; child = child->next, i++)
841 {
842 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
843 aSelections.Add(i);
844 }
845 }
846
847 return count;
848 }
849
850 bool wxListBox::IsSelected( int n ) const
851 {
852 wxCHECK_MSG( m_list != NULL, FALSE, wxT("invalid listbox") );
853
854 GList *target = g_list_nth( m_list->children, n );
855
856 wxCHECK_MSG( target, FALSE, wxT("invalid listbox index") );
857
858 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
859 }
860
861 void wxListBox::SetSelection( int n, bool select )
862 {
863 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
864
865 m_blockEvent = TRUE;
866
867 if (select)
868 {
869 if ((m_windowStyle & wxLB_SINGLE) != 0)
870 gtk_list_unselect_item( m_list, m_prevSelection );
871 gtk_list_select_item( m_list, n );
872 m_prevSelection = n;
873 }
874 else
875 gtk_list_unselect_item( m_list, n );
876
877 m_blockEvent = FALSE;
878 }
879
880 void wxListBox::DoSetFirstItem( int n )
881 {
882 wxCHECK_RET( m_list, wxT("invalid listbox") );
883
884 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
885 return;
886
887 // terribly efficient
888 const gchar *vadjustment_key = "gtk-vadjustment";
889 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
890
891 GtkAdjustment *adjustment =
892 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
893 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
894
895 GList *target = g_list_nth( m_list->children, n );
896 wxCHECK_RET( target, wxT("invalid listbox index") );
897
898 GtkWidget *item = GTK_WIDGET(target->data);
899 wxCHECK_RET( item, wxT("invalid listbox code") );
900
901 if (item->allocation.y == -1)
902 {
903 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
904 data->m_listbox = this;
905 data->m_item = n;
906 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
907
908 return;
909 }
910
911 float y = item->allocation.y;
912 if (y > adjustment->upper - adjustment->page_size)
913 y = adjustment->upper - adjustment->page_size;
914 gtk_adjustment_set_value( adjustment, y );
915 }
916
917 // ----------------------------------------------------------------------------
918 // helpers
919 // ----------------------------------------------------------------------------
920
921 int wxListBox::GtkGetIndex( GtkWidget *item ) const
922 {
923 if (item)
924 {
925 GList *child = m_list->children;
926 int count = 0;
927 while (child)
928 {
929 if (GTK_WIDGET(child->data) == item) return count;
930 count++;
931 child = child->next;
932 }
933 }
934 return -1;
935 }
936
937 #if wxUSE_TOOLTIPS
938 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
939 {
940 GList *child = m_list->children;
941 while (child)
942 {
943 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
944 child = child->next;
945 }
946 }
947 #endif // wxUSE_TOOLTIPS
948
949 GtkWidget *wxListBox::GetConnectWidget()
950 {
951 return GTK_WIDGET(m_list);
952 }
953
954 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
955 {
956 if (GTK_WIDGET(m_list)->window == window) return TRUE;
957
958 GList *child = m_list->children;
959 while (child)
960 {
961 GtkWidget *bin = GTK_WIDGET( child->data );
962 if (bin->window == window) return TRUE;
963 child = child->next;
964 }
965
966 return FALSE;
967 }
968
969 void wxListBox::ApplyWidgetStyle()
970 {
971 SetWidgetStyle();
972
973 if (m_backgroundColour.Ok())
974 {
975 GdkWindow *window = GTK_WIDGET(m_list)->window;
976 if ( window )
977 {
978 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
979 gdk_window_set_background( window, m_backgroundColour.GetColor() );
980 gdk_window_clear( window );
981 }
982 }
983
984 GList *child = m_list->children;
985 while (child)
986 {
987 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
988
989 GtkBin *bin = GTK_BIN( child->data );
990 GtkWidget *label = GTK_WIDGET( bin->child );
991 gtk_widget_set_style( label, m_widgetStyle );
992
993 child = child->next;
994 }
995 }
996
997 void wxListBox::OnInternalIdle()
998 {
999 wxCursor cursor = m_cursor;
1000 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1001
1002 if (GTK_WIDGET(m_list)->window && cursor.Ok())
1003 {
1004 /* I now set the cursor the anew in every OnInternalIdle call
1005 as setting the cursor in a parent window also effects the
1006 windows above so that checking for the current cursor is
1007 not possible. */
1008
1009 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
1010
1011 GList *child = m_list->children;
1012 while (child)
1013 {
1014 GtkBin *bin = GTK_BIN( child->data );
1015 GtkWidget *label = GTK_WIDGET( bin->child );
1016
1017 if (!label->window)
1018 break;
1019 else
1020 gdk_window_set_cursor( label->window, cursor.GetCursor() );
1021
1022 child = child->next;
1023 }
1024 }
1025
1026 UpdateWindowUI();
1027 }
1028
1029 wxSize wxListBox::DoGetBestSize() const
1030 {
1031 int lbWidth = 100; // some defaults
1032 int lbHeight = 110;
1033 int wLine;
1034
1035 // Find the widest line
1036 for(int i = 0; i < GetCount(); i++) {
1037 wxString str(GetString(i));
1038 GetTextExtent(str, &wLine, NULL);
1039 lbWidth = wxMax(lbWidth, wLine);
1040 }
1041
1042 // Add room for the scrollbar
1043 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1044
1045 // And just a bit more
1046 int cx, cy;
1047 GetTextExtent("X", &cx, &cy);
1048 lbWidth += 3 * cx;
1049
1050 // don't make the listbox too tall (limit height to around 10 items) but don't
1051 // make it too small neither
1052 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1053
1054 return wxSize(lbWidth, lbHeight);
1055 }
1056
1057 #endif