//+------------------------------------------------------------------+
//|                                              ay-PriceScaleLR.mq4 |
//|                          CopyLeft © 2011, ahmad.yani@hotmail.com |
//|                                          http://eyesfx.web44.net |
//+------------------------------------------------------------------+
#property copyright "CopyLeft © 2011, ahmad.yani@hotmail.com"
#property link      "http://eyesfx.web44.net"

#property indicator_chart_window
#import "user32.dll"
   int GetDC(int hwnd);
   int ReleaseDC(int hwnd,int hdc);

#import "gdi32.dll"
   color GetPixel(int hdc,int x,int y);
#import


//--- input parameters
extern int       RowHeight  = 50;
extern int       AdjTextPos = 5;
extern int       TextClrIdx = 159;
extern int       LineClrIdx = 31;
extern int       LineWidth  = 1;
extern bool      ShowLeftPS = false;

//--- global vars
string gsPref = "price.";
int    giDigits;
double gdPoint, gdRowSize;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   if (Point == 0.001 || Point == 0.00001) 
   { gdPoint = Point*10; giDigits = Digits - 1; }
   else 
   { gdPoint = Point; giDigits = Digits; }  
   
   gdRowSize = (RowHeight/1.0)/(MathPow(10,giDigits));
   
   gsPref = StringConcatenate(gsPref, RowHeight, ".");
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   delObjs();
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int hwnd = WindowHandle(Symbol(),Period());
       
   int      iSpaceRS = WindowBarsPerChart() - WindowFirstVisibleBar();
   string   sPrice;
   datetime dt1, dt2;
   double   dPrice, rhh, rll;
   double   hh = High[iHighest(NULL, 0, MODE_HIGH)];
   double   ll = Low[iLowest(NULL, 0, MODE_LOW)];
   color    cChartBg, cTextColor, cLineColor;
   int      iclr, ired, igreen, iblue;
   static   int iLastSpaceRS;
   static   color cbg;   
                
   //--get window background color;   
   int hdc   = GetDC(hwnd);
   cChartBg  = GetPixel(hdc,1,2);
   ReleaseDC(hwnd,hdc);         

   //do nothing if chart is not visible
   if (cChartBg == -1) return(0);
   
   //do nothing if chart bg && hori scorll not changed
   if (cbg == cChartBg && iLastSpaceRS == iSpaceRS ) return(0);
   
   //--store data
   cbg = cChartBg;
   iLastSpaceRS = iSpaceRS;
   //--
   
   intToRGB        (cChartBg, ired, igreen, iblue);
   getNextGradColor(TextClrIdx, ired, igreen, iblue, true); 
   cTextColor = RGB(ired, igreen, iblue);  
   
   intToRGB        (cChartBg, ired, igreen, iblue);  
   getNextGradColor(LineClrIdx, ired, igreen, iblue, true); 
   cLineColor = RGB(ired, igreen, iblue);                  
   
   rhh = MathCeil (hh/gdRowSize) * gdRowSize;
   rll = MathFloor(ll/gdRowSize) * gdRowSize;    
      
   dt1 = Time[0]+(iSpaceRS-AdjTextPos)*Period()*60;
   if (iSpaceRS<0 ) dt1 = Time[(-iSpaceRS)+AdjTextPos];
   dt2 = Time[WindowFirstVisibleBar()-AdjTextPos];

   dPrice = rhh;
   while (dPrice>=rll)
   {
      sPrice = DoubleToStr(dPrice, giDigits);
      createText("r."+sPrice, dt1, dPrice, sPrice, 7, "tahoma", cTextColor); 

      if (ShowLeftPS) createText("l."+sPrice, dt2, dPrice, sPrice, 7, "tahoma", cTextColor); 
      
      if (cChartBg != cLineColor) 
         createTl("line."+sPrice, D'2037.12.31 23:59:59', dPrice, 1, dPrice, cLineColor, STYLE_SOLID, LineWidth, true, true);
      
      dPrice -= gdRowSize;
   }
   
   return(0);
}

