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