xref: /haiku/src/apps/cortex/support/MouseTrackingHelpers.cpp (revision 0c93c0a807b27096abbfad677436afb7d1712d4a)
1 // MouseTrackingHelpers.cpp
2 // e.moon 8mar99
3 
4 #include "MouseTrackingHelpers.h"
5 
6 #include "debug_tools.h"
7 
8 __USE_CORTEX_NAMESPACE
9 
10 // ---------------------------------------------------------------- //
11 // ctor/dtor
12 // ---------------------------------------------------------------- //
13 
14 MouseTrackingSourceView::MouseTrackingSourceView(
15 	BRect frame,
16 	const char* pName,
17 	uint32 resizeMode,
18 	uint32 flags,
19 	uint32 trackingFlags) :
20 
21 	BView(frame, pName, resizeMode, flags),
22 	m_trackingFlags(trackingFlags),
23 	m_bTracking(false),
24 	m_pDest(0) {
25 
26 //	FrameResized(frame.Width(), frame.Height());
27 }
28 
29 MouseTrackingSourceView::~MouseTrackingSourceView() {}
30 
31 // get mouse-down point in screen coordinates; returns
32 // B_OK on success, or B_ERROR if no longer tracking
33 // the mouse.
34 status_t MouseTrackingSourceView::getTrackingOrigin(
35 	BPoint* poPoint) const {
36 	if(!m_bTracking)
37 		return B_ERROR;
38 	*poPoint = m_initPoint;
39 	return B_OK;
40 }
41 
42 // fetch/set the destination handler
43 
44 status_t MouseTrackingSourceView::setTrackingDestination(
45 	IMouseTrackingDestination* pDest) {
46 	if(m_bTracking)
47 		return B_ERROR;
48 
49 	m_pDest = pDest;
50 	return B_OK;
51 }
52 
53 // ---------------------------------------------------------------- //
54 // BView impl.
55 // ---------------------------------------------------------------- //
56 
57 // handle mouse events
58 void MouseTrackingSourceView::MouseDown(BPoint point) {
59 	if(!m_trackingFlags)
60 		return;
61 
62 	// get mouse state & initial point
63 	uint32 buttons;
64 	GetMouse(&point, &buttons);
65 	m_prevPoint = ConvertToScreen(point);
66 	m_initPoint = m_prevPoint;
67 
68 	// start tracking the mouse
69 	SetMouseEventMask(B_POINTER_EVENTS,
70 		B_LOCK_WINDOW_FOCUS|B_NO_POINTER_HISTORY);
71 	m_bTracking = true;
72 
73 	// notify destination
74 	if(m_pDest)
75 		m_pDest->mouseTrackingBegin(this, buttons, point);
76 }
77 
78 void MouseTrackingSourceView::MouseMoved(BPoint point, uint32 transit,
79 	const BMessage* pMsg) {
80 
81 	if(m_bTracking) {
82 
83 		// mouse-tracking update: figure number of pixels moved
84 		// (along the axes I care about)
85 		uint32 buttons;
86 		GetMouse(&point, &buttons, false);
87 		ConvertToScreen(&point);
88 
89 		if(point == m_prevPoint) // no motion?
90 			return;
91 
92 		float xDelta = m_trackingFlags & TRACK_HORIZONTAL ?
93 			point.x - m_prevPoint.x : 0.0;
94 		float yDelta = m_trackingFlags & TRACK_VERTICAL ?
95 			point.y - m_prevPoint.y : 0.0;
96 
97 		// pass info to destination view
98 		if(m_pDest)
99 			m_pDest->mouseTrackingUpdate(buttons, xDelta, yDelta, point);
100 
101 		// store point for future delta calculations
102 		m_prevPoint = point;
103 	}
104 }
105 
106 void MouseTrackingSourceView::MouseUp(BPoint point) {
107 	if(m_bTracking) {
108 //		PRINT(( "MouseTrackingSourceView::MouseUp()\n"));
109 
110 		// +++++ handle final update
111 
112 		// clean up
113 		m_bTracking = false;
114 		if(m_pDest)
115 			m_pDest->mouseTrackingEnd();
116 	}
117 }
118 
119 // look for a default destination
120 void MouseTrackingSourceView::AttachedToWindow() {
121 	if(m_pDest) // already have a destination
122 		return;
123 
124 	for(BView* pParent = Parent(); pParent; pParent = pParent->Parent()) {
125 		IMouseTrackingDestination* pFound =
126 			dynamic_cast<IMouseTrackingDestination*>(pParent);
127 		if(pFound) // found a valid destination
128 			m_pDest = pFound;
129 	}
130 }
131 
132 /*
133 // track current frame rectangle
134 void MouseTrackingSourceView::FrameResized(float width, float height) {
135 	_inherited::FrameResized(width, height);
136 	m_prevFrame = Frame();
137 
138 	// +++++ adjust if currently tracking?
139 }
140 */
141 
142 // END -- MouseTrackingHelpers.cpp --
143 
144