Jump to content
Sign in to follow this  
BoonPek

Text Editor

Recommended Posts

BoonPek

Here's a rather simple Text Editor I made. There could be some bugs that I'm not aware of. If you find any, you can tell me.

#NoTrayIcon

#include <Misc.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <GuiEdit.au3>
#include <String.au3>

$WIDTH = 800
$HEIGHT = 600
$EXT1 = "|Text Documents (*.txt)"
$EXT2 = "|AutoIt Scripts (*.au3)"
$EXT3 = "|All Files(*.*)"
$EXT = $EXT1 & $EXT2 & $EXT3
$SETTINGS = "Settings.ini"

Global $FILESAVE, $FILEOPEN = "", $SAVES, $SAVE, $FILEOPENED = 0

$FONTSIZE = IniRead($SETTINGS, "font", "size", 10)
$FONTWEIGHT = IniRead($SETTINGS, "font", "weight", 500)
$FONTATTRIB = IniRead($SETTINGS, "font", "attrib", "")
$FONTSTYLE = IniRead($SETTINGS, "font", "font", "Lucida Console")

$MAINGUI = GUICreate("Text Editor", $WIDTH, $HEIGHT, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE)

$EDITBOX = GUICtrlCreateEdit("", 0, 0, $WIDTH, $HEIGHT - 20, -1)

$FILE = GUICtrlCreateMenu("File")

$NEW = GUICtrlCreateMenuItem("New", $FILE)
$OPEN = GUICtrlCreateMenuItem("Open...", $FILE)
$SAVE = GUICtrlCreateMenuItem("Save", $FILE)
$SAVEAS = GUICtrlCreateMenuItem("Save As...", $FILE)
$PRINT = GUICtrlCreateMenuItem("Print...", $FILE)

GUICtrlCreateMenuItem("", $FILE)

$EXIT = GUICtrlCreateMenuItem("Exit", $FILE)

$EDIT = GUICtrlCreateMenu("Edit")
$UNDO = GUICtrlCreateMenuItem("Undo", $EDIT)

$FONT = GUICtrlCreateMenuItem("Font", $EDIT)

GUICtrlSetFont($EDITBOX, $FONTSIZE, $FONTWEIGHT, $FONTATTRIB, $FONTSTYLE)

GUISetState(@SW_SHOW & $GUI_ACCEPTFILES)

While 1
    $Msg = GUIGetMsg()
    Select

        Case $Msg = $GUI_EVENT_CLOSE
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                    Exit
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                    Exit
                EndIf
                If $SAVECHANGES = 7 Then
                    ExitLoop
                    Exit
                EndIf
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then Exit

        Case $Msg = $OPEN
            $FILEOPEN = FileOpenDialog("Open", $FILEOPEN, $EXT)
            $TEXT = FileRead($FILEOPEN, FileGetSize($FILEOPEN))
            GUICtrlSetData($EDITBOX, $TEXT)
            $FILEOPENED = 1

        Case $Msg = $NEW
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                EndIf
                If $SAVECHANGES = 7 Then GUICtrlSetData($EDITBOX, "")
                If $SAVECHANGES = 2 Then Sleep(1)
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then GUICtrlSetData($EDITBOX, "")
            $FILEOPENED = 0

        Case $Msg = $EXIT
            If _GUICtrlEdit_CanUndo($EDITBOX) Then
                $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
                If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
                    SaveAs()
                    Exit
                EndIf
                If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
                    FileDelete($FILEOPEN)
                    FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
                    Exit
                EndIf
                If $SAVECHANGES = 7 Then ExitLoop
                If $SAVECHANGES = 2 Then
                    Sleep(1)
                EndIf
            EndIf
            If Not _GUICtrlEdit_CanUndo($EDITBOX) Then Exit

        Case $Msg = $SAVEAS
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $SAVE And $FILEOPENED = 1
            FileDelete($FILEOPEN)
            FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
            _GUICtrlEdit_EmptyUndoBuffer($EDITBOX)

        Case $Msg = $SAVE And $FILEOPENED = 0
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $FONT
            $FONTCHOOSE = _ChooseFont($FONTSTYLE, $FONTSIZE, 0, $FONTWEIGHT)
            If Not @error Then
                GUICtrlSetFont($EDITBOX, $FONTCHOOSE[3], $FONTCHOOSE[4], $FONTCHOOSE[1], $FONTCHOOSE[2])
                IniWrite($SETTINGS, "font", "attrib", $FONTCHOOSE[1])
                IniWrite($SETTINGS, "font", "font", $FONTCHOOSE[2])
                IniWrite($SETTINGS, "font", "size", $FONTCHOOSE[3])
                IniWrite($SETTINGS, "font", "weight", $FONTCHOOSE[4])
            EndIf

        Case $Msg = $SAVE And $FILEOPENED = 0
            GUICtrlSetState($EDITBOX, $GUI_DISABLE)
            SaveAs()
            $FILEOPENED = 1
            GUICtrlSetState($EDITBOX, $GUI_ENABLE)

        Case $Msg = $PRINT
            $PRINTMSGBOX = MsgBox(35, "Text Editor", "Are you sure you want to print this page?")
            Select

                Case $PRINTMSGBOX = 6
                    If FileExists("Print.txt") Then
                        FileDelete("Print.txt")
                    EndIf
                    FileWrite("Print.txt", GUICtrlRead($EDITBOX))
                    $PRINTFILE = "Print.txt"
                    _FilePrint($PRINTFILE)
                    FileDelete($PRINTFILE)
            EndSelect

        Case $Msg = $UNDO
            _GUICtrlEdit_Undo($EDITBOX)

        Case _GUICtrlEdit_CanUndo($EDITBOX) And $SAVES = 1
            GUICtrlSetState($SAVE, $GUI_ENABLE)
            $SAVES = 0

        Case Not _GUICtrlEdit_CanUndo($EDITBOX) And $SAVES = 0
            GUICtrlSetState($SAVE, $GUI_DISABLE)
            $SAVES = 1
    EndSelect
