home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CKeyScrollAttachment.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  3.5 KB  |  131 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #include "CKeyScrollAttachment.h"
  21.  
  22.  
  23. CKeyScrollAttachment::CKeyScrollAttachment(LStream* inStream)
  24.     : LAttachment(inStream)
  25. {
  26.     mViewToScroll = dynamic_cast<LView*>(GetOwnerHost());
  27.     ThrowIfNULL_(mViewToScroll);
  28.     
  29.     Assert_(mMessage == msg_KeyPress);
  30. }
  31.  
  32.  
  33. void
  34. CKeyScrollAttachment::ExecuteSelf(
  35.     MessageT    /* inMessage */,
  36.     void        *ioParam)
  37. {
  38.     mExecuteHost = false;        // We handle navigation keys
  39.     Int16 theKey = ((EventRecord*) ioParam)->message & charCodeMask;
  40.     
  41.     switch (theKey)
  42.         {
  43.         case char_Home:            // Scroll to top left
  44.             mViewToScroll->ScrollImageTo(0, 0, true);
  45.             break;
  46.             
  47.         case char_End:
  48.             {                    // Scroll to bottom right
  49.             SDimension16    frameSize;
  50.             SDimension32    imageSize;
  51.             mViewToScroll->GetFrameSize(frameSize);
  52.             mViewToScroll->GetImageSize(imageSize);
  53.             // Obviously we don't want to scroll a small image to the bottom of
  54.             // the frame...
  55.             SInt16 deltaV = imageSize.height - frameSize.height;
  56.             if (deltaV < 0)
  57.                 deltaV = 0;
  58.             SInt16 deltaH = imageSize.width - frameSize.width;
  59.             if (deltaH < 0)
  60.                 deltaH = 0;
  61.             if (deltaV || deltaH)
  62.                 mViewToScroll->ScrollImageTo(deltaH, deltaV, true);
  63.             }
  64.             break;
  65.             
  66.         case char_PageUp:
  67.             {                    // Scroll up by height of Frame,
  68.                                 //   but not past top of Image
  69.             SPoint32        frameLoc;
  70.             SPoint32        imageLoc;
  71.             mViewToScroll->GetFrameLocation(frameLoc);
  72.             mViewToScroll->GetImageLocation(imageLoc);
  73.             
  74.             Int32    upMax = frameLoc.v - imageLoc.v;
  75.             if (upMax > 0)
  76.                 {
  77.                 SPoint32        scrollUnit;
  78.                 SDimension16    frameSize;
  79.                 mViewToScroll->GetScrollUnit(scrollUnit);
  80.                 mViewToScroll->GetFrameSize(frameSize);
  81.  
  82.                 Int32    up = (frameSize.height - 1) / scrollUnit.v;
  83.                 if (up <= 0)
  84.                     up = 1;
  85.  
  86.                 up *= scrollUnit.v;
  87.                 if (up > upMax)
  88.                     up = upMax;
  89.  
  90.                 mViewToScroll->ScrollImageBy(0, -up, true);
  91.                 }
  92.             }
  93.             break;
  94.             
  95.         case char_PageDown:
  96.             {                    // Scroll down by height of Frame,
  97.                                 //   but not past bottom of Image
  98.             SPoint32        frameLoc;
  99.             SPoint32        imageLoc;
  100.             SDimension16    frameSize;
  101.             SDimension32    imageSize;
  102.             mViewToScroll->GetFrameLocation(frameLoc);
  103.             mViewToScroll->GetImageLocation(imageLoc);
  104.             mViewToScroll->GetFrameSize(frameSize);
  105.             mViewToScroll->GetImageSize(imageSize);
  106.             
  107.             Int32    downMax = imageSize.height - frameSize.height -
  108.                                 (frameLoc.v - imageLoc.v);
  109.             if (downMax > 0) {
  110.                 SPoint32        scrollUnit;
  111.                 mViewToScroll->GetScrollUnit(scrollUnit);
  112.  
  113.                 Int32    down = (frameSize.height - 1) / scrollUnit.v;
  114.                 if (down <= 0) {
  115.                     down = 1;
  116.                 }
  117.                 down *= scrollUnit.v;
  118.                 if (down > downMax) {
  119.                     down = downMax;
  120.                 }
  121.                 mViewToScroll->ScrollImageBy(0, down, true);
  122.             }
  123.             break;
  124.         }
  125.             
  126.         default:
  127.             mExecuteHost = true;    // Some other key, let host respond
  128.             break;
  129.     }
  130. }
  131.