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