' Copyright (C) 2006 Christopher Josph Dean Schaefer
'
' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either version 2
' of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Imports System.Drawing.Printing
Imports System.Text.RegularExpressions
Public Partial Class MainForm
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
Sub ExitToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'exit
end
End Sub
Sub OpenToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'open
OpenFileDialog1.ShowDialog(Me)
me.Text = "CPAC-TextEditor " & "(" & OpenFileDialog1.FileName & ")"
End Sub
Sub SaveToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'save
'check to see if the last file opened exists if so write over it.
If System.IO.File.Exists(OpenFileDialog1.FileName) = True Then
richtextbox1.SaveFile(OpenFileDialog1.FileName,RichTextBoxStreamType.PlainText)
me.Text = "CPAC-TextEditor " & "(" & OpenFileDialog1.FileName & ")"
End If
End Sub
Sub SaveAsToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'save as
SaveFileDialog1.ShowDialog(Me)
me.Text = "CPAC-TextEditor " & "(" & SaveFileDialog1.FileName & ")"
End Sub
Private WithEvents pdoc As New PrintDocument()
Sub PrintToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'print
PrintDialog1.Document = pdoc
If PrintDialog1.ShowDialog(me) = DialogResult.OK Then
pdoc.Print()
End If
End Sub
Sub UndoToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'undo
richtextbox1.Undo
End Sub
Sub RedoToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'redo
richtextbox1.Redo
End Sub
Sub CutToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'cut
richtextbox1.Cut
End Sub
Sub CopyToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'copy
richtextbox1.Copy
End Sub
Sub PasteToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Paste
richtextbox1.Paste
End Sub
Sub DeleteToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Delete
richtextbox1.SelectedText = ""
End Sub
Sub SelectAllToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Select All
richtextbox1.SelectAll
End Sub
Sub WordCountToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Word Count
msgbox(("There are " & (richtextbox1.Text.Split(" ").GetUpperBound(0)+1) & " words in this document."),MsgBoxStyle.Information,"Word Count")
End Sub
Sub LineCountToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Line Count
msgbox(("There are " & (richtextbox1.Lines.GetUpperBound(0)+1) & " lines in this document."),MsgBoxStyle.Information,"Word Count")
End Sub
Sub OpenFileDialog1FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
If system.IO.File.Exists(OpenFileDialog1.FileName) = True Then
Richtextbox1.LoadFile(OpenFileDialog1.FileName,RichTextBoxStreamType.PlainText)
End If
End Sub
Sub SaveFileDialog1FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
Richtextbox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText)
End Sub
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Static intCurrentChar As Int32
Dim font As New font("Microsoft Sans Serif", 24)
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(richtextbox1.Text, intCurrentChar + 1), font,New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt,intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(richtextbox1.Text, intCurrentChar + 1), font,Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < richtextbox1.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Sub RichTextBox1TextChanged(sender As Object, e As System.EventArgs)
pageSetupDialog1.Document = pdoc
ToolStripStatusLabel1.Text = "Ready"
End Sub
Sub PrintSetupToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'print setup
PageSetupDialog1.ShowDialog(me)
End Sub
Sub NewToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'new
richtextbox1.Clear
OpenFileDialog1.FileName = ""
SaveFileDialog1.FileName = ""
End Sub
Sub MainFormLoad(sender As Object, e As System.EventArgs)
pageSetupDialog1.Document = pdoc
End Sub
Sub TimeDateToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'insert time and date
Dim cache As String = ""
If system.Windows.Forms.Clipboard.ContainsText = True Then
cache = system.Windows.Forms.Clipboard.GetText()
End If
system.Windows.Forms.Clipboard.SetText(Date.Now.ToString)
richtextbox1.Paste
system.Windows.Forms.Clipboard.SetText(cache)
End Sub
Protected FindDialogInput As CPAC_Lib_CommonTypes.FindData
Protected LastFind As Integer
Sub FindToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Find
LastFind = 0
Dim fd As New CPAC_Lib_Dialogs.FindDialog()
fd.ShowDialog(Me)
FindDialogInput = fd.data
If FindDialogInput.MatchCase = True Then
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase or RichTextBoxFinds.Reverse)
End If
Else
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.Reverse)
End If
End If
if LastFind <= 0 then ToolStripStatusLabel1.Text = "Not Found" else ToolStripStatusLabel1.Text = "Ready"
End Sub
Sub FindNextToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Find Next
If FindDialogInput.MatchCase = True Then
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase or RichTextBoxFinds.Reverse)
End If
Else
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.Reverse)
End If
End If
End Sub
Protected ReplaceDialogInput As CPAC_Lib_CommonTypes.ReplaceData
Sub ReplaceToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Replace
Dim rd As New CPAC_Lib_Dialogs.ReplaceDialog
rd.ShowDialog(Me)
ReplaceDialogInput = rd.data
If ReplaceDialogInput.Pattern <> "" Then
If ReplaceDialogInput.FindOnly = True Then
If ReplaceDialogInput.MatchCase = False Then
LastFind = 1 + richtextbox1.Find(ReplaceDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(ReplaceDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
End If
Else
dim reg as New Regex(ReplaceDialogInput.Pattern)
If ReplaceDialogInput.ReplaceOnce = False Then
richtextbox1.Text = reg.Replace(richtextbox1.Text,ReplaceDialogInput.Replacement)
Else
richtextbox1.Text = reg.Replace(richtextbox1.Text,ReplaceDialogInput.Replacement,1)
End If
End If
end if
End Sub
Sub WordWrapToolStripMenuItemClick(sender As Object, e As System.EventArgs)
If WordWrapToolStripMenuItem.Checked = True Then
WordWrapToolStripMenuItem.Checked = false
richtextbox1.WordWrap = WordWrapToolStripMenuItem.Checked
Else
WordWrapToolStripMenuItem.Checked = True
richtextbox1.WordWrap = WordWrapToolStripMenuItem.Checked
End If
End Sub
Sub FontToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Select Font
If FontDialog1.ShowDialog(Me) = DialogResult.OK Then
If richtextbox1.SelectedText.Length > 0 Then
richtextbox1.SelectionFont = FontDialog1.Font
Else
richtextbox1.Font = FontDialog1.Font
End If
End If
End Sub
Sub AboutToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'About Box
Dim ab As New About
ab.ShowDialog(me)
End Sub
End Class