VERSION DVX 1.00
' helpedit.frm -- DVX Help Editor
'
' A .dhs help source editor with syntax highlighting and
' live preview via the DVX Help Viewer.
'
' Add commdlg.bas and help.bas to your project, then click Run.

Begin Form HelpEdit
    Caption   = "DVX Help Editor"
    Layout    = VBox
    AutoSize  = False
    Resizable = True
    Centered  = True
    Width     = 640
    Height    = 440
    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 mnuBuild
        Caption = "&Build"
        Begin Menu mnuCompile
            Caption = "&Compile"
        End
        Begin Menu mnuPreview
            Caption = "&Preview"
        End
    End
    Begin Menu mnuHelp
        Caption = "&Help"
        Begin Menu mnuHelpTopic
            Caption = "Help &Topics"
        End
    End
    Begin TextArea Editor
        Weight = 1
    End
    Begin Label LblStatus
        Weight = 0
    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.


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

Load HelpEdit
HelpEdit.Show

' Configure the editor
Editor.SetSyntaxMode "dhs"
Editor.SetShowLineNumbers True
Editor.SetAutoIndent True
Editor.SetCaptureTabs True
Editor.SetTabWidth 2

' Start with a template
Editor.Text = ".topic intro" + CHR$(10) + ".title My Help File" + CHR$(10) + ".toc 0 My Help File" + CHR$(10) + ".default" + CHR$(10) + CHR$(10) + ".h1 Welcome" + CHR$(10) + CHR$(10) + "This is your help file. Edit the .dhs source here," + CHR$(10) + "then click Compile and Preview to see the result." + CHR$(10)

MarkClean
LblStatus.Caption = "Ready."


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


FUNCTION AskSave() AS INTEGER
    IF NOT IsDirty() THEN
        AskSave = 0
        EXIT FUNCTION
    END IF
    DIM result AS INTEGER
    result = basPromptSave("Help Editor")
    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 UpdateTitle
    DIM title AS STRING
    IF currentFile <> "" THEN
        title = currentFile + " - DVX Help Editor"
    ELSE
        title = "DVX Help Editor"
    END IF
    IF dirty THEN
        title = "* " + title
    END IF
    HelpEdit.Caption = title
END SUB


SUB MarkClean
    dirty     = 0
    savedHash = TextHash()
    UpdateTitle
END SUB


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


SUB mnuNew_Click
    IF AskSave() THEN
        EXIT SUB
    END IF
    currentFile = ""
    hlpFile     = ""
    Editor.Text = ".topic intro" + CHR$(10) + ".title My Help File" + CHR$(10) + ".toc 0 My Help File" + CHR$(10) + ".default" + CHR$(10) + CHR$(10) + ".h1 Welcome" + CHR$(10) + CHR$(10)
    MarkClean
    LblStatus.Caption = "New file."
    UpdateTitle
END SUB


SUB mnuOpen_Click
    IF AskSave() THEN
        EXIT SUB
    END IF

    DIM path AS STRING
    path = basFileOpen("Open Help Source", "Help Source (*.dhs)|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
    hlpFile     = ""
    MarkClean
    LblStatus.Caption = path
    UpdateTitle
END SUB


SUB FileSave
    IF currentFile = "" THEN
        FileSaveAs
        EXIT SUB
    END IF

    OPEN currentFile FOR OUTPUT AS #1
    PRINT #1, Editor.Text
    CLOSE #1

    MarkClean
    LblStatus.Caption = "Saved: " + currentFile
    UpdateTitle
END SUB


SUB FileSaveAs
    DIM path AS STRING
    path = basFileSave("Save Help Source", "Help Source (*.dhs)|All Files (*.*)")
    IF path = "" THEN
        EXIT SUB
    END IF

    currentFile = path
    FileSave
END SUB


SUB mnuSave_Click
    FileSave
END SUB


SUB mnuSaveAs_Click
    FileSaveAs
END SUB


SUB mnuExit_Click
    Unload HelpEdit
END SUB


SUB DoCompile
    ' Save first
    IF currentFile = "" THEN
        FileSaveAs
        IF currentFile = "" THEN
            LblStatus.Caption = "Save cancelled."
            EXIT SUB
        END IF
    ELSE
        OPEN currentFile FOR OUTPUT AS #1
        PRINT #1, Editor.Text
        CLOSE #1
        MarkClean
    END IF

    ' Derive .hlp filename from .dhs filename
    DIM baseName AS STRING
    baseName = currentFile

    IF LEN(baseName) > 4 THEN
        IF UCASE$(RIGHT$(baseName, 4)) = ".DHS" THEN
            baseName = LEFT$(baseName, LEN(baseName) - 4)
        END IF
    END IF

    hlpFile = baseName + ".hlp"

    LblStatus.Caption = "Compiling..."
    DoEvents

    IF HelpCompile(currentFile, hlpFile) THEN
        LblStatus.Caption = "Compiled: " + hlpFile
    ELSE
        LblStatus.Caption = "Compile failed!"
        hlpFile = ""
    END IF
END SUB


SUB mnuCompile_Click
    DoCompile
END SUB


SUB mnuPreview_Click
    IF hlpFile = "" THEN
        DoCompile
    END IF

    IF hlpFile = "" THEN
        EXIT SUB
    END IF

    LblStatus.Caption = "Opening viewer..."
    HelpView hlpFile
END SUB


SUB mnuHelpTopic_Click
    HelpView "dvxhelp.hlp"
END SUB


SUB HelpEdit_QueryUnload(Cancel AS INTEGER)
    Cancel = AskSave()
END SUB
