home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / EXCEL_like2080408212007.psc / flexgrid / menu / cLongScroll.cls < prev    next >
Text File  |  2003-07-04  |  11KB  |  390 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CLongScroll"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' *************************************************************************
  15. '  Copyright ⌐1994-2000 Karl E. Peterson
  16. '  All Rights Reserved, http://www.mvps.org/vb
  17. ' *************************************************************************
  18. '  You are free to use this code within your own applications, but you
  19. '  are expressly forbidden from selling or otherwise distributing this
  20. '  source code, non-compiled, without prior written consent.
  21. ' *************************************************************************
  22.  
  23. Option Explicit
  24.  
  25. ' Object variables used to reference client controls.
  26. Private WithEvents m_ClientH As HScrollBar
  27. Attribute m_ClientH.VB_VarHelpID = -1
  28. Private WithEvents m_ClientV As VScrollBar
  29. Attribute m_ClientV.VB_VarHelpID = -1
  30. Private m_Client As Object
  31.  
  32. ' Variables to track virtual scrollbar properties.
  33. Private m_Min As Long
  34. Private m_Max As Long
  35. Private m_SmallChange As Long
  36. Private m_LargeChange As Long
  37. Private m_Value As Long
  38.  
  39. ' Variables to track real scrollbar properties.
  40. Private m_vbValue As Integer
  41. Private m_vbMin As Integer
  42. Private m_vbMax As Integer
  43. Private m_vbSmallChange As Integer
  44. Private m_vbLargeChange As Integer
  45.  
  46. ' Flag property to warn of possible recursion into
  47. ' real scrollbar's Change event.
  48. Private m_Recursing As Boolean
  49.  
  50. ' Default property values
  51. Private Const defValue = 0
  52. Private Const defMin = 0
  53. Private Const defMax = 32767
  54. Private Const defSmallChange = 1
  55. Private Const defLargeChange = 1
  56. Private Const defLargeChangeClient = 10
  57.  
  58. ' Notification events
  59. Public Event Change()
  60.  
  61.  
  62. ' **************************************************************
  63. '  Initialization
  64. ' **************************************************************
  65. Private Sub Class_Initialize()
  66.    ' Set default startup property values same as VB
  67.    m_Value = defValue
  68.    m_Min = defMin
  69.    m_Max = defMax
  70.    m_SmallChange = defSmallChange
  71.    m_LargeChange = defLargeChange
  72. End Sub
  73.  
  74. ' **************************************************************
  75. '  Public Properties
  76. ' **************************************************************
  77. Public Property Set Client(obj As Object)
  78.    ' Allow for proper clean-up
  79.    If obj Is Nothing Then
  80.       Set m_Client = Nothing
  81.       Set m_ClientH = Nothing
  82.       Set m_ClientV = Nothing
  83.    Else
  84.       ' Assign passed object to m_Client if appropriate type
  85.       If TypeOf obj Is HScrollBar Then
  86.          Set m_ClientH = obj
  87.          Set m_ClientV = Nothing
  88.          Set m_Client = obj
  89.       ElseIf TypeOf obj Is VScrollBar Then
  90.          Set m_ClientH = Nothing
  91.          Set m_ClientV = obj
  92.          Set m_Client = obj
  93.       Else
  94.          Err.Clear
  95.          Err.Raise Number:=vbObjectError + 513, _
  96.                    Source:="CLongScroll", _
  97.                    Description:="Client object must be a scrollbar."
  98.       End If
  99.    End If
  100.    
  101.    ' Assign new value to intrinsic properties
  102.    If Not (m_Client Is Nothing) Then
  103.       m_Client.Min = defMin
  104.       m_Client.Max = defMax
  105.       m_Client.SmallChange = defSmallChange
  106.       m_Client.LargeChange = defLargeChangeClient
  107.       m_Recursing = True
  108.          m_Client.Value = defValue
  109.       m_Recursing = False
  110.    End If
  111. End Property
  112.  
  113. Public Property Get Client() As Object
  114.    ' Return m_Client object
  115.    Set Client = m_Client
  116. End Property
  117.  
  118. Public Property Let Max(ByVal NewVal As Long)
  119.    ' Assign Virtual Max property
  120.    m_Max = NewVal
  121.    
  122.    ' Make sure m_Value is in legal range
  123.    If m_Max > m_Min Then
  124.       If m_Value > m_Max Then
  125.          Me.Value = m_Max
  126.       End If
  127.    ElseIf m_Min > m_Max Then
  128.       If m_Value > m_Min Then
  129.          Me.Value = m_Min
  130.       End If
  131.    Else 'm_Min = m_Max
  132.       If m_Max <> m_Value Then
  133.          Me.Value = m_Max
  134.       End If
  135.    End If
  136. End Property
  137.  
  138. Public Property Get Max() As Long
  139.    ' Return Virtual Max property
  140.    Max = m_Max
  141. End Property
  142.  
  143. Public Property Let Min(ByVal NewVal As Long)
  144.    ' Assign Virtual Min property
  145.    m_Min = NewVal
  146.    
  147.    ' Make sure m_Value is in legal range
  148.    If m_Max > m_Min Then
  149.       If m_Value < m_Min Then
  150.          Me.Value = m_Min
  151.       End If
  152.    ElseIf m_Min > m_Max Then
  153.       If m_Value < m_Max Then
  154.          Me.Value = m_Min
  155.       End If
  156.    Else 'm_Min = m_Max
  157.       If m_Min <> m_Value Then
  158.          Me.Value = m_Min
  159.       End If
  160.    End If
  161. End Property
  162.  
  163. Public Property Get Min() As Long
  164.    ' Return Virtual Min property
  165.    Min = m_Min
  166. End Property
  167.  
  168. Public Property Let LargeChange(ByVal NewVal As Long)
  169.    ' Assign Virtual LargeChange property
  170.    m_LargeChange = NewVal
  171. End Property
  172.  
  173. Public Property Get LargeChange() As Long
  174.    ' Return Virtual LargeChange property
  175.    LargeChange = m_LargeChange
  176. End Property
  177.  
  178. Public Property Let SmallChange(ByVal NewVal As Long)
  179.    ' Assign Virtual SmallChange property
  180.    m_SmallChange = NewVal
  181. End Property
  182.  
  183. Public Property Get SmallChange() As Long
  184.    ' Return Virtual SmallChange property
  185.    SmallChange = m_SmallChange
  186. End Property
  187.  
  188. Public Property Let Value(ByVal NewVal As Long)
  189.    Dim VirtualRange As Long
  190.    Dim RealRange As Long
  191.    Dim Percent As Double
  192.    Dim tmpMin As Long
  193.    Dim tmpMax As Long
  194.    
  195.    ' Get current values from real scrollbar
  196.    Call ReadRealValues
  197.    
  198.    ' Cases where Virtual(Min>Max) need to be handled specially.
  199.    ' Some calculations require swapped values.
  200.    If m_Min > m_Max Then
  201.       tmpMin = m_Max
  202.       tmpMax = m_Min
  203.    Else
  204.       tmpMin = m_Min
  205.       tmpMax = m_Max
  206.    End If
  207.    
  208.    ' Rather than raise an error, correct out-of-range values
  209.    If NewVal < tmpMin Then
  210.       NewVal = tmpMin
  211.    ElseIf NewVal > tmpMax Then
  212.       NewVal = tmpMax
  213.    End If
  214.    
  215.    ' Set Virtual value
  216.    m_Value = NewVal
  217.    
  218.    ' Calculate Real value of scrollbar
  219.    VirtualRange = Abs(m_Max - m_Min)
  220.    RealRange = Abs(m_vbMax - m_vbMin)
  221.    If VirtualRange Then
  222.       Percent = Abs(m_Value - tmpMin) / VirtualRange
  223.    Else
  224.       Percent = 0
  225.    End If
  226.    
  227.    ' If Virtual(Min>Max) then flip value
  228.    If m_Min <= m_Max Then
  229.       m_vbValue = m_vbMin + (Percent * RealRange)
  230.    Else
  231.       m_vbValue = m_vbMax - (Percent * RealRange)
  232.    End If
  233.    
  234.    ' Update real scrollbar and notify client
  235.    Call UpdateRealValue
  236.    RaiseEvent Change
  237. End Property
  238.  
  239. Public Property Get Value() As Long
  240. Attribute Value.VB_UserMemId = 0
  241.    ' Return Virtual value for scrollbar
  242.    Value = m_Value
  243. End Property
  244.  
  245. ' **************************************************************
  246. '  Sunken Client Events
  247. ' **************************************************************
  248. Private Sub m_ClientH_Change()
  249.    ' In this, and the other change/scroll events,
  250.    ' pass execution to a generic calc routine that
  251.    ' resets the scrollbar's values and updates
  252.    ' internal tracking variables.
  253.    Call ClientChange
  254. End Sub
  255.  
  256. Private Sub m_ClientH_Scroll()
  257.    Call ClientChange
  258. End Sub
  259.  
  260. Private Sub m_ClientV_Change()
  261.    Call ClientChange
  262. End Sub
  263.  
  264. Private Sub m_ClientV_Scroll()
  265.    Call ClientChange
  266. End Sub
  267.  
  268. ' **************************************************************
  269. '  Private Methods
  270. ' **************************************************************
  271. Private Sub ClientChange()
  272.    Dim Delta As Long
  273.    Dim VirtualRange As Long
  274.    Dim RealRange As Long
  275.    Dim Percent As Double
  276.    Dim tmpMin As Long
  277.    Dim tmpMax As Long
  278.    
  279.    ' Bail if recursing
  280.    If m_Recursing Then Exit Sub
  281.    
  282.    ' Calculate real change
  283.    Delta = m_Client.Value - m_vbValue
  284.    
  285.    ' Get current values from real scrollbar
  286.    Call ReadRealValues
  287.    
  288.    ' Cases where Virtual(Min>Max) need to be handled specially.
  289.    ' Most calculations can use swapped values.
  290.    If m_Min > m_Max Then
  291.       tmpMin = m_Max
  292.       tmpMax = m_Min
  293.       Delta = -1 * Delta
  294.    Else
  295.       tmpMin = m_Min
  296.       tmpMax = m_Max
  297.    End If
  298.    
  299.    ' See if Large or Small Change
  300.    If Abs(Delta) = m_vbLargeChange Or _
  301.       Abs(Delta) = m_vbSmallChange Then
  302.       
  303.       ' Adjust change to match virtual scaling
  304.       If Abs(Delta) = m_vbLargeChange Then
  305.          Delta = Sgn(Delta) * m_LargeChange
  306.       ElseIf Abs(Delta) = m_vbSmallChange Then
  307.          Delta = Sgn(Delta) * m_SmallChange
  308.       End If
  309.       
  310.       ' Set virtual scale
  311.       m_Value = m_Value + Delta
  312.       
  313.       ' Check if out of bounds
  314.       If m_Value < tmpMin Then
  315.          m_Value = tmpMin
  316.       ElseIf m_Value > tmpMax Then
  317.          m_Value = tmpMax
  318.       End If
  319.       
  320.       ' Calculate Real value of scrollbar
  321.       VirtualRange = Abs(m_Max - m_Min)
  322.       RealRange = Abs(m_vbMax - m_vbMin)
  323.       If VirtualRange Then
  324.          Percent = Abs(m_Value - tmpMin) / VirtualRange
  325.       Else
  326.          Percent = 0
  327.       End If
  328.       
  329.       ' If Virtual(Min>Max) then flip value
  330.       If m_Min <= m_Max Then
  331.          m_vbValue = m_vbMin + (Percent * RealRange)
  332.       Else
  333.          m_vbValue = m_vbMax - (Percent * RealRange)
  334.       End If
  335.  
  336.    Else
  337.       ' User moved thumb on scrollbar
  338.       ' Calculate Virtual value of scrollbar
  339.       VirtualRange = Abs(m_Max - m_Min)
  340.       RealRange = Abs(m_vbMax - m_vbMin)
  341.       If RealRange Then
  342.          Percent = Abs(m_vbValue - m_vbMin) / RealRange
  343.       Else
  344.          Percent = 0
  345.       End If
  346.       
  347.       ' If Virtual(Min>Max) then flip value
  348.       If m_Min <= m_Max Then
  349.          m_Value = tmpMin + (Percent * VirtualRange)
  350.       Else
  351.          m_Value = tmpMax - (Percent * VirtualRange)
  352.       End If
  353.    End If
  354.    
  355.    ' Update real scrollbar
  356.    Call UpdateRealValue
  357.    
  358.    ' Raise notification event
  359.    RaiseEvent Change
  360. End Sub
  361.  
  362. Private Sub ReadRealValues()
  363.    ' Read current values from scrollbar
  364.    m_vbValue = m_Client.Value
  365.    m_vbMin = m_Client.Min
  366.    m_vbMax = m_Client.Max
  367.    m_vbSmallChange = m_Client.SmallChange
  368.    m_vbLargeChange = m_Client.LargeChange
  369. End Sub
  370.  
  371. Private Sub UpdateRealValue()
  372.    ' This assures that if the virtual value is not quite
  373.    ' to either the Min or Max that there's still room to
  374.    ' adjust the slider.
  375.    If m_vbValue = m_vbMin Then
  376.      If m_Value > m_Min Then
  377.        m_vbValue = m_vbMin + 1
  378.      End If
  379.    ElseIf m_vbValue = m_vbMax Then
  380.      If m_Value < m_Max Then
  381.        m_vbValue = m_vbMax - 1
  382.      End If
  383.    End If
  384.    
  385.    ' Update display.  Note possible recursion!
  386.    m_Recursing = True
  387.       m_Client.Value = m_vbValue
  388.    m_Recursing = False
  389. End Sub
  390.