webengine/osswebengine/WebKit/WebView/WebDynamicScrollBarsView.m
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005 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  *
       
     8  * 1.  Redistributions of source code must retain the above copyright
       
     9  *     notice, this list of conditions and the following disclaimer. 
       
    10  * 2.  Redistributions in binary form must reproduce the above copyright
       
    11  *     notice, this list of conditions and the following disclaimer in the
       
    12  *     documentation and/or other materials provided with the distribution. 
       
    13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
       
    14  *     its contributors may be used to endorse or promote products derived
       
    15  *     from this software without specific prior written permission. 
       
    16  *
       
    17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
       
    18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
       
    21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
       
    26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27  */
       
    28 
       
    29 #import <WebKit/WebDynamicScrollBarsView.h>
       
    30 
       
    31 #import <WebKit/WebDocument.h>
       
    32 #import <WebKitSystemInterface.h>
       
    33 
       
    34 @implementation WebDynamicScrollBarsView
       
    35 
       
    36 - (void)setSuppressLayout: (BOOL)flag;
       
    37 {
       
    38     suppressLayout = flag;
       
    39 }
       
    40 
       
    41 - (void)setScrollBarsSuppressed:(BOOL)suppressed repaintOnUnsuppress:(BOOL)repaint
       
    42 {
       
    43     suppressScrollers = suppressed;
       
    44 
       
    45     // This code was originally changes for a Leopard performance imporvement. We decided to 
       
    46     // ifdef it to fix correctness issues on Tiger documented in <rdar://problem/5441823>.
       
    47 #ifndef BUILDING_ON_TIGER
       
    48     if (suppressed) {
       
    49         [[self verticalScroller] setNeedsDisplay:NO];
       
    50         [[self horizontalScroller] setNeedsDisplay:NO];
       
    51     }
       
    52         
       
    53     if (!suppressed && repaint)
       
    54         [super reflectScrolledClipView:[self contentView]];
       
    55 #else
       
    56     if (suppressed || repaint) { 
       
    57         [[self verticalScroller] setNeedsDisplay: !suppressed]; 
       
    58         [[self horizontalScroller] setNeedsDisplay: !suppressed]; 
       
    59     }
       
    60 #endif
       
    61 }
       
    62 
       
    63 - (void)updateScrollers
       
    64 {
       
    65     // We need to do the work below twice in the case where a scroll bar disappears,
       
    66     // making the second layout have a wider width than the first. Doing it more than
       
    67     // twice would indicate some kind of infinite loop, so we do it at most twice.
       
    68     // It's quite efficient to do this work twice in the normal case, so we don't bother
       
    69     // trying to figure out of the second pass is needed or not.
       
    70     if (inUpdateScrollers)
       
    71         return;
       
    72     
       
    73     inUpdateScrollers = true;
       
    74 
       
    75     int pass;
       
    76     BOOL hasVerticalScroller = [self hasVerticalScroller];
       
    77     BOOL hasHorizontalScroller = [self hasHorizontalScroller];
       
    78     BOOL oldHasVertical = hasVerticalScroller;
       
    79     BOOL oldHasHorizontal = hasHorizontalScroller;
       
    80 
       
    81     for (pass = 0; pass < 2; pass++) {
       
    82         BOOL scrollsVertically;
       
    83         BOOL scrollsHorizontally;
       
    84 
       
    85         if (!suppressLayout && !suppressScrollers && (hScroll == WebCoreScrollbarAuto || vScroll == WebCoreScrollbarAuto)) {
       
    86             // Do a layout if pending, before checking if scrollbars are needed.
       
    87             // This fixes 2969367, although may introduce a slowdown in live resize performance.
       
    88             NSView *documentView = [self documentView];
       
    89             if ((hasVerticalScroller != oldHasVertical ||
       
    90                 hasHorizontalScroller != oldHasHorizontal || [documentView inLiveResize]) && [documentView conformsToProtocol:@protocol(WebDocumentView)]) {
       
    91                 [(id <WebDocumentView>)documentView setNeedsLayout: YES];
       
    92                 [(id <WebDocumentView>)documentView layout];
       
    93             }
       
    94 
       
    95             NSSize documentSize = [documentView frame].size;
       
    96             NSSize frameSize = [self frame].size;
       
    97 
       
    98             scrollsVertically = (vScroll == WebCoreScrollbarAlwaysOn) ||
       
    99                 (vScroll == WebCoreScrollbarAuto && documentSize.height > frameSize.height);
       
   100             if (scrollsVertically)
       
   101                 scrollsHorizontally = (hScroll == WebCoreScrollbarAlwaysOn) ||
       
   102                     (hScroll == WebCoreScrollbarAuto && documentSize.width + [NSScroller scrollerWidth] > frameSize.width);
       
   103             else {
       
   104                 scrollsHorizontally = (hScroll == WebCoreScrollbarAlwaysOn) ||
       
   105                     (hScroll == WebCoreScrollbarAuto && documentSize.width > frameSize.width);
       
   106                 if (scrollsHorizontally)
       
   107                     scrollsVertically = (vScroll == WebCoreScrollbarAlwaysOn) ||
       
   108                         (vScroll == WebCoreScrollbarAuto && documentSize.height + [NSScroller scrollerWidth] > frameSize.height);
       
   109             }
       
   110         } else {
       
   111             scrollsHorizontally = (hScroll == WebCoreScrollbarAuto) ? hasHorizontalScroller : (hScroll == WebCoreScrollbarAlwaysOn);
       
   112             scrollsVertically = (vScroll == WebCoreScrollbarAuto) ? hasVerticalScroller : (vScroll == WebCoreScrollbarAlwaysOn);
       
   113         }
       
   114 
       
   115         if (hasVerticalScroller != scrollsVertically) {
       
   116             [self setHasVerticalScroller:scrollsVertically];
       
   117             hasVerticalScroller = scrollsVertically;
       
   118         }
       
   119 
       
   120         if (hasHorizontalScroller != scrollsHorizontally) {
       
   121             [self setHasHorizontalScroller:scrollsHorizontally];
       
   122             hasHorizontalScroller = scrollsHorizontally;
       
   123         }
       
   124     }
       
   125 
       
   126     if (suppressScrollers) {
       
   127         [[self verticalScroller] setNeedsDisplay: NO];
       
   128         [[self horizontalScroller] setNeedsDisplay: NO];
       
   129     }
       
   130 
       
   131     inUpdateScrollers = false;
       
   132 }
       
   133 
       
   134 // Make the horizontal and vertical scroll bars come and go as needed.
       
   135 - (void)reflectScrolledClipView:(NSClipView *)clipView
       
   136 {
       
   137     if (clipView == [self contentView]) {
       
   138         // FIXME: This hack here prevents infinite recursion that takes place when we
       
   139         // gyrate between having a vertical scroller and not having one. A reproducible
       
   140         // case is clicking on the "the Policy Routing text" link at
       
   141         // http://www.linuxpowered.com/archive/howto/Net-HOWTO-8.html.
       
   142         // The underlying cause is some problem in the NSText machinery, but I was not
       
   143         // able to pin it down.
       
   144         if (!inUpdateScrollers && [[NSGraphicsContext currentContext] isDrawingToScreen])
       
   145             [self updateScrollers];
       
   146     }
       
   147 
       
   148     // This code was originally changed for a Leopard performance imporvement. We decided to 
       
   149     // ifdef it to fix correctness issues on Tiger documented in <rdar://problem/5441823>.
       
   150 #ifndef BUILDING_ON_TIGER
       
   151     // Update the scrollers if they're not being suppressed.
       
   152     if (!suppressScrollers)
       
   153         [super reflectScrolledClipView:clipView];
       
   154 #else
       
   155     [super reflectScrolledClipView:clipView]; 
       
   156   
       
   157     // Validate the scrollers if they're being suppressed. 
       
   158     if (suppressScrollers) { 
       
   159         [[self verticalScroller] setNeedsDisplay: NO]; 
       
   160         [[self horizontalScroller] setNeedsDisplay: NO]; 
       
   161     }
       
   162 #endif
       
   163 }
       
   164 
       
   165 - (void)setAllowsScrolling:(BOOL)flag
       
   166 {
       
   167     if (hScrollModeLocked && vScrollModeLocked)
       
   168         return;
       
   169 
       
   170     if (flag && vScroll == WebCoreScrollbarAlwaysOff)
       
   171         vScroll = WebCoreScrollbarAuto;
       
   172     else if (!flag && vScroll != WebCoreScrollbarAlwaysOff)
       
   173         vScroll = WebCoreScrollbarAlwaysOff;
       
   174 
       
   175     if (flag && hScroll == WebCoreScrollbarAlwaysOff)
       
   176         hScroll = WebCoreScrollbarAuto;
       
   177     else if (!flag && hScroll != WebCoreScrollbarAlwaysOff)
       
   178         hScroll = WebCoreScrollbarAlwaysOff;
       
   179 
       
   180     [self updateScrollers];
       
   181 }
       
   182 
       
   183 - (BOOL)allowsScrolling
       
   184 {
       
   185     // Returns YES if either horizontal or vertical scrolling is allowed.
       
   186     return hScroll != WebCoreScrollbarAlwaysOff || vScroll != WebCoreScrollbarAlwaysOff;
       
   187 }
       
   188 
       
   189 - (void)setAllowsHorizontalScrolling:(BOOL)flag
       
   190 {
       
   191     if (hScrollModeLocked)
       
   192         return;
       
   193     if (flag && hScroll == WebCoreScrollbarAlwaysOff)
       
   194         hScroll = WebCoreScrollbarAuto;
       
   195     else if (!flag && hScroll != WebCoreScrollbarAlwaysOff)
       
   196         hScroll = WebCoreScrollbarAlwaysOff;
       
   197     [self updateScrollers];
       
   198 }
       
   199 
       
   200 - (void)setAllowsVerticalScrolling:(BOOL)flag
       
   201 {
       
   202     if (vScrollModeLocked)
       
   203         return;
       
   204     if (flag && vScroll == WebCoreScrollbarAlwaysOff)
       
   205         vScroll = WebCoreScrollbarAuto;
       
   206     else if (!flag && vScroll != WebCoreScrollbarAlwaysOff)
       
   207         vScroll = WebCoreScrollbarAlwaysOff;
       
   208     [self updateScrollers];
       
   209 }
       
   210 
       
   211 - (BOOL)allowsHorizontalScrolling
       
   212 {
       
   213     return hScroll != WebCoreScrollbarAlwaysOff;
       
   214 }
       
   215 
       
   216 - (BOOL)allowsVerticalScrolling
       
   217 {
       
   218     return vScroll != WebCoreScrollbarAlwaysOff;
       
   219 }
       
   220 
       
   221 -(WebCoreScrollbarMode)horizontalScrollingMode
       
   222 {
       
   223     return hScroll;
       
   224 }
       
   225 
       
   226 -(WebCoreScrollbarMode)verticalScrollingMode
       
   227 {
       
   228     return vScroll;
       
   229 }
       
   230 
       
   231 - (void)setHorizontalScrollingMode:(WebCoreScrollbarMode)mode
       
   232 {
       
   233     [self setHorizontalScrollingMode:mode andLock:NO];
       
   234 }
       
   235 
       
   236 - (void)setHorizontalScrollingMode:(WebCoreScrollbarMode)mode andLock:(BOOL)lock
       
   237 {
       
   238     if (mode == hScroll || hScrollModeLocked)
       
   239         return;
       
   240 
       
   241     hScroll = mode;
       
   242 
       
   243     if (lock)
       
   244         [self setHorizontalScrollingModeLocked:YES];
       
   245 
       
   246     [self updateScrollers];
       
   247 }
       
   248 
       
   249 - (void)setVerticalScrollingMode:(WebCoreScrollbarMode)mode
       
   250 {
       
   251     [self setVerticalScrollingMode:mode andLock:NO];
       
   252 }
       
   253 
       
   254 - (void)setVerticalScrollingMode:(WebCoreScrollbarMode)mode andLock:(BOOL)lock
       
   255 {
       
   256     if (mode == vScroll || vScrollModeLocked)
       
   257         return;
       
   258 
       
   259     vScroll = mode;
       
   260 
       
   261     if (lock)
       
   262         [self setVerticalScrollingModeLocked:YES];
       
   263 
       
   264     [self updateScrollers];
       
   265 }
       
   266 
       
   267 - (void)setScrollingMode:(WebCoreScrollbarMode)mode
       
   268 {
       
   269     [self setScrollingMode:mode andLock:NO];
       
   270 }
       
   271 
       
   272 - (void)setScrollingMode:(WebCoreScrollbarMode)mode andLock:(BOOL)lock
       
   273 {
       
   274     if ((mode == vScroll && mode == hScroll) || (vScrollModeLocked && hScrollModeLocked))
       
   275         return;
       
   276 
       
   277     BOOL update = NO;
       
   278     if (mode != vScroll && !vScrollModeLocked) {
       
   279         vScroll = mode;
       
   280         update = YES;
       
   281     }
       
   282 
       
   283     if (mode != hScroll && !hScrollModeLocked) {
       
   284         hScroll = mode;
       
   285         update = YES;
       
   286     }
       
   287 
       
   288     if (lock)
       
   289         [self setScrollingModesLocked:YES];
       
   290 
       
   291     if (update)
       
   292         [self updateScrollers];
       
   293 }
       
   294 
       
   295 - (void)setHorizontalScrollingModeLocked:(BOOL)locked
       
   296 {
       
   297     hScrollModeLocked = locked;
       
   298 }
       
   299 
       
   300 - (void)setVerticalScrollingModeLocked:(BOOL)locked
       
   301 {
       
   302     vScrollModeLocked = locked;
       
   303 }
       
   304 
       
   305 - (void)setScrollingModesLocked:(BOOL)locked
       
   306 {
       
   307     hScrollModeLocked = vScrollModeLocked = locked;
       
   308 }
       
   309 
       
   310 - (BOOL)horizontalScrollingModeLocked
       
   311 {
       
   312     return hScrollModeLocked;
       
   313 }
       
   314 
       
   315 - (BOOL)verticalScrollingModeLocked
       
   316 {
       
   317     return vScrollModeLocked;
       
   318 }
       
   319 
       
   320 - (BOOL)autoforwardsScrollWheelEvents
       
   321 {
       
   322     return YES;
       
   323 }
       
   324 
       
   325 - (void)scrollWheel:(NSEvent *)event
       
   326 {
       
   327     float deltaX;
       
   328     float deltaY;
       
   329     BOOL isContinuous;
       
   330     WKGetWheelEventDeltas(event, &deltaX, &deltaY, &isContinuous);
       
   331 
       
   332     if (fabsf(deltaY) > fabsf(deltaX)) {
       
   333         if (![self allowsVerticalScrolling]) {
       
   334             [[self nextResponder] scrollWheel:event];
       
   335             return;
       
   336         }
       
   337     } else if (![self allowsHorizontalScrolling]) {
       
   338         [[self nextResponder] scrollWheel:event];
       
   339         return;
       
   340     }
       
   341 
       
   342     [super scrollWheel:event];
       
   343 }
       
   344 
       
   345 @end