libs-gui/Source/NSCollectionViewFlowLayout.m

440 lines
12 KiB
Mathematica
Raw Normal View History

2021-05-30 08:53:03 +00:00
/* Implementation of class NSCollectionViewFlowLayout
Copyright (C) 2021 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: 30-05-2021
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/NSIndexPath.h>
2021-05-30 08:53:03 +00:00
#import "AppKit/NSCollectionViewFlowLayout.h"
#import "AppKit/NSCollectionViewItem.h"
2021-05-30 08:53:03 +00:00
2022-07-15 11:11:13 +00:00
#import "GSGuiPrivate.h"
Add methods and actions for layout classes as well as the abstract classes for NSCollectionViewLayout. Declarations added to collectionview layout header. Clean compile for NSCollectionViewLayout abstract class Add header for NSCollectionViewFlowLayout.h Add skeleton implementation of NSCollectionViewFlowLayout Add implementation for simple setter/getter methods. Correct compilation error with non-clang compilers Fix compilation issues Add getter/setter for collectionViewLayout Skeleton of NSCollectionViewTransitionLayout implementation Add encoding/decoding for collectionViewLayout key Make NSCollectionViewGridLayout default if the NSCollectionViewLayoutKey is not set Add protocols for DataSource and Prefetching for NSCollectionView Add 10.11 methods to delegate declaration remove templating from NSCollectionViewDelegate that gcc can't handle. New delegate methods for NSCollectionViewDelegate to support NSSets of NSIndexPaths and multiple selection. Reset .travis.yml to the proper version and modify NSCollectionView.h to use forward declarations for both NSPasteboard and NSPasteboardWriting so that we don't explicitly import. Add declarations for missing methods from 10.11 version of NSCollectionView Add new methods for 10.11 for moving, deleting and collapsing sections. Add new methods from 10.11 Add the remaining missing methods as well as delcarations for blocks needed for batch handler and completion handler. Add skeleton implementations for all new methods Changes to add new methods to collection view for delegate Consolidate the registeredClass and registeredNib maps. Implementation of dataSource related methods Implment nib loading for NSCollectionViewItem subclasses Remove uneeded ivars, add categories for override and layout Minor formatting change Update categories and organization of code Add init method to NSCollectionViewFlowLayoutInvalidationContext Add collectionview layout subclasses Declarations added to collectionview layout header. Clean compile for NSCollectionViewLayout abstract class Add skeleton implementation of NSCollectionViewFlowLayout Add implementation for simple setter/getter methods. Correct compilation error with non-clang compilers Fix compilation issues Add getter/setter for collectionViewLayout Add encoding/decoding for collectionViewLayout key Make NSCollectionViewGridLayout default if the NSCollectionViewLayoutKey is not set Add protocols for DataSource and Prefetching for NSCollectionView New delegate methods for NSCollectionViewDelegate to support NSSets of NSIndexPaths and multiple selection. Reset .travis.yml to the proper version and modify NSCollectionView.h to use forward declarations for both NSPasteboard and NSPasteboardWriting so that we don't explicitly import. Add declarations for missing methods from 10.11 version of NSCollectionView Add new methods for 10.11 for moving, deleting and collapsing sections. Add new methods from 10.11 Changes to add new methods to collection view for delegate Consolidate the registeredClass and registeredNib maps. Add init method to NSCollectionViewFlowLayoutInvalidationContext Fix issues with rebase
2021-05-31 11:47:58 +00:00
@implementation NSCollectionViewFlowLayoutInvalidationContext
- (instancetype) init
{
self = [super init];
if (self != nil)
{
_invalidateFlowLayoutAttributes = YES;
_invalidateFlowLayoutDelegateMetrics = YES;
}
return self;
}
- (void) setInvalidateFlowLayoutDelegateMetrics: (BOOL)flag
{
_invalidateFlowLayoutDelegateMetrics = flag;
}
- (BOOL) invalidateFlowLayoutDelegateMetrics
{
return _invalidateFlowLayoutDelegateMetrics;
}
- (void) setInvalidateFlowLayoutAttributes: (BOOL)flag
{
_invalidateFlowLayoutAttributes = flag;
}
- (BOOL) invalidateFlowLayoutAttributes
{
return _invalidateFlowLayoutAttributes;
}
@end
2021-05-30 08:53:03 +00:00
@implementation NSCollectionViewFlowLayout
- (id) initWithCoder: (NSCoder *)coder
{
self = [super initWithCoder: coder];
if (self)
{
if ([coder allowsKeyedCoding])
{
2022-07-14 15:17:39 +00:00
if ([coder containsValueForKey: @"NSMinimumLineSpacing"])
{
_minimumLineSpacing = [coder decodeFloatForKey: @"NSMinimumLineSpacing"];
}
if ([coder containsValueForKey: @"NSMinimumInteritemSpacing"])
{
_minimumInteritemSpacing = [coder decodeFloatForKey: @"NSMinimumInteritemSpacing"];
}
if ([coder containsValueForKey: @"NSItemSize"])
{
_itemSize = [coder decodeSizeForKey: @"NSItemSize"];
}
if ([coder containsValueForKey: @"NSEstimatedItemSize"])
{
_estimatedItemSize = [coder decodeSizeForKey: @"NSEstimatedItemSize"];
}
if ([coder containsValueForKey: @"NSScrollDirection"])
{
_scrollDirection = [coder decodeIntForKey: @"NSScrollDirection"];
}
if ([coder containsValueForKey: @"NSHeaderReferenceSize"])
{
_headerReferenceSize = [coder decodeSizeForKey: @"NSHeaderReferneceSize"];
}
if ([coder containsValueForKey: @"NSFooterReferenceSize"])
{
_footerReferenceSize = [coder decodeSizeForKey: @"NSFooterReferenceSize"];
}
// decode inset...
if ([coder containsValueForKey: @"NSSectionInset.bottom"])
{
_sectionInset.bottom = [coder decodeFloatForKey: @"NSSectionInset.bottom"];
}
if ([coder containsValueForKey: @"NSSectionInset.top"])
{
_sectionInset.top = [coder decodeFloatForKey: @"NSSectionInset.top"];
}
if ([coder containsValueForKey: @"NSSectionInset.left"])
{
_sectionInset.left = [coder decodeFloatForKey: @"NSSectionInset.left"];
}
if ([coder containsValueForKey: @"NSSectionInset.right"])
{
_sectionInset.right = [coder decodeFloatForKey: @"NSSectionInset.right"];
}
if ([coder containsValueForKey: @"NSSectionHeadersPinToVisibleBounds"])
{
_sectionHeadersPinToVisibleBounds = [coder decodeBoolForKey: @"NSSectionHeadersPinToVisibleBounds"];
}
if ([coder containsValueForKey: @"NSSectionFootersPinToVisibleBounds"])
{
_sectionFootersPinToVisibleBounds = [coder decodeBoolForKey: @"NSSectionFootersPinToVisibleBounds"];
}
}
else
{
2022-07-15 11:34:17 +00:00
[coder decodeValueOfObjCType: @encode(CGFloat)
at: &_minimumInteritemSpacing];
_itemSize = [coder decodeSize];
2022-07-15 11:34:17 +00:00
_estimatedItemSize = [coder decodeSize];
decode_NSInteger(coder, &_scrollDirection);
_headerReferenceSize = [coder decodeSize];
_footerReferenceSize = [coder decodeSize];
// decode inset...
[coder decodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.bottom];
[coder decodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.top];
[coder decodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.left];
[coder decodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.right];
[coder decodeValueOfObjCType: @encode(BOOL)
at: &_sectionHeadersPinToVisibleBounds];
[coder decodeValueOfObjCType: @encode(BOOL)
at: &_sectionFootersPinToVisibleBounds];
}
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
2022-07-14 15:17:39 +00:00
if ([coder allowsKeyedCoding])
{
[coder encodeFloat: _minimumLineSpacing
forKey: @"NSMinimumLineSpacing"];
[coder encodeFloat: _minimumInteritemSpacing
forKey: @"NSMinimumInteritemSpacing"];
[coder encodeSize: _itemSize
forKey: @"NSItemSize"];
[coder encodeSize: _estimatedItemSize
forKey: @"NSEstimatedItemSize"];
[coder encodeInt: _scrollDirection
forKey: @"NSScrollDirection"];
[coder encodeSize: _headerReferenceSize
forKey: @"NSHeaderReferneceSize"];
[coder encodeSize: _footerReferenceSize
forKey: @"NSFooterReferenceSize"];
// decode inset...
[coder encodeFloat: _sectionInset.bottom
forKey: @"NSSectionInset.bottom"];
[coder encodeFloat: _sectionInset.top
forKey: @"NSSectionInset.top"];
[coder encodeFloat: _sectionInset.left
forKey: @"NSSectionInset.left"];
[coder encodeFloat: _sectionInset.right
forKey: @"NSSectionInset.right"];
[coder encodeBool: _sectionHeadersPinToVisibleBounds
forKey: @"NSSectionHeadersPinToVisibleBounds"];
[coder encodeBool: _sectionFootersPinToVisibleBounds
forKey: @"NSSectionFootersPinToVisibleBounds"];
}
else
{
2022-07-15 11:11:13 +00:00
[coder encodeValueOfObjCType: @encode(CGFloat)
at: &_minimumInteritemSpacing];
[coder encodeSize: _itemSize];
[coder encodeSize: _estimatedItemSize];
encode_NSInteger(coder, &_scrollDirection);
[coder encodeSize: _headerReferenceSize];
[coder encodeSize: _footerReferenceSize];
// decode inset...
[coder encodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.bottom];
[coder encodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.top];
[coder encodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.left];
[coder encodeValueOfObjCType: @encode(CGFloat)
at: &_sectionInset.right];
[coder encodeValueOfObjCType: @encode(BOOL)
at: &_sectionHeadersPinToVisibleBounds];
[coder encodeValueOfObjCType: @encode(BOOL)
at: &_sectionFootersPinToVisibleBounds];
2022-07-14 15:17:39 +00:00
}
}
Add methods and actions for layout classes as well as the abstract classes for NSCollectionViewLayout. Declarations added to collectionview layout header. Clean compile for NSCollectionViewLayout abstract class Add header for NSCollectionViewFlowLayout.h Add skeleton implementation of NSCollectionViewFlowLayout Add implementation for simple setter/getter methods. Correct compilation error with non-clang compilers Fix compilation issues Add getter/setter for collectionViewLayout Skeleton of NSCollectionViewTransitionLayout implementation Add encoding/decoding for collectionViewLayout key Make NSCollectionViewGridLayout default if the NSCollectionViewLayoutKey is not set Add protocols for DataSource and Prefetching for NSCollectionView Add 10.11 methods to delegate declaration remove templating from NSCollectionViewDelegate that gcc can't handle. New delegate methods for NSCollectionViewDelegate to support NSSets of NSIndexPaths and multiple selection. Reset .travis.yml to the proper version and modify NSCollectionView.h to use forward declarations for both NSPasteboard and NSPasteboardWriting so that we don't explicitly import. Add declarations for missing methods from 10.11 version of NSCollectionView Add new methods for 10.11 for moving, deleting and collapsing sections. Add new methods from 10.11 Add the remaining missing methods as well as delcarations for blocks needed for batch handler and completion handler. Add skeleton implementations for all new methods Changes to add new methods to collection view for delegate Consolidate the registeredClass and registeredNib maps. Implementation of dataSource related methods Implment nib loading for NSCollectionViewItem subclasses Remove uneeded ivars, add categories for override and layout Minor formatting change Update categories and organization of code Add init method to NSCollectionViewFlowLayoutInvalidationContext Add collectionview layout subclasses Declarations added to collectionview layout header. Clean compile for NSCollectionViewLayout abstract class Add skeleton implementation of NSCollectionViewFlowLayout Add implementation for simple setter/getter methods. Correct compilation error with non-clang compilers Fix compilation issues Add getter/setter for collectionViewLayout Add encoding/decoding for collectionViewLayout key Make NSCollectionViewGridLayout default if the NSCollectionViewLayoutKey is not set Add protocols for DataSource and Prefetching for NSCollectionView New delegate methods for NSCollectionViewDelegate to support NSSets of NSIndexPaths and multiple selection. Reset .travis.yml to the proper version and modify NSCollectionView.h to use forward declarations for both NSPasteboard and NSPasteboardWriting so that we don't explicitly import. Add declarations for missing methods from 10.11 version of NSCollectionView Add new methods for 10.11 for moving, deleting and collapsing sections. Add new methods from 10.11 Changes to add new methods to collection view for delegate Consolidate the registeredClass and registeredNib maps. Add init method to NSCollectionViewFlowLayoutInvalidationContext Fix issues with rebase
2021-05-31 11:47:58 +00:00
- (CGFloat) minimumLineSpacing
{
return _minimumLineSpacing;
}
- (void) setMinimumLineSpacing: (CGFloat)spacing
{
_minimumLineSpacing = spacing;
}
- (CGFloat) minimumInteritemSpacing
{
return _minimumInteritemSpacing;
}
- (void) setMinimumInteritemSpacing: (CGFloat)spacing
{
_minimumInteritemSpacing = spacing;
}
- (NSSize) itemSize
{
return _itemSize;
}
- (void) setItemSize: (NSSize)itemSize
{
_itemSize = itemSize;
}
- (NSSize) estimatedItemSize
{
return _estimatedItemSize;
}
- (void) setEstimatedItemSize: (NSSize)size
{
_estimatedItemSize = size;
}
- (NSCollectionViewScrollDirection) scrollDirection
{
return _scrollDirection;
}
- (void) setScrollDirection: (NSCollectionViewScrollDirection)direction
{
_scrollDirection = direction;
}
- (NSSize) headerReferenceSize
{
return _headerReferenceSize;
}
- (void) setHeaderReferenceSize: (NSSize)size
{
_headerReferenceSize = size;
}
- (NSSize) footerReferenceSize
{
return _footerReferenceSize;
}
- (void) setFooterReferenceSize: (NSSize)size
{
_footerReferenceSize = size;
}
- (NSEdgeInsets) sectionInset
{
return _sectionInset;
}
- (void) setSectionInset: (NSEdgeInsets)inset
{
_sectionInset = inset;
}
- (BOOL) sectionHeadersPinToVisibleBounds
{
return _sectionHeadersPinToVisibleBounds;
}
- (void) setSectionHeadersPinToVisibleBounds: (BOOL)f
{
_sectionHeadersPinToVisibleBounds = f;
}
- (BOOL) sectionFootersPinToVisibleBounds
{
return _sectionFootersPinToVisibleBounds;
}
- (void) setSectionFootersPinToVisibleBounds: (BOOL)f
{
_sectionFootersPinToVisibleBounds = f;
}
- (BOOL) sectionAtIndexIsCollapsed: (NSUInteger)sectionIndex
{
return NO;
}
- (void) collapseSectionAtIndex: (NSUInteger)sectionIndex
{
}
- (void) expandSectionAtIndex: (NSUInteger)sectionIndex
{
}
// Methods to override for specific layouts...
- (void) prepareLayout
{
[super prepareLayout];
}
- (NSArray *) layoutAttributesForElementsInRect: (NSRect)rect
{
return nil;
}
- (NSCollectionViewLayoutAttributes *) layoutAttributesForItemAtIndexPath: (NSIndexPath *)indexPath
{
NSCollectionViewLayoutAttributes *attrs = [[NSCollectionViewLayoutAttributes alloc] init];
NSSize sz = [self itemSize];
NSInteger s = [indexPath section];
NSInteger r = [indexPath item];
CGFloat h = sz.height;
CGFloat w = sz.width;
NSRect f = NSMakeRect(r * w, s * h, h, w);
// NSLog(@"Flow layout for index path = %@", indexPath);
[attrs setFrame: f];
[attrs setHidden: NO];
[attrs setZIndex: 0];
[attrs setSize: sz];
// NSLog(@"f = %@", NSStringFromRect(f));
// NSLog(@"s = %@", NSStringFromSize(sz));
// NSLog(@"self = %@", self);
AUTORELEASE(attrs);
return attrs;
}
- (NSCollectionViewLayoutAttributes *)
layoutAttributesForSupplementaryViewOfKind: (NSCollectionViewSupplementaryElementKind)elementKind
atIndexPath: (NSIndexPath *)indexPath
{
return nil;
}
- (NSCollectionViewLayoutAttributes *)
layoutAttributesForDecorationViewOfKind: (NSCollectionViewDecorationElementKind)elementKind
atIndexPath: (NSIndexPath *)indexPath
{
return nil;
}
- (NSCollectionViewLayoutAttributes *)layoutAttributesForDropTargetAtPoint: (NSPoint)pointInCollectionView
{
return nil;
}
- (NSCollectionViewLayoutAttributes *)layoutAttributesForInterItemGapBeforeIndexPath: (NSIndexPath *)indexPath
{
return nil;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange: (NSRect)newBounds
{
return NO;
}
- (NSCollectionViewLayoutInvalidationContext *)invalidationContextForBoundsChange: (NSRect)newBounds
{
return nil;
}
- (BOOL)shouldInvalidateLayoutForPreferredLayoutAttributes: (NSCollectionViewLayoutAttributes *)preferredAttributes
withOriginalAttributes: (NSCollectionViewLayoutAttributes *)originalAttributes
{
return NO;
}
- (NSCollectionViewLayoutInvalidationContext *)
invalidationContextForPreferredLayoutAttributes: (NSCollectionViewLayoutAttributes *)preferredAttributes
withOriginalAttributes: (NSCollectionViewLayoutAttributes *)originalAttributes
{
return nil;
}
- (NSPoint) targetContentOffsetForProposedContentOffset: (NSPoint)proposedContentOffset
withScrollingVelocity: (NSPoint)velocity
{
return NSZeroPoint;
}
- (NSPoint) targetContentOffsetForProposedContentOffset: (NSPoint)proposedContentOffset
{
return NSZeroPoint;
}
- (NSSize) collectionViewContentSize
{
return [_collectionView frame].size;
}
// end subclassing hooks...
2021-05-30 08:53:03 +00:00
@end