1446ac6d7SIngo Weinhold /*
2446ac6d7SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3446ac6d7SIngo Weinhold * Distributed under the terms of the MIT License.
4446ac6d7SIngo Weinhold */
5446ac6d7SIngo Weinhold #ifndef LIST_SELECTION_MODEL_H
6446ac6d7SIngo Weinhold #define LIST_SELECTION_MODEL_H
7446ac6d7SIngo Weinhold
8446ac6d7SIngo Weinhold
9446ac6d7SIngo Weinhold #include <SupportDefs.h>
10446ac6d7SIngo Weinhold
11*82d09e80SRene Gollent #include <Array.h>
12446ac6d7SIngo Weinhold #include <ObjectList.h>
13446ac6d7SIngo Weinhold
14446ac6d7SIngo Weinhold
15446ac6d7SIngo Weinhold class ListSelectionModel {
16446ac6d7SIngo Weinhold public:
17446ac6d7SIngo Weinhold class Listener;
18446ac6d7SIngo Weinhold
19446ac6d7SIngo Weinhold public:
20446ac6d7SIngo Weinhold ListSelectionModel();
2163481b10SIngo Weinhold ListSelectionModel(
2263481b10SIngo Weinhold const ListSelectionModel& other);
23446ac6d7SIngo Weinhold ~ListSelectionModel();
24446ac6d7SIngo Weinhold
CountSelectedItems()25446ac6d7SIngo Weinhold int32 CountSelectedItems() const
26446ac6d7SIngo Weinhold { return fSelectedItems.Count(); }
27446ac6d7SIngo Weinhold int32 SelectedItemAt(int32 index) const;
28446ac6d7SIngo Weinhold bool IsItemSelected(int32 itemIndex) const;
29446ac6d7SIngo Weinhold
30446ac6d7SIngo Weinhold void Clear();
31446ac6d7SIngo Weinhold bool SelectItem(int32 itemIndex,
32446ac6d7SIngo Weinhold bool extendSelection);
33446ac6d7SIngo Weinhold bool SelectItems(int32 itemIndex, int32 count,
34446ac6d7SIngo Weinhold bool extendSelection);
35446ac6d7SIngo Weinhold void DeselectItem(int32 itemIndex);
36446ac6d7SIngo Weinhold void DeselectItems(int32 itemIndex, int32 count);
37446ac6d7SIngo Weinhold
38446ac6d7SIngo Weinhold void ItemsAdded(int32 itemIndex, int32 count);
39446ac6d7SIngo Weinhold void ItemsRemoved(int32 itemIndex, int32 count);
40446ac6d7SIngo Weinhold
41446ac6d7SIngo Weinhold bool AddListener(Listener* listener);
42446ac6d7SIngo Weinhold void RemoveListener(Listener* listener);
43446ac6d7SIngo Weinhold
4463481b10SIngo Weinhold ListSelectionModel& operator=(const ListSelectionModel& other);
4563481b10SIngo Weinhold
46446ac6d7SIngo Weinhold private:
47446ac6d7SIngo Weinhold typedef BObjectList<Listener> ListenerList;
48446ac6d7SIngo Weinhold
49446ac6d7SIngo Weinhold private:
50446ac6d7SIngo Weinhold int32 _FindItem(int32 itemIndex) const;
51446ac6d7SIngo Weinhold int32 _CountSelectedItemsInRange(int32 index,
52446ac6d7SIngo Weinhold int32 endItemIndex) const;
53446ac6d7SIngo Weinhold
54446ac6d7SIngo Weinhold void _NotifyItemsSelected(int32 index, int32 count);
55446ac6d7SIngo Weinhold void _NotifyItemsDeselected(int32 index,
56446ac6d7SIngo Weinhold int32 count);
57446ac6d7SIngo Weinhold
58446ac6d7SIngo Weinhold private:
59446ac6d7SIngo Weinhold Array<int32> fSelectedItems;
60446ac6d7SIngo Weinhold ListenerList fListeners;
61446ac6d7SIngo Weinhold };
62446ac6d7SIngo Weinhold
63446ac6d7SIngo Weinhold
64446ac6d7SIngo Weinhold class ListSelectionModel::Listener {
65446ac6d7SIngo Weinhold public:
66446ac6d7SIngo Weinhold virtual ~Listener();
67446ac6d7SIngo Weinhold
68446ac6d7SIngo Weinhold virtual void ItemsSelected(ListSelectionModel* model,
69446ac6d7SIngo Weinhold int32 index, int32 count);
70446ac6d7SIngo Weinhold virtual void ItemsDeselected(ListSelectionModel* model,
71446ac6d7SIngo Weinhold int32 index, int32 count);
72446ac6d7SIngo Weinhold };
73446ac6d7SIngo Weinhold
74446ac6d7SIngo Weinhold
75446ac6d7SIngo Weinhold inline bool
IsItemSelected(int32 itemIndex)76446ac6d7SIngo Weinhold ListSelectionModel::IsItemSelected(int32 itemIndex) const
77446ac6d7SIngo Weinhold {
78446ac6d7SIngo Weinhold int32 index = _FindItem(itemIndex);
79446ac6d7SIngo Weinhold return index < fSelectedItems.Count() && fSelectedItems[index] == itemIndex;
80446ac6d7SIngo Weinhold }
81446ac6d7SIngo Weinhold
82446ac6d7SIngo Weinhold
83446ac6d7SIngo Weinhold inline int32
SelectedItemAt(int32 index)84446ac6d7SIngo Weinhold ListSelectionModel::SelectedItemAt(int32 index) const
85446ac6d7SIngo Weinhold {
86446ac6d7SIngo Weinhold return index >= 0 && index < fSelectedItems.Count()
87446ac6d7SIngo Weinhold ? fSelectedItems[index] : -1;
88446ac6d7SIngo Weinhold }
89446ac6d7SIngo Weinhold
90446ac6d7SIngo Weinhold
91446ac6d7SIngo Weinhold inline bool
SelectItem(int32 itemIndex,bool extendSelection)92446ac6d7SIngo Weinhold ListSelectionModel::SelectItem(int32 itemIndex, bool extendSelection)
93446ac6d7SIngo Weinhold {
94446ac6d7SIngo Weinhold return SelectItems(itemIndex, 1, extendSelection);
95446ac6d7SIngo Weinhold }
96446ac6d7SIngo Weinhold
97446ac6d7SIngo Weinhold
98446ac6d7SIngo Weinhold inline void
DeselectItem(int32 itemIndex)99446ac6d7SIngo Weinhold ListSelectionModel::DeselectItem(int32 itemIndex)
100446ac6d7SIngo Weinhold {
101446ac6d7SIngo Weinhold DeselectItems(itemIndex, 1);
102446ac6d7SIngo Weinhold }
103446ac6d7SIngo Weinhold
104446ac6d7SIngo Weinhold
105446ac6d7SIngo Weinhold #endif // LIST_SELECTION_MODEL_H
106