summaryrefslogtreecommitdiff
path: root/indra/llui/llflatlistview.cpp
blob: a3debdb19ad2150723c5195c080fa6aeceefdbab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
/** 
 * @file llflatlistview.cpp
 * @brief LLFlatListView base class and extension to support messages for several cases of an empty list.
 *
 * $LicenseInfo:firstyear=2009&license=viewergpl$
 * 
 * Copyright (c) 2009-2010, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llpanel.h"
#include "lltextbox.h"

#include "llflatlistview.h"

static const LLDefaultChildRegistry::Register<LLFlatListView> flat_list_view("flat_list_view");

const LLSD SELECTED_EVENT	= LLSD().with("selected", true);
const LLSD UNSELECTED_EVENT	= LLSD().with("selected", false);

//forward declaration
bool llsds_are_equal(const LLSD& llsd_1, const LLSD& llsd_2);

LLFlatListView::Params::Params()
:	item_pad("item_pad"),
	allow_select("allow_select"),
	multi_select("multi_select"),
	keep_one_selected("keep_one_selected"),
	keep_selection_visible_on_reshape("keep_selection_visible_on_reshape",false),
	no_items_text("no_items_text")
{};

void LLFlatListView::reshape(S32 width, S32 height, BOOL called_from_parent /* = TRUE */)
{
	S32 delta = height - getRect().getHeight();
	LLScrollContainer::reshape(width, height, called_from_parent);
	setItemsNoScrollWidth(width);
	rearrangeItems();
	
	if(delta!= 0 && mKeepSelectionVisibleOnReshape)
	{
		ensureSelectedVisible();
	}
}

const LLRect& LLFlatListView::getItemsRect() const
{
	return mItemsPanel->getRect(); 
}

bool LLFlatListView::addItem(LLPanel * item, const LLSD& value /*= LLUUID::null*/, EAddPosition pos /*= ADD_BOTTOM*/,bool rearrange /*= true*/)
{
	if (!item) return false;
	if (value.isUndefined()) return false;
	
	//force uniqueness of items, easiest check but unreliable
	if (item->getParent() == mItemsPanel) return false;
	
	item_pair_t* new_pair = new item_pair_t(item, value);
	switch (pos)
	{
	case ADD_TOP:
		mItemPairs.push_front(new_pair);
		//in LLView::draw() children are iterated in backorder
		mItemsPanel->addChildInBack(item);
		break;
	case ADD_BOTTOM:
		mItemPairs.push_back(new_pair);
		mItemsPanel->addChild(item);
		break;
	default:
		break;
	}
	
	//_4 is for MASK
	item->setMouseDownCallback(boost::bind(&LLFlatListView::onItemMouseClick, this, new_pair, _4));
	item->setRightMouseDownCallback(boost::bind(&LLFlatListView::onItemRightMouseClick, this, new_pair, _4));

	// Children don't accept the focus
	item->setTabStop(false);

	if (rearrange)
	{
		rearrangeItems();
		notifyParentItemsRectChanged();
	}
	return true;
}


bool LLFlatListView::insertItemAfter(LLPanel* after_item, LLPanel* item_to_add, const LLSD& value /*= LLUUID::null*/)
{
	if (!after_item) return false;
	if (!item_to_add) return false;
	if (value.isUndefined()) return false;

	if (mItemPairs.empty()) return false;

	//force uniqueness of items, easiest check but unreliable
	if (item_to_add->getParent() == mItemsPanel) return false;

	item_pair_t* after_pair = getItemPair(after_item);
	if (!after_pair) return false;

	item_pair_t* new_pair = new item_pair_t(item_to_add, value);
	if (after_pair == mItemPairs.back())
	{
		mItemPairs.push_back(new_pair);
		mItemsPanel->addChild(item_to_add);
	}
	else
	{
		pairs_iterator_t it = mItemPairs.begin();
		for (; it != mItemPairs.end(); ++it)
		{
			if (*it == after_pair)
			{
				// insert new elements before the element at position of passed iterator.
				mItemPairs.insert(++it, new_pair);
				mItemsPanel->addChild(item_to_add);
				break;
			}
		}
	}

	//_4 is for MASK
	item_to_add->setMouseDownCallback(boost::bind(&LLFlatListView::onItemMouseClick, this, new_pair, _4));
	item_to_add->setRightMouseDownCallback(boost::bind(&LLFlatListView::onItemRightMouseClick, this, new_pair, _4));

	rearrangeItems();
	notifyParentItemsRectChanged();
	return true;
}


bool LLFlatListView::removeItem(LLPanel* item)
{
	if (!item) return false;
	if (item->getParent() != mItemsPanel) return false;
	
	item_pair_t* item_pair = getItemPair(item);
	if (!item_pair) return false;

	return removeItemPair(item_pair);
}

