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