#property strict
#property indicator_chart_window

extern ENUM_BASE_CORNER InpCorner   =  CORNER_LEFT_UPPER;   // Graph corner for attachment
extern int              btnx_coor      =  10;                  // Coordinate X
extern int              btny_coor      =  20;                  // Coordinate X
extern color            panl_0_cl   =  clrGainsboro;        // Toolbar color
extern bool             panl_0_st   =  true;               // Show background


string obj_name[2] = {"name_1","name_2"};

bool              InpSelection      =  false;               // Select for Movements
bool              InpHidden         =  true;                // Hidden in the list of objects
bool              InpHidden_OBJ     =  false;               // Hidden in the list of objects
bool              InpBackRect       =  false;               // Object in the background

int objx_coor = btnx_coor;
int objy_coor = btny_coor+20;
int x_size = 283;
int y_size = 30;
int x_objsize = 155;
int x_step = 5;
int y_panl = 20;
int x_rect = 20;
int y_rect = 20;
int y_line = 6;

//+------------------------------------------------------------------+
//| Custom Indicator Initialization Function                         |
//+------------------------------------------------------------------+
int OnInit()
{
  if (ObjectFind(0,obj_name[0])!=0)
  {     
          CreateButton();
          
  }
  
  ObjectSetInteger(0,obj_name[0],OBJPROP_CORNER,InpCorner);
  

  if (GetButtonState(obj_name[0])!="off")
  {
          CreatePanel();
  }
  else 
  {
   RectLabelDelete(0,obj_name[1]);
  }
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom Indicator Deinitialization Function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//   Comment("");
  if(reason!=REASON_CHARTCHANGE)
  {
      ButtonDelete(0,obj_name[0]);
      RectLabelDelete(0,obj_name[1]);
  }
}


//+------------------------------------------------------------------+
//| Chart Event Function                                             |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   
   datetime dt_1     = 0;
   double   price_1  = 0;
   datetime dt_2     = 0;
   double   price_2  = 0;
   int      window   = 0;
   int      x        = 0;
   int      y        = 0;
   
   
   // Clicking On The BUtton
   
   static string prevState ="";
   if (id==CHARTEVENT_OBJECT_CLICK && sparam==obj_name[0])
   {
      string newState = GetButtonState(obj_name[0]);
         if (newState!=prevState){
         if (newState=="off")
                  { 
   RectLabelDelete(0,obj_name[1]);
   prevState=newState; 
                  }
            else  { 
          CreatePanel();
                    prevState=newState; 
                  }
                  }
       }
}
//+------------------------------------------------------------------+
//| Identify Button State                                            |
//+------------------------------------------------------------------+
string GetButtonState(string whichbutton)
{
      bool selected = ObjectGetInteger(0,whichbutton,OBJPROP_STATE);
      if (selected)
           { return ("on"); } 
      else { return ("off");}
}
//+------------------------------------------------------------------+
//| Create Button Function                                           |
//+------------------------------------------------------------------+
void CreateButton()
{
   // Button
   int x_pn = btnx_coor , y_pn = btny_coor;
   if (InpCorner == 1)  x_pn = btnx_coor + x_size + x_step;
   if (InpCorner == 2)  y_pn = btny_coor + y_rect + x_step;
   if (InpCorner == 3) {x_pn = btnx_coor + x_size + x_step; y_pn = btny_coor + y_rect + x_step;}
   
   if (!ButtonCreate(0,obj_name[0],0,x_pn,y_pn,x_rect+50,y_rect,InpCorner,"Toolbar","Arial",10,clrRed,clrWhite,clrBlack,true,true,InpSelection,true,0)) {
      return;
   }   
   ObjectSetString(0,"test",OBJPROP_TOOLTIP,"Toolbar SHOW/HIDE");
}
//+------------------------------------------------------------------+
//| Create Panel Function                                            |
//+------------------------------------------------------------------+
void CreatePanel()
{
   // Toolbar
   if (panl_0_st) {
      int x_pn = objx_coor, y_pn = objy_coor;
      if (InpCorner == 1)  x_pn = objx_coor + x_size + x_step;
      if (InpCorner == 2)  y_pn = objy_coor + y_rect + x_step;
      if (InpCorner == 3) {x_pn = objx_coor + x_size + x_step; y_pn = objy_coor + y_rect + x_step;}
   
      if (!RectLabelCreate(0,obj_name[1],0,x_pn,y_pn,x_size,y_size,panl_0_cl,BORDER_SUNKEN,InpCorner,
           clrBlack,STYLE_SOLID,2,true,InpSelection,true,0)) {
         return;
      }   
   } else {
      panl_0_cl = ChartBackColorGet(0);
   }
}
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
//| Create A Button                                                  |
//+------------------------------------------------------------------+
bool ButtonCreate(const long              chart_ID=0,               // chart ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,                 // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',  // background color
                  const color             border_clr=clrNONE,       // border color
                  const bool              state=false,              // pressed/released
                  const bool              back=false,               // in the background
                  const bool              selection=false,          // highlight to move
                  const bool              hidden=true,              // hidden in the object list
                  const long              z_order=0)                // priority for mouse click
{
   //--- reset error value
   ResetLastError();
   //--- create a button
   if (ObjectFind(name) == -1)
   ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0);
   
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);              // set the coordinates of the button
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);              // set the size of the label
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);            // set the angle of the graph, relative to which the coordinates of the point
   ObjectSetString (chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);         // set the background color
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);                // display on the front (false) or back (true) plan
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);     // enable (true) or disable (false) the mouse move mode
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);            // hide (true) or display (false) the name of the graphic in the list of objects
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);           // set the priority to receive the mouse click event on the graph
   //--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Delete A Button                                                  |