bool LLFlatListView::removeItemByValue(const LLSD& value)
{
	if (value.isUndefined()) return false;
	
	item_pair_t* item_pair = getItemPair(value);
	if (!item_pair) return false;

	return removeItemPair(item_pair);
}

bool LLFlatListView::removeItemByUUID(const LLUUID& uuid)
{
	return removeItemByValue(LLSD(uuid));
}

LLPanel* LLFlatListView::getItemByValue(const LLSD& value) const
{
	if (value.isUndefined()) return NULL;

	item_pair_t* pair = getItemPair(value);
	if (pair) return pair->first;
	return NULL;
}

bool LLFlatListView::selectItem(LLPanel* item, bool select /*= true*/)
{
	if (!item) return false;
	if (item->getParent() != mItemsPanel) return false;
	
	item_pair_t* item_pair = getItemPair(item);
	if (!item_pair) return false;

	return selectItemPair(item_pair, select);
}

bool LLFlatListView::selectItemByValue(const LLSD& value, bool select /*= true*/)
{
	if (value.isUndefined()) return false;

	item_pair_t* item_pair = getItemPair(value);
	if (!item_pair) return false;

	return selectItemPair(item_pair, select);
}

bool LLFlatListView::selectItemByUUID(const LLUUID& uuid, bool select /* = true*/)
{
	return selectItemByValue(LLSD(uuid), select);
}


LLSD LLFlatListView::getSelectedValue() const
{
	if (mSelectedItemPairs.empty()) return LLSD();

	item_pair_t* first_selected_pair = mSelectedItemPairs.front();
	return first_selected_pair->second;
}

void LLFlatListView::getSelectedValues(std::vector<LLSD>& selected_values) const
{
	if (mSelectedItemPairs.empty()) return;

	for (pairs_const_iterator_t it = mSelectedItemPairs.begin(); it != mSelectedItemPairs.end(); ++it)
	{
		selected_values.push_back((*it)->second);
	}
}

LLUUID LLFlatListView::getSelectedUUID() const
{
	const LLSD& value = getSelectedValue();
	if (value.isDefined() && value.isUUID())
	{
		return value.asUUID();
	}
	else 
	{
		return LLUUID::null;
	}
}

void LLFlatListView::getSelectedUUIDs(uuid_vec_t& selected_uuids) const
{
	if (mSelectedItemPairs.empty()) return;

	for (pairs_const_iterator_t it = mSelectedItemPairs.begin(); it != mSelectedItemPairs.end(); ++it)
	{
		selected_uuids.push_back((*it)->second.asUUID());
	}
}

LLPanel* LLFlatListView::getSelectedItem() const
{
	if (mSelectedItemPairs.empty()) return NULL;

	return mSelectedItemPairs.front()->first;
}

void LLFlatListView::getSelectedItems(std::vector<LLPanel*>& selected_items) const
{
	if (mSelectedItemPairs.empty()) return;

	for (pairs_const_iterator_t it = mSelectedItemPairs.begin(); it != mSelectedItemPairs.end(); ++it)
	{
		selected_items.push_back((*it)->first);
	}
}

void LLFlatListView::resetSelection(bool no_commit_on_deselection /*= false*/)
{
	if (mSelectedItemPairs.empty()) return;

	for (pairs_iterator_t it= mSelectedItemPairs.begin(); it != mSelectedItemPairs.end(); ++it)
	{
		item_pair_t* pair_to_deselect = *it;
		LLPanel* item = pair_to_deselect->first;
		item->setValue(UNSELECTED_EVENT);
	}

	mSelectedItemPairs.clear();

	if (mCommitOnSelectionChange && !no_commit_on_deselection)
	{
		onCommit();
	}

	// Stretch selected item rect to ensure it won't be clipped
	mSelectedItemsBorder->setRect(getLastSelectedItemRect().stretch(-1));
}

void LLFlatListView::setNoItemsCommentText(const std::string& comment_text)
{
	mNoItemsCommentTextbox->setValue(comment_text);
}

U32 LLFlatListView::size(const bool only_visible_items) const
{
	if (only_visible_items)
	{
		U32 size = 0;
		for (pairs_const_iterator_t
				 iter = mItemPairs.begin(),
				 iter_end = mItemPairs.end();
			 iter != iter_end; ++iter)
		{
			if ((*iter)->first->getVisible())
				++size;
		}
		return size;
	}
	else
	{
		return mItemPairs.size();
	}
}

void LLFlatListView::clear()
{
	// do not use LLView::deleteAllChildren to avoid removing nonvisible items. drag-n-drop for ex.
	for (pairs_iterator_t it = mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		mItemsPanel->removeChild((*it)->first);
		(*it)->first->die();
		delete *it;
	}
	mItemPairs.clear();
	mSelectedItemPairs.clear();

	// also set items panel height to zero. Reshape it to allow reshaping of non-item children
	LLRect rc = mItemsPanel->getRect();
	rc.mBottom = rc.mTop;
	mItemsPanel->reshape(rc.getWidth(), rc.getHeight());
	mItemsPanel->setRect(rc);

	setNoItemsCommentVisible(true);
	notifyParentItemsRectChanged();
}

