xref: /haiku/src/apps/cortex/support/MouseTrackingHelpers.cpp (revision d3571a6f55195ced06c2f48905d7c86b5408c13e)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // MouseTrackingHelpers.cpp
33 // e.moon 8mar99
34 
35 #include "MouseTrackingHelpers.h"
36 
37 __USE_CORTEX_NAMESPACE
38 
39 
MouseTrackingSourceView(BRect frame,const char * name,uint32 resizeMode,uint32 flags,uint32 trackingFlags)40 MouseTrackingSourceView::MouseTrackingSourceView(BRect frame, const char* name,
41 		uint32 resizeMode, uint32 flags, uint32 trackingFlags)
42 	: BView(frame, name, resizeMode, flags),
43 	m_pDest(0),
44 	m_trackingFlags(trackingFlags),
45 	m_bTracking(false)
46 {
47 	//FrameResized(frame.Width(), frame.Height());
48 }
49 
50 
~MouseTrackingSourceView()51 MouseTrackingSourceView::~MouseTrackingSourceView()
52 {
53 }
54 
55 
56 // get mouse-down point in screen coordinates; returns
57 // B_OK on success, or B_ERROR if no longer tracking
58 // the mouse.
getTrackingOrigin(BPoint * poPoint) const59 status_t MouseTrackingSourceView::getTrackingOrigin(
60 	BPoint* poPoint) const {
61 	if(!m_bTracking)
62 		return B_ERROR;
63 	*poPoint = m_initPoint;
64 	return B_OK;
65 }
66 
67 // fetch/set the destination handler
68 
setTrackingDestination(IMouseTrackingDestination * pDest)69 status_t MouseTrackingSourceView::setTrackingDestination(
70 	IMouseTrackingDestination* pDest) {
71 	if(m_bTracking)
72 		return B_ERROR;
73 
74 	m_pDest = pDest;
75 	return B_OK;
76 }
77 
78 // ---------------------------------------------------------------- //
79 // BView impl.
80 // ---------------------------------------------------------------- //
81 
82 // handle mouse events
MouseDown(BPoint point)83 void MouseTrackingSourceView::MouseDown(BPoint point) {
84 	if(!m_trackingFlags)
85 		return;
86 
87 	// get mouse state & initial point
88 	uint32 buttons;
89 	GetMouse(&point, &buttons);
90 	m_prevPoint = ConvertToScreen(point);
91 	m_initPoint = m_prevPoint;
92 
93 	// start tracking the mouse
94 	SetMouseEventMask(B_POINTER_EVENTS,
95 		B_LOCK_WINDOW_FOCUS|B_NO_POINTER_HISTORY);
96 	m_bTracking = true;
97 
98 	// notify destination
99 	if(m_pDest)
100 		m_pDest->mouseTrackingBegin(this, buttons, point);
101 }
102 
MouseMoved(BPoint point,uint32 transit,const BMessage * pMsg)103 void MouseTrackingSourceView::MouseMoved(BPoint point, uint32 transit,
104 	const BMessage* pMsg) {
105 
106 	if(m_bTracking) {
107 
108 		// mouse-tracking update: figure number of pixels moved
109 		// (along the axes I care about)
110 		uint32 buttons;
111 		GetMouse(&point, &buttons, false);
112 		ConvertToScreen(&point);
113 
114 		if(point == m_prevPoint) // no motion?
115 			return;
116 
117 		float xDelta = m_trackingFlags & TRACK_HORIZONTAL ?
118 			point.x - m_prevPoint.x : 0.0;
119 		float yDelta = m_trackingFlags & TRACK_VERTICAL ?
120 			point.y - m_prevPoint.y : 0.0;
121 
122 		// pass info to destination view
123 		if(m_pDest)
124 			m_pDest->mouseTrackingUpdate(buttons, xDelta, yDelta, point);
125 
126 		// store point for future delta calculations
127 		m_prevPoint = point;
128 	}
129 }
130 
MouseUp(BPoint point)131 void MouseTrackingSourceView::MouseUp(BPoint point) {
132 	if(m_bTracking) {
133 //		PRINT(( "MouseTrackingSourceView::MouseUp()\n"));
134 
135 		// +++++ handle final update
136 
137 		// clean up
138 		m_bTracking = false;
139 		if(m_pDest)
140 			m_pDest->mouseTrackingEnd();
141 	}
142 }
143 
144 // look for a default destination
AttachedToWindow()145 void MouseTrackingSourceView::AttachedToWindow() {
146 	if(m_pDest) // already have a destination
147 		return;
148 
149 	for(BView* pParent = Parent(); pParent; pParent = pParent->Parent()) {
150 		IMouseTrackingDestination* pFound =
151 			dynamic_cast<IMouseTrackingDestination*>(pParent);
152 		if(pFound) // found a valid destination
153 			m_pDest = pFound;
154 	}
155 }
156 
157 /*
158 // track current frame rectangle
159 void MouseTrackingSourceView::FrameResized(float width, float height) {
160 	_inherited::FrameResized(width, height);
161 	m_prevFrame = Frame();
162 
163 	// +++++ adjust if currently tracking?
164 }
165 */
166 
167 // END -- MouseTrackingHelpers.cpp --
168 
169