]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
Add wxDocManager::FindTemplate() method.
[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"
e6e83933 22 #include "wx/checklst.h"
aaa6d89a 23 #include "wx/arrstr.h"
ad9835c9
WS
24#endif
25
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" {
89954433
VZ
96static gint gtk_listitem_focus_in_callback( GtkWidget *WXUNUSED(widget),
97 GdkEvent *WXUNUSED(event),
98 wxWindow *win )
c9433ea9
RR
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 112 wxChildFocusEvent eventChildFocus(win);
937013e0 113 (void)win->HandleWindowEvent(eventChildFocus);
c9433ea9
RR
114
115 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
116 eventFocus.SetEventObject(win);
117
937013e0 118 (void)win->HandleWindowEvent(eventFocus);
c9433ea9
RR
119 }
120
121 return FALSE;
122}
865bb325 123}
c9433ea9
RR
124
125//-----------------------------------------------------------------------------
126// "focus_out_event"
127//-----------------------------------------------------------------------------
128
865bb325 129extern "C" {
89954433
VZ
130static gint gtk_listitem_focus_out_callback( GtkWidget *WXUNUSED(widget),
131 GdkEventFocus *WXUNUSED(gdk_event),
132 wxWindowGTK *win )
c9433ea9
RR
133{
134 if (g_isIdle)
135 wxapp_install_idle_handler();
136
d3b9f782 137 g_focusWindow = NULL;
c9433ea9
RR
138
139 // don't send the window a kill focus event if it thinks that it doesn't
140 // have focus already
141 if ( win->m_hasFocus )
142 {
caf6e6de 143 win->m_hasFocus = false;
c9433ea9
RR
144
145 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
146 event.SetEventObject( win );
147
148 // even if we did process the event in wx code, still let GTK itself
149 // process it too as otherwise bad things happen, especially in GTK2
150 // where the text control simply aborts the program if it doesn't get
151 // the matching focus out event
937013e0 152 (void)win->HandleWindowEvent( event );
c9433ea9
RR
153 }
154
155 return FALSE;
156}
865bb325 157}
c9433ea9 158
caaa4cfd 159//-----------------------------------------------------------------------------
7a30ee8f 160// "button_release_event"
caaa4cfd
RR
161//-----------------------------------------------------------------------------
162
7a30ee8f
RR
163/* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once
164 a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the
165 listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event.
166 this can lead to race conditions so that we emit the dclick event
167 after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */
168
865bb325 169extern "C" {
ff8bfdbb 170static gint
014b0d06
VZ
171gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget),
172 GdkEventButton * WXUNUSED(gdk_event),
173 wxListBox *listbox )
caaa4cfd 174{
acfd422a
RR
175 if (g_isIdle) wxapp_install_idle_handler();
176
caaa4cfd
RR
177 if (g_blockEventsOnDrag) return FALSE;
178 if (g_blockEventsOnScroll) return FALSE;
179
a2053b27 180 if (!listbox->m_hasVMT) return FALSE;
caaa4cfd 181
7a30ee8f 182 if (!g_hasDoubleClicked) return FALSE;
ff8bfdbb 183
6c8a980f
VZ
184 wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
185 event.SetEventObject( listbox );
ff8bfdbb 186
6c8a980f
VZ
187 wxArrayInt aSelections;
188 int n, count = listbox->GetSelections(aSelections);
189 if ( count > 0 )
190 {
191 n = aSelections[0];
192 if ( listbox->HasClientObjectData() )
193 event.SetClientObject( listbox->GetClientObject(n) );
194 else if ( listbox->HasClientUntypedData() )
195 event.SetClientData( listbox->GetClientData(n) );
196 event.SetString( listbox->GetString(n) );
197 }
198 else
199 {
200 n = -1;
201 }
5b077d48 202
687706f5 203 event.SetInt(n);
6c8a980f 204
937013e0 205 listbox->HandleWindowEvent( event );
ff8bfdbb 206
7a30ee8f
RR
207 return FALSE;
208}
865bb325 209}
7a30ee8f
RR
210
211//-----------------------------------------------------------------------------
212// "button_press_event"
213//-----------------------------------------------------------------------------
214
865bb325 215extern "C" {
7a30ee8f 216static gint
014b0d06
VZ
217gtk_listbox_button_press_callback( GtkWidget *widget,
218 GdkEventButton *gdk_event,
219 wxListBox *listbox )
7a30ee8f
RR
220{
221 if (g_isIdle) wxapp_install_idle_handler();
222
223 if (g_blockEventsOnDrag) return FALSE;
224 if (g_blockEventsOnScroll) return FALSE;
225
226 if (!listbox->m_hasVMT) return FALSE;
227
11e1c70d 228 int sel = listbox->GtkGetIndex( widget );
7a30ee8f 229
88ac883a 230#if wxUSE_CHECKLISTBOX
7a30ee8f
RR
231 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
232 {
233 wxCheckListBox *clb = (wxCheckListBox *)listbox;
234
235 clb->Check( sel, !clb->IsChecked(sel) );
236
237 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
238 event.SetEventObject( listbox );
239 event.SetInt( sel );
937013e0 240 listbox->HandleWindowEvent( event );
4f22cf8d 241 }
88ac883a 242#endif // wxUSE_CHECKLISTBOX
014b0d06 243
4fcfa27c
RR
244 if ((gdk_event->state == 0) &&
245 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
246 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
247 {
caf6e6de 248 listbox->m_blockEvent = true;
376c0148
RR
249
250 int i;
251 for (i = 0; i < (int)listbox->GetCount(); i++)
252 if (i != sel)
253 gtk_list_unselect_item( GTK_LIST(listbox->m_list), i );
17a1ebd1 254
caf6e6de 255 listbox->m_blockEvent = false;
17a1ebd1 256
376c0148 257 return false;
4fcfa27c
RR
258 }
259
7a30ee8f
RR
260 /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */
261 g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS);
4f22cf8d 262
caaa4cfd
RR
263 return FALSE;
264}
865bb325 265}
66bd6b93 266
1144d24d
RR
267//-----------------------------------------------------------------------------
268// "key_press_event"
269//-----------------------------------------------------------------------------
270
865bb325 271extern "C" {
ff8bfdbb 272static gint
1144d24d
RR
273gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox )
274{
9ef6ff8c 275 if (g_isIdle)
354aa1e3 276 wxapp_install_idle_handler();
acfd422a 277
9ef6ff8c 278 if (g_blockEventsOnDrag)
354aa1e3 279 return FALSE;
1144d24d 280
caf6e6de 281 bool ret = false;
1144d24d 282
354aa1e3
RR
283 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
284 {
285 wxNavigationKeyEvent new_event;
286 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
287 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
288 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
289 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
290 new_event.SetCurrentFocus( listbox );
937013e0 291 ret = listbox->HandleWindowEvent( new_event );
354aa1e3 292 }
9ef6ff8c 293
4a82abc8
RR
294 if ((gdk_event->keyval == GDK_Return) && (!ret))
295 {
296 // eat return in all modes
caf6e6de 297 ret = true;
4a82abc8 298 }
f96b15a3 299
354aa1e3 300#if wxUSE_CHECKLISTBOX
1e8d2f69 301 if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret))
354aa1e3
RR
302 {
303 int sel = listbox->GtkGetIndex( widget );
ff8bfdbb 304
354aa1e3 305 wxCheckListBox *clb = (wxCheckListBox *)listbox;
ff8bfdbb 306
354aa1e3 307 clb->Check( sel, !clb->IsChecked(sel) );
ff8bfdbb 308
354aa1e3
RR
309 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
310 new_event.SetEventObject( listbox );
311 new_event.SetInt( sel );
937013e0 312 ret = listbox->HandleWindowEvent( new_event );
354aa1e3
RR
313 }
314#endif // wxUSE_CHECKLISTBOX
ff8bfdbb 315
fec195b2 316 // Check or uncheck item with SPACE
17a1ebd1 317 if ((gdk_event->keyval == ' ') && (!ret) &&
fec195b2
RR
318 (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
319 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
320 {
321 int sel = listbox->GtkGetIndex( widget );
17a1ebd1 322
fec195b2
RR
323 if (sel != -1)
324 {
caf6e6de 325 ret = true;
17a1ebd1 326
fec195b2
RR
327 if (listbox->IsSelected( sel ))
328 gtk_list_unselect_item( listbox->m_list, sel );
329 else
330 gtk_list_select_item( listbox->m_list, sel );
17a1ebd1 331
fec195b2
RR
332 wxCommandEvent new_event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
333 new_event.SetEventObject( listbox );
334 wxArrayInt aSelections;
335 int n, count = listbox->GetSelections(aSelections);
336 if ( count > 0 )
337 {
338 n = aSelections[0];
339 if ( listbox->HasClientObjectData() )
340 new_event.SetClientObject( listbox->GetClientObject(n) );
341 else if ( listbox->HasClientUntypedData() )
342 new_event.SetClientData( listbox->GetClientData(n) );
343 new_event.SetString( listbox->GetString(n) );
344 }
345 else
346 {
347 n = -1;
348 }
687706f5 349 new_event.SetInt(n);
937013e0 350 listbox->HandleWindowEvent( new_event );
fec195b2
RR
351 }
352 }
17a1ebd1 353
354aa1e3
RR
354 if (ret)
355 {
356 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
357 return TRUE;
358 }
ff8bfdbb 359
1144d24d
RR
360 return FALSE;
361}
865bb325 362}
1144d24d 363
c801d85f 364//-----------------------------------------------------------------------------
a60c99e6 365// "select" and "deselect"
c801d85f
KB
366//-----------------------------------------------------------------------------
367
9c9c3d7a
VZ
368static void gtk_listitem_select_cb( GtkWidget *widget,
369 wxListBox *listbox,
370 bool is_selection )
c801d85f 371{
acfd422a
RR
372 if (g_isIdle) wxapp_install_idle_handler();
373
a2053b27 374 if (!listbox->m_hasVMT) return;
fd0eed64 375 if (g_blockEventsOnDrag) return;
8161b5b9 376
bac95742 377 if (listbox->m_blockEvent) return;
9ef6ff8c 378
fd0eed64 379 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
6c8a980f 380 event.SetEventObject( listbox );
8161b5b9 381
9c9c3d7a
VZ
382 // indicate whether this is a selection or a deselection
383 event.SetExtraLong( is_selection );
159b66c0
RR
384
385 if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0)
386 {
387 int sel = listbox->GtkGetIndex( widget );
388
389 if (listbox->m_prevSelection != sel)
390 gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection );
391
392 listbox->m_prevSelection = sel;
393 }
dcf40a56 394
09cf7c58 395 wxArrayInt aSelections;
2ee3ee1b 396 int n, count = listbox->GetSelections(aSelections);
09cf7c58
RR
397 if ( count > 0 )
398 {
2ee3ee1b 399 n = aSelections[0];
6c8a980f
VZ
400 if ( listbox->HasClientObjectData() )
401 event.SetClientObject( listbox->GetClientObject(n) );
402 else if ( listbox->HasClientUntypedData() )
403 event.SetClientData( listbox->GetClientData(n) );
404 event.SetString( listbox->GetString(n) );
09cf7c58
RR
405 }
406 else
407 {
2ee3ee1b 408 n = -1;
09cf7c58
RR
409 }
410
687706f5 411 event.SetInt(n);
2ee3ee1b 412
159b66c0
RR
413// No longer required with new code in wxLB_SINGLE
414// listbox->GetEventHandler()->AddPendingEvent( event );
937013e0 415 listbox->HandleWindowEvent( event );
6de97a3b 416}
c801d85f 417
865bb325
VZ
418extern "C" {
419static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
420{
421 gtk_listitem_select_cb( widget, listbox, TRUE );
422}
423}
424
425extern "C" {
426static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox )
427{
428 gtk_listitem_select_cb( widget, listbox, FALSE );
429}
430}
431
a60c99e6
RR
432//-----------------------------------------------------------------------------
433// wxListBox
c801d85f
KB
434//-----------------------------------------------------------------------------
435
865bb325 436extern "C" {
f2e93537 437static gint
89954433 438gtk_listbox_realized_callback( GtkWidget *WXUNUSED(widget), wxListBox *win )
f2e93537
RR
439{
440 if (g_isIdle)
441 wxapp_install_idle_handler();
17a1ebd1 442
f2e93537
RR
443 GList *child = win->m_list->children;
444 for (child = win->m_list->children; child != NULL; child = child->next)
445 gtk_widget_show( GTK_WIDGET(child->data) );
17a1ebd1 446
f2e93537
RR
447 return false;
448}
865bb325 449}
f2e93537
RR
450
451//-----------------------------------------------------------------------------
452// wxListBox
453//-----------------------------------------------------------------------------
454
b1294ada 455IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
c801d85f 456
2ee3ee1b
VZ
457// ----------------------------------------------------------------------------
458// construction
459// ----------------------------------------------------------------------------
460
fd0eed64 461wxListBox::wxListBox()
c801d85f 462{
d3b9f782 463 m_list = NULL;
88ac883a 464#if wxUSE_CHECKLISTBOX
caf6e6de 465 m_hasCheckBoxes = false;
88ac883a 466#endif // wxUSE_CHECKLISTBOX
6de97a3b 467}
c801d85f 468
584ad2a3
MB
469bool wxListBox::Create( wxWindow *parent, wxWindowID id,
470 const wxPoint &pos, const wxSize &size,
471 const wxArrayString& choices,
472 long style, const wxValidator& validator,
473 const wxString &name )
474{
475 wxCArrayString chs(choices);
476
477 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
478 style, validator, name );
479}
480
dcf40a56 481bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
482 const wxPoint &pos, const wxSize &size,
483 int n, const wxString choices[],
2ee3ee1b
VZ
484 long style, const wxValidator& validator,
485 const wxString &name )
c801d85f 486{
caf6e6de
WS
487 m_needParent = true;
488 m_acceptsFocus = true;
159b66c0 489 m_prevSelection = 0; // or -1 ??
caf6e6de 490 m_blockEvent = false;
dcf40a56 491
4dcaf11a
RR
492 if (!PreCreation( parent, pos, size ) ||
493 !CreateBase( parent, id, pos, size, style, validator, name ))
494 {
223d09f6 495 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 496 return false;
4dcaf11a 497 }
6de97a3b 498
d3b9f782 499 m_widget = gtk_scrolled_window_new( NULL, NULL );
034be888
RR
500 if (style & wxLB_ALWAYS_SB)
501 {
502 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
503 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
504 }
505 else
506 {
507 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
508 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
509 }
ff8bfdbb 510
fd0eed64 511 m_list = GTK_LIST( gtk_list_new() );
dcf40a56 512
4a82abc8 513 GtkSelectionMode mode;
fd0eed64 514 if (style & wxLB_MULTIPLE)
4a82abc8 515 {
fd0eed64 516 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 517 }
fd0eed64 518 else if (style & wxLB_EXTENDED)
4a82abc8 519 {
fd0eed64 520 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
521 }
522 else
523 {
524 // if style was 0 set single mode
525 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 526 mode = GTK_SELECTION_SINGLE;
4a82abc8 527 }
6a6d4eed 528
fd0eed64 529 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
dcf40a56 530
38c7b3d3 531 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) );
38c7b3d3 532
e115e771
RR
533 /* make list scroll when moving the focus down using cursor keys */
534 gtk_container_set_focus_vadjustment(
535 GTK_CONTAINER(m_list),
536 gtk_scrolled_window_get_vadjustment(
2ee3ee1b 537 GTK_SCROLLED_WINDOW(m_widget)));
e115e771 538
fd0eed64 539 gtk_widget_show( GTK_WIDGET(m_list) );
dcf40a56 540
f2e93537
RR
541 gtk_signal_connect( GTK_OBJECT(m_list), "realize",
542 GTK_SIGNAL_FUNC(gtk_listbox_realized_callback), (gpointer) this );
17a1ebd1 543
2ee3ee1b
VZ
544 if ( style & wxLB_SORT )
545 {
a236aa20 546 // this will change Append() behaviour
2ee3ee1b
VZ
547 m_strings = new wxSortedArrayString;
548 }
eb553cb2
VZ
549 else
550 {
d3b9f782 551 m_strings = NULL;
eb553cb2 552 }
2ee3ee1b 553
a236aa20 554 Append(n, choices);
dcf40a56 555
f03fc89f 556 m_parent->DoAddChild( this );
ff8bfdbb 557
abdeb9e7 558 PostCreation(size);
170acdc9 559 SetInitialSize(size); // need this too because this is a wxControlWithItems
dcf40a56 560
caf6e6de 561 return true;
6de97a3b 562}
c801d85f 563
fd0eed64 564wxListBox::~wxListBox()
c801d85f 565{
caf6e6de 566 m_hasVMT = false;
4a82abc8 567
caaa4cfd 568 Clear();
f96b15a3 569
89954433 570 delete m_strings;
6de97a3b 571}
c801d85f 572
8161b5b9
VZ
573// ----------------------------------------------------------------------------
574// adding items
575// ----------------------------------------------------------------------------
576
a236aa20
VZ
577int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
578 unsigned int pos,
579 void **clientData,
580 wxClientDataType type)
bb0ca8df 581{
a236aa20 582 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
bb0ca8df 583
a236aa20
VZ
584 const unsigned count = GetCount();
585 wxCHECK_MSG( pos <= count, wxNOT_FOUND,
586 wxT("invalid index in wxListBox::InsertItems") );
11e1c70d
RR
587
588 // code elsewhere supposes we have as many items in m_clientList as items
2ee3ee1b 589 // in the listbox
a236aa20
VZ
590 wxASSERT_MSG( m_clientList.GetCount() == count,
591 wxT("bug in client data management") );
2ee3ee1b 592
9f884528
RD
593 InvalidateBestSize();
594
a236aa20 595 const unsigned numItems = items.GetCount();
9ef6ff8c 596
a236aa20 597 for ( unsigned int n = 0; n < numItems; ++n, ++pos )
bb0ca8df 598 {
a236aa20 599 const wxString& item = items[n];
f96b15a3 600
a236aa20
VZ
601 const unsigned idx = m_strings ? m_strings->Add(item)
602 : pos;
0a07a7d8 603
934960d1 604 GtkAddItem(item, idx == GetCount() ? (unsigned) -1 : idx);
bb0ca8df 605
a236aa20
VZ
606 m_clientList.Insert(idx, NULL);
607
608 AssignNewItemClientData(idx, clientData, n, type);
bb0ca8df 609 }
2ee3ee1b 610
8228b893 611 wxASSERT_MSG( m_clientList.GetCount() == GetCount(),
11e1c70d 612 wxT("bug in client data management") );
9ef6ff8c 613
a236aa20 614 return pos - 1;
bb0ca8df
VZ
615}
616
11e1c70d 617void wxListBox::GtkAddItem( const wxString &item, int pos )
c801d85f 618{
223d09f6 619 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
ff8bfdbb 620
caaa4cfd 621 GtkWidget *list_item;
ff8bfdbb 622
bb0ca8df 623 wxString label(item);
88ac883a 624#if wxUSE_CHECKLISTBOX
caaa4cfd
RR
625 if (m_hasCheckBoxes)
626 {
d752a3c3 627 label.Prepend(wxCHECKLBOX_STRING);
caaa4cfd 628 }
88ac883a 629#endif // wxUSE_CHECKLISTBOX
fc54776e 630
fab591c5 631 list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) );
bb0ca8df 632
11e1c70d
RR
633 GList *gitem_list = g_list_alloc ();
634 gitem_list->data = list_item;
9ef6ff8c 635
11e1c70d
RR
636 if (pos == -1)
637 gtk_list_append_items( GTK_LIST (m_list), gitem_list );
638 else
639 gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos );
3502e687 640
c4526184 641 gtk_signal_connect_after( GTK_OBJECT(list_item), "select",
fd0eed64 642 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
dcf40a56 643
159b66c0 644 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
c4526184 645 gtk_signal_connect_after( GTK_OBJECT(list_item), "deselect",
65045edd 646 GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this );
dcf40a56 647
ff8bfdbb
VZ
648 gtk_signal_connect( GTK_OBJECT(list_item),
649 "button_press_event",
650 (GtkSignalFunc)gtk_listbox_button_press_callback,
651 (gpointer) this );
652
74505862
RR
653 gtk_signal_connect_after( GTK_OBJECT(list_item),
654 "button_release_event",
655 (GtkSignalFunc)gtk_listbox_button_release_callback,
656 (gpointer) this );
657
354aa1e3 658 gtk_signal_connect( GTK_OBJECT(list_item),
bb0ca8df
VZ
659 "key_press_event",
660 (GtkSignalFunc)gtk_listbox_key_press_callback,
661 (gpointer)this );
ff8bfdbb 662
c9433ea9
RR
663
664 gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event",
665 GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this );
666
667 gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event",
668 GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this );
669
fd0eed64
RR
670 ConnectWidget( list_item );
671
2b07d713
RR
672 if (GTK_WIDGET_REALIZED(m_widget))
673 {
f2e93537 674 gtk_widget_show( list_item );
17a1ebd1 675
2b07d713
RR
676 gtk_widget_realize( list_item );
677 gtk_widget_realize( GTK_BIN(list_item)->child );
014b0d06 678
291a8f20 679#if wxUSE_TOOLTIPS
f03fc89f 680 if (m_tooltip) m_tooltip->Apply( this );
291a8f20 681#endif
2b07d713 682 }
631a05be
RL
683
684 // Apply current widget style to the new list_item
685 GtkRcStyle *style = CreateWidgetStyle();
686 if (style)
687 {
688 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
689 GtkBin *bin = GTK_BIN( list_item );
17a1ebd1 690 gtk_widget_modify_style( GTK_WIDGET( bin->child ), style );
631a05be
RL
691 gtk_rc_style_unref( style );
692 }
fd0eed64
RR
693}
694
2ee3ee1b 695// ----------------------------------------------------------------------------
8161b5b9 696// deleting items
2ee3ee1b 697// ----------------------------------------------------------------------------
ff8bfdbb 698
a236aa20 699void wxListBox::DoClear()
fd0eed64 700{
223d09f6 701 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 702
8228b893 703 gtk_list_clear_items( m_list, 0, (int)GetCount() );
8161b5b9 704
780bb874
RR
705 if ( GTK_LIST(m_list)->last_focus_child != NULL )
706 {
707 // This should be NULL, I think.
708 GTK_LIST(m_list)->last_focus_child = NULL;
709 }
dcf40a56 710
11e1c70d 711 m_clientList.Clear();
ff8bfdbb 712
2ee3ee1b
VZ
713 if ( m_strings )
714 m_strings->Clear();
6de97a3b 715}
c801d85f 716
a236aa20 717void wxListBox::DoDeleteOneItem(unsigned int n)
c801d85f 718{
223d09f6 719 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
fc54776e 720
fd0eed64 721 GList *child = g_list_nth( m_list->children, n );
dcf40a56 722
223d09f6 723 wxCHECK_RET( child, wxT("wrong listbox index") );
dcf40a56 724
d3b9f782 725 GList *list = g_list_append( NULL, child->data );
fd0eed64
RR
726 gtk_list_remove_items( m_list, list );
727 g_list_free( list );
dcf40a56 728
222ed1d6 729 wxList::compatibility_iterator node = m_clientList.Item( n );
2ee3ee1b 730 if ( node )
fd0eed64 731 {
222ed1d6 732 m_clientList.Erase( node );
f5e27805 733 }
ff8bfdbb 734
2ee3ee1b 735 if ( m_strings )
ba8c1601 736 m_strings->RemoveAt(n);
2ee3ee1b
VZ
737}
738
8161b5b9
VZ
739// ----------------------------------------------------------------------------
740// client data
741// ----------------------------------------------------------------------------
742
aa61d352 743void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9
VZ
744{
745 wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") );
746
222ed1d6 747 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
748 wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") );
749
750 node->SetData( (wxObject*) clientData );
751}
752
aa61d352 753void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9
VZ
754{
755 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") );
756
222ed1d6 757 wxList::compatibility_iterator node = m_clientList.Item( n );
8161b5b9
VZ
758 wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") );
759
b1d4dd7a 760 return node->GetData();
8161b5b9
VZ
761}
762
2ee3ee1b
VZ
763// ----------------------------------------------------------------------------
764// string list access
765// ----------------------------------------------------------------------------
766
8161b5b9
VZ
767wxString wxListBox::GetRealLabel(GList *item) const
768{
769 GtkBin *bin = GTK_BIN( item->data );
770 GtkLabel *label = GTK_LABEL( bin->child );
771
772 wxString str;
773
8161b5b9 774 str = wxString( label->label );
8161b5b9
VZ
775
776#if wxUSE_CHECKLISTBOX
e9670814 777 // checklistboxes have "[±] " prepended to their lables, remove it
8161b5b9 778 //
3cbab641 779 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk1/checklst.h
8161b5b9
VZ
780 if ( m_hasCheckBoxes )
781 str.erase(0, 4);
782#endif // wxUSE_CHECKLISTBOX
783
784 return str;
785}
786
aa61d352 787void wxListBox::SetString(unsigned int n, const wxString &string)
2ee3ee1b
VZ
788{
789 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
790
791 GList *child = g_list_nth( m_list->children, n );
792 if (child)
793 {
794 GtkBin *bin = GTK_BIN( child->data );
795 GtkLabel *label = GTK_LABEL( bin->child );
796
797 wxString str;
798#if wxUSE_CHECKLISTBOX
799 if (m_hasCheckBoxes)
d752a3c3 800 str += wxCHECKLBOX_STRING;
2ee3ee1b
VZ
801#endif // wxUSE_CHECKLISTBOX
802 str += string;
803
fab591c5 804 gtk_label_set( label, wxGTK_CONV( str ) );
2ee3ee1b
VZ
805 }
806 else
f5e27805 807 {
2ee3ee1b 808 wxFAIL_MSG(wxT("wrong listbox index"));
fd0eed64 809 }
6de97a3b 810}
c801d85f 811
aa61d352 812wxString wxListBox::GetString(unsigned int n) const
c801d85f 813{
11e62fe6 814 wxCHECK_MSG( m_list != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 815
2ee3ee1b
VZ
816 GList *child = g_list_nth( m_list->children, n );
817 if (child)
818 {
8161b5b9 819 return GetRealLabel(child);
2ee3ee1b
VZ
820 }
821
822 wxFAIL_MSG(wxT("wrong listbox index"));
823
11e62fe6 824 return wxEmptyString;
2ee3ee1b
VZ
825}
826
aa61d352 827unsigned int wxListBox::GetCount() const
2ee3ee1b 828{
8228b893 829 wxCHECK_MSG( m_list != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 830
11e1c70d
RR
831 GList *children = m_list->children;
832 return g_list_length(children);
6de97a3b 833}
c801d85f 834
11e62fe6 835int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 836{
11e62fe6 837 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 838
fd0eed64
RR
839 GList *child = m_list->children;
840 int count = 0;
841 while (child)
842 {
11e62fe6 843 if ( item.IsSameAs( GetRealLabel(child), bCase ) )
bb0ca8df 844 return count;
ff8bfdbb 845
fd0eed64
RR
846 count++;
847 child = child->next;
848 }
849
2ee3ee1b 850 // it's not an error if the string is not found -> no wxCHECK
dcf40a56 851
2ee3ee1b 852 return wxNOT_FOUND;
6de97a3b 853}
c801d85f 854
2ee3ee1b
VZ
855// ----------------------------------------------------------------------------
856// selection
857// ----------------------------------------------------------------------------
858
fd0eed64 859int wxListBox::GetSelection() const
c801d85f 860{
e6e83933 861 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 862
fd0eed64
RR
863 GList *child = m_list->children;
864 int count = 0;
865 while (child)
866 {
867 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count;
868 count++;
869 child = child->next;
870 }
e6e83933 871 return wxNOT_FOUND;
6de97a3b 872}
c801d85f 873
fd0eed64 874int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 875{
e6e83933 876 wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 877
fd0eed64
RR
878 // get the number of selected items first
879 GList *child = m_list->children;
880 int count = 0;
881 for (child = m_list->children; child != NULL; child = child->next)
882 {
883 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
884 count++;
885 }
c801d85f 886
fd0eed64
RR
887 aSelections.Empty();
888
ff8bfdbb 889 if (count > 0)
868a2826 890 {
fd0eed64
RR
891 // now fill the list
892 aSelections.Alloc(count); // optimization attempt
893 int i = 0;
894 for (child = m_list->children; child != NULL; child = child->next, i++)
895 {
896 if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED)
897 aSelections.Add(i);
898 }
6a6d4eed 899 }
dcf40a56 900
fd0eed64 901 return count;
6de97a3b 902}
c801d85f 903
2ee3ee1b 904bool wxListBox::IsSelected( int n ) const
c801d85f 905{
caf6e6de 906 wxCHECK_MSG( m_list != NULL, false, wxT("invalid listbox") );
ff8bfdbb 907
fd0eed64 908 GList *target = g_list_nth( m_list->children, n );
9ef6ff8c 909
caf6e6de 910 wxCHECK_MSG( target, false, wxT("invalid listbox index") );
9ef6ff8c 911
c72b150f 912 return (GTK_WIDGET(target->data)->state == GTK_STATE_SELECTED) ;
6de97a3b 913}
c801d85f 914
c6179a84 915void wxListBox::DoSetSelection( int n, bool select )
c801d85f 916{
223d09f6 917 wxCHECK_RET( m_list != NULL, wxT("invalid listbox") );
ff8bfdbb 918
caf6e6de 919 m_blockEvent = true;
953704c1 920
fd0eed64 921 if (select)
159b66c0
RR
922 {
923 if ((m_windowStyle & wxLB_SINGLE) != 0)
924 gtk_list_unselect_item( m_list, m_prevSelection );
fd0eed64 925 gtk_list_select_item( m_list, n );
159b66c0
RR
926 m_prevSelection = n;
927 }
fd0eed64
RR
928 else
929 gtk_list_unselect_item( m_list, n );
014b0d06 930
caf6e6de 931 m_blockEvent = false;
6de97a3b 932}
c801d85f 933
9ef6ff8c 934void wxListBox::DoSetFirstItem( int n )
c801d85f 935{
9ef6ff8c
VZ
936 wxCHECK_RET( m_list, wxT("invalid listbox") );
937
938 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list))
939 return;
f96b15a3
RD
940
941 // terribly efficient
9ef6ff8c
VZ
942 const gchar *vadjustment_key = "gtk-vadjustment";
943 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
f96b15a3
RD
944
945 GtkAdjustment *adjustment =
9ef6ff8c
VZ
946 (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id);
947 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
948
949 GList *target = g_list_nth( m_list->children, n );
950 wxCHECK_RET( target, wxT("invalid listbox index") );
f96b15a3 951
9ef6ff8c
VZ
952 GtkWidget *item = GTK_WIDGET(target->data);
953 wxCHECK_RET( item, wxT("invalid listbox code") );
954
4a82abc8 955 if (item->allocation.y == -1)
9ef6ff8c 956 {
4a82abc8
RR
957 wxlistbox_idle_struct* data = new wxlistbox_idle_struct;
958 data->m_listbox = this;
959 data->m_item = n;
960 data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data );
f96b15a3 961
4a82abc8 962 return;
9ef6ff8c
VZ
963 }
964
4a82abc8
RR
965 float y = item->allocation.y;
966 if (y > adjustment->upper - adjustment->page_size)
967 y = adjustment->upper - adjustment->page_size;
8161b5b9 968 gtk_adjustment_set_value( adjustment, y );
6de97a3b 969}
c801d85f 970
2ee3ee1b
VZ
971// ----------------------------------------------------------------------------
972// helpers
973// ----------------------------------------------------------------------------
c801d85f 974
11e1c70d 975int wxListBox::GtkGetIndex( GtkWidget *item ) const
c801d85f 976{
fd0eed64 977 if (item)
c801d85f 978 {
fd0eed64
RR
979 GList *child = m_list->children;
980 int count = 0;
981 while (child)
982 {
983 if (GTK_WIDGET(child->data) == item) return count;
984 count++;
985 child = child->next;
986 }
6de97a3b 987 }
fd0eed64 988 return -1;
6de97a3b 989}
c801d85f 990
ff8bfdbb 991#if wxUSE_TOOLTIPS
b5be07d4 992void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
b1170810 993{
b1170810
RR
994 GList *child = m_list->children;
995 while (child)
996 {
d3b9f782 997 gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), wxConvCurrent->cWX2MB(tip), NULL );
b1170810
RR
998 child = child->next;
999 }
1000}
ff8bfdbb 1001#endif // wxUSE_TOOLTIPS
b1170810 1002
fd0eed64 1003GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 1004{
fec195b2
RR
1005 // return GTK_WIDGET(m_list);
1006 return m_widget;
6de97a3b 1007}
e3e65dac 1008
f96aa4d9 1009bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
868a2826 1010{
89954433
VZ
1011 return m_widget->window == window ||
1012 GTK_WIDGET(m_list)->window == window;
868a2826 1013}
e3e65dac 1014
f40fdaa3 1015void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 1016{
f40fdaa3 1017 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b
RR
1018 {
1019 GdkWindow *window = GTK_WIDGET(m_list)->window;
f03fc89f
VZ
1020 if ( window )
1021 {
1022 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1023 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1024 gdk_window_clear( window );
1025 }
e380f72b 1026 }
ff8bfdbb 1027
fd0eed64
RR
1028 GList *child = m_list->children;
1029 while (child)
1030 {
f40fdaa3 1031 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
ff8bfdbb 1032
caaa4cfd
RR
1033 GtkBin *bin = GTK_BIN( child->data );
1034 GtkWidget *label = GTK_WIDGET( bin->child );
f40fdaa3 1035 gtk_widget_modify_style( label, style );
ff8bfdbb 1036
fd0eed64
RR
1037 child = child->next;
1038 }
68dda785 1039}
dcf924a3 1040
5e014a0c
RR
1041void wxListBox::OnInternalIdle()
1042{
1043 wxCursor cursor = m_cursor;
1044 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1045
f7a11f8c 1046 if (GTK_WIDGET(m_list)->window && cursor.Ok())
5e014a0c 1047 {
f7a11f8c 1048 /* I now set the cursor the anew in every OnInternalIdle call
2ee3ee1b
VZ
1049 as setting the cursor in a parent window also effects the
1050 windows above so that checking for the current cursor is
1051 not possible. */
9ef6ff8c 1052
2ee3ee1b 1053 gdk_window_set_cursor( GTK_WIDGET(m_list)->window, cursor.GetCursor() );
5e014a0c
RR
1054
1055 GList *child = m_list->children;
1056 while (child)
1057 {
1058 GtkBin *bin = GTK_BIN( child->data );
1059 GtkWidget *label = GTK_WIDGET( bin->child );
9ef6ff8c 1060
2ee3ee1b
VZ
1061 if (!label->window)
1062 break;
1063 else
1064 gdk_window_set_cursor( label->window, cursor.GetCursor() );
5e014a0c
RR
1065
1066 child = child->next;
1067 }
1068 }
1069
d7fa7eaa
RR
1070 if (g_delayedFocus == this)
1071 {
1072 if (GTK_WIDGET_REALIZED(m_widget))
1073 {
1074 gtk_widget_grab_focus( m_widget );
1075 g_delayedFocus = NULL;
1076 }
1077 }
1078
e39af974
JS
1079 if (wxUpdateUIEvent::CanUpdate(this))
1080 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
5e014a0c
RR
1081}
1082
f68586e5
VZ
1083wxSize wxListBox::DoGetBestSize() const
1084{
f96b15a3
RD
1085 int lbWidth = 100; // some defaults
1086 int lbHeight = 110;
1087 int wLine;
1088
1089 // Find the widest line
aa61d352 1090 for(unsigned int i = 0; i < GetCount(); i++) {
f96b15a3
RD
1091 wxString str(GetString(i));
1092 GetTextExtent(str, &wLine, NULL);
1093 lbWidth = wxMax(lbWidth, wLine);
1094 }
1095
1096 // Add room for the scrollbar
a756f210 1097 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3
RD
1098
1099 // And just a bit more
1100 int cx, cy;
2b5f62a0 1101 GetTextExtent( wxT("X"), &cx, &cy);
f96b15a3
RD
1102 lbWidth += 3 * cx;
1103
1104 // don't make the listbox too tall (limit height to around 10 items) but don't
1105 // make it too small neither
1106 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1107
9f884528 1108 wxSize best(lbWidth, lbHeight);
17a1ebd1 1109 CacheBestSize(best);
9f884528 1110 return best;
f68586e5
VZ
1111}
1112
3ae4c570
VZ
1113void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y)
1114{
1115 // the mouse event coords are relative to the listbox items, we need to
1116 // translate them to the normal client coords
1117 x += widget->allocation.x;
1118 y += widget->allocation.y;
1119}
1120
9d522606
RD
1121
1122// static
1123wxVisualAttributes
1124wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1125{
1126 return GetDefaultAttributesFromGTKWidget(gtk_list_new, true);
1127}
1128
3ae4c570 1129#endif // wxUSE_LISTBOX