]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
don't let def window proc start another drag operation if we just started one ourselv...
[wxWidgets.git] / src / gtk / listbox.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/listbox.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
4a46cbe8 5// Modified By: Ryan Norton (GtkTreeView implementation)
f96aa4d9
RR
6// Id: $Id$
7// Copyright: (c) 1998 Robert Roebling
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
dcf924a3
RR
14#if wxUSE_LISTBOX
15
88a7a4e1
WS
16#include "wx/listbox.h"
17
ad9835c9
WS
18#ifndef WX_PRECOMP
19 #include "wx/dynarray.h"
88a7a4e1 20 #include "wx/intl.h"
e4db172a 21 #include "wx/log.h"
de6185e2 22 #include "wx/utils.h"
9eddec69 23 #include "wx/settings.h"
e6e83933 24 #include "wx/checklst.h"
aaa6d89a 25 #include "wx/arrstr.h"
ad9835c9
WS
26#endif
27
fab591c5 28#include "wx/gtk/private.h"
4a46cbe8 29#include "wx/gtk/treeentry_gtk.h"
291a8f20
RR
30
31#if wxUSE_TOOLTIPS
ad9835c9 32 #include "wx/tooltip.h"
291a8f20 33#endif
c801d85f 34
4a46cbe8 35#include <gtk/gtk.h>
16c1f79c 36#include <gdk/gdkkeysyms.h>
83624f79 37
66bd6b93
RR
38//-----------------------------------------------------------------------------
39// data
40//-----------------------------------------------------------------------------
41
d7fa7eaa
RR
42extern bool g_blockEventsOnDrag;
43extern bool g_blockEventsOnScroll;
caaa4cfd 44
7a30ee8f 45
4a82abc8 46
c9433ea9 47//-----------------------------------------------------------------------------
4a46cbe8 48// Macro to tell which row the strings are in (1 if native checklist, 0 if not)
c9433ea9
RR
49//-----------------------------------------------------------------------------
50
470402b9 51#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
52# define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
53#else
54# define WXLISTBOX_DATACOLUMN_ARG(x) (0)
470402b9 55#endif // wxUSE_CHECKLISTBOX
17a1ebd1 56
4a46cbe8 57#define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
c9433ea9
RR
58
59//-----------------------------------------------------------------------------
4a46cbe8 60// "row-activated"
c9433ea9
RR
61//-----------------------------------------------------------------------------
62
865bb325 63extern "C" {
4a46cbe8
RR
64static void
65gtk_listbox_row_activated_callback(GtkTreeView *treeview,
66 GtkTreePath *path,
67 GtkTreeViewColumn *col,
68 wxListBox *listbox)
c9433ea9 69{
4a46cbe8
RR
70 if (g_blockEventsOnDrag) return;
71 if (g_blockEventsOnScroll) return;
c9433ea9 72
470402b9 73 // This is triggered by either a double-click or a space press
c9433ea9 74
4a46cbe8 75 int sel = gtk_tree_path_get_indices(path)[0];
c9433ea9 76
470402b9
RR
77 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
78 event.SetEventObject( listbox );
acfd422a 79
470402b9 80 if (listbox->IsSelected(sel))
4a46cbe8 81 {
470402b9 82 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
f4322df6 83
470402b9 84 if (entry)
4a46cbe8 85 {
470402b9
RR
86 event.SetInt(sel);
87 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
88
89 if ( listbox->HasClientObjectData() )
90 event.SetClientObject( (wxClientData*) gtk_tree_entry_get_userdata(entry) );
91 else if ( listbox->HasClientUntypedData() )
92 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
f4322df6 93
470402b9 94 g_object_unref (entry);
4a46cbe8 95 }
470402b9 96 else
c64371de 97 {
470402b9
RR
98 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
99 event.SetInt(-1);
4a46cbe8 100 }
6c8a980f 101 }
470402b9 102 else
7a30ee8f 103 {
470402b9 104 event.SetInt(-1);
4f22cf8d
RR
105 }
106
470402b9 107 listbox->GetEventHandler()->ProcessEvent( event );
caaa4cfd 108}
865bb325 109}
66bd6b93 110
c801d85f 111//-----------------------------------------------------------------------------
470402b9 112// "changed"
c801d85f
KB
113//-----------------------------------------------------------------------------
114
4a46cbe8 115extern "C" {
470402b9
RR
116static void
117gtk_listitem_changed_callback( GtkTreeSelection* selection, wxListBox *listbox )
c801d85f 118{
470402b9 119 if (g_blockEventsOnDrag) return;
f4322df6 120
470402b9 121 if (listbox->m_blockEvent) return;
f4322df6 122
470402b9
RR
123 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
124 event.SetEventObject( listbox );
159b66c0 125
470402b9 126 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
159b66c0 127 {
470402b9
RR
128 wxArrayInt selections;
129 listbox->GetSelections( selections );
f4322df6 130
470402b9
RR
131 if (selections.GetCount() == 0)
132 {
133 // indicate that this is a deselection
134 event.SetExtraLong( 0 );
135 event.SetInt( -1 );
f4322df6 136
470402b9 137 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 138
470402b9
RR
139 return;
140 }
141 else
142 {
143 // indicate that this is a selection
144 event.SetExtraLong( 1 );
145 event.SetInt( selections[0] );
f4322df6 146
470402b9
RR
147 listbox->GetEventHandler()->ProcessEvent( event );
148 }
149 }
150 else
151 {
152 int index = listbox->GetSelection();
153 if (index == wxNOT_FOUND)
154 {
155 // indicate that this is a deselection
156 event.SetExtraLong( 0 );
157 event.SetInt( -1 );
f4322df6 158
470402b9 159 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 160
470402b9
RR
161 return;
162 }
163 else
164 {
165 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
4a46cbe8 166
470402b9
RR
167 // indicate that this is a selection
168 event.SetExtraLong( 1 );
4a46cbe8 169
470402b9
RR
170 event.SetInt( index );
171 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
dcf40a56 172
470402b9
RR
173 if ( listbox->HasClientObjectData() )
174 event.SetClientObject(
caf6e6de 175 (wxClientData*) gtk_tree_entry_get_userdata(entry)
4a46cbe8 176 );
470402b9
RR
177 else if ( listbox->HasClientUntypedData() )
178 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
09cf7c58 179
470402b9 180 listbox->GetEventHandler()->ProcessEvent( event );
4a46cbe8 181
470402b9
RR
182 g_object_unref (entry);
183 }
4a46cbe8 184 }
4a46cbe8 185}
6de97a3b 186}
c801d85f 187
4a46cbe8
RR
188//-----------------------------------------------------------------------------
189// GtkTreeEntry destruction (to destroy client data)
190//-----------------------------------------------------------------------------
191
865bb325 192extern "C" {
caf6e6de 193static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
4a46cbe8 194 wxListBox* listbox)
865bb325 195{
470402b9 196 if (listbox->HasClientObjectData())
4a46cbe8
RR
197 {
198 gpointer userdata = gtk_tree_entry_get_userdata(entry);
470402b9 199 if (userdata)
4a46cbe8
RR
200 delete (wxClientData *)userdata;
201 }
865bb325
VZ
202}
203}
204
4a46cbe8
RR
205//-----------------------------------------------------------------------------
206// Sorting callback (standard CmpNoCase return value)
207//-----------------------------------------------------------------------------
208
865bb325 209extern "C" {
4a46cbe8
RR
210static gint gtk_listbox_sort_callback(GtkTreeModel *model,
211 GtkTreeIter *a,
212 GtkTreeIter *b,
213 wxListBox *listbox)
865bb325 214{
4a46cbe8
RR
215 GtkTreeEntry* entry;
216 GtkTreeEntry* entry2;
217
218 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
219 a,
caf6e6de 220 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
221 &entry, -1);
222 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
223 b,
caf6e6de 224 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
225 &entry2, -1);
226 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
227 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
228
229 //We compare collate keys here instead of calling g_utf8_collate
230 //as it is rather slow (and even the docs reccommend this)
231 int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry),
232 gtk_tree_entry_get_collate_key(entry2));
233
3fe39b0c
MR
234 g_object_unref (entry);
235 g_object_unref (entry2);
4a46cbe8
RR
236
237 return ret;
865bb325
VZ
238}
239}
240
a60c99e6 241//-----------------------------------------------------------------------------
4a46cbe8 242// Searching callback (TRUE == not equal, FALSE == equal)
c801d85f
KB
243//-----------------------------------------------------------------------------
244
865bb325 245extern "C" {
4a46cbe8
RR
246static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
247 gint column,
248 const gchar* key,
249 GtkTreeIter* iter,
250 wxListBox* listbox)
f2e93537 251{
4a46cbe8
RR
252 GtkTreeEntry* entry;
253
254 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
255 iter,
caf6e6de 256 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
257 &entry, -1);
258 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
e808cf8a 259 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
17a1ebd1 260
4a46cbe8
RR
261 int ret = strcasecmp(keycollatekey,
262 gtk_tree_entry_get_collate_key(entry));
17a1ebd1 263
3fe39b0c 264 g_object_unref (entry);
4a46cbe8
RR
265
266 return ret != 0;
f2e93537 267}
865bb325 268}
f2e93537
RR
269
270//-----------------------------------------------------------------------------
271// wxListBox
272//-----------------------------------------------------------------------------
273
4a46cbe8 274IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
c801d85f 275
2ee3ee1b
VZ
276// ----------------------------------------------------------------------------
277// construction
278// ----------------------------------------------------------------------------
279
b6e5dfef 280void wxListBox::Init()
c801d85f 281{
4a46cbe8 282 m_treeview = (GtkTreeView*) NULL;
88ac883a 283#if wxUSE_CHECKLISTBOX
caf6e6de 284 m_hasCheckBoxes = false;
88ac883a 285#endif // wxUSE_CHECKLISTBOX
6de97a3b 286}
c801d85f 287
584ad2a3
MB
288bool wxListBox::Create( wxWindow *parent, wxWindowID id,
289 const wxPoint &pos, const wxSize &size,
290 const wxArrayString& choices,
291 long style, const wxValidator& validator,
292 const wxString &name )
293{
294 wxCArrayString chs(choices);
295
296 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
297 style, validator, name );
298}
299
dcf40a56 300bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
301 const wxPoint &pos, const wxSize &size,
302 int n, const wxString choices[],
2ee3ee1b
VZ
303 long style, const wxValidator& validator,
304 const wxString &name )
c801d85f 305{
caf6e6de 306 m_needParent = true;
caf6e6de 307 m_blockEvent = false;
dcf40a56 308
4dcaf11a
RR
309 if (!PreCreation( parent, pos, size ) ||
310 !CreateBase( parent, id, pos, size, style, validator, name ))
311 {
223d09f6 312 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 313 return false;
4dcaf11a 314 }
6de97a3b 315
fd0eed64 316 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
317 if (style & wxLB_ALWAYS_SB)
318 {
319 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
320 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
321 }
322 else
323 {
324 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
325 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
326 }
ff8bfdbb 327
6493aaca
VZ
328
329 GtkScrolledWindowSetBorder(m_widget, style);
330
4a46cbe8
RR
331 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
332
333 //wxListBox doesn't have a header :)
334 //NB: If enabled SetFirstItem doesn't work correctly
335 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
336
470402b9 337#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
338 if(m_hasCheckBoxes)
339 ((wxCheckListBox*)this)->DoCreateCheckList();
470402b9 340#endif // wxUSE_CHECKLISTBOX
4a46cbe8
RR
341
342 // Create the data column
caf6e6de 343 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 344 gtk_cell_renderer_text_new(),
caf6e6de 345 "text",
4a46cbe8
RR
346 WXLISTBOX_DATACOLUMN, NULL);
347
348 // Now create+set the model (GtkListStore) - first argument # of columns
470402b9 349#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
350 if(m_hasCheckBoxes)
351 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
352 GTK_TYPE_TREE_ENTRY);
353 else
354#endif
355 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
356
357 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 358
3fe39b0c 359 g_object_unref (m_liststore); //free on treeview destruction
caf6e6de 360
4a46cbe8
RR
361 // Disable the pop-up textctrl that enables searching - note that
362 // the docs specify that even if this disabled (which we are doing)
363 // the user can still have it through the start-interactive-search
364 // key binding...either way we want to provide a searchequal callback
caf6e6de 365 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
366 // on a successful search
367 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 368 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
369 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
370 this,
371 NULL);
372
373 gtk_tree_view_set_enable_search(m_treeview, FALSE);
374
375
376 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
f4322df6 377
470402b9
RR
378 g_signal_connect_after (selection, "changed",
379 G_CALLBACK (gtk_listitem_changed_callback), this);
dcf40a56 380
4a82abc8 381 GtkSelectionMode mode;
fd0eed64 382 if (style & wxLB_MULTIPLE)
4a82abc8 383 {
fd0eed64 384 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 385 }
fd0eed64 386 else if (style & wxLB_EXTENDED)
4a82abc8 387 {
fd0eed64 388 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
389 }
390 else
391 {
392 // if style was 0 set single mode
393 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 394 mode = GTK_SELECTION_SINGLE;
4a82abc8 395 }
6a6d4eed 396
4a46cbe8
RR
397 gtk_tree_selection_set_mode( selection, mode );
398
470402b9 399 // Handle sortable stuff
4a46cbe8
RR
400 if(style & wxLB_SORT)
401 {
470402b9 402 // Setup sorting in ascending (wx) order
caf6e6de
WS
403 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
404 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
405 GTK_SORT_ASCENDING);
406
470402b9 407 // Set the sort callback
4a46cbe8
RR
408 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
409 WXLISTBOX_DATACOLUMN,
410 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
411 this, //userdata
412 NULL //"destroy notifier"
413 );
414 }
415
dcf40a56 416
dcd3c79c 417 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 418
4a46cbe8 419 gtk_widget_show( GTK_WIDGET(m_treeview) );
0bb3fc29 420 m_focusWidget = GTK_WIDGET(m_treeview);
dcf40a56 421
4a46cbe8 422 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
17a1ebd1 423
470402b9
RR
424 // generate dclick events
425 g_signal_connect_after(m_treeview, "row-activated",
4a46cbe8 426 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 427
f03fc89f 428 m_parent->DoAddChild( this );
ff8bfdbb 429
abdeb9e7 430 PostCreation(size);
170acdc9 431 SetInitialSize(size); // need this too because this is a wxControlWithItems
dcf40a56 432
caf6e6de 433 return true;
6de97a3b 434}
c801d85f 435
fd0eed64 436wxListBox::~wxListBox()
c801d85f 437{
caf6e6de 438 m_hasVMT = false;
4a82abc8 439
caaa4cfd 440 Clear();
6de97a3b 441}
c801d85f 442
8161b5b9
VZ
443// ----------------------------------------------------------------------------
444// adding items
445// ----------------------------------------------------------------------------
446
caf6e6de 447void wxListBox::GtkInsertItems(const wxArrayString& items,
aa61d352 448 void** clientData, unsigned int pos)
bb0ca8df 449{
4a46cbe8 450 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 451
9f884528
RD
452 InvalidateBestSize();
453
4a46cbe8 454 // Create and set column ids and GValues
9ef6ff8c 455
aa61d352
VZ
456 unsigned int nNum = items.GetCount();
457 unsigned int nCurCount = wxListBox::GetCount();
4a46cbe8 458 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
bb0ca8df 459
2eb1512a
MR
460 GtkTreeIter* pIter = NULL; // append by default
461 GtkTreeIter iter;
462 if (pos != nCurCount)
bb0ca8df 463 {
0d5f4ba3
VZ
464 wxCHECK_RET( GtkGetIteratorFor(pos, &iter),
465 wxT("internal wxListBox error in insertion") );
bb0ca8df 466
4a46cbe8 467 pIter = &iter;
bb0ca8df 468 }
2ee3ee1b 469
aa61d352 470 for (unsigned int i = 0; i < nNum; ++i)
2ee3ee1b 471 {
4a46cbe8 472 wxString label = items[i];
ff8bfdbb 473
4a46cbe8 474 GtkTreeEntry* entry = gtk_tree_entry_new();
d723a1ba 475 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
caf6e6de 476 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 477 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 478 this);
dcf40a56 479
4a46cbe8
RR
480 if (clientData)
481 gtk_tree_entry_set_userdata(entry, clientData[i]);
9fa72bd2 482
4a46cbe8
RR
483 GtkTreeIter itercur;
484 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 485
0d5f4ba3 486 GtkSetItem(itercur, entry);
fd0eed64 487
0d5f4ba3 488 g_object_unref (entry);
4a46cbe8
RR
489 }
490}
17a1ebd1 491
aa61d352 492void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
4a46cbe8 493{
8228b893
WS
494 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
495
aa61d352 496 GtkInsertItems(items, NULL, pos);
4a46cbe8 497}
014b0d06 498
4a46cbe8
RR
499int wxListBox::DoAppend( const wxString& item )
500{
be4bb86e
RR
501 wxCHECK_MSG( m_treeview != NULL, -1, wxT("invalid listbox") );
502
503 InvalidateBestSize();
504
505 GtkTreeEntry* entry = gtk_tree_entry_new();
506 gtk_tree_entry_set_label( entry, wxGTK_CONV(item) );
507 gtk_tree_entry_set_destroy_func(entry,
508 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
509 this);
510
511 GtkTreeIter itercur;
512 gtk_list_store_insert_before( m_liststore, &itercur, NULL );
513
0d5f4ba3 514 GtkSetItem(itercur, entry);
be4bb86e 515
0d5f4ba3 516 g_object_unref (entry);
be4bb86e 517
0d5f4ba3 518 return GtkGetIndexFor(itercur);
fd0eed64
RR
519}
520
2ee3ee1b
VZ
521void wxListBox::DoSetItems( const wxArrayString& items,
522 void **clientData)
fd0eed64 523{
2ee3ee1b 524 Clear();
4a46cbe8 525 GtkInsertItems(items, clientData, 0);
fd0eed64 526}
dcf40a56 527
2ee3ee1b 528// ----------------------------------------------------------------------------
8161b5b9 529// deleting items
2ee3ee1b 530// ----------------------------------------------------------------------------
ff8bfdbb 531
fd0eed64
RR
532void wxListBox::Clear()
533{
4a46cbe8 534 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 535
cdff1318
RD
536 InvalidateBestSize();
537
4a46cbe8 538 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
6de97a3b 539}
c801d85f 540
aa61d352 541void wxListBox::Delete(unsigned int n)
c801d85f 542{
4a46cbe8 543 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 544
cdff1318
RD
545 InvalidateBestSize();
546
4a46cbe8 547 GtkTreeIter iter;
0d5f4ba3 548 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
dcf40a56 549
0d5f4ba3
VZ
550 // this returns false if iter is invalid (e.g. deleting item at end) but
551 // since we don't use iter, we ignore the return value
4a46cbe8
RR
552 gtk_list_store_remove(m_liststore, &iter);
553}
dcf40a56 554
4a46cbe8 555// ----------------------------------------------------------------------------
0d5f4ba3 556// helper functions for working with iterators
4a46cbe8 557// ----------------------------------------------------------------------------
2ee3ee1b 558
0d5f4ba3 559bool wxListBox::GtkGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
4a46cbe8 560{
0d5f4ba3
VZ
561 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
562 iter, NULL, pos) )
4a46cbe8 563 {
0d5f4ba3
VZ
564 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
565 return false;
f5e27805 566 }
ff8bfdbb 567
0d5f4ba3
VZ
568 return true;
569}
570
571int wxListBox::GtkGetIndexFor(GtkTreeIter& iter) const
572{
573 GtkTreePath *path =
574 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
575
576 gint* pIntPath = gtk_tree_path_get_indices(path);
577
578 wxCHECK_MSG( pIntPath, wxNOT_FOUND, _T("failed to get iterator path") );
579
580 int idx = pIntPath[0];
581
582 gtk_tree_path_free( path );
583
584 return idx;
585}
586
587// get GtkTreeEntry from position (note: you need to g_unref it if valid)
588GtkTreeEntry *wxListBox::GtkGetEntry(unsigned n) const
589{
590 GtkTreeIter iter;
591 if ( !GtkGetIteratorFor(n, &iter) )
592 return NULL;
593
4a46cbe8
RR
594
595 GtkTreeEntry* entry = NULL;
596 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
597 WXLISTBOX_DATACOLUMN, &entry, -1);
598
599 return entry;
2ee3ee1b
VZ
600}
601
0d5f4ba3
VZ
602void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
603{
604#if wxUSE_CHECKLISTBOX
605 if ( m_hasCheckBoxes )
606 {
607 gtk_list_store_set(m_liststore, &iter,
608 0, FALSE, // FALSE == not toggled
609 1, entry,
610 -1);
611 }
612 else
613#endif // wxUSE_CHECKLISTBOX
614 {
615 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
616 }
617}
618
8161b5b9
VZ
619// ----------------------------------------------------------------------------
620// client data
621// ----------------------------------------------------------------------------
622
aa61d352 623void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 624{
aa61d352 625 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 626 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 627
4a46cbe8
RR
628 GtkTreeEntry* entry = GtkGetEntry(n);
629 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 630
4a46cbe8 631 void* userdata = gtk_tree_entry_get_userdata( entry );
3fe39b0c 632 g_object_unref (entry);
4a46cbe8 633 return userdata;
8161b5b9
VZ
634}
635
aa61d352 636wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
8161b5b9 637{
4a46cbe8 638 return (wxClientData*) wxListBox::DoGetItemClientData(n);
8161b5b9
VZ
639}
640
aa61d352 641void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 642{
aa61d352 643 wxCHECK_RET( IsValid(n),
4a46cbe8 644 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 645
4a46cbe8
RR
646 GtkTreeEntry* entry = GtkGetEntry(n);
647 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 648
4a46cbe8 649 gtk_tree_entry_set_userdata( entry, clientData );
3fe39b0c 650 g_object_unref (entry);
8161b5b9
VZ
651}
652
aa61d352 653void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
8161b5b9 654{
4a46cbe8
RR
655 // wxItemContainer already deletes data for us
656 wxListBox::DoSetItemClientData(n, (void*) clientData);
8161b5b9
VZ
657}
658
2ee3ee1b
VZ
659// ----------------------------------------------------------------------------
660// string list access
661// ----------------------------------------------------------------------------
662
33c6e437 663void wxListBox::SetString(unsigned int n, const wxString& label)
2ee3ee1b 664{
8228b893 665 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 666 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 667
4a46cbe8
RR
668 GtkTreeEntry* entry = GtkGetEntry(n);
669 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 670
33c6e437
VZ
671 // update the item itself
672 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
4a46cbe8 673
33c6e437
VZ
674 // and update the model which will refresh the tree too
675 GtkTreeIter iter;
0d5f4ba3 676 wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
4a46cbe8 677
0d5f4ba3
VZ
678 // FIXME: this resets the checked status of a wxCheckListBox item
679
680 GtkSetItem(iter, entry);
6de97a3b 681}
c801d85f 682
aa61d352 683wxString wxListBox::GetString(unsigned int n) const
c801d85f 684{
4a46cbe8 685 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 686
4a46cbe8
RR
687 GtkTreeEntry* entry = GtkGetEntry(n);
688 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
689
690 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 691
3fe39b0c 692 g_object_unref (entry);
4a46cbe8 693 return label;
2ee3ee1b
VZ
694}
695
aa61d352 696unsigned int wxListBox::GetCount() const
2ee3ee1b 697{
8228b893 698 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 699
aa61d352 700 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 701}
c801d85f 702
11e62fe6 703int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 704{
4a46cbe8 705 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 706
4a46cbe8 707 //Sort of hackish - maybe there is a faster way
aa61d352 708 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 709
aa61d352 710 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
711 {
712 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 713 return (int)i;
fd0eed64
RR
714 }
715
dcf40a56 716
4a46cbe8 717 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 718 return wxNOT_FOUND;
6de97a3b 719}
c801d85f 720
2ee3ee1b
VZ
721// ----------------------------------------------------------------------------
722// selection
723// ----------------------------------------------------------------------------
724
fd0eed64 725int wxListBox::GetSelection() const
c801d85f 726{
e6e83933
WS
727 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
728 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 729 wxT("must be single selection listbox"));
ff8bfdbb 730
caf6e6de 731 GtkTreeIter iter;
4a46cbe8
RR
732 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
733
caf6e6de 734 // only works on single-sel
4a46cbe8 735 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 736 return wxNOT_FOUND;
4a46cbe8 737
0d5f4ba3 738 return GtkGetIndexFor(iter);
6de97a3b 739}
c801d85f 740
fd0eed64 741int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 742{
e6e83933 743 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 744
fd0eed64
RR
745 aSelections.Empty();
746
fd0eed64 747 int i = 0;
caf6e6de 748 GtkTreeIter iter;
4a46cbe8
RR
749 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
750
751 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
752 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
753 do
fd0eed64 754 {
4a46cbe8
RR
755 if (gtk_tree_selection_iter_is_selected(selection, &iter))
756 aSelections.Add(i);
757
758 i++;
759 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 760 }
dcf40a56 761
4a46cbe8 762 return aSelections.GetCount();
6de97a3b 763}
c801d85f 764
2ee3ee1b 765bool wxListBox::IsSelected( int n ) const
c801d85f 766{
caf6e6de 767 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 768
4a46cbe8 769 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 770
4a46cbe8 771 GtkTreeIter iter;
0d5f4ba3 772 wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
9ef6ff8c 773
4a46cbe8 774 return gtk_tree_selection_iter_is_selected(selection, &iter);
6de97a3b 775}
c801d85f 776
c6179a84 777void wxListBox::DoSetSelection( int n, bool select )
c801d85f 778{
ef33382e
VZ
779 // passing -1 to SetSelection() is documented to deselect all items
780 if ( n == wxNOT_FOUND )
781 {
782 // ... and not generate any events in the process
783 GtkDeselectAll();
470402b9 784 return;
ef33382e
VZ
785 }
786
787 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
788
789 // don't generate the selection event
790 GtkSetSelection(n, select, true);
791}
792
793void wxListBox::GtkDeselectAll()
794{
795 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
796
797 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
798
799 m_blockEvent = true;
800
801 gtk_tree_selection_unselect_all(selection);
802
803 m_blockEvent = false;
4a46cbe8
RR
804}
805
806void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
807{
808 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
809
810 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
811
812 GtkTreeIter iter;
0d5f4ba3 813 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
ff8bfdbb 814
4a46cbe8 815 m_blockEvent = blockEvent;
953704c1 816
fd0eed64 817 if (select)
4a46cbe8 818 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 819 else
4a46cbe8 820 gtk_tree_selection_unselect_iter(selection, &iter);
014b0d06 821
caf6e6de 822 m_blockEvent = false;
6de97a3b 823}
c801d85f 824
9ef6ff8c 825void wxListBox::DoSetFirstItem( int n )
c801d85f 826{
4a46cbe8 827 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 828 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 829
4a46cbe8
RR
830 //RN: I have no idea why this line is needed...
831 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
9ef6ff8c 832 return;
f96b15a3 833
28354d90 834 GtkTreeIter iter;
0d5f4ba3
VZ
835 if ( !GtkGetIteratorFor(n, &iter) )
836 return;
4a46cbe8 837
28354d90
VZ
838 GtkTreePath* path = gtk_tree_model_get_path(
839 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 840
28354d90
VZ
841 // Scroll to the desired cell (0.0 == topleft alignment)
842 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
843 TRUE, 0.0f, 0.0f);
4a46cbe8 844
28354d90 845 gtk_tree_path_free(path);
6de97a3b 846}
c801d85f 847
c00fed0e
VZ
848// ----------------------------------------------------------------------------
849// hittest
850// ----------------------------------------------------------------------------
851
57772185 852int wxListBox::DoListHitTest(const wxPoint& point) const
c00fed0e 853{
a183ddba
VZ
854 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
855 // we only want visible items we need to check for it manually here
22a35096 856 if ( !GetClientRect().Contains(point) )
a183ddba
VZ
857 return wxNOT_FOUND;
858
57772185 859 // need to translate from master window since it is in client coords
c00fed0e 860 gint binx, biny;
caf6e6de 861 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
c00fed0e
VZ
862 &binx, &biny, NULL, NULL, NULL);
863
864 GtkTreePath* path;
57772185
VZ
865 if ( !gtk_tree_view_get_path_at_pos
866 (
caf6e6de 867 m_treeview,
57772185
VZ
868 point.x - binx,
869 point.y - biny,
870 &path,
871 NULL, // [out] column (always 0 here)
872 NULL, // [out] x-coord relative to the cell (not interested)
873 NULL // [out] y-coord relative to the cell
874 ) )
c00fed0e
VZ
875 {
876 return wxNOT_FOUND;
877 }
878
879 int index = gtk_tree_path_get_indices(path)[0];
880 gtk_tree_path_free(path);
881
882 return index;
883}
4a46cbe8 884
2ee3ee1b
VZ
885// ----------------------------------------------------------------------------
886// helpers
887// ----------------------------------------------------------------------------
c801d85f 888
ff8bfdbb 889#if wxUSE_TOOLTIPS
b5be07d4 890void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
b1170810 891{
4a46cbe8
RR
892 // RN: Is this needed anymore?
893 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
b1170810 894}
ff8bfdbb 895#endif // wxUSE_TOOLTIPS
b1170810 896
fd0eed64 897GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 898{
e5e41cfe
VZ
899 // the correct widget for listbox events (such as mouse clicks for example)
900 // is m_treeview, not the parent scrolled window
901 return GTK_WIDGET(m_treeview);
6de97a3b 902}
e3e65dac 903
1c2b98d6 904GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 905{
1c2b98d6 906 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 907}
e3e65dac 908
f40fdaa3 909void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 910{
f40fdaa3 911 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b 912 {
04e044c4 913 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 914 if (window)
f03fc89f 915 {
08f60019 916 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
917 gdk_window_set_background( window, m_backgroundColour.GetColor() );
918 gdk_window_clear( window );
919 }
e380f72b 920 }
ff8bfdbb 921
4a46cbe8 922 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
68dda785 923}
dcf924a3 924
f68586e5
VZ
925wxSize wxListBox::DoGetBestSize() const
926{
4a46cbe8
RR
927 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
928
cdff1318 929 // Start with a minimum size that's not too small
f96b15a3 930 int cx, cy;
2b5f62a0 931 GetTextExtent( wxT("X"), &cx, &cy);
c64371de
RD
932 int lbWidth = 3 * cx;
933 int lbHeight = 10;
cdff1318 934
caf6e6de 935 // Get the visible area of the tree view (limit to the 10th item
cdff1318 936 // so that it isn't too big)
aa61d352 937 unsigned int count = GetCount();
cdff1318
RD
938 if (count)
939 {
940 int wLine;
941
942 // Find the widest line
aa61d352 943 for(unsigned int i = 0; i < count; i++) {
cdff1318
RD
944 wxString str(GetString(i));
945 GetTextExtent(str, &wLine, NULL);
946 lbWidth = wxMax(lbWidth, wLine);
947 }
948
949 lbWidth += 3 * cx;
950
951 // And just a bit more for the checkbox if present and then some
952 // (these are rough guesses)
470402b9 953#if wxUSE_CHECKLISTBOX
cdff1318
RD
954 if ( m_hasCheckBoxes )
955 {
956 lbWidth += 35;
957 cy = cy > 25 ? cy : 25; // rough height of checkbox
958 }
959#endif
f96b15a3 960
aa61d352
VZ
961 // don't make the listbox too tall (limit height to around 10 items) but don't
962 // make it too small neither
cdff1318
RD
963 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
964 }
caf6e6de 965
cdff1318
RD
966 // Add room for the scrollbar
967 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 968
9f884528 969 wxSize best(lbWidth, lbHeight);
17a1ebd1 970 CacheBestSize(best);
9f884528 971 return best;
f68586e5
VZ
972}
973
9d522606
RD
974// static
975wxVisualAttributes
976wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
977{
4a46cbe8 978 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
979}
980
3ae4c570 981#endif // wxUSE_LISTBOX