WebCore:
[WebKit-https.git] / WebCore / kwq / KWQNSViewExtras.m
1 /*
2  * Copyright (C) 2003 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "KWQNSViewExtras.h"
27
28 @implementation NSView (KWQNSViewExtras)
29
30 - (void)_KWQ_scrollFrameToVisible
31 {
32     [self _KWQ_scrollRectToVisible:[self bounds] forceCentering:NO];
33 }
34
35 - (void)_KWQ_scrollRectToVisible:(NSRect)rect forceCentering:(BOOL)forceCentering
36 {
37     [self _KWQ_scrollRectToVisible:rect inView:self forceCentering:forceCentering];
38 }
39
40 - (void)_KWQ_scrollRectToVisible:(NSRect)rect inView:(NSView *)view forceCentering:(BOOL)forceCentering
41 {
42     [[self superview] _KWQ_scrollRectToVisible:rect inView:view forceCentering:forceCentering];
43 }
44
45 - (void)_KWQ_scrollPointRecursive:(NSPoint)p
46 {
47     [self _KWQ_scrollPointRecursive:p inView:self];
48 }
49
50 - (void)_KWQ_scrollPointRecursive:(NSPoint)p inView:(NSView *)view
51 {
52     p = [self convertPoint: p fromView:view];
53     [self scrollPoint: p];
54     [[self superview] _KWQ_scrollPointRecursive:p inView:self];
55 }
56
57 @end
58
59 @implementation NSClipView (KWQNSViewExtras)
60
61 - (void)_KWQ_scrollRectToVisible:(NSRect)rect inView:(NSView *)view forceCentering:(BOOL)forceCentering
62 {
63     NSRect exposeRect = [self convertRect:rect fromView:view];
64     NSRect visibleRect = [self bounds];
65     
66     if (forceCentering || !NSContainsRect(visibleRect, exposeRect)) {
67         // Make an expose rectangle that will end up centering the passed-in rectangle horizontally.
68         if (exposeRect.size.width >= visibleRect.size.width) {
69             if (forceCentering) {
70                 float expLeft = exposeRect.origin.x;
71                 float expRight = exposeRect.origin.x + exposeRect.size.width;
72                 float visLeft = visibleRect.origin.x;
73                 float visRight = visibleRect.origin.x + visibleRect.size.width;
74                 float diffLeft = visLeft - expLeft;
75                 float diffRight = expRight - visRight;
76                 if (diffLeft >= 0 && diffRight >= 0) {
77                     // Exposed rect fills the visible rect.
78                     // We don't want the view to budge.
79                     exposeRect.origin.x = visLeft;
80                 }
81                 else if (diffLeft < 0) {
82                     // Scroll so left of visible region shows left of expose rect.
83                     // Leave expose origin as it is to make this happen.
84                 }
85                 else {
86                     // Scroll so right of visible region shows right of expose rect.
87                     exposeRect.origin.x = expRight - visibleRect.size.width;
88                 }
89             }
90             exposeRect.size.width = visibleRect.size.width;
91         } 
92         else {
93             exposeRect.origin.x -= (visibleRect.size.width - exposeRect.size.width) / 2.0;
94             exposeRect.size.width += (visibleRect.size.width - exposeRect.size.width);
95         }
96         
97         // Make an expose rectangle that will end up centering the passed-in rectangle vertically.
98         if (exposeRect.size.height >= visibleRect.size.height) {
99             if (forceCentering) {
100                 if ([self isFlipped]) {
101                     float expTop = exposeRect.origin.y;
102                     float expBottom = exposeRect.origin.y + exposeRect.size.height;
103                     float visTop = visibleRect.origin.y;
104                     float visBottom = visibleRect.origin.y + visibleRect.size.height;
105                     float diffTop = visTop - expTop;
106                     float diffBottom = expBottom - visBottom;
107                     if (diffTop >= 0 && diffBottom >= 0) {
108                         // Exposed rect fills the visible rect.
109                         // We don't want the view to budge.
110                         exposeRect.origin.y = visTop;
111                     }
112                     else if (diffTop < 0) {
113                         // Scroll so top of visible region shows top of expose rect.
114                         // Leave expose origin as it is to make this happen.
115                     }
116                     else {
117                         // Scroll so bottom of visible region shows bottom of expose rect.
118                         exposeRect.origin.y = expBottom - visibleRect.size.height;
119                     }
120                 }
121                 else {
122                     float expTop = exposeRect.origin.y + exposeRect.size.height;
123                     float expBottom = exposeRect.origin.y;
124                     float visTop = visibleRect.origin.y + visibleRect.size.height;
125                     float visBottom = visibleRect.origin.y;
126                     float diffTop = expTop - visTop;
127                     float diffBottom = visBottom - expBottom;
128                     if (diffTop >= 0 && diffBottom >= 0) {
129                         // Exposed rect fills the visible rect.
130                         // We don't want the view to budge.
131                         exposeRect.origin.y = visBottom;
132                     }
133                     else if (diffTop < 0) {
134                         // Scroll so top of visible region shows top of expose rect.
135                         exposeRect.origin.y = expTop + visibleRect.size.height;
136                     }
137                     else {
138                         // Scroll so bottom of visible region shows bottom of expose rect.
139                         // Leave expose origin as it is to make this happen.
140                     }
141                 } 
142             }
143             exposeRect.size.height = visibleRect.size.height;
144         } 
145         else {
146             if ([self isFlipped]) {
147                 exposeRect.origin.y -= (visibleRect.size.height - exposeRect.size.height) / 2.0;
148             } 
149             else {
150                 exposeRect.origin.y += (visibleRect.size.height - exposeRect.size.height) / 2.0;
151             }
152             exposeRect.size.height += (visibleRect.size.height - exposeRect.size.height);
153         }
154         
155         [self scrollRectToVisible:exposeRect];
156     }
157
158     [super _KWQ_scrollRectToVisible:rect inView:view forceCentering:forceCentering];
159 }
160
161
162 @end