1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkView_DEFINED 11 #define SkView_DEFINED 12 13 #include "SkEventSink.h" 14 #include "SkRect.h" 15 #include "SkMatrix.h" 16 #include "SkMetaData.h" 17 18 class SkCanvas; 19 20 /** \class SkView 21 22 SkView is the base class for screen management. All widgets and controls inherit 23 from SkView. 24 */ 25 class SkView : public SkEventSink { 26 public: 27 enum Flag_Shift { 28 kVisible_Shift, 29 kNoClip_Shift, 30 31 kFlagShiftCount 32 }; 33 enum Flag_Mask { 34 kVisible_Mask = 1 << kVisible_Shift, //!< set if the view is visible 35 kNoClip_Mask = 1 << kNoClip_Shift, //!< set if the view is not clipped to its bounds 36 37 kAllFlagMasks = (uint32_t)(0 - 1) >> (32 - kFlagShiftCount) 38 }; 39 40 SkView(uint32_t flags = 0); 41 virtual ~SkView(); 42 43 /** Return the flags associated with the view 44 */ getFlags()45 uint32_t getFlags() const { return fFlags; } 46 /** Set the flags associated with the view 47 */ 48 void setFlags(uint32_t flags); 49 50 /** Helper that returns non-zero if the kVisible_Mask bit is set in the view's flags 51 */ isVisible()52 int isVisible() const { return fFlags & kVisible_Mask; } isClipToBounds()53 int isClipToBounds() const { return !(fFlags & kNoClip_Mask); } 54 /** Helper to set/clear the view's kVisible_Mask flag */ 55 void setVisibleP(bool); 56 void setClipToBounds(bool); 57 58 /** Return the view's width */ width()59 SkScalar width() const { return fWidth; } 60 /** Return the view's height */ height()61 SkScalar height() const { return fHeight; } 62 /** Set the view's width and height. These must both be >= 0. This does not affect the view's loc */ 63 void setSize(SkScalar width, SkScalar height); setSize(const SkPoint & size)64 void setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); } setWidth(SkScalar width)65 void setWidth(SkScalar width) { this->setSize(width, fHeight); } setHeight(SkScalar height)66 void setHeight(SkScalar height) { this->setSize(fWidth, height); } 67 68 /** Call this to have the view draw into the specified canvas. */ 69 virtual void draw(SkCanvas* canvas); 70 71 // Click handling 72 73 class Click { 74 public: 75 Click(SkView* target); 76 virtual ~Click(); 77 78 enum State { 79 kDown_State, 80 kMoved_State, 81 kUp_State 82 }; 83 SkPoint fOrig, fPrev, fCurr; 84 SkIPoint fIOrig, fIPrev, fICurr; 85 State fState; 86 unsigned fModifierKeys; 87 88 SkMetaData fMeta; 89 private: 90 SkEventSinkID fTargetID; 91 92 friend class SkView; 93 }; 94 Click* findClickHandler(SkScalar x, SkScalar y, unsigned modifierKeys); 95 96 static void DoClickDown(Click*, int x, int y, unsigned modi); 97 static void DoClickMoved(Click*, int x, int y, unsigned modi); 98 static void DoClickUp(Click*, int x, int y, unsigned modi); 99 100 protected: 101 /** Override this to draw inside the view. Be sure to call the inherited version too */ 102 virtual void onDraw(SkCanvas*); 103 /** Override this to be notified when the view's size changes. Be sure to call the inherited version too */ 104 virtual void onSizeChange(); 105 106 /** Override this if you might handle the click 107 */ 108 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi); 109 /** Override this to track clicks, returning true as long as you want to track 110 the pen/mouse. 111 */ 112 virtual bool onClick(Click*); 113 114 private: 115 SkScalar fWidth, fHeight; 116 uint8_t fFlags; 117 }; 118 119 #endif 120