VERSION DVX 1.00
Begin Form NotepadForm
    Caption   = "Untitled - Notepad"
    Layout    = VBox
    AutoSize  = False
    Resizable = True
    Centered  = True
    Width     = 480
    Height    = 320
    Begin Menu mnuFile
        Caption = "&File"
        Begin Menu mnuNew
            Caption = "&New"
        End
        Begin Menu mnuOpen
            Caption = "&Open..."
        End
        Begin Menu mnuSep1
            Caption = "-"
        End
        Begin Menu mnuSave
            Caption = "&Save"
        End
        Begin Menu mnuSaveAs
            Caption = "Save &As..."
        End
        Begin Menu mnuSep2
            Caption = "-"
        End
        Begin Menu mnuExit
            Caption = "E&xit"
        End
    End
    Begin Menu mnuEdit
        Caption = "&Edit"
        Begin Menu mnuCut
            Caption = "Cu&t"
        End
        Begin Menu mnuCopy
            Caption = "&Copy"
        End
        Begin Menu mnuPaste
            Caption = "&Paste"
        End
        Begin Menu mnuSep3
            Caption = "-"
        End
        Begin Menu mnuSelAll
            Caption = "Select &All"
        End
    End
    Begin TextArea Editor
        Weight = 1
    End
End

' The MIT License (MIT)
'
' Copyright (C) 2026 Scott Duensing
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to
' deal in the Software without restriction, including without limitation the
' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
' sell copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.


'$INCLUDE: 'commdlg.bas'

DIM currentFile AS STRING
DIM savedHash AS LONG
DIM dirty AS INTEGER
currentFile = ""
savedHash   = 0
dirty       = 0


SUB NotepadForm_Load
    Editor.SetCaptureTabs True
    Editor.SetTabWidth 4
END SUB


FUNCTION TextHash() AS LONG
    DIM h AS LONG
    DIM t AS STRING
    t = Editor.Text
    h = 0
    DIM i AS INTEGER
    FOR i = 1 TO LEN(t)
        h = h * 31 + ASC(MID$(t, i, 1))
    NEXT i
    TextHash = h
END FUNCTION


FUNCTION IsDirty() AS INTEGER
    IsDirty = (TextHash() <> savedHash)
END FUNCTION


SUB UpdateTitle
    DIM title AS STRING
    IF currentFile <> "" THEN
        title = currentFile + " - Notepad"
    ELSE
        title = "Untitled - Notepad"
    END IF
    IF dirty THEN
        title = "* " + title
    END IF
    NotepadForm.Caption = title
END SUB


SUB MarkClean
    dirty     = 0
    MarkClean
END SUB


SUB Editor_Change
    IF NOT dirty THEN
        dirty = -1
        UpdateTitle
    END IF
END SUB


FUNCTION AskSave() AS INTEGER
    IF NOT IsDirty() THEN
        AskSave = 0
        EXIT FUNCTION
    END IF
    DIM result AS INTEGER
    result = basPromptSave("Notepad")
    IF result = DVX_SAVE_YES THEN
        FileSave
        AskSave = 0
    ELSEIF result = DVX_SAVE_NO THEN
        AskSave = 0
    ELSE
        AskSave = 1
    END IF
END FUNCTION


SUB FileSave
    IF currentFile = "" THEN
        FileSaveAs
        EXIT SUB
    END IF
    OPEN currentFile FOR OUTPUT AS #1
    PRINT #1, Editor.Text
    CLOSE #1
    MarkClean
END SUB


SUB FileSaveAs
    DIM path AS STRING
    path = basFileSave("Save As", "Text Files (*.txt)|All Files (*.*)")
    IF path = "" THEN
        EXIT SUB
    END IF
    currentFile = path
    FileSave
    UpdateTitle
END SUB


SUB mnuNew_Click
    IF AskSave() THEN
        EXIT SUB
    END IF
    Editor.Text = ""
    currentFile = ""
    MarkClean
    UpdateTitle
END SUB


SUB mnuOpen_Click
    IF AskSave() THEN
        EXIT SUB
    END IF
    DIM path AS STRING
    path = basFileOpen("Open", "Text Files (*.txt)|All Files (*.*)")
    IF path = "" THEN
        EXIT SUB
    END IF
    DIM text AS STRING
    DIM ln AS STRING
    text = ""
    OPEN path FOR INPUT AS #1
    DO WHILE NOT EOF(1)
        LINE INPUT #1, ln
        IF text <> "" THEN
            text = text + CHR$(10)
        END IF
        text = text + ln
    LOOP
    CLOSE #1
    Editor.Text = text
    currentFile = path
    MarkClean
    UpdateTitle
END SUB


SUB mnuSave_Click
    FileSave
END SUB


SUB mnuSaveAs_Click
    FileSaveAs
END SUB


SUB mnuExit_Click
    Unload NotepadForm
END SUB


SUB mnuCut_Click
    Editor.Cut
END SUB


SUB mnuCopy_Click
    Editor.Copy
END SUB


SUB mnuPaste_Click
    Editor.Paste
END SUB


SUB mnuSelAll_Click
    Editor.SelectAll
END SUB


SUB NotepadForm_QueryUnload(Cancel AS Integer)
    Cancel = AskSave()
END SUB
