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