1/*
2    PPDocumentAnimationFileNoticeSheetController.m
3
4    Copyright 2013-2018 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 "PPDocumentAnimationFileNoticeSheetController.h"
26
27
28#define kAnimationFileAlertMessageText                                                      \
29                    @"Warning: This file contains an animation with multiple images, "      \
30                    "however, only the first image was loaded, because PikoPixel "          \
31                    "currently only supports single-image editing.\n\nIf this image is "    \
32                    "later saved at the current file's location, the unloaded animation "   \
33                    "images will be overwritten (removed).\n\nTo preserve the animation "   \
34                    "in the original file, choose a new save location for the edited image."
35
36#define kAnimationFileAlertInformativeText              @""
37
38#define kAnimationFileAlertDefaultButtonText            @"Choose a new save location..."
39#define kAnimationFileAlertAlternateButtonText          @"Use the current save location"
40
41#define kChooseNewSaveLocationReturnCode                NSAlertDefaultReturn
42
43
44@interface PPDocumentAnimationFileNoticeSheetController (PrivateMethods)
45
46- initWithDelegate: (id) delegate;
47
48- (bool) beginAlertModalForWindow: (NSWindow *) window;
49
50- (void) notifyDelegateSheetDidFinishAndShouldChangeSaveLocation:
51                                                            (bool) shouldChangeSaveLocation;
52
53@end
54
55@implementation PPDocumentAnimationFileNoticeSheetController
56
57+ (bool) beginAnimationFileNoticeSheetForDocumentWindow: (NSWindow *) window
58            delegate: (id) delegate
59{
60    PPDocumentAnimationFileNoticeSheetController *controller;
61
62    controller = [[[self alloc] initWithDelegate: delegate] autorelease];
63
64    if (![controller beginAlertModalForWindow: window])
65    {
66        goto ERROR;
67    }
68
69    return YES;
70
71ERROR:
72    return NO;
73}
74
75- initWithDelegate: (id) delegate
76{
77    NSAlert *alert;
78
79    self = [super init];
80
81    if (!self)
82        goto ERROR;
83
84    if (!delegate)
85        goto ERROR;
86
87    alert = [NSAlert alertWithMessageText: kAnimationFileAlertMessageText
88                        defaultButton: kAnimationFileAlertDefaultButtonText
89                        alternateButton: kAnimationFileAlertAlternateButtonText
90                        otherButton: @""
91                        informativeTextWithFormat: kAnimationFileAlertInformativeText];
92
93    if (!alert)
94        goto ERROR;
95
96    _delegate = delegate;
97    _alert = [alert retain];
98
99    return self;
100
101ERROR:
102    [self release];
103
104    return nil;
105}
106
107- init
108{
109    return [self initWithDelegate: nil];
110}
111
112- (void) dealloc
113{
114    [_alert release];
115
116    [super dealloc];
117}
118
119#pragma mark NSAlert sheet modal
120
121- (bool) beginAlertModalForWindow: (NSWindow *) window;
122{
123    if (![window isVisible] || [[_alert window] isVisible])
124    {
125        goto ERROR;
126    }
127
128    [_alert beginSheetModalForWindow: window
129            modalDelegate: self
130            didEndSelector: @selector(alertDidEnd:returnCode:contextInfo:)
131            contextInfo: NULL];
132
133    [self retain];
134
135    return YES;
136
137ERROR:
138    return NO;
139}
140
141- (void) alertDidEnd: (NSAlert *) alert
142            returnCode: (int) returnCode
143            contextInfo: (void *) contextInfo
144{
145    bool shouldChangeSaveLocation = (returnCode == kChooseNewSaveLocationReturnCode) ? YES : NO;
146
147    [self notifyDelegateSheetDidFinishAndShouldChangeSaveLocation: shouldChangeSaveLocation];
148
149    [self autorelease];
150}
151
152#pragma mark Delegate notifiers
153
154- (void) notifyDelegateSheetDidFinishAndShouldChangeSaveLocation:
155                                                            (bool) shouldChangeSaveLocation
156{
157    if ([_delegate respondsToSelector:
158                    @selector(
159                        documentAnimationFileNoticeSheetDidFinishAndShouldChangeSaveLocation:)])
160    {
161        [_delegate documentAnimationFileNoticeSheetDidFinishAndShouldChangeSaveLocation:
162                                                                    shouldChangeSaveLocation];
163    }
164}
165
166@end
167