Jump to content

Simple Text Editor (STE) [TUTORIAL]


dbzfanatic
 Share

Recommended Posts

I made this to give people an idea of how to do certain things with autoit. The "hello world" program doesn't really teach much and "Autoit 1-2-3" is more for theory than practice. This way people can see actual working code and understand it without downloading anything extra and possibly be inspired to use a new function in their programs to achieve a new result they've wanted. I hope that this could be a stepping-stone for people and I may update it with more menus, options, error checking, and shortcuts/features in the future but for now I believe it's a good place to start concerning something like this.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.13.7 (beta)
 Author:         dbzfanatic

 Script Function:
    Simpel Text Editor (STE)

#ce ----------------------------------------------------------------------------


#Region ### Includes Section. Add includes here. Includes add functions to your program to make it work. ###
#include <Misc.au3>
#include <File.au3>
#include <String.au3>
#include <GUIEdit.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#EndRegion ### End Includes Section ###

#Region ### Variable Declarations ### Declare your variables here for use later. ###
Dim $data, $original;initialize the variables.
Global $file;declaring a variable global means functions can change the value and this new value can be used by other functions.
#EndRegion ### End Variable Declarations. ###

#Region ### Command Line Parameters. ### Allows use of parameters before the program starts. Specify any parameters here. ###
If $cmdLine[0] > 0 Then;Check if any parameters are actually given.
    If $cmdLine[1] <> "" Then;Make sure the parameter(s) passed aren't blank. We'll only use one parameter here, the file to be opened.
        $file = $cmdLine[1];Save the filename to the variable $file if the parameter isn't blank.
    EndIf;End checking parameter 1 (filename)
EndIf;End parameter checking.
#EndRegion ### End Command Line Parameters ###

#Region ### Startup Checks ###
If $file <> "" Then;Check if a file was dragged onto the program.
    FileOpen($file,0); Open the file in read mode to prevent accidental errasure in case of an error.
    If @error = -1 Then;Check for error opening the file.
        MsgBox(48,"Error","Could not open the file.");Show the user a window letting them know there was an error.
    EndIf;End error checking.
    $data = FileRead($file);Read the file. Notice how the variable $file is used as the handle. Much easier than typing the path and faster.
    $original = $data;set the original text to a variable called $original for checking.
    If @error = 1 Then;Check if there was an error reading the file.
        MsgBox(48,"Error","Could not read the file contents.");Show the user a window letting them know the file couldn't be read.
    EndIf;End error checking.
EndIf;End checking for file and reading the data.
#EndRegion ### End Startup Checks ###

;HotKeySet("^s","_Save") ###Set the hotkey (shortcut) of ctrl+s to the function _Save(). ^ means ctrl in autoit and ctrl means control. Commented out due to limitations.
HotKeySet("^o","_Open");Set the hotkey (shortcut) of ctrl+o to the function _Open(). ^ means ctrl in autoit and ctrl means control.

#Region ### START Koda GUI section ### Form=
$frmTextEditor = GUICreate("Sample Text Editor (STE)", 620, 435, -1, -1);create the basic form. Width = 620, Height - 435. Left and Top = -1. -1 Is default (centered in this case)
$txtEdit = _GUICtrlEdit_Create($frmTextEditor,"", 0, 0, 620, 435, BitOr($ES_MULTILINE,$ES_WANTRETURN, $ES_AUTOVSCROLL));Create the actual place to edit text. Left and Top = 0. Sets it so that the start is the top left corner. Width = 620, Height = 435. Makes it the same size as the window.
;Style = BitOr($ES_MULTILINE,$ES_WANTRETURN, $ES_AUTOVSCROLL, $ES_NOHIDSEL). BitOr combines the selected styles. $ES_MULTILINE and $ES_WANTRETURN allow new lines by pressing enter. $ES_AUTOVSCROLL allows a vertical scroll bar.
GUICtrlSetResizing(-1,102);Set the resizing. Control = -1. -1 is the last control created/used. Resizing = 102. Makes it grow or shrink with the window.
$mnuFile = GUICtrlCreateMenu("&File");Create the file menu. This is the main menu and will have the sub-menus.
$mnuOpen = GUICtrlCreateMenuItem("&Open", $mnuFile);Create the "Open" menu item.
$mnuSave = GUICtrlCreateMenuItem("&Save", $mnuFile);Create the "Save" menu item.
$mnuSaveAs = GUICtrlCreateMenuItem("Save As...", $mnuFile);Create the "Save As..." menu item.
$div = GUICtrlCreateMenuItem("",$mnuFile);Create a divider. If you leave the text blank a divider is formed.
$mnuExit = GUICtrlCreateMenuItem("&Exit", $mnuFile);Create the "Exit" menu item.
#EndRegion ### END Koda GUI section ###

