xref: /haiku/src/servers/app/Layer.h (revision 642467d0771f3c0c025dfcc847197d36e3e3703f)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, Haiku, Inc.
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Layer.h
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //					Adi Oanca <adioanca@cotty.iren.com>
25 //					Stephan Aßmus <superstippi@gmx.de>
26 //	Description:	Class used for rendering to the frame buffer. One layer per
27 //					view on screen and also for window decorators
28 //
29 //------------------------------------------------------------------------------
30 #ifndef _LAYER_H_
31 #define _LAYER_H_
32 
33 #include <GraphicsDefs.h>
34 #include <List.h>
35 #include <Locker.h>
36 #include <OS.h>
37 #include <String.h>
38 #include <Rect.h>
39 #include <Region.h>
40 
41 #include "RGBColor.h"
42 #include "ServerWindow.h"
43 
44 //#define NEW_CLIPPING 1
45 
46 enum {
47 	B_LAYER_NONE		= 1,
48 	B_LAYER_MOVE		= 2,
49 	B_LAYER_SIMPLE_MOVE	= 3,
50 	B_LAYER_RESIZE		= 4,
51 	B_LAYER_MASK_RESIZE	= 5,
52 };
53 
54 enum {
55 	B_LAYER_ACTION_NONE = 0,
56 	B_LAYER_ACTION_MOVE,
57 	B_LAYER_ACTION_RESIZE
58 };
59 
60 enum {
61 	B_LAYER_CHILDREN_DEPENDANT = 0x1000U,
62 };
63 
64 // easy way to determine class type
65 enum {
66 	AS_LAYER_CLASS		= 1,
67 	AS_WINBORDER_CLASS	= 2,
68 	AS_ROOTLAYER_CLASS	= 3,
69 };
70 
71 class ServerApp;
72 class RootLayer;
73 class DisplayDriver;
74 class LayerData;
75 class ServerBitmap;
76 
77 class Layer {
78  public:
79 								Layer(BRect frame, const char* name,
80 									  int32 token, uint32 resize,
81 									  uint32 flags, DisplayDriver* driver);
82 	virtual						~Layer();
83 
84 			void				AddChild(Layer* child, ServerWindow* serverWin);
85 			void				RemoveChild(Layer* child);
86 			void				RemoveSelf();
87 			bool				HasChild(Layer* layer);
88 
89 			void				SetRootLayer(RootLayer* rl)
90 									{ fRootLayer = rl; }
91 			RootLayer*			GetRootLayer() const
92 									{ return fRootLayer; }
93 
94 			uint32				CountChildren() const;
95 			Layer*				FindLayer(const int32 token);
96 			Layer*				LayerAt(const BPoint &pt);
97 
98 	virtual	Layer*				FirstChild() const;
99 	virtual	Layer*				NextChild() const;
100 	virtual	Layer*				PreviousChild() const;
101 	virtual	Layer*				LastChild() const;
102 
103 	virtual	void				SetName(const char* name);
104 	inline	const char*			Name() const
105 									{ return fName.String(); }
106 
107 	inline	uint32				ResizeMode() const
108 									{ return fResizeMode; }
109 
110 	virtual	void				SetFlags(uint32 flags);
111 	inline	uint32				Flags() const
112 									{ return fFlags; }
113 
114 #ifndef NEW_CLIPPING
115 	virtual	void				RebuildFullRegion();
116 			void				StartRebuildRegions(const BRegion& reg,
117 													Layer* target,
118 													uint32 action,
119 													BPoint& pt);
120 
121 			void				RebuildRegions(const BRegion& reg,
122 											   uint32 action,
123 											   BPoint pt,
124 											   BPoint ptOffset);
125 
126 			uint32				ResizeOthers(float x, float y,
127 											 BPoint coords[],
128 											 BPoint* ptOffset);
129 
130 			void				EmptyGlobals();
131 
132 #endif
133 			void				Redraw(const BRegion& reg,
134 									   Layer* startFrom = NULL);
135 
136 	virtual	void				Draw(const BRect& r);
137 
138 			void				Show(bool invalidate = true);
139 			void				Hide(bool invalidate = true);
140 			bool				IsHidden() const;
141 
142 	// graphic state
143 			void				PushState();
144 			void				PopState();
145 
146 	// coordinate system
147 			BRect				Bounds() const;
148 			BRect				Frame() const;
149 
150 	virtual	void				MoveBy(float x, float y);
151 	virtual	void				ResizeBy(float x, float y);
152 	virtual	void				ScrollBy(float x, float y);
153 
154 			BPoint				BoundsOrigin() const; // BoundsFrameDiff()?
155 			float				Scale() const;
156 
157 			BPoint				ConvertToParent(BPoint pt);
158 			BRect				ConvertToParent(BRect rect);
159 			BRegion				ConvertToParent(BRegion* reg);
160 
161 			BPoint				ConvertFromParent(BPoint pt);
162 			BRect				ConvertFromParent(BRect rect);
163 			BRegion				ConvertFromParent(BRegion* reg);
164 
165 			BPoint				ConvertToTop(BPoint pt);
166 			BRect				ConvertToTop(BRect rect);
167 			BRegion				ConvertToTop(BRegion* reg);
168 
169 			BPoint				ConvertFromTop(BPoint pt);
170 			BRect				ConvertFromTop(BRect rect);
171 			BRegion				ConvertFromTop(BRegion *reg);
172 
173 
174 			DisplayDriver*		GetDisplayDriver() const
175 									{ return fDriver; }
176 
177 			ServerWindow*		Window() const
178 									{ return fServerWin; }
179 
180 			ServerApp*			App() const
181 									{ return fServerWin? fServerWin->App(): NULL; }
182 
183 	virtual	bool				HasClient()
184 									{ return true; }
185 //			bool				IsServerLayer() const;
186 
187 			uint32				EventMask() const
188 									{ return fEventMask; }
189 
190 			uint32				EventOptions() const
191 									{ return fEventOptions; }
192 
193 			void				PruneTree();
194 
195 	// debugging
196 			void				PrintToStream();
197 			void				PrintNode();
198 			void				PrintTree();
199 
200 	// server "private" - should not be used
201 			void				SetAsTopLayer(bool option)
202 									{ fIsTopLayer = option; }
203 
204 	inline	bool				IsTopLayer() const
205 									{ return fIsTopLayer; }
206 
207 			BRegion*			ClippingRegion() const
208 									{ return fClipReg; }
209 
210 								// automatic background blanking by app_server
211 			void				SetViewColor(const RGBColor& color);
212 	inline	const RGBColor&		ViewColor() const
213 									{ return fViewColor; }
214 
215 			void				SetBackgroundBitmap(const ServerBitmap* bitmap);
216 	inline	const ServerBitmap*	BackgroundBitmap() const
217 									{ return fBackgroundBitmap; }
218 
219 								// overlay support
220 								// TODO: This can't be all, what about color key?
221 			void				SetOverlayBitmap(const ServerBitmap* bitmap);
222 	inline	const ServerBitmap*	OverlayBitmap() const
223 									{ return fOverlayBitmap; }
224 
225 			void				CopyBits(BRect& src, BRect& dst,
226 										 int32 xOffset, int32 yOffset);
227 
228 #ifdef NEW_CLIPPING
229 	inline	const BRegion&		VisibleRegion() const { return fVisible2; }
230 	inline	const BRegion&		FullVisible() const { return fFullVisible2; }
231 
232 	virtual	void				GetWantedRegion(BRegion& reg) const;
233 
234 	virtual	void				MovedByHook(float dx, float dy);
235 	virtual	void				ResizedByHook(float dx, float dy, bool automatic);
236 	virtual	void				ScrolledByHook(float dx, float dy);
237 
238 			void				ConvertToParent2(BPoint* pt) const;
239 			void				ConvertToParent2(BRect* rect) const;
240 			void				ConvertToParent2(BRegion* reg) const;
241 			void				ConvertFromParent2(BPoint* pt) const;
242 			void				ConvertFromParent2(BRect* rect) const;
243 			void				ConvertFromParent2(BRegion* reg) const;
244 
245 			void				ConvertToScreen2(BPoint* pt) const;
246 			void				ConvertToScreen2(BRect* rect) const;
247 			void				ConvertToScreen2(BRegion* reg) const;
248 			void				ConvertFromScreen2(BPoint* pt) const;
249 			void				ConvertFromScreen2(BRect* rect) const;
250 			void				ConvertFromScreen2(BRegion* reg) const;
251 			bool				IsVisuallyHidden() const;
252  private:
253  			void				do_Hide();
254  			void				do_Show();
255 			void				do_RebuildVisibleRegions(const BRegion &invalid,
256 														const Layer *startFrom);
257 			void				do_MoveBy(float dx, float dy);
258 			void				do_ResizeBy(float dx, float dy);
259 			void				do_ScrollBy(float dx, float dy);
260 
261 			void				do_Invalidate(	const BRegion &invalid,
262 												const Layer *startFrom = NULL);
263 
264 			void				do_Redraw(	const BRegion &invalid,
265 											const Layer *startFrom = NULL);
266 
267 			void				rebuild_visible_regions(const BRegion &invalid,
268 													const BRegion &parentLocalVisible,
269 													const Layer *startFrom);
270 
271 	virtual	bool				alter_visible_for_children(BRegion &region);
272 	virtual	void				get_user_regions(BRegion &reg);
273 
274 			void				clear_visible_regions();
275 			void				resize_layer_frame_by(float x, float y);
276 			void				rezize_layer_redraw_more(BRegion &reg, float dx, float dy);
277 			void				resize_layer_full_update_on_resize(BRegion &reg, float dx, float dy);
278 
279 #endif
280  private:
281 			void				do_CopyBits(BRect& src, BRect& dst,
282 											int32 xOffset, int32 yOffset);
283 
284  protected:
285 	friend class RootLayer;
286 	friend class WinBorder;
287 	friend class ServerWindow;
288 // TODO: remove, is here for debugging purposes only
289 friend class OffscreenWinBorder;
290 
291 #ifndef NEW_CLIPPING
292 			void				move_layer(float x, float y);
293 			void				resize_layer(float x, float y);
294 
295 			void				FullInvalidate(const BRect& rect);
296 			void				FullInvalidate(const BRegion& region);
297 			void				Invalidate(const BRegion& region);
298 #endif
299 
300 			BRect				fFrame;
301 // TODO: should be removed or reused in a similar fashion
302 // to hold the accumulated origins from the graphics state stack.
303 // The same needs to be done for "scale". (Keeping an accumulated
304 // value.)
305 //			BPoint				fBoundsLeftTop;
306 			WinBorder*			fOwner;
307 
308 			Layer*				fParent;
309 			Layer*				fPreviousSibling;
310 			Layer*				fNextSibling;
311 			Layer*				fFirstChild;
312 			Layer*				fLastChild;
313 
314 	mutable	Layer*				fCurrent;
315 
316 #ifndef NEW_CLIPPING
317 			BRegion				fVisible;
318 			BRegion				fFullVisible;
319 			BRegion				fFull;
320 			int8				fFrameAction;
321 #else
322  private:
323 			BRegion				fVisible2;
324 			BRegion				fFullVisible2;
325  protected:
326 #endif
327 			BRegion*			fClipReg;
328 
329 			ServerWindow*		fServerWin;
330 			BString				fName;
331 			int32				fViewToken;
332 			uint32				fFlags;
333 			uint32				fResizeMode;
334 			uint32				fEventMask;
335 			uint32				fEventOptions;
336 			bool				fHidden;
337 			bool				fIsTopLayer;
338 			uint16				fAdFlags;
339 			int8				fClassID;
340 
341 			DisplayDriver*		fDriver;
342 			LayerData*			fLayerData;
343 
344 			RootLayer*			fRootLayer;
345 
346  private:
347 			void				RequestDraw(const BRegion& reg,
348 											Layer* startFrom);
349 			ServerWindow*		SearchForServerWindow();
350 
351 			void				SendUpdateMsg(BRegion& reg);
352 			void				AddToViewsWithInvalidCoords() const;
353 			void				SendViewCoordUpdateMsg() const;
354 
355 			RGBColor			fViewColor;
356 
357 	const	ServerBitmap*		fBackgroundBitmap;
358 	const	ServerBitmap*		fOverlayBitmap;
359 
360 };
361 
362 #endif // _LAYER_H_
363