home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CMouseDragger.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.0 KB  |  341 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. #ifndef __H_CMouseDragger
  20. #define __H_CMouseDragger
  21. #pragma once
  22.  
  23. /*======================================================================================
  24.     AUTHOR:            Ted Morris - 25 SEPT 96
  25.                     ⌐1996 Netscape Communications Corp. All rights reserved worldwide.
  26.  
  27.     DESCRIPTION:    Implements a class used for tracking a mouse action within an LView
  28.                     class and making it undoable.
  29.                     
  30.                     To use this class, do the following:
  31.                     
  32.     MODIFICATIONS:
  33.  
  34.     Date            Person            Description
  35.     ----            ------            -----------
  36. ======================================================================================*/
  37.  
  38.  
  39. /*====================================================================================*/
  40.     #pragma mark INCLUDE FILES
  41. /*====================================================================================*/
  42.  
  43. #include <LView.h>
  44. #include <LTableView.h>
  45.  
  46.  
  47. /*====================================================================================*/
  48.     #pragma mark TYPEDEFS
  49. /*====================================================================================*/
  50.  
  51.  
  52. /*====================================================================================*/
  53.     #pragma mark CONSTANTS
  54. /*====================================================================================*/
  55.  
  56.  
  57. /*====================================================================================*/
  58.     #pragma mark CLASS DECLARATIONS
  59. /*====================================================================================*/
  60.  
  61. #pragma mark -
  62.  
  63. class LImageRect {
  64.  
  65. public:
  66.  
  67.                         LImageRect(void) {
  68.                             Set(0, 0, 0, 0);
  69.                         }
  70.                         LImageRect(Int32 inLeft, Int32 inTop, Int32 inRight, Int32 inBottom) {
  71.                             Set(inLeft, inTop, inRight, inBottom);
  72.                         }
  73.                         LImageRect(const LImageRect *inImageRect) {
  74.                             Set(inImageRect->Left(), inImageRect->Top(), 
  75.                                 inImageRect->Right(), inImageRect->Bottom());
  76.                         }
  77.                         
  78.     Int32                Left(void) const {
  79.                             return mTopLeft.h;
  80.                         }
  81.     Int32                Top(void) const {
  82.                             return mTopLeft.v;
  83.                         }
  84.     Int32                Right(void) const {
  85.                             return mBotRight.h;
  86.                         }
  87.     Int32                Bottom(void) const {
  88.                             return mBotRight.v;
  89.                         }
  90.     void                Left(Int32 inValue) {
  91.                             mTopLeft.h = inValue;
  92.                         }
  93.     void                Top(Int32 inValue) {
  94.                             mTopLeft.v = inValue;
  95.                         }
  96.     void                Right(Int32 inValue) {
  97.                             mBotRight.h = inValue;
  98.                         }
  99.     void                Bottom(Int32 inValue) {
  100.                             mBotRight.v = inValue;
  101.                         }
  102.     void                Set(const LImageRect *inImageRect) {
  103.                             mTopLeft.h = inImageRect->Left();
  104.                             mTopLeft.v = inImageRect->Top();
  105.                             mBotRight.h = inImageRect->Right();
  106.                             mBotRight.v = inImageRect->Bottom();
  107.                         }
  108.     void                Set(Int32 inLeft, Int32 inTop, Int32 inRight, Int32 inBottom) {
  109.                             mTopLeft.h = inLeft;
  110.                             mTopLeft.v = inTop;
  111.                             mBotRight.h = inRight;
  112.                             mBotRight.v = inBottom;
  113.                         }
  114.     void                Offset(Int32 inDeltaH, Int32 inDeltaV) {
  115.                             mTopLeft.h += inDeltaH;
  116.                             mTopLeft.v += inDeltaV;
  117.                             mBotRight.h += inDeltaH;
  118.                             mBotRight.v += inDeltaV;
  119.                         }
  120.     SPoint32            *TopLeft(void) {
  121.                             return &mTopLeft;
  122.                         }
  123.     SPoint32            *BotRight(void) {
  124.                             return &mBotRight;
  125.                         }
  126.     Boolean                IsEmpty(void) const {
  127.                             return ((mTopLeft.h >= mBotRight.h) || (mTopLeft.v >= mBotRight.v));
  128.                         }
  129.     Boolean                PointIn(Int32 inHorz, Int32 inVert) const {
  130.                             return (((inHorz >= mTopLeft.h) && (inHorz < mBotRight.h)) &&
  131.                                     ((inVert >= mTopLeft.v) && (inVert < mBotRight.v)));
  132.                         }
  133.     Boolean                PinPoint(Int32 *ioHorz, Int32 *ioVert) const {
  134.                             Boolean rtnVal = false;
  135.                             if ( *ioHorz < mTopLeft.h ) {
  136.                                 *ioHorz = mTopLeft.h; rtnVal = true;
  137.                             } else if ( *ioHorz >= mBotRight.h ) {
  138.                                 *ioHorz = mBotRight.h; rtnVal = true;
  139.                             }
  140.                             if ( *ioVert < mTopLeft.v ) {
  141.                                 *ioVert = mTopLeft.v; rtnVal = true;
  142.                             } else if ( *ioVert >= mBotRight.v ) {
  143.                                 *ioVert = mBotRight.v; rtnVal = true;
  144.                             }
  145.                             return rtnVal;
  146.                         }
  147.                         
  148.     static Boolean        EqualPoints32(const SPoint32 *inPointA, const SPoint32 *inPointB) {
  149.                             return ((inPointA->h == inPointB->h) && (inPointA->v == inPointB->v));
  150.                         }
  151.  
  152. protected:
  153.  
  154.     // Instance variables ==========================================================
  155.     
  156.     SPoint32            mTopLeft;
  157.     SPoint32            mBotRight;
  158. };
  159.  
  160. #pragma mark -
  161.  
  162. class CMouseDragger {
  163.                   
  164. public:
  165.  
  166.                         CMouseDragger(void) {
  167.                         }
  168.     virtual                ~CMouseDragger(void) {
  169.                         }
  170.  
  171.     // Start public interface ----------------------------------------------------------
  172.  
  173.     Boolean                DoTrackMouse(LView *inView, const SPoint32 *inStartMouseImagePt,
  174.                                      const LImageRect *inMouseImagePinRect = nil,
  175.                                      const Int16 inMinHMoveStart = 1, const Int16 inMinVMoveStart = 1);
  176.  
  177.     // End public interface ------------------------------------------------------------
  178.  
  179.     static Boolean         CanScrollView(LView *inView, Point inGlobalMousePt, 
  180.                                       const Rect *inGlobalViewFrame = nil);
  181.  
  182.     static void         GlobalToImagePt(LView *inView, Point inGlobalPt, 
  183.                                         SPoint32 *outImagePt);
  184.     static void          ImageToPortPt(LView *inView, const SPoint32 *inImagePt, 
  185.                                       Point *outPortPt);
  186.     static void          PortToImagePt(LView *inView, Point inPortPoint, 
  187.                                       SPoint32 *outImagePt);
  188.     static void          PortToImageRect(LView *inView, const Rect *inPortRect, 
  189.                                           LImageRect *outImageRect);
  190.     static void          ImageToLocalRect(LView *inView, LImageRect *inImageRect, 
  191.                                            Rect *outLocalRect);
  192.  
  193. protected:
  194.  
  195.     // Override methods
  196.  
  197.     virtual void        BeginTracking(const SPoint32 *inStartMouseImagePt);
  198.  
  199.     virtual Boolean        KeepTracking(const SPoint32 *inStartMouseImagePt,
  200.                                      const SPoint32 *inPrevMouseImagePt,
  201.                                      const SPoint32 *inCurrMouseImagePt);
  202.  
  203.     virtual void        EndTracking(const SPoint32 *inStartMouseImagePt,
  204.                                     const SPoint32 *inEndMouseImagePt);
  205.  
  206.     virtual void        AboutToScrollView(const SPoint32 *inStartMouseImagePt,
  207.                                            const SPoint32 *inCurrMouseImagePt);
  208.     virtual void        DoneScrollingView(const SPoint32 *inStartMouseImagePt,
  209.                                            const SPoint32 *inCurrMouseImagePt);
  210.  
  211.                         enum EDrawRegionState { 
  212.                             eDoErase = 0            // Normal erase
  213.                         ,    eDoEraseScroll = 1        // Erase just before a scoll
  214.                         ,    eDoDraw = 2             // Draw
  215.                         };
  216.                         
  217.     virtual void        DrawDragRegion(const SPoint32 *inStartMouseImagePt,
  218.                                        const SPoint32 *inCurrMouseImagePt, 
  219.                                        EDrawRegionState inDrawState);
  220.  
  221.     // Instance variables ==========================================================
  222.     
  223.     LView                *mView;
  224.     SPoint32            mStartImagePt;
  225.     SPoint32            mEndImagePt;
  226. };
  227.  
  228.  
  229. // Class specific for dragging rows in an LTableView. Be careful, it hasn't been
  230. // tested on a wide range of LTableView descendents! Also, assumes that the
  231. // table only has a single column! Note: This class also assumes the presence
  232. // of icm8 resources for the arrow icons.
  233.  
  234. /*======================================================================================
  235.     The ClickCell() method for your table will look something like this:
  236.     
  237.     void CMyTableView::ClickCell(const STableCell &inCell, const SMouseDownEvent &inMouseDown) {
  238.  
  239.         if ( LPane::GetClickCount() == 2 ) {
  240.         
  241.             // Do double click stuff
  242.             
  243.         } else if ( mRows > 1 ) {
  244.  
  245.             CTableRowDragger dragger(inCell.row);
  246.             
  247.             SPoint32 startMouseImagePt;
  248.             LocalToImagePoint(inMouseDown.whereLocal, startMouseImagePt);
  249.             
  250.             // Restrict dragging to a thin vertical column the height of the image
  251.             
  252.             LImageRect dragPinRect;
  253.             {
  254.                 Int32 left, top, right, bottom;
  255.                 GetImageCellBounds(inCell, left, top, right, bottom);
  256.                 dragPinRect.Set(startMouseImagePt.h, startMouseImagePt.v - top, startMouseImagePt.h, 
  257.                                 mImageSize.height - (bottom - startMouseImagePt.v) + 1);
  258.             }
  259.             
  260.             TrySetCursor(cHandClosedCursorID);
  261.             dragger.DoTrackMouse(this, &startMouseImagePt, &dragPinRect, 2, 2);
  262.                 
  263.             TableIndexT newRow = dragger.GetDraggedRow();
  264.                 
  265.             if ( newRow ) {
  266.                 CMyMoveRowAction *action = new CMyMoveRowAction(this, inCell.row, newRow);
  267.                 FailNIL_(action);
  268.                 PostAction(action);
  269.             }
  270.         }
  271.     }
  272. ======================================================================================*/
  273.  
  274. // CTableRowDragger
  275.  
  276. class CTableRowDragger : public CMouseDragger {
  277.  
  278. public:
  279.  
  280.                         CTableRowDragger(TableIndexT inClickedRow) :
  281.                                           CMouseDragger(),
  282.                                           mClickedRow(inClickedRow),
  283.                                           mOverRow(inClickedRow),
  284.                                           mNumCols(1) {
  285.                                           
  286.                         }
  287.                         
  288.     TableIndexT            GetDraggedRow(void) {
  289.                             return ((mClickedRow == mOverRow) ? 0 : mOverRow);
  290.                         }
  291.                         
  292. protected:
  293.  
  294.                         enum {    // Constants
  295.                             icm8_LeftInsertArrow = 29203
  296.                         ,    icm8_RightInsertArrow = 29204
  297.                         ,    cArrowVCenterTopOffset = 3
  298.                         ,    cArrowPolyWidth = 4
  299.                         ,    cArrowHMargin = 1
  300.                         ,    cSmallIconWidth = 16
  301.                         ,    cSmallIconHeight = 12
  302.  
  303.                         };
  304.  
  305.     virtual void        BeginTracking(const SPoint32 *inStartMouseImagePt);
  306.     virtual void        EndTracking(const SPoint32 *inStartMouseImagePt,
  307.                                     const SPoint32 *inEndMouseImagePt);
  308.                         
  309.     virtual void        DrawDragRegion(const SPoint32 *inStartMouseImagePt,
  310.                                        const SPoint32 *inCurrMouseImagePt,
  311.                                        EDrawRegionState inDrawState);
  312.     void                UpdateInsertionPoint(const LImageRect *inImageCellDrawRect,
  313.                                              LTableView *inTableView,
  314.                                              EDrawRegionState inDrawState);
  315.     void                CalcArrowRects(Int16 inPortLineV, Rect *outLeftRect, Rect *outRightRect,
  316.                                        Boolean inErase);
  317.  
  318.     // Instance variables ==========================================================
  319.     
  320.     TableIndexT            mClickedRow;        // The row that was clicked
  321.     TableIndexT            mOverRow;            // The current row that the mouse is over
  322.     TableIndexT            mNumCols;            // The current row that the mouse is over
  323.  
  324.     Boolean                mDidDraw;
  325.     
  326.     Int16                mPortInsertionLineV;    // Port vertical coordinate that the last
  327.                                                 // insertion line and arrows was draw at,
  328.                                                 // min_Int16 if no line was last drawn 
  329.     Int16                mPortInsertionLineLeft;    // Port left coordinate for drawing the insertion line
  330.     Int16                mPortInsertionLineRight;// Port right coordinate for drawing the insertion line
  331.     
  332.     Rect                mPortLeftArrowBox;
  333.     Rect                mPortRightArrowBox;
  334.  
  335.     Int16                mPortFrameMinV;
  336.     Int16                mPortFrameMaxV;
  337. };
  338.  
  339.  
  340. #endif // __H_CMouseDragger
  341.