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// DiagramView.h (Cortex/DiagramView)
33//
34// * PURPOSE
35//   BView and DiagramItemGroup derived class providing the
36//   one and only drawing context all child DiagramItems will
37//   use.
38//
39// * HISTORY
40//   c.lenz		25sep99		Begun
41//
42
43#ifndef __DiagramView_H__
44#define __DiagramView_H__
45
46#include "DiagramItemGroup.h"
47
48#include <Region.h>
49#include <View.h>
50
51//class BBitmap;
52
53#include "cortex_defs.h"
54__BEGIN_CORTEX_NAMESPACE
55
56//class DiagramBox;
57class DiagramWire;
58class DiagramEndPoint;
59
60class DiagramView : public BView,
61					public DiagramItemGroup
62{
63
64public:				// *** ctor/dtor
65
66						DiagramView(
67							BRect frame,
68							const char *name,
69							bool multiSelection,
70							uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
71							uint32 flags = B_WILL_DRAW);
72
73	virtual				~DiagramView();
74
75public:					// *** hook functions
76
77	// is called from MessageReceived() if a wire has been dropped
78	// on the background or on an incompatible endpoint
79	virtual void		connectionAborted(
80							DiagramEndPoint *fromWhich)
81						{ /* does nothing */ }
82
83	// is called from MessageReceived() if an endpoint has accepted
84	// a wire-drop (i.e. connection)
85	virtual void		connectionEstablished(
86							DiagramEndPoint *fromWhich,
87							DiagramEndPoint *toWhich)
88						{ /* does nothing */ }
89
90	// must be implemented to return an instance of the DiagramWire
91	// derived class
92	virtual DiagramWire *createWire(
93							DiagramEndPoint *fromWhich,
94							DiagramEndPoint *woWhich) = 0;
95
96	// must be implemented to return an instance of the DiagramWire
97	// derived class; this version is for temporary wires used in
98	// drag & drop connecting
99	virtual DiagramWire *createWire(
100							DiagramEndPoint *fromWhich) = 0;
101
102	// hook called from BackgroundMouseDown() if the background was hit
103	virtual void		BackgroundMouseDown(
104							BPoint point,
105							uint32 buttons,
106							uint32 clicks)
107						{ /* does nothing */ }
108
109	// hook called from MouseMoved() if the mouse is floating over
110	// the background (i.e. with no message attached)
111	virtual void		MouseOver(
112							BPoint point,
113							uint32 transit)
114						{ /* does nothing */ }
115
116	// hook called from MouseMoved() if a message is being dragged
117	// over the background
118	virtual void		MessageDragged(
119							BPoint point,
120							uint32 transit,
121							const BMessage *message);
122
123	// hook called from MessageReceived() if a message has been
124	// dropped over the background
125	virtual void		MessageDropped(
126							BPoint point,
127							BMessage *message);
128
129public:					// derived from BView
130
131	// initial scrollbar update [e.moon 16nov99]
132	virtual void		AttachedToWindow();
133
134	// draw the background and all items
135	virtual void		Draw(
136							BRect updateRect);
137
138	// updates the scrollbars
139	virtual void		FrameResized(
140							float width,
141							float height);
142
143	// return data rect [c.lenz 1mar2000]
144	virtual void		GetPreferredSize(
145							float *width,
146							float *height);
147
148	// handles the messages M_SELECTION_CHANGED and M_WIRE_DROPPED
149	// and passes a dropped message on to the item it was dropped on
150	virtual void		MessageReceived(
151							BMessage *message);
152
153	// handles the arrow keys for moving DiagramBoxes
154	virtual void		KeyDown(
155							const char *bytes,
156							int32 numBytes);
157
158	// if an item is located at the click point, this function calls
159	// that items MouseDown() method; else a deselectAll() command is
160	// made and rect-tracking is initiated
161	virtual void		MouseDown(
162							BPoint point);
163
164	// if an item is located under the given point, this function
165	// calls that items MessageDragged() hook if a message is being
166	// dragged, and MouseOver() if not
167	virtual void		MouseMoved(
168							BPoint point,
169							uint32 transit,
170							const BMessage *message);
171
172	// ends rect-tracking and wire-tracking
173	virtual void		MouseUp(
174							BPoint point);
175
176public:					// *** derived from DiagramItemGroup
177
178	// extends the DiagramItemGroup implementation by setting
179	// the items owner and calling the attachedToDiagram() hook
180	// on it
181	virtual bool		AddItem(
182							DiagramItem *item);
183
184	// extends the DiagramItemGroup implementation by calling
185	// the detachedToDiagram() hook on the item
186	virtual bool		RemoveItem(
187							DiagramItem *item);
188
189public:					// *** operations
190
191	// update the temporary wire to follow the mouse cursor
192	void				trackWire(
193							BPoint point);
194
195	bool				isWireTracking() const
196						{ return m_draggedWire; }
197
198protected:				// *** internal operations
199
200	// do the actual background drawing
201	void				drawBackground(
202							BRect updateRect);
203
204	// returns the current background color
205	rgb_color			backgroundColor() const
206						{ return m_backgroundColor; }
207
208	// set the background color; does not refresh the display
209	void				setBackgroundColor(
210							rgb_color color);
211
212	// set the background bitmap; does not refresh the display
213	void				setBackgroundBitmap(
214							BBitmap *bitmap);
215
216	// updates the region containing the rects of all boxes in
217	// the view (and thereby the "data-rect") and then adapts
218	// the scrollbars if necessary
219	void				updateDataRect();
220
221private:				// *** internal operations
222
223	// setup a temporary wire for "live" dragging and attaches
224	// a message to the mouse
225	void				_beginWireTracking(
226							DiagramEndPoint *fromEndPoint);
227
228	// delete the temporary dragged wire and invalidate display
229	void				_endWireTracking();
230
231	// setups rect-tracking to additionally drag a message for
232	// easier identification in MouseMoved()
233	void				_beginRectTracking(
234								BPoint origin);
235
236	// takes care of actually selecting/deselecting boxes when
237	// they intersect with the tracked rect
238	void				_trackRect(
239							BPoint origin,
240							BPoint current);
241
242	// updates the scrollbars (if there are any) to represent
243	// the current data-rect
244	void				_updateScrollBars();
245
246private:				// *** data
247
248	// the button pressed at the last mouse event
249	int32				m_lastButton;
250
251	// the number of clicks with the last button
252	int32				m_clickCount;
253
254	// the point last clicked in this view
255	BPoint				m_lastClickPoint;
256
257	// the button currently pressed (reset to 0 on mouse-up)
258	// [e.moon 16nov99]
259	int32					m_pressedButton;
260
261	// last mouse position in screen coordinates [e.moon 16nov99]
262	// only valid while m_pressedButton != 0
263	BPoint				m_lastDragPoint;
264
265	// a pointer to the temporary wire used for wire
266	// tracking
267	DiagramWire		   *m_draggedWire;
268
269	// contains the rects of all DiagramBoxes in this view
270	BRegion				m_boxRegion;
271
272	// contains the rect of the view actually containing something
273	// (i.e. DiagramBoxes) and all free space left/above of that
274	BRect				m_dataRect;
275
276	// true if a bitmap is used for the background; false
277	// if a color is used
278	bool				m_useBackgroundBitmap;
279
280	// the background color of the view
281	rgb_color			m_backgroundColor;
282
283	// the background bitmap of the view
284	BBitmap			   *m_backgroundBitmap;
285};
286
287__END_CORTEX_NAMESPACE
288#endif // __DiagramView_H__
289
290