]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
bug in wxString::Printf() corrected (the length of the string wasn't updated
[wxWidgets.git] / src / gtk1 / listbox.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: listbox.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
a3622daa 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "listbox.h"
13#endif
14
6a6d4eed 15#include "wx/dynarray.h"
c801d85f 16#include "wx/listbox.h"
09cf7c58 17#include "wx/utils.h"
caaa4cfd
RR
18#include "wx/intl.h"
19#include "wx/checklst.h"
c801d85f 20
83624f79
RR
21#include "gdk/gdk.h"
22#include "gtk/gtk.h"
23
38c7b3d3
RR
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
66bd6b93
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
caaa4cfd
RR
39extern bool g_blockEventsOnScroll;
40
41//-----------------------------------------------------------------------------
42// "button_press_event"
43//-----------------------------------------------------------------------------
44
45static gint
46gtk_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
caaa4cfd
RR
53 int sel = listbox->GetIndex( widget );
54
4f22cf8d
RR
55 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
56 {
57 wxCheckListBox *clb = (wxCheckListBox *)listbox;
caaa4cfd 58
4f22cf8d 59 clb->Check( sel, !clb->IsChecked(sel) );
caaa4cfd 60
4f22cf8d
RR
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
caaa4cfd
RR
75 return FALSE;
76}
66bd6b93 77
1144d24d
RR
78//-----------------------------------------------------------------------------
79// "key_press_event"
80//-----------------------------------------------------------------------------
81
82static gint
83gtk_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
4f22cf8d
RR
97 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
98 event.SetEventObject( listbox );
99 event.SetInt( sel );
100 listbox->GetEventHandler()->ProcessEvent( event );
101
1144d24d
RR
102 return FALSE;
103}
104
c801d85f 105//-----------------------------------------------------------------------------
a60c99e6 106// "select" and "deselect"
c801d85f
KB
107//-----------------------------------------------------------------------------
108
09cf7c58 109static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
c801d85f 110{
fd0eed64
RR
111 if (!listbox->HasVMT()) return;
112 if (g_blockEventsOnDrag) return;
dcf40a56 113
fd0eed64 114 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
dcf40a56 115
09cf7c58
RR
116 wxArrayInt aSelections;
117 int count = listbox->GetSelections(aSelections);
118 if ( count > 0 )
119 {
fd0eed64
RR
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);
09cf7c58
RR
124 }
125 else
126 {
fd0eed64
RR
127 event.m_commandInt = -1 ;
128 event.m_commandString = copystring("") ;
09cf7c58
RR
129 }
130
fd0eed64 131 event.SetEventObject( listbox );
a3622daa 132
fd0eed64
RR
133 listbox->GetEventHandler()->ProcessEvent( event );
134 if (event.m_commandString) delete[] event.m_commandString ;
6de97a3b 135}
c801d85f 136
a60c99e6
RR
137//-----------------------------------------------------------------------------
138// wxListBox
c801d85f
KB
139//-----------------------------------------------------------------------------
140
141IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
142
fd0eed64 143wxListBox::wxListBox()
c801d85f 144{
fd0eed64 145 m_list = (GtkList *) NULL;
caaa4cfd 146 m_hasCheckBoxes = FALSE;
6de97a3b 147}
c801d85f 148
dcf40a56 149bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
150 const wxPoint &pos, const wxSize &size,
151 int n, const wxString choices[],
152 long style, const wxValidator& validator, const wxString &name )
c801d85f 153{
fd0eed64 154 m_needParent = TRUE;
b292e2f5 155 m_acceptsFocus = TRUE;
dcf40a56 156
fd0eed64 157 PreCreation( parent, id, pos, size, style, name );
dcf40a56 158
fd0eed64 159 SetValidator( validator );
6de97a3b 160
fd0eed64
RR
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 );
b292e2f5 164
fd0eed64 165 m_list = GTK_LIST( gtk_list_new() );
dcf40a56 166
fd0eed64
RR
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;
6a6d4eed 172
fd0eed64 173 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
dcf40a56 174
38c7b3d3
RR
175#ifdef NEW_GTK_SCROLL_CODE
176 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
177#else
fd0eed64 178 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
38c7b3d3
RR
179#endif
180
b292e2f5
RR
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
fd0eed64 200 gtk_widget_show( GTK_WIDGET(m_list) );
dcf40a56 201
fd0eed64
RR
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 );
dcf40a56 206
fd0eed64
RR
207 for (int i = 0; i < n; i++)
208 {
f5e27805
RR
209 m_clientDataList.Append( (wxObject*) NULL );
210 m_clientObjectList.Append( (wxObject*) NULL );
211
fd0eed64 212 GtkWidget *list_item;
caaa4cfd
RR
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 }
dcf40a56 224
b292e2f5
RR
225#ifdef __WXDEBUG__
226 debug_focus_in( list_item, "wxListBox::list_item", name );
227#endif
228
fd0eed64 229 gtk_container_add( GTK_CONTAINER(m_list), list_item );
dcf40a56 230
fd0eed64
RR
231 gtk_signal_connect( GTK_OBJECT(list_item), "select",
232 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 233
fd0eed64
RR
234 if (style & wxLB_MULTIPLE)
235 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
236 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 237
caaa4cfd
RR
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
4f22cf8d
RR
246 gtk_signal_connect( GTK_OBJECT(list_item),
247 "key_press_event",
248 (GtkSignalFunc)gtk_listbox_key_press_callback,
249 (gpointer)this );
250
fd0eed64 251 ConnectWidget( list_item );
c67d8618 252
fd0eed64
RR
253 gtk_widget_show( list_item );
254 }
dcf40a56 255
fd0eed64 256 m_parent->AddChild( this );
6ca41e57 257
fd0eed64 258 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 259
fd0eed64 260 PostCreation();
dcf40a56 261
fd0eed64 262 gtk_widget_realize( GTK_WIDGET(m_list) );
dcf40a56 263
fd0eed64
RR
264 SetBackgroundColour( parent->GetBackgroundColour() );
265 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 266
fd0eed64 267 Show( TRUE );
dcf40a56 268
fd0eed64 269 return TRUE;
6de97a3b 270}
c801d85f 271
fd0eed64 272wxListBox::~wxListBox()
c801d85f 273{
caaa4cfd 274 Clear();
6de97a3b 275}
c801d85f 276
fd0eed64 277void wxListBox::AppendCommon( const wxString &item )
c801d85f 278{
fd0eed64
RR
279 wxCHECK_RET( m_list != NULL, "invalid listbox" );
280
caaa4cfd
RR
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 }
fc54776e 293
fd0eed64
RR
294 gtk_signal_connect( GTK_OBJECT(list_item), "select",
295 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 296
fd0eed64
RR
297 if (GetWindowStyleFlag() & wxLB_MULTIPLE)
298 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
299 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 300
fd0eed64 301 gtk_container_add( GTK_CONTAINER(m_list), list_item );
dcf40a56 302
fd0eed64
RR
303 if (m_widgetStyle) ApplyWidgetStyle();
304
caaa4cfd
RR
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 }
4f22cf8d
RR
312
313 gtk_signal_connect( GTK_OBJECT(list_item),
314 "key_press_event",
315 (GtkSignalFunc)gtk_listbox_key_press_callback,
316 (gpointer)this );
caaa4cfd 317
fd0eed64
RR
318 gtk_widget_show( list_item );
319
320 ConnectWidget( list_item );
321
ac57418f 322#ifdef wxUSE_DRAG_AND_DROP
38c7b3d3 323#ifndef NEW_GTK_DND_CODE
33a5bc52 324 if (m_dropTarget) m_dropTarget->RegisterWidget( list_item );
38c7b3d3 325#endif
ac57418f 326#endif
fd0eed64
RR
327}
328
329void wxListBox::Append( const wxString &item )
330{
f5e27805
RR
331 m_clientDataList.Append( (wxObject*) NULL );
332 m_clientObjectList.Append( (wxObject*) NULL );
fd0eed64
RR
333
334 AppendCommon( item );
335}
336
337void wxListBox::Append( const wxString &item, void *clientData )
338{
f5e27805
RR
339 m_clientDataList.Append( (wxObject*) clientData );
340 m_clientObjectList.Append( (wxObject*) NULL );
fd0eed64
RR
341
342 AppendCommon( item );
343}
dcf40a56 344
fd0eed64
RR
345void wxListBox::Append( const wxString &item, wxClientData *clientData )
346{
f5e27805
RR
347 m_clientObjectList.Append( (wxObject*) clientData );
348 m_clientDataList.Append( (wxObject*) NULL );
fd0eed64
RR
349
350 AppendCommon( item );
351}
dcf40a56 352
fd0eed64
RR
353void 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;
7bce6aec 359
f5e27805 360 node->SetData( (wxObject*) clientData );
fd0eed64 361}
dcf40a56 362
fd0eed64
RR
363void* 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
f5e27805 370 return node->Data();
fd0eed64 371}
dcf40a56 372
fd0eed64
RR
373void wxListBox::SetClientObject( int n, wxClientData* clientData )
374{
375 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
376
f5e27805 377 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64
RR
378 if (!node) return;
379
380 wxClientData *cd = (wxClientData*) node->Data();
381 if (cd) delete cd;
382
383 node->SetData( (wxObject*) clientData );
6de97a3b 384}
c801d85f 385
fd0eed64 386wxClientData* wxListBox::GetClientObject( int n )
c801d85f 387{
fd0eed64
RR
388 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
389
f5e27805 390 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64
RR
391 if (!node) return (wxClientData*) NULL;
392
393 return (wxClientData*) node->Data();
394}
395
396void wxListBox::Clear()
397{
398 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 399
fd0eed64 400 gtk_list_clear_items( m_list, 0, Number() );
dcf40a56 401
f5e27805 402 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
403 while (node)
404 {
405 wxClientData *cd = (wxClientData*)node->Data();
406 if (cd) delete cd;
407 node = node->Next();
408 }
f5e27805
RR
409 m_clientObjectList.Clear();
410
fd0eed64 411 m_clientDataList.Clear();
6de97a3b 412}
c801d85f
KB
413
414void wxListBox::Delete( int n )
415{
fd0eed64 416 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 417
fd0eed64 418 GList *child = g_list_nth( m_list->children, n );
dcf40a56 419
caaa4cfd 420 wxCHECK_RET( child, "wrong listbox index" );
dcf40a56 421
fd0eed64
RR
422 GList *list = g_list_append( NULL, child->data );
423 gtk_list_remove_items( m_list, list );
424 g_list_free( list );
dcf40a56 425
f5e27805
RR
426 wxNode *node = m_clientObjectList.Nth( n );
427 if (node)
fd0eed64
RR
428 {
429 wxClientData *cd = (wxClientData*)node->Data();
430 if (cd) delete cd;
f5e27805
RR
431 m_clientObjectList.DeleteNode( node );
432 }
433
434 node = m_clientDataList.Nth( n );
435 if (node)
436 {
fd0eed64
RR
437 m_clientDataList.DeleteNode( node );
438 }
6de97a3b 439}
c801d85f
KB
440
441void wxListBox::Deselect( int n )
442{
fd0eed64 443 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 444
fd0eed64 445 gtk_list_unselect_item( m_list, n );
6de97a3b 446}
c801d85f
KB
447
448int wxListBox::FindString( const wxString &item ) const
449{
fd0eed64 450 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
fc54776e 451
fd0eed64
RR
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 );
caaa4cfd
RR
458
459 wxString str = label->label;
460 if (m_hasCheckBoxes) str.Remove( 0, 4 );
461
462 if (str == item) return count;
463
fd0eed64
RR
464 count++;
465 child = child->next;
466 }
467
468 // it's not an error if the string is not found -> no wxCHECK
dcf40a56 469
c801d85f 470 return -1;
6de97a3b 471}
c801d85f 472
fd0eed64 473int wxListBox::GetSelection() const
c801d85f 474{
fd0eed64 475 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
fc54776e 476
fd0eed64
RR
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;
6de97a3b 486}
c801d85f 487
fd0eed64 488int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 489{
fd0eed64 490 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
fc54776e 491
fd0eed64
RR
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 }
c801d85f 500
fd0eed64
RR
501 aSelections.Empty();
502
503 if (count > 0)
868a2826 504 {
fd0eed64
RR
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 }
6a6d4eed 513 }
dcf40a56 514
fd0eed64 515 return count;
6de97a3b 516}
c801d85f
KB
517
518wxString wxListBox::GetString( int n ) const
519{
fd0eed64 520 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
fc54776e 521
fd0eed64
RR
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 );
caaa4cfd
RR
527
528 wxString str = label->label;
529 if (m_hasCheckBoxes) str.Remove( 0, 4 );
530
531 return str;
fd0eed64
RR
532 }
533 wxFAIL_MSG("wrong listbox index");
534 return "";
6de97a3b 535}
c801d85f 536
fd0eed64 537wxString wxListBox::GetStringSelection() const
c801d85f 538{
fd0eed64 539 wxCHECK_MSG( m_list != NULL, "", "invalid listbox" );
fc54776e 540
fd0eed64
RR
541 GList *selection = m_list->selection;
542 if (selection)
543 {
544 GtkBin *bin = GTK_BIN( selection->data );
caaa4cfd
RR
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;
fd0eed64 551 }
caaa4cfd 552
fd0eed64
RR
553 wxFAIL_MSG("no listbox selection available");
554 return "";
6de97a3b 555}
c801d85f 556
fd0eed64 557int wxListBox::Number()
c801d85f 558{
fd0eed64 559 wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" );
fc54776e 560
fd0eed64
RR
561 GList *child = m_list->children;
562 int count = 0;
563 while (child) { count++; child = child->next; }
564 return count;
6de97a3b 565}
c801d85f 566
debe6624 567bool wxListBox::Selected( int n )
c801d85f 568{
fd0eed64 569 wxCHECK_MSG( m_list != NULL, FALSE, "invalid listbox" );
fc54776e 570
fd0eed64
RR
571 GList *target = g_list_nth( m_list->children, n );
572 if (target)
c801d85f 573 {
fd0eed64
RR
574 GList *child = m_list->selection;
575 while (child)
576 {
577 if (child->data == target->data) return TRUE;
578 child = child->next;
579 }
6de97a3b 580 }
fd0eed64
RR
581 wxFAIL_MSG("wrong listbox index");
582 return FALSE;
6de97a3b 583}
c801d85f 584
debe6624 585void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f 586{
fd0eed64 587 wxFAIL_MSG("wxListBox::Set not implemented");
6de97a3b 588}
c801d85f
KB
589
590void wxListBox::SetFirstItem( int WXUNUSED(n) )
591{
fd0eed64 592 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 593}
c801d85f
KB
594
595void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
596{
fd0eed64 597 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 598}
c801d85f 599
debe6624 600void wxListBox::SetSelection( int n, bool select )
c801d85f 601{
fd0eed64 602 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 603
fd0eed64
RR
604 if (select)
605 gtk_list_select_item( m_list, n );
606 else
607 gtk_list_unselect_item( m_list, n );
6de97a3b 608}
c801d85f 609
09cf7c58 610void wxListBox::SetString( int n, const wxString &string )
c801d85f 611{
fd0eed64 612 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 613
fd0eed64
RR
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 );
caaa4cfd
RR
619
620 wxString str;
621 if (m_hasCheckBoxes) str += "[-] ";
622 str += string;
623
624 gtk_label_set( label, str );
fd0eed64
RR
625 }
626 else
627 {
628 wxFAIL_MSG("wrong listbox index");
629 }
6de97a3b 630}
c801d85f 631
debe6624 632void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f 633{
fd0eed64 634 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 635
fd0eed64 636 SetSelection( FindString(string), select );
6de97a3b 637}
c801d85f
KB
638
639int wxListBox::GetIndex( GtkWidget *item ) const
640{
fd0eed64 641 if (item)
c801d85f 642 {
fd0eed64
RR
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 }
6de97a3b 651 }
fd0eed64 652 return -1;
6de97a3b 653}
c801d85f 654
ac57418f 655#ifdef wxUSE_DRAG_AND_DROP
a60c99e6
RR
656void wxListBox::SetDropTarget( wxDropTarget *dropTarget )
657{
fd0eed64 658 wxCHECK_RET( m_list != NULL, "invalid listbox" );
fc54776e 659
38c7b3d3 660#ifndef NEW_GTK_DND_CODE
33a5bc52 661 if (m_dropTarget)
fd0eed64 662 {
33a5bc52
RR
663 GList *child = m_list->children;
664 while (child)
665 {
666 m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) );
667 child = child->next;
668 }
fd0eed64 669 }
fed46e72 670#endif
38c7b3d3
RR
671
672 wxWindow::SetDropTarget( dropTarget );
fed46e72
RR
673
674#ifndef NEW_GTK_DND_CODE
33a5bc52 675 if (m_dropTarget)
fd0eed64 676 {
33a5bc52
RR
677 GList *child = m_list->children;
678 while (child)
679 {
680 m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) );
681 child = child->next;
682 }
fd0eed64 683 }
38c7b3d3 684#endif
a60c99e6 685}
ac57418f 686#endif
a60c99e6 687
fd0eed64 688GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 689{
fd0eed64 690 return GTK_WIDGET(m_list);
6de97a3b 691}
e3e65dac 692
f96aa4d9 693bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
868a2826 694{
fd0eed64 695 if (wxWindow::IsOwnGtkWindow( window )) return TRUE;
a60c99e6 696
fd0eed64
RR
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 }
f96aa4d9 704
fd0eed64 705 return FALSE;
868a2826 706}
e3e65dac 707
58614078 708void wxListBox::ApplyWidgetStyle()
c058d771 709{
fd0eed64 710 SetWidgetStyle();
fc54776e 711
e380f72b
RR
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 }
f96aa4d9 719
fd0eed64
RR
720 GList *child = m_list->children;
721 while (child)
722 {
fd0eed64 723 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
caaa4cfd
RR
724
725 GtkBin *bin = GTK_BIN( child->data );
726 GtkWidget *label = GTK_WIDGET( bin->child );
727 gtk_widget_set_style( label, m_widgetStyle );
728
fd0eed64
RR
729 child = child->next;
730 }
68dda785 731}