%Unicode = 1
#include "win32api.inc"
#INCLUDE "WinLIFT32.INC"
#resource manifest, 1, "manifest.man"
#resource icon, 2, "icon.ico"

%NULL             = 0
%ClientW          = 480
%ClientH          = 360

%IDI_ICON1        = 101

%IDC_BTN_LEFT     = 101
%IDC_BTN_PLAY     = 102
%IDC_BTN_RIGHT    = 103
%IDC_BTN_CHECK    = 104
%IDC_BTN_DELETE   = 105
%IDC_BTN_RECYCLE  = 106
%IDC_BTN_PRINT    = 107
%IDC_BTN_TEXT     = 108
%IDC_BTN_SAVE     = 109
%IDC_BTN_32BIT    = 110
%IDC_BTN_64BIT    = 111
%IDC_BTN_00       = 112
%IDC_BTN_01       = 113
%IDC_BTN_02       = 114
%IDC_BTN_03       = 115
%IDC_BTN_04       = 116
%IDC_BTN_05       = 117
%IDC_BTN_06       = 118
%IDC_BTN_07       = 119
%IDC_BTN_08       = 120
%IDC_BTN_09       = 121
%IDC_CLOCK        = 122
%IDC_FRAME        = 123
%IDC_LIPS         = 124
%IDC_BTN_HELP     = 125

%IDC_SKIN_LIST    = 126

type PROP
    minWidth  as long
    minHeight as long
    hInstance as long
end type

global gP as PROP

declare function zTrace lib "zTrace32.DLL" alias "zTrace" (zMessage as wstringz) AS LONG
declare function PathCombine lib "SHLWAPI.DLL" alias "PathCombineW" (byref lpszDest as wstringz, byref lpszDir as wstringz, byref lpszFile as wstringz) as long

sub List_SelectPlus(byval hList as long, byval nSelected as long)
    if nSelected > 0 then decr nSelected '// Because ListBox is zero based
    if (IsWindow(hList)) then SendMessage(hList, %LB_SETCURSEL, nSelected, 0)
end sub

sub List_DeleteAll(byval hList as dword)
    if (IsWindow(hList)) then SendMessage(hList, %LB_RESETCONTENT, 0, 0)
end sub

function List_Add(byval hList as dword, zTmp as wstringz) as long
    if (IsWindow(hList)) then
        if (len(zTmp)) then function = SendMessage(hList, %LB_ADDSTRING, 0, varptr(zTmp))
    end if
end function

function rWidth(byval r as rect) as long
    function = r.right - r.left
end function

function rHeight(byval r as rect) as long
    function = r.bottom - r.top
end function

function Resource(sFile as wstringz) as string
    static szresource as wstringz * %MAX_PATH
    szresource = EXE.path$ + "resource\" + sFile
    function = szresource
end function

sub SkinDetect(byval hParent as dword, zSkinFile as WSTRINGZ)
    local fd as WIN32_FIND_DATA
    local zBackground as wstringz * %MAX_PATH
    local zPath as wstringz * %MAX_PATH
    local hList, hFind as dword
    hList = GetDlgItem(hParent, %IDC_SKIN_LIST)
    if (hList = 0) then
        hList = CreateWindowEx(0, "LISTBOX", "", %WS_CHILD or %LBS_SORT, 0, -1000, 0, 0, hParent, %IDC_SKIN_LIST, gP.hinstance, byval %NULL)
    else
        List_DeleteAll(hList)
    end if

    CharLower(zSkinFile)
    hFind = FindFirstFile("*.sks", fd)
    local nCount, nUseItem as long
    if (hFind <> %INVALID_HANDLE_VALUE) then
        do
            if (fd.dwFileAttributes and %FILE_ATTRIBUTE_DIRECTORY) = 0 then '// Only use files present in the directory, ignore if it is directory
                if (List_Add(hList, fd.cFileName) > -1) then
                    incr nCount
                    CharLower(fd.cFileName)
                    if ((fd.cFileName = zSkinFile) and (nUseItem = 0)) then nUseItem = nCount
                end if
            end if
        loop while FindNextFile(hFind, fd)
        FindClose(hFind)
    end if
    if (nCount > 0) then
        if (nUseItem = 0) then nUseItem = 1
        List_SelectPlus(hList, nUseItem)
    end if
    skSkinDisable(hList)
