Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 182 lines (148 sloc) 7.008 kb
544c9ac @tjw Initial import of LinkBack 1.0 alpha from <http://linkbackproject.org/fi...
tjw authored
1 // SKTGridPanelController.m
2 // Sketch Example
3 //
4
5 #import "SKTGridPanelController.h"
6 #import "SKTGridView.h"
7 #import "SKTGraphicView.h"
8 #import "SKTDrawWindowController.h"
9
10 @implementation SKTGridPanelController
11
12 + (id)sharedGridPanelController {
13 static SKTGridPanelController *sharedGridPanelController = nil;
14
15 if (!sharedGridPanelController) {
16 sharedGridPanelController = [[SKTGridPanelController allocWithZone:NULL] init];
17 }
18
19 return sharedGridPanelController;
20 }
21
22 - (id)init {
23 self = [self initWithWindowNibName:@"GridPanel"];
24 if (self) {
25 [self setWindowFrameAutosaveName:@"Grid"];
26 }
27 return self;
28 }
29
30 - (void)dealloc {
31 [[NSNotificationCenter defaultCenter] removeObserver:self];
32 [super dealloc];
33 }
34
35 - (void)updatePanel {
36 if ([self isWindowLoaded]) {
37 BOOL hasGraphicView = ((_inspectingGraphicView == nil) ? NO : YES);
38 [snapsToGridCheckbox setState:([self snapsToGrid] ? NSOnState : NSOffState)];
39 [showsGridCheckbox setState:([self showsGrid] ? NSOnState : NSOffState)];
40 [gridSpacingSlider setIntValue:[self gridSpacing]];
41 [gridColorWell setColor:[self gridColor]];
42 [snapsToGridCheckbox setEnabled:hasGraphicView];
43 [showsGridCheckbox setEnabled:hasGraphicView];
44 [gridSpacingSlider setEnabled:hasGraphicView];
45 [gridColorWell setEnabled:hasGraphicView];
46 [gridView setNeedsDisplay:YES];
47 }
48 }
49
50 - (void)setMainWindow:(NSWindow *)mainWindow {
51 NSWindowController *controller = [mainWindow windowController];
52
53 if (controller && [controller isKindOfClass:[SKTDrawWindowController class]]) {
54 _inspectingGraphicView = [(SKTDrawWindowController *)controller graphicView];
55 } else {
56 _inspectingGraphicView = nil;
57 }
58 [self updatePanel];
59 }
60
61 - (void)mainWindowChanged:(NSNotification *)notification {
62 [self setMainWindow:[notification object]];
63 }
64
65 - (void)mainWindowResigned:(NSNotification *)notification {
66 [self setMainWindow:nil];
67 }
68
69 - (void)windowDidLoad {
70 [super windowDidLoad];
71 [(NSPanel *)[self window] setFloatingPanel:YES];
72 [(NSPanel *)[self window] setBecomesKeyOnlyIfNeeded:YES];
73 [self setMainWindow:[NSApp mainWindow]];
74 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainWindowChanged:) name:NSWindowDidBecomeMainNotification object:nil];
75 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainWindowResigned:) name:NSWindowDidResignMainNotification object:nil];
76 }
77
78 - (BOOL)snapsToGrid {
79 return (_inspectingGraphicView ? [_inspectingGraphicView snapsToGrid] : NO);
80 }
81
82 - (BOOL)showsGrid {
83 return (_inspectingGraphicView ? [_inspectingGraphicView showsGrid] : NO);
84 }
85
86 - (float)gridSpacing {
87 return (_inspectingGraphicView ? [_inspectingGraphicView gridSpacing] : 8);
88 }
89
90 - (NSColor *)gridColor {
91 return (_inspectingGraphicView ? [_inspectingGraphicView gridColor] : [NSColor lightGrayColor]);
92 }
93
94 - (IBAction)snapsToGridCheckboxAction:(id)sender {
95 if (_inspectingGraphicView) {
96 [_inspectingGraphicView setSnapsToGrid:[sender state]];
97 }
98 }
99
100 - (IBAction)showsGridCheckboxAction:(id)sender {
101 if (_inspectingGraphicView) {
102 [_inspectingGraphicView setShowsGrid:[sender state]];
103 }
104 }
105
106 - (IBAction)gridSpacingSliderAction:(id)sender {
107 if (_inspectingGraphicView) {
108 [_inspectingGraphicView setGridSpacing:(float)[sender intValue]];
109 }
110 [gridView setNeedsDisplay:YES];
111 }
112
113 - (IBAction)gridColorWellAction:(id)sender {
114 if (_inspectingGraphicView) {
115 [_inspectingGraphicView setGridColor:[sender color]];
116 }
117 [gridView setNeedsDisplay:YES];
118 }
119
120 @end
121
122 void SKTDrawGridWithSettingsInRect(float spacing, NSColor *color, NSRect rect, NSPoint gridOrigin) {
123 int curLine, endLine;
124 NSBezierPath *gridPath = [NSBezierPath bezierPath];
125
126 [color set];
127
128 // Columns
129 curLine = ceil((NSMinX(rect) - gridOrigin.x) / spacing);
130 endLine = floor((NSMaxX(rect) - gridOrigin.x) / spacing);
131 for (; curLine<=endLine; curLine++) {
132 [gridPath moveToPoint:NSMakePoint((curLine * spacing) + gridOrigin.x, NSMinY(rect))];
133 [gridPath lineToPoint:NSMakePoint((curLine * spacing) + gridOrigin.x, NSMaxY(rect))];
134 }
135
136 // Rows
137 curLine = ceil((NSMinY(rect) - gridOrigin.y) / spacing);
138 endLine = floor((NSMaxY(rect) - gridOrigin.y) / spacing);
139 for (; curLine<=endLine; curLine++) {
140 [gridPath moveToPoint:NSMakePoint(NSMinX(rect), (curLine * spacing) + gridOrigin.y)];
141 [gridPath lineToPoint:NSMakePoint(NSMaxX(rect), (curLine * spacing) + gridOrigin.y)];
142 }
143
144 [gridPath setLineWidth:0.0];
145 [gridPath stroke];
146 }
147
148
149
150 /*
151 IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
152 consideration of your agreement to the following terms, and your use, installation,
153 modification or redistribution of this Apple software constitutes acceptance of these
154 terms. If you do not agree with these terms, please do not use, install, modify or
155 redistribute this Apple software.
156
157 In consideration of your agreement to abide by the following terms, and subject to these
158 terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in
159 this original Apple software (the "Apple Software"), to use, reproduce, modify and
160 redistribute the Apple Software, with or without modifications, in source and/or binary
161 forms; provided that if you redistribute the Apple Software in its entirety and without
162 modifications, you must retain this notice and the following text and disclaimers in all
163 such redistributions of the Apple Software. Neither the name, trademarks, service marks
164 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
165 the Apple Software without specific prior written permission from Apple. Except as expressly
166 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
167 herein, including but not limited to any patent rights that may be infringed by your
168 derivative works or by other works in which the Apple Software may be incorporated.
169
170 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
171 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
172 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
173 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
174
175 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
176 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
177 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
178 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
179 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
180 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
181 */
Something went wrong with that request. Please try again.