/**
 * real_time_info.mq4
 *
 * Copyright © 2009 Trader 613, Roman Ignatov
 *
 * mailto:mqlexpert@gmail.com
 *
 * $Id: real_time_info.mq4,v 1.1.2.4 2009/05/25 07:54:46 r.ignatov Exp $
 */


/**
 * To change board location select Diamond handle on the top left corner 
 * and drag it to a new position.
 *
 * To change background and info element colours 
 * select corresponding graphic object and edit its colour.
 * 
 * The board position as well as info element colour scheme 
 * is stored in experts\files\real_time_info.txt and restores at the next initialisation.
 * To use parameters from the setup dialogue set LastSetup property to false.
 */


#property copyright "Copyright © 2009 Trader 613, Roman Ignatov"
#property link      "mailto:mqlexpert@gmail.com"


#include <stdlib.mqh>
#include <stderror.mqh>
#import "user32.dll"
int GetDC(int hwnd);
int ReleaseDC(int hwnd,int hdc);
int GetWindowRect(int hWnd, int rect[4]);                    
#import "Gdi32.dll"
int GetDeviceCaps(int hdc,int p);
#import


#define FRANKFURT  0
#define LONDON     1
#define NEWYORK    2
#define TOKYO      3
#define SPREAD     4
#define TIMELEFT   5
#define INFO       6
#define ANCHOR     6
#define LABELS     7

#define RTODAY     0
#define RYTDAY     1
#define R2DAYS     2
#define R5DAVG     3
#define INFO2      4
#define ANCHOR2    4
#define LABELS2    5


#define BG0        13
#define BGSIZE     10
#define NAMESIZE   23

#define BG02       9
#define BGSIZE2    6
#define NAMESIZE2  15

#define TIMEZONESIZE 4

#define TIMEFONTSIZE     8
#define TEXTFONTSIZE     8
#define SPREADFONTSIZE   8
#define TIMELEFTFONTSIZE 13
#define ANCHORFONTSIZE   8
#define BGFONTSIZE       32

#define BGOFFSETX   9
#define BGOFFSETY   24

#define WIDTH  170
#define HEIGHT 170

#define LABELINDENT 13
#define INFOINDENT  69

#define LABELINDENT2 10
#define INFOINDENT2  71

#define TIMEOUT 200

extern int ChartCorner = 2; // 0 - Top left, 1 - Top right, 2 - Bottom Left, 3 - Bottom right
extern int ChartCorner2 = 0; // 0 - Top left, 1 - Top right, 2 - Bottom Left, 3 - Bottom right
extern bool LastSetup = false; // Restore position and colours from the last session or use values from the indicator setup dialogue. 

extern int FrankfurtTimeZone = -1;
extern int LondonTimeZone    = -2;
extern int NewYorkTimeZone   = -7;
extern int TokyoTimeZone     = 6;

extern color BgColour         = C'230,245,232';        
extern color AnchorColour     = C'230,245,232';         
extern color LabelColour      = Gray;       
                                                
extern color FrankfurtColour  = Sienna;           
extern color LondonColour     = Sienna;         
extern color NewYorkColour    = Sienna;         
extern color TokyoColour      = Sienna;          
extern color SpreadColour     = Sienna;  
extern color TimeLeftColour   = Sienna;  


string Font = "Tahoma";
/*
   Text =   "Today\'s Range: " +  R0  + "\n" 
            "Yesterday\'s Range: " +  R1   + "\n" + 
            "2 days ago Range: " +  R2   + "\n" + 
            "5 day average: " +  R5;
*/
string Name[NAMESIZE], Text[] =  {"Frankfurt", "London", "New York", "Tokyo", "Spread", "Time Left", "Anchor"};
double ScaleX, ScaleY, Place[NAMESIZE][2];
int Coordinates[BG0][2] = {INFOINDENT, 13, INFOINDENT, 28, INFOINDENT, 43, INFOINDENT, 58, INFOINDENT, 73, 20, 107, 0, 0,    LABELINDENT, 13, LABELINDENT, 28, LABELINDENT, 43, LABELINDENT, 58, LABELINDENT, 73, 33, 95};
int Handle, TZ[4], TimeFrame, TimeOut, XP, YP, RP[4];

