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