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