Header changes (gtk.h etc no longer included in defs.h
[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 #ifndef NEW_GTK_DND_CODE
323 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
324 #endif
325 }
326
327 void wxListBox::Append( const wxString &item )
328 {
329 m_clientDataList.Append( (wxObject*) NULL );
330 m_clientObjectList.Append( (wxObject*) NULL );
331
332 AppendCommon( item );
333 }
334
335 void wxListBox::Append( const wxString &item, void *clientData )
336 {
337 m_clientDataList.Append( (wxObject*) clientData );
338 m_clientObjectList.Append( (wxObject*) NULL );
339
340 AppendCommon( item );
341 }
342
343 void wxListBox::Append( const wxString &item, wxClientData *clientData )
344 {
345 m_clientObjectList.Append( (wxObject*) clientData );
346 m_clientDataList.Append( (wxObject*) NULL );
347
348 AppendCommon( item );
349 }
350
351 void wxListBox::SetClientData( int n, void* clientData )
352 {
353 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
354
355 wxNode *node = m_clientDataList.Nth( n );
356 if (!node) return;
357
358 node->SetData( (wxObject*) clientData );
359 }
360
361 void* wxListBox::GetClientData( int n )
362 {
363 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
364
365 wxNode *node = m_clientDataList.Nth( n );
366 if (!node) return NULL;
367
368 return node->Data();
369 }
370
371 void wxListBox::SetClientObject( int n, wxClientData* clientData )
372 {
373 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
374
375 wxNode *node = m_clientObjectList.Nth( n );
376 if (!node) return;
377
378 wxClientData *cd = (wxClientData*) node->Data();
379 if (cd) delete cd;
380
381 node->SetData( (wxObject*) clientData );
382 }
383
384 wxClientData* wxListBox::GetClientObject( int n )
385 {
386 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
387
388 wxNode *node = m_clientObjectList.Nth( n );
389 if (!node) return (wxClientData*) NULL;
390
391 return (wxClientData*) node->Data();
392 }
393
394 void wxListBox::Clear()
395 {
396 wxCHECK_RET( m_list != NULL, "invalid listbox" );
397
398 gtk_list_clear_items( m_list, 0, Number() );
399
400 wxNode *node = m_clientObjectList.First();
401 while (node)
402 {
403 wxClientData *cd = (wxClientData*)node->Data();
404 if (cd) delete cd;
405 node = node->Next();
406 }
407 m_clientObjectList.Clear();
408
409 m_clientDataList.Clear();
410 }
411
412 void wxListBox::Delete( int n )
413 {
414 wxCHECK_RET( m_list != NULL, "invalid listbox" );
415
416 GList *child = g_list_nth( m_list->children, n );
417
418 wxCHECK_RET( child, "wrong listbox index" );
419
420 GList *list = g_list_append( NULL, child->data );
421 gtk_list_remove_items( m_list, list );
422 g_list_free( list );
423
424 wxNode *node = m_clientObjectList.Nth( n );
425 if (node)
426 {
427 wxClientData *cd = (wxClientData*)node->Data();
428 if (cd) delete cd;
429 m_clientObjectList.DeleteNode( node );
430 }
431
432 node = m_clientDataList.Nth( n );
433 if (node)
434 {
435 m_clientDataList.DeleteNode( node );
436 }
437 }
438
439 void wxListBox::Deselect( int n )
440 {
441 wxCHECK_RET( m_list != NULL, "invalid listbox" );
442
443 gtk_list_unselect_item( m_list, n );
444 }
445
446 int wxListBox::FindString( const wxString &item ) const
447 {
448 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
449
450 GList *child = m_list->children;
451 int count = 0;
452 while (child)
453 {
454 GtkBin *bin = GTK_BIN( child->data );
455 GtkLabel *label = GTK_LABEL( bin->child );
456
457 wxString str = label->label;
458 if (m_hasCheckBoxes) str.Remove( 0, 4 );
459
460 if (str == item) return count;
461
462 count++;
463 child = child->next;
464 }
465
466 // it's not an error if the string is not found -> no wxCHECK
467
468 return -1;
469 }
470
471 int wxListBox::GetSelection() const
472 {
473 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
474
475 GList *child = m_list->children;
476 int count = 0;
477 while (child)
478 {
479 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
480 count++;
481 child = child->next;
482 }
483 return -1;
484 }
485
486 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
487 {
488 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
489
490 // get the number of selected items first
491 GList *child = m_list->children;
492 int count = 0;
493 for (child = m_list->children; child != NULL; child = child->next)
494 {
495 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
496 count++;
497 }
498
499 aSelections.Empty();
500
501 if (count > 0)
502 {
503 // now fill the list
504 aSelections.Alloc(count); // optimization attempt
505 int i = 0;
506 for (child = m_list->children; child != NULL; child = child->next, i++)
507 {
508 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
509 aSelections.Add(i);
510 }
511 }
512
513 return count;
514 }
515
516 wxString wxListBox::GetString( int n ) const
517 {
518 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
519
520 GList *child = g_list_nth( m_list->children, n );
521 if (child)
522 {
523 GtkBin *bin = GTK_BIN( child->data );
524 GtkLabel *label = GTK_LABEL( bin->child );
525
526 wxString str = label->label;
527 if (m_hasCheckBoxes) str.Remove( 0, 4 );
528
529 return str;
530 }
531 wxFAIL_MSG("wrong listbox index");
532 return "";
533 }
534
535 wxString wxListBox::GetStringSelection() const
536 {
537 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
538
539 GList *selection = m_list->selection;
540 if (selection)
541 {
542 GtkBin *bin = GTK_BIN( selection->data );
543 GtkLabel *label = GTK_LABEL( bin->child );
544
545 wxString str = label->label;
546 if (m_hasCheckBoxes) str.Remove( 0, 4 );
547
548 return str;
549 }
550
551 wxFAIL_MSG("no listbox selection available");
552 return "";
553 }
554
555 int wxListBox::Number()
556 {
557 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
558
559 GList *child = m_list->children;
560 int count = 0;
561 while (child) { count++; child = child->next; }
562 return count;
563 }
564
565 bool wxListBox::Selected( int n )
566 {
567 wxCHECK_MSG( m_list != NULL, FALSE, "invalid listbox" );
568
569 GList *target = g_list_nth( m_list->children, n );
570 if (target)
571 {
572 GList *child = m_list->selection;
573 while (child)
574 {
575 if (child->data == target->data) return TRUE;
576 child = child->next;
577 }
578 }
579 wxFAIL_MSG("wrong listbox index");
580 return FALSE;
581 }
582
583 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
584 {
585 wxFAIL_MSG("wxListBox::Set not implemented");
586 }
587
588 void wxListBox::SetFirstItem( int WXUNUSED(n) )
589 {
590 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
591 }
592
593 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
594 {
595 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
596 }
597
598 void wxListBox::SetSelection( int n, bool select )
599 {
600 wxCHECK_RET( m_list != NULL, "invalid listbox" );
601
602 if (select)
603 gtk_list_select_item( m_list, n );
604 else
605 gtk_list_unselect_item( m_list, n );
606 }
607
608 void wxListBox::SetString( int n, const wxString &string )
609 {
610 wxCHECK_RET( m_list != NULL, "invalid listbox" );
611
612 GList *child = g_list_nth( m_list->children, n );
613 if (child)
614 {
615 GtkBin *bin = GTK_BIN( child->data );
616 GtkLabel *label = GTK_LABEL( bin->child );
617
618 wxString str;
619 if (m_hasCheckBoxes) str += "[-] ";
620 str += string;
621
622 gtk_label_set( label, str );
623 }
624 else
625 {
626 wxFAIL_MSG("wrong listbox index");
627 }
628 }
629
630 void wxListBox::SetStringSelection( const wxString &string, bool select )
631 {
632 wxCHECK_RET( m_list != NULL, "invalid listbox" );
633
634 SetSelection( FindString(string), select );
635 }
636
637 int wxListBox::GetIndex( GtkWidget *item ) const
638 {
639 if (item)
640 {
641 GList *child = m_list->children;
642 int count = 0;
643 while (child)
644 {
645 if (GTK_WIDGET(child->data) == item) return count;
646 count++;
647 child = child->next;
648 }
649 }
650 return -1;
651 }
652
653 void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
654 {
655 wxCHECK_RET( m_list != NULL, "invalid listbox" );
656
657 #ifndef NEW_GTK_DND_CODE
658 if (m_dropTarget)
659 {
660 GList *child = m_list->children;
661 while (child)
662 {
663 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
664 child = child->next;
665 }
666 }
667 #endif
668
669 wxWindow::SetDropTarget( dropTarget );
670
671 #ifndef NEW_GTK_DND_CODE
672 if (m_dropTarget)
673 {
674 GList *child = m_list->children;
675 while (child)
676 {
677 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
678 child = child->next;
679 }
680 }
681 #endif
682 }
683
684 GtkWidget *wxListBox::GetConnectWidget()
685 {
686 return GTK_WIDGET(m_list);
687 }
688
689 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
690 {
691 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
692
693 GList *child = m_list->children;
694 while (child)
695 {
696 GtkWidget *bin = GTK_WIDGET( child->data );
697 if (bin->window == window) return TRUE;
698 child = child->next;
699 }
700
701 return FALSE;
702 }
703
704 void wxListBox::ApplyWidgetStyle()
705 {
706 SetWidgetStyle();
707
708 if (m_backgroundColour.Ok())
709 {
710 GdkWindow *window = GTK_WIDGET(m_list)->window;
711 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
712 gdk_window_set_background( window, m_backgroundColour.GetColor() );
713 gdk_window_clear( window );
714 }
715
716 GList *child = m_list->children;
717 while (child)
718 {
719 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
720
721 GtkBin *bin = GTK_BIN( child->data );
722 GtkWidget *label = GTK_WIDGET( bin->child );
723 gtk_widget_set_style( label, m_widgetStyle );
724
725 child = child->next;
726 }
727 }