//+------------------------------------------------------------------+
bool ButtonDelete(const long   chart_ID=0,    // chart's ID
                  const string name="Button") // button name
{
   //--- reset the error value
   ResetLastError();
   //--- delete a button
   if (ObjectFind(chart_ID,name) >= 0) 
      ObjectDelete(chart_ID,name);
   //--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Create A Rectangular Label                                      |
//+------------------------------------------------------------------+
bool RectLabelCreate(const long             chart_ID=0,               // chart ID
                     const string           name="RectLabel",         // label name
                     const int              sub_window=0,             // the number of the subwindow
                     const int              x=0,                      // coordinate along the X axis
                     const int              y=0,                      // coordinate along the Y axis
                     const int              width=50,                 // width
                     const int              height=18,                // height
                     const color            back_clr=C'236,233,216',  // background color
                     const ENUM_BORDER_TYPE border=BORDER_SUNKEN,     // boundary type
                     const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // the angle of the graph for the binding
                     const color            clr=clrRed,               // the color of the flat border (Flat)
                     const ENUM_LINE_STYLE  style=STYLE_SOLID,        // flat border style
                     const int              line_width=1,             // the thickness of the flat boundary
                     const bool             back=false,               // in the background
                     const bool             selection=false,          // select for displacements
                     const bool             hidden=true,              // hidden in the list of objects
                     const long             z_order=0)                // priority for mouse click
{
   //--- reset error value
   ResetLastError();
   //--- create a rectangular label
   if (ObjectFind(name) == -1)
   ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0);

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);              // set the coordinates of the label
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);              // set the size of the label
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);         // set the background color
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);       // set the boundary type
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);            // set the angle of the graph, relative to which the coordinates of the point
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);                // set the color of the flat frame (in Flat mode)
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);              // set the line style of the flat frame
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_width);         // set the thickness of the flat border
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);                // display on the front (false) or back (true) plan
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);     // enable (true) or disable (false) the mouse move mode
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);            // hide (true) or display (false) the name of the graphic in the list of objects
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);           // set the priority to receive the mouse click event on the graph
   //--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Delete A Rectangular Label                                       |
//+------------------------------------------------------------------+
bool RectLabelDelete(const long   chart_ID=0,       // chart ID
                     const string name="RectLabel") // label name
{
   //--- reset error value
   ResetLastError();
   //--- delete a rectangular label
   if (ObjectFind(chart_ID,name) >= 0) 
      ObjectDelete(chart_ID,name);
   //--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Background Color                                                 |
//+------------------------------------------------------------------+
color ChartBackColorGet(const long chart_ID=0)
{
//--- prepare the variable to get the color
   long result=clrNONE;
//--- reset error value
   ResetLastError();
//--- get the background color of the chart
   if(!ChartGetInteger(chart_ID,CHART_COLOR_BACKGROUND,0,result))
     {
      //--- we will display an error message in the journal "Experts"
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- return the value of the chart property
   return((color)result);
}