string Name2[NAMESIZE2], Text2[] =  {"R Today", "R Yesterday", "R 2 Days ago", "R 5 Day Avg", "Anchor2"};
double ScaleX2, ScaleY2, Place2[NAMESIZE2][2];
int Coordinates2[BG02][2] = {INFOINDENT2, 13, INFOINDENT2, 28, INFOINDENT2, 43, INFOINDENT2, 58, 0, 0,  LABELINDENT2, 13, LABELINDENT2, 28, LABELINDENT2, 43, LABELINDENT2, 58};
int Handle2, TZ2[4], TimeFrame2, TimeOut2, XP2, YP2, RP2[4];

void start() {
}

int init()
{
    int i, h, x, y;
    int i2, h2, x2, y2;
    string s = "$Id: real_time_info.mq4,v 1.1.2.4 2009/05/25 07:54:46 r.ignatov Exp $";
    
    Print(StringSubstr(s, 5, StringFind(s, "/", 5) - 10));
    Print("Copyright © 2009, Trader 613, Roman Ignatov");
    Print("mailto:mqlexpert@gmail.com");

    XP = -1;
    YP = -1;
    RP[0] = -1;
    RP[1] = -1;
    RP[2] = -1;
    RP[3] = -1;

    XP2 = -1;
    YP2 = -1;
    RP2[0] = -1;
    RP2[1] = -1;
    RP2[2] = -1;
    RP2[3] = -1;
    
    h = GetDC(0);
    ScaleX = GetDeviceCaps(h, 88) / 96.;
    ScaleY = GetDeviceCaps(h, 90) / 96.;
    ReleaseDC(0, h);
    
    Handle = WindowHandle(Symbol(), Period());
    
    TZ[0] = FrankfurtTimeZone * 3600;
    TZ[1] = LondonTimeZone * 3600;
    TZ[2] = NewYorkTimeZone * 3600;
    TZ[3] = TokyoTimeZone * 3600;
    
    TimeFrame = Period() * 60;
    
    TimeOut = TIMEOUT;
    
    for (i = 0; i < BG0; i++)
    {
        Place[i][0] = Coordinates[i][0] * ScaleX + 0.5;
        Place[i][1] = Coordinates[i][1] * ScaleY + 0.5;
    }
    
    for (h = 0; h < BGSIZE; i++, h++)
    {
        Place[i][0] = h % 2 * BGOFFSETX * ScaleX + 0.5;
        Place[i][1] = h / 2 * BGOFFSETY * ScaleY + 0.5;
    }
    
    for (i2 = 0; i2 < BG02; i2++)
    {
        Place2[i2][0] = Coordinates2[i2][0] * ScaleX + 0.5;
        Place2[i2][1] = Coordinates2[i2][1] * ScaleY + 0.5;
    }
    
    for (h2 = 0; h2 < BGSIZE2; i2++, h2++)
    {
        Place2[i2][0] = h2 % 2 * BGOFFSETX * ScaleX + 0.5;
        Place2[i2][1] = h2 / 2 * BGOFFSETY * ScaleY + 0.5;
    }

    for (i = 0, x = LABELS; i < LABELS; i++)
    {
        Name[i] = StringConcatenate("RTI ", Text[i]);
        ObjectCreate(Name[i], OBJ_LABEL, 0, 0, 0.);

        if (i >= INFO) continue;

        Name[x] = StringConcatenate(Name[i], " label");
        ObjectCreate(Name[x], OBJ_LABEL, 0, 0, 0.);
        ObjectSet(Name[x], OBJPROP_COLOR, LabelColour);
        ObjectSetText(Name[x], Text[i], TEXTFONTSIZE, Font);
        x++;
    }

    for (i2 = 0, x2 = LABELS2; i2 < LABELS2; i2++)
    {
        Name2[i2] = StringConcatenate("RTI2 ", Text2[i2]);
        ObjectCreate(Name2[i2], OBJ_LABEL, 0, 0, 0.);

        if (i2 >= INFO2) continue;

        Name2[x2] = StringConcatenate(Name2[i2], " label");
        ObjectCreate(Name2[x2], OBJ_LABEL, 0, 0, 0.);
        ObjectSet(Name2[x2], OBJPROP_COLOR, LabelColour);
        ObjectSetText(Name2[x2], Text2[i2], TEXTFONTSIZE, Font);
        x2++;
    }

    ObjectSet(Name[FRANKFURT], OBJPROP_COLOR, FrankfurtColour);
    ObjectSet(Name[LONDON], OBJPROP_COLOR, LondonColour);
    ObjectSet(Name[NEWYORK], OBJPROP_COLOR, NewYorkColour);
    ObjectSet(Name[TOKYO], OBJPROP_COLOR, TokyoColour);
    ObjectSet(Name[SPREAD], OBJPROP_COLOR, SpreadColour);
    ObjectSet(Name[TIMELEFT], OBJPROP_COLOR, TimeLeftColour);
    
    ObjectSet(Name2[RTODAY], OBJPROP_COLOR, TimeLeftColour);
    ObjectSet(Name2[RYTDAY], OBJPROP_COLOR, TimeLeftColour);
    ObjectSet(Name2[R2DAYS], OBJPROP_COLOR, TimeLeftColour);
    ObjectSet(Name2[R5DAVG], OBJPROP_COLOR, TimeLeftColour);

    setPosition(5, 5);
    ObjectSet(Name[ANCHOR], OBJPROP_COLOR, AnchorColour);
    ObjectSetText(Name[ANCHOR], "u", ANCHORFONTSIZE, "Wingdings");

    setPosition2(5, 5);
    ObjectSet(Name2[ANCHOR2], OBJPROP_COLOR, AnchorColour);
    ObjectSetText(Name2[ANCHOR2], "u", ANCHORFONTSIZE, "Wingdings");

    for (i = BG0, x = 0; x < BGSIZE; i++, x++)
    {
        Name[i] = StringConcatenate("RTI Bg", x);
        ObjectCreate(Name[i], OBJ_LABEL, 0, 0, 0.);
    }   
    ObjectSet(Name[BG0], OBJPROP_COLOR, BgColour);
    
    for (i2 = BG02, x2 = 0; x2 < BGSIZE2; i2++, x2++)
    {
        Name2[i2] = StringConcatenate("RTI2 Bg", x2);
        ObjectCreate(Name2[i2], OBJ_LABEL, 0, 0, 0.);
    }   
    ObjectSet(Name2[BG02], OBJPROP_COLOR, BgColour);

    if (LastSetup)
    {
        
        if (h > 0)
        {
            i = FileReadNumber(h);
            x = FileReadNumber(h);
            y = FileReadNumber(h);
            if (GetLastError() == ERR_NO_ERROR)
            {
                if (ChartCorner != i) ChartCorner = i;
                else if (x >= 0) if (y >= 0) setPosition(x, y);

                for (i = BG0; i >= 0 && !FileIsEnding(h); i--)
                {
                    x = FileReadNumber(h);
                    y = GetLastError();
                    if (y != ERR_NO_ERROR) if (y != ERR_END_OF_FILE) break;
                    if (x >= 0) ObjectSet(Name[i], OBJPROP_COLOR, x);
                }
            }

            i = FileReadNumber(h);
            x = FileReadNumber(h);
            y = FileReadNumber(h);
            if (GetLastError() == ERR_NO_ERROR)
            {
                if (ChartCorner2 != i) ChartCorner2 = i;
                else if (x >= 0) if (y >= 0) setPosition2(x, y);

                for (i = BG02; i >= 0 && !FileIsEnding(h); i--)
                {
                    x = FileReadNumber(h);
                    y = GetLastError();
                    if (y != ERR_NO_ERROR) if (y != ERR_END_OF_FILE) break;
                    if (x >= 0) ObjectSet(Name2[i], OBJPROP_COLOR, x);
                }
            }

            FileClose(h);
        }
    }

    for (i = BG0, h = ObjectGet(Name[i], OBJPROP_COLOR); ;)
    {
        ObjectSetText(Name[i], "nnn", BGFONTSIZE, "Wingdings");
        i++;
        if (i >= NAMESIZE) break;
        ObjectSet(Name[i], OBJPROP_COLOR, h);
    }
    
    for (i2 = BG02, h2 = ObjectGet(Name2[i2], OBJPROP_COLOR); ;)
    {
        ObjectSetText(Name2[i2], "nnn", BGFONTSIZE, "Wingdings");
        i2++;
        if (i2 >= NAMESIZE2) break;
        ObjectSet(Name2[i2], OBJPROP_COLOR, h2);
    }

    while (!IsStopped())
    {
        setValues();
        setValues2();
        WindowRedraw();
        Sleep(TimeOut);
    }
}