end sub

sub AddChildControls(byval hWnd as dword)

    local zSkinFile as wstringz * %MAX_PATH
    zSkinFile = "Sony.sks"
    local zUserKey as wstringz * %MAX_PATH

    SkinDetect(hWnd, zSkinFile)

    if (skInitEngine(zSkinFile, zUserKey)) then
        if (skSkinWindow(hWnd, "  Dock  |  Undock  |  Minimize  |  Maximize  |  Restore  |  Close  ")) then
            '// If hWnd has been skinned with success,
            '// use this new minimum track size matching the WinLIFT specific NCAREA
            local r as RECT
            GetWindowRect(hWnd, r)
            gP.minWidth = rWidth(r)   '// Using the specific skin systemMetrics
            gP.minHeight = rHeight(r) '// Using the specific skin systemMetrics
        end if

        local hCtrl as dword

        '// These are WinLIFT controls, to replace the standard Windows buttons.
        hCtrl = skButtonImage(hWnd, Resource("BTN_Left.png"), 216-57, 300+12, %IDC_BTN_LEFT, 5)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ_BOTTOM)
        skCreateToolTip (hCtrl, "Previous skin theme")

        hCtrl = skButtonImage(hWnd, Resource("BTN_Play.png"), 216, 300, %IDC_BTN_PLAY, 5)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ_BOTTOM)

        hCtrl = skButtonImage(hWnd, Resource("BTN_Right.png"), 216+49, 300+12, %IDC_BTN_RIGHT, 5)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ_BOTTOM)
        skCreateToolTip (hCtrl, "Next skin theme")

        hCtrl = skPushButtonImage(hWnd, Resource("Check_32.png"), "BS_LEFT", 22, 30, 140, 50, %IDC_BTN_CHECK, %BS_LEFT or %BS_VCENTER)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ)

        hCtrl = skPushButtonImage(hWnd, Resource("Delete_32.png"), "BS_CENTER", 22 + 140 + 8, 30, 140, 50, %IDC_BTN_DELETE, %BS_CENTER or %BS_VCENTER)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ)

        hCtrl = skPushButtonImage(hWnd, Resource("Recycle_32.png"), "BS_RIGHT", 22 + (140 + 8) * 2, 30, 140, 50, %IDC_BTN_RECYCLE, %BS_RIGHT or %BS_VCENTER)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ)

        hCtrl = skPushButtonImage(hWnd, Resource("Print_32.png"), "BS_TOP", 22, 20 + 70, 140, 50, %IDC_BTN_PRINT, %BS_TOP)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)

        hCtrl = skPushButtonImage(hWnd, "", "Pristina font", 22 + 140 + 8, 20 + 70, 140, 50, %IDC_BTN_TEXT, %BS_CENTER or %BS_VCENTER)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        '// Use Private font collection.
        skSetLabelFont(hCtrl, Resource("Pristina.ttf"), 28, skARGB(255, 255,255,0), 0, 0)

        hCtrl = skPushButtonImage(hWnd, Resource("Save_32.png"), "BS_BOTTOM", 22 + (140 + 8) * 2, 20 + 70, 140, 50, %IDC_BTN_SAVE, %BS_BOTTOM)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)

        hCtrl = skPushButtonImage(hWnd, Resource("00_32.png"), "", 40 + (40 * 0), 20 + 140, 38, 38, %IDC_BTN_00, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("01_32.png"), "", 40 + (40 * 1), 20 + 140, 38, 38, %IDC_BTN_01, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("02_32.png"), "", 40 + (40 * 2), 20 + 140, 38, 38, %IDC_BTN_02, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("03_32.png"), "", 40 + (40 * 3), 20 + 140, 38, 38, %IDC_BTN_03, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("04_32.png"), "", 40 + (40 * 4), 20 + 140, 38, 38, %IDC_BTN_04, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("05_32.png"), "", 40 + (40 * 5), 20 + 140, 38, 38, %IDC_BTN_05, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("06_32.png"), "", 40 + (40 * 6), 20 + 140, 38, 38, %IDC_BTN_06, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("07_32.png"), "", 40 + (40 * 7), 20 + 140, 38, 38, %IDC_BTN_07, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("08_32.png"), "", 40 + (40 * 8), 20 + 140, 38, 38, %IDC_BTN_08, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)
        hCtrl = skPushButtonImage(hWnd, Resource("09_32.png"), "", 40 + (40 * 9), 20 + 140, 38, 38, %IDC_BTN_09, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER)

        hCtrl = skClockCtrl(hWnd, Resource("Clock_08_92.png"), 14, 258, 0, 0, %IDC_CLOCK, &H7FCB0000, &H7F8F8F8F, 0)
        skSetAnchorCtrl(hCtrl, %ANCHOR_BOTTOM)

        hCtrl = skButtonImage(hWnd, Resource("32bit_80.png"), 380, 264, %IDC_BTN_64BIT, 5)
        skSetAnchorCtrl(hCtrl, %ANCHOR_BOTTOM_RIGHT)

        hCtrl = skStaticImage(hWnd, Resource("Aero.png"), 10, 10, 460, 208, %IDC_FRAME)
        skSetZorder(hCtrl, %HWND_BOTTOM)
        skSetAnchorCtrl(hCtrl, %ANCHOR_HEIGHT_WIDTH)

        hCtrl = skStaticImage(hWnd, Resource("makeuplips.png"), -20, -70, 200, 134, %IDC_LIPS)
        skSetZorder(hCtrl, %HWND_TOP)

        hCtrl = skButtonImage(hWnd, Resource("BTN_Help.png"), 216, 238, %IDC_BTN_HELP, 5)
        skSetAnchorCtrl(hCtrl, %ANCHOR_CENTER_HORZ_BOTTOM)
        skCreateToolTip (hCtrl, "Open WinLFT.chm")
    end if
