; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "Fresh artistic license."         |
;|_______________________________________________________________________________________|
;                                                                                        |
;   Description: Test program for TFreshEdit control.                                    |
;                                                                                        |
;   Target OS: All/Linux/Win32 - depending on current test needs.                        |
;                                                                                        |
;   Dependencies:                                                                        |
;                                                                                        |
;   Notes: The main test program calls "PrepareDemo" procedure.                          |
;________________________________________________________________________________________|


__TestGround:

; Global data...
iglobal

__TestData:

frmMainForm:
        ObjTemplate  tfParent or tfEnd, Form, frmMain, \
                     x, 100,        \
                     y, 50,         \
                     width, 640,    \
                     height, 480,   \
                     borderKind, borderFull,    \
                     Visible, TRUE, \
                     Caption, 'FreshEdit test application.'

        ObjTemplate tfEnd, FreshEdit, Editor,   \
                    x, 8, y, 8,                 \
                    width, 624, height, 464,    \
                    Align, waClient,            \
                    Visible, TRUE


__TestDataSize = $ - __TestData
endg


;cTestTextFile text 'test_utf8.asm'
cTestTextFile text 'eve.asm'


uglobal
  Timer1 dd ?
endg



proc PrepareDemo
begin
        stdcall CreateFromTemplate, frmMainForm, 0
        mov     ecx, [Application]
        mov     [ecx+TApplication.MainWindow], frmMain

        stdcall Set, [Editor], TFreshEdit._procSyntax, SyntaxFASM
        stdcall Set, [Editor], TFreshEdit.OnControlKey, procKeyPress

; Load test file

        stdcall LoadBinaryFile, cTestTextFile
        jc      .exiterror
        push    eax

;        execute [Editor], TFreshEdit.InsertString, eax
        stdcall Set, [Editor], TFreshEdit.Text, eax
        stdcall FreeMem ; from the stack

; position the caret to the begining.
        mov     esi, [Editor]
        mov     [esi+TFreshEdit._xCaret], 0
        mov     [esi+TFreshEdit._yCaret], 0

;        execute [Editor], TWindow.Focus
;        stdcall TFreshEdit.__FitCaretInWindow

;        stdcall Get, [Editor], TFreshEdit.MaxLineLen    ; compute the maximal length of the lines.
;        stdcall TFreshEdit.__UpdateScrollBars, [Editor]

        mov     eax, [Editor]
        mov     eax, [eax+TFreshEdit._pLines]
        or      [eax+TArray.array+sizeof.TEditorLine*18+TEditorLine.flags], lfBookmark or lfProtected

.exit:
        return

.exiterror:
        DebugMsg  "Error open file"
        jmp     .exit
endp



uglobal
  ScrollIt dd ?
endg




proc procKeyPress, .self, .keyevent
begin
        mov     ebx, [.keyevent]

        mov     eax, [ebx+TKeyboardEvent.key]
        cmp     eax, $02        ; ctrl+B
        je      .bookmark_toggle

        cmp     eax, 'W' and $1f
        je      .word_wrap_toggle

        cmp     eax, 12 ; ctrl+L
        je      .clipleft
        cmp     eax, 18 ; ctrl+R
        je      .clipright
        cmp     eax, 20 ; ctrl+T
        je      .test_something


        mov     eax, [ebx+TKeyboardEvent.scancode]
        cmp     eax, keyLeftNumpad
        je      .left
        cmp     eax, keyLeft
        je      .left

        cmp     eax, keyRightNumpad
        je      .right
        cmp     eax, keyRight
        je      .right

        cmp     eax, keyUpNumpad
        je      .up
        cmp     eax, keyUp
        je      .up

        cmp     eax, keyDownNumpad
        je      .down
        cmp     eax, keyDown
        je      .down

        cmp     eax, keyPgUpNumpad
        je      .pgup
        cmp     eax, keyPgUp
        je      .pgup

        cmp     eax, keyPgDnNumpad
        je      .pgdn
        cmp     eax, keyPgDown
        je      .pgdn

        cmp     eax, keyHomeNumpad
        je      .home
        cmp     eax, keyHome
        je      .home

        cmp     eax, keyEndNumpad
        je      .end
        cmp     eax, keyEnd
        je      .end

        cmp     eax, keyInsNumpad
        je      .insert
        cmp     eax, keyInsert
        je      .insert


