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