end sub

function WndProc(byval hWnd as dword, byval uMsg as dword, byval wParam as dword, byval lParam as long) as long
    local wID, wEvent, nCount, nListItem as long
    local zSkinFile as wstringz * %MAX_PATH
    local hList as dword

    select case long (uMsg)

    case %WM_CREATE:
        AddChildControls(hWnd)        

    case %WM_GETMINMAXINFO:
        local pMM as MINMAXINFO ptr
        pMM = lParam
        @pMM.ptMinTrackSize.x = gP.minWidth
        @pMM.ptMinTrackSize.y = gP.minHeight      

    case %WM_COMMAND:
        wID    = LOINT(wParam)
        wEvent = HIINT(wParam)
        select case long (wID)
        case %IDC_BTN_LEFT, %IDC_BTN_RIGHT:
            hList = GetDlgItem(hWnd, %IDC_SKIN_LIST)
            nCount = SendMessage(hList, %LB_GETCOUNT, 0, 0) - 1
            nListItem = SendMessage(hList, %LB_GETCURSEL, 0, 0)
            if (uMsg = %IDC_BTN_LEFT) then
                if (nListItem > 0) then
                    decr nListItem
                else
                    nListItem = nCount
                end if
            else
                if (nListItem < nCount) then
                    incr nListItem
                else
                    nListItem = 0
                end if
            end if
            SendMessage(hList, %LB_SETCURSEL, nListItem, 0)
            SendMessage(hList, %LB_GETTEXT, nListItem, varptr(zSkinFile))
            if ("Sony.sks" = zSkinFile) then
                ShowWindow(GetDlgItem(hWnd, %IDC_LIPS), %SW_SHOW)
            else
                ShowWindow(GetDlgItem(hWnd, %IDC_LIPS), %SW_HIDE)
            end if
            skSkinChange(zSkinFile)
            function = 0: exit function
        case %IDC_BTN_64BIT:
            'skDialogInfo("About", "\nWinLIFT 7.00\n\nModal dialog box\nto popup a specific message.", "OK")
            local zMsg as wstringz * %MAX_PATH
            zMsg = $CRLF + "WinLIFT 7.00" + $CRLF + $CRLF + "Modal dialog box" + $CRLF + "to popup a specific message."
            skDialogInfo("About", zMsg, "OK")
            function = 0: exit function
        case %IDC_BTN_HELP:
            ShellExecute(byval %NULL, "open", "winlift.chm", "", "", %SW_SHOW)
            function = 0: exit function
        end select
        

    case %WM_DESTROY:
        PostQuitMessage(0)
        
    end select

    function = DefWindowProc(hWnd, uMsg, wParam, lParam)
