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