void LLFlatListView::sort()
{
	if (!mItemComparator)
	{
		llwarns << "No comparator specified for sorting FlatListView items." << llendl;
		return;
	}

	mItemPairs.sort(ComparatorAdaptor(*mItemComparator));
	rearrangeItems();
}

bool LLFlatListView::updateValue(const LLSD& old_value, const LLSD& new_value)
{
	if (old_value.isUndefined() || new_value.isUndefined()) return false;
	if (llsds_are_equal(old_value, new_value)) return false;

	item_pair_t* item_pair = getItemPair(old_value);
	if (!item_pair) return false;

	item_pair->second = new_value;
	return true;
}

//////////////////////////////////////////////////////////////////////////
// PROTECTED STUFF
//////////////////////////////////////////////////////////////////////////

LLFlatListView::LLFlatListView(const LLFlatListView::Params& p)
:	LLScrollContainer(p)
  , mItemComparator(NULL)
  , mItemsPanel(NULL)
  , mItemPad(p.item_pad)
  , mAllowSelection(p.allow_select)
  , mMultipleSelection(p.multi_select)
  , mKeepOneItemSelected(p.keep_one_selected)
  , mCommitOnSelectionChange(false)
  , mPrevNotifyParentRect(LLRect())
  , mNoItemsCommentTextbox(NULL)
  , mIsConsecutiveSelection(false)
  , mKeepSelectionVisibleOnReshape(p.keep_selection_visible_on_reshape)
{
	mBorderThickness = getBorderWidth();

	LLRect scroll_rect = getRect();
	LLRect items_rect;

	setItemsNoScrollWidth(scroll_rect.getWidth());
	items_rect.setLeftTopAndSize(mBorderThickness, scroll_rect.getHeight() - mBorderThickness, mItemsNoScrollWidth, 0);

	LLPanel::Params pp;
	pp.rect(items_rect);
	mItemsPanel = LLUICtrlFactory::create<LLPanel> (pp);
	addChild(mItemsPanel);

	//we don't need to stretch in vertical direction on reshaping by a parent
	//no bottom following!
	mItemsPanel->setFollows(FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_TOP);

	LLViewBorder::Params params;
	params.name("scroll border");
	params.rect(getLastSelectedItemRect());
	params.visible(false);
	params.bevel_style(LLViewBorder::BEVEL_IN);
	mSelectedItemsBorder = LLUICtrlFactory::create<LLViewBorder> (params);
	mItemsPanel->addChild( mSelectedItemsBorder );

	{
		// create textbox for "No Items" comment text
		LLTextBox::Params text_p = p.no_items_text;
		if (!text_p.rect.isProvided())
		{
			LLRect comment_rect = getRect();
			comment_rect.setOriginAndSize(0, 0, comment_rect.getWidth(), comment_rect.getHeight());
			comment_rect.stretch(-getBorderWidth());
			text_p.rect(comment_rect);
		}
		text_p.border_visible(false);

		if (!text_p.follows.isProvided())
		{
			text_p.follows.flags(FOLLOWS_ALL);
		}
		mNoItemsCommentTextbox = LLUICtrlFactory::create<LLTextBox>(text_p, this);
	}
};

// virtual
void LLFlatListView::draw()
{
	// Highlight border if a child of this container has keyboard focus
	if( mSelectedItemsBorder->getVisible() )
	{
		mSelectedItemsBorder->setKeyboardFocusHighlight( hasFocus() );
	}
	LLScrollContainer::draw();
}

// virtual
BOOL LLFlatListView::postBuild()
{
	setTabStop(true);
	return LLScrollContainer::postBuild();
}

