home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap25 / mdi / mdinote.bas < prev    next >
Encoding:
BASIC Source File  |  1995-07-26  |  6.5 KB  |  202 lines

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3.  
  4. Type FormState
  5.     Deleted As Integer
  6.     Dirty As Integer
  7.     Color As Long
  8. End Type
  9.  
  10. Public FState()  As FormState
  11. Public Document() As New frmNotePad
  12. Public gFindString, gFindCase As Integer, gFindDirection As Integer
  13. Public gCurPos As Integer, gFirstTime As Integer
  14. Public ArrayNum As Integer
  15. Public gToolsHidden As Boolean
  16. Public Const ThisApp = "MDINote" ' Registry App constant.
  17. Public Const ThisKey = "Recent Files" ' Registry Key constant.
  18.  
  19.  
  20. Function AnyPadsLeft() As Integer
  21.     Dim i As Integer
  22.  
  23.     ' Cycle through the document array.
  24.     ' Return true if there is at least one open document.
  25.     For i = 1 To UBound(Document)
  26.         If Not FState(i).Deleted Then
  27.             AnyPadsLeft = True
  28.             Exit Function
  29.         End If
  30.     Next
  31. End Function
  32.  
  33.  
  34. Sub EditCopyProc()
  35.     ' Copy the selected text onto the Clipboard.
  36.     Clipboard.SetText frmMDI.ActiveForm.ActiveControl.SelText
  37. End Sub
  38.  
  39. Sub EditCutProc()
  40.     ' Copy the selected text onto the Clipboard.
  41.     Clipboard.SetText frmMDI.ActiveForm.ActiveControl.SelText
  42.     ' Delete the selected text.
  43.     frmMDI.ActiveForm.ActiveControl.SelText = ""
  44. End Sub
  45.  
  46. Sub EditPasteProc()
  47.     ' Place the text from the Clipboard into the active control.
  48.     frmMDI.ActiveForm.ActiveControl.SelText = Clipboard.GetText()
  49. End Sub
  50.  
  51. Sub FileNew()
  52.     Dim fIndex As Integer
  53.  
  54.     fIndex = FindFreeIndex()
  55.     Document(fIndex).Tag = fIndex
  56.     Document(fIndex).Caption = "Untitled:" & fIndex
  57.     Document(fIndex).Show
  58.  
  59.     ' Make sure the toolbar edit buttons are visible.
  60.     frmMDI!imgcutbutton.Visible = True
  61.     frmMDI!imgcopybutton.Visible = True
  62.     frmMDI!imgPasteButton.Visible = True
  63. End Sub
  64.  
  65. Function FindFreeIndex() As Integer
  66.     Dim i As Integer
  67.     Dim ArrayCount As Integer
  68.  
  69.     ArrayCount = UBound(Document)
  70.  
  71.     ' Cycle through the document array. If one of the
  72.     ' documents has been deleted, then return that index.
  73.     For i = 1 To ArrayCount
  74.         If FState(i).Deleted Then
  75.             FindFreeIndex = i
  76.             FState(i).Deleted = False
  77.             Exit Function
  78.         End If
  79.     Next
  80.  
  81.     ' If none of the elements in the document array have
  82.     ' been deleted, then increment the document and the
  83.     ' state arrays by one and return the index to the
  84.     ' new element.
  85.     ReDim Preserve Document(ArrayCount + 1)
  86.     ReDim Preserve FState(ArrayCount + 1)
  87.     FindFreeIndex = UBound(Document)
  88. End Function
  89.  
  90. Sub FindIt()
  91.     Dim start, pos, findstring, sourcestring, Msg, Response, Offset
  92.     
  93.     If (gCurPos = frmMDI.ActiveForm.ActiveControl.SelStart) Then
  94.         Offset = 1
  95.     Else
  96.         Offset = 0
  97.     End If
  98.  
  99.     If gFirstTime Then Offset = 0
  100.     start = frmMDI.ActiveForm.ActiveControl.SelStart + Offset
  101.         
  102.     If gFindCase Then
  103.         findstring = gFindString
  104.         sourcestring = frmMDI.ActiveForm.ActiveControl.TEXT
  105.     Else
  106.         findstring = UCase(gFindString)
  107.         sourcestring = UCase(frmMDI.ActiveForm.ActiveControl.TEXT)
  108.     End If
  109.             
  110.     If gFindDirection = 1 Then
  111.         pos = InStr(start + 1, sourcestring, findstring)
  112.     Else
  113.         For pos = start - 1 To 0 Step -1
  114.             If pos = 0 Then Exit For
  115.             If Mid(sourcestring, pos, Len(findstring)) = findstring Then Exit For
  116.         Next
  117.     End If
  118.  
  119.     ' If the string is found...
  120.     If pos Then
  121.         frmMDI.ActiveForm.ActiveControl.SelStart = pos - 1
  122.         frmMDI.ActiveForm.ActiveControl.SelLength = Len(findstring)
  123.     Else
  124.         Msg = "Cannot find " & Chr(34) & gFindString & Chr(34)
  125.         Response = MsgBox(Msg, 0, App.Title)
  126.     End If
  127.     
  128.     gCurPos = frmMDI.ActiveForm.ActiveControl.SelStart
  129.     gFirstTime = False
  130. End Sub
  131.  
  132. Sub GetRecentFiles()
  133.     ' This procedure demonstrates the use of the GetAllSettings function,
  134.     ' which returns an array of values from the Windows registry. In this
  135.     ' case, the registry contains the files most recently opened.  Use the
  136.     ' SaveSetting statement to write the names of the most recent files.
  137.     ' That statement is used in the WriteRecentFiles procedure.
  138.     Dim i, j As Integer
  139.     Dim varFiles As Variant ' Varible to store the returned array.
  140.     
  141.     ' Get recent files from the registry using the GetAllSettings statement.
  142.     ' ThisApp and ThisKey are constants defined in this module.
  143.     If GetSetting(ThisApp, ThisKey, "RecentFile1") = Empty Then Exit Sub
  144.     
  145.     varFiles = GetAllSettings(ThisApp, ThisKey)
  146.     
  147.     For i = 0 To UBound(varFiles, 1)
  148.         
  149.         frmMDI!mnuRecentFile(0).Visible = True
  150.         frmMDI!mnuRecentFile(i).Caption = varFiles(i, 1)
  151.         frmMDI!mnuRecentFile(i).Visible = True
  152.             ' Iterate through all the documents and update each menu.
  153.             For j = 1 To UBound(Document)
  154.                 If Not FState(j).Deleted Then
  155.                     Document(j).mnuRecentFile(0).Visible = True
  156.                     Document(j).mnuRecentFile(i + 1).Caption = varFiles(i, 1)
  157.                     Document(j).mnuRecentFile(i + 1).Visible = True
  158.                 End If
  159.             Next j
  160.     Next i
  161.  
  162. End Sub
  163.  
  164. Sub OptionsToolbarProc(CurrentForm As Form)
  165.     CurrentForm.mnuOToolbar.Checked = Not CurrentForm.mnuOToolbar.Checked
  166.     If TypeOf CurrentForm Is MDIForm Then
  167.     Else
  168.         frmMDI.mnuOToolbar.Checked = CurrentForm.mnuOToolbar.Checked
  169.     End If
  170.     If CurrentForm.mnuOToolbar.Checked Then
  171.         frmMDI.picToolbar.Visible = True
  172.     Else
  173.         frmMDI.picToolbar.Visible = False
  174.     End If
  175. End Sub
  176.  
  177. Sub WriteRecentFiles(OpenFileName)
  178.     ' This procedure uses the SaveSettings statement to write the names of
  179.     ' recently opened files to the System registry. The SaveSetting
  180.     ' statement requires three parameters. Two of the parameters are
  181.     ' stored as constants and are defined in this module.  The GetAllSettings
  182.     ' function is used in the GetRecentFiles procedure to retrieve the
  183.     ' file names stored in this procedure.
  184.     
  185.     Dim i, j As Integer
  186.     Dim strFile, key As String
  187.  
  188.     ' Copy RecentFile1 to RecentFile2, and so on.
  189.     For i = 3 To 1 Step -1
  190.         key = "RecentFile" & i
  191.         strFile = GetSetting(ThisApp, ThisKey, key)
  192.         If strFile <> "" Then
  193.             key = "RecentFile" & (i + 1)
  194.             SaveSetting ThisApp, ThisKey, key, strFile
  195.         End If
  196.     Next i
  197.   
  198.     ' Write the open file to first recent file.
  199.     SaveSetting ThisApp, ThisKey, "RecentFile1", OpenFileName
  200. End Sub
  201.  
  202.