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