xref: /haiku/src/servers/app/Layer.h (revision cb24f13bb05dfd2a6c92ec8198c4c7a811728767)
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 			DrawState*			CurrentState() const { return fDrawState; }
153 
154 	// coordinate system
155 			BRect				Bounds() const;
156 			BRect				Frame() const;
157 
158 	virtual	void				MoveBy(float x, float y);
159 	virtual	void				ResizeBy(float x, float y);
160 	virtual	void				ScrollBy(float x, float y);
161 
162 	virtual	void				MouseDown(const BMessage *msg);
163 	virtual	void				MouseUp(const BMessage *msg);
164 	virtual	void				MouseMoved(const BMessage *msg);
165 	virtual	void				MouseWheelChanged(const BMessage *msg);
166 
167 	virtual	void				KeyDown(const BMessage *msg);
168 	virtual	void				KeyUp(const BMessage *msg);
169 	virtual	void				UnmappedKeyDown(const BMessage *msg);
170 	virtual	void				UnmappedKeyUp(const BMessage *msg);
171 	virtual	void				ModifiersChanged(const BMessage *msg);
172 
173 	virtual	void				WorkspaceActivated(int32 index, bool active);
174 	virtual	void				WorkspacesChanged(uint32 oldWorkspaces, uint32 newWorkspaces);
175 	virtual	void				Activated(bool active);
176 
177 			BPoint				BoundsOrigin() const; // BoundsFrameDiff()?
178 			float				Scale() const;
179 
180 			BPoint				ConvertToParent(BPoint pt);
181 			BRect				ConvertToParent(BRect rect);
182 			BRegion				ConvertToParent(BRegion* reg);
183 
184 			BPoint				ConvertFromParent(BPoint pt);
185 			BRect				ConvertFromParent(BRect rect);
186 			BRegion				ConvertFromParent(BRegion* reg);
187 
188 			BPoint				ConvertToTop(BPoint pt);
189 			BRect				ConvertToTop(BRect rect);
190 			BRegion				ConvertToTop(BRegion* reg);
191 
192 			BPoint				ConvertFromTop(BPoint pt);
193 			BRect				ConvertFromTop(BRect rect);
194 			BRegion				ConvertFromTop(BRegion *reg);
195 
196 
197 			DisplayDriver*		GetDisplayDriver() const
198 									{ return fDriver; }
199 
200 			ServerWindow*		Window() const
201 									{ return fServerWin; }
202 
203 			ServerApp*			App() const
204 									{ return fServerWin? fServerWin->App(): NULL; }
205 
206 	inline	WinBorder*			Owner() const
207 									{ return fOwner; }
208 
209 	virtual	bool				HasClient()
210 									{ return true; }
211 
212 			uint32				EventMask() const
213 									{ return fEventMask; }
214 
215 			uint32				EventOptions() const
216 									{ return fEventOptions; }
217 
218 	inline	void				QuietlySetEventMask(uint32 em)
219 									{ fEventMask = em; }
220 
221 	inline	void				QuietlySetEventOptions(uint32 eo)
222 									{ fEventOptions = eo; }
223 
224 			void				PruneTree();
225 
226 	// debugging
227 			void				PrintToStream();
228 			void				PrintNode();
229 			void				PrintTree();
230 
231 	// server "private" - should not be used
232 			void				SetAsTopLayer(bool option)
233 									{ fIsTopLayer = option; }
234 
235 	inline	bool				IsTopLayer() const
236 									{ return fIsTopLayer; }
237 
238 			BRegion*			ClippingRegion() const
239 									{ return fClipReg; }
240 
241 								// automatic background blanking by app_server
242 			void				SetViewColor(const RGBColor& color);
243 	inline	const RGBColor&		ViewColor() const
244 									{ return fViewColor; }
245 
246 			void				SetBackgroundBitmap(const ServerBitmap* bitmap);
247 	inline	const ServerBitmap*	BackgroundBitmap() const
248 									{ return fBackgroundBitmap; }
249 
250 								// overlay support
251 								// TODO: This can't be all, what about color key?
252 			void				SetOverlayBitmap(const ServerBitmap* bitmap);
253 	inline	const ServerBitmap*	OverlayBitmap() const
254 									{ return fOverlayBitmap; }
255 
256 			void				CopyBits(BRect& src, BRect& dst,
257 										 int32 xOffset, int32 yOffset);
258 #ifndef NEW_CLIPPING
259 	inline	const BRegion&		VisibleRegion() const { return fVisible; }
260 	inline	const BRegion&		FullVisible() const { return fFullVisible; }
261 
262 #else
263 	inline	const BRegion&		VisibleRegion() const { return fVisible2; }
264 	inline	const BRegion&		FullVisible() const { return fFullVisible2; }
265 
266 	virtual	void				GetWantedRegion(BRegion& reg) const;
267 
268 	virtual	void				MovedByHook(float dx, float dy);
269 	virtual	void				ResizedByHook(float dx, float dy, bool automatic);
270 	virtual	void				ScrolledByHook(float dx, float dy);
271 
272 			void				ConvertToParent2(BPoint* pt) const;
273 			void				ConvertToParent2(BRect* rect) const;
274 			void				ConvertToParent2(BRegion* reg) const;
275 			void				ConvertFromParent2(BPoint* pt) const;
276 			void				ConvertFromParent2(BRect* rect) const;
277 			void				ConvertFromParent2(BRegion* reg) const;
278 
279 			void				ConvertToScreen2(BPoint* pt) const;
280 			void				ConvertToScreen2(BRect* rect) const;
281 			void				ConvertToScreen2(BRegion* reg) const;
282 			void				ConvertFromScreen2(BPoint* pt) const;
283 			void				ConvertFromScreen2(BRect* rect) const;
284 			void				ConvertFromScreen2(BRegion* reg) const;
285 			bool				IsVisuallyHidden() const;
286  private:
287  			void				do_Hide();
288  			void				do_Show();
289 			void				do_RebuildVisibleRegions(const BRegion &invalid,
290 														const Layer *startFrom);
291 			void				do_MoveBy(float dx, float dy);
292 			void				do_ResizeBy(float dx, float dy);
293 			void				do_ScrollBy(float dx, float dy);
294 
295 			void				do_Invalidate(	const BRegion &invalid,
296 												const Layer *startFrom = NULL);
297 
298 			void				do_Redraw(	const BRegion &invalid,
299 											const Layer *startFrom = NULL);
300 
301 			void				rebuild_visible_regions(const BRegion &invalid,
302 													const BRegion &parentLocalVisible,
303 													const Layer *startFrom);
304 
305 	virtual	void				_ReserveRegions(BRegion &reg);
306 	virtual	void				_GetWantedRegion(BRegion &reg);
307 
308 			void				clear_visible_regions();
309 			void				resize_layer_frame_by(float x, float y);
310 			void				rezize_layer_redraw_more(BRegion &reg, float dx, float dy);
311 			void				resize_layer_full_update_on_resize(BRegion &reg, float dx, float dy);
312 
313 #endif
314  private:
315 			void				do_CopyBits(BRect& src, BRect& dst,
316 											int32 xOffset, int32 yOffset);
317 
318  protected:
319 	friend class RootLayer;
320 	friend class WinBorder;
321 	friend class ServerWindow;
322 
323 #ifndef NEW_CLIPPING
324 			void				move_layer(float x, float y);
325 			void				resize_layer(float x, float y);
326 
327 			void				FullInvalidate(const BRect& rect);
328 			void				FullInvalidate(const BRegion& region);
329 			void				Invalidate(const BRegion& region);
330 #endif
331 
332 			BRect				fFrame;
333 // TODO: should be removed or reused in a similar fashion
334 // to hold the accumulated origins from the graphics state stack.
335 // The same needs to be done for "scale". (Keeping an accumulated
336 // value.)
337 //			BPoint				fBoundsLeftTop;
338 			WinBorder*			fOwner;
339 
340 			Layer*				fParent;
341 			Layer*				fPreviousSibling;
342 			Layer*				fNextSibling;
343 			Layer*				fFirstChild;
344 			Layer*				fLastChild;
345 
346 	mutable	Layer*				fCurrent;
347 
348 #ifndef NEW_CLIPPING
349 			BRegion				fVisible;
350 			BRegion				fFullVisible;
351 			BRegion				fFull;
352 			int8				fFrameAction;
353 #else
354  private:
355 			BRegion				fVisible2;
356 			BRegion				fFullVisible2;
357  protected:
358 #endif
359 			BRegion*			fClipReg;
360 
361 			ServerWindow*		fServerWin;
362 			BString				fName;
363 			int32				fViewToken;
364 			uint32				fFlags;
365 			uint32				fResizeMode;
366 			uint32				fEventMask;
367 			uint32				fEventOptions;
368 			bool				fHidden;
369 			bool				fIsTopLayer;
370 			uint16				fAdFlags;
371 
372 			DisplayDriver*		fDriver;
373 			DrawState*			fDrawState;
374 
375 			RootLayer*			fRootLayer;
376 
377  private:
378 			void				RequestDraw(const BRegion& reg,
379 											Layer* startFrom);
380 			ServerWindow*		SearchForServerWindow();
381 
382 			status_t			SendUpdateMsg(BRegion& reg);
383 			void				AddToViewsWithInvalidCoords() const;
384 			void				SendViewCoordUpdateMsg() const;
385 
386 			RGBColor			fViewColor;
387 
388 	const	ServerBitmap*		fBackgroundBitmap;
389 	const	ServerBitmap*		fOverlayBitmap;
390 
391 };
392 
393 #endif // _LAYER_H_
394