; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: TScrollWindow is abstract class that is window with two scrollbars.
;               it is base for all windows that have to present information in scrollable
;               form, as editors, treeview, listview, tables, etc.
;
;  Target OS: Any
;
;  Dependencies:
;  Notes:
;_________________________________________________________________________________________
module "TreeView library"



object TScrollWindow, TWindow

  .XScroller    dd ?
  .YScroller    dd ?

  ._start_y     dd ?
  ._start_x     dd ?

  param  .StartX,   ._start_x, .SetStartX
  param  .StartY,   ._start_y, .SetStartY

  method .SetStartX, .value
  method .SetStartY, .value

  method .Create, .parent

  method   .Resize, .newWidth, .newHeight

  abstract .UpdateScrollBar

  method .EventScroll, .direction, .command, .value

endobj




method TScrollWindow.Create ; .parent
begin
        inherited [.parent]

        push    esi
        mov     esi, [.self]

        create  [esi+TScrollWindow.YScroller], TScrollbar, esi
        set     [esi+TScrollWindow.YScroller], TScrollbar:Kind, sbkVertical
        set     [esi+TScrollWindow.YScroller], TScrollbar:width, [GUI.scrollWidth]

        create  [esi+TScrollWindow.XScroller], TScrollbar, esi
        set     [esi+TScrollWindow.XScroller], TScrollbar:Kind, sbkHorizontal
        set     [esi+TScrollWindow.XScroller], TScrollbar:height, [GUI.scrollWidth]

        pop     esi
        return
endp




method TScrollWindow.Resize
begin
        inherited [.newWidth], [.newHeight]
        exec    [.self], TScrollWindow:UpdateScrollBar
        return
endp





method TScrollWindow.EventScroll
begin
        pushad

        mov     esi, [.self]

        cmp     [.direction], scrollX
        je      .horiz_scroll

        cmp     [.command], scWheelUp
        je      .scrollupy

        cmp     [.command], scWheelDn
        je      .scrolldny

        cmp     [.command], scTrack
        jne     .finish

        set     esi, TScrollWindow:StartY, [.value]

.finish:
        popad
        return

.scrollupy:
        get     eax, esi, TScrollWindow:StartY
        dec     eax
        jns     @f
        xor     eax, eax
@@:
        set     esi, TScrollWindow:StartY, eax
        jmp     .finish


.scrolldny:
        get     ecx, [esi+TScrollWindow.YScroller], TScrollbar:Max
        get     eax, esi, TScrollWindow:StartY
        inc     eax
        cmp     eax, ecx
        jle     @f
        dec     eax
@@:
        set     [.self], TScrollWindow:StartY, eax
        jmp     .finish


.horiz_scroll:
        cmp     [.command], scWheelUp
        je      .scrollupx

        cmp     [.command], scWheelDn
        je      .scrolldnx

        cmp     [.command], scTrack
        jne     .finish

        set     esi, TScrollWindow:StartX, [.value]
        jmp     .finish


.scrollupx:
        get     eax, esi, TScrollWindow:StartX
        dec     eax
        jns     @f
        xor     eax, eax
@@:
        set     esi, TScrollWindow:StartX, eax
        jmp     .finish


.scrolldnx:
        get     ecx, [esi+TScrollWindow.XScroller], TScrollbar:Max
        get     eax, esi, TScrollWindow:StartX
        inc     eax
        cmp     eax, ecx
        jle     @f
        dec     eax
@@:
        set     [.self], TScrollWindow:StartX, eax
        jmp     .finish

endp




method TScrollWindow.SetStartX    ;, .value
begin
        pushad

        mov     esi, [.self]
        mov     ebx, [.value]
        xchg    ebx, [esi+TScrollWindow._start_x]
        cmp     ebx, [esi+TScrollWindow._start_x]
        je      .finish

        set     [esi+TScrollWindow.XScroller], TScrollbar:Pos, [esi+TScrollWindow._start_x]
        exec    [.self], TWindow:Refresh

.finish:
        popad
        return

endp



method TScrollWindow.SetStartY   ;, .value
begin
        pushad

        mov     esi, [.self]
        mov     ebx, [.value]
        xchg    ebx, [esi+TScrollWindow._start_y]
        cmp     ebx, [esi+TScrollWindow._start_y]
        je      .finish

        set     [esi+TScrollWindow.YScroller], TScrollbar:Pos, [esi+TScrollWindow._start_y]
        exec    [.self], TWindow:Refresh

.finish:
        popad
        return

endp




endmodule