_GUICtrlEdit_SetText($txtEdit,$data);Set the file contents to be displayed.
GUISetState();Show the window.

#Region ### Main body of program ###
While 1;Do everything inside this loop while the program is running.
    If _IsPressed("11") = 1 And _IsPressed("53") = 1 Then;Check to see if ctrl+s is pressed
        _Save(_GUICtrlEdit_GetText($txtEdit));Call the function _Save(), sending the text retrieved from $txtEdit as a parameter
    EndIf;End check
    If _IsPressed("11") = 1 And _IsPressed("41") = 1 Then;Check to see if ctrl+a is pressed
        _GUICtrlEdit_SetSel($txtEdit,0,-1);select all text.
    EndIf;End check
    $nMsg = GUIGetMsg();Check what's happening in the GUI (Graphical User Interface)
    Switch $nMsg;Run through GUI events
        Case $GUI_EVENT_CLOSE;If the GUI is closed
            _Exit();Call the exit function
        Case $mnuOpen;Was the Open menu item clicked?
            _Open();If so call the function _Open()
        Case $mnuSave;Was the Save menu item clicked?
            _Save(_GUICtrlEdit_GetText($txtEdit));if so read the contents of the edit and pass them as a parameter to the function _Save()
        Case $mnuSaveAs
            $file = FileSaveDialog("Select file.",@WorkingDir,"text files (*.txt) | All files (*.*)",16);Select/create a file to save to.
            _Save(_GUICtrlEdit_GetText($txtEdit));Call the save function with current text as the parameter
        Case $mnuExit
            
    EndSwitch;End running throug GUI events.
    Sleep(20);Add a tiny (20 milliseconds) delay to cut down on CPU usage. Not really needed since GUIGetMsg() has a built-in delay.
WEnd;End main body loop
#EndRegion ### End the main body ###

#Region ### Custom Functions ###
Func _Open();Name the function, deisgnate it as a function, and set parameters here.
    $file = FileOpenDialog("Open",@WorkingDir,"text (*.txt)| all (*.*)");Start an open file dialog. Define text files (*.txt) as one filter, define all files (*.*) as a second filter. Open the file from the last used directory.
    If @error = 1 Then;Check to see if file selection failed
        MsgBox(48,"Error", "Could not open file or no file selected.");Show the user a window telling them the file could not be opened.
    Else;If no error
        $data = FileRead($file);Read the data of the file
        _GUICtrlEdit_SetText($txtEdit,$data);Set the data to be displayed
    EndIf;End checking.
EndFunc;End function specification.

Func _Save($sData);Name the function, deisgnate it as a function, and set parameters here. Parameter $sData. $s means string type, Data is the variable name. $sData is optional since it is given a default value. Default value is any/all text in the edit.
    FileClose($file);Close the file to free up memory for writing.
    FileOpen($file,2);Open the file into write mode so the changes can be saved.
    If @error = -1 Then;Check if the file can't be opened in write mode.
        MsgBox(48,"Error","File could not be written to.");Display a window telling the user the file could not be written to
    EndIf;End error checking
    FileWrite($file,$sData);Save the changes.
    If @error = 1 Then;Check if the write has failed
        MsgBox(48,"Error","Could not write file contents.");Display a window telling the user the write has failed.
    EndIf;End error checking.
    $original = $sData;set the newly-saved text as the text to check against.
