; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: TProgressBar object class; Simple progress bar control.
;
;  Target OS: Any
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________
module "TProgressbar library"


object TProgress, TWindow
  ._Pos   dd ?
  ._Max   dd ?
  ._Color dd ?

  param .Pos, ._Pos, .SetPos
  param .Max, ._Max, .SetMax

  method .Step
  method .SetPos, .value
  method .SetMax, .value

  method .Create, .pParent

  method .Paint
endobj



method TProgress.Create
begin
        mov     eax, [.self]
        mov     [eax+TProgress.__want_focus], FALSE

        inherited [.pParent]
        return
endp


method TProgress.Step
begin
        push    eax ecx

        mov     eax, [.self]
        mov     ecx, [eax+TProgress._Pos]
        cmp     ecx, [eax+TProgress._Max]
        jge     .exit

        inc     [eax+TProgress._Pos]
        exec    eax, TProgress:Refresh

.exit:
        pop     ecx eax
        clc
        return
endp


method TProgress.SetPos
begin
        push    eax ecx
        mov     ecx, [.self]
        mov     eax, [.value]

        xchg    eax, [ecx+TProgress._Pos]
        cmp     eax, [ecx+TProgress._Pos]
        je      .finish

        exec    ecx, TWindow:Refresh

.finish:
        pop     ecx eax
        return
endp


method TProgress.SetMax
begin
        push    eax ecx
        mov     ecx, [.self]
        mov     eax, [.value]

        xchg    eax, [ecx+TProgress._Max]
        cmp     eax, [ecx+TProgress._Max]
        je      .finish

        exec    ecx, TWindow:Refresh

.finish:
        pop     ecx eax
        return
endp




method TProgress.Paint
 .bounds TBounds
begin
        pushad

        mov     esi, [.self]

        cmp     [esi+TProgress._canvas], 0
        je      .finish

        mov     [.bounds.x], 0
        mov     [.bounds.y], 0
        mov     eax, [esi+TProgress._width]
        mov     ecx, [esi+TProgress._height]
        mov     [.bounds.width], eax
        mov     [.bounds.height], ecx

        lea     eax, [.bounds]
        stdcall [DrawBox], [esi+TProgress._canvas], eax, [GUI.clProgressBk], [GUI.progressBorder], [GUI.boxBorderWidth]

        mov     eax, [esi+TProgress._Pos]
        mov     ecx, [esi+TProgress._Max]
        test    ecx, ecx
        jnz     @f
        inc     ecx
@@:
        cmp     eax, ecx
        jle     @f
        mov     eax, ecx
@@:
        imul    eax, [.bounds.width]
        cdq
        idiv    ecx

        stdcall DrawSolidRect, [esi+TProgress._canvas], [.bounds.x], [.bounds.y], eax, [.bounds.height], [GUI.clProgressBar]

.finish:
        inherited
        popad
        return
endp



endmodule
