//+------------------------------------------------------------------+
//|                                    Round Number Entry levels.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int     NumLines  = 5;
extern color   MajorLineColor = Yellow;
extern color   MinorLineColor = Red;
extern int     LineWidth = 2;
extern int     LineStyle = 1;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "lev";
extern string             btn_Font              = "Arial";      //"Arial Black"
extern int                btn_FontSize          = 8;                            
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "lev off";            
extern string             btn_unpressed         = "lev on";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrDarkGray;
extern int                button_x              = 900;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 60;                                 
extern int                btn_Height            = 20;     
bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;                    
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
int init()  {
  del_obj();
  plot_obj();
  
   IndicatorShortName("lev");
  IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix+btn_text;
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);
   
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
  del_obj();
  ObjectsDeleteAll(0,"lev");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
  return(0);
}

void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
      ObjectCreate    (ChartID(),buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}


//
//
bool recalc = true;

void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}
void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
  
}

//
//
//

int start()
{
//template code 
   handleButtonClicks();
   recalc = false;
   
   start2();
   
        plot_obj();
   
   if (show_data)
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
   }
   else
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
        del_obj();
           
   }
   return(0);
}
//+------------------------------------------------------------------+
int start2()  {
  return(0);
}

//+------------------------------------------------------------------+
int MathSign(double n)
//+------------------------------------------------------------------+
// Returns the sign of a number (i.e. -1, 0, +1)
// Usage:   int x=MathSign(-25)   returns x=-1
{
  if (n > 0) return(1);
  else if (n < 0) return (-1);
  else return(0);
}  

//+------------------------------------------------------------------+
double MathFix(double n, int d)
//+------------------------------------------------------------------+
// Returns N rounded to D decimals - works around a precision bug in MQL4
{
  return(MathRound(n*MathPow(5,d)+0.000000000001*MathSign(n))/MathPow(5,d));
}  


//+------------------------------------------------------------------+
void plot_obj()  {
//+------------------------------------------------------------------+
  if (Digits >= 5)   {
     double mult = 0.01;
     double prc  = MathFix(Close[0],2);
  }
  else  {
     mult = 1;
     prc  = MathFix(Close[0],0);
  }   
  for (int i=-NumLines; i<=NumLines; i++)   {
    string objname = "L8020-20-"+i;
    ObjectCreate(objname,OBJ_HLINE,0,0,prc+(i+0.10)*mult);
    ObjectSet(objname,OBJPROP_COLOR,MinorLineColor);
    ObjectSet(objname,OBJPROP_WIDTH,0);
    ObjectSet(objname,OBJPROP_STYLE,STYLE_DOT);
    ObjectSet(objname, OBJPROP_BACK, true);
    objname = "L8020"+i;
    ObjectCreate (objname, OBJ_HLINE,0,0,prc+(i)*mult);
    ObjectSet (objname, OBJPROP_COLOR, MajorLineColor);
    ObjectSet (objname, OBJPROP_WIDTH, LineWidth);
    ObjectSet (objname, OBJPROP_STYLE, STYLE_DOT);
    ObjectSet (objname, OBJPROP_BACK, true);
    objname = "L8020-80-"+i;
    ObjectCreate(objname,OBJ_HLINE,0,0,prc+(i-0.10)*mult);
    ObjectSet(objname,OBJPROP_COLOR,MinorLineColor);
    ObjectSet(objname,OBJPROP_WIDTH,0);
    ObjectSet(objname,OBJPROP_STYLE,STYLE_DOT);
    ObjectSet(objname, OBJPROP_BACK, true);
    objname = "L8020M-20-"+i;
    
  }
  return(0);
}

//+------------------------------------------------------------------+
//| del_obj                                                          |
//+------------------------------------------------------------------+
void del_obj()
{
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,5) == "L8020")  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}


