2 * Copyright (c) 1999-2000, Eric Moon.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
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.
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.
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.
32 // DiagramItem.h (Cortex/DiagramView)
35 // Provides a base class for all items that can be handled
36 // by the DiagramView implementation. A basic interface with
37 // some common implementation is defined, with methods related
38 // to drawing, mouse handling, selecting, dragging, comparison
39 // for sorting, and access to the drawing context which is the
40 // DiagramView instance.
43 // c.lenz 25sep99 Begun
46 #ifndef __DiagramItem_H__
47 #define __DiagramItem_H__
50 #include <InterfaceDefs.h>
56 #include "cortex_defs.h"
57 __BEGIN_CORTEX_NAMESPACE
59 class DiagramItemGroup
;
63 int compareSelectionTime(const void *lValue
, const void *rValue
);
67 friend class DiagramItemGroup
;
68 friend class DiagramView
;
69 friend class DiagramBox
;
80 public: // *** ctor/dtor
85 virtual ~DiagramItem();
87 public: // *** accessors
89 // returns the item type assigned in the ctor
93 // returns pointer to the drawing context of the DiagramView
95 DiagramView
*view() const
98 // returns pointer to the DiagramItemGroup the item belongs to
99 DiagramItemGroup
*group() const
102 // returns true if the item is currently selected
103 bool isSelected() const
104 { return m_selected
; }
106 public: // *** operations
108 // changes the selection state of the item, and updates the
109 // m_selectionTime member in the process to ensure proper
110 // sorting; calls the selected() hook if the state has
114 // sets the item to selected without changing m_selectionTime
115 // to the time of selection but prior to the last "replacing"
116 // selection (i.e. thru select()) use this method for additive
117 // selecting; still calls the selected() hook
120 // deselects the item; calls the deselected() hook if the
121 // state has actually changed
124 // moves the items frame to a given point by calling MoveBy with the
125 // absolute coords translated into relative shift amount
128 BRegion
*updateRegion
= 0)
129 { MoveBy(point
.x
- Frame().left
, point
.y
- Frame().top
, updateRegion
); }
131 // resizes the items frame to given dimensions; simply calls the ResizeBy
136 { ResizeBy(width
- Frame().Width(), height
- Frame().Height()); }
138 public: // *** hook functions
140 // is called when the item has been attached to the DiagramView
141 // and the view() pointer is valid
142 virtual void attachedToDiagram()
143 { /* does nothing */ }
145 // is called just before the item is being detached from the
147 virtual void detachedFromDiagram()
148 { /* does nothing */ }
150 // is called from the DiagramViews MouseDown() function after
151 // finding out the mouse buttons and clicks quantity.
152 virtual void MouseDown(
158 // is called from the DiagramViews MouseMoved() when *no* message is being
159 // dragged, i.e. the mouse is simply floating above the item
160 virtual void MouseOver(
165 // is called from the DiagramViews MouseMoved() when a message is being
166 // dragged; always call the base class version when overriding!
167 virtual void MessageDragged(
170 const BMessage
*message
)
173 // is called from the DiagramViews MessageReceived() function when an
174 // message has been received through Drag&Drop; always call the base
175 // class version when overriding!
176 virtual void MessageDropped(
181 // is called when the item has been selected or deselected in some way
182 virtual void selected()
183 { /* does nothing */ }
184 virtual void deselected()
185 { /* does nothing */ }
187 public: // *** interface definition
189 // this function must be implemented by derived classes to return the
190 // items frame rectangle in the DiagramViews coordinates
191 virtual BRect
Frame() const = 0;
193 // this function should be implemented for non-rectangular subclasses
194 // (like wires) to estimate how close a given point is to the object;
195 // the default implementation returns 1.0 when the point lies within
196 // the Frame() rect and 0.0 if not
197 virtual float howCloseTo(
200 // this is the hook function called by DiagramView when it's time to
203 BRect updateRect
) = 0;
205 // should move the items frame by the specified amount and do the
206 // necessary drawing instructions to update the display; if the
207 // caller supplied a BRegion pointer in updateRegion, this method
208 // should add other areas affected by the move to it (e.g. wire
213 BRegion
*updateRegion
= 0)
214 { /* does nothing */ }
216 // should resize the items frame by the specified amount
217 virtual void ResizeBy(
220 { /* does nothing */ }
222 protected: // *** selecting/dragging
224 // turn on/off the built-in selection handling
227 { m_selectable
= selectable
; }
228 bool isSelectable() const
229 { return m_selectable
; }
231 // turn on/off the built-in drag & drop handling
234 { m_draggable
= draggable
; }
235 bool isDraggable() const
236 { return m_draggable
; }
238 protected: // *** compare functions
240 // compares the time when each item was last selected and
241 // returns -1 for the most recent.
242 friend int compareSelectionTime(
246 protected: // *** internal methods
248 // called only by DiagramItemGroup objects in the method
250 virtual void _SetOwner(
254 private: // *** data members
256 // the items type (M_BOX, M_WIRE or M_ENDPOINT)
259 // a pointer to the drawing context (the DiagramView instance)
262 // a pointer to the DiagramItemGroup the item belongs to
263 DiagramItemGroup
*m_group
;
265 // can the object be dragged
268 // can the object be selected
271 // is the object currently selected
274 // when was the object selected the last time or added (used
275 // for drawing order)
276 bigtime_t m_selectionTime
;
278 // stores the most recent time a item was selected thru
279 // the select() method
280 static bigtime_t m_lastSelectionTime
;
282 // counts the number of selections thru selectAdding()
283 // since the last call to select()
284 static int32 m_countSelected
;
287 __END_CORTEX_NAMESPACE
288 #endif /* __DiagramItem_H__ */