xref: /haiku/src/apps/cortex/DiagramView/DiagramItem.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // DiagramItem.h (Cortex/DiagramView)
2 //
3 // * PURPOSE
4 //   Provides a base class for all items that can be handled
5 //   by the DiagramView implementation. A basic interface with
6 //	 some common implementation is defined, with methods related
7 //   to drawing, mouse handling, selecting, dragging, comparison
8 //   for sorting, and access to the drawing context which is the
9 //   DiagramView instance.
10 //
11 // * HISTORY
12 //   c.lenz		25sep99		Begun
13 //
14 
15 #ifndef __DiagramItem_H__
16 #define __DiagramItem_H__
17 
18 #include <OS.h>
19 #include <InterfaceDefs.h>
20 #include <Region.h>
21 
22 class BView;
23 
24 #include "cortex_defs.h"
25 __BEGIN_CORTEX_NAMESPACE
26 
27 class DiagramItemGroup;
28 class DiagramView;
29 class DiagramBox;
30 
31 class DiagramItem
32 {
33 	friend DiagramItemGroup;
34 	friend DiagramView;
35 	friend DiagramBox;
36 
37 public:					// *** types
38 
39 	enum diagram_item_t {
40 		M_BOX		= 0x1,
41 		M_WIRE		= 0x2,
42 		M_ENDPOINT	= 0x4,
43 		M_ANY		= 0x7
44 	};
45 
46 public:					// *** ctor/dtor
47 
48 						DiagramItem(
49 							uint32 itemType);
50 
51 	virtual				~DiagramItem();
52 
53 public:					// *** accessors
54 
55 	// returns the item type assigned in the ctor
56 	uint32				type() const
57 						{ return m_type; }
58 
59 	// returns pointer to the drawing context of the DiagramView
60 	// object
61 	DiagramView		   *view() const
62 						{ return m_view; }
63 
64 	// returns pointer to the DiagramItemGroup the item belongs to
65 	DiagramItemGroup   *group() const
66 						{ return m_group; }
67 
68 	// returns true if the item is currently selected
69 	bool				isSelected() const
70 						{ return m_selected; }
71 
72 public:					// *** operations
73 
74 	// changes the selection state of the item, and updates the
75 	// m_selectionTime member in the process to ensure proper
76 	// sorting; calls the selected() hook if the state has
77 	// actually changed
78 	void				select();
79 
80 	// sets the item to selected without changing m_selectionTime
81 	// to the time of selection but prior to the last "replacing"
82 	// selection (i.e. thru select()) use this method for additive
83 	// selecting; still calls the selected() hook
84 	void				selectAdding();
85 
86 	// deselects the item; calls the deselected() hook if the
87 	// state has actually changed
88 	void				deselect();
89 
90 	// moves the items frame to a given point by calling moveBy with the
91 	// absolute coords translated into relative shift amount
92 	void				moveTo(
93 							BPoint point,
94 							BRegion *updateRegion = 0)
95 						{ moveBy(point.x - frame().left, point.y - frame().top, updateRegion); }
96 
97 	// resizes the items frame to given dimensions; simply calls the resizeBy
98 	// implementation
99 	void				resizeTo(
100 							float width,
101 							float height)
102 						{ resizeBy(width - frame().Width(), height - frame().Height()); }
103 
104 public:					// *** hook functions
105 
106 	// is called when the item has been attached to the DiagramView
107 	// and the view() pointer is valid
108 	virtual void		attachedToDiagram()
109 						{ /* does nothing */ }
110 
111 	// is called just before the item is being detached from the
112 	// the DiagramView
113 	virtual void		detachedFromDiagram()
114 						{ /* does nothing */ }
115 
116 	// is called from the DiagramViews MouseDown() function after
117 	// finding out the mouse buttons and clicks quantity.
118 	virtual void		mouseDown(
119 							BPoint point,
120 							uint32 buttons,
121 							uint32 clicks)
122 						{/* does nothing */}
123 
124 	// is called from the DiagramViews MouseMoved() when *no* message is being
125 	// dragged, i.e. the mouse is simply floating above the item
126 	virtual void		mouseOver(
127 							BPoint point,
128 							uint32 transit)
129 						{/* does nothing */}
130 
131 	// is called from the DiagramViews MouseMoved() when a message is being
132 	// dragged; always call the base class version when overriding!
133 	virtual void		messageDragged(
134 							BPoint point,
135 							uint32 transit,
136 							const BMessage *message)
137 						{/* does nothing */}
138 
139 	// is called from the DiagramViews MessageReceived() function when an
140 	// message has been received through Drag&Drop; always call the base
141 	// class version when overriding!
142 	virtual void		messageDropped(
143 							BPoint point,
144 							BMessage *message)
145 						{/* does nothing */}
146 
147 	// is called when the item has been selected or deselected in some way
148 	virtual void		selected()
149 						{ /* does nothing */ }
150 	virtual void		deselected()
151 						{ /* does nothing */ }
152 
153 public:					// *** interface definition
154 
155 	// this function must be implemented by derived classes to return the
156 	// items frame rectangle in the DiagramViews coordinates
157 	virtual BRect		frame() const = 0;
158 
159 	// this function should be implemented for non-rectangular subclasses
160 	// (like wires) to estimate how close a given point is to the object;
161 	// the default implementation returns 1.0 when the point lies within
162 	// the Frame() rect and 0.0 if not
163 	virtual float		howCloseTo(
164 							BPoint point) const;
165 
166 	// this is the hook function called by DiagramView when it's time to
167 	// draw the object
168 	virtual void		draw(
169 							BRect updateRect) = 0;
170 
171 	// should move the items frame by the specified amount and do the
172 	// necessary drawing instructions to update the display; if the
173 	// caller supplied a BRegion pointer in updateRegion, this method
174 	// should add other areas affected by the move to it (e.g. wire
175 	// frames)
176 	virtual void		moveBy(
177 							float x,
178 							float y,
179 							BRegion *updateRegion = 0)
180 						{ /* does nothing */ }
181 
182 	// should resize the items frame by the specified amount
183 	virtual void		resizeBy(
184 							float horizontal,
185 							float vertical)
186 						{ /* does nothing */ }
187 
188 protected:				// *** selecting/dragging
189 
190 	// turn on/off the built-in selection handling
191 	void				makeSelectable(
192 							bool selectable)
193 							{ m_selectable = selectable; }
194 	bool				isSelectable() const
195 							{ return m_selectable; }
196 
197 	// turn on/off the built-in drag & drop handling
198 	void				makeDraggable(
199 							bool draggable)
200 							{ m_draggable = draggable; }
201 	bool				isDraggable() const
202 							{ return m_draggable; }
203 
204 protected:				// *** compare functions
205 
206 	// compares the time when each item was last selected and
207 	// returns -1 for the most recent.
208 	friend int			compareSelectionTime(
209 							const void *lValue,
210 							const void *rValue);
211 
212 protected:				// *** internal methods
213 
214 	// called only by DiagramItemGroup objects in the method
215 	// addItem()
216 	virtual void		_setOwner(
217 							DiagramView *owner)
218 						{ m_view = owner; }
219 
220 private:				// *** data members
221 
222 	// the items type (M_BOX, M_WIRE or M_ENDPOINT)
223 	uint32				m_type;
224 
225 	// a pointer to the drawing context (the DiagramView instance)
226 	DiagramView		   *m_view;
227 
228 	// a pointer to the DiagramItemGroup the item belongs to
229 	DiagramItemGroup   *m_group;
230 
231 	// can the object be dragged
232 	bool				m_draggable;
233 
234 	// can the object be selected
235 	bool				m_selectable;
236 
237 	// is the object currently selected
238 	bool				m_selected;
239 
240 	// when was the object selected the last time or added (used
241 	// for drawing order)
242 	bigtime_t			m_selectionTime;
243 
244 	// stores the most recent time a item was selected thru
245 	// the select() method
246 	static bigtime_t	m_lastSelectionTime;
247 
248 	// counts the number of selections thru selectAdding()
249 	// since the last call to select()
250 	static int32		m_countSelected;
251 };
252 
253 __END_CORTEX_NAMESPACE
254 #endif /* __DiagramItem_H__ */
255