//+------------------------------------------------------------------+
//|                                               New_Chart_Indi.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Phylo-File45"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#include <WinUser32.mqh>

#import "user32.dll"
   int      PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int      GetWindow(int hWnd,int uCmd);
   int      GetParent(int hWnd);
#import

input int templateIndex = 0; // Template Index (1st = 0, 2nd = 1, 3rd = 2, etc)
input int LR = 120; // Left-Right
input int UD = 55; // Up-Down
input ENUM_BASE_CORNER BC = 3; // Corner

input color BT1 = White; // Button Font
input bool  FB  = false; // Font Bold
input color BodC = Blue; // Button Body
input color BB = SlateGray; // Button Border

input int BFS = 12; // Button Font Size

string FBx;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   switch(FB)
   {
     case 0: FBx = "Arial"; break;
     case 1: FBx = "Arial Bold";
   } 
  
   ObjectCreate(0,"NewChartButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_XDISTANCE,LR);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_YDISTANCE,UD);
   ObjectSetInteger(0,"NewChartButton", OBJPROP_CORNER, BC);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_XSIZE,100);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_YSIZE,40);
   ObjectSetString(0,"NewChartButton",OBJPROP_TEXT,"New Chart");
   ObjectSetString(0,"NewChartButton",OBJPROP_FONT, FBx);
    
   ObjectSetInteger(0,"NewChartButton",OBJPROP_COLOR,BT1);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_BGCOLOR,BodC);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_BORDER_COLOR,BB);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_STATE,false);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_FONTSIZE,BFS);
   ObjectSetInteger(0,"NewChartButton",OBJPROP_BACK,false); 
   
   
   return(INIT_SUCCEEDED);
}
  
void OnDeinit(const int reason)
{
   ObjectDelete("NewChartButton");
}  
//+------------------------------------------------------------------+
//| 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(rates_total);
}
 
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="NewChartButton")
   {
     if(ChartOpen(Symbol(),0))
     {
       bool blnContinue = true;   
       int intParent = GetParent( WindowHandle( Symbol(), Period() ) );   
       int intChild = GetWindow( intParent, GW_HWNDFIRST );  
     
       if(intChild > 0)   
       {
         if (intChild != intParent) PostMessageA( intChild, WM_COMMAND, 34800 + templateIndex, 0 );
       }
       else  
       {  
          blnContinue = false;   
       }
        
    }
  }
}  
