1/*
2 Project: Graphos
3 GRBoxEditor.m
4
5 Copyright (C) 2007-2015 GNUstep Application Project
6
7 Author: Ing. Riccardo Mottola
8
9 Created: 2007-09-18
10
11 This application is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This application is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 Library General Public License for more details.
20
21 You should have received a copy of the GNU General Public
22 License along with this library; if not, write to the Free
23 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 */
25
26#import "GRBoxEditor.h"
27#import "GRDocView.h"
28#import "GRFunctions.h"
29
30@implementation GRBoxEditor
31
32- (id)initEditor:(GRDrawableObject *)anObject
33{
34  self = [super initEditor:anObject];
35  if(self != nil)
36    {
37    }
38  return self;
39}
40
41- (NSPoint)constrainControlPoint:(NSPoint)p
42{
43  NSPoint pos;
44  CGFloat w, h;
45  NSPoint retP;
46
47  retP = p;
48  pos = [(GRBox *)object position];
49  w = pos.x-p.x;
50  h = pos.y-p.y;
51
52  if (w < h)
53    retP.y = pos.y+w;
54  else
55    retP.x = pos.x+h;
56
57  return retP;
58}
59
60- (NSPoint)moveControlAtPoint:(NSPoint)p
61{
62  GRObjectControlPoint *cp;
63  NSEvent *event;
64  BOOL found = NO;
65  CGFloat zFactor;
66  NSPoint pp;
67
68  pp = NSZeroPoint;
69  cp = [(GRBox *)object startControlPoint];
70  if (pointInRect([cp centerRect], p))
71    {
72      [self selectForEditing];
73      [(GRPathObject *)object setCurrentPoint:cp];
74      [cp select];
75      found =  YES;
76    }
77  cp = [(GRBox *)object endControlPoint];
78  if (pointInRect([cp centerRect], p))
79    {
80      [self selectForEditing];
81      [(GRPathObject *)object setCurrentPoint:cp];
82      [cp select];
83      found =  YES;
84    }
85
86  if(!found)
87    return p;
88
89  zFactor = [object zoomFactor];
90
91  event = [[[object view] window] nextEventMatchingMask:
92				    NSLeftMouseUpMask | NSLeftMouseDraggedMask];
93  if([event type] == NSLeftMouseDragged)
94    {
95      [[object view] verifyModifiersOfEvent: event];
96      do
97        {
98	  pp = [event locationInWindow];
99	  pp = [[object view] convertPoint: pp fromView: nil];
100	  pp = GRpointDeZoom(pp, zFactor);
101	  if([[object view] shiftclick])
102	    pp = [self constrainControlPoint:pp];
103
104	  [[(GRPathObject *)object currentPoint] moveToPoint: pp];
105	  [(GRPathObject *)object remakePath];
106
107	  [[object view] setNeedsDisplay: YES];
108	  event = [[[object view] window] nextEventMatchingMask:
109					    NSLeftMouseUpMask | NSLeftMouseDraggedMask];
110	  [[object view] verifyModifiersOfEvent: event];
111        }
112      while([event type] != NSLeftMouseUp);
113
114  }
115
116  return pp;
117}
118
119
120- (void)draw
121{
122  if(![object visible])
123    return;
124
125  if([self isGroupSelected])
126    {
127      [[(GRBox *)object startControlPoint] drawControlAsSelected:NO];
128      [[(GRBox *)object endControlPoint] drawControlAsSelected:NO];
129    }
130
131  if([self isEditSelected])
132    {
133      [[(GRBox *)object startControlPoint] drawControl];
134      [[(GRBox *)object endControlPoint] drawControl];
135    }
136}
137
138@end
139