2
* Copyright (C) 2004 Stefan Kleine Stegemann
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
#import "NSView+Scrolling.h"
22
@implementation NSView (Scrolling)
26
if (![self enclosingScrollView])
31
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
32
[self scrollPoint: NSMakePoint(NSMinX(visibleRect), NSHeight([self frame]))];
35
- (void) scrollToBottom
37
if (![self enclosingScrollView])
42
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
43
[self scrollPoint: NSMakePoint(NSMinX(visibleRect), 0)];
48
if (![self enclosingScrollView])
53
return [self scrollUp: [[self enclosingScrollView] verticalPageScroll]];
56
- (BOOL) scrollPageDown
58
if (![self enclosingScrollView])
63
return [self scrollDown: [[self enclosingScrollView] verticalPageScroll]];
68
if (![self enclosingScrollView])
73
return [self scrollUp: [[self enclosingScrollView] verticalLineScroll]];
76
- (BOOL) scrollLineDown
78
if (![self enclosingScrollView])
83
return [self scrollDown: [[self enclosingScrollView] verticalLineScroll]];
86
- (BOOL) scrollUp: (float)amount
88
if (![self enclosingScrollView])
93
NSAssert(amount > 0, @"negative amount");
95
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
96
NSPoint targetPoint = NSMakePoint(NSMinX(visibleRect),
97
NSMinY(visibleRect) + amount);
99
float maxY = NSHeight([self frame]) - NSHeight(visibleRect);
100
if (targetPoint.y > maxY)
102
targetPoint.y = maxY;
104
[self scrollPoint: targetPoint];
106
return !NSEqualRects(visibleRect, [[self enclosingScrollView] documentVisibleRect]);
109
- (BOOL) scrollDown: (float)amount
111
if (![self enclosingScrollView])
116
NSAssert(amount > 0, @"negative amount");
118
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
119
NSPoint targetPoint = NSMakePoint(NSMinX(visibleRect),
120
NSMinY(visibleRect) - amount);
122
if (targetPoint.y < 0)
126
[self scrollPoint: targetPoint];
128
return !NSEqualRects(visibleRect, [[self enclosingScrollView] documentVisibleRect]);
131
- (void) scrollToLeftEdge
133
if (![self enclosingScrollView])
138
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
139
NSPoint targetPoint = NSMakePoint(0, NSMinY(visibleRect));
141
[self scrollPoint: targetPoint];
144
- (void) scrollToRightEdge
146
if (![self enclosingScrollView])
151
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
152
NSPoint targetPoint = NSMakePoint(NSWidth([self frame]) - NSWidth(visibleRect),
153
NSMinY(visibleRect));
155
[self scrollPoint: targetPoint];
158
- (BOOL) scrollLineLeft
160
if (![self enclosingScrollView])
165
return [self scrollLeft: [[self enclosingScrollView] horizontalLineScroll]];
168
- (BOOL) scrollLineRight
170
if (![self enclosingScrollView])
175
return [self scrollRight: [[self enclosingScrollView] horizontalLineScroll]];
178
- (BOOL) scrollLeft: (float)amount
180
if (![self enclosingScrollView])
185
NSAssert(amount > 0, @"negative amount");
187
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
188
NSPoint targetPoint = NSMakePoint(NSMinX(visibleRect) - amount,
189
NSMinY(visibleRect));
191
if (targetPoint.x < 0)
195
[self scrollPoint: targetPoint];
197
return !NSEqualRects(visibleRect, [[self enclosingScrollView] documentVisibleRect]);
200
- (BOOL) scrollRight: (float)amount
202
if (![self enclosingScrollView])
207
NSAssert(amount > 0, @"negative amount");
209
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
210
NSPoint targetPoint = NSMakePoint(NSMinX(visibleRect) + amount,
211
NSMinY(visibleRect));
213
float maxX = NSWidth([self frame]) - NSWidth(visibleRect);
214
if (targetPoint.x > maxX)
216
targetPoint.x = maxX;
218
[self scrollPoint: targetPoint];
220
return !NSEqualRects(visibleRect, [[self enclosingScrollView] documentVisibleRect]);