.finish:
        mov     eax, chgfNoClearSelection
        return


.clipleft:
        stdcall Get, esi, TFreshEdit.CurrentLine
        jc      .finish
        stdcall StrClipSpacesL, [eax+TEditorLine.Data]
        mov     eax, chgfNeedRefresh
        return

.clipright:
        stdcall Get, esi, TFreshEdit.CurrentLine
        jc      .finish
        stdcall StrClipSpacesR, [eax+TEditorLine.Data]
        mov     eax, chgfNeedRefresh
        clc
        return

.test_something:
        mov     eax, [.self]
        execute eax, TFreshEdit.FormatLine, [eax+TFreshEdit._yCaret]

;        execute [.self], TFreshEdit.InsertLine
;        mov     ecx, eax
;        stdcall StrNew
;        mov     [ecx+TEditorLine.Data],eax

        mov     eax, chgfNeedRefresh
        return

strTestString text 'Insert This Text '



.word_wrap_toggle:
        stdcall Get, esi, TFreshEdit.CurrentLine
        jc      .finish

        xor     [eax+TEditorLine.flags], lfWordWrap
        execute esi, TFreshEdit.FormatLine, [esi+TFreshEdit._yCaret]

        mov     eax, chgfNeedRefresh
        return


.bookmark_toggle:
        stdcall Get, esi, TFreshEdit.CurrentLine
        jc      .finish

        xor     [eax+TEditorLine.flags], lfBookmark
        mov     eax, chgfNeedRefresh or chgfNoClearSelection
        return


.insert:
        test    [ebx+TKeyboardEvent.kbdStatus], maskAlt
        jnz     .change_sel_mode
        cmp     [ebx+TKeyboardEvent.kbdStatus], 0
        je      .change_ins_mode

        mov     eax, chgfNoClearSelection
        return

.change_sel_mode:
        stdcall Get, esi, TFreshEdit._SelMode
        inc     eax
        cmp     eax, selmBlock
        jbe     @f
        xor     eax, eax
@@:
        stdcall Set, esi, TFreshEdit._SelMode, eax
        mov     eax, chgfNoClearSelection
        return

.change_ins_mode:
; not implemented yet.
        mov     eax, chgfNoClearSelection
        return


.home:
        execute esi, TFreshEdit.LineBegin
        xor     eax, eax
        return

.end:
        execute esi, TFreshEdit.LineEnd
        xor     eax, eax
        return

.left:
        mov     eax, -1
        mov     ecx, mvcMoveX
        jmp     .moveit


.right:
        mov     eax, 1
        mov     ecx, mvcMoveX
        jmp     .moveit

.up:
        mov     eax, -1
        mov     ecx, mvcMoveY
        jmp     .moveit

.down:
        mov     eax, 1
        mov     ecx, mvcMoveY
        jmp     .moveit

.pgup:
        mov     eax, [esi+TFreshEdit._rows]
        dec     eax
        neg     eax
        mov     ecx, mvcMoveY or mvcScrollY
        jmp     .moveit

.pgdn:
        mov     eax, [esi+TFreshEdit._rows]
        dec     eax
        mov     ecx, mvcMoveY or mvcScrollY
        jmp     .moveit


.moveit:
        test    [ebx+TKeyboardEvent.kbdStatus], maskScrLk
        jz      .moveitnow

        test    ecx, mvcMoveX
        jz      @f
        or      ecx, mvcScrollX
@@:
        test    ecx, mvcMoveY
        jz      @f
        or      ecx, mvcScrollY
@@:

.moveitnow:
        execute [.self], TFreshEdit.Move2, ecx, eax, FALSE
        xor     eax, eax
        return
endp





DispSize 'Test ground dummy code:', ($-__TestGround)+__TestDataSize

; without this, the application crashes in Linux with untraceable seg fault. Why???
; It seems it crashes during the load, before even one instruction executed.
; Maybe it is some kind of bug in the elf formater...
rb 64

include 'FreshEdit/fasm_syntax.asm'
