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