//+------------------------------------------------------------------+
//| createText                                                       |
//+------------------------------------------------------------------+
void createText(string name, datetime t, double p, string text
   , int size=8, string font="Arial", color c=White, int winid=0)
{
   name = gsPref + name;
   
   if (ObjectFind(name) != winid)
      ObjectCreate (name,OBJ_TEXT,winid,0,0);
   
   ObjectSet    (name,OBJPROP_TIME1,  t);
   ObjectSet    (name,OBJPROP_PRICE1, p);
   ObjectSetText(name,text,size,font, c);

}
//+------------------------------------------------------------------+
//| createTl                                                         |
//+------------------------------------------------------------------+ 
void createTl(string tlname, datetime t1, double v1, 
     datetime t2, double v2, color tlColor, int style = STYLE_SOLID, 
     int width = 1, bool back=true, bool ray=false, string desc="", int winid=0)
{
   tlname = gsPref + tlname;
   if(ObjectFind(tlname) != winid)
   {
      ObjectCreate(
            tlname
            , OBJ_TREND
            , winid
            , t1
            , v1
            , t2
            , v2
            );      
   }else
   {
      ObjectMove(tlname, 0, t1, v1);   
      ObjectMove(tlname, 1, t2, v2);
   }
   ObjectSet(tlname, OBJPROP_COLOR, tlColor);
   ObjectSet(tlname, OBJPROP_RAY,   ray);
   ObjectSet(tlname, OBJPROP_STYLE, style);
   ObjectSet(tlname, OBJPROP_WIDTH, width);
   ObjectSet(tlname, OBJPROP_BACK,  back);
   ObjectSetText(tlname, desc);
} 

//+------------------------------------------------------------------+
//| convert color to red, green and blue values                      |
//+------------------------------------------------------------------+  
void intToRGB(int clr, int &ired, int &igreen, int &iblue )
{   
   ired   = (clr % 0x100); 
   igreen = (clr % 0x10000 / 0x100);     
   iblue  = (clr % 0x1000000 / 0x10000);            
}

void getNextGradColor(int iclr, int &ired, int &igreen, int &iblue, bool reset = false)
{
    
   static bool minred, mingreen, minblue;
  
   if (reset==true) { minred = false; mingreen=false; minblue = false; }
      
   if ( ired   + iclr > 255 )  minred   = true;
   if ( ired   - iclr < 0 )    minred   = false;         
   if ( igreen + iclr > 255 )  mingreen = true;
   if ( igreen - iclr < 0 )    mingreen = false;         
   if ( iblue  + iclr > 255 )  minblue  = true;
   if ( iblue  - iclr < 0 )    minblue  = false;
                                      
   if ( minred )   ired   -= iclr;  else ired   +=iclr;         
   if ( mingreen ) igreen -= iclr;  else igreen +=iclr;         
   if ( minblue )  iblue  -= iclr;  else iblue  +=iclr; 

}

// taken from stdlib.mp4 library  
//+------------------------------------------------------------------+
//| convert red, green and blue values to color                      |
//+------------------------------------------------------------------+
int RGB(int red_value,int green_value,int blue_value)
{
   //---- check parameters
   if(red_value<0)     red_value   = 0;
   if(red_value>255)   red_value   = 255;
   if(green_value<0)   green_value = 0;
   if(green_value>255) green_value = 255;
   if(blue_value<0)    blue_value  = 0;
   if(blue_value>255)  blue_value  = 255;
   //----
   green_value<<=8;
   blue_value<<=16;
   return(red_value+green_value+blue_value);
}

void delObjs(string s="")
{
   int objs = ObjectsTotal();
   if (StringLen(s) == 0) s = gsPref;
   
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringSubstr(name,0,StringLen(s)) == s)       
         ObjectDelete(name); 
   }   
}
//+------------------------------------------------------------------+