File: NSPathControl.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (575 lines) | stat: -rw-r--r-- 14,008 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* Implementation of class NSPathControl
   Copyright (C) 2020 Free Software Foundation, Inc.
   
   By: Gregory John Casamento
   Date: Wed Apr 22 18:19:40 EDT 2020

   This file is part of the GNUstep 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.1 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; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
*/

#import <Foundation/NSNotification.h>

#import "AppKit/NSPathControl.h"
#import "AppKit/NSPathCell.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSDragging.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSMenu.h"
#import "AppKit/NSOpenPanel.h"
#import "AppKit/NSPathComponentCell.h"
#import "AppKit/NSPathControlItem.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSTrackingArea.h"

static NSNotificationCenter *nc = nil;
static Class pathCellClass;

@interface NSPathCell (PathControlPrivate)
- (void) _setClickedPathComponentCell: (NSPathComponentCell *)c;
@end

@interface NSPathComponentCell (PathControlPrivate)
- (NSPathControlItem *) _pathControlItem;
@end

@implementation NSPathControl

+ (void) initialize
{
  if (self == [NSPathControl class])
    {
      [self setVersion: 1.0];
      [self setCellClass: [NSPathCell class]];
      nc = [NSNotificationCenter defaultCenter];
    }
}


+ (Class) cellClass
{
  return pathCellClass;
}

+ (void) setCellClass: (Class)classId
{
  pathCellClass = classId;
}

- (void) resetCursorRects
{
  [[self superview] removeTrackingRect: _trackingTag];
  _trackingTag = [[self superview] addTrackingRect: [self frame]
                                             owner: self
                                          userData: nil
                                      assumeInside: YES];
}

