1 // MouseTrackingHelpers.h 2 // e.moon 8mar99 3 // 4 // Some helper classes for mouse tracking, designed to make 5 // it easier to delegate drag operations to a parent class 6 // (especially useful for pane-separator bars & other operations 7 // in which a view is moved or resized as the user drags 8 // within it.) 9 // 10 // HISTORY 11 // e.moon 11may99: fixed m_bTracking bug (not init'd in ctor) 12 // fixed mouseTrackingBegin() documentation 13 // (point is in view, not screen, coords) 14 15 #ifndef __MouseTrackingHelpers_H__ 16 #define __MouseTrackingHelpers_H__ 17 18 #include <View.h> 19 20 #include "cortex_defs.h" 21 __BEGIN_CORTEX_NAMESPACE 22 23 class MouseTrackingSourceView; 24 25 // interface for a mouse-tracking destination 26 class IMouseTrackingDestination { 27 public: // operations 28 IMouseTrackingDestination() { } 29 virtual ~IMouseTrackingDestination() { } 30 // a MouseTrackingSourceView has started tracking the mouse 31 // (point is in pSource view coordinates) 32 virtual void mouseTrackingBegin( 33 MouseTrackingSourceView* pSource, 34 uint32 buttons, 35 BPoint point)=0; 36 37 // mouse-tracking update from child view 38 // (point is in screen coordinates) 39 virtual void mouseTrackingUpdate( 40 uint32 buttons, 41 float xDelta, 42 float yDelta, 43 BPoint point)=0; 44 45 // mouse-tracking done 46 virtual void mouseTrackingEnd()=0; 47 }; 48 49 // mouse-tracking 'source' view 50 // hands off to a destination view (by default its immediate 51 // parent) 52 53 class MouseTrackingSourceView : public BView { 54 typedef BView _inherited; 55 56 public: // types & constants 57 // these bits determine which mouse axes will be tracked 58 // (ie. if TRACK_HORIZONTAL isn't set, mouseTrackUpdate() 59 // will always be called with xDelta == 0.0) 60 enum mouse_tracking_flag_t { 61 TRACK_HORIZONTAL = 1, 62 TRACK_VERTICAL = 2 63 }; 64 65 public: // ctor/dtor 66 MouseTrackingSourceView( 67 BRect frame, 68 const char* pName, 69 uint32 resizeMode=B_FOLLOW_LEFT|B_FOLLOW_TOP, 70 uint32 flags=B_WILL_DRAW|B_FRAME_EVENTS, 71 uint32 trackingFlags=TRACK_HORIZONTAL|TRACK_VERTICAL); 72 73 ~MouseTrackingSourceView(); 74 75 bool isTracking() const { return m_bTracking; } 76 77 // get mouse-down point in screen coordinates; returns 78 // B_OK on success, or B_ERROR if no longer tracking 79 // the mouse. 80 status_t getTrackingOrigin(BPoint* poPoint) const; 81 82 // fetch/set the destination handler 83 // (if not yet attached to a window, or if no parent 84 // implements IMouseTrackDestination, trackingDestination() 85 // returns 0) 86 // 87 // setting the destination handler isn't allowed if a tracking 88 // operation is currently in progress 89 90 IMouseTrackingDestination* trackingDestination() const { 91 return m_pDest; 92 } 93 status_t setTrackingDestination(IMouseTrackingDestination* pDest); 94 95 public: // BView impl. 96 97 // handle mouse events 98 virtual void MouseDown(BPoint point); 99 virtual void MouseMoved(BPoint point, uint32 transit, 100 const BMessage* pMsg); 101 virtual void MouseUp(BPoint point); 102 103 // look for a default destination 104 virtual void AttachedToWindow(); 105 106 /* 107 // track current frame rectangle 108 virtual void FrameResized(float width, float height); 109 */ 110 protected: // members 111 112 IMouseTrackingDestination* m_pDest; 113 uint32 m_trackingFlags; 114 115 // mouse-tracking state 116 bool m_bTracking; 117 BPoint m_prevPoint; // in screen coords 118 BPoint m_initPoint; // in screen coords 119 120 // BRect m_prevFrame; 121 }; 122 123 __END_CORTEX_NAMESPACE 124 #endif /* __MouseTrackingHelpers_H__ */ 125