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