end function

function WinMain(byval hInstance as long, byval hPrevInstance as long, byval lpCmdLine as asciiz ptr, byval iCmdShow as long) as long

    local IsInitialized, nRet as long
    local wcx as WNDCLASSEX
    local szClass as WSTRINGZ * 10
    szClass = "ZBUTTONS"

    gP.hInstance = hInstance

    '// Detect process without using a Mutex
    local hFound as dword
    hFound = FindWindow(szClass, "")
    if (hFound) then
        if (IsIconic(hFound)) then ShowWindow(hFound, %SW_RESTORE)
        SetForegroundWindow(hFound)
        function = nRet
        exit function
    end if

    '// Register the main window class
    wcx.cbSize = sizeof(WNDCLASSEX)
    IsInitialized = GetClassInfoEx(hInstance, szClass, wcx)
    if (not IsInitialized) then
         wcx.style         = %CS_HREDRAW or %CS_VREDRAW
         wcx.lpfnWndProc   = codeptr(WndProc)
         wcx.cbClsExtra    = 0
         wcx.cbWndExtra    = 0
         wcx.hInstance     = hInstance
         wcx.hIcon         = LoadIcon(hInstance, byval 2)
         wcx.hCursor       = LoadCursor(%NULL, byval %IDC_ARROW)
         wcx.hbrBackground = %COLOR_WINDOW + 1
         wcx.lpszMenuName  = %NULL
         wcx.lpszClassName = varptr(szClass)
         wcx.hIconSm       = wcx.hIcon
         if (RegisterClassEx(wcx)) then IsInitialized = -1
    end if
    if (IsInitialized) then

        local hWnd, dwExStyle, dwStyle as dword
        local x, y as long
        dwExStyle = 0: dwStyle = %WS_OVERLAPPEDWINDOW
        local r as rect
        SetRect(r, 0, 0, %ClientW, %ClientH)
        AdjustWindowRectEx(r, dwStyle, %FALSE, dwExStyle) '// Adjust Window To True Requested Size
        gP.minWidth = rWidth(r)
        gP.minHeight = rHeight(r)
        x = max((GetSystemMetrics(%SM_CXSCREEN) - gP.minWidth) \ 2, 0)
        y = max((GetSystemMetrics(%SM_CYSCREEN) - gP.minHeight) \ 2, 0)
        hWnd = CreateWindowEx(dwExStyle, szClass, "Skin themes", dwStyle, x, y, gP.minWidth, gP.minHeight, 0, 0, hInstance, BYVAL %NULL)

        if (hWnd) then
            MoveWindow(hWnd, x - 75, y - 20, gP.minWidth + 150, gP.minHeight + 40, 0)
            ShowWindow(hWnd, iCmdShow)
            UpdateWindow(hWnd)

            '// Main message loop
            local msg as tagMsg
            while (GetMessage(msg, byval %NULL, 0, 0))
                TranslateMessage(msg)
                DispatchMessage(msg)
            wend
            nRet = msg.wParam
        end if

    end if

    function = nRet
end function
