//+------------------------------------------------------------------+
//|                                                    BDMhelper.mq4 |
//|                                           Alguasta Software 2015 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Alguasta Software 2015"
#property link      ""
#property version   "1.06"
#property strict
#property indicator_chart_window

extern int xoffset=1; // Chart horizontal position
extern int yoffset=0; // Chart vertical position

#import "kernel32.dll"
int GetFileAttributesA(string);
int GetLastError(void);
int OpenFile(string, long &arr[], int);
#import

#import "user32.dll"
int GetForegroundWindow();
int FindWindowExW(int,int, string ,string);
int GetWindowRect(int,int &arr[]);
int GetClientRect(int,int &arr[]);
int SetWindowLongW(int,int,int);
int SetWindowLongPtr(int,int,int);
int GetWindowLongW(int,int);
int GetSystemMenu(int,bool);
int InsertMenuW(int,int,int,int,string);
int SetWindowPos(int,int,int,int,int,int,int);
int GetParent(int);
int GetWindowTextW(int,short &arr[],int);
int ShowWindow(int,int);
int GetClassNameW(int,short &arr[],int);
int GetDlgCtrlID(int);
int GetDlgItem(int,int);
bool IsZoomed(int);
bool InvalidateRect(int,int &arr[],bool);
bool AnimateWindow(int, int, int);
int GetWindowRect(int,int &arr[]);
bool MoveWindow(int,int,int,int,int,bool);
bool BringWindowToTop(int);
double GetLastError(void);
void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import

#define GWL_STYLE         -16 
#define WS_SIZEBOX        0x00040000
#define WS_MINIMIZEBOX    0x00020000
#define WS_MAXIMIZEBOX    0x00010000
#define WS_CAPTION        0x00C00000
#define WS_THICKFRAME     0x00040000
#define WS_SYSMENU        0x00080000
#define WS_MAXIMIZE       0x01000000
#define WS_MINIMIZE       0x20000000
#define WS_DLGFRAME       0x00400000
#define WS_POPUP          0x80000000
#define WS_CHILD          0x40000000
#define WS_THINFRAME      0x00100000
#define MF_STRING         0x00000000
#define MF_BYPOSITION     0x00000400
#define SW_MAXIMIZE       0x0003
#define SC_SIZE           0xF000
#define SC_MAXIMIZE       0xF030
#define SC_MINIMIZE       0xF020
#define SC_RESTORE        0xF120
#define SWP_NOSIZE        0x0001
#define SWP_NOMOVE        0x0002
#define SWP_NOZORDER      0x0004
#define SWP_NOREDRAW      0x0008
#define SWP_NOACTIVATE    0x0010
#define SWP_FRAMECHANGED  0x0020
#define  KEYEVENTF_KEYUP 0x0002

int originalwidth;
int originalheight;
int originalx;
int originaly;
int originaltf;
bool chartlarge = false;
int windowposinfo[4];
int mainposinfo[4];
color chartcolor;
color generalcolor;
color buycolor;
color sellcolor;

int wndh,wndhp;
int xpos,ypos,xsize,ysize;

string helpertemplates[8];
int sck[5];
string scriptnames[4];

