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