]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/listbox.cpp
moved XML classes to the core
[wxWidgets.git] / src / gtk1 / listbox.cpp
... / ...
CommitLineData
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/defs.h"
16
17#if wxUSE_LISTBOX
18
19#include "wx/listbox.h"
20#include "wx/dynarray.h"
21#include "wx/arrstr.h"
22#include "wx/utils.h"
23#include "wx/intl.h"
24#include "wx/checklst.h"
25#include "wx/settings.h"
26#include "wx/gtk/private.h"
27
28#if wxUSE_TOOLTIPS
29#include "wx/tooltip.h"
30#endif
31
32#include <gdk/gdk.h>
33#include <gtk/gtk.h>
34#include <gdk/gdkkeysyms.h>
35
36//-----------------------------------------------------------------------------
37// idle system
38//-----------------------------------------------------------------------------
39
40extern void wxapp_install_idle_handler();
41extern bool g_isIdle;
42
43//-----------------------------------------------------------------------------
44// data
45//-----------------------------------------------------------------------------
46
47extern bool g_blockEventsOnDrag;
48extern bool g_blockEventsOnScroll;
49extern wxCursor g_globalCursor;
50extern wxWindowGTK *g_delayedFocus;
51
52static bool g_hasDoubleClicked = FALSE;
53
54//-----------------------------------------------------------------------------
55// idle callback for SetFirstItem
56//-----------------------------------------------------------------------------
57
58struct wxlistbox_idle_struct
59{
60 wxListBox *m_listbox;
61 int m_item;
62 gint m_tag;
63};
64
65extern "C" gint wxlistbox_idle_callback( gpointer gdata )
66{
67 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
68 gdk_threads_enter();
69
70 gtk_idle_remove( data->m_tag );
71
72 // check that the items haven't been deleted from the listbox since we had
73 // installed this callback
74 wxListBox *lbox = data->m_listbox;
75 if ( data->m_item < lbox->GetCount() )
76 {
77 lbox->SetFirstItem( data->m_item );
78 }
79
80 delete data;
81
82 gdk_threads_leave();
83
84 return TRUE;
85}
86
87//-----------------------------------------------------------------------------
88// "button_release_event"
89//-----------------------------------------------------------------------------
90
91/* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
92 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
93 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
94 this can lead to race conditions so that we emit the dclick event
95 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
96
97static gint
98gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
99 GdkEventButton * WXUNUSED(gdk_event),
100 wxListBox *listbox )
101{
102 if (g_isIdle) wxapp_install_idle_handler();
103
104 if (g_blockEventsOnDrag) return FALSE;
105 if (g_blockEventsOnScroll) return FALSE;
106
107 if (!listbox->m_hasVMT) return FALSE;
108
109 if (!g_hasDoubleClicked) return FALSE;
110
111 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
112 event.SetEventObject( listbox );
113
114 wxArrayInt aSelections;
115 int n, count = listbox->GetSelections(aSelections);
116 if ( count > 0 )
117 {
118 n = aSelections[0];
119 if ( listbox->HasClientObjectData() )
120 event.SetClientObject( listbox->GetClientObject(n) );
121 else if ( listbox->HasClientUntypedData() )
122 event.SetClientData( listbox->GetClientData(n) );
123 event.SetString( listbox->GetString(n) );
124 }
125 else
126 {
127 n = -1;
128 }
129
130 event.m_commandInt = n;
131
132 listbox->GetEventHandler()->ProcessEvent( event );
133
134 return FALSE;
135}
136
137//-----------------------------------------------------------------------------
138// "button_press_event"
139//-----------------------------------------------------------------------------
140
141static gint
142gtk_listbox_button_press_callback( GtkWidget *widget,
143 GdkEventButton *gdk_event,
144 wxListBox *listbox )
145{
146 if (g_isIdle) wxapp_install_idle_handler();
147
148 if (g_blockEventsOnDrag) return FALSE;
149 if (g_blockEventsOnScroll) return FALSE;
150
151 if (!listbox->m_hasVMT) return FALSE;
152
153 int sel = listbox->GtkGetIndex( widget );
154
155#if wxUSE_CHECKLISTBOX
156 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
157 {
158 wxCheckListBox *clb = (wxCheckListBox *)listbox;
159
160 clb->Check( sel, !clb->IsChecked(sel) );
161
162 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
163 event.SetEventObject( listbox );
164 event.SetInt( sel );
165 listbox->GetEventHandler()->ProcessEvent( event );
166 }
167#endif // wxUSE_CHECKLISTBOX
168
169 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
170 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
171
172 return FALSE;
173}
174
175//-----------------------------------------------------------------------------
176// "key_press_event"
177//-----------------------------------------------------------------------------
178
179static gint
180gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
181{
182 if (g_isIdle)
183 wxapp_install_idle_handler();
184
185 if (g_blockEventsOnDrag)
186 return FALSE;
187
188 bool ret = FALSE;
189
190 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
191 {
192 wxNavigationKeyEvent new_event;
193 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
194 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
195 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
196 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
197 new_event.SetCurrentFocus( listbox );
198 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
199 }
200
201 if ((gdk_event->keyval == GDK_Return) && (!ret))
202 {
203 // eat return in all modes
204 ret = TRUE;
205 }
206
207#if wxUSE_CHECKLISTBOX
208 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
209 {
210 int sel = listbox->GtkGetIndex( widget );
211
212 wxCheckListBox *clb = (wxCheckListBox *)listbox;
213
214 clb->Check( sel, !clb->IsChecked(sel) );
215
216 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
217 new_event.SetEventObject( listbox );
218 new_event.SetInt( sel );
219 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
220 }
221#endif // wxUSE_CHECKLISTBOX
222
223 if (ret)
224 {
225 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
226 return TRUE;
227 }
228
229 return FALSE;
230}
231
232//-----------------------------------------------------------------------------
233// "select" and "deselect"
234//-----------------------------------------------------------------------------
235
236static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
237
238static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
239{
240 gtk_listitem_select_cb( widget, listbox, TRUE );
241}
242
243static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
244{
245 gtk_listitem_select_cb( widget, listbox, FALSE );
246}
247
248static void gtk_listitem_select_cb( GtkWidget *widget,
249 wxListBox *listbox,
250 bool is_selection )
251{
252 if (g_isIdle) wxapp_install_idle_handler();
253
254 if (!listbox->m_hasVMT) return;
255 if (g_blockEventsOnDrag) return;
256
257 if (listbox->m_blockEvent) return;
258
259 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
260 event.SetEventObject( listbox );
261
262 // indicate whether this is a selection or a deselection
263 event.SetExtraLong( is_selection );
264
265 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
266 {
267 int sel = listbox->GtkGetIndex( widget );
268
269 if (listbox->m_prevSelection != sel)
270 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
271
272 listbox->m_prevSelection = sel;
273 }
274
275 wxArrayInt aSelections;
276 int n, count = listbox->GetSelections(aSelections);
277 if ( count > 0 )
278 {
279 n = aSelections[0];
280 if ( listbox->HasClientObjectData() )
281 event.SetClientObject( listbox->GetClientObject(n) );
282 else if ( listbox->HasClientUntypedData() )
283 event.SetClientData( listbox->GetClientData(n) );
284 event.SetString( listbox->GetString(n) );
285 }
286 else
287 {
288 n = -1;
289 }
290
291 event.m_commandInt = n;
292
293// No longer required with new code in wxLB_SINGLE
294// listbox->GetEventHandler()->AddPendingEvent( event );
295 listbox->GetEventHandler()->ProcessEvent( event );
296}
297
298//-----------------------------------------------------------------------------
299// wxListBox
300//-----------------------------------------------------------------------------
301
302IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
303
304// ----------------------------------------------------------------------------
305// construction
306// ----------------------------------------------------------------------------
307
308wxListBox::wxListBox()
309{
310 m_list = (GtkList *) NULL;
311#if wxUSE_CHECKLISTBOX
312 m_hasCheckBoxes = FALSE;
313#endif // wxUSE_CHECKLISTBOX
314}
315
316bool wxListBox::Create( wxWindow *parent, wxWindowID id,
317 const wxPoint &pos, const wxSize &size,
318 int n, const wxString choices[],
319 long style, const wxValidator& validator,
320 const wxString &name )
321{
322 m_needParent = TRUE;
323 m_acceptsFocus = TRUE;
324 m_prevSelection = 0; // or -1 ??
325 m_blockEvent = FALSE;
326
327 if (!PreCreation( parent, pos, size ) ||
328 !CreateBase( parent, id, pos, size, style, validator, name ))
329 {
330 wxFAIL_MSG( wxT("wxListBox creation failed") );
331 return FALSE;
332 }
333
334 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
335 if (style & wxLB_ALWAYS_SB)
336 {
337 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
338 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
339 }
340 else
341 {
342 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
343 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
344 }
345
346 m_list = GTK_LIST( gtk_list_new() );
347
348 GtkSelectionMode mode;
349 if (style & wxLB_MULTIPLE)
350 {
351 mode = GTK_SELECTION_MULTIPLE;
352 }
353 else if (style & wxLB_EXTENDED)
354 {
355 mode = GTK_SELECTION_EXTENDED;
356 }
357 else
358 {
359 // if style was 0 set single mode
360 m_windowStyle |= wxLB_SINGLE;
361 mode = GTK_SELECTION_MULTIPLE;
362 }
363
364 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
365
366 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
367
368 /* make list scroll when moving the focus down using cursor keys */
369 gtk_container_set_focus_vadjustment(
370 GTK_CONTAINER(m_list),
371 gtk_scrolled_window_get_vadjustment(
372 GTK_SCROLLED_WINDOW(m_widget)));
373
374 gtk_widget_show( GTK_WIDGET(m_list) );
375
376 if ( style & wxLB_SORT )
377 {
378 // this will change DoAppend() behaviour
379 m_strings = new wxSortedArrayString;
380 }
381 else
382 {
383 m_strings = (wxSortedArrayString *)NULL;
384 }
385
386 for (int i = 0; i < n; i++)
387 {
388 // add one by one
389 DoAppend(choices[i]);
390 }
391
392 // call it after appending the strings to the listbox, otherwise it doesn't
393 // work correctly
394 SetBestSize( size );
395
396 m_parent->DoAddChild( this );
397
398 PostCreation();
399
400 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ) );
401 SetForegroundColour( parent->GetForegroundColour() );
402 SetFont( parent->GetFont() );
403
404 Show( TRUE );
405
406 return TRUE;
407}
408
409wxListBox::~wxListBox()
410{
411 m_hasVMT = FALSE;
412
413 Clear();
414
415 if (m_strings)
416 delete m_strings;
417}
418
419// ----------------------------------------------------------------------------
420// adding items
421// ----------------------------------------------------------------------------
422
423void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
424{
425 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
426
427 // VZ: notice that InsertItems knows nothing about sorting, so calling it
428 // from outside (and not from our own Append) is likely to break
429 // everything
430
431 // code elsewhere supposes we have as many items in m_clientList as items
432 // in the listbox
433 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
434 wxT("bug in client data management") );
435
436 GList *children = m_list->children;
437 int length = g_list_length(children);
438
439 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
440
441 size_t nItems = items.GetCount();
442 int index;
443
444 if (m_strings)
445 {
446 for (size_t n = 0; n < nItems; n++)
447 {
448 index = m_strings->Add( items[n] );
449
450 if (index != GetCount())
451 {
452 GtkAddItem( items[n], index );
453 wxList::compatibility_iterator node = m_clientList.Item( index );
454 m_clientList.Insert( node, (wxObject*) NULL );
455 }
456 else
457 {
458 GtkAddItem( items[n] );
459 m_clientList.Append( (wxObject*) NULL );
460 }
461 }
462 }
463 else
464 {
465 if (pos == length)
466 {
467 for ( size_t n = 0; n < nItems; n++ )
468 {
469 GtkAddItem( items[n] );
470
471 m_clientList.Append((wxObject *)NULL);
472 }
473 }
474 else
475 {
476 wxList::compatibility_iterator node = m_clientList.Item( pos );
477 for ( size_t n = 0; n < nItems; n++ )
478 {
479 GtkAddItem( items[n], pos+n );
480
481 m_clientList.Insert( node, (wxObject *)NULL );
482 }
483 }
484 }
485
486 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
487 wxT("bug in client data management") );
488}
489
490int wxListBox::DoAppend( const wxString& item )
491{
492 if (m_strings)
493 {
494 // need to determine the index
495 int index = m_strings->Add( item );
496
497 // only if not at the end anyway
498 if (index != GetCount())
499 {
500 GtkAddItem( item, index );
501
502 wxList::compatibility_iterator node = m_clientList.Item( index );
503 m_clientList.Insert( node, (wxObject *)NULL );
504
505 return index;
506 }
507 }
508
509 GtkAddItem(item);
510
511 m_clientList.Append((wxObject *)NULL);
512
513 return GetCount() - 1;
514}
515
516void wxListBox::GtkAddItem( const wxString &item, int pos )
517{
518 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
519
520 GtkWidget *list_item;
521
522 wxString label(item);
523#if wxUSE_CHECKLISTBOX
524 if (m_hasCheckBoxes)
525 {
526 label.Prepend(wxCHECKLBOX_STRING);
527 }
528#endif // wxUSE_CHECKLISTBOX
529
530 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
531
532 GList *gitem_list = g_list_alloc ();
533 gitem_list->data = list_item;
534
535 if (pos == -1)
536 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
537 else
538 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
539
540 gtk_signal_connect( GTK_OBJECT(list_item), "select",
541 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
542
543 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
544 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
545 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
546
547 gtk_signal_connect( GTK_OBJECT(list_item),
548 "button_press_event",
549 (GtkSignalFunc)gtk_listbox_button_press_callback,
550 (gpointer) this );
551
552 gtk_signal_connect_after( GTK_OBJECT(list_item),
553 "button_release_event",
554 (GtkSignalFunc)gtk_listbox_button_release_callback,
555 (gpointer) this );
556
557 gtk_signal_connect( GTK_OBJECT(list_item),
558 "key_press_event",
559 (GtkSignalFunc)gtk_listbox_key_press_callback,
560 (gpointer)this );
561
562 ConnectWidget( list_item );
563
564 gtk_widget_show( list_item );
565
566 if (GTK_WIDGET_REALIZED(m_widget))
567 {
568 gtk_widget_realize( list_item );
569 gtk_widget_realize( GTK_BIN(list_item)->child );
570
571 // Apply current widget style to the new list_item
572 if (m_widgetStyle)
573 {
574 gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle );
575 GtkBin *bin = GTK_BIN( list_item );
576 GtkWidget *label = GTK_WIDGET( bin->child );
577 gtk_widget_set_style( label, m_widgetStyle );
578 }
579
580#if wxUSE_TOOLTIPS
581 if (m_tooltip) m_tooltip->Apply( this );
582#endif
583 }
584}
585
586void wxListBox::DoSetItems( const wxArrayString& items,
587 void **clientData)
588{
589 Clear();
590
591 DoInsertItems(items, 0);
592
593 if ( clientData )
594 {
595 size_t count = items.GetCount();
596 for ( size_t n = 0; n < count; n++ )
597 {
598 SetClientData(n, clientData[n]);
599 }
600 }
601}
602
603// ----------------------------------------------------------------------------
604// deleting items
605// ----------------------------------------------------------------------------
606
607void wxListBox::Clear()
608{
609 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
610
611 gtk_list_clear_items( m_list, 0, GetCount() );
612
613 if ( GTK_LIST(m_list)->last_focus_child != NULL )
614 {
615 // This should be NULL, I think.
616 GTK_LIST(m_list)->last_focus_child = NULL;
617 }
618
619 if ( HasClientObjectData() )
620 {
621 // destroy the data (due to Robert's idea of using wxList<wxObject>
622 // and not wxList<wxClientData> we can't just say
623 // m_clientList.DeleteContents(TRUE) - this would crash!
624 wxList::compatibility_iterator node = m_clientList.GetFirst();
625 while ( node )
626 {
627 delete (wxClientData *)node->GetData();
628 node = node->GetNext();
629 }
630 }
631 m_clientList.Clear();
632
633 if ( m_strings )
634 m_strings->Clear();
635}
636
637void wxListBox::Delete( int n )
638{
639 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
640
641 GList *child = g_list_nth( m_list->children, n );
642
643 wxCHECK_RET( child, wxT("wrong listbox index") );
644
645 GList *list = g_list_append( (GList*) NULL, child->data );
646 gtk_list_remove_items( m_list, list );
647 g_list_free( list );
648
649 wxList::compatibility_iterator node = m_clientList.Item( n );
650 if ( node )
651 {
652 if ( m_clientDataItemsType == wxClientData_Object )
653 {
654 wxClientData *cd = (wxClientData*)node->GetData();
655 delete cd;
656 }
657
658 m_clientList.Erase( node );
659 }
660
661 if ( m_strings )
662 m_strings->RemoveAt(n);
663}
664
665// ----------------------------------------------------------------------------
666// client data
667// ----------------------------------------------------------------------------
668
669void wxListBox::DoSetItemClientData( int n, void* clientData )
670{
671 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
672
673 wxList::compatibility_iterator node = m_clientList.Item( n );
674 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
675
676 node->SetData( (wxObject*) clientData );
677}
678
679void* wxListBox::DoGetItemClientData( int n ) const
680{
681 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
682
683 wxList::compatibility_iterator node = m_clientList.Item( n );
684 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
685
686 return node->GetData();
687}
688
689void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
690{
691 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
692
693 wxList::compatibility_iterator node = m_clientList.Item( n );
694 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
695
696 // wxItemContainer already deletes data for us
697
698 node->SetData( (wxObject*) clientData );
699}
700
701wxClientData* wxListBox::DoGetItemClientObject( int n ) const
702{
703 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
704
705 wxList::compatibility_iterator node = m_clientList.Item( n );
706 wxCHECK_MSG( node, (wxClientData *)NULL,
707 wxT("invalid index in wxListBox::DoGetItemClientObject") );
708
709 return (wxClientData*) node->GetData();
710}
711
712// ----------------------------------------------------------------------------
713// string list access
714// ----------------------------------------------------------------------------
715
716wxString wxListBox::GetRealLabel(GList *item) const
717{
718 GtkBin *bin = GTK_BIN( item->data );
719 GtkLabel *label = GTK_LABEL( bin->child );
720
721 wxString str;
722
723#ifdef __WXGTK20__
724 str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
725#else
726 str = wxString( label->label );
727#endif
728
729#if wxUSE_CHECKLISTBOX
730