int timebut[9]={1,5,15,30,60,240,1440,10080,43200};
string timebutname[9]={"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int isalarmcolor;
#define  NONE 0
#define  GENERAL 1
#define  BUY 2
#define  SELL 3

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   OnDeInit();

   ArrayInitialize(sck,-1);

   ObjectCreate(0,"chsize",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"chsize",OBJPROP_XSIZE,20);
   ObjectSetInteger(0,"chsize",OBJPROP_YSIZE,20);
   ObjectSetInteger(0,"chsize",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"chsize",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,"chsize",OBJPROP_YDISTANCE,yoffset+30);
   ObjectSetInteger(0,"chsize",OBJPROP_XDISTANCE,xoffset+1);
   ObjectSetString(0,"chsize",OBJPROP_FONT,"Arial");
   ObjectSetInteger(0,"chsize",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"chsize",OBJPROP_COLOR,clrWhite);
   ObjectSetString(0,"chsize",OBJPROP_TEXT,"SZ");
   ObjectSetInteger(0,"chsize",OBJPROP_BACK,0);
   ObjectSetInteger(0,"chsize",OBJPROP_STATE,0);
   ObjectSetInteger(0,"chsize",OBJPROP_SELECTABLE,0);

   ObjectCreate(0,"reset",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"reset",OBJPROP_XSIZE,20);
   ObjectSetInteger(0,"reset",OBJPROP_YSIZE,20);
   ObjectSetInteger(0,"reset",OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,"reset",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,"reset",OBJPROP_YDISTANCE,yoffset+50);
   ObjectSetInteger(0,"reset",OBJPROP_XDISTANCE,xoffset+1);
   ObjectSetString(0,"reset",OBJPROP_FONT,"Arial");
   ObjectSetInteger(0,"reset",OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,"reset",OBJPROP_COLOR,clrWhite);
   ObjectSetString(0,"reset",OBJPROP_TEXT,"RS");
   ObjectSetInteger(0,"reset",OBJPROP_BACK,0);
   ObjectSetInteger(0,"reset",OBJPROP_STATE,0);
   ObjectSetInteger(0,"reset",OBJPROP_SELECTABLE,0);

   if (StringSubstr(ChartSymbol(),0,1) != "#") {
   
   int yoff = yoffset+70;
   string tpl;
   for (int i=1;i<=8;i++) {
      tpl = GlobalVariableGetName("BDM$HT"+i+"$");
      if (tpl != "") {
         helpertemplates[i-1] = StringSubstr(tpl,8);  
         ObjectCreate(0,i+"helper",OBJ_BUTTON,0,0,0);
         ObjectSetInteger(0,i+"helper",OBJPROP_XSIZE,20);
         ObjectSetInteger(0,i+"helper",OBJPROP_YSIZE,20);
         ObjectSetInteger(0,i+"helper",OBJPROP_BGCOLOR,clrGray);
         ObjectSetInteger(0,i+"helper",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
         ObjectSetInteger(0,i+"helper",OBJPROP_YDISTANCE,yoff);
         ObjectSetInteger(0,i+"helper",OBJPROP_XDISTANCE,xoffset+1);
         ObjectSetString(0,i+"helper",OBJPROP_FONT,"Arial");
         ObjectSetInteger(0,i+"helper",OBJPROP_FONTSIZE,8);
         ObjectSetInteger(0,i+"helper",OBJPROP_COLOR,clrWhite);
         ObjectSetString(0,i+"helper",OBJPROP_TEXT,"T"+i);
         ObjectSetInteger(0,i+"helper",OBJPROP_BACK,0);
         ObjectSetInteger(0,i+"helper",OBJPROP_STATE,0);
         ObjectSetInteger(0,i+"helper",OBJPROP_SELECTABLE,0);
         yoff += 20;
      }
   }
 
   string scrp;
   int scriptkey;
   for (int i=1;i<=4;i++) {
      scriptkey = GlobalVariableGet("BDM$SK"+i);
      if (scriptkey > 0) {
         sck[i] = scriptkey;
         scrp = GlobalVariableGetName("BDM$SN"+i+"$");
         if (scrp != "")
            scriptnames[i-1] = StringSubstr(scrp,8);
         else
            scriptnames[i-1] = "Script"+i;
         ObjectCreate(0,i+"script",OBJ_BUTTON,0,0,0);
         ObjectSetInteger(0,i+"script",OBJPROP_XSIZE,20);
         ObjectSetInteger(0,i+"script",OBJPROP_YSIZE,20);
         ObjectSetInteger(0,i+"script",OBJPROP_BGCOLOR,clrGray);
         ObjectSetInteger(0,i+"script",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
         ObjectSetInteger(0,i+"script",OBJPROP_YDISTANCE,yoff);
         ObjectSetInteger(0,i+"script",OBJPROP_XDISTANCE,xoffset+1);
         ObjectSetString(0,i+"script",OBJPROP_FONT,"Arial");
         ObjectSetInteger(0,i+"script",OBJPROP_FONTSIZE,8);
         ObjectSetInteger(0,i+"script",OBJPROP_COLOR,clrWhite);
         ObjectSetString(0,i+"script",OBJPROP_TEXT,StringSubstr(scriptnames[i-1],0,2));
         ObjectSetInteger(0,i+"script",OBJPROP_BACK,0);
         ObjectSetInteger(0,i+"script",OBJPROP_STATE,0);
         ObjectSetInteger(0,i+"script",OBJPROP_SELECTABLE,0);
         yoff += 20;
      } else {
         sck[i] = -1;
      }
   }
   }
   chartlarge = GlobalVariableGet(ChartSymbol()+"L"+ChartID());
   xpos = GlobalVariableGet("BDM$TopLeftX");
   ypos = GlobalVariableGet("BDM$TopLeftY");
   xsize = GlobalVariableGet("BDM$LargeW");
   ysize = GlobalVariableGet("BDM$LargeH");
   chartcolor = ChartGetInteger(0,CHART_COLOR_BACKGROUND);
   generalcolor = GlobalVariableGet("BDM$NAC");
   buycolor = GlobalVariableGet("BDM$BAC");
   sellcolor = GlobalVariableGet("BDM$SAC");
   if (chartlarge == true) {
      enlargeButtons();
      createTimeButtons();
   }
   int alarm = GlobalVariableGet("BDM$ALARM$"+ChartID());

   if (alarm == GENERAL)
      ChartSetInteger(0,CHART_COLOR_BACKGROUND,GlobalVariableGet("$BDM$NAC"));
   else if (alarm == BUY)
      ChartSetInteger(0,CHART_COLOR_BACKGROUND,GlobalVariableGet("$BDM$BAC"));
   else if (alarm == SELL)
      ChartSetInteger(0,CHART_COLOR_BACKGROUND,GlobalVariableGet("$BDM$SAC"));
   
//   EventSetMillisecondTimer(500);

   return(INIT_SUCCEEDED);
  }
int OnDeInit()
  {
   ObjectDelete("chsize");
   ObjectDelete("reset");
   ObjectDelete("1helper");
   ObjectDelete("2helper");
   ObjectDelete("3helper");
   ObjectDelete("4helper");
   ObjectDelete("5helper");
   ObjectDelete("6helper");
   ObjectDelete("7helper");
   ObjectDelete("8helper");
   ObjectDelete("1script");
   ObjectDelete("2script");
   ObjectDelete("3script");
   ObjectDelete("4script");
   removeTimeButtons();

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {

static int prevevent = -1;
bool forcedsize = false;

    string clickedChartObject=ObjectGetString(0,sparam,OBJPROP_TEXT);
    string clickedObjectName = sparam;
    isalarmcolor = ChartGetInteger(0,CHART_COLOR_BACKGROUND);

    if (id == CHARTEVENT_CLICK && prevevent != CHARTEVENT_OBJECT_CLICK && GlobalVariableGet(ChartSymbol()+"L"+ChartID()) == 0
     && (isalarmcolor == generalcolor || isalarmcolor == buycolor || isalarmcolor == sellcolor)) {
         forcedsize = true;
         prevevent = id;
    }

    if (id==CHARTEVENT_OBJECT_CLICK || forcedsize == true) {

      prevevent = id;

      if (clickedObjectName == "reset") {
         ObjectSetInteger(0,"reset",OBJPROP_STATE,0);
//         ChartSetInteger(0,CHART_COLOR_BACKGROUND,chartcolor);
         GlobalVariableSet("BDM$ALARM$"+ChartID(),0);
         WindowRedraw();
         return;
      }

      if (clickedObjectName == "chsize" || forcedsize == true) {

         ObjectSetInteger(0,"chsize",OBJPROP_STATE,0);

         wndh = WindowHandle(Symbol(),Period());
         if (wndh == -1)
            return;

         wndhp = WindowHandle("EURSEK",PERIOD_M1);
         if (wndhp == -1)
            return;

         if (GlobalVariableGet(ChartSymbol()+"L"+ChartID()) == 0) {
            chartlarge = true;
            GlobalVariableSet(ChartSymbol()+"L"+ChartID(),1);
            resizeWindow(Symbol(),Period(),xpos,ypos,xsize,ysize);
            if (StringSubstr(ChartSymbol(),0,1) != "#") {
               applyTemplate();
               createTimeButtons();
            }
            enlargeButtons();
            ChartSetInteger(ChartID(),CHART_SHOW_DATE_SCALE,true);
            ChartSetInteger(ChartID(),CHART_SHOW_PRICE_SCALE,true);
         } else {
            GlobalVariableSet(ChartSymbol()+"L"+ChartID(),0);
            chartlarge = false;
            originalx = GlobalVariableGet(ChartSymbol()+"X"+ChartID());
            originaly = GlobalVariableGet(ChartSymbol()+"Y"+ChartID());
            originalwidth = GlobalVariableGet(ChartSymbol()+"W"+ChartID());
            originalheight = GlobalVariableGet(ChartSymbol()+"H"+ChartID());
            resizeWindow(Symbol(),Period(),originalx,originaly,originalwidth,originalheight);
            if (StringSubstr(ChartSymbol(),0,1) != "#") {
               applyTemplate();
               removeTimeButtons();
            }
            smallButtons();
         }
         WindowRedraw();
         return;
      }
      if (StringFind(clickedObjectName,"helper") >= 0) {
         int helpnum = StringToInteger(clickedObjectName);
         ObjectSetInteger(0,helpnum+"helper",OBJPROP_STATE,0);
         ChartApplyTemplate(ChartID(),helpertemplates[helpnum-1]+".tpl");
         WindowRedraw();
         return;
      }

      if (StringFind(clickedObjectName,"script") >= 0) {
         int scriptnum = StringToInteger(clickedObjectName);
         ObjectSetInteger(0,scriptnum+"script",OBJPROP_STATE,0);
         if (sck[scriptnum] != -1)
            hotKey(sck[scriptnum]);
         WindowRedraw();
         return;
      }


      for (int i=0;i<9;i++) {
         if (clickedObjectName == timebutname[i]) {
//            currtf = timebut[i];
            ChartSetSymbolPeriod(0,NULL,timebut[i]);
             resetTimeButs(clickedObjectName);
             ObjectSetInteger(0,clickedObjectName,OBJPROP_COLOR,clrRed);
            WindowRedraw();
            return;
         }  
      }
   }
   
}
    #define AW_HOR_POSITIVE            0x00000001
    #define AW_HOR_NEGATIVE            0x00000002
    #define AW_VER_POSITIVE            0x00000004
    #define AW_VER_NEGATIVE            0x00000008
    #define AW_CENTER                  0x00000010
    #define AW_HIDE                    0x00010000
    #define AW_ACTIVATE                0x00020000
    #define AW_SLIDE                   0x00040000
    #define AW_BLEND                   0x00080000

 void resizeWindow(string symbol, int period, int xpos, int ypos, int xsize, int ysize) {


   wndh = WindowHandle(symbol,period);
   if (wndh == -1)
      return;

   wndhp = GetParent(wndh);
   if (wndhp == -1)
      return;

   AnimateWindow(wndhp,500,AW_HIDE|AW_CENTER);
   MoveWindow(wndhp,xpos,ypos,xsize,ysize,false);
   BringWindowToTop(wndhp);
   ShowWindow(wndhp,5);
   
}  
void createTimeButtons() {
   for(int i=0;i<9;i++) {
   ObjectCreate(0,timebutname[i],OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,timebutname[i],OBJPROP_XSIZE,25);
   ObjectSetInteger(0,timebutname[i],OBJPROP_YSIZE,20);
   if (Period() == timebut[i])
      ObjectSetInteger(0,timebutname[i],OBJPROP_COLOR,clrRed);
   else
      ObjectSetInteger(0,timebutname[i],OBJPROP_COLOR,clrWhite);
   ObjectSetInteger(0,timebutname[i],OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,timebutname[i],OBJPROP_YDISTANCE,yoffset+30);
   ObjectSetInteger(0,timebutname[i],OBJPROP_XDISTANCE,xoffset+60 + i * 30);
   ObjectSetString(0,timebutname[i],OBJPROP_FONT,"Arial");
   ObjectSetInteger(0,timebutname[i],OBJPROP_FONTSIZE,8);
   ObjectSetInteger(0,timebutname[i],OBJPROP_BGCOLOR,clrGray);
   ObjectSetString(0,timebutname[i],OBJPROP_TEXT,timebutname[i]);
   ObjectSetInteger(0,timebutname[i],OBJPROP_BACK,0);
   ObjectSetInteger(0,timebutname[i],OBJPROP_STATE,0);
   ObjectSetInteger(0,timebutname[i],OBJPROP_SELECTABLE,0);
   }
}
void removeTimeButtons() {
   for(int i=0;i<9;i++) {
      ObjectDelete(0,timebutname[i]);
   }
}
void resetTimeButs(string except) {

   for(int i=0;i<9;i++) {
      if (timebutname[i] != except) {
         ObjectSetInteger(0,timebutname[i],OBJPROP_STATE,0);
         ObjectSetInteger(0,timebutname[i],OBJPROP_COLOR,clrWhite);
      } else {
         ObjectSetInteger(0,timebutname[i],OBJPROP_STATE,1);
         ObjectSetInteger(0,timebutname[i],OBJPROP_COLOR,clrRed);
      }
   }         
}
void enlargeButtons() {

   if (ObjectFind(0,"chsize") >= 0) {
      ObjectSetInteger(0,"chsize",OBJPROP_XSIZE,50);
      ObjectSetString(0,"chsize",OBJPROP_TEXT,"Size");
   }
   if (ObjectFind(0,"reset") >= 0) {
      ObjectSetInteger(0,"reset",OBJPROP_XSIZE,50);
      ObjectSetString(0,"reset",OBJPROP_TEXT,"Reset");
   }
   for (int i=1;i<=8;i++) {
      if (ObjectFind(0,i+"helper") >= 0) {
         ObjectSetInteger(0,i+"helper",OBJPROP_XSIZE,50);
         ObjectSetString(0,i+"helper",OBJPROP_TEXT,helpertemplates[i-1]);
      }
   }
   for (int i=1;i<=4;i++) {
      if (ObjectFind(0,i+"script") >= 0) {
         ObjectSetInteger(0,i+"script",OBJPROP_XSIZE,50);
         ObjectSetString(0,i+"script",OBJPROP_TEXT,scriptnames[i-1]);
      }
   }
}
void smallButtons() {
   if (ObjectFind(0,"chsize") >= 0) {
      ObjectSetInteger(0,"chsize",OBJPROP_XSIZE,20);
      ObjectSetString(0,"chsize",OBJPROP_TEXT,"SZ");
   }
   if (ObjectFind(0,"reset") >= 0) {
      ObjectSetInteger(0,"reset",OBJPROP_XSIZE,20);
      ObjectSetString(0,"reset",OBJPROP_TEXT,"RS");
   }
   for (int i=1;i<=8;i++) {
      if (ObjectFind(0,i+"helper") >= 0) {
         ObjectSetInteger(0,i+"helper",OBJPROP_XSIZE,20);
         ObjectSetString(0,i+"helper",OBJPROP_TEXT,"T"+i);
      }
   }
   for (int i=1;i<=4;i++) {
      if (ObjectFind(0,i+"script") >= 0) {
         ObjectSetInteger(0,i+"script",OBJPROP_XSIZE,20);
         ObjectSetString(0,i+"script",OBJPROP_TEXT,StringSubstr(scriptnames[i-1],0,2));
      }
   }
}
string GlobalVariableGetName(string test) {

int globals = GlobalVariablesTotal();

   for (int i=0;i<globals;i++) {
      if (StringFind(GlobalVariableName(i),test) != -1)
         return(GlobalVariableName(i));
   }
   return("");
}
void applyTemplate() {

string alarmtemplate;

   if (chartlarge == true) {
      if (isalarmcolor == generalcolor || isalarmcolor == buycolor || isalarmcolor == sellcolor) {
         alarmtemplate = GlobalVariableGetName("BDM$ATPL$");
         if (alarmtemplate != "") {
            ChartApplyTemplate(0,StringSubstr(alarmtemplate,9)+".tpl");
            ChartSetInteger(0,CHART_COLOR_BACKGROUND,clrWhite);
         }
      } else {
         ChartApplyTemplate(0,helpertemplates[0]);
      }
      return;
   }
   if (chartlarge == false) {
      alarmtemplate = GlobalVariableGetName("BDM$TPL$");
      if (alarmtemplate != "")
         ChartApplyTemplate(0,StringSubstr(alarmtemplate,8)+".tpl");
   }
}
void hotKey(int key) {
         keybd_event(0x12, 0, 0, 0);
         keybd_event(key+48, 0, 0 , 0);
         keybd_event(key+48, 0, KEYEVENTF_KEYUP , 0);
         keybd_event(0x12, 0, KEYEVENTF_KEYUP, 0);
}
