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