xref: /haiku/src/kits/tracker/ViewState.h (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 #ifndef _VIEW_STATE_H
35 #define _VIEW_STATE_H
36 
37 
38 #include <DataIO.h>
39 #include <String.h>
40 
41 
42 namespace BPrivate {
43 
44 const int32 kColumnStateArchiveVersion = 22;
45 	// bump version when layout or size changes
46 
47 class BColumn {
48 	public:
49 		BColumn(const char *title, float offset, float width,
50 			alignment align, const char *attributeName, uint32 attrType,
51 			const char* displayAs, bool statField, bool editable);
52 		BColumn(const char *title, float offset, float width,
53 			alignment align, const char *attributeName, uint32 attrType,
54 			bool statField, bool editable);
55 		~BColumn();
56 
57 		BColumn(BMallocIO *stream, int32 version, bool endianSwap = false);
58 		BColumn(const BMessage &, int32 index = 0);
59 		static BColumn *InstantiateFromStream(BMallocIO *stream,
60 			bool endianSwap = false);
61 		static BColumn *InstantiateFromMessage(const BMessage &archive,
62 			int32 index = 0);
63 		void ArchiveToStream(BMallocIO *stream) const;
64 		void ArchiveToMessage(BMessage &) const;
65 
66 		const char* Title() const;
67 		float Offset() const;
68 		float Width() const;
69 		alignment Alignment() const;
70 		const char* AttrName() const;
71 		uint32 AttrType() const;
72 		const char* DisplayAs() const;
73 		uint32 AttrHash() const;
74 		bool StatField() const;
75 		bool Editable() const;
76 
77 		void SetOffset(float);
78 		void SetWidth(float);
79 
80 	private:
81 		void _Init(const char *title, float offset, float width,
82 			alignment align, const char *attributeName, uint32 attrType,
83 			const char* displayAs, bool statField, bool editable);
84 		static BColumn* _Sanitize(BColumn* column);
85 
86 		BString fTitle;
87 		float fOffset;
88 		float fWidth;
89 		alignment fAlignment;
90 		BString fAttrName;
91 		BString fDisplayAs;
92 		uint32 fAttrHash;
93 		uint32 fAttrType;
94 		bool fStatField;
95 		bool fEditable;
96 };
97 
98 
99 const int32 kViewStateArchiveVersion = 10;
100 	// bump version when layout or size changes
101 
102 class BViewState {
103 	public:
104 		BViewState();
105 
106 		BViewState(BMallocIO *stream, bool endianSwap = false);
107 		BViewState(const BMessage &message);
108 		static BViewState *InstantiateFromStream(BMallocIO *stream, bool endianSwap = false);
109 		static BViewState *InstantiateFromMessage(const BMessage &message);
110 		void ArchiveToStream(BMallocIO *stream) const;
111 		void ArchiveToMessage(BMessage &message) const;
112 
113 		uint32 ViewMode() const;
114 		uint32 LastIconMode() const;
115 		uint32 IconSize() const;
116 		BPoint ListOrigin() const;
117 		BPoint IconOrigin() const;
118 		uint32 PrimarySort() const;
119 		uint32 SecondarySort() const;
120 		uint32 PrimarySortType() const;
121 		uint32 SecondarySortType() const;
122 		bool ReverseSort() const;
123 
124 		void SetViewMode(uint32);
125 		void SetLastIconMode(uint32);
126 		void SetIconSize(uint32);
127 		void SetListOrigin(BPoint);
128 		void SetIconOrigin(BPoint);
129 		void SetPrimarySort(uint32);
130 		void SetSecondarySort(uint32);
131 		void SetPrimarySortType(uint32);
132 		void SetSecondarySortType(uint32);
133 		void SetReverseSort(bool);
134 
135 		bool StateNeedsSaving();
136 		void MarkSaved();
137 
138 	private:
139 		static BViewState *_Sanitize(BViewState *state, bool fixOnly = false);
140 
141 		uint32 fViewMode;
142 		uint32 fLastIconMode;
143 		uint32 fIconSize;
144 		BPoint fListOrigin;
145 		BPoint fIconOrigin;
146 		uint32 fPrimarySortAttr;
147 		uint32 fSecondarySortAttr;
148 		uint32 fPrimarySortType;
149 		uint32 fSecondarySortType;
150 		bool fReverseSort;
151 		bool fStateNeedsSaving;
152 };
153 
154 
155 inline const char *
156 BColumn::Title() const
157 {
158 	return fTitle.String();
159 }
160 
161 inline float
162 BColumn::Offset() const
163 {
164 	return fOffset;
165 }
166 
167 inline float
168 BColumn::Width() const
169 {
170 	return fWidth;
171 }
172 
173 inline alignment
174 BColumn::Alignment() const
175 {
176 	return fAlignment;
177 }
178 
179 inline const char *
180 BColumn::AttrName() const
181 {
182 	return fAttrName.String();
183 }
184 
185 inline uint32
186 BColumn::AttrHash() const
187 {
188 	return fAttrHash;
189 }
190 
191 inline uint32
192 BColumn::AttrType() const
193 {
194 	return fAttrType;
195 }
196 
197 inline const char *
198 BColumn::DisplayAs() const
199 {
200 	return fDisplayAs.String();
201 }
202 
203 inline bool
204 BColumn::StatField() const
205 {
206 	return fStatField;
207 }
208 
209 inline bool
210 BColumn::Editable() const
211 {
212 	return fEditable;
213 }
214 
215 inline void
216 BColumn::SetWidth(float w)
217 {
218 	fWidth = w;
219 }
220 
221 inline void
222 BColumn::SetOffset(float o)
223 {
224 	fOffset = o;
225 }
226 
227 inline uint32
228 BViewState::ViewMode() const
229 {
230 	return fViewMode;
231 }
232 
233 inline uint32
234 BViewState::LastIconMode() const
235 {
236 	return fLastIconMode;
237 }
238 
239 inline uint32
240 BViewState::IconSize() const
241 {
242 	return fIconSize;
243 }
244 
245 inline BPoint
246 BViewState::ListOrigin() const
247 {
248 	return fListOrigin;
249 }
250 
251 inline BPoint
252 BViewState::IconOrigin() const
253 {
254 	return fIconOrigin;
255 }
256 
257 inline uint32
258 BViewState::PrimarySort() const
259 {
260 	return fPrimarySortAttr;
261 }
262 
263 inline uint32
264 BViewState::SecondarySort() const
265 {
266 	return fSecondarySortAttr;
267 }
268 
269 inline uint32
270 BViewState::PrimarySortType() const
271 {
272 	return fPrimarySortType;
273 }
274 
275 inline uint32
276 BViewState::SecondarySortType() const
277 {
278 	return fSecondarySortType;
279 }
280 
281 inline bool
282 BViewState::ReverseSort() const
283 {
284 	return fReverseSort;
285 }
286 
287 inline void
288 BViewState::SetViewMode(uint32 mode)
289 {
290 	if (mode != fViewMode)
291 		fStateNeedsSaving = true;
292 
293 	fViewMode = mode;
294 }
295 
296 inline void
297 BViewState::SetLastIconMode(uint32 mode)
298 {
299 	if (mode != fLastIconMode)
300 		fStateNeedsSaving = true;
301 
302 	fLastIconMode = mode;
303 }
304 
305 inline void
306 BViewState::SetIconSize(uint32 size)
307 {
308 	fIconSize = size;
309 }
310 
311 inline void
312 BViewState::SetListOrigin(BPoint newOrigin)
313 {
314 	if (newOrigin != fListOrigin)
315 		fStateNeedsSaving = true;
316 
317 	fListOrigin = newOrigin;
318 }
319 
320 inline void
321 BViewState::SetIconOrigin(BPoint newOrigin)
322 {
323 	if (newOrigin != fIconOrigin)
324 		fStateNeedsSaving = true;
325 
326 	fIconOrigin = newOrigin;
327 }
328 
329 inline void
330 BViewState::SetPrimarySort(uint32 attr)
331 {
332 	if (attr != fPrimarySortAttr)
333 		fStateNeedsSaving = true;
334 
335 	fPrimarySortAttr = attr;
336 }
337 
338 inline void
339 BViewState::SetSecondarySort(uint32 attr)
340 {
341 	if (attr != fSecondarySortAttr)
342 		fStateNeedsSaving = true;
343 
344 	fSecondarySortAttr = attr;
345 }
346 
347 inline void
348 BViewState::SetPrimarySortType(uint32 type)
349 {
350 	if (type != fPrimarySortType)
351 		fStateNeedsSaving = true;
352 
353 	fPrimarySortType = type;
354 }
355 
356 inline void
357 BViewState::SetSecondarySortType(uint32 type)
358 {
359 	if (type != fSecondarySortType)
360 		fStateNeedsSaving = true;
361 
362 	fSecondarySortType = type;
363 }
364 
365 inline void
366 BViewState::SetReverseSort(bool on)
367 {
368 	if (fReverseSort != on)
369 		fStateNeedsSaving = true;
370 
371 	fReverseSort = on;
372 }
373 
374 inline bool
375 BViewState::StateNeedsSaving()
376 {
377 	return fStateNeedsSaving;
378 }
379 
380 inline void
381 BViewState::MarkSaved()
382 {
383 	fStateNeedsSaving = false;
384 }
385 
386 
387 } // namespace BPrivate
388 
389 using namespace BPrivate;
390 
391 #endif
392