repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / cortex / DiagramView / DiagramWire.cpp
blob98b849bc11f8a491baa4736aa6f4f80503bbd8ce
1 /*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 // DiagramWire.cpp
34 #include "DiagramWire.h"
35 #include "DiagramDefs.h"
36 #include "DiagramEndPoint.h"
37 #include "DiagramView.h"
39 #include <Message.h>
40 #include <Messenger.h>
42 __USE_CORTEX_NAMESPACE
44 #include <Debug.h>
45 #define D_METHOD(x) //PRINT (x)
46 #define D_MESSAGE(x) //PRINT (x)
48 // -------------------------------------------------------- //
49 // *** ctor/dtor
50 // -------------------------------------------------------- //
52 DiagramWire::DiagramWire(
53 DiagramEndPoint *fromWhich,
54 DiagramEndPoint *toWhich)
55 : DiagramItem(DiagramItem::M_WIRE),
56 m_fromEndPoint(fromWhich),
57 m_toEndPoint(toWhich),
58 m_temporary(false)
60 D_METHOD(("DiagramWire::DiagramWire()\n"));
61 makeDraggable(false);
62 ASSERT(m_fromEndPoint);
63 m_fromEndPoint->connect(this);
64 ASSERT(m_toEndPoint);
65 m_toEndPoint->connect(this);
68 DiagramWire::DiagramWire(
69 DiagramEndPoint *fromWhich,
70 bool isStartPoint)
71 : DiagramItem(DiagramItem::M_WIRE),
72 m_fromEndPoint(isStartPoint ? fromWhich : 0),
73 m_toEndPoint(isStartPoint ? 0 : fromWhich),
74 m_temporary(true),
75 m_dragEndPoint(fromWhich->connectionPoint())
77 D_METHOD(("DiagramWire::DiagramWire(temp)\n"));
78 makeDraggable(false);
79 ASSERT(fromWhich);
80 if (m_fromEndPoint)
82 m_fromEndPoint->connect(this);
84 else if (m_toEndPoint)
86 m_toEndPoint->connect(this);
90 DiagramWire::~DiagramWire()
92 D_METHOD(("DiagramWire::~DiagramWire()\n"));
93 if (m_fromEndPoint)
95 m_fromEndPoint->disconnect();
97 if (m_toEndPoint)
99 m_toEndPoint->disconnect();
103 // -------------------------------------------------------- //
104 // *** accessors
105 // -------------------------------------------------------- //
107 BPoint DiagramWire::startConnectionPoint() const
109 D_METHOD(("DiagramWire::startConnectionPoint()\n"));
110 if (m_fromEndPoint)
112 return m_fromEndPoint->connectionPoint();
114 else if (m_temporary)
116 return m_dragEndPoint;
118 return BPoint(-1.0, -1.0);
121 BPoint DiagramWire::endConnectionPoint() const
123 D_METHOD(("DiagramWire::endConnectionPoint()\n"));
124 if (m_toEndPoint)
126 return m_toEndPoint->connectionPoint();
128 else if (m_temporary)
130 return m_dragEndPoint;
132 return BPoint(-1.0, -1.0);
135 // -------------------------------------------------------- //
136 // *** derived from DiagramItem
137 // -------------------------------------------------------- //
139 float DiagramWire::howCloseTo(
140 BPoint point) const
142 D_METHOD(("DiagramWire::howCloseTo()\n"));
143 BPoint a = startConnectionPoint();
144 BPoint b = endConnectionPoint();
145 float length, result;
146 length = sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
147 result = ((a.y - point.y) * (b.x - a.x)) - ((a.x - point.x) * (b.y - a.y));
148 result = 3.0 - fabs(result / length);
149 return result;
152 void DiagramWire::Draw(
153 BRect updateRect)
155 D_METHOD(("DiagramWire::Draw()\n"));
156 if (view())
158 view()->PushState();
160 BRegion region, clipping;
161 region.Include(Frame() & updateRect);
162 if (view()->GetClippingAbove(this, &clipping))
163 region.Exclude(&clipping);
164 view()->ConstrainClippingRegion(&region);
165 drawWire();
167 view()->PopState();
171 void DiagramWire::MouseDown(
172 BPoint point,
173 uint32 buttons,
174 uint32 clicks)
176 D_METHOD(("DiagramWire::MouseDown()\n"));
177 if (clicks == 1)
179 if (isSelectable())
181 BMessage selectMsg(M_SELECTION_CHANGED);
182 if (modifiers() & B_SHIFT_KEY)
184 selectMsg.AddBool("replace", false);
186 else
188 selectMsg.AddBool("replace", true);
190 selectMsg.AddPointer("item", reinterpret_cast<void *>(this));
191 DiagramView* v = view();
192 BMessenger(v).SendMessage(&selectMsg);
197 void DiagramWire::MessageDragged(
198 BPoint point,
199 uint32 transit,
200 const BMessage *message)
202 D_METHOD(("DiagramWire::MessageDragged()\n"));
203 switch (message->what)
205 case M_WIRE_DRAGGED:
207 view()->trackWire(point);
208 break;
210 default:
212 DiagramItem::MessageDragged(point, transit, message);
217 // END -- DiagramWire.cpp --