void LLFlatListView::rearrangeItems()
{
	static LLUICachedControl<S32> scrollbar_size ("UIScrollbarSize", 0);

	setNoItemsCommentVisible(0==size());

	if (mItemPairs.empty()) return;

	//calculating required height - assuming items can be of different height
	//list should accommodate all its items
	S32 height = 0;

	S32 invisible_children_count = 0;
	pairs_iterator_t it = mItemPairs.begin();
	for (; it != mItemPairs.end(); ++it)
	{
		LLPanel* item = (*it)->first;

		// skip invisible child
		if (!item->getVisible())
		{
			++invisible_children_count;
			continue;
		}

		height += item->getRect().getHeight();
	}

	// add paddings between items, excluding invisible ones
	height += mItemPad * (mItemPairs.size() - invisible_children_count - 1);

	LLRect rc = mItemsPanel->getRect();
	S32 width = mItemsNoScrollWidth;

	// update width to avoid horizontal scrollbar
	if (height > getRect().getHeight() - 2 * mBorderThickness)
		width -= scrollbar_size;

	//changes the bottom, end of the list goes down in the scroll container
	rc.setLeftTopAndSize(rc.mLeft, rc.mTop, width, height);
	mItemsPanel->setRect(rc);

	//reshaping items
	S32 item_new_top = height;
	pairs_iterator_t it2, first_it = mItemPairs.begin();
	for (it2 = first_it; it2 != mItemPairs.end(); ++it2)
	{
		LLPanel* item = (*it2)->first;

		// skip invisible child
		if (!item->getVisible())
			continue;

		LLRect rc = item->getRect();
		rc.setLeftTopAndSize(rc.mLeft, item_new_top, width, rc.getHeight());
		item->reshape(rc.getWidth(), rc.getHeight());
		item->setRect(rc);

		// move top for next item in list
		item_new_top -= (rc.getHeight() + mItemPad);
	}

	// Stretch selected item rect to ensure it won't be clipped
	mSelectedItemsBorder->setRect(getLastSelectedItemRect().stretch(-1));
}

void LLFlatListView::onItemMouseClick(item_pair_t* item_pair, MASK mask)
{
	if (!item_pair) return;

	if (!item_pair->first) 
	{
		llwarning("Attempt to selet an item pair containing null panel item", 0);
		return;
	}

	setFocus(TRUE);
	
	bool select_item = !isSelected(item_pair);

	//*TODO find a better place for that enforcing stuff
	if (mKeepOneItemSelected && numSelected() == 1 && !select_item) return;

	if ( (mask & MASK_SHIFT) && !(mask & MASK_CONTROL)
		 && mMultipleSelection && !mSelectedItemPairs.empty() )
	{
		item_pair_t* last_selected_pair = mSelectedItemPairs.back();

		// If item_pair is already selected - do nothing
		if (last_selected_pair == item_pair)
			return;

		bool grab_items = false;
		bool reverse = false;
		pairs_list_t pairs_to_select;

		// Pick out items from list between last selected and current clicked item_pair.
		for (pairs_iterator_t
				 iter = mItemPairs.begin(),
				 iter_end = mItemPairs.end();
			 iter != iter_end; ++iter)
		{
			item_pair_t* cur = *iter;
			if (cur == last_selected_pair || cur == item_pair)
			{
				// We've got reverse selection if last grabed item isn't a new selection.
				reverse = grab_items && (cur != item_pair);
				grab_items = !grab_items;
				// Skip last selected and current clicked item pairs.
				continue;
			}
			if (!cur->first->getVisible())
			{
				// Skip invisible item pairs.
				continue;
			}
			if (grab_items)
			{
				pairs_to_select.push_back(cur);
			}
		}

		if (reverse)
		{
			pairs_to_select.reverse();
		}

		pairs_to_select.push_back(item_pair);

		for (pairs_iterator_t
				 iter = pairs_to_select.begin(),
				 iter_end = pairs_to_select.end();
			 iter != iter_end; ++iter)
		{
			item_pair_t* pair_to_select = *iter;
			if (isSelected(pair_to_select))
			{
				// Item was already selected but there is a need to keep order from last selected pair to new selection.
				// Do it here to prevent extra mCommitOnSelectionChange in selectItemPair().
				mSelectedItemPairs.remove(pair_to_select);
				mSelectedItemPairs.push_back(pair_to_select);
			}
			else
			{
				selectItemPair(pair_to_select, true);
			}
		}

		if (!select_item)
		{
			// Update last selected item border.
			mSelectedItemsBorder->setRect(getLastSelectedItemRect().stretch(-1));
		}
		return;
	}

	if (!(mask & MASK_CONTROL) || !mMultipleSelection) resetSelection();
	selectItemPair(item_pair, select_item);
}

void LLFlatListView::onItemRightMouseClick(item_pair_t* item_pair, MASK mask)
{
	if (!item_pair)
		return;

	// Forbid deselecting of items on right mouse button click if mMultipleSelection flag is set on,
	// because some of derived classes may have context menu and selected items must be kept.
	if ( !(mask & MASK_CONTROL) && mMultipleSelection && isSelected(item_pair) )
		return;

	// else got same behavior as at onItemMouseClick
	onItemMouseClick(item_pair, mask);
}

