1/*
2 * Copyright (c) 2001-2008, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		DarkWyrm <bpmagic@columbus.rr.com>
7 *		Adi Oanca <adioanca@gmail.com>
8 *		Axel Dörfler, axeld@pinc-software.de
9 *		Stephan Aßmus <superstippi@gmx.de>
10 *		Marcus Overhagen <marcus@overhagen.de>
11 */
12#ifndef	VIEW_H
13#define VIEW_H
14
15
16#include "IntRect.h"
17
18#include <GraphicsDefs.h>
19#include <ObjectList.h>
20#include <Region.h>
21#include <String.h>
22
23class BList;
24class BMessage;
25
26namespace BPrivate {
27	class PortLink;
28};
29
30class DrawState;
31class DrawingEngine;
32class Overlay;
33class Window;
34class ServerBitmap;
35class ServerCursor;
36class ServerPicture;
37class BGradient;
38
39class View {
40 public:
41							View(IntRect frame, IntPoint scrollingOffset,
42								const char* name, int32 token,
43								uint32 resizeMode, uint32 flags);
44	virtual					~View();
45			status_t		InitCheck() const;
46
47			int32			Token() const
48								{ return fToken; }
49
50			IntRect			Frame() const
51								{ return fFrame; }
52			IntRect			Bounds() const;
53
54			void			SetResizeMode(uint32 resizeMode)
55								{ fResizeMode = resizeMode; }
56
57			void			SetName(const char* string);
58			const char*		Name() const
59								{ return fName.String(); }
60
61			void			SetFlags(uint32 flags);
62			uint32			Flags() const
63								{ return fFlags; }
64
65	inline	IntPoint		ScrollingOffset() const
66								{ return fScrollingOffset; }
67
68			void			SetDrawingOrigin(BPoint origin);
69			BPoint			DrawingOrigin() const;
70
71			void			SetScale(float scale);
72			float			Scale() const;
73
74			void			SetUserClipping(const BRegion* region);
75				// region is expected in view coordinates
76
77			// converts the given frame up the view hierarchy and
78			// clips to each views bounds
79			void			ConvertToVisibleInTopView(IntRect* bounds) const;
80
81	virtual	void			AttachedToWindow(::Window* window);
82	virtual void			DetachedFromWindow();
83			::Window*		Window() const { return fWindow; }
84
85			// tree stuff
86			void			AddChild(View* view);
87			bool			RemoveChild(View* view);
88
89	inline	View*			Parent() const
90								{ return fParent; }
91
92	inline	View*			FirstChild() const
93								{ return fFirstChild; }
94	inline	View*			LastChild() const
95								{ return fLastChild; }
96	inline	View*			PreviousSibling() const
97								{ return fPreviousSibling; }
98	inline	View*			NextSibling() const
99								{ return fNextSibling; }
100
101			View*			TopView();
102
103			uint32			CountChildren(bool deep = false) const;
104			void			CollectTokensForChildren(BList* tokenMap) const;
105			void			FindViews(uint32 flags, BObjectList<View>& list,
106								int32& left);
107
108			View*			ViewAt(const BPoint& where);
109
110			// coordinate conversion
111			void			ConvertToParent(BPoint* point) const;
112			void			ConvertToParent(IntPoint* point) const;
113			void			ConvertToParent(BRect* rect) const;
114			void			ConvertToParent(IntRect* rect) const;
115			void			ConvertToParent(BRegion* region) const;
116
117			void			ConvertFromParent(BPoint* point) const;
118			void			ConvertFromParent(IntPoint* point) const;
119			void			ConvertFromParent(BRect* rect) const;
120			void			ConvertFromParent(IntRect* rect) const;
121			void			ConvertFromParent(BRegion* region) const;
122
123			void			ConvertToScreen(BPoint* point) const;
124			void			ConvertToScreen(IntPoint* point) const;
125			void			ConvertToScreen(BRect* rect) const;
126			void			ConvertToScreen(IntRect* rect) const;
127			void			ConvertToScreen(BRegion* region) const;
128
129			void			ConvertFromScreen(BPoint* point) const;
130			void			ConvertFromScreen(IntPoint* point) const;
131			void			ConvertFromScreen(BRect* rect) const;
132			void			ConvertFromScreen(IntRect* rect) const;
133			void			ConvertFromScreen(BRegion* region) const;
134
135			void			ConvertToScreenForDrawing(BPoint* point) const;
136			void			ConvertToScreenForDrawing(BRect* rect) const;
137			void			ConvertToScreenForDrawing(BRegion* region) const;
138			void			ConvertToScreenForDrawing(BGradient* gradient) const;
139
140			void			ConvertToScreenForDrawing(BPoint* dst, const BPoint* src, int32 num) const;
141			void			ConvertToScreenForDrawing(BRect* dst, const BRect* src, int32 num) const;
142			void			ConvertToScreenForDrawing(BRegion* dst, const BRegion* src, int32 num) const;
143
144			void			ConvertFromScreenForDrawing(BPoint* point) const;
145				// used when updating the pen position
146
147			void			MoveBy(int32 dx, int32 dy,
148								BRegion* dirtyRegion);
149
150			void			ResizeBy(int32 dx, int32 dy,
151								BRegion* dirtyRegion);
152
153			void			ScrollBy(int32 dx, int32 dy,
154								BRegion* dirtyRegion);
155
156			void			ParentResized(int32 dx, int32 dy,
157								BRegion* dirtyRegion);
158
159			void			CopyBits(IntRect src, IntRect dst,
160								BRegion& windowContentClipping);
161
162			const BRegion&	LocalClipping() const { return fLocalClipping; }
163
164			const rgb_color& ViewColor() const
165								{ return fViewColor; }
166			void			SetViewColor(const rgb_color& color)
167								{ fViewColor = color; }
168
169			ServerBitmap*	ViewBitmap() const
170								{ return fViewBitmap; }
171			void			SetViewBitmap(ServerBitmap* bitmap,
172								IntRect sourceRect, IntRect destRect,
173								int32 resizingMode, int32 options);
174
175			void			PushState();
176			void			PopState();
177			DrawState*		CurrentState() const { return fDrawState; }
178
179			void			SetEventMask(uint32 eventMask, uint32 options);
180			uint32			EventMask() const
181								{ return fEventMask; }
182			uint32			EventOptions() const
183								{ return fEventOptions; }
184
185			void			SetCursor(ServerCursor* cursor);
186			ServerCursor*	Cursor() const { return fCursor; }
187
188			void			SetPicture(ServerPicture* picture);
189			ServerPicture*	Picture() const
190								{ return fPicture; }
191
192			// for background clearing
193			virtual void	Draw(DrawingEngine* drawingEngine,
194								BRegion* effectiveClipping,
195								BRegion* windowContentClipping,
196								bool deep = false);
197
198			virtual void	MouseDown(BMessage* message, BPoint where);
199			virtual void	MouseUp(BMessage* message, BPoint where);
200			virtual void	MouseMoved(BMessage* message, BPoint where);
201
202			void			SetHidden(bool hidden);
203			bool			IsHidden() const;
204
205			// takes into account parent views hidden status
206			bool			IsVisible() const
207								{ return fVisible; }
208			// update visible status for this view and all children
209			// according to the parents visibility
210			void			UpdateVisibleDeep(bool parentVisible);
211
212			void			UpdateOverlay();
213
214			void			MarkBackgroundDirty();
215			bool			IsBackgroundDirty() const
216								{ return fBackgroundDirty; }
217
218			bool			IsDesktopBackground() const
219								{ return fIsDesktopBackground; }
220
221			void			AddTokensForViewsInRegion(BPrivate::PortLink& link,
222								BRegion& region,
223								BRegion* windowContentClipping);
224
225			// clipping
226			void			RebuildClipping(bool deep);
227			BRegion&		ScreenAndUserClipping(
228								BRegion* windowContentClipping,
229								bool force = false) const;
230			void			InvalidateScreenClipping();
231	inline	bool			IsScreenClippingValid() const
232								{
233									return fScreenClippingValid
234										&& (fUserClipping == NULL
235										|| (fUserClipping != NULL
236										&& fScreenAndUserClipping != NULL));
237								}
238
239			// debugging
240			void			PrintToStream() const;
241#if 0
242			bool			MarkAt(DrawingEngine* engine, const BPoint& where,
243								int32 level = 0);
244#endif
245
246	protected:
247			BRegion&		_ScreenClipping(BRegion* windowContentClipping,
248								bool force = false) const;
249			void			_MoveScreenClipping(int32 x, int32 y,
250								bool deep);
251			Overlay*		_Overlay() const;
252			void			_UpdateOverlayView() const;
253
254			BString			fName;
255			int32			fToken;
256			// area within parent coordinate space
257			IntRect			fFrame;
258			// offset of the local area (bounds)
259			IntPoint		fScrollingOffset;
260
261			rgb_color		fViewColor;
262			DrawState*		fDrawState;
263			ServerBitmap*	fViewBitmap;
264			IntRect			fBitmapSource;
265			IntRect			fBitmapDestination;
266			int32			fBitmapResizingMode;
267			int32			fBitmapOptions;
268
269			uint32			fResizeMode;
270			uint32			fFlags;
271			bool			fHidden : 1;
272			bool			fVisible : 1;
273			bool			fBackgroundDirty : 1;
274			bool			fIsDesktopBackground : 1;
275
276			uint32			fEventMask;
277			uint32			fEventOptions;
278
279			::Window*		fWindow;
280			View*			fParent;
281
282			View*			fFirstChild;
283			View*			fPreviousSibling;
284			View*			fNextSibling;
285			View*			fLastChild;
286
287			ServerCursor*	fCursor;
288			ServerPicture*	fPicture;
289
290			// clipping
291			BRegion			fLocalClipping;
292
293	mutable	BRegion			fScreenClipping;
294	mutable	bool			fScreenClippingValid;
295
296			BRegion*		fUserClipping;
297	mutable	BRegion*		fScreenAndUserClipping;
298};
299
300#endif	// VIEW_H
301