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