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