EndFunc;End function specification.

Func _Exit()
    If _GUICtrlEdit_GetText($txtEdit) <> $original Then;Check the text in the edit and compare it to the original text. If it's different
    $ask = MsgBox(35,"Save?","Save changes to file?");ask if they want to save the changes.
        If $ask = 6 Then;If the user said yes save the file
            _Save(_GUICtrlEdit_GetText($txtEdit));Send the text to the function _Save() as a parameter.
            Exit;Close the program.
        ElseIf $ask = 7 Then;If the user said No exit without saving.
            Exit
        Else;If they clicked cancel or the exit button
        ;Don't do anything
        EndIf;End asking user to save
    Else;if it's the same
        Exit
    EndIf;End exit
EndFunc;End function specification
#EndRegion ### End Custom Functions ###

If you can't read it very well here just copy/paste it into SciTE and voila, you can read it the way I did when it was written.

Edited by dbzfanatic
Link to comment
Share on other sites

It is a nice simple example. After a quick look at the code, I do notice save/save as/exit don't do anything. Save does have a while loop instance and a function but doesn't seem top work. save as and exit do not have a function in place. I still concider myself as a novice at best but these things would need to be addressed before sending it out for a newbie example to follow right? But a nice start...

Link to comment
Share on other sites

I've been up for roughly 22 hours so forgive me overlooking this :) . I'll fix it soon. Save does work so long as the file is already there to be opened, I tested that myself (a lot) before posting. I'm working on it now so the code should be updated within a few minutes.

Edit: fixed the code in the previous post. also if you mean it doesn't seem to work because you get no dialog that happens with save as. Save just saves to whatever file is currently open and I haven't implemented checking to see if a file actually is open yet and I'm too tired to do it right now. Maybe when I wake up >_<.

Edited by dbzfanatic
Link to comment
Share on other sites

Yeah this is it. I'm really hoping this can help some people out with learning the basic ideas of constructing their own scripts. It's not complicated, it works, and the comments explain pretty much everything. If anyone has ideas to make this a bit better (changing comments, display/select data a different way, etc) I'm open to suggestions. :)

Link to comment
Share on other sites

I think I know one,

Case $GUI_EVENT_CLOSE;If the GUI is closed
            _Exit();Call the exit function
        Case $mnuOpen;Was the Open menu item clicked?
            _Open();If so call the function _Open()
        Case $mnuSave;Was the Save menu item clicked?
            _Save(_GUICtrlEdit_GetText($txtEdit));if so read the contents of the edit and pass them as a parameter to the function _Save()
        Case $mnuSaveAs
            $file = FileSaveDialog("Select file.",@WorkingDir,"text files (*.txt) | All files (*.*)",16);Select/create a file to save to.
            _Save(_GUICtrlEdit_GetText($txtEdit));Call the save function with current text as the parameter
        Case $mnuExit

You could change it to

Case $GUI_EVENT_CLOSE; If the red [X] is clicked
            _Exit(); Start function '_Exit()'
        Case $mnuOpen; If the 'Open' menu was clicked
            _Open(); Start function '_Open()'
        Case $mnuSave; If the 'Save' menu was clicked
            _Save(_GUICtrlEdit_GetText($txtEdit)); Read 't$xtEdit' with the _GUICtrlEdit_GetText() Command ==> Start function '_Save()'
        Case $mnuSaveAs; If the 'Save As' menu was clicked
            $file = FileSaveDialog("Select file.",@WorkingDir,"text files (*.txt) | All files (*.*)",16); Opens a Save dialog. The user can now choose a path where to save, and give his/her work a name
            _Save(_GUICtrlEdit_GetText($txtEdit)); Read 't$xtEdit' with the _GUICtrlEdit_GetText() Command ==> Start function '_Save()'
        Case $mnuExit; If the 'Exit' menu was clicked

AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

I'll think about it. Not bad suggestions but I don't want to hand-feed everything to people. I want this to be a mid-level tutorial, teaching people how to do things and yet making them think a bit too.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...