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