- (instancetype) init
{
  self = [super init];
  if (self != nil)
    {
      [self setPathStyle: NSPathStyleStandard];
      [self setURL: nil];
      [self setDelegate: nil];
      [self setAllowedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
    }
  return self;
}

- (void) dealloc
{
  [[self superview] removeTrackingRect: _trackingTag];
  [super dealloc];
}

- (void) mouseEntered: (NSEvent *)event
{
  [_cell mouseEntered: event
            withFrame: [self frame]
               inView: self];
}

- (void) mouseExited: (NSEvent *)event
{
  [_cell mouseExited: event
           withFrame: [self frame]
              inView: self];
}

- (void) setPathStyle: (NSPathStyle)style
{
  [_cell setPathStyle: style];
  [self setNeedsDisplay];
}

- (NSPathStyle) pathStyle
{
  return [_cell pathStyle];
}

- (NSPathComponentCell *) clickedPathComponentCell
{
  return [_cell clickedPathComponentCell];
}

- (NSArray *) pathComponentCells
{
  return [_cell pathComponentCells];
}

- (void) setPathComponentCells: (NSArray *)cells
{
  [_cell setPathComponentCells: cells];
  [self setNeedsDisplay];
}

- (SEL) doubleAction
{
  return [_cell doubleAction];
}

- (void) setDoubleAction: (SEL)doubleAction
{
  [_cell setDoubleAction: doubleAction];
}

- (void) _createPathItems
{
  NSArray *a = [_cell pathComponentCells];
  NSEnumerator *en = [a objectEnumerator];
  NSPathComponentCell *c = nil;
  NSMutableArray *items = [NSMutableArray arrayWithCapacity: [a count]];
  
  while ((c = [en nextObject]) != nil)
    {
      NSPathControlItem *pi = [c _pathControlItem];
      [items addObject: pi];
    }

  [self setPathItems: [items copy]];
}

- (NSURL *) URL
{
  return [_cell URL];
}

- (void) setURL: (NSURL *)url
{
  [_cell setURL: url];
  [self _createPathItems];
  [self setNeedsDisplay];
}

- (id<NSPathControlDelegate>) delegate
{
  return _delegate;
}

- (void) setDelegate: (id<NSPathControlDelegate>) delegate
{
  _delegate = delegate;
}

- (NSDragOperation) draggingSourceOperationMaskForLocal: (BOOL)flag
{
  if (flag)
    {
      return _localMask;
    }

  return _remoteMask;
}

- (void) setDraggingSourceOperationMask: (NSDragOperation)mask 
                               forLocal: (BOOL)local
{
  if (local)
    {
      _localMask = mask;
    }
  else
    {
      _remoteMask = mask;
    }
}

- (NSArray *) allowedTypes
{
  return [_cell allowedTypes];
}

- (void) setAllowedTypes: (NSArray *)allowedTypes
{
  [_cell setAllowedTypes: allowedTypes];
  [self registerForDraggedTypes: allowedTypes];
}

- (NSPathControlItem *) clickedPathItem
{
  return [[self clickedPathComponentCell] _pathControlItem];
}

- (NSArray *) pathItems
{
  return _pathItems;
}

- (void) setPathItems: (NSArray *)items
{
  NSEnumerator *en = [items objectEnumerator];
  NSMutableArray *array = [NSMutableArray arrayWithCapacity: [items count]];
  NSPathControlItem *item = nil;
  
  while ((item = [en nextObject]) != nil)
    {
      NSPathComponentCell *cell = [[NSPathComponentCell alloc] init];  
      [cell setImage: [item image]];
      [cell setURL: [item URL]];
      [array addObject: cell];
    }

  [self setPathComponentCells: array];
  ASSIGNCOPY(_pathItems, items);
  [self setNeedsDisplay];
}

- (NSAttributedString *) placeholderAttributedString
{
  return [_cell placeholderAttributedString];
}

- (void) setPlaceholderAttributedString: (NSAttributedString *)string
{
  [_cell setPlaceholderAttributedString: string];
  [self setNeedsDisplay];
}

- (NSString *) placeholderString
{
  return [_cell placeholderString];
}

- (void) setPlaceholderString: (NSString *)string
{
  [_cell setPlaceholderString: string];
  [self setNeedsDisplay];
}

- (NSColor *) backgroundColor
{
  return [_cell backgroundColor];
}

- (void) setBackgroundColor: (NSColor *)color
{
  [_cell setBackgroundColor: color];
  [self setNeedsDisplay];
}

- (void) _doMenuAction: (id)sender
{
  NSPathComponentCell *cc = (NSPathComponentCell *)[sender representedObject];

  [_cell _setClickedPathComponentCell: cc];
  if ([[self cell] action])
    {
      [self sendAction: [[self cell] action]
                    to: [[self cell] target]];
    }
  
  [[sender menu] close];
}

- (void) _doChooseMenuAction: (id)sender
{
  NSOpenPanel *op = [NSOpenPanel openPanel];
  int result = 0;
  NSFileManager *fm = [NSFileManager defaultManager];
  BOOL isDir = NO;
  NSString *path = nil;
  NSString *file = nil;
  
  [fm fileExistsAtPath: [[self URL] path]
           isDirectory: &isDir];

  if (isDir)
    {
      path = [[self URL] path];
    }
  else
    {
      path = [[[self URL] path] stringByDeletingLastPathComponent];
      file = [[[self URL] path] lastPathComponent];
    }
  
  [op setAllowsMultipleSelection: NO];
  [op setCanChooseFiles: YES];
  [op setCanChooseDirectories: YES];
  
  if ([(id)_delegate respondsToSelector: @selector(pathCell:willPopUpMenu:)])
    {
      [_delegate pathControl: self
        willDisplayOpenPanel: op];
    }

  if ([(id)[_cell delegate] respondsToSelector: @selector(pathCell:willPopUpMenu:)])
    {
      [[_cell delegate] pathCell: _cell
            willDisplayOpenPanel: op];
    }
  
  result = [op runModalForDirectory: path
                               file: file
                              types: nil];
  if (result == NSOKButton)
    {
      NSArray *urls = [op URLs];
      NSURL *url = [urls objectAtIndex: 0];
      [self setURL: url];
    }

  [[sender menu] close];
}

- (void) mouseDown: (NSEvent *)event
{
  if (![self isEnabled])
    {
      [super mouseDown: event];
      return;
    }

  if ([self pathStyle] == NSPathStylePopUp)
    {
      NSPathCell *acell = (NSPathCell *)[self cell];
      NSArray *array = [acell pathComponentCells];
      NSMenu *menu = AUTORELEASE([[NSMenu alloc] initWithTitle: nil]);
      NSPathComponentCell *c = nil;
      NSEnumerator *en = [array objectEnumerator];
      
      while ((c = [en nextObject]) != nil)
        {
          NSURL *u = [c URL];
          NSString *s = [[u path] lastPathComponent];
          NSMenuItem *i = [[NSMenuItem alloc] init];

          [i setTitle: s];
          [i setTarget: self];
          [i setAction: @selector(_doMenuAction:)];
          [i setRepresentedObject: c];
          
          [menu insertItem: i
                   atIndex: 0]; 
          RELEASE(i);
        }

      // Add separator
      [menu insertItem: [NSMenuItem separatorItem]
               atIndex: 0];

      // Add choose menu option
      NSMenuItem *i = [[NSMenuItem alloc] init];
      [i setTitle: _(@"Choose...")];
      [i setTarget: self];
      [i setAction: @selector(_doChooseMenuAction:)];
      [menu insertItem: i
               atIndex: 0];
      RELEASE(i);

      [self setMenu: menu];
      
      if (_delegate)
        {
          if ([(id)_delegate respondsToSelector: @selector(pathControl:willPopUpMenu:)])
            {
              [_delegate pathControl: self
                       willPopUpMenu: menu];
            }
        }
      
      if ([_cell delegate])
        {
          if ([(id)[_cell delegate] respondsToSelector: @selector(pathCell:willPopUpMenu:)])
            {
              [[_cell delegate] pathCell: _cell
                           willPopUpMenu: menu];
            }
        }

      
      [menu popUpMenuPositionItem: [menu itemAtIndex: 0]
                       atLocation: NSMakePoint(0.0, 0.0)
                           inView: self];
    }
  else
    {
      NSPathComponentCell *pcc = [_cell pathComponentCellAtPoint: [event locationInWindow]
                                                       withFrame: [self frame]
                                                          inView: self];
      [_cell _setClickedPathComponentCell: pcc];
      if ([[self cell] action])
        {
          [self sendAction: [[self cell] action]
                        to: [[self cell] target]];
        }
    }
}

- (NSDragOperation) draggingEntered: (id<NSDraggingInfo>)sender
{
  if (_delegate != nil)
    {
      NSDragOperation dop = [_delegate pathControl: self
                                      validateDrop: sender];
      return dop;
    }
  
  return NSDragOperationCopy;
}

- (BOOL) performDragOperation: (id<NSDraggingInfo>)sender
{
  NSPasteboard *pb = [sender draggingPasteboard];
  if ([[pb types] containsObject: NSFilenamesPboardType])
    {
      NSArray *files = [pb propertyListForType: NSFilenamesPboardType];
      if ([files count] > 0)
        {
          NSString *file = [files objectAtIndex: 0];
          NSURL *u = [NSURL URLWithString: file];
          BOOL accept = NO;

          if ([self delegate])
            {
              accept = [_delegate pathControl: self
                                   acceptDrop: sender];
            }
          else
            {
              accept = YES;
            }
          
          if (accept)
            {
              [self setURL: u];
            }
        }
    }
  return YES;
}

- (instancetype) initWithCoder: (NSKeyedUnarchiver *)coder
{
  self = [super initWithCoder: coder];
  if (self != nil)
    {
      if ([coder allowsKeyedCoding])
        {
          if ([coder containsValueForKey: @"NSBackgroundColor"])
            {
              [self setBackgroundColor: [coder decodeObjectForKey: @"NSBackgroundColor"]];
            }
          else
            {
              [self setBackgroundColor: [NSColor windowBackgroundColor]];
            }

          if ([coder containsValueForKey: @"NSDragTypes"])
            {
              [self setAllowedTypes: [coder decodeObjectForKey: @"NSDragTypes"]];
            }
          else
            {
              [self setAllowedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
            }
          
          if ([coder containsValueForKey: @"NSControlAction"])
            {
              NSString *s = [coder decodeObjectForKey: @"NSControlAction"];
              [self setAction: NSSelectorFromString(s)];
            }
          if ([coder containsValueForKey: @"NSControlTarget"])
            {
              id t = [coder decodeObjectForKey: @"NSControlTarget"];
              [self setTarget: t];
            }
        }
      else
        {
          [self setBackgroundColor: [coder decodeObject]];
          [self setAllowedTypes: [coder decodeObject]];
          [self setAction: NSSelectorFromString([coder decodeObject])];
          [self setTarget: [coder decodeObject]];
        }
    }
  return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
  [super encodeWithCoder: coder];
  if ([coder allowsKeyedCoding])
    {
      [coder encodeObject: [self backgroundColor]
                   forKey: @"NSBackgroundColor"];
      [coder encodeObject: [self allowedTypes]
                   forKey: @"NSDragTypes"];
      [coder encodeObject: NSStringFromSelector([self action])
                   forKey: @"NSControlAction"];
      [coder encodeObject: [self target]
                   forKey: @"NSControlTarget"];
    }
  else
    {
      [coder encodeObject: [self backgroundColor]];
      [coder encodeObject: [self allowedTypes]];
      [coder encodeObject: NSStringFromSelector([self action])];
      [coder encodeObject: [self target]];
    }  
}

@end

@implementation NSPathCell (PathControlPrivate)

- (void) _setClickedPathComponentCell: (NSPathComponentCell *)c
{
  _clickedPathComponentCell = c;
}

@end

@implementation NSPathComponentCell (PathControlPrivate)

- (NSPathControlItem *) _pathControlItem
{
  NSPathControlItem *pi = [[NSPathControlItem alloc] init];
  NSURL *u = [self URL];
  NSString *path = [u path];
  
  [pi setImage: [self image]];
  [pi setURL: u];
  [pi setTitle: [path lastPathComponent]];
  AUTORELEASE(pi);
  
  return pi;
}

@end