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