1/** <title>NSCollectionViewItem</title>
2
3   Copyright (C) 2013 Free Software Foundation, Inc.
4
5   Author: Doug Simons (doug.simons@testplant.com)
6           Frank LeGrand (frank.legrand@testplant.com)
7   Date: February 2013
8
9   This file is part of the GNUstep GUI Library.
10
11   This library is free software; you can redistribute it and/or
12   modify it under the terms of the GNU Lesser 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 library 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   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser General Public
22   License along with this library; see the file COPYING.LIB.
23   If not, see <http://www.gnu.org/licenses/> or write to the
24   Free Software Foundation, 51 Franklin Street, Fifth Floor,
25   Boston, MA 02110-1301, USA.
26*/
27
28#import <Foundation/NSArray.h>
29#import <Foundation/NSDictionary.h>
30#import <Foundation/NSKeyedArchiver.h>
31
32#import "AppKit/NSCollectionView.h"
33#import "AppKit/NSCollectionViewItem.h"
34#import "AppKit/NSImageView.h"
35#import "AppKit/NSKeyValueBinding.h"
36#import "AppKit/NSTextField.h"
37
38@implementation NSCollectionViewItem
39
40- (void) awakeFromNib
41{
42}
43
44- (BOOL) isSelected
45{
46  return _isSelected;
47}
48
49- (void) dealloc
50{
51  DESTROY(textField);
52  DESTROY(imageView);
53  [super dealloc];
54}
55
56- (NSCollectionView *) collectionView
57{
58  return (NSCollectionView *)[[self view] superview];
59}
60
61- (NSArray *) draggingImageComponents
62{
63  // FIXME: We don't have NSDraggingImageComponent
64  return [NSArray array];
65}
66
67- (void) setSelected: (BOOL)flag
68{
69  if (_isSelected != flag)
70    {
71      _isSelected = flag;
72    }
73}
74
75- (id) representedObject
76{
77  return [super representedObject];
78}
79
80- (void) setRepresentedObject: (id)anObject
81{
82  [super setRepresentedObject:anObject];
83  //[textField setStringValue:[self representedObject]];
84}
85
86- (NSTextField *) textField
87{
88  return textField;
89}
90
91- (void) setTextField: (NSTextField *)aTextField
92{
93  ASSIGN(textField, aTextField);
94}
95
96- (NSImageView *) imageView
97{
98  return imageView;
99}
100
101- (void) setImageView: (NSImageView *)anImageView
102{
103  ASSIGN(imageView, anImageView);
104}
105
106- (id) initWithCoder: (NSCoder *)aCoder
107{
108  self = [super initWithCoder: aCoder];
109  if (nil != self)
110    {
111      if (YES == [aCoder allowsKeyedCoding])
112	{
113          if ([aCoder containsValueForKey: @"textField"])
114            {
115              [self setTextField: [aCoder decodeObjectForKey: @"textField"]];
116            }
117          else
118            {
119              textField = [[NSTextField alloc] initWithFrame: NSMakeRect(0.0, 0.0, 100.0, 20.0)];
120            }
121          if ([aCoder containsValueForKey: @"imageView"])
122            {
123              [self setImageView: [aCoder decodeObjectForKey: @"imageView"]];
124            }
125          else
126            {
127              imageView = [[NSImageView alloc] initWithFrame: NSMakeRect(0.0, 0.0, 100.0, 100.0)];
128            }
129	}
130      else
131	{
132	  [self setTextField: [aCoder decodeObject]];
133	  [self setImageView: [aCoder decodeObject]];
134	}
135    }
136  return self;
137}
138
139- (void)encodeWithCoder:(NSCoder *)aCoder
140{
141  [super encodeWithCoder: aCoder];
142  if (YES == [aCoder allowsKeyedCoding])
143    {
144      [aCoder encodeObject: textField forKey: @"textField"];
145      [aCoder encodeObject: imageView forKey: @"imageView"];
146    }
147  else
148    {
149      [aCoder encodeObject: textField];
150      [aCoder encodeObject: imageView];
151    }
152}
153
154- (void) copyBindingsTo: (NSCollectionViewItem*)newItem
155                   from: (NSView*)view
156                   onto: (NSView*)newView
157{
158  NSArray *exposedBindings = [view exposedBindings];
159  NSEnumerator *e = [exposedBindings objectEnumerator];
160  NSString *binding = nil;
161  while ((binding = [e nextObject]) != nil)
162    {
163      NSDictionary *info = [view infoForBinding: binding];
164      if (info != nil)
165        {
166          NSObject *target = [info objectForKey: NSObservedObjectKey];
167          if (target == self)
168            {
169              [newView bind: binding
170                   toObject: newItem
171                withKeyPath: [info objectForKey: NSObservedKeyPathKey]
172                    options: [info objectForKey: NSOptionsKey]];
173            }
174        }
175    }
176
177  NSView *sub1 = nil;
178  NSEnumerator *e1 = [[view subviews] objectEnumerator];
179  NSView *sub2 = nil;
180  NSEnumerator *e2 = [[newView subviews] objectEnumerator];
181  while ((sub1 = [e1 nextObject]) != nil)
182    {
183      sub2 = [e2 nextObject];
184      [self copyBindingsTo: newItem from: sub1 onto: sub2];
185    }
186 }
187
188- (id) copyWithZone: (NSZone *)zone
189{
190  // FIXME: Cache this data, as we need a lot of copies
191  NSData *itemAsData = [NSKeyedArchiver archivedDataWithRootObject: self];
192  NSCollectionViewItem *newItem =
193    [NSKeyedUnarchiver unarchiveObjectWithData: itemAsData];
194
195  // Try to copy bindings too
196  [self copyBindingsTo: newItem from: [self view] onto: [newItem view]];
197
198  return RETAIN(newItem);
199}
200
201@end
202