home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / grdrtn / grid.bas < prev    next >
Encoding:
BASIC Source File  |  1995-05-09  |  9.4 KB  |  315 lines

  1. DefInt A-Z
  2.  
  3. 'This program is Copyright 1991 Nelson Ford, PsL.
  4. 'Some routines have been developed by or with the help of
  5. '  others. (The Grid Clip technique came from Jim Dolson.)
  6. 'You may freely use the individual routines from this
  7. '  Grid.Bas in your own applications.
  8.  
  9. '=========================================================
  10. 'Distribution of Grid.Bas source code to others is allowed
  11. '  ONLY if no changes are made to this file, including
  12. '  these notes.
  13. '=========================================================
  14.  
  15. 'If you have improvements, please send them to
  16. '    Nelson Ford
  17. '    Public (software) Library
  18. '    P.O.Box 35705
  19. '    Houston, TX 77235-5705
  20. '    800-242-4PsL  (for info: 713-524-6394)
  21. '    (The above are _NOT_ bbs numbers!)
  22. '    CIS: 71355,470
  23. '  Call or write for a free copy of our monthly shareware
  24. '    magazine and catalog, which includes numerous other
  25. '    routines for Visual Basic available for a low disk fee.
  26. '  Click on each "Proc." for more documentation.
  27.  
  28. 'The following is just for reference:
  29. 'Grid Parameters:
  30. '   Grid1.Cols = # of columns
  31. '   Grid1.Rows = # of rows
  32. '   Grid1.Col = current column
  33. '   Grid1.Row = current row
  34. '   Grid1.ColWidth = width of the current column
  35. '   Grid1.RowHeight = width of the current row
  36. '   Grid1.ColAlignment = 0=L, 1=C, 2=R
  37. '   Grid1.Text = contents of the current cell
  38. '   .CellSelected = T/F
  39. '   .SelStartRow = first selected row
  40. '   .SelEndRow = last selected row
  41. '   .SelStartCol = first selected column
  42. '   .SelEndCol = last selected column
  43. '   .TopRow = first visible row
  44. '   .LeftCol = first visible column
  45.  
  46. 'Put the following in the calling form if you use the
  47. '  MoveCell routine: (These routines are already in the
  48. '  GridInpt.Frm file.)
  49.  
  50. 'Declare Function ControlhWnd Lib "ctlhwnd.dll" (Ctl As Control) As Integer
  51. 'Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
  52. 'Declare Function GetWindow Lib "User" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
  53. 'Declare Function GetNextWindow Lib "User" (ByVal hWnd As Integer, ByVal wFlag As Integer) As Integer
  54. 'Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Long
  55. 'Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
  56.  
  57. 'Const WS_CLIPSIBLINGS = &H4000000
  58. 'Const SWP_NOSIZE = &H1
  59. 'Const SWP_NOMOVE = &H2
  60. 'Const GW_HWNDNEXT = 2
  61. 'Const GW_CHILD = 5
  62. 'Const GWL_STYLE = (-16)
  63.  
  64. 'Sub SetClipSiblingBit (ByVal hWndParent As Integer)
  65. '  Dim hWndChild As Integer
  66. '  Dim Style As Long
  67. '  hWndChild = GetWindow(hWndParent, GW_CHILD)
  68. '  Do
  69. '    Style = GetWindowLong(hWndChild, GWL_STYLE)
  70. '    Style = Style Or WS_CLIPSIBLINGS
  71. '    Style = SetWindowLong(hWndChild, GWL_STYLE, Style)
  72. '    If GetWindow(hWndChild, GW_CHILD) <> 0 Then
  73. '      Call SetClipSiblingBit(hWndChild)
  74. '    End If
  75. '    hWndChild = GetNextWindow(hWndChild, GW_HWNDNEXT)
  76. '  Loop Until hWndChild = 0
  77. 'End Sub
  78.  
  79. Sub ClearGrid (G As Control)
  80. ' Clears the entire grid.
  81.   G.SelStartRow = 0
  82.   G.SelStartCol = 0
  83.   G.SelEndRow = G.Rows - 1
  84.   G.SelEndCol = G.Cols - 1
  85.   G.Clip = ""
  86.   G.SelEndRow = 0
  87.   G.SelEndCol = 0
  88. End Sub
  89.  
  90. Sub DelRow (G As Control)
  91.   If G.CellSelected = 0 Then
  92.     If G.SelStartRow = G.SelEndRow Then
  93.       G.row = G.SelStartRow
  94.       G.Col = G.SelStartCol
  95.     Else
  96.       MsgBox "First click on a line to select it."
  97.       Exit Sub
  98.     End If
  99.   End If
  100.   G.SelStartRow = G.row
  101.   G.SelStartCol = 0
  102.   G.SelEndCol = G.Cols - 1
  103.   X = MsgBox("Are you sure?", 52, "Delete Line")
  104.   If X = 6 Then
  105.     G.SelStartRow = G.row + 1
  106.     G.SelEndRow = G.Rows - 1
  107.     G.SelStartCol = 0
  108.     G.SelEndCol = G.Cols - 1
  109.     c$ = G.Clip
  110.     G.SelStartRow = G.SelStartRow - 1
  111.     G.SelEndRow = G.Rows - 2
  112.     G.Clip = c$
  113.     G.SelStartRow = G.row
  114.     G.SelEndRow = G.row
  115.     G.SelStartCol = 0
  116.     G.SelEndCol = G.Cols - 2
  117.   End If
  118.   G.SetFocus
  119. End Sub
  120.  
  121. Sub DelRowNoPrompt (G As Control)
  122.   G.SelStartRow = G.row + 1
  123.   G.SelEndRow = G.Rows - 1
  124.   G.SelStartCol = 0
  125.   G.SelEndCol = G.Cols - 1
  126.   c$ = G.Clip
  127.   G.SelStartRow = G.SelStartRow - 1
  128.   G.SelEndRow = G.Rows - 2
  129.   G.Clip = c$
  130.   G.SelStartRow = G.row
  131.   G.SelEndRow = G.row
  132.   G.SelStartCol = 0
  133.   G.SelEndCol = G.Cols - 2
  134. End Sub
  135.  
  136. Sub Grid2Clip (G As Control)
  137.   G.SelStartRow = 0
  138.   G.SelStartCol = 0
  139.   G.SelEndRow = G.Rows - 1
  140.   G.SelEndCol = G.Cols - 1
  141.   ClipBoard.SetText (G.Clip)
  142.   G.SelStartRow = G.row
  143.   G.SelStartCol = 0
  144.   G.SelEndRow = G.row
  145.   G.SelEndCol = 0
  146.  
  147. End Sub
  148.  
  149. Sub HiLiteRow (G As Control)
  150.   G.SelStartRow = G.row
  151.   G.SelEndRow = G.row
  152.   G.SelStartCol = 0
  153.   G.SelEndCol = G.Cols - 1
  154. End Sub
  155.  
  156. Sub InsertRow (G As Control)
  157.   G.SelStartRow = G.row
  158.   G.SelStartCol = G.Col
  159.   G.SelEndRow = G.Rows - 2
  160.   G.SelEndCol = G.Cols - 1
  161.   X$ = G.Clip
  162.   G.SelStartRow = G.row + 1
  163.   G.SelEndRow = G.Rows - 1
  164.   G.Clip = X$
  165.   c = G.Col
  166.   For i = G.FixedCols To G.Cols - 1
  167.     G.Col = i
  168.     G.Text = ""
  169.   Next
  170.   G.Col = c
  171.   G.SelStartRow = G.row
  172.   G.SelEndRow = G.row
  173. End Sub
  174.  
  175. Sub LoadGrid (G As Control, Fi$)
  176.     Open Fi$ For Binary Access Read As 1 Len = 1
  177.     X = LOF(1)
  178.     Close #1
  179.     Open Fi$ For Binary Access Read As 1 Len = X
  180.     Temp$ = Space$(X)
  181.     Get 1, 1, Temp$
  182.     G.SelStartRow = G.FixedRows
  183.     G.SelStartCol = G.FixedCols
  184.     G.SelEndRow = G.Rows - 1
  185.     G.SelEndCol = G.Cols - 1
  186.     G.Clip = Temp$
  187.     Close #1
  188.     G.SelEndRow = G.SelStartRow
  189.     G.SelEndCol = G.SelStartCol
  190. End Sub
  191.  
  192. Sub MoveCell (G As Control, Cell As Control, KeyCode)
  193.   'Notes:
  194.   '  For some reason, VB does not allow the Text box to be
  195.   '    as short as a Grid Row. VBTools' text box does.
  196.   '  Alternatively, you can do a loop to set RowHeight to
  197.   '    Text.Height for each row in the Grid.
  198.   '  Or you can leave the oversized Text box, which is not
  199.   '    all that bad looking.
  200.   '  In this procedure, you will notice that I've had to
  201.   '    use "fudge factors" (eg: -50, +15, +20). I don't
  202.   '    know why this is necessary, but it is. I haven't
  203.   '    tested this on other video types, so I don't know
  204.   '    if these fudge factors are constant on all types.
  205.   '  I've already put a lot of time into this and didn't
  206.   '    have more time to get the Grid scroll bars to work
  207.   '    with this. For example, setting G.TopRow back to 0
  208.   '    will not move the vertical scroll bar marker back
  209.   '    to the top. I assume that this coordination will
  210.   '    require API calls.
  211.   '  Another routine that needs to be added is the ability
  212.   '    to click on a cell and have the Text box move to it.
  213.   '  If I were making this shareware, which I considered,
  214.   '    I would have polished up all these things, but hey,
  215.   '    waddya want for nothing?
  216.   '  If someone else adds these things, I hope you will
  217.   '    likewise share.
  218.  
  219.   G.Text = Cell.Text
  220.   Select Case KeyCode
  221.   Case 40  'Down cursor
  222.     If G.row < G.Rows - 1 Then
  223.       If Cell.Top - G.Top + G.RowHeight * 2 > G.Height Then
  224.         G.TopRow = G.TopRow + 1
  225.       Else
  226.         Cell.Top = Cell.Top + G.RowHeight + 15
  227.       End If
  228.       G.row = G.row + 1
  229.     Else
  230.       Beep ' alternatively, use the next two lines to wrap
  231.       'G.Row = G.TopRow
  232.       'Cell.Top = G.Top
  233.     End If
  234.   Case 38  'Up cursor
  235.     If G.row > 0 Then
  236.       If G.row <= G.TopRow And G.TopRow > 0 Then
  237.         G.TopRow = G.TopRow - 1
  238.       Else
  239.         Cell.Top = Cell.Top - G.RowHeight - 15
  240.       End If
  241.       G.row = G.row - 1
  242.     Else
  243.       Beep
  244.     End If
  245.   Case 39  'Right cursor
  246.     If G.Col = G.Cols - 1 Then
  247.       Beep 'or wrap:
  248.       'G.Col = G.LeftCol
  249.       'Cell.Left = G.Left
  250.       'Cell.Width = G.ColWidth + 20
  251.     Else
  252.       Cell.Left = Cell.Left + G.ColWidth + 20
  253.       G.Col = G.Col + 1
  254.       Cell.Width = G.ColWidth + 20
  255.       If Cell.Left - G.Left + Cell.Width > G.Width Then
  256.         G.LeftCol = G.LeftCol + 1
  257.         Cell.Left = Cell.Left - G.ColWidth - 20
  258.       End If
  259.     End If
  260.   Case 37  'Left Cursor
  261.     If G.Col > 0 Then
  262.       G.Col = G.Col - 1
  263.       Cell.Width = G.ColWidth + 20
  264.       If G.Col < G.LeftCol Then
  265.         G.LeftCol = G.LeftCol - 1
  266.         Cell.Left = G.Left
  267.       Else
  268.         Cell.Left = Cell.Left - G.ColWidth - 20
  269.       End If
  270.     Else
  271.       Beep
  272.       'G.Col = G.Cols - 1
  273.       'Cell.Width = G.ColWidth + 20
  274.     End If
  275.   Case 36  'Home
  276.     Cell.Left = G.Left
  277.     Cell.Top = G.Top
  278.     G.Col = G.LeftCol
  279.     G.row = G.TopRow
  280.   Case 35  'End
  281.     'put in code for End, PgUp, PgDn,
  282.     '   Ctrl-PgUp (top of Grid), Ctrl-PgDn, etc.
  283.   End Select
  284.   KeyCode = 0 'throw away the cursor movement
  285.   Cell.Text = G.Text
  286.   Cell.SelStart = 0
  287.   Cell.SelLength = 32767
  288.   'keep the Grid highlight under the Text box:
  289.   G.SelStartCol = G.Col
  290.   G.SelEndCol = G.Col
  291.   G.SelStartRow = G.row
  292.   G.SelEndRow = G.row
  293. End Sub
  294.  
  295. Sub SaveGrid (G As Control, Fi$, Cancel)
  296. ' The Cancel is included for when you call SaveGrid from
  297. '   a Form_Unload procedure.
  298.   X = MsgBox("Save data to disk?", 35)
  299.   If X = 2 Then
  300.     Cancel = -1
  301.     G.SetFocus
  302.   ElseIf X = 6 Then
  303.     G.SelStartRow = G.FixedRows
  304.     G.SelEndRow = G.Rows - 1
  305.     G.SelStartCol = G.FixedCols
  306.     G.SelEndCol = G.Cols - 1
  307.     Open Fi$ For Output As #1
  308.     Print #1, G.Clip
  309.     Close #1
  310.     G.SelEndRow = G.SelStartRow
  311.     G.SelEndCol = G.SelStartCol
  312.   End If
  313. End Sub
  314.  
  315.