xref: /haiku/src/apps/debuganalyzer/gui/ListSelectionModel.h (revision 63481b10d3fbbc39c40a71af242f21ae42eb2cd4)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef LIST_SELECTION_MODEL_H
6 #define LIST_SELECTION_MODEL_H
7 
8 
9 #include <SupportDefs.h>
10 
11 #include <ObjectList.h>
12 
13 #include "../debugger/util/Array.h"
14 
15 
16 class ListSelectionModel {
17 public:
18 			class Listener;
19 
20 public:
21 								ListSelectionModel();
22 								ListSelectionModel(
23 									const ListSelectionModel& other);
24 								~ListSelectionModel();
25 
26 			int32				CountSelectedItems() const
27 									{ return fSelectedItems.Count(); }
28 			int32				SelectedItemAt(int32 index) const;
29 			bool				IsItemSelected(int32 itemIndex) const;
30 
31 			void				Clear();
32 			bool				SelectItem(int32 itemIndex,
33 									bool extendSelection);
34 			bool				SelectItems(int32 itemIndex, int32 count,
35 									bool extendSelection);
36 			void				DeselectItem(int32 itemIndex);
37 			void				DeselectItems(int32 itemIndex, int32 count);
38 
39 			void				ItemsAdded(int32 itemIndex, int32 count);
40 			void				ItemsRemoved(int32 itemIndex, int32 count);
41 
42 			bool				AddListener(Listener* listener);
43 			void				RemoveListener(Listener* listener);
44 
45 			ListSelectionModel&	operator=(const ListSelectionModel& other);
46 
47 private:
48 			typedef BObjectList<Listener> ListenerList;
49 
50 private:
51 			int32				_FindItem(int32 itemIndex) const;
52 			int32				_CountSelectedItemsInRange(int32 index,
53 									int32 endItemIndex) const;
54 
55 			void				_NotifyItemsSelected(int32 index, int32 count);
56 			void				_NotifyItemsDeselected(int32 index,
57 									int32 count);
58 
59 private:
60 			Array<int32>		fSelectedItems;
61 			ListenerList		fListeners;
62 };
63 
64 
65 class ListSelectionModel::Listener {
66 public:
67 	virtual						~Listener();
68 
69 	virtual	void				ItemsSelected(ListSelectionModel* model,
70 									int32 index, int32 count);
71 	virtual	void				ItemsDeselected(ListSelectionModel* model,
72 									int32 index, int32 count);
73 };
74 
75 
76 inline bool
77 ListSelectionModel::IsItemSelected(int32 itemIndex) const
78 {
79 	int32 index = _FindItem(itemIndex);
80 	return index < fSelectedItems.Count() && fSelectedItems[index] == itemIndex;
81 }
82 
83 
84 inline int32
85 ListSelectionModel::SelectedItemAt(int32 index) const
86 {
87 	return index >= 0 && index < fSelectedItems.Count()
88 		? fSelectedItems[index] : -1;
89 }
90 
91 
92 inline bool
93 ListSelectionModel::SelectItem(int32 itemIndex, bool extendSelection)
94 {
95 	return SelectItems(itemIndex, 1, extendSelection);
96 }
97 
98 
99 inline void
100 ListSelectionModel::DeselectItem(int32 itemIndex)
101 {
102 	DeselectItems(itemIndex, 1);
103 }
104 
105 
106 #endif	// LIST_SELECTION_MODEL_H
107