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