$ filename "Demo1.exe"

'uses rtl32
'uses rtl64

uses WinUtil
uses autosizing

% HWND_DESKTOP=0
% LOGPIXELSX=88
% LOGPIXELSY=90


sys Button_TL, Button_TR, MLE_TLBR, Button_BL, Button_BR

enum  Control_ids
   ID_Button_TL=1001
   ID_Button_TR
   ID_MLE_TLBR
   ID_Button_BL
   ID_Button_BR   
end enum

% Btn_Style    = WS_CHILD|WS_VISIBLE|WS_TABSTOP
% Btn_ExtStyle = WS_EX_STATICEDGE

% ID_MENU0=9000
% ID_MENU1=9001
% ID_MENU2=9002
% ID_MENU3=9003
% ID_MENU4=9004

Function O2CreateFont(szFaceName As Zstring,Byval lPointSize As long) As sys
'James Fuller
    Dim tlf As LOGFONT
    Dim hdc As sys
    Dim nNum As long
    hdc = GetDc(%HWND_DESKTOP)
    nNum = GetDeviceCaps(hdc, %LOGPIXELSY)
    tlf.lfHeight         = -MulDiv(lPointSize,nNum , 72)
    tlf.lfWidth          =  0
    tlf.lfEscapement     =  0
    tlf.lfOrientation    =  0
    tlf.lfWeight         =  0
    tlf.lfItalic         =  0
    tlf.lfUnderline      =  0
    tlf.lfStrikeOut      =  0
    tlf.lfCharSet        =  %ANSI_CHARSET
    tlf.lfOutPrecision   =  %OUT_TT_PRECIS
    tlf.lfClipPrecision  =  %CLIP_DEFAULT_PRECIS
    tlf.lfQuality        =  %DEFAULT_QUALITY
    tlf.lfPitchAndFamily =  %FF_DONTCARE
    tlf.lfFaceName       =  szFaceName
    ReleaseDC(%HWND_DESKTOP, hdc)
Function = CreateFontIndirect(@tlf)
End Function


declare sub AddMenu(sys hwndOwner)

sys hInstance=inst
MainWindow 410,360,WS_OVERLAPPEDWINDOW

function createControl(string ctlclass, string Text, sys hwnd, int id, x,y,w,h, int Style, optional ExStyle=0) as sys
   sys hCtrl
   hCtrl=CreateWindowEx(ExStyle, ctlclass, Text, Style, x,y,w,h,  hWnd, id, hInstance, null)
   if hCtrl=null then mbox "Error: Cannot create " ctlclass
   return hCtrl
end function


function WndProc(sys hwnd, uint uMsg, sys wParam, lParam) as sys callback

    select uMsg

        case WM_CREATE
           SetWindowText(hwnd, "Autosizing Demo 1")
           sys hFont = O2CreateFont("Segoe UI",9)
           Button_TL = createControl("Button","Exit TL",hwnd,ID_Button_TL, 8, 8, 88, 24, Btn_Style, Btn_ExtStyle)
           SendMessage(Button_TL, WM_SETFONT,hFont,0)
           Button_TR = createControl("Button","TR",hwnd,ID_Button_TR, 296, 8, 88, 24, Btn_Style, Btn_ExtStyle)
           SendMessage(Button_TR, WM_SETFONT,hFont,0)
           MLE_TLBR = createControl("Edit","TLBR",hwnd,ID_MLE_TLBR, 110, 75, 180, 140, 
                         WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP|WS_HSCROLL|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_WANTRETURN, 
                         WS_EX_CLIENTEDGE)
           SendMessage(MLE_TLBR, WM_SETFONT,hFont,0)
           Button_BL = createControl("Button","BL",hwnd,ID_Button_BL, 8, 272, 88, 24, Btn_Style, Btn_ExtStyle)
           SendMessage(Button_BL, WM_SETFONT,hFont,0)
           Button_BR = createControl("Button","BR",hwnd,ID_Button_BR, 296, 272, 88, 24, Btn_Style, Btn_ExtStyle)
           SendMessage(Button_BR, WM_SETFONT,hFont,0)

           AddMenu(hwnd)

           pinCtl(Button_TL, hwnd, "TL")
           pinCtl(Button_TR, hwnd, "TR")
           pinCtl(MLE_TLBR, hwnd, "TL","BR" )
           pinCtl(Button_BL, hwnd, "BL")
           pinCtl(Button_BR, hwnd, "BR")

        case WM_COMMAND
           select loword(wParam)   'id
              case ID_MENU4, ID_Button_TL
                 SendMessage(hWnd, WM_CLOSE,0,0)
           end select

        case WM_GETMINMAXINFO
           setMinSize 340,270

        case WM_SIZE        
          'Resize the controls
          resizeControls(hwnd)

        case WM_CLOSE
           DeleteObject(hFont)
           DestroyWindow(hwnd)

        case WM_DESTROY
            PostQuitMessage(0)

        case else
           return DefWindowProc(hwnd, uMsg, wParam, lParam)

    end select

    return 0
end function


sub AddMenu(sys hwnd)
    sys MainMenu
    MainMenu=CreateMenu()

    sys FileMenu
    FileMenu=CreateMenu()
    InsertMenu(MainMenu,  0, MF_POPUP, FileMenu, "&File")
    AppendMenu(FileMenu, MF_STRING or MF_GRAYED, ID_MENU1, "&Open")
    AppendMenu(FileMenu, MF_STRING or MF_GRAYED, ID_MENU2, "&Save")
    AppendMenu(FileMenu, MF_SEPARATOR, 0, "")
    AppendMenu(FileMenu, MF_STRING, ID_MENU4, "&Exit")

    ' activate menu
    if not SetMenu(hwnd, MainMenu) then
      mbox "Error: Cannot create Menu"
    end if
end sub

