1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // hdChangeConnectionHandle.cpp - Base Handle to allow change connected figures at connection figures
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 #include "pgAdmin3.h"
13 
14 // wxWindows headers
15 #include <wx/wx.h>
16 #include <wx/dcbuffer.h>
17 
18 // App headers
19 #include "hotdraw/handles/hdChangeConnectionHandle.h"
20 #include "hotdraw/figures/hdLineConnection.h"
21 #include "hotdraw/utilities/hdPoint.h"
22 #include "hotdraw/main/hdDrawingView.h"
23 
24 
hdChangeConnectionHandle(hdLineConnection * owner)25 hdChangeConnectionHandle::hdChangeConnectionHandle(hdLineConnection *owner):
26 	hdIHandle(owner)
27 {
28 	connection = owner;
29 	targetFigure = NULL;
30 	originalTarget = NULL;
31 }
32 
~hdChangeConnectionHandle()33 hdChangeConnectionHandle::~hdChangeConnectionHandle()
34 {
35 
36 }
37 
draw(wxBufferedDC & context,hdDrawingView * view)38 void hdChangeConnectionHandle::draw(wxBufferedDC &context, hdDrawingView *view)
39 {
40 
41 	hdRect copy = getDisplayBox(view->getIdx());
42 	view->CalcScrolledPosition(copy.x, copy.y, &copy.x, &copy.y);
43 
44 	copy.Deflate(2, 2);
45 	context.SetPen(*wxGREEN_PEN);
46 	context.SetBrush(*wxGREEN_BRUSH);
47 	context.DrawRectangle(copy);
48 }
49 
createCursor()50 wxCursor hdChangeConnectionHandle::createCursor()
51 {
52 	return wxCursor(wxCURSOR_CROSS);
53 }
54 
invokeStart(hdMouseEvent & event,hdDrawingView * view)55 void hdChangeConnectionHandle::invokeStart(hdMouseEvent &event, hdDrawingView *view)
56 {
57 	originalTarget = target();
58 	disconnect(view);
59 }
60 
invokeStep(hdMouseEvent & event,hdDrawingView * view)61 void hdChangeConnectionHandle::invokeStep(hdMouseEvent &event, hdDrawingView *view)
62 {
63 	int x = event.GetPosition().x, y = event.GetPosition().y;
64 	hdPoint p = hdPoint(x, y);
65 	hdIFigure *figure = findConnectableFigure(view->getIdx(), x, y, view->getDrawing());
66 	targetFigure = figure;
67 	hdIConnector *target = findConnectionTarget(view->getIdx(), x, y, view->getDrawing());
68 	if(target)
69 	{
70 		p = target->getDisplayBox().center(view->getIdx());
71 	}
72 	setPoint(view->getIdx(), p);
73 	connection->updateConnection(view->getIdx());
74 	if(view)
75 		view->Refresh();
76 }
77 
invokeEnd(hdMouseEvent & event,hdDrawingView * view)78 void hdChangeConnectionHandle::invokeEnd(hdMouseEvent &event, hdDrawingView *view)
79 {
80 	int x = event.GetPosition().x, y = event.GetPosition().y;
81 	hdIConnector *target = findConnectionTarget(view->getIdx(), x, y, view->getDrawing());
82 	if(!target)
83 	{
84 		target = originalTarget;
85 	}
86 	connect(target, view);
87 	connection->updateConnection(view->getIdx());
88 	if(view)
89 		view->Refresh();
90 }
91 
92 
findConnectableFigure(int posIdx,int x,int y,hdDrawing * drawing)93 hdIFigure *hdChangeConnectionHandle::findConnectableFigure (int posIdx, int x, int y, hdDrawing *drawing)
94 {
95 	hdIFigure *out = NULL;
96 	hdIteratorBase *iterator = drawing->figuresInverseEnumerator();
97 	while(iterator->HasNext())
98 	{
99 		hdIFigure *figure = (hdIFigure *) iterator->Next();
100 		if(figure->containsPoint(posIdx, x, y) && isConnectionPossible(figure))
101 		{
102 			out = figure;
103 			break;
104 		}
105 	}
106 	delete iterator;
107 	return out;
108 }
findConnectionTarget(int posIdx,int x,int y,hdDrawing * drawing)109 hdIConnector *hdChangeConnectionHandle::findConnectionTarget(int posIdx, int x, int y, hdDrawing *drawing)
110 {
111 	hdIFigure *target = findConnectableFigure(posIdx, x, y, drawing);
112 	if(target)
113 		return target->connectorAt(posIdx, x, y);
114 	else
115 		return NULL;
116 }
117