BOOL LLFlatListView::handleKeyHere(KEY key, MASK mask)
{
	BOOL reset_selection = (mask != MASK_SHIFT);
	BOOL handled = FALSE;
	switch (key)
	{
		case KEY_RETURN:
		{
			if (mSelectedItemPairs.size() && mask == MASK_NONE)
			{
				mOnReturnSignal(this, getValue());
				handled = TRUE;
			}
			break;
		}
		case KEY_UP:
		{
			if ( !selectNextItemPair(true, reset_selection) && reset_selection)
			{
				// If case we are in accordion tab notify parent to go to the previous accordion
				if(notifyParent(LLSD().with("action","select_prev")) > 0 )//message was processed
					resetSelection();
			}
			break;
		}
		case KEY_DOWN:
		{
			if ( !selectNextItemPair(false, reset_selection) && reset_selection)
			{
				// If case we are in accordion tab notify parent to go to the next accordion
				if( notifyParent(LLSD().with("action","select_next")) > 0 ) //message was processed
					resetSelection();
			}
			break;
		}
		default:
			break;
	}

	if ( ( key == KEY_UP || key == KEY_DOWN ) && mSelectedItemPairs.size() )
	{
		ensureSelectedVisible();
		/*
		LLRect visible_rc = getVisibleContentRect();
		LLRect selected_rc = getLastSelectedItemRect();

		if ( !visible_rc.contains (selected_rc) )
		{
			// But scroll in Items panel coordinates
			scrollToShowRect(selected_rc);
		}

		// In case we are in accordion tab notify parent to show selected rectangle
		LLRect screen_rc;
		localRectToScreen(selected_rc, &screen_rc);
		notifyParent(LLSD().with("scrollToShowRect",screen_rc.getValue()));*/

		handled = TRUE;
	}

	return handled ? handled : LLScrollContainer::handleKeyHere(key, mask);
}

LLFlatListView::item_pair_t* LLFlatListView::getItemPair(LLPanel* item) const
{
	llassert(item);

	for (pairs_const_iterator_t it= mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		item_pair_t* item_pair = *it;
		if (item_pair->first == item) return item_pair;
	}
	return NULL;
}

//compares two LLSD's
bool llsds_are_equal(const LLSD& llsd_1, const LLSD& llsd_2)
{
	llassert(llsd_1.isDefined());
	llassert(llsd_2.isDefined());
	
	if (llsd_1.type() != llsd_2.type()) return false;

	if (!llsd_1.isMap())
	{
		if (llsd_1.isUUID()) return llsd_1.asUUID() == llsd_2.asUUID();

		//assumptions that string representaion is enough for other types
		return llsd_1.asString() == llsd_2.asString();
	}

	if (llsd_1.size() != llsd_2.size()) return false;

	LLSD::map_const_iterator llsd_1_it = llsd_1.beginMap();
	LLSD::map_const_iterator llsd_2_it = llsd_2.beginMap();
	for (S32 i = 0; i < llsd_1.size(); ++i)
	{
		if ((*llsd_1_it).first != (*llsd_2_it).first) return false;
		if (!llsds_are_equal((*llsd_1_it).second, (*llsd_2_it).second)) return false;
		++llsd_1_it;
		++llsd_2_it;
	}
	return true;
}

LLFlatListView::item_pair_t* LLFlatListView::getItemPair(const LLSD& value) const
{
	llassert(value.isDefined());
	
	for (pairs_const_iterator_t it= mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		item_pair_t* item_pair = *it;
		if (llsds_are_equal(item_pair->second, value)) return item_pair;
	}
	return NULL;
}

bool LLFlatListView::selectItemPair(item_pair_t* item_pair, bool select)
{
	llassert(item_pair);

	if (!mAllowSelection && select) return false;

	if (isSelected(item_pair) == select) return true; //already in specified selection state
	if (select)
	{
		mSelectedItemPairs.push_back(item_pair);
	}
	else
	{
		mSelectedItemPairs.remove(item_pair);
	}

	//a way of notifying panel of selection state changes
	LLPanel* item = item_pair->first;
	item->setValue(select ? SELECTED_EVENT : UNSELECTED_EVENT);

	if (mCommitOnSelectionChange)
	{
		onCommit();
	}

	// Stretch selected item rect to ensure it won't be clipped
	mSelectedItemsBorder->setRect(getLastSelectedItemRect().stretch(-1));
	// By default mark it as not consecutive selection
	mIsConsecutiveSelection = false;

	return true;
}

void LLFlatListView::scrollToShowFirstSelectedItem()
{
	if (!mSelectedItemPairs.size())	return;

	LLRect selected_rc = mSelectedItemPairs.front()->first->getRect();

	if (selected_rc.isValid())
	{
		scrollToShowRect(selected_rc);
	}
}

LLRect LLFlatListView::getLastSelectedItemRect()
{
	if (!mSelectedItemPairs.size())
	{
		return LLRect::null;
	}

	return mSelectedItemPairs.back()->first->getRect();
}

void LLFlatListView::selectFirstItem	()
{
	// No items - no actions!
	if (0 == size()) return;

	// Select first visible item
	for (pairs_iterator_t
			 iter = mItemPairs.begin(),
			 iter_end = mItemPairs.end();
		 iter != iter_end; ++iter)
	{
		// skip invisible items
		if ( (*iter)->first->getVisible() )
		{
			selectItemPair(*iter, true);
			ensureSelectedVisible();
			break;
		}
	}
}

