]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
Better correction to the wxListBox misbehaviour.
[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
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 {
376c0148
RR
240 listbox->m_blockEvent = TRUE;
241
242 int i;
243 for (i = 0; i < (int)listbox->GetCount(); i++)
244 if (i != sel)
245 gtk_list_unselect_item( GTK_LIST(listbox->m_list), i );
246
247 listbox->m_blockEvent = FALSE;
248
249 return false;
4fcfa27c
RR
250 }
251
7a30ee8f
RR
252 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
253 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
4f22cf8d 254
caaa4cfd
RR
255 return FALSE;
256}
66bd6b93 257
1144d24d
RR
258//-----------------------------------------------------------------------------
259// "key_press_event"
260//-----------------------------------------------------------------------------
261
ff8bfdbb 262static gint
1144d24d
RR
263gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
264{
9ef6ff8c 265 if (g_isIdle)
354aa1e3 266 wxapp_install_idle_handler();
acfd422a 267
9ef6ff8c 268 if (g_blockEventsOnDrag)
354aa1e3 269 return FALSE;
1144d24d 270
354aa1e3 271 bool ret = FALSE;
1144d24d 272
354aa1e3
RR
273 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
274 {
275 wxNavigationKeyEvent new_event;
276 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
277 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
278 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
279 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
280 new_event.SetCurrentFocus( listbox );
281 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
282 }
9ef6ff8c 283
4a82abc8
RR
284 if ((gdk_event->keyval == GDK_Return) && (!ret))
285 {
286 // eat return in all modes
287 ret = TRUE;
288 }
f96b15a3 289
354aa1e3 290#if wxUSE_CHECKLISTBOX
1e8d2f69 291 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
354aa1e3
RR
292 {
293 int sel = listbox->GtkGetIndex( widget );
ff8bfdbb 294
354aa1e3 295 wxCheckListBox *clb = (wxCheckListBox *)listbox;
ff8bfdbb 296
354aa1e3 297 clb->Check( sel, !clb->IsChecked(sel) );
ff8bfdbb 298
354aa1e3
RR
299 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
300 new_event.SetEventObject( listbox );
301 new_event.SetInt( sel );
302 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
303 }
304#endif // wxUSE_CHECKLISTBOX
ff8bfdbb 305
fec195b2
RR
306 // Check or uncheck item with SPACE
307 if ((gdk_event->keyval == ' ') && (!ret) &&
308 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
309 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
310 {
311 int sel = listbox->GtkGetIndex( widget );
312
313 if (sel != -1)
314 {
315 ret = TRUE;
316
317 if (listbox->IsSelected( sel ))
318 gtk_list_unselect_item( listbox->m_list, sel );
319 else
320 gtk_list_select_item( listbox->m_list, sel );
321
322 wxCommandEvent new_event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
323 new_event.SetEventObject( listbox );
324 wxArrayInt aSelections;
325 int n, count = listbox->GetSelections(aSelections);
326 if ( count > 0 )
327 {
328 n = aSelections[0];
329 if ( listbox->HasClientObjectData() )
330 new_event.SetClientObject( listbox->GetClientObject(n) );
331 else if ( listbox->HasClientUntypedData() )
332 new_event.SetClientData( listbox->GetClientData(n) );
333 new_event.SetString( listbox->GetString(n) );
334 }
335 else
336 {
337 n = -1;
338 }
687706f5 339 new_event.SetInt(n);
fec195b2
RR
340 listbox->GetEventHandler()->ProcessEvent( new_event );
341 }
342 }
343
354aa1e3
RR
344 if (ret)
345 {
346 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
347 return TRUE;
348 }
ff8bfdbb 349
1144d24d
RR
350 return FALSE;
351}
352
c801d85f 353//-----------------------------------------------------------------------------
a60c99e6 354// "select" and "deselect"
c801d85f
KB
355//-----------------------------------------------------------------------------
356
0a07a7d8
RR
357static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection );
358
359static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
360{
361 gtk_listitem_select_cb( widget, listbox, TRUE );
362}
65045edd
RR
363
364static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
365{
0a07a7d8 366 gtk_listitem_select_cb( widget, listbox, FALSE );
65045edd
RR
367}
368
9c9c3d7a
VZ
369static void gtk_listitem_select_cb( GtkWidget *widget,
370 wxListBox *listbox,
371 bool is_selection )
c801d85f 372{
acfd422a
RR
373 if (g_isIdle) wxapp_install_idle_handler();
374
a2053b27 375 if (!listbox->m_hasVMT) return;
fd0eed64 376 if (g_blockEventsOnDrag) return;
8161b5b9 377
bac95742 378 if (listbox->m_blockEvent) return;
9ef6ff8c 379
fd0eed64 380 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
6c8a980f 381 event.SetEventObject( listbox );
8161b5b9 382
9c9c3d7a
VZ
383 // indicate whether this is a selection or a deselection
384 event.SetExtraLong( is_selection );
159b66c0
RR
385
386 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
387 {
388 int sel = listbox->GtkGetIndex( widget );
389
390 if (listbox->m_prevSelection != sel)
391 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
392
393 listbox->m_prevSelection = sel;
394 }
dcf40a56 395
09cf7c58 396 wxArrayInt aSelections;
2ee3ee1b 397 int n, count = listbox->GetSelections(aSelections);
09cf7c58
RR
398 if ( count > 0 )
399 {
2ee3ee1b 400 n = aSelections[0];
6c8a980f
VZ
401 if ( listbox->HasClientObjectData() )
402 event.SetClientObject( listbox->GetClientObject(n) );
403 else if ( listbox->HasClientUntypedData() )
404 event.SetClientData( listbox->GetClientData(n) );
405 event.SetString( listbox->GetString(n) );
09cf7c58
RR
406 }
407 else
408 {
2ee3ee1b 409 n = -1;
09cf7c58
RR
410 }
411
687706f5 412 event.SetInt(n);
2ee3ee1b 413
159b66c0
RR
414// No longer required with new code in wxLB_SINGLE
415// listbox->GetEventHandler()->AddPendingEvent( event );
416 listbox->GetEventHandler()->ProcessEvent( event );
6de97a3b 417}
c801d85f 418
a60c99e6
RR
419//-----------------------------------------------------------------------------
420// wxListBox
c801d85f
KB
421//-----------------------------------------------------------------------------
422
f2e93537
RR
423static gint
424gtk_listbox_realized_callback( GtkWidget *m_widget, wxListBox *win )
425{
426 if (g_isIdle)
427 wxapp_install_idle_handler();
428
429 GList *child = win->m_list->children;
430 for (child = win->m_list->children; child != NULL; child = child->next)
431 gtk_widget_show( GTK_WIDGET(child->data) );
432
433 return false;
434}
435
436//-----------------------------------------------------------------------------
437// wxListBox
438//-----------------------------------------------------------------------------
439
c801d85f
KB
440IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
441
2ee3ee1b
VZ
442// ----------------------------------------------------------------------------
443// construction
444// ----------------------------------------------------------------------------
445
fd0eed64 446wxListBox::wxListBox()
c801d85f 447{
fd0eed64 448 m_list = (GtkList *) NULL;
88ac883a 449#if wxUSE_CHECKLISTBOX
caaa4cfd 450 m_hasCheckBoxes = FALSE;
88ac883a 451#endif // wxUSE_CHECKLISTBOX
6de97a3b 452}
c801d85f 453
584ad2a3
MB
454bool wxListBox::Create( wxWindow *parent, wxWindowID id,
455 const wxPoint &pos, const wxSize &size,
456 const wxArrayString& choices,
457 long style, const wxValidator& validator,
458 const wxString &name )
459{
460 wxCArrayString chs(choices);
461
462 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
463 style, validator, name );
464}
465
dcf40a56 466bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
467 const wxPoint &pos, const wxSize &size,
468 int n, const wxString choices[],
2ee3ee1b
VZ
469 long style, const wxValidator& validator,
470 const wxString &name )
c801d85f 471{
fd0eed64 472 m_needParent = TRUE;
b292e2f5 473 m_acceptsFocus = TRUE;
159b66c0 474 m_prevSelection = 0; // or -1 ??
bac95742 475 m_blockEvent = FALSE;
dcf40a56 476
4dcaf11a
RR
477 if (!PreCreation( parent, pos, size ) ||
478 !CreateBase( parent, id, pos, size, style, validator, name ))
479 {
223d09f6 480 wxFAIL_MSG( wxT("wxListBox creation failed") );
2ee3ee1b 481 return FALSE;
4dcaf11a 482 }
6de97a3b 483
fd0eed64 484 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
485 if (style & wxLB_ALWAYS_SB)
486 {
487 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
488 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
489 }
490 else
491 {
492 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
493 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
494 }
ff8bfdbb 495
fd0eed64 496 m_list = GTK_LIST( gtk_list_new() );
dcf40a56 497
4a82abc8 498 GtkSelectionMode mode;
fd0eed64 499 if (style & wxLB_MULTIPLE)
4a82abc8 500 {
fd0eed64 501 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 502 }
fd0eed64 503 else if (style & wxLB_EXTENDED)
4a82abc8 504 {
fd0eed64 505 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
506 }
507 else
508 {
509 // if style was 0 set single mode
510 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 511 mode = GTK_SELECTION_SINGLE;
4a82abc8 512 }
6a6d4eed 513
fd0eed64 514 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
dcf40a56 515
38c7b3d3 516 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
38c7b3d3 517
e115e771
RR
518 /* make list scroll when moving the focus down using cursor keys */
519 gtk_container_set_focus_vadjustment(
520 GTK_CONTAINER(m_list),
521 gtk_scrolled_window_get_vadjustment(
2ee3ee1b 522 GTK_SCROLLED_WINDOW(m_widget)));
e115e771 523
fd0eed64 524 gtk_widget_show( GTK_WIDGET(m_list) );
dcf40a56 525
f2e93537
RR
526 gtk_signal_connect( GTK_OBJECT(m_list), "realize",
527 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback), (gpointer) this );
528
2ee3ee1b
VZ
529 if ( style & wxLB_SORT )
530 {
531 // this will change DoAppend() behaviour
532 m_strings = new wxSortedArrayString;
533 }
eb553cb2
VZ
534 else
535 {
536 m_strings = (wxSortedArrayString *)NULL;
537 }
2ee3ee1b 538
fd0eed64
RR
539 for (int i = 0; i < n; i++)
540 {
11e1c70d 541 // add one by one
2ee3ee1b 542 DoAppend(choices[i]);
fd0eed64 543 }
dcf40a56 544
f03fc89f 545 m_parent->DoAddChild( this );
ff8bfdbb 546
abdeb9e7
RD
547 PostCreation(size);
548 SetBestSize(size); // need this too because this is a wxControlWithItems
dcf40a56 549
fd0eed64 550 return TRUE;
6de97a3b 551}
c801d85f 552
fd0eed64 553wxListBox::~wxListBox()
c801d85f 554{
4a82abc8
RR
555 m_hasVMT = FALSE;
556
caaa4cfd 557 Clear();
f96b15a3 558
6981d3ec
JS
559 if (m_strings)
560 delete m_strings;
6de97a3b 561}
c801d85f 562
8161b5b9
VZ
563// ----------------------------------------------------------------------------
564// adding items
565// ----------------------------------------------------------------------------
566
2ee3ee1b 567void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
bb0ca8df 568{
223d09f6 569 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
bb0ca8df 570
11e1c70d
RR
571 // VZ: notice that InsertItems knows nothing about sorting, so calling it
572 // from outside (and not from our own Append) is likely to break
573 // everything
574
575 // code elsewhere supposes we have as many items in m_clientList as items
2ee3ee1b 576 // in the listbox
11e1c70d 577 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
2ee3ee1b
VZ
578 wxT("bug in client data management") );
579
9f884528
RD
580 InvalidateBestSize();
581
bb0ca8df
VZ
582 GList *children = m_list->children;
583 int length = g_list_length(children);
9ef6ff8c 584
223d09f6 585 wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") );
bb0ca8df 586
2ee3ee1b 587 size_t nItems = items.GetCount();
0a07a7d8 588 int index;
2ee3ee1b 589
0a07a7d8 590 if (m_strings)
bb0ca8df 591 {
0a07a7d8 592 for (size_t n = 0; n < nItems; n++)
bb0ca8df 593 {
0a07a7d8 594 index = m_strings->Add( items[n] );
f96b15a3 595
0a07a7d8
RR
596 if (index != GetCount())
597 {
598 GtkAddItem( items[n], index );
222ed1d6 599 wxList::compatibility_iterator node = m_clientList.Item( index );
0a07a7d8
RR
600 m_clientList.Insert( node, (wxObject*) NULL );
601 }
602 else
603 {
604 GtkAddItem( items[n] );
605 m_clientList.Append( (wxObject*) NULL );
606 }
bb0ca8df 607 }
bb0ca8df 608 }
11e1c70d 609 else
bb0ca8df 610 {
0a07a7d8
RR
611 if (pos == length)
612 {
613 for ( size_t n = 0; n < nItems; n++ )
614 {
615 GtkAddItem( items[n] );
616
617 m_clientList.Append((wxObject *)NULL);
618 }
619 }
620 else
bb0ca8df 621 {
222ed1d6 622 wxList::compatibility_iterator node = m_clientList.Item( pos );
0a07a7d8
RR
623 for ( size_t n = 0; n < nItems; n++ )
624 {
625 GtkAddItem( items[n], pos+n );
bb0ca8df 626
0a07a7d8
RR
627 m_clientList.Insert( node, (wxObject *)NULL );
628 }
bb0ca8df
VZ
629 }
630 }
2ee3ee1b 631
11e1c70d
RR
632 wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(),
633 wxT("bug in client data management") );
2ee3ee1b
VZ
634}
635
636int wxListBox::DoAppend( const wxString& item )
637{
9f884528
RD
638 InvalidateBestSize();
639
11e1c70d 640 if (m_strings)
2ee3ee1b
VZ
641 {
642 // need to determine the index
11e1c70d 643 int index = m_strings->Add( item );
9ef6ff8c
VZ
644
645 // only if not at the end anyway
646 if (index != GetCount())
647 {
648 GtkAddItem( item, index );
649
222ed1d6 650 wxList::compatibility_iterator node = m_clientList.Item( index );
11e1c70d 651 m_clientList.Insert( node, (wxObject *)NULL );
9ef6ff8c
VZ
652
653 return index;
654 }
2ee3ee1b 655 }
9ef6ff8c 656
11e1c70d 657 GtkAddItem(item);
2ee3ee1b 658
11e1c70d 659 m_clientList.Append((wxObject *)NULL);
2ee3ee1b 660
11e1c70d 661 return GetCount() - 1;
bb0ca8df
VZ
662}
663
11e1c70d 664void wxListBox::GtkAddItem( const wxString &item, int pos )
c801d85f 665{
223d09f6 666 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
ff8bfdbb 667
caaa4cfd 668 GtkWidget *list_item;
ff8bfdbb 669
bb0ca8df 670 wxString label(item);
88ac883a 671#if wxUSE_CHECKLISTBOX
caaa4cfd
RR
672 if (m_hasCheckBoxes)
673 {
d752a3c3 674 label.Prepend(wxCHECKLBOX_STRING);
caaa4cfd 675 }
88ac883a 676#endif // wxUSE_CHECKLISTBOX
fc54776e 677
fab591c5 678 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
bb0ca8df 679
11e1c70d
RR
680 GList *gitem_list = g_list_alloc ();
681 gitem_list->data = list_item;
9ef6ff8c 682
11e1c70d
RR
683 if (pos == -1)
684 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
685 else
686 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
3502e687 687
c4526184 688 gtk_signal_connect_after( GTK_OBJECT(list_item), "select",
fd0eed64 689 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 690
159b66c0 691 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
c4526184 692 gtk_signal_connect_after( GTK_OBJECT(list_item), "deselect",
65045edd 693 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
dcf40a56 694
ff8bfdbb
VZ
695 gtk_signal_connect( GTK_OBJECT(list_item),
696 "button_press_event",
697 (GtkSignalFunc)gtk_listbox_button_press_callback,
698 (gpointer) this );
699
74505862
RR
700 gtk_signal_connect_after( GTK_OBJECT(list_item),
701 "button_release_event",
702 (GtkSignalFunc)gtk_listbox_button_release_callback,
703 (gpointer) this );
704
354aa1e3 705 gtk_signal_connect( GTK_OBJECT(list_item),
bb0ca8df
VZ
706 "key_press_event",
707 (GtkSignalFunc)gtk_listbox_key_press_callback,
708 (gpointer)this );
ff8bfdbb 709
c9433ea9
RR
710
711 gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event",
712 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this );
713
714 gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event",
715 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this );
716
fd0eed64
RR
717 ConnectWidget( list_item );
718
2b07d713
RR
719 if (GTK_WIDGET_REALIZED(m_widget))
720 {
f2e93537
RR
721 gtk_widget_show( list_item );
722
2b07d713
RR
723 gtk_widget_realize( list_item );
724 gtk_widget_realize( GTK_BIN(list_item)->child );
014b0d06 725
291a8f20 726#if wxUSE_TOOLTIPS
f03fc89f 727 if (m_tooltip) m_tooltip->Apply( this );
291a8f20 728#endif
2b07d713 729 }
631a05be
RL
730
731 // Apply current widget style to the new list_item
732 GtkRcStyle *style = CreateWidgetStyle();
733 if (style)
734 {
735 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
736 GtkBin *bin = GTK_BIN( list_item );
737 GtkWidget *label = GTK_WIDGET( bin->child );
738 gtk_widget_modify_style( label, style );
739 gtk_rc_style_unref( style );
740 }
fd0eed64
RR
741}
742
2ee3ee1b
VZ
743void wxListBox::DoSetItems( const wxArrayString& items,
744 void **clientData)
fd0eed64 745{
2ee3ee1b 746 Clear();
fd0eed64 747
2ee3ee1b 748 DoInsertItems(items, 0);
ff8bfdbb 749
2ee3ee1b
VZ
750 if ( clientData )
751 {
752 size_t count = items.GetCount();
753 for ( size_t n = 0; n < count; n++ )
754 {
755 SetClientData(n, clientData[n]);
756 }
757 }
fd0eed64 758}
dcf40a56 759
2ee3ee1b 760// ----------------------------------------------------------------------------
8161b5b9 761// deleting items
2ee3ee1b 762// ----------------------------------------------------------------------------
ff8bfdbb 763
fd0eed64
RR
764void wxListBox::Clear()
765{
223d09f6 766 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 767
c4c92801 768 gtk_list_clear_items( m_list, 0, GetCount() );
8161b5b9 769
780bb874
RR
770 if ( GTK_LIST(m_list)->last_focus_child != NULL )
771 {
772 // This should be NULL, I think.
773 GTK_LIST(m_list)->last_focus_child = NULL;
774 }
dcf40a56 775
6c8a980f
VZ
776 if ( HasClientObjectData() )
777 {
778 // destroy the data (due to Robert's idea of using wxList<wxObject>
779 // and not wxList<wxClientData> we can't just say
780 // m_clientList.DeleteContents(TRUE) - this would crash!
222ed1d6 781 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
782 while ( node )
783 {
b1d4dd7a
RL
784 delete (wxClientData *)node->GetData();
785 node = node->GetNext();
6c8a980f
VZ
786 }
787 }
11e1c70d 788 m_clientList.Clear();
ff8bfdbb 789
2ee3ee1b
VZ
790 if ( m_strings )
791 m_strings->Clear();
6de97a3b 792}
c801d85f
KB
793
794void wxListBox::Delete( int n )
795{
223d09f6 796 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 797
fd0eed64 798 GList *child = g_list_nth( m_list->children, n );
dcf40a56 799
223d09f6 800 wxCHECK_RET( child, wxT("wrong listbox index") );
dcf40a56 801
bbe0af5b 802 GList *list = g_list_append( (GList*) NULL, child->data );
fd0eed64
RR
803 gtk_list_remove_items( m_list, list );
804 g_list_free( list );
dcf40a56 805
222ed1d6 806 wxList::compatibility_iterator node = m_clientList.Item( n );
2ee3ee1b 807 if ( node )
fd0eed64 808 {
1e6feb95 809 if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b 810 {
b1d4dd7a 811 wxClientData *cd = (wxClientData*)node->GetData();
2ee3ee1b
VZ
812 delete cd;
813 }
814
222ed1d6 815 m_clientList.Erase( node );
f5e27805 816 }
ff8bfdbb 817
2ee3ee1b 818 if ( m_strings )
ba8c1601 819 m_strings->RemoveAt(n);
2ee3ee1b
VZ
820}
821
8161b5b9
VZ
822// ----------------------------------------------------------------------------
823// client data
824// ----------------------------------------------------------------------------
825
826void wxListBox::DoSetItemClientData( int n, void* clientData )
827{
828 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
829
222ed1d6 830 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
831 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
832
833 node->SetData( (wxObject*) clientData );
834}
835
836void* wxListBox::DoGetItemClientData( int n ) const
837{
838 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
839
222ed1d6 840 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
841 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
842
b1d4dd7a 843 return node->GetData();
8161b5b9
VZ
844}
845
846void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
847{
848 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
849
222ed1d6 850 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
851 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") );
852
853 // wxItemContainer already deletes data for us
854
855 node->SetData( (wxObject*) clientData );
856}
857
858wxClientData* wxListBox::DoGetItemClientObject( int n ) const
859{
860 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") );
861
222ed1d6 862 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
863 wxCHECK_MSG( node, (wxClientData *)NULL,
864 wxT("invalid index in wxListBox::DoGetItemClientObject") );
865
b1d4dd7a 866 return (wxClientData*) node->GetData();
8161b5b9
VZ
867}
868
2ee3ee1b
VZ
869// ----------------------------------------------------------------------------
870// string list access
871// ----------------------------------------------------------------------------
872
8161b5b9
VZ
873wxString wxListBox::GetRealLabel(GList *item) const
874{
875 GtkBin *bin = GTK_BIN( item->data );
876 GtkLabel *label = GTK_LABEL( bin->child );
877
878 wxString str;
879
880#ifdef __WXGTK20__
881 str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
882#else
883 str = wxString( label->label );
884#endif
885
886#if wxUSE_CHECKLISTBOX
887