]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
first step into modernizing MLTE-Classic as it still seems to be the most complete...
[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
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "listbox.h"
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
2da2f941 18#include "wx/defs.h"
dcf924a3
RR
19
20#if wxUSE_LISTBOX
21
2da2f941 22#include "wx/listbox.h"
dcf924a3 23#include "wx/dynarray.h"
2da2f941 24#include "wx/arrstr.h"
09cf7c58 25#include "wx/utils.h"
caaa4cfd
RR
26#include "wx/intl.h"
27#include "wx/checklst.h"
72a16063 28#include "wx/settings.h"
fab591c5 29#include "wx/gtk/private.h"
291a8f20
RR
30
31#if wxUSE_TOOLTIPS
b1170810 32#include "wx/tooltip.h"
291a8f20 33#endif
c801d85f 34
4a82abc8 35#include <gdk/gdk.h>
16c1f79c
RR
36#include <gtk/gtk.h>
37#include <gdk/gdkkeysyms.h>
83624f79 38
acfd422a
RR
39//-----------------------------------------------------------------------------
40// idle system
41//-----------------------------------------------------------------------------
42
43extern void wxapp_install_idle_handler();
44extern bool g_isIdle;
45
66bd6b93
RR
46//-----------------------------------------------------------------------------
47// data
48//-----------------------------------------------------------------------------
49
d7fa7eaa
RR
50extern bool g_blockEventsOnDrag;
51extern bool g_blockEventsOnScroll;
52extern wxCursor g_globalCursor;
53extern wxWindowGTK *g_delayedFocus;
c9433ea9
RR
54extern wxWindowGTK *g_focusWindow;
55extern wxWindowGTK *g_focusWindowLast;
caaa4cfd 56
5e014a0c 57static bool g_hasDoubleClicked = FALSE;
7a30ee8f 58
4a82abc8
RR
59//-----------------------------------------------------------------------------
60// idle callback for SetFirstItem
61//-----------------------------------------------------------------------------
62
63struct wxlistbox_idle_struct
64{
65 wxListBox *m_listbox;
66 int m_item;
67 gint m_tag;
68};
69
295272bd 70extern "C" gint wxlistbox_idle_callback( gpointer gdata )
4a82abc8
RR
71{
72 wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata;
73 gdk_threads_enter();
74
75 gtk_idle_remove( data->m_tag );
f96b15a3 76
992295c4
VZ
77 // check that the items haven't been deleted from the listbox since we had
78 // installed this callback
79 wxListBox *lbox = data->m_listbox;
80 if ( data->m_item < lbox->GetCount() )
81 {
82 lbox->SetFirstItem( data->m_item );
83 }
f96b15a3 84
4a82abc8 85 delete data;
f96b15a3 86
4a82abc8
RR
87 gdk_threads_leave();
88
89 return TRUE;
90}
91
c9433ea9
RR
92//-----------------------------------------------------------------------------
93// "focus_in_event"
94//-----------------------------------------------------------------------------
95
96static gint gtk_listitem_focus_in_callback( GtkWidget *widget,
97 GdkEvent *WXUNUSED(event),
98 wxWindow *win )
99{
100 if (g_isIdle)
101 wxapp_install_idle_handler();
102
103 g_focusWindowLast =
104 g_focusWindow = win;
105
106 // does the window itself think that it has the focus?
107 if ( !win->m_hasFocus )
108 {
109 // not yet, notify it
110 win->m_hasFocus = TRUE;
111
112 wxChildFocusEvent eventChildFocus(win);
113 (void)win->GetEventHandler()->ProcessEvent(eventChildFocus);
114
115 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
116 eventFocus.SetEventObject(win);
117
118 (void)win->GetEventHandler()->ProcessEvent(eventFocus);
119 }
120
121 return FALSE;
122}
123
124//-----------------------------------------------------------------------------
125// "focus_out_event"
126//-----------------------------------------------------------------------------
127
128static gint gtk_listitem_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win )
129{
130 if (g_isIdle)
131 wxapp_install_idle_handler();
132
133 g_focusWindow = (wxWindowGTK *)NULL;
134
135 // don't send the window a kill focus event if it thinks that it doesn't
136 // have focus already
137 if ( win->m_hasFocus )
138 {
139 win->m_hasFocus = FALSE;
140
141 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
142 event.SetEventObject( win );
143
144 // even if we did process the event in wx code, still let GTK itself
145 // process it too as otherwise bad things happen, especially in GTK2
146 // where the text control simply aborts the program if it doesn't get
147 // the matching focus out event
148 (void)win->GetEventHandler()->ProcessEvent( event );
149 }
150
151 return FALSE;
152}
153
caaa4cfd 154//-----------------------------------------------------------------------------
7a30ee8f 155// "button_release_event"
caaa4cfd
RR
156//-----------------------------------------------------------------------------
157
7a30ee8f
RR
158/* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
159 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
160 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
161 this can lead to race conditions so that we emit the dclick event
162 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
163
ff8bfdbb 164static gint
014b0d06
VZ
165gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
166 GdkEventButton * WXUNUSED(gdk_event),
167 wxListBox *listbox )
caaa4cfd 168{
acfd422a
RR
169 if (g_isIdle) wxapp_install_idle_handler();
170
caaa4cfd
RR
171 if (g_blockEventsOnDrag) return FALSE;
172 if (g_blockEventsOnScroll) return FALSE;
173
a2053b27 174 if (!listbox->m_hasVMT) return FALSE;
caaa4cfd 175
7a30ee8f 176 if (!g_hasDoubleClicked) return FALSE;
ff8bfdbb 177
6c8a980f
VZ
178 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
179 event.SetEventObject( listbox );
ff8bfdbb 180
6c8a980f
VZ
181 wxArrayInt aSelections;
182 int n, count = listbox->GetSelections(aSelections);
183 if ( count > 0 )
184 {
185 n = aSelections[0];
186 if ( listbox->HasClientObjectData() )
187 event.SetClientObject( listbox->GetClientObject(n) );
188 else if ( listbox->HasClientUntypedData() )
189 event.SetClientData( listbox->GetClientData(n) );
190 event.SetString( listbox->GetString(n) );
191 }
192 else
193 {
194 n = -1;
195 }
5b077d48 196
687706f5 197 event.SetInt(n);
6c8a980f
VZ
198
199 listbox->GetEventHandler()->ProcessEvent( event );
ff8bfdbb 200
7a30ee8f
RR
201 return FALSE;
202}
203
204//-----------------------------------------------------------------------------
205// "button_press_event"
206//-----------------------------------------------------------------------------
207
208static gint
014b0d06
VZ
209gtk_listbox_button_press_callback( GtkWidget *widget,
210 GdkEventButton *gdk_event,
211 wxListBox *listbox )
7a30ee8f
RR
212{
213 if (g_isIdle) wxapp_install_idle_handler();
214
215 if (g_blockEventsOnDrag) return FALSE;
216 if (g_blockEventsOnScroll) return FALSE;
217
218 if (!listbox->m_hasVMT) return FALSE;
219
11e1c70d 220 int sel = listbox->GtkGetIndex( widget );
7a30ee8f 221
88ac883a 222#if wxUSE_CHECKLISTBOX
7a30ee8f
RR
223 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
224 {
225 wxCheckListBox *clb = (wxCheckListBox *)listbox;
226
227 clb->Check( sel, !clb->IsChecked(sel) );
228
229 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
230 event.SetEventObject( listbox );
231 event.SetInt( sel );
232 listbox->GetEventHandler()->ProcessEvent( event );
4f22cf8d 233 }
88ac883a 234#endif // wxUSE_CHECKLISTBOX
014b0d06 235
4fcfa27c
RR
236 if ((gdk_event->state == 0) &&
237 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
238 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
239 {
240 if (listbox->IsSelected(sel))
241 {
242 gtk_list_unselect_item( GTK_LIST(listbox->m_list), sel );
243 return true;
244 }
245 }
246
7a30ee8f
RR
247 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
248 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
4f22cf8d 249
caaa4cfd
RR
250 return FALSE;
251}
66bd6b93 252
1144d24d
RR
253//-----------------------------------------------------------------------------
254// "key_press_event"
255//-----------------------------------------------------------------------------
256
ff8bfdbb 257static gint
1144d24d
RR
258gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
259{
9ef6ff8c 260 if (g_isIdle)
354aa1e3 261 wxapp_install_idle_handler();
acfd422a 262
9ef6ff8c 263 if (g_blockEventsOnDrag)
354aa1e3 264 return FALSE;
1144d24d 265
354aa1e3 266 bool ret = FALSE;
1144d24d 267
354aa1e3
RR
268 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
269 {
270 wxNavigationKeyEvent new_event;
271 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
272 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
273 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
274 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
275 new_event.SetCurrentFocus( listbox );
276 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
277 }
9ef6ff8c 278
4a82abc8
RR
279 if ((gdk_event->keyval == GDK_Return) && (!ret))
280 {
281 // eat return in all modes
282 ret = TRUE;
283 }
f96b15a3 284
354aa1e3 285#if wxUSE_CHECKLISTBOX
1e8d2f69 286 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
354aa1e3
RR
287 {
288 int sel = listbox->GtkGetIndex( widget );
ff8bfdbb 289
354aa1e3 290 wxCheckListBox *clb = (wxCheckListBox *)listbox;
ff8bfdbb 291
354aa1e3 292 clb->Check( sel, !clb->IsChecked(sel) );
ff8bfdbb 293
354aa1e3
RR
294 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
295 new_event.SetEventObject( listbox );
296 new_event.SetInt( sel );
297 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
298 }
299#endif // wxUSE_CHECKLISTBOX
ff8bfdbb 300
fec195b2
RR
301 // Check or uncheck item with SPACE
302 if ((gdk_event->keyval == ' ') && (!ret) &&
303 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
304 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
305 {
306 int sel = listbox->GtkGetIndex( widget );
307
308 if (sel != -1)
309 {
310 ret = TRUE;
311
312 if (listbox->IsSelected( sel ))
313 gtk_list_unselect_item( listbox->m_list, sel );
314 else
315 gtk_list_select_item( listbox->m_list, sel );
316
317 wxCommandEvent new_event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
318 new_event.SetEventObject( listbox );
319 wxArrayInt aSelections;
320 int n, count = listbox->GetSelections(aSelections);
321 if ( count > 0 )
322 {
323 n = aSelections[0];
324 if ( listbox->HasClientObjectData() )
325 new_event.SetClientObject( listbox->GetClientObject(n) );
326 else if ( listbox->HasClientUntypedData() )
327 new_event.SetClientData( listbox->GetClientData(n) );
328 new_event.SetString( listbox->GetString(n) );
329 }
330 else
331 {
332 n = -1;
333 }
687706f5 334 new_event.SetInt(n);
fec195b2
RR
335 listbox->GetEventHandler()->ProcessEvent( new_event );
336 }
337 }
338
354aa1e3
RR
339 if (ret)
340 {
341 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
342 return TRUE;
343 }
ff8bfdbb 344
1144d24d
RR
345 return FALSE;
346}
347
c801d85f 348//-----------------------------------------------------------------------------
a60c99e6 349// "select" and "deselect"
c801d85f
KB
350//-----------------------------------------------------------------------------
351
0a07a7d8
RR
352static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
353
354static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
355{
356 gtk_listitem_select_cb( widget, listbox, TRUE );
357}
65045edd
RR
358
359static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
360{
0a07a7d8 361 gtk_listitem_select_cb( widget, listbox, FALSE );
65045edd
RR
362}
363
9c9c3d7a
VZ
364static void gtk_listitem_select_cb( GtkWidget *widget,
365 wxListBox *listbox,
366 bool is_selection )
c801d85f 367{
acfd422a
RR
368 if (g_isIdle) wxapp_install_idle_handler();
369
a2053b27 370 if (!listbox->m_hasVMT) return;
fd0eed64 371 if (g_blockEventsOnDrag) return;
8161b5b9 372
bac95742 373 if (listbox->m_blockEvent) return;
9ef6ff8c 374
fd0eed64 375 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
6c8a980f 376 event.SetEventObject( listbox );
8161b5b9 377
9c9c3d7a
VZ
378 // indicate whether this is a selection or a deselection
379 event.SetExtraLong( is_selection );
159b66c0
RR
380
381 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
382 {
383 int sel = listbox->GtkGetIndex( widget );
384
385 if (listbox->m_prevSelection != sel)
386 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
387
388 listbox->m_prevSelection = sel;
389 }
dcf40a56 390
09cf7c58 391 wxArrayInt aSelections;
2ee3ee1b 392 int n, count = listbox->GetSelections(aSelections);
09cf7c58
RR
393 if ( count > 0 )
394 {
2ee3ee1b 395 n = aSelections[0];
6c8a980f
VZ
396 if ( listbox->HasClientObjectData() )
397 event.SetClientObject( listbox->GetClientObject(n) );
398 else if ( listbox->HasClientUntypedData() )
399 event.SetClientData( listbox->GetClientData(n) );
400 event.SetString( listbox->GetString(n) );
09cf7c58
RR
401 }
402 else
403 {
2ee3ee1b 404 n = -1;
09cf7c58
RR
405 }
406
687706f5 407 event.SetInt(n);
2ee3ee1b 408
159b66c0
RR
409// No longer required with new code in wxLB_SINGLE
410// listbox->GetEventHandler()->AddPendingEvent( event );
411 listbox->GetEventHandler()->ProcessEvent( event );
6de97a3b 412}
c801d85f 413
a60c99e6
RR
414//-----------------------------------------------------------------------------
415// wxListBox
c801d85f
KB
416//-----------------------------------------------------------------------------
417
f2e93537
RR
418static gint
419gtk_listbox_realized_callback( GtkWidget *m_widget, wxListBox *win )
420{
421 if (g_isIdle)
422 wxapp_install_idle_handler();
423
424 GList *child = win->m_list->children;
425 for (child = win->m_list->children; child != NULL; child = child->next)
426 gtk_widget_show( GTK_WIDGET(child->data) );
427
428 return false;
429}
430
431//-----------------------------------------------------------------------------
432// wxListBox
433//-----------------------------------------------------------------------------
434
c801d85f
KB
435IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
436
2ee3ee1b
VZ
437// ----------------------------------------------------------------------------
438// construction
439// ----------------------------------------------------------------------------
440
fd0eed64 441wxListBox::wxListBox()
c801d85f 442{
fd0eed64 443 m_list = (GtkList *) NULL;
88ac883a 444#if wxUSE_CHECKLISTBOX
caaa4cfd 445 m_hasCheckBoxes = FALSE;
88ac883a 446#endif // wxUSE_CHECKLISTBOX
6de97a3b 447}
c801d85f 448
584ad2a3
MB
449bool wxListBox::Create( wxWindow *parent, wxWindowID id,
450 const wxPoint &pos, const wxSize &size,
451 const wxArrayString& choices,
452 long style, const wxValidator& validator,
453 const wxString &name )
454{
455 wxCArrayString chs(choices);
456
457 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
458 style, validator, name );
459}
460
dcf40a56 461bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
462 const wxPoint &pos, const wxSize &size,
463 int n, const wxString choices[],
2ee3ee1b
VZ
464 long style, const wxValidator& validator,
465 const wxString &name )
c801d85f 466{
fd0eed64 467 m_needParent = TRUE;
b292e2f5 468 m_acceptsFocus = TRUE;
159b66c0 469 m_prevSelection = 0; // or -1 ??
bac95742 470 m_blockEvent = FALSE;
dcf40a56 471
4dcaf11a
RR
472 if (!PreCreation( parent, pos, size ) ||
473 !CreateBase( parent, id, pos, size, style, validator, name ))
474 {
223d09f6 475 wxFAIL_MSG( wxT("wxListBox creation failed") );
2ee3ee1b 476 return FALSE;
4dcaf11a 477 }
6de97a3b 478
fd0eed64 479 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
480 if (style & wxLB_ALWAYS_SB)
481 {
482 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
483 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
484 }
485 else
486 {
487 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
488 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
489 }
ff8bfdbb 490
fd0eed64 491 m_list = GTK_LIST( gtk_list_new() );
dcf40a56 492
4a82abc8 493 GtkSelectionMode mode;
fd0eed64 494 if (style & wxLB_MULTIPLE)
4a82abc8 495 {
fd0eed64 496 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 497 }
fd0eed64 498 else if (style & wxLB_EXTENDED)
4a82abc8 499 {
fd0eed64 500 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
501 }
502 else
503 {
504 // if style was 0 set single mode
505 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 506 mode = GTK_SELECTION_SINGLE;
4a82abc8 507 }
6a6d4eed 508
fd0eed64 509 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
dcf40a56 510
38c7b3d3 511 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
38c7b3d3 512
e115e771
RR
513 /* make list scroll when moving the focus down using cursor keys */
514 gtk_container_set_focus_vadjustment(
515 GTK_CONTAINER(m_list),
516 gtk_scrolled_window_get_vadjustment(
2ee3ee1b 517 GTK_SCROLLED_WINDOW(m_widget)));
e115e771 518
fd0eed64 519 gtk_widget_show( GTK_WIDGET(m_list) );
dcf40a56 520
f2e93537
RR
521 gtk_signal_connect( GTK_OBJECT(m_list), "realize",
522 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback), (gpointer) this );
523
2ee3ee1b
VZ
524 if ( style & wxLB_SORT )
525 {
526 // this will change DoAppend() behaviour
527 m_strings = new wxSortedArrayString;
528 }
eb553cb2
VZ
529 else
530 {
531 m_strings = (wxSortedArrayString *)NULL;
532 }
2ee3ee1b 533
fd0eed64
RR
534 for (int i = 0; i < n; i++)
535 {
11e1c70d 536 // add one by one
2ee3ee1b 537 DoAppend(choices[i]);
fd0eed64 538 }
dcf40a56 539
f03fc89f 540 m_parent->DoAddChild( this );
ff8bfdbb 541
abdeb9e7
RD
542 PostCreation(size);
543 SetBestSize(size); // need this too because this is a wxControlWithItems
dcf40a56 544
fd0eed64 545 return TRUE;
6de97a3b 546}
c801d85f 547
fd0eed64 548wxListBox::~wxListBox()
c801d85f 549{
4a82abc8
RR
550 m_hasVMT = FALSE;
551
caaa4cfd 552 Clear();
f96b15a3 553
6981d3ec
JS
554 if (m_strings)
555 delete m_strings;
6de97a3b 556}
c801d85f 557
8161b5b9
VZ
558// ----------------------------------------------------------------------------
559// adding items
560// ----------------------------------------------------------------------------
561
2ee3ee1b 562void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
bb0ca8df 563{
223d09f6 564 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
bb0ca8df 565
11e1c70d
RR
566 // VZ: notice that InsertItems knows nothing about sorting, so calling it
567 // from outside (and not from our own Append) is likely to break
568 // everything
569
570 // code elsewhere supposes we have as many items in m_clientList as items
2ee3ee1b 571 // in the listbox
11e1c70d 572 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
2ee3ee1b
VZ
573 wxT("bug in client data management") );
574
9f884528
RD
575 InvalidateBestSize();
576
bb0ca8df
VZ
577 GList *children = m_list->children;
578 int length = g_list_length(children);
9ef6ff8c 579
223d09f6 580 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
bb0ca8df 581
2ee3ee1b 582 size_t nItems = items.GetCount();
0a07a7d8 583 int index;
2ee3ee1b 584
0a07a7d8 585 if (m_strings)
bb0ca8df 586 {
0a07a7d8 587 for (size_t n = 0; n < nItems; n++)
bb0ca8df 588 {
0a07a7d8 589 index = m_strings->Add( items[n] );
f96b15a3 590
0a07a7d8
RR
591 if (index != GetCount())
592 {
593 GtkAddItem( items[n], index );
222ed1d6 594 wxList::compatibility_iterator node = m_clientList.Item( index );
0a07a7d8
RR
595 m_clientList.Insert( node, (wxObject*) NULL );
596 }
597 else
598 {
599 GtkAddItem( items[n] );
600 m_clientList.Append( (wxObject*) NULL );
601 }
bb0ca8df 602 }
bb0ca8df 603 }
11e1c70d 604 else
bb0ca8df 605 {
0a07a7d8
RR
606 if (pos == length)
607 {
608 for ( size_t n = 0; n < nItems; n++ )
609 {
610 GtkAddItem( items[n] );
611
612 m_clientList.Append((wxObject *)NULL);
613 }
614 }
615 else
bb0ca8df 616 {
222ed1d6 617 wxList::compatibility_iterator node = m_clientList.Item( pos );
0a07a7d8
RR
618 for ( size_t n = 0; n < nItems; n++ )
619 {
620 GtkAddItem( items[n], pos+n );
bb0ca8df 621
0a07a7d8
RR
622 m_clientList.Insert( node, (wxObject *)NULL );
623 }
bb0ca8df
VZ
624 }
625 }
2ee3ee1b 626
11e1c70d
RR
627 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
628 wxT("bug in client data management") );
2ee3ee1b
VZ
629}
630
631int wxListBox::DoAppend( const wxString& item )
632{
9f884528
RD
633 InvalidateBestSize();
634
11e1c70d 635 if (m_strings)
2ee3ee1b
VZ
636 {
637 // need to determine the index
11e1c70d 638 int index = m_strings->Add( item );
9ef6ff8c
VZ
639
640 // only if not at the end anyway
641 if (index != GetCount())
642 {
643 GtkAddItem( item, index );
644
222ed1d6 645 wxList::compatibility_iterator node = m_clientList.Item( index );
11e1c70d 646 m_clientList.Insert( node, (wxObject *)NULL );
9ef6ff8c
VZ
647
648 return index;
649 }
2ee3ee1b 650 }
9ef6ff8c 651
11e1c70d 652 GtkAddItem(item);
2ee3ee1b 653
11e1c70d 654 m_clientList.Append((wxObject *)NULL);
2ee3ee1b 655
11e1c70d 656 return GetCount() - 1;
bb0ca8df
VZ
657}
658
11e1c70d 659void wxListBox::GtkAddItem( const wxString &item, int pos )
c801d85f 660{
223d09f6 661 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
ff8bfdbb 662
caaa4cfd 663 GtkWidget *list_item;
ff8bfdbb 664
bb0ca8df 665 wxString label(item);
88ac883a 666#if wxUSE_CHECKLISTBOX
caaa4cfd
RR
667 if (m_hasCheckBoxes)
668 {
d752a3c3 669 label.Prepend(wxCHECKLBOX_STRING);
caaa4cfd 670 }
88ac883a 671#endif // wxUSE_CHECKLISTBOX
fc54776e 672
fab591c5 673 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
bb0ca8df 674
11e1c70d
RR
675 GList *gitem_list = g_list_alloc ();
676 gitem_list->data = list_item;
9ef6ff8c 677
11e1c70d
RR
678 if (pos == -1)
679 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
680 else
681 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
3502e687 682
c4526184 683 gtk_signal_connect_after( GTK_OBJECT(list_item), "select",
fd0eed64 684 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 685
159b66c0 686 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
c4526184 687 gtk_signal_connect_after( GTK_OBJECT(list_item), "deselect",
65045edd 688 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
dcf40a56 689
ff8bfdbb
VZ
690 gtk_signal_connect( GTK_OBJECT(list_item),
691 "button_press_event",
692 (GtkSignalFunc)gtk_listbox_button_press_callback,
693 (gpointer) this );
694
74505862
RR
695 gtk_signal_connect_after( GTK_OBJECT(list_item),
696 "button_release_event",
697 (GtkSignalFunc)gtk_listbox_button_release_callback,
698 (gpointer) this );
699
354aa1e3 700 gtk_signal_connect( GTK_OBJECT(list_item),
bb0ca8df
VZ
701 "key_press_event",
702 (GtkSignalFunc)gtk_listbox_key_press_callback,
703 (gpointer)this );
ff8bfdbb 704
c9433ea9
RR
705
706 gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event",
707 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this );
708
709 gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event",
710 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this );
711
fd0eed64
RR
712 ConnectWidget( list_item );
713
2b07d713
RR
714 if (GTK_WIDGET_REALIZED(m_widget))
715 {
f2e93537
RR
716 gtk_widget_show( list_item );
717
2b07d713
RR
718 gtk_widget_realize( list_item );
719 gtk_widget_realize( GTK_BIN(list_item)->child );
014b0d06 720
291a8f20 721#if wxUSE_TOOLTIPS
f03fc89f 722 if (m_tooltip) m_tooltip->Apply( this );
291a8f20 723#endif
2b07d713 724 }
631a05be
RL
725
726 // Apply current widget style to the new list_item
727 GtkRcStyle *style = CreateWidgetStyle();
728 if (style)
729 {
730 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
731 GtkBin *bin = GTK_BIN( list_item );
732 GtkWidget *label = GTK_WIDGET( bin->child );
733 gtk_widget_modify_style( label, style );
734 gtk_rc_style_unref( style );
735 }
fd0eed64
RR
736}
737
2ee3ee1b
VZ
738void wxListBox::DoSetItems( const wxArrayString& items,
739 void **clientData)
fd0eed64 740{
2ee3ee1b 741 Clear();
fd0eed64 742
2ee3ee1b 743 DoInsertItems(items, 0);
ff8bfdbb 744
2ee3ee1b
VZ
745 if ( clientData )
746 {
747 size_t count = items.GetCount();
748 for ( size_t n = 0; n < count; n++ )
749 {
750 SetClientData(n, clientData[n]);
751 }
752 }
fd0eed64 753}
dcf40a56 754
2ee3ee1b 755// ----------------------------------------------------------------------------
8161b5b9 756// deleting items
2ee3ee1b 757// ----------------------------------------------------------------------------
ff8bfdbb 758
fd0eed64
RR
759void wxListBox::Clear()
760{
223d09f6 761 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 762
c4c92801 763 gtk_list_clear_items( m_list, 0, GetCount() );
8161b5b9 764
780bb874
RR
765 if ( GTK_LIST(m_list)->last_focus_child != NULL )
766 {
767 // This should be NULL, I think.
768 GTK_LIST(m_list)->last_focus_child = NULL;
769 }
dcf40a56 770
6c8a980f
VZ
771 if ( HasClientObjectData() )
772 {
773 // destroy the data (due to Robert's idea of using wxList<wxObject>
774 // and not wxList<wxClientData> we can't just say
775 // m_clientList.DeleteContents(TRUE) - this would crash!
222ed1d6 776 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
777 while ( node )
778 {
b1d4dd7a
RL
779 delete (wxClientData *)node->GetData();
780 node = node->GetNext();
6c8a980f
VZ
781 }
782 }
11e1c70d 783 m_clientList.Clear();
ff8bfdbb 784
2ee3ee1b
VZ
785 if ( m_strings )
786 m_strings->Clear();
6de97a3b 787}
c801d85f
KB
788
789void wxListBox::Delete( int n )
790{
223d09f6 791 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 792
fd0eed64 793 GList *child = g_list_nth( m_list->children, n );
dcf40a56 794
223d09f6 795 wxCHECK_RET( child, wxT("wrong listbox index") );
dcf40a56 796
bbe0af5b 797 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
798 gtk_list_remove_items( m_list, list );
799 g_list_free( list );
dcf40a56 800
222ed1d6 801 wxList::compatibility_iterator node = m_clientList.Item( n );
2ee3ee1b 802 if ( node )
fd0eed64 803 {
1e6feb95 804 if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b 805 {
b1d4dd7a 806 wxClientData *cd = (wxClientData*)node->GetData();
2ee3ee1b
VZ
807 delete cd;
808 }
809
222ed1d6 810 m_clientList.Erase( node );
f5e27805 811 }
ff8bfdbb 812
2ee3ee1b 813 if ( m_strings )
ba8c1601 814 m_strings->RemoveAt(n);
2ee3ee1b
VZ
815}
816
8161b5b9
VZ
817// ----------------------------------------------------------------------------
818// client data
819// ----------------------------------------------------------------------------
820
821void wxListBox::DoSetItemClientData( int n, void* clientData )
822{
823 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
824
222ed1d6 825 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
826 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
827
828 node->SetData( (wxObject*) clientData );
829}
830
831void* wxListBox::DoGetItemClientData( int n ) const
832{
833 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
834
222ed1d6 835 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
836 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
837
b1d4dd7a 838 return node->GetData();
8161b5b9
VZ
839}
840
841void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
842{
843 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
844
222ed1d6 845 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
846 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
847
848 // wxItemContainer already deletes data for us
849
850 node->SetData( (wxObject*) clientData );
851}
852
853wxClientData* wxListBox::DoGetItemClientObject( int n ) const
854{
855 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
856
222ed1d6 857 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
858 wxCHECK_MSG( node, (wxClientData *)NULL,
859 wxT("invalid index in wxListBox::DoGetItemClientObject") );
860
b1d4dd7a 861 return (wxClientData*) node->GetData();
8161b5b9
VZ
862}
863
2ee3ee1b
VZ
864// ----------------------------------------------------------------------------
865// string list access
866// ----------------------------------------------------------------------------
867
8161b5b9
VZ
868wxString wxListBox::GetRealLabel(GList *item) const
869{
870 GtkBin *bin = GTK_BIN( item->data );
871 GtkLabel *label = GTK_LABEL( bin->child );
872
873 wxString str;
874
875#ifdef __WXGTK20__
876 str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
877#else
878 str = wxString( label->label );
879#endif
880
881#if wxUSE_CHECKLISTBOX
882