void deinit()
{
    int i, h, c, r[4];
    
   
    if (h >= 0)
    {        
        GetLastError();
        c = ObjectGet(Name[ANCHOR], OBJPROP_XDISTANCE);
        if (GetLastError() != ERR_NO_ERROR)
        {
            c = -1;
            i = -1;
        }
        else
        {
            i = ObjectGet(Name[ANCHOR], OBJPROP_YDISTANCE);
            setPosition(c, i);
            c = ObjectGet(Name[ANCHOR], OBJPROP_XDISTANCE);
            i = ObjectGet(Name[ANCHOR], OBJPROP_YDISTANCE);
        }
    
        FileWrite(h, ChartCorner, c, i);

        for (i = BG0; i >= 0; i--)
        {
            GetLastError();
            c = ObjectGet(Name[i], OBJPROP_COLOR);
            if (GetLastError() != ERR_NO_ERROR) c = -1;
            FileWrite(h, c);
        }
        
        GetLastError();
        c = ObjectGet(Name2[ANCHOR2], OBJPROP_XDISTANCE);
        if (GetLastError() != ERR_NO_ERROR)
        {
            c = -1;
            i = -1;
        }
        else
        {
            i = ObjectGet(Name2[ANCHOR2], OBJPROP_YDISTANCE);
            setPosition2(c, i);
            c = ObjectGet(Name2[ANCHOR2], OBJPROP_XDISTANCE);
            i = ObjectGet(Name2[ANCHOR2], OBJPROP_YDISTANCE);
        }
    
        FileWrite(h, ChartCorner2, c, i);

        for (i = BG02; i >= 0; i--)
        {
            GetLastError();
            c = ObjectGet(Name2[i], OBJPROP_COLOR);
            if (GetLastError() != ERR_NO_ERROR) c = -1;
            FileWrite(h, c);
        }

        FileClose(h);
    }
    
    for (i = NAMESIZE - 1; i >= 0; i--) ObjectDelete(Name[i]);
    for (i = NAMESIZE2 - 1; i >= 0; i--) ObjectDelete(Name2[i]);
    WindowRedraw();
}


