'いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい
' Template for DDT main dialog
'--------------------------------------------------------------------
#COMPILE EXE "Zooming.exe"

DECLARE FUNCTION zTrace LIB "zTrace.DLL" ALIAS "zTrace" (zMessage AS ASCIIZ) AS LONG

%TRUE               = 1
%FALSE              = 0
%TBS_AUTOTICKS      = &H0001
%TBS_VERT           = &H0002
%TBS_HORZ           = &H0000
%TBS_TOP            = &H0004
%TBS_BOTTOM         = &H0000

%TBM_GETPOS             = %WM_USER
%TBM_GETRANGEMIN        = %WM_USER + 1
%TBM_GETRANGEMAX        = %WM_USER + 2
%TBM_GETTIC             = %WM_USER + 3
%TBM_SETTIC             = %WM_USER + 4
%TBM_SETPOS             = %WM_USER + 5
%TBM_SETRANGE           = %WM_USER + 6
%TBM_SETRANGEMIN        = %WM_USER + 7
%TBM_SETRANGEMAX        = %WM_USER + 8
%TBM_CLEARTICS          = %WM_USER + 9
%TBM_SETSEL             = %WM_USER + 10
%TBM_SETSELSTART        = %WM_USER + 11
%TBM_SETSELEND          = %WM_USER + 12
%TBM_GETPTICS           = %WM_USER + 14
%TBM_GETTICPOS          = %WM_USER + 15
%TBM_GETNUMTICS         = %WM_USER + 16
%TBM_GETSELSTART        = %WM_USER + 17
%TBM_GETSELEND          = %WM_USER + 18
%TBM_CLEARSEL           = %WM_USER + 19
%TBM_SETTICFREQ         = %WM_USER + 20
%TBM_SETPAGESIZE        = %WM_USER + 21
%TBM_GETPAGESIZE        = %WM_USER + 22
%TBM_SETLINESIZE        = %WM_USER + 23
%TBM_GETLINESIZE        = %WM_USER + 24
%TBM_GETTHUMBRECT       = %WM_USER + 25
%TBM_GETCHANNELRECT     = %WM_USER + 26
%TBM_SETTHUMBLENGTH     = %WM_USER + 27
%TBM_GETTHUMBLENGTH     = %WM_USER + 28
%TBM_SETTOOLTIPS        = %WM_USER + 29
%TBM_GETTOOLTIPS        = %WM_USER + 30
%TBM_SETTIPSIDE         = %WM_USER + 31

TYPE RECT
    nLeft AS LONG
    nTop AS LONG
    nRight AS LONG
    nBottom AS LONG
END TYPE

TYPE POINTAPI
    x AS LONG
    y AS LONG
END TYPE


%WM_USER                                        = &H400
%WM_DRAWITEM                                    = &H2B
%WM_INITDIALOG                                  = &H110
%WM_COMMAND                                     = &H111
%WS_OVERLAPPED                                  = &H0
%WS_POPUP                                       = &H80000000
%WS_CHILD                                       = &H40000000
%WS_VISIBLE                                     = &H10000000
%WS_CAPTION                                     = &H00C00000  ' WS_BORDER OR WS_DLGFRAME
%WS_VSCROLL                                     = &H00200000
%WS_SYSMENU                                     = &H00080000
%WS_THICKFRAME                                  = &H00040000
%WS_TABSTOP                                     = &H00010000
%WS_MINIMIZEBOX                                 = &H00020000
%WS_MAXIMIZEBOX                                 = &H00010000
%WS_OVERLAPPEDWINDOW                            = %WS_OVERLAPPED OR %WS_CAPTION OR %WS_SYSMENU OR %WS_THICKFRAME OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX

%BS_LEFT            = &H100&
%BS_RIGHT           = &H200&
%BS_CENTER          = &H300&
%BS_TOP             = &H400&
%BS_BOTTOM          = &H800&
%BS_VCENTER         = &HC00&


%IDC_GDIMAGE      = 101
%IDC_SLIDEZOOM    = 102
%IDC_NEWIMAGE     = 103
%IDC_CLOCK        = 122

