jessegrosjean / bdocuments

This URL has Read+Write access

bdocuments / BDocumentWindowController.m
100644 155 lines (122 sloc) 5.845 kb
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
//
// BDocumentWindowController.m
// BDocuments
//
// Created by Jesse Grosjean on 10/16/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
 
#import "BDocumentWindowController.h"
#import "BDocument.h"
 
 
@implementation BDocumentWindowController
 
#pragma mark Class Methods
 
+ (void)initialize {
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], BNumberPagesWhenPrinting,
nil]];
}
 
#pragma mark Init
 
- (id)initWithWindowControllerUserDefaultsKey:(NSString *)newDefaultsKey {
    if (self = [self initWithWindowControllerUserDefaultsKey:newDefaultsKey nibName:nil]) {
    }
    return self;
}
 
- (id)initWithWindowControllerUserDefaultsKey:(NSString *)newDefaultsKey nibName:(NSString *)nibName {
    if (self = [super initWithWindowNibName:nibName]) {
savedWindowControllerUserDefaultsKey = newDefaultsKey;
if (!savedWindowControllerUserDefaultsKey) {
CFUUIDRef uuid = CFUUIDCreate(NULL);
savedWindowControllerUserDefaultsKey = NSMakeCollectable(CFUUIDCreateString(NULL, uuid));
CFRelease(uuid);
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(writeWindowControllerUserDefaults) name:BDocumentUserDefaultsWillSynchronizeNotification object:nil];
    }
    return self;
}
 
#pragma mark awake from nib like methods
 
- (void)awakeFromNib {
[self setWindowControllerUserDefaultsKey:savedWindowControllerUserDefaultsKey];
}
 
#pragma mark WindowController Use Defaults
 
- (NSString *)windowControllerUserDefaultsKey {
return windowControllerUserDefaultsKey;
}
 
- (void)setWindowControllerUserDefaultsKey:(NSString *)newDefaultsKey {
windowControllerUserDefaultsKey = newDefaultsKey;
 
if (windowControllerUserDefaultsKey) {
[self readWindowControllerUserDefaults];
}
}
 
- (NSMutableDictionary *)windowControllerUserDefaults {
return [[[self document] documentUserDefaultForKey:BDocumentsWindowControllersDefaultsKey] objectForKey:[self windowControllerUserDefaultsKey]];
}
 
- (id)windowControllerUserDefaultForKey:(NSString *)key {
return [[self windowControllerUserDefaults] objectForKey:key];
}
 
- (void)setWindowControllerUserDefault:(id)newDefault forKey:(NSString *)key {
if ([self windowControllerUserDefaultsKey]) {
NSMutableDictionary *windowControllerState = [[[self document] documentUserDefaultForKey:BDocumentsWindowControllersDefaultsKey] objectForKey:windowControllerUserDefaultsKey];
 
if (newDefault) {
[windowControllerState setObject:newDefault forKey:key];
} else {
[windowControllerState removeObjectForKey:key];
}
}
}
 
- (void)readWindowControllerUserDefaults {
BDocument *document = [self document];
NSMutableDictionary *windowControllersState = [document documentUserDefaultForKey:BDocumentsWindowControllersDefaultsKey];
 
if (!windowControllersState) {
windowControllersState = [NSMutableDictionary dictionary];
[document setDocumentUserDefault:windowControllersState forKey:BDocumentsWindowControllersDefaultsKey];
}
 
NSMutableDictionary *windowControllerState = [windowControllersState objectForKey:windowControllerUserDefaultsKey];
if (!windowControllerState) {
windowControllerState = [NSMutableDictionary dictionary];
[windowControllersState setObject:windowControllerState forKey:windowControllerUserDefaultsKey];
}
 
[self setWindowControllerUserDefault:NSStringFromClass([self class]) forKey:@"class"];
 
NSValue *windowFrameValue = [[self windowControllerUserDefaultForKey:BWindowFrameKey] copy];
NSNumber *windowIsMiniturized = [[self windowControllerUserDefaultForKey:BWindowIsMiniturizedKey] copy];
NSNumber *windowIsMain = [[self windowControllerUserDefaultForKey:BWindowIsMainKey] copy];
 
if (windowFrameValue) {
[self setShouldCascadeWindows:NO];
[[self window] setFrame:[windowFrameValue rectValue] display:NO];
}
 
if (windowIsMiniturized && [windowIsMiniturized boolValue]) {
[[self window] performSelector:@selector(performMiniaturize:) withObject:self afterDelay:0];
}
 
if (windowIsMain && [windowIsMain boolValue]) {
[[self window] performSelector:@selector(makeMainWindow) withObject:nil afterDelay:0];
}
}
 
- (void)writeWindowControllerUserDefaults {
[self setWindowControllerUserDefault:[NSValue valueWithRect:[[self window] frame]] forKey:BWindowFrameKey];
[self setWindowControllerUserDefault:[NSNumber numberWithBool:[[self window] isMiniaturized]] forKey:BWindowIsMiniturizedKey];
[self setWindowControllerUserDefault:[NSNumber numberWithBool:[[self window] isMainWindow]] forKey:BWindowIsMainKey];
}
 
- (void)setDocument:(NSDocument *)newDocument {
if (!newDocument) {
[self writeWindowControllerUserDefaults];
}
[super setDocument:newDocument];
}
 
#pragma mark Printing
 
- (NSView *)printViewForPrintInfo:(NSPrintInfo *)printInfo {
return [[self window] contentView];
}
 
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError {
NSPrintInfo *printInfo = [[[self document] printInfo] copy];
[[printInfo dictionary] addEntriesFromDictionary:printSettings];
[[printInfo dictionary] setValue:[[NSUserDefaults standardUserDefaults] objectForKey:BNumberPagesWhenPrinting] forKey:NSPrintHeaderAndFooter];
NSView *printView = [self printViewForPrintInfo:printInfo];
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:printView printInfo:printInfo];
[printOperation setJobTitle:[[self document] displayName]];
[printOperation setShowPanels:YES];
return printOperation;
}
 
@end
 
NSString *BWindowFrameKey = @"BWindowFrameKey";
NSString *BWindowIsMainKey = @"BWindowIsMainKey";
NSString *BWindowIsMiniturizedKey = @"BWindowIsMiniturizedKey";
NSString *BDocumentsWindowControllersDefaultsKey = @"BDocumentsWindowControllersDefaultsKey";
NSString *BNumberPagesWhenPrinting = @"BNumberPagesWhenPrinting";