void setPosition(int x, int y)
{
    int r[4];

    if (ChartCorner != 0) GetWindowRect(Handle, r);
    if (ChartCorner % 2 != 0) x = r[2] - r[0] - x - WIDTH * ScaleX + 0.5;
    ObjectSet(Name[ANCHOR], OBJPROP_XDISTANCE, x);
    if (ChartCorner / 2 != 0) y = r[3] - r[1] - y - HEIGHT * ScaleY + 0.5;
    ObjectSet(Name[ANCHOR], OBJPROP_YDISTANCE, y);
}

void setPosition2(int x, int y)
{
    int r[4];

    if (ChartCorner2 != 0) GetWindowRect(Handle, r);
    if (ChartCorner2 % 2 != 0) x = r[2] - r[0] - x - WIDTH * ScaleX + 0.5;
    ObjectSet(Name2[ANCHOR2], OBJPROP_XDISTANCE, x);
    if (ChartCorner2 / 2 != 0) y = r[3] - r[1] - y - HEIGHT * ScaleY + 0.5;
    ObjectSet(Name2[ANCHOR2], OBJPROP_YDISTANCE, y);
}

void setValues()
{
    static int counter = 0;
    datetime tm;
    int i, j, x, y, r[4];
    string s;
    
    RefreshRates();
    
    tm = TimeLocal();
    
    for (i = 0; i < TIMEZONESIZE; i++) ObjectSetText(Name[i], TimeToStr(tm + TZ[i], TIME_MINUTES), TIMEFONTSIZE, Font);

    i = (Ask - Bid) / Point + 0.5;
    s = i;
    ObjectSetText(Name[SPREAD], s, SPREADFONTSIZE, Font);
    
    ObjectSetText(Name[TIMELEFT], TimeToStr(TimeFrame - tm % TimeFrame, TIME_MINUTES | TIME_SECONDS), TIMELEFTFONTSIZE, Font);

    x = ObjectGet(Name[ANCHOR], OBJPROP_XDISTANCE);
    y = ObjectGet(Name[ANCHOR], OBJPROP_YDISTANCE);

    if (GetTickCount() > counter) TimeOut = TIMEOUT;
    
    if (XP == x && YP == y)
    {
        GetWindowRect(Handle, r);
        if (r[0] == RP[0]) if (r[1] == RP[1]) if (r[2] == RP[2]) if (r[3] == RP[3]) return;

        if (RP[0] >= 0)
        {
            x = r[2] - r[0];
            if (ChartCorner % 2 != 0) XP += x - (RP[2] - RP[0]);
            y = r[3] - r[1];
            if (ChartCorner / 2 != 0) YP += r[3] - r[1] - (RP[3] - RP[1]);

            x -= WIDTH;
            if (XP > x) XP = x;
            if (XP < 5) XP = 5;
            y -= HEIGHT;
            if (YP > y) YP = y;
            if (YP < 5) YP = 5;
        }
        
        RP[0] = r[0];
        RP[1] = r[1];
        RP[2] = r[2];
        RP[3] = r[3];
    }
    else
    {
        XP = x;
        YP = y;
    }
    
    counter = GetTickCount() + 1000;
    TimeOut = 10;
    
    //for (i = NAMESIZE - 1; i >= 0; i--)
    for (i = 0; i<NAMESIZE; i++)
    {
        ObjectSet(Name[i], OBJPROP_XDISTANCE, XP + Place[i][0]);
        ObjectSet(Name[i], OBJPROP_YDISTANCE, YP + Place[i][1]);
    }
}