void LLFlatListView::selectLastItem		()
{
	// No items - no actions!
	if (0 == size()) return;

	// Select last visible item
	for (pairs_list_t::reverse_iterator
			 r_iter = mItemPairs.rbegin(),
			 r_iter_end = mItemPairs.rend();
		 r_iter != r_iter_end; ++r_iter)
	{
		// skip invisible items
		if ( (*r_iter)->first->getVisible() )
		{
			selectItemPair(*r_iter, true);
			ensureSelectedVisible();
			break;
		}
	}
}

void LLFlatListView::ensureSelectedVisible()
{
	LLRect selected_rc = getLastSelectedItemRect();

	if ( selected_rc.isValid() )
	{
		scrollToShowRect(selected_rc);
	}
}


// virtual
bool LLFlatListView::selectNextItemPair(bool is_up_direction, bool reset_selection)
{
	// No items - no actions!
	if ( 0 == size() )
		return false;

	if (!mIsConsecutiveSelection)
	{
		// Leave only one item selected if list has not consecutive selection
		if (mSelectedItemPairs.size() && !reset_selection)
		{
			item_pair_t* cur_sel_pair = mSelectedItemPairs.back();
			resetSelection();
			selectItemPair (cur_sel_pair, true);
		}
	}

	if ( mSelectedItemPairs.size() )
	{
		item_pair_t* to_sel_pair = NULL;
		item_pair_t* cur_sel_pair = NULL;

		// Take the last selected pair
		cur_sel_pair = mSelectedItemPairs.back();
		// Bases on given direction choose next item to select
		if ( is_up_direction )
		{
			// Find current selected item position in mItemPairs list
			pairs_list_t::reverse_iterator sel_it = std::find(mItemPairs.rbegin(), mItemPairs.rend(), cur_sel_pair);

			for (;++sel_it != mItemPairs.rend();)
			{
				// skip invisible items
				if ( (*sel_it)->first->getVisible() )
				{
					to_sel_pair = *sel_it;
					break;
				}
			}
		}
		else
		{
			// Find current selected item position in mItemPairs list
			pairs_list_t::iterator sel_it = std::find(mItemPairs.begin(), mItemPairs.end(), cur_sel_pair);

			for (;++sel_it != mItemPairs.end();)
			{
				// skip invisible items
				if ( (*sel_it)->first->getVisible() )
				{
					to_sel_pair = *sel_it;
					break;
				}
			}
		}

		if ( to_sel_pair )
		{
			bool select = true;
			if ( reset_selection )
			{
				// Reset current selection if we were asked about it
				resetSelection();
			}
			else
			{
				// If item already selected and no reset request than we should deselect last selected item.
				select = (mSelectedItemPairs.end() == std::find(mSelectedItemPairs.begin(), mSelectedItemPairs.end(), to_sel_pair));
			}
			// Select/Deselect next item
			selectItemPair(select ? to_sel_pair : cur_sel_pair, select);
			// Mark it as consecutive selection
			mIsConsecutiveSelection = true;
			return true;
		}
	}
	else
	{
		// If there weren't selected items then choose the first one bases on given direction
		// Force selection to first item
		if (is_up_direction)
			selectLastItem();
		else
			selectFirstItem();
		// Mark it as consecutive selection
		mIsConsecutiveSelection = true;
		return true;
	}

	return false;
}

BOOL LLFlatListView::canSelectAll() const
{
	return 0 != size() && mAllowSelection && mMultipleSelection;
}

void LLFlatListView::selectAll()
{
	if (!mAllowSelection || !mMultipleSelection)
		return;

	mSelectedItemPairs.clear();

	for (pairs_const_iterator_t it= mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		item_pair_t* item_pair = *it;
		mSelectedItemPairs.push_back(item_pair);
		//a way of notifying panel of selection state changes
		LLPanel* item = item_pair->first;
		item->setValue(SELECTED_EVENT);
	}

	if (mCommitOnSelectionChange)
	{
		onCommit();
	}

	// Stretch selected item rect to ensure it won't be clipped
	mSelectedItemsBorder->setRect(getLastSelectedItemRect().stretch(-1));
}

bool LLFlatListView::isSelected(item_pair_t* item_pair) const
{
	llassert(item_pair);

	pairs_const_iterator_t it_end = mSelectedItemPairs.end();
	return std::find(mSelectedItemPairs.begin(), it_end, item_pair) != it_end;
}

