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