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