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