1/*
2    PPDocumentWindowController_Sheets.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 "PPDocumentWindowController_Sheets.h"
26
27#import "PPDocument.h"
28#import "PPCanvasView.h"
29#import "PPDocumentSizeSheetController.h"
30#import "PPDocumentScaleSheetController.h"
31#import "PPDocumentResizeSheetController.h"
32#import "PPDocumentBackgroundSettingsSheetController.h"
33#import "PPDocumentGridSettingsSheetController.h"
34#import "PPDocumentSamplerImagesSettingsSheetController.h"
35#import "PPDocumentAnimationFileNoticeSheetController.h"
36#import "PPDocumentFlattenedSaveNoticeSheetController.h"
37#import "PPBackgroundPattern.h"
38#import "NSObject_PPUtilities.h"
39
40
41@interface PPDocumentWindowController (SheetsPrivateMethods)
42
43- (void) ppCloseDocumentWithMessage: (NSString *) message;
44
45- (void) ppDocumentCloseAlertSheetDidEnd: (NSAlert *) alert
46            returnCode: (NSInteger) returnCode
47            contextInfo: (void *) contextInfo;
48
49@end
50
51@implementation PPDocumentWindowController (Sheets)
52
53- (void) beginSizeSheet
54{
55    [PPDocumentSizeSheetController beginSizeSheetForDocumentWindow: [self window]
56                                                        delegate: self];
57}
58
59- (void) beginScaleSheet
60{
61    [PPDocumentScaleSheetController beginScaleSheetForDocumentWindow: [self window]
62                                        canvasBitmap: [_ppDocument mergedVisibleLayersBitmap]
63                                        delegate: self];
64}
65
66- (void) beginResizeSheet
67{
68    [PPDocumentResizeSheetController beginResizeSheetForDocumentWindow: [self window]
69                                        currentImageSize: [_ppDocument canvasSize]
70                                        delegate: self];
71}
72
73- (void) beginBackgroundSettingsSheet
74{
75    [PPDocumentBackgroundSettingsSheetController
76                                beginBackgroundSettingsSheetForDocumentWindow: [self window]
77                                backgroundPattern: [_ppDocument backgroundPattern]
78                                backgroundImage: [_ppDocument backgroundImage]
79                                backgroundImageVisibility:
80                                                    [_ppDocument shouldDisplayBackgroundImage]
81                                backgroundImageSmoothing:
82                                                    [_ppDocument shouldSmoothenBackgroundImage]
83                                delegate: self];
84}
85
86- (void) beginGridSettingsSheet
87{
88    [PPDocumentGridSettingsSheetController
89                                    beginGridSettingsSheetForDocumentWindow: [self window]
90                                    gridPattern: [_ppDocument gridPattern]
91                                    gridVisibility: [_ppDocument shouldDisplayGrid]
92                                    delegate: self];
93}
94
95- (void) beginSamplerImagesSettingsSheet
96{
97    [PPDocumentSamplerImagesSettingsSheetController
98                                beginSamplerImagesSettingsSheetForWindow: [self window]
99                                samplerImages: [_ppDocument samplerImages]
100                                delegate: self];
101}
102
103- (void) beginAnimationFileNoticeSheet
104{
105    [PPDocumentAnimationFileNoticeSheetController
106                                beginAnimationFileNoticeSheetForDocumentWindow: [self window]
107                                delegate: self];
108}
109
110- (void) beginFlattenedSaveNoticeSheet
111{
112    [PPDocumentFlattenedSaveNoticeSheetController
113                            beginFlattenedSaveNoticeSheetForDocumentWindow: [self window]];
114}
115
116#pragma mark PPDocumentSizeSheetController delegate methods
117
118- (void) documentSizeSheetDidFinishWithWidth: (int) width
119            andHeight: (int) height
120{
121    if (![_ppDocument setupNewPPDocumentWithCanvasSize: NSMakeSize(width, height)])
122    {
123        goto ERROR;
124    }
125
126    return;
127
128ERROR:
129    [self ppCloseDocumentWithMessage: @"ERROR: Could not create document."];
130}
131
132- (void) documentSizeSheetDidCancel
133{
134    [_ppDocument close];
135}
136
137#pragma mark PPDocumentScaleSheetController delegate methods
138
139- (void) documentScaleSheetDidFinishWithNewImageSize: (NSSize) newImageSize
140{
141    [_ppDocument resizeToSize: newImageSize shouldScale: YES];
142}
143
144- (void) documentScaleSheetDidCancel
145{
146}
147
148#pragma mark PPDocumentResizeSheetController delegate methods
149
150- (void) documentResizeSheetDidFinishWithNewImageSize: (NSSize) newImageSize
151            shouldScale: (bool) shouldScale
152{
153    [_ppDocument resizeToSize: newImageSize shouldScale: shouldScale];
154}
155
156- (void) documentResizeSheetDidCancel
157{
158}
159
160#pragma mark PPDocumentBackgroundSettingsSheetController delegate methods
161
162- (void) backgroundSettingsSheetDidUpdatePattern: (PPBackgroundPattern *) backgroundPattern
163{
164    [_canvasView setBackgroundColor: [backgroundPattern patternFillColor]];
165}
166
167- (void) backgroundSettingsSheetDidUpdateImage: (NSImage *) backgroundImage
168{
169    [_canvasView setBackgroundImage: backgroundImage];
170}
171
172- (void) backgroundSettingsSheetDidUpdateImageVisibility: (bool) shouldDisplayImage
173{
174    [_canvasView setBackgroundImageVisibility: shouldDisplayImage];
175}
176
177- (void) backgroundSettingsSheetDidUpdateImageSmoothing: (bool) shouldSmoothenImage
178{
179    [_canvasView setBackgroundImageSmoothing: shouldSmoothenImage];
180}
181
182- (void) backgroundSettingsSheetDidFinishWithBackgroundPattern:
183                                                    (PPBackgroundPattern *) backgroundPattern
184            backgroundImage: (NSImage *) backgroundImage
185            shouldDisplayImage: (bool) shouldDisplayImage
186            shouldSmoothenImage: (bool) shouldSmoothenImage
187{
188    [_ppDocument setBackgroundPattern: backgroundPattern
189                    backgroundImage: backgroundImage
190                    shouldDisplayBackgroundImage: shouldDisplayImage
191                    shouldSmoothenBackgroundImage: shouldSmoothenImage];
192
193    // compressed background image data is not automatically set up by setBackgroundPattern:...
194    // don't need to manually update it here, but doing so saves time during the next autosave
195
196    if (backgroundImage)
197    {
198        [_ppDocument setupCompressedBackgroundImageData];
199    }
200}
201
202- (void) backgroundSettingsSheetDidCancel
203{
204    [_canvasView setBackgroundImage: [_ppDocument backgroundImage]
205                    backgroundImageVisibility: [_ppDocument shouldDisplayBackgroundImage]
206                    backgroundImageSmoothing: [_ppDocument shouldSmoothenBackgroundImage]
207                    backgroundColor: [_ppDocument backgroundPatternAsColor]];
208}
209
210#pragma mark PPDocumentGridSettingsSheetController delegate methods
211
212- (void) gridSettingsSheetDidUpdateGridPattern: (PPGridPattern *) gridPattern
213                            andVisibility: (bool) shouldDisplayGrid
214{
215    [_canvasView setGridPattern: gridPattern
216                    gridVisibility: shouldDisplayGrid];
217}
218
219- (void) gridSettingsSheetDidFinishWithGridPattern: (PPGridPattern *) gridPattern
220                                andVisibility: (bool) shouldDisplayGrid
221{
222    [_ppDocument setGridPattern: gridPattern
223                    shouldDisplayGrid: shouldDisplayGrid];
224}
225
226- (void) gridSettingsSheetDidCancel
227{
228    [_canvasView setGridPattern: [_ppDocument gridPattern]
229                    gridVisibility: [_ppDocument shouldDisplayGrid]];
230}
231
232#pragma mark PPDocumentSamplerImagesSettingsSheetController delegate methods
233
234- (void) samplerImagesSettingsSheetDidFinishWithSamplerImages: (NSArray *) samplerImages
235{
236    [_ppDocument setSamplerImages: samplerImages];
237}
238
239- (void) samplerImagesSettingsSheetDidCancel
240{
241}
242
243#pragma mark PPDocumentAnimationFileNoticeSheetController delegate methods
244
245- (void) documentAnimationFileNoticeSheetDidFinishAndShouldChangeSaveLocation:
246                                                            (bool) shouldChangeSaveLocation
247{
248    if (shouldChangeSaveLocation)
249    {
250        [_ppDocument ppPerformSelectorFromNewStackFrame: @selector(saveDocumentAs:)];
251    }
252}
253
254#pragma mark Private methods
255
256- (void) ppCloseDocumentWithMessage: (NSString *) message
257{
258    NSAlert *closeAlert;
259
260    if (!message)
261        goto ERROR;
262
263    closeAlert = [NSAlert alertWithMessageText: message
264                            defaultButton: @"OK"
265                            alternateButton: nil
266                            otherButton: nil
267                            informativeTextWithFormat: @""];
268
269    if (!closeAlert)
270        goto ERROR;
271
272    [closeAlert beginSheetModalForWindow: [self window]
273                modalDelegate: self
274                didEndSelector:
275                            @selector(ppDocumentCloseAlertSheetDidEnd:returnCode:contextInfo:)
276                contextInfo: NULL];
277
278    return;
279
280ERROR:
281    [_ppDocument close];
282}
283
284- (void) ppDocumentCloseAlertSheetDidEnd: (NSAlert *) alert
285            returnCode: (NSInteger) returnCode
286            contextInfo: (void *) contextInfo
287{
288    [[alert window] orderOut: self];
289
290    [_ppDocument close];
291}
292
293@end
294