//__________________________________________________________
// This program is PUBLIC DOMAIN
// This program has NO Copyright
// The writer		:  Sergiy Podolyak
//	Write date		:  2010-2016
// https://ua.linkedin.com/in/sergiy-podolyak-a34079110
// http://www.graalino.com
// File        	:  OBz-Visual-Lib.mq4
// Description		:	Library of trading functions from the
//							trend-following experts "Ocean Breeze"
//							and "Graalino-Pro"
// Version     	:  1.92
// Version Date	:  21 Aug 2016
// Project     	:  MT4 trading
// Start Date  	:  May 2010
//_______________________________________________________________________________
//
#property library
// But You can just include any this function into your project, not as a library as a whole
//______________________________________________________________________________________
//..................../ Choose only ONE: "Lite" == "MQL4-only" \....................
//#define PRO_VERSION
#define TESTING_VERSION
//#define LITE_VERSION
//..................................................................
//..........................................................................
#import "user32.dll"
int GetWindowRect (int hwnd, int & rect[]);
#import
//_____________________________________________________________________________________
//																	|
// 		Post_Text_Block	()								|
//_________________________________________________|
void Post_Text_Block
(
   string & Text_Array[][],
   color Text_Color_Param
)
{
	int Win;
	int Row, Column;
	int Array_Height;
	int Array_Width;
	int Font_Size;
	//....................................................................
#ifndef LITE_VERSION
	double Font_Size_Min = 6;
	double Font_Size_Max = 22;
	double Win_Size_Y_Min = 150;
	double Win_Size_Y_Max = 1200;
	double Font_Mult;
	double Font_Size_Scaled;
	double Window_Size_Y;
	double Win_Heights_Ratio;
	double Font_Sizes_Ratio;
	double Size_Factor;
	double Grow_Factor;
	int LTRB[4];
	int Rect_Handle;
	//....................................................................
	Rect_Handle = GetWindowRect (WindowHandle (Symbol (), Period () ), LTRB);
	if (Rect_Handle != 0)
	{
		//	Window_Size_X = LTRB[2] - LTRB[0];
		Window_Size_Y = LTRB[3] - LTRB[1];
	}
	else
	{
		//	Window_Size_X = 400.0;
		Window_Size_Y = 300.0;
	}
	//....................................................................
	Win_Heights_Ratio = Win_Size_Y_Max / Win_Size_Y_Min;
	Font_Sizes_Ratio = Font_Size_Max / Font_Size_Min;
	//....................................................................
	Size_Factor = MathMax (Window_Size_Y / Win_Size_Y_Min, 1.0);
	Grow_Factor = MathSqrt ( ( MathAbs (Win_Size_Y_Max - Window_Size_Y + Win_Size_Y_Min)
	                           / Win_Size_Y_Max ));
	Font_Mult = Size_Factor * Grow_Factor * (Font_Sizes_Ratio / Win_Heights_Ratio);
	Font_Size_Scaled = Font_Size_Min * Font_Mult;
	Font_Size_Scaled = MathMax (Font_Size_Scaled, Font_Size_Min);
	Font_Size = (int) MathMin (Font_Size_Scaled, Font_Size_Max);
#else
	Font_Size = 8;
#endif
	//..........................................................................
	Win = 0;
	Array_Height = ArrayRange (Text_Array, 0);
	Array_Width = ArrayRange (Text_Array, 1);
	for (Row = 0; Row < Array_Height; Row++)
	{
		for (Column = 0; Column < Array_Width; Column++)
		{
			Create_Visual_From_Text_Block (Text_Array, Row, Column, Win, Font_Size, Text_Color_Param);
		}
	}
	WindowRedraw ();
	//..........................................................................
	return;
}
//_____________________________________________________________________________________
//																	|
// 		Create_Visual_From_Text_Block	()				|
//_________________________________________________|
void Create_Visual_From_Text_Block (	string & Text_Array[][],
                                       int Row,
                                       int Column ,
                                       int Window_Number,
                                       int Font_Size,
                                       color Text_Color_Param)
{
	int 		Y_Shift = Font_Size;
	int 		X_Shift = Font_Size / 2;
	string 	Object_Name_Mask = "Text";
	string 	Object_Name = Object_Name_Mask + "_" + (string) Row + "_" + (string) Column;
	int 		Obj_Win;
	int		Max_Len;
	int 		Col_Distance;
	//..........................................................................
	if (Column >= 1)
	{
		Max_Len = Find_Max_Len_In_Text_Block (Text_Array, Column - 1);
		Col_Distance = ( (Max_Len + 3) * Font_Size * 7) / 12 ;
	}
	else
	{
		Col_Distance = 0;
	}
	//.........................................................
	Obj_Win = ObjectFind (Object_Name);
	if (Obj_Win == -1) // no Object found
	{
		// create Label
		ObjectCreate (Object_Name, OBJ_LABEL, Window_Number, 0, 0, 0);
		// tie coord to left upper corner
		ObjectSet (Object_Name, OBJPROP_CORNER, 0);
		// horis step in pixels
		ObjectSet (Object_Name, OBJPROP_XDISTANCE, X_Shift + Column * Col_Distance);
		// vert step in pixels
		ObjectSet (Object_Name, OBJPROP_YDISTANCE, Y_Shift + Row * 2 * Font_Size);
		ObjectSetText (Object_Name, Text_Array[Row][Column], Font_Size, "Arial", Text_Color_Param) ;
	}
	else  // object found, look for the Win number
	{
		if (Obj_Win == Window_Number) return;
		else // wrong window for object, delete it
		{
			ObjectDelete (Object_Name);
		}
	}
	//..........................................................................
	return;
}
//_____________________________________________________________________________________
//																	|
// 		Find_Max_Len_In_Text_Block ()					|
//_________________________________________________|
int Find_Max_Len_In_Text_Block (	string & Text_Array[][],
                                 int Column )
{
	int Max_Len = 0; // StringLen (Text_Array[0][Column]);
	int Len;
	int Row;
	int Height;
	Height = ArrayRange (Text_Array, 0);
	for (Row = 0; Row < Height; Row++)
	{
		Len = StringLen (Text_Array[Row][Column]);
		if (Len > Max_Len)
		{
			Max_Len = Len;
		}
	}
	return (Max_Len);
}
//_____________________________________________________________________________________
//																	|
// 		Delete_All_Text_Objects	()						|
//_________________________________________________|
int Delete_All_Text_Objects (int Win_Number_In)
{
	int Objs_To_Delete = 0;
	int Obj_Total;
	int Deleted_Total;
	int Object_Type;
	//..............................
	Obj_Total = ObjectsTotal (EMPTY) ;
	Object_Type = OBJ_LABEL;
	Deleted_Total = ObjectsDeleteAll (Win_Number_In, Object_Type);
	if (Deleted_Total > 0)
	{
		// Print ("Deleted ", Deleted_Total, " visual objects");
		Objs_To_Delete = Obj_Total - Deleted_Total;
	}
	return (Objs_To_Delete);
}
//______________________________________________________________________________________
//																	|
// 		Append_Text_Block	()						 		|
//_________________________________________________|
static int Curr_Text_Block_Row = -1;
int Append_Text_Block
(	string & Text_Block_In[][],
   int Column,
   string Value
)
{
	if (Curr_Text_Block_Row < 0)
	{
		Curr_Text_Block_Row = 0;
	}
	else
	{
		if ( Column == 0)
		{
			Curr_Text_Block_Row++;
		}
	}
	//...........................................
	Text_Block_In[Curr_Text_Block_Row][Column] = Value;
	//...........................................
	return (Curr_Text_Block_Row);
}
//______________________________________________________________________________________
//																	|
// 		Clean_Text_Block	()						 		|
//_________________________________________________|
int Clean_Text_Block (string & Text_Block_In[][])
{
	int Row, Column;
	int Height, Width;
	Height = ArrayRange (Text_Block_In, 0);
	Width = ArrayRange (Text_Block_In, 1);
	for (Row = 0; Row < Height; Row++)
	{
		for (Column = 0; Column < Width; Column++)
		{
			Text_Block_In[Row][Column] = "";
		}
	}
	Curr_Text_Block_Row = -1;
	return (Curr_Text_Block_Row);
}
//_____________________________________________________________________________________
//																	|
// 	Get_Window_Size	()									|
//_________________________________________________|
int Get_Window_Size	(	string Bond_Name,
                        int Time_Frame)
{
	int Siz;
	double Window_Size_X;
	double Window_Size_Y;
	//..........................................................................
	Window_Size_X = 400.0;
	Window_Size_Y = 300.0;
#ifndef LITE_VERSION
	int LTRB[4];
	int Rect_Handle = GetWindowRect (WindowHandle (Bond_Name, Time_Frame), LTRB);
	if (Rect_Handle != 0)
	{
		Window_Size_X = LTRB[2] - LTRB[0];
		Window_Size_Y = LTRB[3] - LTRB[1];
		//Alert ("Left=", LTRB[0], " Top=", LTRB[1], " Right=" + LTRB[2], " Bottom=", LTRB[3]);
		//Alert ("Y=", Window_Size_Y, " X=", Window_Size_X);
	}
#endif
	Siz = (int)MathMin (MathAbs (Window_Size_X), MathAbs (Window_Size_Y) );
	return (Siz);
}
//\_______________________/ END OF THIS FILE \____________________________________/
