xref: /haiku/src/servers/app/Layer.h (revision 2138d632a6ea8a98434e9b207d8f3adb421326d7)
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 //#define NEW_INPUT_HANDLING 1
46 
47 enum {
48 	B_LAYER_NONE		= 1,
49 	B_LAYER_MOVE		= 2,
50 	B_LAYER_SIMPLE_MOVE	= 3,
51 	B_LAYER_RESIZE		= 4,
52 	B_LAYER_MASK_RESIZE	= 5,
53 };
54 
55 enum {
56 	B_LAYER_ACTION_NONE = 0,
57 	B_LAYER_ACTION_MOVE,
58 	B_LAYER_ACTION_RESIZE
59 };
60 
61 enum {
62 	B_LAYER_CHILDREN_DEPENDANT = 0x1000U,
63 };
64 
65 // easy way to determine class type
66 enum {
67 	AS_LAYER_CLASS		= 1,
68 	AS_WINBORDER_CLASS	= 2,
69 	AS_ROOTLAYER_CLASS	= 3,
70 };
71 
72 class ServerApp;
73 class RootLayer;
74 class DisplayDriver;
75 class LayerData;
76 class ServerBitmap;
77 
78 class PointerEvent {
79  public:
80 	int32		code;		// B_MOUSE_UP, B_MOUSE_DOWN, B_MOUSE_MOVED,
81 							// B_MOUSE_WHEEL_CHANGED
82 	bigtime_t	when;
83 	BPoint		where;
84 	float		wheel_delta_x;
85 	float		wheel_delta_y;
86 	int32		modifiers;
87 	int32		buttons;	// B_PRIMARY_MOUSE_BUTTON, B_SECONDARY_MOUSE_BUTTON
88 							// B_TERTIARY_MOUSE_BUTTON
89 	int32		clicks;
90 };
91 
92 class Layer {
93  public:
94 								Layer(BRect frame, const char* name,
95 									  int32 token, uint32 resize,
96 									  uint32 flags, DisplayDriver* driver);
97 	virtual						~Layer();
98 
99 			void				AddChild(Layer* child, ServerWindow* serverWin);
100 			void				RemoveChild(Layer* child);
101 			void				RemoveSelf();
102 			bool				HasChild(Layer* layer);
103 
104 			void				SetRootLayer(RootLayer* rl)
105 									{ fRootLayer = rl; }
106 			RootLayer*			GetRootLayer() const
107 									{ return fRootLayer; }
108 
109 			uint32				CountChildren() const;
110 			Layer*				FindLayer(const int32 token);
111 			Layer*				LayerAt(const BPoint &pt);
112 
113 	virtual	Layer*				FirstChild() const;
114 	virtual	Layer*				NextChild() const;
115 	virtual	Layer*				PreviousChild() const;
116 	virtual	Layer*				LastChild() const;
117 
118 	virtual	void				SetName(const char* name);
119 	inline	const char*			Name() const
120 									{ return fName.String(); }
121 
122 	inline	uint32				ResizeMode() const
123 									{ return fResizeMode; }
124 
125 	virtual	void				SetFlags(uint32 flags);
126 	inline	uint32				Flags() const
127 									{ return fFlags; }
128 
129 #ifndef NEW_CLIPPING
130 	virtual	void				RebuildFullRegion();
131 			void				StartRebuildRegions(const BRegion& reg,
132 													Layer* target,
133 													uint32 action,
134 													BPoint& pt);
135 
136 			void				RebuildRegions(const BRegion& reg,
137 											   uint32 action,
138 											   BPoint pt,
139 											   BPoint ptOffset);
140 
141 			uint32				ResizeOthers(float x, float y,
142 											 BPoint coords[],
143 											 BPoint* ptOffset);
144 
145 			void				EmptyGlobals();
146 
147 #endif
148 			void				Redraw(const BRegion& reg,
149 									   Layer* startFrom = NULL);
150 
151 	virtual	void				Draw(const BRect& r);
152 
153 			void				Show(bool invalidate = true);
154 			void				Hide(bool invalidate = true);
155 			bool				IsHidden() const;
156 
157 	// graphic state
158 			void				PushState();
159 			void				PopState();
160 
161 	// coordinate system
162 			BRect				Bounds() const;
163 			BRect				Frame() const;
164 
165 	virtual	void				MoveBy(float x, float y);
166 	virtual	void				ResizeBy(float x, float y);
167 	virtual	void				ScrollBy(float x, float y);
168 #ifdef NEW_INPUT_HANDLING
169 	virtual	void				MouseDown(const PointerEvent& evt);
170 	virtual	void				MouseUp(const PointerEvent& evt);
171 	virtual	void				MouseMoved(const PointerEvent& evt, uint32 transit);
172 #endif
173 			BPoint				BoundsOrigin() const; // BoundsFrameDiff()?
174 			float				Scale() const;
175 
176 			BPoint				ConvertToParent(BPoint pt);
177 			BRect				ConvertToParent(BRect rect);
178 			BRegion				ConvertToParent(BRegion* reg);
179 
180 			BPoint				ConvertFromParent(BPoint pt);
181 			BRect				ConvertFromParent(BRect rect);
182 			BRegion				ConvertFromParent(BRegion* reg);
183 
184 			BPoint				ConvertToTop(BPoint pt);
185 			BRect				ConvertToTop(BRect rect);
186 			BRegion				ConvertToTop(BRegion* reg);
187 
188 			BPoint				ConvertFromTop(BPoint pt);
189 			BRect				ConvertFromTop(BRect rect);
190 			BRegion				ConvertFromTop(BRegion *reg);
191 
192 
193 			DisplayDriver*		GetDisplayDriver() const
194 									{ return fDriver; }
195 
196 			ServerWindow*		Window() const
197 									{ return fServerWin; }
198 
199 			ServerApp*			App() const
200 									{ return fServerWin? fServerWin->App(): NULL; }
201 
202 	virtual	bool				HasClient()
203 									{ return true; }
204 //			bool				IsServerLayer() const;
205 
206 			uint32				EventMask() const
207 									{ return fEventMask; }
208 
209 			uint32				EventOptions() const
210 									{ return fEventOptions; }
211 
212 	inline	void				QuietlySetEventMask(uint32 em)
213 									{ fEventMask = em; }
214 
215 	inline	void				QuietlySetEventOptions(uint32 eo)
216 									{ fEventOptions = eo; }
217 
218 			void				PruneTree();
219 
220 	// debugging
221 			void				PrintToStream();
222 			void				PrintNode();
223 			void				PrintTree();
224 
225 	// server "private" - should not be used
226 			void				SetAsTopLayer(bool option)
227 									{ fIsTopLayer = option; }
228 
229 	inline	bool				IsTopLayer() const
230 									{ return fIsTopLayer; }
231 
232 			BRegion*			ClippingRegion() const
233 									{ return fClipReg; }
234 
235 								// automatic background blanking by app_server
236 			void				SetViewColor(const RGBColor& color);
237 	inline	const RGBColor&		ViewColor() const
238 									{ return fViewColor; }
239 
240 			void				SetBackgroundBitmap(const ServerBitmap* bitmap);
241 	inline	const ServerBitmap*	BackgroundBitmap() const
242 									{ return fBackgroundBitmap; }
243 
244 								// overlay support
245 								// TODO: This can't be all, what about color key?
246 			void				SetOverlayBitmap(const ServerBitmap* bitmap);
247 	inline	const ServerBitmap*	OverlayBitmap() const
248 									{ return fOverlayBitmap; }
249 
250 			void				CopyBits(BRect& src, BRect& dst,
251 										 int32 xOffset, int32 yOffset);
252 
253 #ifdef NEW_CLIPPING
254 	inline	const BRegion&		VisibleRegion() const { return fVisible2; }
255 	inline	const BRegion&		FullVisible() const { return fFullVisible2; }
256 
257 	virtual	void				GetWantedRegion(BRegion& reg) const;
258 
259 	virtual	void				MovedByHook(float dx, float dy);
260 	virtual	void				ResizedByHook(float dx, float dy, bool automatic);
261 	virtual	void				ScrolledByHook(float dx, float dy);
262 
263 			void				ConvertToParent2(BPoint* pt) const;
264 			void				ConvertToParent2(BRect* rect) const;
265 			void				ConvertToParent2(BRegion* reg) const;
266 			void				ConvertFromParent2(BPoint* pt) const;
267 			void				ConvertFromParent2(BRect* rect) const;
268 			void				ConvertFromParent2(BRegion* reg) const;
269 
270 			void				ConvertToScreen2(BPoint* pt) const;
271 			void				ConvertToScreen2(BRect* rect) const;
272 			void				ConvertToScreen2(BRegion* reg) const;
273 			void				ConvertFromScreen2(BPoint* pt) const;
274 			void				ConvertFromScreen2(BRect* rect) const;
275 			void				ConvertFromScreen2(BRegion* reg) const;
276 			bool				IsVisuallyHidden() const;
277  private:
278  			void				do_Hide();
279  			void				do_Show();
280 			void				do_RebuildVisibleRegions(const BRegion &invalid,
281 														const Layer *startFrom);
282 			void				do_MoveBy(float dx, float dy);
283 			void				do_ResizeBy(float dx, float dy);
284 			void				do_ScrollBy(float dx, float dy);
285 
286 			void				do_Invalidate(	const BRegion &invalid,
287 												const Layer *startFrom = NULL);
288 
289 			void				do_Redraw(	const BRegion &invalid,
290 											const Layer *startFrom = NULL);
291 
292 			void				rebuild_visible_regions(const BRegion &invalid,
293 													const BRegion &parentLocalVisible,
294 													const Layer *startFrom);
295 
296 	virtual	bool				alter_visible_for_children(BRegion &region);
297 	virtual	void				get_user_regions(BRegion &reg);
298 
299 			void				clear_visible_regions();
300 			void				resize_layer_frame_by(float x, float y);
301 			void				rezize_layer_redraw_more(BRegion &reg, float dx, float dy);
302 			void				resize_layer_full_update_on_resize(BRegion &reg, float dx, float dy);
303 
304 #endif
305  private:
306 			void				do_CopyBits(BRect& src, BRect& dst,
307 											int32 xOffset, int32 yOffset);
308 
309  protected:
310 	friend class RootLayer;
311 	friend class WinBorder;
312 	friend class ServerWindow;
313 // TODO: remove, is here for debugging purposes only
314 friend class OffscreenWinBorder;
315 
316 #ifndef NEW_CLIPPING
317 			void				move_layer(float x, float y);
318 			void				resize_layer(float x, float y);
319 
320 			void				FullInvalidate(const BRect& rect);
321 			void				FullInvalidate(const BRegion& region);
322 			void				Invalidate(const BRegion& region);
323 #endif
324 
325 			BRect				fFrame;
326 // TODO: should be removed or reused in a similar fashion
327 // to hold the accumulated origins from the graphics state stack.
328 // The same needs to be done for "scale". (Keeping an accumulated
329 // value.)
330 //			BPoint				fBoundsLeftTop;
331 			WinBorder*			fOwner;
332 
333 			Layer*				fParent;
334 			Layer*				fPreviousSibling;
335 			Layer*				fNextSibling;
336 			Layer*				fFirstChild;
337 			Layer*				fLastChild;
338 
339 	mutable	Layer*				fCurrent;
340 
341 #ifndef NEW_CLIPPING
342 			BRegion				fVisible;
343 			BRegion				fFullVisible;
344 			BRegion				fFull;
345 			int8				fFrameAction;
346 #else
347  private:
348 			BRegion				fVisible2;
349 			BRegion				fFullVisible2;
350  protected:
351 #endif
352 			BRegion*			fClipReg;
353 
354 			ServerWindow*		fServerWin;
355 			BString				fName;
356 			int32				fViewToken;
357 			uint32				fFlags;
358 			uint32				fResizeMode;
359 			uint32				fEventMask;
360 			uint32				fEventOptions;
361 			bool				fHidden;
362 			bool				fIsTopLayer;
363 			uint16				fAdFlags;
364 			int8				fClassID;
365 
366 			DisplayDriver*		fDriver;
367 			LayerData*			fLayerData;
368 
369 			RootLayer*			fRootLayer;
370 
371  private:
372 			void				RequestDraw(const BRegion& reg,
373 											Layer* startFrom);
374 			ServerWindow*		SearchForServerWindow();
375 
376 			void				SendUpdateMsg(BRegion& reg);
377 			void				AddToViewsWithInvalidCoords() const;
378 			void				SendViewCoordUpdateMsg() const;
379 
380 			RGBColor			fViewColor;
381 
382 	const	ServerBitmap*		fBackgroundBitmap;
383 	const	ServerBitmap*		fOverlayBitmap;
384 
385 };
386 
387 #endif // _LAYER_H_
388