bool LLFlatListView::removeItemPair(item_pair_t* item_pair)
{
	llassert(item_pair);

	bool deleted = false;
	for (pairs_iterator_t it = mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		item_pair_t* _item_pair = *it;
		if (_item_pair == item_pair)
		{
			mItemPairs.erase(it);
			deleted = true;
			break;
		}
	}

	if (!deleted) return false;

	for (pairs_iterator_t it = mSelectedItemPairs.begin(); it != mSelectedItemPairs.end(); ++it)
	{
		item_pair_t* selected_item_pair = *it;
		if (selected_item_pair == item_pair)
		{
			it = mSelectedItemPairs.erase(it);
			break;
		}
	}

	mItemsPanel->removeChild(item_pair->first);
	item_pair->first->die();
	delete item_pair;

	rearrangeItems();
	notifyParentItemsRectChanged();

	return true;
}

void LLFlatListView::notifyParentItemsRectChanged()
{
	S32 comment_height = 0;

	// take into account comment text height if exists
	if (mNoItemsCommentTextbox && mNoItemsCommentTextbox->getVisible())
	{
		// top text padding inside the textbox is included into the height
		comment_height = mNoItemsCommentTextbox->getTextPixelHeight();

		// take into account a distance from parent's top border to textbox's top
		comment_height += getRect().getHeight() - mNoItemsCommentTextbox->getRect().mTop;
	}

	LLRect req_rect =  getItemsRect();

	// get maximum of items total height and comment text height
	req_rect.setOriginAndSize(req_rect.mLeft, req_rect.mBottom, req_rect.getWidth(), llmax(req_rect.getHeight(), comment_height));

	// take into account border size.
	req_rect.stretch(getBorderWidth());

	if (req_rect == mPrevNotifyParentRect)
		return;

	mPrevNotifyParentRect = req_rect;

	LLSD params;
	params["action"] = "size_changes";
	params["width"] = req_rect.getWidth();
	params["height"] = req_rect.getHeight();

	if (getParent()) // dummy widgets don't have a parent
		getParent()->notifyParent(params);
}

void LLFlatListView::setNoItemsCommentVisible(bool visible) const
{
	if (mNoItemsCommentTextbox)
	{
		if (visible)
		{
/*
// *NOTE: MA 2010-02-04
// Deprecated after params of the comment text box were moved into widget (flat_list_view.xml)
// can be removed later if nothing happened.
			// We have to update child rect here because of issues with rect after reshaping while creating LLTextbox
			// It is possible to have invalid LLRect if Flat List is in LLAccordionTab
			LLRect comment_rect = getLocalRect();

			// To see comment correctly (EXT - 3244) in mNoItemsCommentTextbox we must get border width
			// of LLFlatListView (@see getBorderWidth()) and stretch mNoItemsCommentTextbox to this width
			// But getBorderWidth() returns 0 if LLFlatListView not visible. So we have to get border width
			// from 'scroll_border'
			LLViewBorder* scroll_border = getChild<LLViewBorder>("scroll border");
			comment_rect.stretch(-scroll_border->getBorderWidth());
			mNoItemsCommentTextbox->setRect(comment_rect);
*/
		}
		mNoItemsCommentTextbox->setVisible(visible);
	}
}

void LLFlatListView::getItems(std::vector<LLPanel*>& items) const
{
	if (mItemPairs.empty()) return;

	items.clear();
	for (pairs_const_iterator_t it = mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		items.push_back((*it)->first);
	}
}

void LLFlatListView::getValues(std::vector<LLSD>& values) const
{
	if (mItemPairs.empty()) return;

	values.clear();
	for (pairs_const_iterator_t it = mItemPairs.begin(); it != mItemPairs.end(); ++it)
	{
		values.push_back((*it)->second);
	}
}

// virtual
void LLFlatListView::onFocusReceived()
{
	mSelectedItemsBorder->setVisible(TRUE);
	gEditMenuHandler = this;
}
// virtual
void LLFlatListView::onFocusLost()
{
	mSelectedItemsBorder->setVisible(FALSE);
	// Route menu back to the default
 	if( gEditMenuHandler == this )
	{
		gEditMenuHandler = NULL;
	}
}

//virtual 
S32 LLFlatListView::notify(const LLSD& info)
{
	if(info.has("action"))
	{
		std::string str_action = info["action"];
		if(str_action == "select_first")
		{
			setFocus(true);
			selectFirstItem();
			return 1;
		}
		else if(str_action == "select_last")
		{
			setFocus(true);
			selectLastItem();
			return 1;
		}
	}
	else if (info.has("rearrange"))
	{
		rearrangeItems();
		notifyParentItemsRectChanged();
		return 1;
	}
	return 0;
}

