Attribute VB_Name = "ModUndoRedo"
' ModUndoRedo.bas

Option Explicit

' Undo/Redo
Public SAVEHEADER() As Long
Public Gen As Long
Public MaxGen As Long
Public CurrentGen As Long

Public Sub KILLSAVDATS()
Dim k As Long
Dim oldName$
   For k = 1 To MaxGen
      oldName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(k)) & ".dat"
      If FileExists(oldName$) Then
         Kill oldName$
         DoEvents
      End If
   Next k
End Sub

Public Sub NewSaveDATA(frm As Form, REGALL As Long)
Dim newName$
Dim fnum As Long
Dim mHDC As Long, mBMPold As Long
   With frm
      KILLSAVDATS
      .cmdUnReDo(0).Enabled = False
      .cmdUnReDo(1).Enabled = False
      
      SAVEHEADER(0) = .picDRAW.Width
      SAVEHEADER(1) = .picDRAW.Height
      SAVEHEADER(2) = xlo
      SAVEHEADER(3) = ylo
      SAVEHEADER(4) = REGALL   '0/1  ' 0 saved picDATAREG(), 1 saved whole picDATA()
      SAVEHEADER(5) = FullW
      SAVEHEADER(6) = FullH
      If SAVEHEADER(4) = 0 Then
         With BHIDATAREG
            .biSize = 40
            .biPlanes = 1
            .biWidth = SAVEHEADER(0)
            .biHeight = SAVEHEADER(1)
            .biBitCount = 32
         End With
         ReDim picDATAREG(0 To 3, 0 To SAVEHEADER(0) - 1, 0 To SAVEHEADER(1) - 1)
         mHDC = CreateCompatibleDC(0)
         mBMPold = SelectObject(mHDC, .picDRAW.Image)
         GetDIBits mHDC, .picDRAW.Image, 0, SAVEHEADER(1), picDATAREG(0, 0, 0), BHIDATAREG, 0
         SelectObject mHDC, mBMPold
         DeleteDC mHDC
         DeleteObject mBMPold
             
      End If
   End With
   
   Gen = 1
   CurrentGen = Gen
   newName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(CurrentGen)) & ".dat"
   fnum = FreeFile
   Open newName$ For Binary As #fnum
   Put #fnum, , SAVEHEADER()
   If SAVEHEADER(4) = 0 Then
      Put #fnum, , picDATAREG()
   Else
      Put #fnum, , picDATA()
   End If
   Close #fnum
End Sub


Public Sub SaveDATA(frm As Form, REGALL As Long)
' REGALL 0 save picDATREG(), 1 Save picDATA()
Dim k As Long
Dim fnum As Long
Dim oldName$, newName$
      SAVEHEADER(0) = frm.picDRAW.Width
      SAVEHEADER(1) = frm.picDRAW.Height
      SAVEHEADER(2) = xlo
      SAVEHEADER(3) = ylo
      SAVEHEADER(4) = REGALL   '0/1  ' 0 saved picDATAREG(), 1 saved whole picDATA()
      SAVEHEADER(5) = FullW
      SAVEHEADER(6) = FullH
      ' SavePictures   ' Undo, Redo
      If CurrentGen = Gen Then
         Form1.LabGen.ForeColor = vbRed

         If Gen + 1 > MaxGen Then  ' Gen=10
            oldName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(1)) & ".dat"
            If FileExists(oldName$) Then
               Kill oldName$
               DoEvents
            End If
            For k = 2 To MaxGen
               oldName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(k)) & ".dat"
               newName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(k - 1)) & ".dat"
               Name oldName$ As newName$
            Next k
            oldName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(MaxGen)) & ".dat"
            If FileExists(oldName$) Then
               Kill oldName$
               DoEvents
            End If
            Gen = MaxGen - 1  ' 9 will become 10
            frm.cmdUnReDo(1).Enabled = False
         End If
         Gen = Gen + 1
         CurrentGen = Gen
         newName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(Gen)) & ".dat"
         If FileExists(newName$) Then
            Kill newName$
            DoEvents
         End If
         fnum = FreeFile
         Open newName$ For Binary As #fnum
         Put #fnum, , SAVEHEADER()
         If SAVEHEADER(4) = 0 Then
            Put #fnum, , picDATAREG()
         Else
            Put #fnum, , picDATA()
         End If
         Close #fnum
      
      ElseIf CurrentGen < Gen Then
         ' Kill CurrentGen+1 to gen
         For k = CurrentGen + 1 To Gen
            oldName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(k)) & ".dat"
            If FileExists(oldName$) Then
               Kill oldName$
               DoEvents
            End If
         Next k
         CurrentGen = CurrentGen + 1
         Gen = CurrentGen
         
         fnum = FreeFile
         newName$ = PathSpec$ & "SAVDAT" & Trim$(Str$(Gen)) & ".dat"
         Open newName$ For Binary As #fnum
         Put #fnum, , SAVEHEADER()
         If SAVEHEADER(4) = 0 Then
            Put #fnum, , picDATAREG()
         Else
            Put #fnum, , picDATA()
         End If
         Close #fnum
         
         frm.cmdUnReDo(1).Enabled = False
      End If
      If CurrentGen = 1 And Gen > 1 Then
         frm.cmdUnReDo(0).Enabled = False  ' X Undo
         frm.cmdUnReDo(1).Enabled = True   '  ReDo
      ElseIf CurrentGen = Gen And Gen > 1 Then
         frm.cmdUnReDo(1).Enabled = False  ' X ReDo
         frm.cmdUnReDo(0).Enabled = True   ' Undo
      End If
      frm.LabGen.Caption = Str$(CurrentGen)
End Sub


