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