void setValues2()
{
    static int counter2 = 0;
    datetime tm;
    int i, j, x, y, r[4];
    string s;
    
    RefreshRates();
    
   int R0,R1,R2,R5;

   R0 =  (iHigh(NULL,PERIOD_D1,0)-iLow(NULL,PERIOD_D1,0))/Point;
   R1 =  (iHigh(NULL,PERIOD_D1,1)-iLow(NULL,PERIOD_D1,1))/Point;
   R2 =  (iHigh(NULL,PERIOD_D1,2)-iLow(NULL,PERIOD_D1,2))/Point;

   for(i=1;i<=5;i++)
      R5 = R5 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;

   R5 = R5/5;

   ObjectSetText(Name2[RTODAY], DoubleToStr(R0,0), TIMEFONTSIZE, Font);
   ObjectSetText(Name2[RYTDAY], DoubleToStr(R1,0), TIMEFONTSIZE, Font);
   ObjectSetText(Name2[R2DAYS], DoubleToStr(R2,0), TIMEFONTSIZE, Font);
   ObjectSetText(Name2[R5DAVG], DoubleToStr(R5,0), TIMEFONTSIZE, Font);

    x = ObjectGet(Name2[ANCHOR2], OBJPROP_XDISTANCE);
    y = ObjectGet(Name2[ANCHOR2], OBJPROP_YDISTANCE);

    if (GetTickCount() > counter2) TimeOut2 = TIMEOUT;
    
    if (XP2 == x && YP2 == y)
    {
        GetWindowRect(Handle, r);
        if (r[0] == RP2[0]) if (r[1] == RP2[1]) if (r[2] == RP2[2]) if (r[3] == RP2[3]) return;

        if (RP2[0] >= 0)
        {
            x = r[2] - r[0];
            if (ChartCorner2 % 2 != 0) XP2 += x - (RP2[2] - RP2[0]);
            y = r[3] - r[1];
            if (ChartCorner2 / 2 != 0) YP2 += r[3] - r[1] - (RP2[3] - RP2[1]);

            x -= WIDTH;
            if (XP2 > x) XP2 = x;
            if (XP2 < 5) XP2 = 5;
            y -= HEIGHT;
            if (YP2 > y) YP2 = y;
            if (YP2 < 5) YP2 = 5;
        }
        
        RP2[0] = r[0];
        RP2[1] = r[1];
        RP2[2] = r[2];
        RP2[3] = r[3];
    }
    else
    {
        XP2 = x;
        YP2 = y;
    }
    
    counter2 = GetTickCount() + 1000;
    TimeOut2 = 10;
    
    //for (i = NAMESIZE - 1; i >= 0; i--)
    for (i = 0; i<NAMESIZE2; i++)
    {
        ObjectSet(Name2[i], OBJPROP_XDISTANCE, XP2 + Place2[i][0]);
        ObjectSet(Name2[i], OBJPROP_YDISTANCE, YP2 + Place2[i][1]);
    }
}

