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