void LLFlatListView::detachItems(std::vector<LLPanel*>& detached_items)
{
	LLSD action;
	action.with("detach", LLSD());
	// Clear detached_items list
	detached_items.clear();
	// Go through items and detach valid items, remove them from items panel
	// and add to detached_items.
	for (pairs_iterator_t
			 iter = mItemPairs.begin(),
			 iter_end = mItemPairs.end();
		 iter != iter_end; ++iter)
	{
		LLPanel* pItem = (*iter)->first;
		if (1 == pItem->notify(action))
		{
			selectItemPair((*iter), false);
			mItemsPanel->removeChild(pItem);
			detached_items.push_back(pItem);
		}
	}
	if (!detached_items.empty())
	{
		// Some items were detached, clean ourself from unusable memory
		if (detached_items.size() == mItemPairs.size())
		{
			// This way will be faster if all items were disconnected
			for (pairs_iterator_t
					 iter = mItemPairs.begin(),
					 iter_end = mItemPairs.end();
				 iter != iter_end; ++iter)
			{
				(*iter)->first = NULL;
				delete *iter;
			}
			mItemPairs.clear();
			// Also set items panel height to zero.
			// Reshape it to allow reshaping of non-item children.
			LLRect rc = mItemsPanel->getRect();
			rc.mBottom = rc.mTop;
			mItemsPanel->reshape(rc.getWidth(), rc.getHeight());
			mItemsPanel->setRect(rc);
			setNoItemsCommentVisible(true);
		}
		else
		{
			for (std::vector<LLPanel*>::const_iterator
					 detached_iter = detached_items.begin(),
					 detached_iter_end = detached_items.end();
				 detached_iter != detached_iter_end; ++detached_iter)
			{
				LLPanel* pDetachedItem = *detached_iter;
				for (pairs_iterator_t
						 iter = mItemPairs.begin(),
						 iter_end = mItemPairs.end();
					 iter != iter_end; ++iter)
				{
					item_pair_t* item_pair = *iter;
					if (item_pair->first == pDetachedItem)
					{
						mItemPairs.erase(iter);
						item_pair->first = NULL;
						delete item_pair;
						break;
					}
				}
			}
			rearrangeItems();
		}
		notifyParentItemsRectChanged();
	}
}


/************************************************************************/
/*             LLFlatListViewEx implementation                          */
/************************************************************************/
LLFlatListViewEx::Params::Params()
: no_items_msg("no_items_msg")
, no_filtered_items_msg("no_filtered_items_msg")
{

}

LLFlatListViewEx::LLFlatListViewEx(const Params& p)
:	LLFlatListView(p)
, mNoFilteredItemsMsg(p.no_filtered_items_msg)
, mNoItemsMsg(p.no_items_msg)
, mForceShowingUnmatchedItems(false)
, mHasMatchedItems(false)
{

}

void LLFlatListViewEx::updateNoItemsMessage(const std::string& filter_string)
{
	bool items_filtered = !filter_string.empty();
	if (items_filtered)
	{
		// items were filtered
		LLStringUtil::format_map_t args;
		args["[SEARCH_TERM]"] = LLURI::escape(filter_string);
		std::string text = mNoFilteredItemsMsg;
		LLStringUtil::format(text, args);
		setNoItemsCommentText(text);
	}
	else
	{
		// list does not contain any items at all
		setNoItemsCommentText(mNoItemsMsg);
	}

}

bool LLFlatListViewEx::getForceShowingUnmatchedItems()
{
	return mForceShowingUnmatchedItems;
}

void LLFlatListViewEx::setForceShowingUnmatchedItems(bool show)
{
	mForceShowingUnmatchedItems = show;
}

void LLFlatListViewEx::setFilterSubString(const std::string& filter_str)
{
	if (0 != LLStringUtil::compareInsensitive(filter_str, mFilterSubString))
	{
		mFilterSubString = filter_str;
		updateNoItemsMessage(mFilterSubString);
		filterItems();
	}
}

void LLFlatListViewEx::filterItems()
{
	typedef std::vector <LLPanel*> item_panel_list_t;

	std::string cur_filter = mFilterSubString;
	LLStringUtil::toUpper(cur_filter);

	LLSD action;
	action.with("match_filter", cur_filter);

	item_panel_list_t items;
	getItems(items);

	mHasMatchedItems = false;
	for (item_panel_list_t::iterator
			 iter = items.begin(),
			 iter_end = items.end();
		 iter != iter_end; ++iter)
	{
		LLPanel* pItem = (*iter);
		// 0 signifies that filter is matched,
		// i.e. we don't hide items that don't support 'match_filter' action, separators etc.
		if (0 == pItem->notify(action))
		{
			mHasMatchedItems = true;
			pItem->setVisible(true);
		}
		else
		{
			// TODO: implement (re)storing of current selection.
			if(!mForceShowingUnmatchedItems)
			{
				selectItem(pItem, false);
			}
			pItem->setVisible(mForceShowingUnmatchedItems);
		}
	}

	sort();
	notifyParentItemsRectChanged();
}

bool LLFlatListViewEx::hasMatchedItems()
{
	return mHasMatchedItems;
}

//EOF