'--------------------------------------------------------------------
DECLARE CALLBACK FUNCTION DlgProc() AS LONG
DECLARE FUNCTION CreateWindowEx LIB "USER32.DLL" ALIAS "CreateWindowExA" (BYVAL dwExStyle AS DWORD, lpClassName AS ASCIIZ, lpWindowName AS ASCIIZ, BYVAL dwStyle AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, _
    BYVAL nWidth AS LONG, BYVAL nHeight AS LONG, BYVAL hWndParent AS DWORD, BYVAL hMenu AS DWORD, BYVAL hInstance AS DWORD, lpParam AS ANY) AS DWORD
DECLARE FUNCTION GetWindowRect LIB "USER32.DLL" ALIAS "GetWindowRect" (BYVAL hWnd AS DWORD, lpRect AS RECT) AS LONG
DECLARE FUNCTION ScreenToClient LIB "USER32.DLL" ALIAS "ScreenToClient" (BYVAL hWnd AS DWORD, lpPoint AS POINTAPI) AS LONG
DECLARE FUNCTION SendMessage LIB "USER32.DLL" ALIAS "SendMessageA" (BYVAL hWnd AS DWORD, BYVAL dwMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
DECLARE FUNCTION GetDlgItem LIB "USER32.DLL" ALIAS "GetDlgItem" (BYVAL hDlg AS DWORD, BYVAL nIDDlgItem AS LONG) AS DWORD
DECLARE FUNCTION GetDlgCtrlID LIB "USER32.DLL" ALIAS "GetDlgCtrlID" (BYVAL hWnd AS DWORD) AS LONG
DECLARE FUNCTION GetClientRect LIB "USER32.DLL" ALIAS "GetClientRect" (BYVAL hwnd AS DWORD, lpRect AS RECT) AS LONG

#INCLUDE "WINLIFT.INC"
#INCLUDE "GDIMAGE.INC"

'いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい
' Main entrance
'--------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
    LOCAL hDlg, hCtrl, hGDImage AS DWORD, sResource AS STRING, zFullpathToImage AS ASCIIZ * 260
    LOCAL p AS POINTAPI, rc AS RECT
    LOCAL imgW, imgH, xP, yP, xS, yS AS LONG

    DIALOG NEW PIXELS, 0, "Zooming",,, 780, 572, %WS_OVERLAPPEDWINDOW, 0 TO hDlg

    sResource = EXE.Path$ + "Resource\"

    IF RegisterGDImageClass() THEN
       zFullpathToImage = sResource + "Bimbo.png"
       hGDImage = CreateWindowEx(0, "ZIMAGECTRL", _
                                 zFullpathToImage, _ ' Full path to default image background or ""
                                 %WS_CHILD Or %WS_VISIBLE Or %WS_HSCROLL Or %WS_VSCROLL, _
                                 78, 40, 628, 432, _
                                 hDlg, %IDC_GDIMAGE, _
                                 zInstance(), _
                                 BYVAL 0&)

       hCtrl =    CreateWindowEx( 0, _
                                 "MSCTLS_TRACKBAR32", _
                                 "", _
                                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %TBS_VERT OR %TBS_BOTTOM OR %TBS_AUTOTICKS, _
                                 728, 40, 32, 432, _
                                 hDlg, %IDC_SLIDEZOOM, _
                                 zInstance(), _
                                 BYVAL 0&)
    END IF

    IF skInitEngine("AERO.sks", "") THEN
       CALL skSkinWindow(hDlg, "Dock|Undock|Minimize|Maximize|Restore|Close")

       CALL skSetAnchorCtrl(hGDImage, %ANCHOR_HEIGHT_RIGHT)
       CALL skSetAnchorCtrl(hCtrl, %ANCHOR_RIGHT)

       IF hGDImage THEN
          CALL ZI_SetProperty(hGDImage, %ZI_ZoomWindow, 1)
          CALL ZI_SetAnchorMode(hGDImage, %ANCHOR_HEIGHT_WIDTH)

'         // Add a skinned border around the GDImage control.
          CALL GetWindowRect(hGDImage, rc)
          p.X = rc.nLeft: p.Y = rc.nTop
          CALL ScreenToClient(hDlg, p)
          CALL skBorder(hGDImage, p.X - 1, p.Y - 1, rc.nRight - rc.nLeft + 2, rc.nBottom - rc.nTop + 2, 0, 1)

          CALL skSetAnchorCtrl(hGDImage, %ANCHOR_HEIGHT_RIGHT)

'         // Compute the good range to use with the zoom slider
          CALL ZI_GetImageSizeFromControl(hGDImage, imgW, imgH)
          CALL ZI_Iconise(imgW, imgH, rc.nRight - rc.nLeft, rc.nBottom - rc.nTop, xP, yP, xS, yS)
          IF imgW THEN
             hCtrl = GetDlgItem(hDlg, %IDC_SLIDEZOOM)
             CALL SendMessage(hCtrl, %TBM_SETPAGESIZE, 0, 1)
             CALL SendMessage(hCtrl, %TBM_SETLINESIZE, 0, 1)
             CALL SendMessage(hCtrl, %TBM_SETTICFREQ, 0, 1)
             CALL SendMessage(hCtrl, %TBM_SETRANGE, 1, MAK(LONG, 1, imgW))
             CALL SendMessage(hCtrl, %TBM_SETPOS, 1, xS)
          END IF

       END IF

'      // 4.73 WinLIFT clock
       hCtrl = skClockCtrl(hDlg, sResource + "Clock_03_92.png", 4, 476, 0, 0, %IDC_CLOCK, &H7FCB0000, &H7F8F8F8F, 0)
       CALL skSetAnchorCtrl(hCtrl, %ANCHOR_BOTTOM)

       hCtrl = skButtonImage(hDlg, sResource + "SelectImage.png", 780 - 58, 498, %IDC_NEWIMAGE, 5)
       CALL skSetAnchorCtrl(hCtrl, %ANCHOR_BOTTOM_RIGHT)

    END IF

    DIALOG SHOW MODAL hDlg CALL DlgProc

END FUNCTION

'いいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいいい
' Main callback
'--------------------------------------------------------------------
CALLBACK FUNCTION DlgProc() AS LONG
    LOCAL hGDImage, hCtrl, hDW AS DWORD, ZoomValue, OldZoomValue AS SINGLE, nPos, imgW, imgH, xP, yP, xS, yS AS LONG
    LOCAL rc AS RECT, sDataString AS STRING

    SELECT CASE CBMSG

    CASE %WM_COMMAND

         SELECT CASE CBCTL
         CASE %IDCANCEL
              IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN 'end prog
                 DIALOG END CBHNDL
              END IF

         CASE %IDC_NEWIMAGE
              hDW = ZI_CreateDW(CBHNDL)
              sDataString = ZI_LoadDialog(CBHNDL)
              CALL ZI_DestroyDW(hDW)

              IF LEN(sDataString) THEN
                 hGDImage = GetDlgItem(CBHNDL, %IDC_GDIMAGE)
                 CALL ZI_SetFromFile(hGDImage, (sDataString))
'                // Compute the good range to use with the zoom slider
                 CALL ZI_GetImageSizeFromControl(hGDImage, imgW, imgH)
                 CALL GetClientRect(hGDImage, rc)
                 CALL ZI_Iconise(imgW, imgH, rc.nRight, rc.nBottom, xP, yP, xS, yS)
                 IF imgW THEN
                    hCtrl = GetDlgItem(CBHNDL, %IDC_SLIDEZOOM)
                    CALL SendMessage(hCtrl, %TBM_SETRANGE, 1, MAK(LONG, 1, imgW))
                    CALL SendMessage(hCtrl, %TBM_SETPOS, 1, xS)
                 END IF
              END IF

         END SELECT

    CASE %WM_VSCROLL
         IF GetDlgCtrlId(CBLPARAM) = %IDC_SLIDEZOOM THEN
            hGDImage = GetDlgItem(CBHNDL, %IDC_GDIMAGE)
            CALL ZI_GetImageSizeFromControl(hGDImage, imgW, imgH)
            nPos = SendMessage(CBLPARAM, %TBM_GetPos, 0, 0) '// Do not use nPosition, because it could be NULL.
            OldZoomValue = ZI_GetZoomValue(hGDImage)
            ZoomValue = nPos / ImgW
            IF ZoomValue <> OldZoomValue THEN
               CALL ZI_SetZoomValue(hGDImage, ZoomValue)
               CALL ZI_SetZoomScrollInfo(hGDImage, OldZoomValue, ZoomValue)
            END IF
         END IF

    END SELECT

END FUNCTION
