1/* 2 Project: Sudoku 3 Document.m 4 5 Copyright (C) 2007-2011 The Free Software Foundation, Inc 6 7 Author: Marko Riedel 8 9 This application is free software; you can redistribute it and/or 10 modify it under the terms of the GNU General Public 11 License as published by the Free Software Foundation; either 12 version 3 of the License, or (at your option) any later version. 13 14 This application is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Library General Public License for more details. 18 19 You should have received a copy of the GNU General Public 20 License along with this library; if not, write to the Free 21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22*/ 23 24#import <AppKit/AppKit.h> 25 26#import "SudokuView.h" 27#import "Document.h" 28 29@interface Document (Private) 30 31- (NSWindow*)makeWindow; 32 33@end 34 35@implementation Document 36 37- init 38{ 39 [super init]; 40 41 sdkview = nil; lines = nil; 42 return self; 43} 44 45- (NSData *)dataRepresentationOfType:(NSString *)aType 46{ 47 if([aType isEqualToString:DOCTYPE]) 48 { 49 NSString *all; 50 [[sdkview window] saveFrameUsingName:[self fileName]]; 51 52 all = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n", 53 [[sdkview sudoku] stateToString:FIELD_VALUE], 54 [[sdkview sudoku] stateToString:FIELD_PUZZLE], 55 [[sdkview sudoku] stateToString:FIELD_GUESS], 56 [[sdkview sudoku] stateToString:FIELD_SCORE]]; 57 58 return [all dataUsingEncoding:NSASCIIStringEncoding]; 59 } 60 else 61 { 62 NSString *msg = [NSString stringWithFormat: @"Unknown type: %@", 63 [aType uppercaseString]]; 64 NSRunAlertPanel(@"Alert", msg, @"Ok", nil, nil); 65 return nil; 66 } 67} 68 69- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType 70{ 71 if([aType isEqualToString:DOCTYPE]) 72 { 73 lines = 74 [[NSString stringWithCString:[data bytes] 75 length:[data length]] 76 componentsSeparatedByString:@"\n"]; 77 78 RETAIN(lines); 79 } 80 else 81 { 82 NSString *msg = [NSString stringWithFormat: @"Unknown type: %@", 83 [aType uppercaseString]]; 84 NSRunAlertPanel(@"Alert", msg, @"Ok", nil, nil); 85 return NO; 86 } 87 88 return YES; 89} 90 91- (void) makeWindowControllers 92{ 93 NSWindowController *controller; 94 NSWindow *win = [self makeWindow]; 95 96 controller = [[NSWindowController alloc] initWithWindow: win]; 97 RELEASE (win); 98 [self addWindowController: controller]; 99 RELEASE(controller); 100 101 // We have to do this ourself, as there is currently no nib file 102 // [controller setShouldCascadeWindows:NO]; 103 [self windowControllerDidLoadNib: controller]; 104 105 [win setFrameAutosaveName:[self fileName]]; 106 if([win setFrameUsingName:[self fileName]]==NO){ 107 [win center]; 108 } 109 110 [win orderFrontRegardless]; 111 [win makeKeyWindow]; 112 [win display]; 113} 114 115- (void)windowControllerDidLoadNib:(NSWindowController *)aController; 116{ 117 NSEnumerator *en; 118 119 [super windowControllerDidLoadNib:aController]; 120 121 en = [lines objectEnumerator]; 122 if(lines != nil){ 123 [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_VALUE]; 124 [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_PUZZLE]; 125 [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_GUESS]; 126 [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_SCORE]; 127 128 RELEASE(lines); lines = nil; 129 } 130} 131 132- (Sudoku *)sudoku 133{ 134 return [sdkview sudoku]; 135} 136 137- (SudokuView *)sudokuView 138{ 139 return sdkview; 140} 141 142- resetPuzzle:(id)sender 143{ 144 [sdkview reset]; 145 [self updateChangeCount:NSChangeDone]; 146 147 return self; 148} 149 150- solvePuzzle:(id)sender 151{ 152 [sdkview loadSolution]; 153 [self updateChangeCount:NSChangeDone]; 154 155 return self; 156} 157 158 159@end 160 161@implementation Document (Private) 162 163- (NSWindow*)makeWindow 164{ 165 NSWindow *window; 166 int m = (NSTitledWindowMask | 167 NSClosableWindowMask | 168 NSMiniaturizableWindowMask); 169 170 NSRect frame = {{ 0, 0}, {SDK_DIM, SDK_DIM} }; 171 sdkview = [[SudokuView alloc] initWithFrame:frame]; 172 173 frame = [sdkview frame]; // just in case 174 175 window = 176 [[NSWindow alloc] initWithContentRect:frame 177 styleMask:m 178 backing: NSBackingStoreRetained 179 defer:YES]; 180 [window setDelegate:self]; 181 182 [window setContentView:sdkview]; 183 [window setReleasedWhenClosed:YES]; 184 185 [self setFileType:DOCTYPE]; 186 187 return window; 188} 189 190@end 191