File: PPDocumentView.m

package info (click to toggle)
gnustep-examples 1%3A1.4.0%2Bgit20210703-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,164 kB
  • sloc: objc: 17,202; makefile: 56
file content (206 lines) | stat: -rw-r--r-- 4,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "PPController.h"
#import "PPDocumentClass.h"
#import "PPDocumentView.h"

@implementation PPDocumentView

- (void) awakeFromNib
{
  lastFrame = nil;
  selection = NO;
}

/*- (BOOL) acceptsFirstResponder
{
  return YES;
}*/

- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
{
  return YES;
}

- (void) setFile: (NSBitmapImageRep *)image
{
  ASSIGN(lastFrame, image);
  [self setNeedsDisplay: YES];
}

- (void) setContent: (NSBitmapImageRep *)data
{
  ASSIGN(lastFrame, data);
  
  [[document undoManager] registerUndoWithTarget: self
                                                        selector: @selector(setContent:)
                                                          object: [self currentContent]];
  [self setNeedsDisplay: YES];
}

- (NSBitmapImageRep *) currentContent
{
  NSBitmapImageRep *bitmap;
  
  bitmap = [self bitmapImageRepForCachingDisplayInRect: [self frame]];
  
  return bitmap;
}

/*- (void) mouseDown: (NSEvent *)theEvent
{
  BOOL click = YES;
  NSEvent *event;
  NSBitmapImageRep *bitmap = [self currentContent];
  
  [[document undoManager] registerUndoWithTarget: self
                                                        selector: @selector(setContent:)
                                                          object: bitmap];
  ASSIGN(lastFrame, bitmap);
  
  locA = [self convertPoint: [theEvent locationInWindow] fromView: nil];

  while (click)
    {
      event = [[self window] nextEventMatchingMask:
                        NSLeftMouseUpMask | NSLeftMouseDraggedMask];
                        
      switch ([event type])
        {
        case NSLeftMouseDragged:
          {
            locB = [self convertPoint: [event locationInWindow] fromView: nil];
            NSSize size = NSMakeSize(locB.x - locA.x, locB.y - locA.y);
            selectedRect.origin = locA;
            selectedRect.size = size;
            selection = YES;
            
            if ([[NSApp delegate] tool] == 0)
              {
                [self setNeedsDisplayInRect: NSMakeRect(locB.x - 2, locB.y - 2, 4, 4)];
              }
            else
              {
                [self setNeedsDisplay: YES];
              }
          }
          break;
        case NSLeftMouseUp:
          {
            click = NO;
            selection = NO;
          }
          break;
        }
    }
}*/

- (void) mouseDown: (NSEvent *)theEvent
{
  NSBitmapImageRep *bitmap = [self currentContent];
  
  [[document undoManager] registerUndoWithTarget: self
                                                        selector: @selector(setContent:)
                                                          object: bitmap];
  ASSIGN(lastFrame, bitmap);
  
  locA = [self convertPoint: [theEvent locationInWindow] fromView: nil];
  selectedRect.origin = locA;
  selection = YES;
  
  switch ([[NSApp delegate] tool])
    {
    case 0:
      [[document undoManager] setActionName: @"Trazo"];
      break;
    case 1:
      [[document undoManager] setActionName: @"Linea"];
      break;
    case 2:
      [[document undoManager] setActionName: @"Circulo"];
      break;
    case 3:
      [[document undoManager] setActionName: @"Rectangulo"];
      break;
    }
}

- (void) mouseDragged: (NSEvent *)theEvent
{
  locB = [self convertPoint: [theEvent locationInWindow] fromView: nil];
  NSSize size = NSMakeSize(locB.x - locA.x, locB.y - locA.y);
  selectedRect.size = size;
      
  if ([[NSApp delegate] tool] == 0)
    {
      [self setNeedsDisplayInRect: NSMakeRect(locB.x - 2, locB.y - 2, 4, 4)];
    }
  else
    {
      [self setNeedsDisplay: YES];
    }
}

- (void) mouseUp: (NSEvent*)theEvent
{
  ASSIGN(lastFrame, [self currentContent]);
  selection = NO;
}

- (void) drawRect: (NSRect)rect
{
  if (lastFrame != nil)
    {
      [lastFrame drawInRect: [self frame]];
    }
  else
    {
      [[NSColor whiteColor] set];
      [[NSBezierPath bezierPathWithRect: rect] fill];
    }
  
  if (selection)
    {
      NSBezierPath *path;
      [[[[NSApp delegate] colorWell] color] set];

      switch ([[NSApp delegate] tool])
        {
        case 0:
          {
            path = [NSBezierPath bezierPathWithRect: NSMakeRect(locB.x - 2, locB.y - 2, 4, 4)];
            [path fill];
          }
          break;
        case 1:
          {
            path = [NSBezierPath bezierPath];
            [path moveToPoint: locA];
            [path lineToPoint: locB];
            [path setLineWidth: 2];
            [path stroke];
          }
          break;
        case 2:
          {
            path = [NSBezierPath bezierPathWithOvalInRect: selectedRect];
            [path fill];
          }
          break;
        case 3:
          {
            path = [NSBezierPath bezierPathWithRect: selectedRect];
            [path fill];
          }
          break;
        }
    }
}

- (void) dealloc
{
  [lastFrame release];
  [super dealloc];
}

@end