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