File: Document.m

package info (click to toggle)
sudoku.app 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: objc: 1,728; makefile: 9
file content (190 lines) | stat: -rw-r--r-- 4,457 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* 
   Project: Sudoku
   Document.m

   Copyright (C) 2007-2011 The Free Software Foundation, Inc

   Author: Marko Riedel

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.
 
   This application 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
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#import <AppKit/AppKit.h>

#import "SudokuView.h"
#import "Document.h"

@interface Document (Private)

- (NSWindow*)makeWindow;

@end

@implementation Document

- init
{
  [super init];

  sdkview = nil; lines = nil;
  return self;
}

- (NSData *)dataRepresentationOfType:(NSString *)aType 
{
  if([aType isEqualToString:DOCTYPE])
    {
      NSString *all;
      [[sdkview window] saveFrameUsingName:[self fileName]];

      all =	    [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n",
			      [[sdkview sudoku] stateToString:FIELD_VALUE],
			      [[sdkview sudoku] stateToString:FIELD_PUZZLE],
			      [[sdkview sudoku] stateToString:FIELD_GUESS],
			      [[sdkview sudoku] stateToString:FIELD_SCORE]];

      return [all dataUsingEncoding:NSASCIIStringEncoding];
    }
  else
    {
      NSString *msg = [NSString stringWithFormat: @"Unknown type: %@", 
				[aType uppercaseString]];
      NSRunAlertPanel(@"Alert", msg, @"Ok", nil, nil);
      return nil;
    }
}

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType 
{
  if([aType isEqualToString:DOCTYPE])
    {
      lines = 
	[[NSString stringWithCString:[data bytes] 
			      length:[data length]] 
                componentsSeparatedByString:@"\n"];

      RETAIN(lines);
    }
  else
    {
      NSString *msg = [NSString stringWithFormat: @"Unknown type: %@", 
				[aType uppercaseString]];
      NSRunAlertPanel(@"Alert", msg, @"Ok", nil, nil);
      return NO;
    }

  return YES;
}

- (void) makeWindowControllers
{
  NSWindowController *controller;
  NSWindow *win = [self makeWindow];
  
  controller = [[NSWindowController alloc] initWithWindow: win];
  RELEASE (win);
  [self addWindowController: controller];
  RELEASE(controller);

  // We have to do this ourself, as there is currently no nib file
  // [controller setShouldCascadeWindows:NO];
  [self windowControllerDidLoadNib: controller];

  [win setFrameAutosaveName:[self fileName]];
  if([win setFrameUsingName:[self fileName]]==NO){
      [win center];
  }

  [win orderFrontRegardless];
  [win makeKeyWindow];
  [win display];
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController;
{
  NSEnumerator *en;

  [super windowControllerDidLoadNib:aController];

  en = [lines objectEnumerator];
  if(lines != nil){
    [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_VALUE];
    [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_PUZZLE];
    [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_GUESS];
    [[sdkview sudoku] stateFromLineEnumerator:en what:FIELD_SCORE];
    
    RELEASE(lines); lines = nil;
  }
}

- (Sudoku *)sudoku
{
  return [sdkview sudoku];
}

- (SudokuView *)sudokuView
{
  return sdkview;
}

- resetPuzzle:(id)sender
{
  [sdkview reset];
  [self updateChangeCount:NSChangeDone];

  return self;
}

- solvePuzzle:(id)sender
{
  [sdkview loadSolution];
  [self updateChangeCount:NSChangeDone];

  return self;
}


@end

@implementation Document (Private)

- (NSWindow*)makeWindow
{
  NSWindow *window;
  int m = (NSTitledWindowMask |  
	   NSClosableWindowMask | 
           NSMiniaturizableWindowMask);

  NSRect frame = {{ 0, 0}, {SDK_DIM, SDK_DIM} };
  sdkview  = [[SudokuView alloc] initWithFrame:frame];

  frame = [sdkview frame]; // just in case

  window = 
      [[NSWindow alloc] initWithContentRect:frame 
			styleMask:m                   
			backing: NSBackingStoreRetained 
                             defer:YES];
  [window setDelegate:self];

  [window setContentView:sdkview];
  [window setReleasedWhenClosed:YES];

  [self setFileType:DOCTYPE];
  
  return window;
}

@end