File: NSTextAttachment.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (388 lines) | stat: -rw-r--r-- 10,246 bytes parent folder | download | duplicates (2)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/** <title>NSTextAttachment</title>

   <abstract>Classes to represent text attachments.</abstract>
   
   NSTextAttachment is used to represent text attachments. When inline, 
   text attachments appear as the value of the NSAttachmentAttributeName 
   attached to the special character NSAttachmentCharacter.
   
   NSTextAttachment uses an object obeying the NSTextAttachmentCell 
   protocol to get input from the user and to display an image.

   NSTextAttachmentCell is a simple subclass of NSCell which provides 
   the NSTextAttachment protocol.

   Copyright (C) 2000 Free Software Foundation, Inc.

   Author: Fred Kiefer <FredKiefer@gmx.de>
   Date: June 2000
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#import "AppKit/NSCell.h"
#import "AppKit/NSFileWrapper.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSTextContainer.h"
#import "AppKit/NSTextAttachment.h"
#import "AppKit/NSTextView.h"


@implementation NSTextAttachmentCell

- (void)drawWithFrame: (NSRect)cellFrame 
	       inView: (NSView *)controlView 
       characterIndex: (NSUInteger)charIndex
{
  //  cellFrame.origin.y -= cellFrame.size.height;
  [self drawWithFrame: cellFrame 
	inView: controlView];
}

- (void)drawWithFrame: (NSRect)cellFrame 
	       inView: (NSView *)controlView 
       characterIndex: (NSUInteger)charIndex
	layoutManager: (NSLayoutManager *)layoutManager
{
  [self drawWithFrame: cellFrame 
	inView: controlView
	characterIndex: charIndex];
}

- (NSPoint)cellBaselineOffset
{
  return NSZeroPoint;
}

- (NSRect)cellFrameForTextContainer: (NSTextContainer *)textContainer 
	       proposedLineFragment: (NSRect)lineFrag
		      glyphPosition: (NSPoint)position 
		     characterIndex: (NSUInteger)charIndex
{
  NSRect aRect;
  
  aRect.origin = [self cellBaselineOffset];
  aRect.size = [self cellSize];
  return aRect;
}

- (BOOL)wantsToTrackMouse
{
  return YES;
}

- (BOOL)wantsToTrackMouseForEvent: (NSEvent *)theEvent 
			   inRect: (NSRect)cellFrame 
			   ofView: (NSView *)controlView
		 atCharacterIndex: (NSUInteger)charIndex
{
  return [self wantsToTrackMouse];
}

- (BOOL)trackMouse: (NSEvent *)theEvent 
	    inRect: (NSRect)cellFrame 
	    ofView: (NSView *)controlView 
      untilMouseUp: (BOOL)flag
{
  if ([controlView respondsToSelector: @selector(delegate)])
    {
      NSTextView *textView = (NSTextView*)controlView;
      id delegate = [textView delegate];
      NSEventType type = [theEvent type];
      
      if (type == NSLeftMouseUp)
        {
	  if ([theEvent clickCount] == 2)
	    {
	      if (delegate != nil && [delegate respondsToSelector: 
		@selector(textView:doubleClickedOnCell:inRect:)])
	        {
		  [delegate textView: textView 
			    doubleClickedOnCell: self 
			    inRect: cellFrame];
		  return YES;
		}
	    }
	  else
	    {
	      if (delegate != nil && [delegate respondsToSelector: 
		@selector(textView:clickedOnCell:inRect:)])
	        {
		  [delegate textView: textView 
			    clickedOnCell: self 
			    inRect: cellFrame];
		  return YES;
		}
	    }
	}
      else if (type == NSLeftMouseDragged)
        {
	  if (delegate != nil && [delegate respondsToSelector: 
	    @selector(textView:draggedCell:inRect:event:)])
	    {
	      [delegate textView: textView 
			draggedCell: self 
			inRect: cellFrame
			event: theEvent];
	      return YES;
	    }
	}
    }
     
  return [super trackMouse: theEvent 
		inRect: cellFrame 
		ofView: controlView 
		untilMouseUp: flag];
}

- (BOOL)trackMouse: (NSEvent *)theEvent 
	    inRect: (NSRect)cellFrame 
	    ofView: (NSView *)controlView
  atCharacterIndex: (NSUInteger)charIndex 
      untilMouseUp: (BOOL)flag
{
  if ([controlView respondsToSelector: @selector(delegate)])
    {
      NSTextView *textView = (NSTextView*)controlView;
      id delegate = [textView delegate];
      NSEventType type = [theEvent type];
      
      if (type == NSLeftMouseDown)
        { 
	  if ([theEvent clickCount] == 2)
	    {
              if (delegate != nil)
                {
                  if ([delegate respondsToSelector: 
                    @selector(textView:doubleClickedOnCell:inRect:atIndex:)])
                    {
                      [delegate textView: textView 
                                doubleClickedOnCell: self 
                                inRect: cellFrame
                                atIndex: charIndex];
                      return YES;
                    }
                  else if ([delegate respondsToSelector: 
                    @selector(textView:doubleClickedOnCell:inRect:)])
                    {
                      [delegate textView: textView 
                                doubleClickedOnCell: self 
                                inRect: cellFrame];
                      return YES;
                    }
                }
	    }
	  else
	    {
              if (delegate != nil)
                {
                  if ([delegate respondsToSelector: 
                    @selector(textView:clickedOnCell:inRect:atIndex:)])
                    {
                      [delegate textView: textView 
                                clickedOnCell: self 
                                inRect: cellFrame
                                atIndex: charIndex];
                      return YES;
                    }
                  else if ([delegate respondsToSelector: 
                    @selector(textView:clickedOnCell:inRect:)])
                    {
                      [delegate textView: textView 
                                clickedOnCell: self 
                                inRect: cellFrame];
                      return YES;
                    }
                }
	    }
	}
      else if (type == NSLeftMouseDragged)
        {
	  if (delegate != nil && [delegate respondsToSelector: 
	    @selector(textView:draggedCell:inRect:event:atIndex:)])
	    {
	      [delegate textView: textView 
			draggedCell: self 
			inRect: cellFrame
			event: theEvent
			atIndex: charIndex];
	      return YES;
	    }
	}
    }
  
  return [self trackMouse: theEvent 
	       inRect: cellFrame 
	       ofView: controlView 
	       untilMouseUp: flag];
}

- (void)setAttachment: (NSTextAttachment *)anObject
{
  // Do not retain the attachment
  _attachment = anObject;
}

- (NSTextAttachment *)attachment
{
  return _attachment;
}

//FIXME: I had to add those methods to keep the compiler quite. 
// This are already defined on the super class and should be taken from there.

- (NSSize)cellSize
{
  return [super cellSize];
}

- (void)highlight: (BOOL)flag 
	withFrame: (NSRect)cellFrame 
	   inView: (NSView *)controlView
{
  [super highlight: flag 
	 withFrame: cellFrame 
	 inView: controlView];
}

- (void)drawWithFrame: (NSRect)cellFrame inView: (NSView *)controlView
{
  [super drawWithFrame: cellFrame inView: controlView];
}

@end


@implementation NSTextAttachment

- (id) init
{
  return [self initWithFileWrapper: nil];
}

- (void) dealloc
{
  DESTROY(_fileWrapper);
  DESTROY(_cell);
  [super dealloc];
}

- (id) initWithFileWrapper: (NSFileWrapper *)fileWrapper
{
  self = [super init];
  if (self != nil)
    {
      _cell = [[NSTextAttachmentCell alloc ] init];
      [self setFileWrapper: fileWrapper];
    }
  return self;
}

- (void)setFileWrapper: (NSFileWrapper *)fileWrapper
{
  ASSIGN(_fileWrapper, fileWrapper);
  // Reset the cell, so it shows the new attachment
  if ([_cell respondsToSelector: @selector(setAttachment:)] == YES)
    {
      [_cell setAttachment: self];
    }
  if (_taflags.cell_explicitly_set == 0)
    {
      if (fileWrapper != nil)
        {
          NSImage *icon = nil;
          NSString *fileName = [fileWrapper filename];
          
          if (fileName != nil)
            {
              // Try to set the image to the file wrapper content
              icon = [[NSImage alloc] initByReferencingFile: fileName];
            }
          if (icon == nil)
            icon = RETAIN([fileWrapper icon]);
          
          [(NSTextAttachmentCell*)_cell setImage: icon];
          RELEASE(icon);
        }
    }
}

- (NSFileWrapper *)fileWrapper
{
  return _fileWrapper;
}

- (id <NSTextAttachmentCell>)attachmentCell
{
  return _cell;
}

- (void)setAttachmentCell: (id <NSTextAttachmentCell>)cell
{
  ASSIGN(_cell, cell);
  _taflags.cell_explicitly_set = 1;
  if ([_cell respondsToSelector: @selector(setAttachment:)] == YES)
    {
      [_cell setAttachment: self];
    }
}

/*
 * NSCoding protocol
 */
- (void) encodeWithCoder: (NSCoder*)aCoder
{
  if ([aCoder allowsKeyedCoding])
    {
      [aCoder encodeObject: [self fileWrapper] forKey: @"NSFileWrapper"];
      if (_cell != nil)
        {
          [aCoder encodeObject: _cell forKey: @"NSCell"];
        }
    }
  else
    {
      [aCoder encodeObject: _fileWrapper];
      [aCoder encodeObject: _cell];
    }
}

- (id) initWithCoder: (NSCoder*)aDecoder
{
  if ([aDecoder allowsKeyedCoding])
    {
      [self setFileWrapper: [aDecoder decodeObjectForKey: @"NSFileWrapper"]];
      [self setAttachmentCell: [aDecoder decodeObjectForKey: @"NSCell"]];
    }
  else
    {
      [aDecoder decodeValueOfObjCType: @encode(id) at: &_fileWrapper];
      [aDecoder decodeValueOfObjCType: @encode(id) at: &_cell];
      
      // Reconnect the cell, so the cell does not have to store the attachment
      if ([_cell respondsToSelector: @selector(setAttachment:)] == YES)
	{
	  [_cell setAttachment: self];
	}
    }
  return self;
}

@end