WEnd
Exit

Func SaveAs()
    $FILESAVEAS = FileSaveDialog("Save As", "", "Text Document (*.txt)|All Files (*.*)")
    If Not @error Then
        $STRING = StringSplit($FILESAVEAS, ".")
        If $STRING[0] = 1 Then
            FileDelete($FILESAVEAS)
            FileWrite($FILESAVEAS & ".txt", GUICtrlRead($EDITBOX))
        Else
            FileWrite($FILESAVEAS, GUICtrlRead($EDITBOX))
        EndIf
    EndIf
EndFunc

Things I plan on fixing:

The changes to the font wont be printed out, it would stay to notepad's default. I will try to get past this by editing the configuration for notepad in the registry to the one in the editor and print, then restoring the settings.

Make some shortcut Keys like Ctrl+N for new. I could do this by using the HotKeySet, but I'll need alot of them so right now I'll find another way.

More Undo's. Currently you can only undo once, then redo. I don't know how I can do this, any help would be appreciated.

The File Save Dialog. I wanted to make it able to save autoit (*.au3) files as well but the function doesn't do it automatically.

Copy, Paste, Cut, Find, Replace and Goto.

Edited by BoonPek

Share this post


Link to post
Share on other sites
wakillon

Simple but it's a good start !

Thanks to share Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
Fullerene

excellent example.

Share this post


Link to post
Share on other sites
Aldunson

Looks good.

If I might make a suggestion. You can simplify a lot of your code into functions instead of repeating it.

Opt("GUIOnEventMode", 1)

$EXIT = GUICtrlCreateMenuItem("Exit", $FILE)
GUICtrlSetOnEvent($EXIT, "_Exit")

....

Case $Msg = $GUI_EVENT_CLOSE
        _Exit()

..............................

    Case $Msg = $SAVEAS
        _Save()
    Case $Msg = $SAVE And $FILEOPENED = 0
        _Save()


..............................

Func _Save()
    GUICtrlSetState($EDITBOX, $GUI_DISABLE)
    SaveAs()
    $FILEOPENED = 1
    GUICtrlSetState($EDITBOX, $GUI_ENABLE)
EndFunc   ;==>_Save

Func _Exit()
    If _GUICtrlEdit_CanUndo($EDITBOX) Then
        $SAVECHANGES = MsgBox(35, "Text Editor", "Your original file has been modified." & @CRLF & "Would you like to save changes to it?")
        If $SAVECHANGES = 6 And $FILEOPENED = 0 Then
            SaveAs()
            Exit
        EndIf
        If $SAVECHANGES = 6 And $FILEOPENED = 1 Then
            FileDelete($FILEOPEN)
            FileWrite($FILEOPEN, GUICtrlRead($EDITBOX))
            Exit
        EndIf
        If $SAVECHANGES = 7 Then
            Exit
        EndIf
    EndIf
    If Not _GUICtrlEdit_CanUndo($EDITBOX) Then Exit
EndFunc   ;==>_Exit

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×