xref: /haiku/src/servers/app/Layer.h (revision 8d8f5950d0c28592beaef5196091ef2a5ce92901)
1 /*
2  * Copyright (c) 2001-2005, 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  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 #ifndef _LAYER_H_
11 #define _LAYER_H_
12 
13 #include <GraphicsDefs.h>
14 #include <List.h>
15 #include <Locker.h>
16 #include <OS.h>
17 #include <String.h>
18 #include <Rect.h>
19 #include <Region.h>
20 
21 #include "RGBColor.h"
22 #include "ServerWindow.h"
23 
24 enum {
25 	B_LAYER_NONE		= 1,
26 	B_LAYER_MOVE		= 2,
27 	B_LAYER_SIMPLE_MOVE	= 3,
28 	B_LAYER_RESIZE		= 4,
29 	B_LAYER_MASK_RESIZE	= 5,
30 };
31 
32 enum {
33 	B_LAYER_ACTION_NONE = 0,
34 	B_LAYER_ACTION_MOVE,
35 	B_LAYER_ACTION_RESIZE
36 };
37 
38 enum {
39 	B_LAYER_CHILDREN_DEPENDANT = 0x1000U,
40 };
41 
42 class ServerApp;
43 class RootLayer;
44 class DrawingEngine;
45 class DrawState;
46 class ServerBitmap;
47 
48 class PointerEvent {
49  public:
50 	int32		code;		// B_MOUSE_UP, B_MOUSE_DOWN, B_MOUSE_MOVED,
51 							// B_MOUSE_WHEEL_CHANGED
52 	bigtime_t	when;
53 	BPoint		where;
54 	float		wheel_delta_x;
55 	float		wheel_delta_y;
56 	int32		modifiers;
57 	int32		buttons;	// B_PRIMARY_MOUSE_BUTTON, B_SECONDARY_MOUSE_BUTTON
58 							// B_TERTIARY_MOUSE_BUTTON
59 	int32		clicks;
60 };
61 
62 class Layer {
63  public:
64 								Layer(BRect frame, const char* name,
65 									  int32 token, uint32 resize,
66 									  uint32 flags, DrawingEngine* driver);
67 	virtual						~Layer();
68 
69 	// name
70 	virtual	void				SetName(const char* name);
71 	inline	const char*			Name() const
72 									{ return fName.String(); }
73 
74 	// children handling
75 			void				AddChild(Layer* child, ServerWindow* serverWin);
76 			void				RemoveChild(Layer* child);
77 			void				RemoveSelf();
78 			bool				HasChild(Layer* layer);
79 			Layer*				Parent() const
80 									{ return fParent; }
81 
82 			uint32				CountChildren() const;
83 			Layer*				FindLayer(const int32 token);
84 			Layer*				LayerAt(const BPoint &pt, bool recursive = true);
85 
86 	virtual	Layer*				FirstChild() const;
87 	virtual	Layer*				NextChild() const;
88 	virtual	Layer*				PreviousChild() const;
89 	virtual	Layer*				LastChild() const;
90 
91 			void				SetAsTopLayer(bool option)
92 									{ fIsTopLayer = option; }
93 	inline	bool				IsTopLayer() const
94 									{ return fIsTopLayer; }
95 
96 	// visible state
97 			void				Show(bool invalidate = true);
98 			void				Hide(bool invalidate = true);
99 			bool				IsHidden() const;
100 			bool				IsVisuallyHidden() const;
101 
102 	// graphic state and attributes
103 			void				PushState();
104 			void				PopState();
105 			DrawState*			CurrentState() const { return fDrawState; }
106 
107 			void				SetViewColor(const RGBColor& color);
108 	inline	const RGBColor&		ViewColor() const
109 									{ return fViewColor; }
110 			void				SetBackgroundBitmap(const ServerBitmap* bitmap);
111 	inline	const ServerBitmap*	BackgroundBitmap() const
112 									{ return fBackgroundBitmap; }
113 			void				SetOverlayBitmap(const ServerBitmap* bitmap);
114 	inline	const ServerBitmap*	OverlayBitmap() const
115 									{ return fOverlayBitmap; }
116 
117 	// coordinate system
118 			BRect				Bounds() const;
119 			BRect				Frame() const;
120 			BPoint				BoundsOrigin() const; // BoundsFrameDiff()?
121 			float				Scale() const;
122 
123 			void				ConvertToParent(BPoint* pt) const;
124 			void				ConvertToParent(BRect* rect) const;
125 			void				ConvertToParent(BRegion* reg) const;
126 			void				ConvertFromParent(BPoint* pt) const;
127 			void				ConvertFromParent(BRect* rect) const;
128 			void				ConvertFromParent(BRegion* reg) const;
129 
130 			void				ConvertToScreen(BPoint* pt) const;
131 			void				ConvertToScreen(BRect* rect) const;
132 			void				ConvertToScreen(BRegion* reg) const;
133 			void				ConvertFromScreen(BPoint* pt) const;
134 			void				ConvertFromScreen(BRect* rect) const;
135 			void				ConvertFromScreen(BRegion* reg) const;
136 
137 	virtual	void				MoveBy(float x, float y);
138 	virtual	void				ResizeBy(float x, float y);
139 	virtual	void				ScrollBy(float x, float y);
140 			void				CopyBits(BRect& src, BRect& dst,
141 										 int32 xOffset, int32 yOffset);
142 
143 	// input handling
144 	virtual	void				MouseDown(const BMessage *msg);
145 	virtual	void				MouseUp(const BMessage *msg);
146 	virtual	void				MouseMoved(const BMessage *msg);
147 	virtual	void				MouseWheelChanged(const BMessage *msg);
148 
149 	virtual	void				KeyDown(const BMessage *msg);
150 	virtual	void				KeyUp(const BMessage *msg);
151 	virtual	void				UnmappedKeyDown(const BMessage *msg);
152 	virtual	void				UnmappedKeyUp(const BMessage *msg);
153 	virtual	void				ModifiersChanged(const BMessage *msg);
154 
155 	virtual	void				WorkspaceActivated(int32 index, bool active);
156 	virtual	void				WorkspacesChanged(uint32 oldWorkspaces, uint32 newWorkspaces);
157 	virtual	void				Activated(bool active);
158 
159 	// action hooks
160 	virtual	void				MovedByHook(float dx, float dy);
161 	virtual	void				ResizedByHook(float dx, float dy, bool automatic);
162 	virtual	void				ScrolledByHook(float dx, float dy);
163 
164 	// app_server objects getters
165 			ServerWindow*		Window() const
166 									{ return fServerWin; }
167 			ServerApp*			App() const
168 									{ return fServerWin? fServerWin->App(): NULL; }
169 	inline	WinBorder*			Owner() const
170 									{ return fOwner; }
171 			RootLayer*			GetRootLayer() const
172 									{ return fRootLayer; }
173 			void				SetRootLayer(RootLayer* rl)
174 									{ fRootLayer = rl; }
175 			DrawingEngine*		GetDrawingEngine() const
176 									{ return fDriver; }
177 
178 	// flags
179 			uint32				EventMask() const
180 									{ return fEventMask; }
181 			uint32				EventOptions() const
182 									{ return fEventOptions; }
183 	inline	void				QuietlySetEventMask(uint32 em)
184 									{ fEventMask = em; }
185 	inline	void				QuietlySetEventOptions(uint32 eo)
186 									{ fEventOptions = eo; }
187 	inline	uint32				ResizeMode() const
188 									{ return fResizeMode; }
189 	inline	uint32				Flags() const
190 									{ return fFlags; }
191 			void				SetFlags(uint32 flags);
192 
193 	// clipping stuff and redraw
194 			void				SetUserClipping(const BRegion& region);
195 				// region is expected in layer coordinates
196 
197 	inline	const BRegion&		VisibleRegion() const { return fVisible; }
198 	inline	const BRegion&		FullVisible() const { return fFullVisible; }
199 	inline	const BRegion&		DrawingRegion() const { return fDrawingRegion; }
200 
201 	virtual	void				GetOnScreenRegion(BRegion& region);
202 
203 			void				MarkForRebuild(const BRegion &dirty);
204 			void				TriggerRebuild();
205 			void				_GetAllRebuildDirty(BRegion *totalReg);
206 
207 	virtual	void				Draw(const BRect& r);
208 			void				_AllRedraw(const BRegion &invalid);
209 
210  private:
211 	friend class RootLayer;
212 	friend class WinBorder;
213 	friend class ServerWindow;
214 
215  			void				do_Hide();
216  			void				do_Show();
217 			void				do_CopyBits(BRect& src, BRect& dst,
218 											int32 xOffset, int32 yOffset);
219 
220 	// private clipping stuff
221 	virtual	void				_ReserveRegions(BRegion &reg);
222 			void				_RebuildVisibleRegions( const BRegion &invalid,
223 														const BRegion &parentLocalVisible,
224 														const Layer *startFrom);
225 			void				_RebuildDrawingRegion();
226 			void				_ClearVisibleRegions();
227 			void				_ResizeLayerFrameBy(float x, float y);
228 			void				_RezizeLayerRedrawMore(BRegion &reg, float dx, float dy);
229 			void				_ResizeLayerFullUpdateOnResize(BRegion &reg, float dx, float dy);
230 
231 	// for updating client-side coordinates
232 			void				AddToViewsWithInvalidCoords() const;
233 			void				SendViewCoordUpdateMsg() const;
234 
235 
236 			BString				fName;
237 			BRect				fFrame;
238 // TODO: should be removed or reused in a similar fashion
239 // to hold the accumulated origins from the graphics state stack.
240 // The same needs to be done for "scale". (Keeping an accumulated
241 // value.)
242 //			BPoint				fBoundsLeftTop;
243 
244 			BRegion				fVisible;
245 			BRegion				fFullVisible;
246 			BRegion				fDirtyForRebuild;
247 			BRegion				fDrawingRegion;
248 
249 			DrawingEngine*		fDriver;
250 			RootLayer*			fRootLayer;
251 			ServerWindow*		fServerWin;
252 			WinBorder*			fOwner;
253 
254 			DrawState*			fDrawState;
255 
256 			Layer*				fParent;
257 			Layer*				fPreviousSibling;
258 			Layer*				fNextSibling;
259 			Layer*				fFirstChild;
260 			Layer*				fLastChild;
261 
262 	mutable	Layer*				fCurrent;
263 
264 			int32				fViewToken;
265 			uint32				fFlags;
266 			uint16				fAdFlags;
267 			uint32				fResizeMode;
268 			uint32				fEventMask;
269 			uint32				fEventOptions;
270 
271 			bool				fHidden;
272 			bool				fIsTopLayer;
273 			RGBColor			fViewColor;
274 
275 	const	ServerBitmap*		fBackgroundBitmap;
276 	const	ServerBitmap*		fOverlayBitmap;
277 
278 };
279 
280 #endif // _LAYER_H_
281