repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / cortex / DiagramView / DiagramEndPoint.cpp
blobc3565e2c77fb89b8602c22ad225e87abc06fa8d1
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 // DiagramEndPoint.cpp
34 #include "DiagramEndPoint.h"
35 #include "DiagramDefs.h"
36 #include "DiagramWire.h"
37 #include "DiagramView.h"
39 #include <Message.h>
40 #include <Messenger.h>
41 __USE_CORTEX_NAMESPACE
43 #include <Debug.h>
44 #define D_METHOD(x) //PRINT (x)
45 #define D_MESSAGE(x) //PRINT (x)
46 #define D_MOUSE(x) //PRINT (x)
48 // -------------------------------------------------------- //
49 // *** ctor/dtor
50 // -------------------------------------------------------- //
52 DiagramEndPoint::DiagramEndPoint(
53 BRect frame)
54 : DiagramItem(DiagramItem::M_ENDPOINT),
55 m_frame(frame),
56 m_wire(0),
57 m_connected(false),
58 m_connecting(false)
60 D_METHOD(("DiagramEndPoint::DiagramEndPoint()\n"));
61 makeDraggable(true);
64 DiagramEndPoint::~DiagramEndPoint()
66 D_METHOD(("DiagramEndPoint::~DiagramEndPoint()\n"));
69 // -------------------------------------------------------- //
70 // *** hook functions
71 // -------------------------------------------------------- //
73 BPoint DiagramEndPoint::connectionPoint() const
75 D_METHOD(("DiagramEndPoint::connectionPoint()\n"));
76 return BPoint(Frame().left + Frame().Height() / 2.0, Frame().top + Frame().Width() / 2.0);
79 bool DiagramEndPoint::connectionRequested(
80 DiagramEndPoint *fromWhich)
82 D_METHOD(("DiagramEndPoint::connectionRequested()\n"));
83 if (!isConnected())
85 return true;
87 return false;
90 // -------------------------------------------------------- //
91 // *** derived from DiagramItem
92 // -------------------------------------------------------- //
94 void DiagramEndPoint::Draw(
95 BRect updateRect)
97 D_METHOD(("DiagramEndPoint::Draw()\n"));
98 if (view())
100 view()->PushState();
102 BRegion region;
103 region.Include(Frame() & updateRect);
104 view()->ConstrainClippingRegion(&region);
105 drawEndPoint();
107 view()->PopState();
111 void DiagramEndPoint::MouseDown(
112 BPoint point,
113 uint32 buttons,
114 uint32 clicks)
116 D_MOUSE(("DiagramEndPoint::MouseDown()\n"));
117 if (clicks == 1)
119 if (isSelectable())
121 BMessage selectMsg(M_SELECTION_CHANGED);
122 if (modifiers() & B_SHIFT_KEY)
124 selectMsg.AddBool("replace", false);
126 else
128 selectMsg.AddBool("replace", true);
130 selectMsg.AddPointer("item", reinterpret_cast<void *>(this));
131 DiagramView* v = view();
132 BMessenger(v).SendMessage(&selectMsg);
134 if (isDraggable() && (buttons == B_PRIMARY_MOUSE_BUTTON))
136 BMessage dragMsg(M_WIRE_DRAGGED);
137 dragMsg.AddPointer("from", static_cast<void *>(this));
138 view()->DragMessage(&dragMsg, BRect(0.0, 0.0, -1.0, -1.0), view());
139 view()->MessageDragged(point, B_INSIDE_VIEW, &dragMsg);
144 void DiagramEndPoint::MessageDragged(
145 BPoint point,
146 uint32 transit,
147 const BMessage *message)
149 D_MOUSE(("DiagramEndPoint::MessageDragged()\n"));
150 switch (message->what)
152 case M_WIRE_DRAGGED:
154 D_MESSAGE(("DiagramEndPoint::MessageDragged(M_WIRE_DRAGGED)\n"));
155 switch (transit)
157 case B_INSIDE_VIEW:
159 //PRINT((" -> transit: B_INSIDE_VIEW\n"));
160 // this is a WORK-AROUND caused by the unreliability
161 // of BViews DragMessage() routine !!
162 if (isConnecting())
164 break;
166 else if (isConnected())
168 view()->trackWire(point);
170 /* this should be enough in theory:
171 if (!isConnecting())
173 view()->trackWire(point);
175 //break;*/
177 case B_ENTERED_VIEW:
179 //PRINT((" -> transit: B_ENTERED_VIEW\n"));
180 DiagramEndPoint *endPoint;
181 if ((message->FindPointer("from", reinterpret_cast<void **>(&endPoint)) == B_OK)
182 && (endPoint != this))
184 if (connectionRequested(endPoint))
186 view()->trackWire(connectionPoint());
187 if (!isConnecting())
189 m_connecting = true;
190 connected();
193 else
195 view()->trackWire(point);
196 if (isConnecting())
198 m_connecting = false;
199 disconnected();
203 break;
205 case B_EXITED_VIEW:
207 //PRINT((" -> transit: B_EXITED_VIEW\n"));
208 if (isConnecting())
210 m_connecting = false;
211 disconnected();
213 break;
216 break;
218 default:
220 DiagramItem::MessageDragged(point, transit, message);
225 void DiagramEndPoint::MessageDropped(
226 BPoint point,
227 BMessage *message)
229 D_MESSAGE(("DiagramEndPoint::MessageDropped()\n"));
230 switch (message->what)
232 case M_WIRE_DRAGGED:
234 D_MESSAGE(("DiagramEndPoint::MessageDropped(M_WIRE_DRAGGED)\n"));
235 DiagramEndPoint *endPoint;
236 if ((message->FindPointer("from", reinterpret_cast<void **>(&endPoint)) == B_OK)
237 && (endPoint != this))
239 bool success = connectionRequested(endPoint);
240 BMessage dropMsg(M_WIRE_DROPPED);
241 dropMsg.AddPointer("from", reinterpret_cast<void *>(endPoint));
242 dropMsg.AddPointer("to", reinterpret_cast<void *>(this));
243 dropMsg.AddBool("success", success);
244 DiagramView* v = view();
245 BMessenger(v).SendMessage(&dropMsg);
246 if (isConnecting())
248 m_connecting = false;
249 disconnected();
252 return;
254 default:
256 DiagramItem::MessageDropped(point, message);
261 // -------------------------------------------------------- //
262 // *** frame related operations
263 // -------------------------------------------------------- //
265 void DiagramEndPoint::MoveBy(
266 float x,
267 float y,
268 BRegion *updateRegion)
270 D_METHOD(("DiagramEndPoint::MoveBy()\n"));
271 if (isConnected() && m_wire && updateRegion)
273 updateRegion->Include(m_wire->Frame());
274 m_frame.OffsetBy(x, y);
275 m_wire->endPointMoved(this);
276 updateRegion->Include(m_wire->Frame());
278 else
280 m_frame.OffsetBy(x, y);
281 if (m_wire)
283 m_wire->endPointMoved(this);
288 void DiagramEndPoint::ResizeBy(
289 float horizontal,
290 float vertical)
292 D_METHOD(("DiagramEndPoint::ResizeBy()\n"));
293 m_frame.right += horizontal;
294 m_frame.bottom += vertical;
297 // -------------------------------------------------------- //
298 // *** connection methods
299 // -------------------------------------------------------- //
301 void DiagramEndPoint::connect(
302 DiagramWire *wire)
304 D_METHOD(("DiagramEndPoint::connect()\n"));
305 if (!m_connected && wire)
307 m_connected = true;
308 m_wire = wire;
309 makeDraggable(false);
310 connected();
314 void DiagramEndPoint::disconnect()
316 D_METHOD(("DiagramEndPoint::disconnect()\n"));
317 if (m_connected)
319 m_connected = false;
320 m_wire = 0;
321 makeDraggable(true);
322 disconnected();
326 // END -- DiagramEndPoint.cpp --