1/*
2    NSWindow_PPUtilities.m
3
4    Copyright 2013-2018,2020 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for Mac OS X and GNUstep.
8    PikoPixel is a graphical application for drawing & editing pixel-art images.
9
10    PikoPixel is free software: you can redistribute it and/or modify it under
11    the terms of the GNU Affero General Public License as published by the
12    Free Software Foundation, either version 3 of the License, or (at your
13    option) any later version approved for PikoPixel by its copyright holder (or
14    an authorized proxy).
15
16    PikoPixel is distributed in the hope that it will be useful, but WITHOUT ANY
17    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18    FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19    details.
20
21    You should have received a copy of the GNU Affero General Public License
22    along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#import "NSWindow_PPUtilities.h"
26
27#import "PPGeometry.h"
28#import "NSBitmapImageRep_PPUtilities.h"
29#import "NSImage_PPUtilities.h"
30
31
32#if !PP_SDK_HAS_NSWINDOWANIMATION
33
34#   define NSWindowAnimationBehaviorNone    2
35
36@interface NSWindow (SetAnimationBehaviorMethodForLegacySDKs)
37
38- (void) setAnimationBehavior: (NSInteger) animationBehavior;
39
40@end
41
42#endif // !PP_SDK_HAS_NSWINDOWANIMATION
43
44@implementation NSWindow (PPUtilities)
45
46- (void) ppMakeKeyWindowIfMain
47{
48    if ([self isMainWindow] && ![self isKeyWindow] && ![NSApp modalWindow])
49    {
50        [self makeKeyWindow];
51    }
52}
53
54- (void) ppSetDocumentWindowTitlebarIcon: (NSImage *) iconImage
55{
56    NSButton *titlebarIconButton;
57    NSSize buttonImageSize;
58    NSBitmapImageRep *buttonBitmap = nil;
59    NSImage *buttonImage = nil;
60
61    titlebarIconButton = [self standardWindowButton: NSWindowDocumentIconButton];
62
63    if (!titlebarIconButton)
64        return;
65
66    buttonImageSize = [titlebarIconButton bounds].size;
67
68    if (iconImage)
69    {
70        buttonBitmap = [NSBitmapImageRep ppImageBitmapOfSize: buttonImageSize];
71    }
72
73    if (buttonBitmap)
74    {
75        NSRect iconImageFrame, buttonBoundsForIconImage;
76
77        iconImageFrame = PPGeometry_OriginRectOfSize([iconImage size]);
78
79        buttonBoundsForIconImage =
80            PPGeometry_ScaledBoundsForFrameOfSizeToFitFrameOfSize(iconImageFrame.size,
81                                                                    buttonImageSize);
82
83        if (!NSIsEmptyRect(buttonBoundsForIconImage))
84        {
85            [buttonBitmap ppSetAsCurrentGraphicsContext];
86
87            [[NSGraphicsContext currentContext]
88                                        setImageInterpolation: NSImageInterpolationHigh];
89
90            [iconImage drawInRect: buttonBoundsForIconImage
91                        fromRect: iconImageFrame
92                        operation: NSCompositeCopy
93                        fraction: 1.0f];
94
95            [buttonBitmap ppRestoreGraphicsContext];
96        }
97
98        buttonImage = [NSImage ppImageWithBitmap: buttonBitmap];
99    }
100
101    [titlebarIconButton setImage: buttonImage];
102}
103
104- (void) ppDisableWindowAnimation
105{
106    static bool needToCheckSetAnimationBehaviorSelector = YES,
107                setAnimationBehaviorSelectorIsSupported = NO;
108
109    if (needToCheckSetAnimationBehaviorSelector)
110    {
111        setAnimationBehaviorSelectorIsSupported =
112            ([NSWindow instancesRespondToSelector: @selector(setAnimationBehavior:)]) ? YES : NO;
113
114        needToCheckSetAnimationBehaviorSelector = NO;
115    }
116
117    if (setAnimationBehaviorSelectorIsSupported)
118    {
119        [self setAnimationBehavior: NSWindowAnimationBehaviorNone];
120    }
121}
122
123@end
124