]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/listbox.cpp
some feeble attempts at making wxBeginBusyCursor work
[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/dynarray.h"
16#include "wx/listbox.h"
17#include "wx/utils.h"
18#include "wx/intl.h"
19#include "wx/checklst.h"
20
21#if wxUSE_TOOLTIPS
22#include "wx/tooltip.h"
23#endif
24
25#if wxUSE_DRAG_AND_DROP
26#include "wx/dnd.h"
27#endif
28
29#include "gdk/gdk.h"
30#include "gtk/gtk.h"
31
32//-------------------------------------------------------------------------
33// conditional compilation
34//-------------------------------------------------------------------------
35
36#if (GTK_MINOR_VERSION == 1)
37#if (GTK_MICRO_VERSION >= 5)
38#define NEW_GTK_SCROLL_CODE
39#endif
40#endif
41
42//-----------------------------------------------------------------------------
43// data
44//-----------------------------------------------------------------------------
45
46extern bool g_blockEventsOnDrag;
47extern bool g_blockEventsOnScroll;
48
49//-----------------------------------------------------------------------------
50// "button_press_event"
51//-----------------------------------------------------------------------------
52
53static gint
54gtk_listbox_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxListBox *listbox )
55{
56 if (g_blockEventsOnDrag) return FALSE;
57 if (g_blockEventsOnScroll) return FALSE;
58
59 if (!listbox->HasVMT()) return FALSE;
60
61 int sel = listbox->GetIndex( widget );
62
63 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
64 {
65 wxCheckListBox *clb = (wxCheckListBox *)listbox;
66
67 clb->Check( sel, !clb->IsChecked(sel) );
68
69 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
70 event.SetEventObject( listbox );
71 event.SetInt( sel );
72 listbox->GetEventHandler()->ProcessEvent( event );
73 }
74
75 if (gdk_event->type == GDK_2BUTTON_PRESS)
76 {
77 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
78 event.SetEventObject( listbox );
79
80 wxArrayInt aSelections;
81 int count = listbox->GetSelections(aSelections);
82 if ( count > 0 )
83 {
84 event.m_commandInt = aSelections[0] ;
85 event.m_clientData = listbox->GetClientData( event.m_commandInt );
86 wxString str(listbox->GetString(event.m_commandInt));
87 if (str != "") event.m_commandString = copystring((char *)(const char *)str);
88 }
89 else
90 {
91 event.m_commandInt = -1 ;
92 event.m_commandString = copystring("") ;
93 }
94
95 listbox->GetEventHandler()->ProcessEvent( event );
96
97 if (event.m_commandString) delete[] event.m_commandString ;
98 }
99
100 return FALSE;
101}
102
103//-----------------------------------------------------------------------------
104// "key_press_event"
105//-----------------------------------------------------------------------------
106
107static gint
108gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
109{
110 if (g_blockEventsOnDrag) return FALSE;
111
112 if (!listbox->HasVMT()) return FALSE;
113
114 if (gdk_event->keyval != ' ') return FALSE;
115
116 int sel = listbox->GetIndex( widget );
117
118 wxCheckListBox *clb = (wxCheckListBox *)listbox;
119
120 clb->Check( sel, !clb->IsChecked(sel) );
121
122 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
123 event.SetEventObject( listbox );
124 event.SetInt( sel );
125 listbox->GetEventHandler()->ProcessEvent( event );
126
127 return FALSE;
128}
129
130//-----------------------------------------------------------------------------
131// "select" and "deselect"
132//-----------------------------------------------------------------------------
133
134static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
135{
136 if (!listbox->HasVMT()) return;
137 if (g_blockEventsOnDrag) return;
138
139 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
140
141 wxArrayInt aSelections;
142 int count = listbox->GetSelections(aSelections);
143 if ( count > 0 )
144 {
145 event.m_commandInt = aSelections[0] ;
146 event.m_clientData = listbox->GetClientData( event.m_commandInt );
147 wxString str(listbox->GetString(event.m_commandInt));
148 if (str != "") event.m_commandString = copystring((char *)(const char *)str);
149 }
150 else
151 {
152 event.m_commandInt = -1 ;
153 event.m_commandString = copystring("") ;
154 }
155
156 event.SetEventObject( listbox );
157
158 listbox->GetEventHandler()->ProcessEvent( event );
159 if (event.m_commandString) delete[] event.m_commandString ;
160}
161
162//-----------------------------------------------------------------------------
163// wxListBox
164//-----------------------------------------------------------------------------
165
166IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
167
168wxListBox::wxListBox()
169{
170 m_list = (GtkList *) NULL;
171 m_hasCheckBoxes = FALSE;
172}
173
174bool wxListBox::Create( wxWindow *parent, wxWindowID id,
175 const wxPoint &pos, const wxSize &size,
176 int n, const wxString choices[],
177 long style, const wxValidator& validator, const wxString &name )
178{
179 m_needParent = TRUE;
180 m_acceptsFocus = TRUE;
181
182 PreCreation( parent, id, pos, size, style, name );
183
184 SetValidator( validator );
185
186 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
187 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
188 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
189
190 m_list = GTK_LIST( gtk_list_new() );
191
192 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
193 if (style & wxLB_MULTIPLE)
194 mode = GTK_SELECTION_MULTIPLE;
195 else if (style & wxLB_EXTENDED)
196 mode = GTK_SELECTION_EXTENDED;
197
198 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
199
200#ifdef NEW_GTK_SCROLL_CODE
201 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
202#else
203 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
204#endif
205
206#ifdef __WXDEBUG__
207 debug_focus_in( m_widget, "wxListBox::m_widget", name );
208
209 debug_focus_in( GTK_WIDGET(m_list), "wxListBox::m_list", name );
210
211 GtkScrolledWindow *s_window = GTK_SCROLLED_WINDOW(m_widget);
212
213 debug_focus_in( s_window->hscrollbar, "wxWindow::hsrcollbar", name );
214 debug_focus_in( s_window->vscrollbar, "wxWindow::vsrcollbar", name );
215
216#ifdef NEW_GTK_SCROLL_CODE
217 GtkViewport *viewport = GTK_VIEWPORT(s_window->child);
218#else
219 GtkViewport *viewport = GTK_VIEWPORT(s_window->viewport);
220#endif
221
222 debug_focus_in( GTK_WIDGET(viewport), "wxWindow::viewport", name );
223#endif
224
225 gtk_widget_show( GTK_WIDGET(m_list) );
226
227 wxSize newSize = size;
228 if (newSize.x == -1) newSize.x = 100;
229 if (newSize.y == -1) newSize.y = 110;
230 SetSize( newSize.x, newSize.y );
231
232 for (int i = 0; i < n; i++)
233 {
234 m_clientDataList.Append( (wxObject*) NULL );
235 m_clientObjectList.Append( (wxObject*) NULL );
236
237 GtkWidget *list_item;
238
239 if (m_hasCheckBoxes)
240 {
241 wxString str = "[-] ";
242 str += choices[i];
243 list_item = gtk_list_item_new_with_label( str );
244 }
245 else
246 {
247 list_item = gtk_list_item_new_with_label( choices[i] );
248 }
249
250#ifdef __WXDEBUG__
251 debug_focus_in( list_item, "wxListBox::list_item", name );
252#endif
253
254 gtk_container_add( GTK_CONTAINER(m_list), list_item );
255
256 gtk_signal_connect( GTK_OBJECT(list_item), "select",
257 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
258
259 if (style & wxLB_MULTIPLE)
260 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
261 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
262
263 gtk_signal_connect( GTK_OBJECT(list_item),
264 "button_press_event",
265 (GtkSignalFunc)gtk_listbox_button_press_callback,
266 (gpointer) this );
267
268 if (m_hasCheckBoxes)
269 {
270 gtk_signal_connect( GTK_OBJECT(list_item),
271 "key_press_event",
272 (GtkSignalFunc)gtk_listbox_key_press_callback,
273 (gpointer)this );
274 }
275
276 ConnectWidget( list_item );
277
278 gtk_widget_show( list_item );
279 }
280
281 m_parent->AddChild( this );
282
283 (m_parent->m_insertCallback)( m_parent, this );
284
285 PostCreation();
286
287 gtk_widget_realize( GTK_WIDGET(m_list) );
288
289 SetBackgroundColour( parent->GetBackgroundColour() );
290 SetForegroundColour( parent->GetForegroundColour() );
291 SetFont( parent->GetFont() );
292
293 Show( TRUE );
294
295 return TRUE;
296}
297
298wxListBox::~wxListBox()
299{
300 Clear();
301}
302
303void wxListBox::AppendCommon( const wxString &item )
304{
305 wxCHECK_RET( m_list != NULL, "invalid listbox" );
306
307 GtkWidget *list_item;
308
309 if (m_hasCheckBoxes)
310 {
311 wxString str = "[-] ";
312 str += item;
313 list_item = gtk_list_item_new_with_label( str );
314 }
315 else
316 {
317 list_item = gtk_list_item_new_with_label( item );
318 }
319
320 gtk_container_add( GTK_CONTAINER(m_list), list_item );
321
322 gtk_signal_connect( GTK_OBJECT(list_item), "select",
323 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
324
325 if (GetWindowStyleFlag() & wxLB_MULTIPLE)
326 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
327 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
328
329 if (m_widgetStyle) ApplyWidgetStyle();
330
331 gtk_signal_connect( GTK_OBJECT(list_item),
332 "button_press_event",
333 (GtkSignalFunc)gtk_listbox_button_press_callback,
334 (gpointer) this );
335
336 if (m_hasCheckBoxes)
337 {
338 gtk_signal_connect( GTK_OBJECT(list_item),
339 "key_press_event",
340 (GtkSignalFunc)gtk_listbox_key_press_callback,
341 (gpointer)this );
342 }
343
344 gtk_widget_show( list_item );
345
346 ConnectWidget( list_item );
347
348#if wxUSE_DRAG_AND_DROP
349#ifndef NEW_GTK_DND_CODE
350 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
351#endif
352#endif
353
354#if wxUSE_TOOLTIPS
355 if (m_toolTip) m_toolTip->Apply( this );
356#endif
357}
358
359void wxListBox::Append( const wxString &item )
360{
361 m_clientDataList.Append( (wxObject*) NULL );
362 m_clientObjectList.Append( (wxObject*) NULL );
363
364 AppendCommon( item );
365}
366
367void wxListBox::Append( const wxString &item, void *clientData )
368{
369 m_clientDataList.Append( (wxObject*) clientData );
370 m_clientObjectList.Append( (wxObject*) NULL );
371
372 AppendCommon( item );
373}
374
375void wxListBox::Append( const wxString &item, wxClientData *clientData )
376{
377 m_clientObjectList.Append( (wxObject*) clientData );
378 m_clientDataList.Append( (wxObject*) NULL );
379
380 AppendCommon( item );
381}
382
383void wxListBox::SetClientData( int n, void* clientData )
384{
385 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
386
387 wxNode *node = m_clientDataList.Nth( n );
388 if (!node) return;
389
390 node->SetData( (wxObject*) clientData );
391}
392
393void* wxListBox::GetClientData( int n )
394{
395 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
396
397 wxNode *node = m_clientDataList.Nth( n );
398 if (!node) return NULL;
399
400 return node->Data();
401}
402
403void wxListBox::SetClientObject( int n, wxClientData* clientData )
404{
405 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
406
407 wxNode *node = m_clientObjectList.Nth( n );
408 if (!node) return;
409
410 wxClientData *cd = (wxClientData*) node->Data();
411 if (cd) delete cd;
412
413 node->SetData( (wxObject*) clientData );
414}
415
416wxClientData* wxListBox::GetClientObject( int n )
417{
418 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
419
420 wxNode *node = m_clientObjectList.Nth( n );
421 if (!node) return (wxClientData*) NULL;
422
423 return (wxClientData*) node->Data();
424}
425
426void wxListBox::Clear()
427{
428 wxCHECK_RET( m_list != NULL, "invalid listbox" );
429
430 gtk_list_clear_items( m_list, 0, Number() );
431
432 wxNode *node = m_clientObjectList.First();
433 while (node)
434 {
435 wxClientData *cd = (wxClientData*)node->Data();
436 if (cd) delete cd;
437 node = node->Next();
438 }
439 m_clientObjectList.Clear();
440
441 m_clientDataList.Clear();
442}
443
444void wxListBox::Delete( int n )
445{
446 wxCHECK_RET( m_list != NULL, "invalid listbox" );
447
448 GList *child = g_list_nth( m_list->children, n );
449
450 wxCHECK_RET( child, "wrong listbox index" );
451
452 GList *list = g_list_append( (GList*) NULL, child->data );
453 gtk_list_remove_items( m_list, list );
454 g_list_free( list );
455
456 wxNode *node = m_clientObjectList.Nth( n );
457 if (node)
458 {
459 wxClientData *cd = (wxClientData*)node->Data();
460 if (cd) delete cd;
461 m_clientObjectList.DeleteNode( node );
462 }
463
464 node = m_clientDataList.Nth( n );
465 if (node)
466 {
467 m_clientDataList.DeleteNode( node );
468 }
469}
470
471void wxListBox::Deselect( int n )
472{
473 wxCHECK_RET( m_list != NULL, "invalid listbox" );
474
475 gtk_list_unselect_item( m_list, n );
476}
477
478int wxListBox::FindString( const wxString &item ) const
479{
480 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
481
482 GList *child = m_list->children;
483 int count = 0;
484 while (child)
485 {
486 GtkBin *bin = GTK_BIN( child->data );
487 GtkLabel *label = GTK_LABEL( bin->child );
488
489 wxString str = label->label;
490 if (m_hasCheckBoxes) str.Remove( 0, 4 );
491
492 if (str == item) return count;
493
494 count++;
495 child = child->next;
496 }
497
498 // it's not an error if the string is not found -> no wxCHECK
499
500 return -1;
501}
502
503int wxListBox::GetSelection() const
504{
505 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
506
507 GList *child = m_list->children;
508 int count = 0;
509 while (child)
510 {
511 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
512 count++;
513 child = child->next;
514 }
515 return -1;
516}
517
518int wxListBox::GetSelections( wxArrayInt& aSelections ) const
519{
520 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
521
522 // get the number of selected items first
523 GList *child = m_list->children;
524 int count = 0;
525 for (child = m_list->children; child != NULL; child = child->next)
526 {
527 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
528 count++;
529 }
530
531 aSelections.Empty();
532
533 if (count > 0)
534 {
535 // now fill the list
536 aSelections.Alloc(count); // optimization attempt
537 int i = 0;
538 for (child = m_list->children; child != NULL; child = child->next, i++)
539 {
540 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
541 aSelections.Add(i);
542 }
543 }
544
545 return count;
546}
547
548wxString wxListBox::GetString( int n ) const
549{
550 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
551
552 GList *child = g_list_nth( m_list->children, n );
553 if (child)
554 {
555 GtkBin *bin = GTK_BIN( child->data );
556 GtkLabel *label = GTK_LABEL( bin->child );
557
558 wxString str = label->label;
559 if (m_hasCheckBoxes) str.Remove( 0, 4 );
560
561 return str;
562 }
563 wxFAIL_MSG("wrong listbox index");
564 return "";
565}
566
567wxString wxListBox::GetStringSelection() const
568{
569 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
570
571 GList *selection = m_list->selection;
572 if (selection)
573 {
574 GtkBin *bin = GTK_BIN( selection->data );
575 GtkLabel *label = GTK_LABEL( bin->child );
576
577 wxString str = label->label;
578 if (m_hasCheckBoxes) str.Remove( 0, 4 );
579
580 return str;
581 }
582
583 wxFAIL_MSG("no listbox selection available");
584 return "";
585}
586
587int wxListBox::Number()
588{
589 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
590
591 GList *child = m_list->children;
592 int count = 0;
593 while (child) { count++; child = child->next; }
594 return count;
595}
596
597bool wxListBox::Selected( int n )
598{
599 wxCHECK_MSG( m_list != NULL, FALSE, "invalid listbox" );
600
601 GList *target = g_list_nth( m_list->children, n );
602 if (target)
603 {
604 GList *child = m_list->selection;
605 while (child)
606 {
607 if (child->data == target->data) return TRUE;
608 child = child->next;
609 }
610 }
611 wxFAIL_MSG("wrong listbox index");
612 return FALSE;
613}
614
615void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
616{
617 wxFAIL_MSG("wxListBox::Set not implemented");
618}
619
620void wxListBox::SetFirstItem( int WXUNUSED(n) )
621{
622 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
623}
624
625void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
626{
627 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
628}
629
630void wxListBox::SetSelection( int n, bool select )
631{
632 wxCHECK_RET( m_list != NULL, "invalid listbox" );
633
634 if (select)
635 gtk_list_select_item( m_list, n );
636 else
637 gtk_list_unselect_item( m_list, n );
638}
639
640void wxListBox::SetString( int n, const wxString &string )
641{
642 wxCHECK_RET( m_list != NULL, "invalid listbox" );
643
644 GList *child = g_list_nth( m_list->children, n );
645 if (child)
646 {
647 GtkBin *bin = GTK_BIN( child->data );
648 GtkLabel *label = GTK_LABEL( bin->child );
649
650 wxString str;
651 if (m_hasCheckBoxes) str += "[-] ";
652 str += string;
653
654 gtk_label_set( label, str );
655 }
656 else
657 {
658 wxFAIL_MSG("wrong listbox index");
659 }
660}
661
662void wxListBox::SetStringSelection( const wxString &string, bool select )
663{
664 wxCHECK_RET( m_list != NULL, "invalid listbox" );
665
666 SetSelection( FindString(string), select );
667}
668
669int wxListBox::GetIndex( GtkWidget *item ) const
670{
671 if (item)
672 {
673 GList *child = m_list->children;
674 int count = 0;
675 while (child)
676 {
677 if (GTK_WIDGET(child->data) == item) return count;
678 count++;
679 child = child->next;
680 }
681 }
682 return -1;
683}
684
685#if wxUSE_TOOLTIPS
686void wxListBox::ApplyToolTip( GtkTooltips *tips, const char *tip )
687{
688 GList *child = m_list->children;
689 while (child)
690 {
691 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), tip, (gchar*) NULL );
692 child = child->next;
693 }
694}
695#endif // wxUSE_TOOLTIPS
696
697#if wxUSE_DRAG_AND_DROP
698void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
699{
700 wxCHECK_RET( m_list != NULL, "invalid listbox" );
701
702#ifndef NEW_GTK_DND_CODE
703 if (m_dropTarget)
704 {
705 GList *child = m_list->children;
706 while (child)
707 {
708 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
709 child = child->next;
710 }
711 }
712#endif
713
714 wxWindow::SetDropTarget( dropTarget );
715
716#ifndef NEW_GTK_DND_CODE
717 if (m_dropTarget)
718 {
719 GList *child = m_list->children;
720 while (child)
721 {
722 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
723 child = child->next;
724 }
725 }
726#endif
727}
728#endif
729
730GtkWidget *wxListBox::GetConnectWidget()
731{
732 return GTK_WIDGET(m_list);
733}
734
735bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
736{
737 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
738
739 GList *child = m_list->children;
740 while (child)
741 {
742 GtkWidget *bin = GTK_WIDGET( child->data );
743 if (bin->window == window) return TRUE;
744 child = child->next;
745 }
746
747 return FALSE;
748}
749
750void wxListBox::ApplyWidgetStyle()
751{
752 SetWidgetStyle();
753
754 if (m_backgroundColour.Ok())
755 {
756 GdkWindow *window = GTK_WIDGET(m_list)->window;
757 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
758 gdk_window_set_background( window, m_backgroundColour.GetColor() );
759 gdk_window_clear( window );
760 }
761
762 GList *child = m_list->children;
763 while (child)
764 {
765 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
766
767 GtkBin *bin = GTK_BIN( child->data );
768 GtkWidget *label = GTK_WIDGET( bin->child );
769 gtk_widget_set_style( label, m_widgetStyle );
770
771 child = child->next;
772 }
773}
774