//+------------------------------------------------------------------+
//|                                                ChartTextDemo.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.10"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <ChartText\CanvasBlock.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input int  InpWidth    = 620;    // widest line of the wrapped blocks, pixels
input bool InpShots    = true;   // save PNG and BMP screenshots of every scene
input bool InpKeep     = false;  // keep the demo charts open
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
string g_status="Demo status line, not a real account: 3 open positions, floating result -41.20, "
                "margin level 487.35%, last signal long at 1.0920, next check at the 14:00 bar.";
string g_broken="Equity 10482.19, margin 487.35%\nSpread 0.4, positions 3";
//+------------------------------------------------------------------+
//| StyleChart                                                       |
//| Black on white and nothing else, so a capture shows the text.    |
//+------------------------------------------------------------------+
void StyleChart(const long chart)
  {
   ChartSetInteger(chart,CHART_COLOR_BACKGROUND,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_FOREGROUND,clrBlack);
   ChartSetInteger(chart,CHART_SHOW_GRID,false);
   ChartSetInteger(chart,CHART_MODE,CHART_LINE);
   ChartSetInteger(chart,CHART_COLOR_CHART_LINE,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_VOLUME,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_BID,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_ASK,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_LAST,clrWhite);
   ChartSetInteger(chart,CHART_COLOR_STOP_LEVEL,clrWhite);
   ChartSetInteger(chart,CHART_SHOW_VOLUMES,CHART_VOLUME_HIDE);
   ChartSetInteger(chart,CHART_SHOW_PERIOD_SEP,false);
   ChartSetInteger(chart,CHART_SHOW_PRICE_SCALE,false);
   ChartSetInteger(chart,CHART_SHOW_DATE_SCALE,false);
   ChartSetInteger(chart,CHART_SHOW_OHLC,false);
  }
//+------------------------------------------------------------------+
//| PutLabel                                                         |
//| One plain OBJ_LABEL, the way most panels create it.              |
//+------------------------------------------------------------------+
void PutLabel(const long chart,const string name,const int y,const string text,const string font="Arial",const int points=10)
  {
   ObjectCreate(chart,name,OBJ_LABEL,0,0,0);
   ObjectSetInteger(chart,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(chart,name,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(chart,name,OBJPROP_XDISTANCE,10);
   ObjectSetInteger(chart,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart,name,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(chart,name,OBJPROP_FONTSIZE,points);
   ObjectSetString(chart,name,OBJPROP_FONT,font);
   ObjectSetString(chart,name,OBJPROP_TEXT,text);
  }
//+------------------------------------------------------------------+
//| PutBox                                                           |
//| An OBJ_EDIT, OBJ_BUTTON or OBJ_RECTANGLE_LABEL 980 pixels wide   |
//| with a white background. The edit and the button get a white     |
//| border. The rectangle label gets a flat black border, because    |
//| its OBJPROP_COLOR sets the border, and a text, if it drew one,   |
//| would have to appear inside the border.                          |
//+------------------------------------------------------------------+
void PutBox(const long chart,const string name,const ENUM_OBJECT type,const int y,const int height,const string text)
  {
   ObjectCreate(chart,name,type,0,0,0);
   ObjectSetInteger(chart,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(chart,name,OBJPROP_XDISTANCE,10);
   ObjectSetInteger(chart,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart,name,OBJPROP_XSIZE,980);
   ObjectSetInteger(chart,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart,name,OBJPROP_BGCOLOR,clrWhite);
   ObjectSetInteger(chart,name,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetInteger(chart,name,OBJPROP_COLOR,clrBlack);
   if(type==OBJ_RECTANGLE_LABEL)
      ObjectSetInteger(chart,name,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(chart,name,OBJPROP_FONTSIZE,10);
   ObjectSetString(chart,name,OBJPROP_FONT,"Arial");
   if(type==OBJ_EDIT)
     {
      ObjectSetInteger(chart,name,OBJPROP_ALIGN,ALIGN_LEFT);
      ObjectSetInteger(chart,name,OBJPROP_READONLY,true);
     }
   ObjectSetString(chart,name,OBJPROP_TEXT,text);
  }
//+------------------------------------------------------------------+
//| Digits                                                           |
//| The digits 0 to 9 repeated.                                      |
//+------------------------------------------------------------------+
string Digits(const int len)
  {
   string s="";
   for(int i=0; i<len; i++)
      s+=StringFormat("%d",i%10);
   return(s);
  }
//+------------------------------------------------------------------+
//| LogType                                                          |
//| One journal line per object of scene 4: its kind, its band on    |
//| the screen, and the widths of the stored and of the sent text.   |
//+------------------------------------------------------------------+
void LogType(const string kind,const int y,const int height,const string sent,const string stored,const bool box=false)
  {
   CScreenMeter meter;
   PrintFormat("SHOT type scene=four kind=%s y=%d height=%d box=%s sent=%d stored=%d width_stored=%d width_sent=%d",kind,y,height,
               box ? "10-990" : "none",StringLen(sent),StringLen(stored),meter.Width(stored),meter.Width(sent));
  }
//+------------------------------------------------------------------+
//| Shot                                                             |
//| Saves the chart as PNG for the article and as BMP for the        |
//| Python check, at the size of the scene.                          |
//+------------------------------------------------------------------+
void Shot(const long chart,const string stem,const int width,const int height)
  {
   ChartRedraw(chart);
   Sleep(700);
   if(!InpShots)
      return;
   bool png=ChartScreenShot(chart,"ChartText\\"+stem+".png",width,height,ALIGN_LEFT);
   bool bmp=ChartScreenShot(chart,"ChartText\\"+stem+".bmp",width,height,ALIGN_LEFT);
   PrintFormat("SHOT %s png=%s bmp=%s size=%dx%d",stem,png ? "ok" : "failed",bmp ? "ok" : "failed",width,height);
  }
//+------------------------------------------------------------------+
//| LogLines                                                         |
//| One journal line per row: its position, the pixel width that     |
//| TextGetSize() gives, and its text.                               |
//+------------------------------------------------------------------+
void LogLines(const string scene,CLabelBlock &block,const int x,const int y0,const int step)
  {
   CScreenMeter meter;
   for(int i=0; i<block.LineCount(); i++)
      PrintFormat("SHOT row scene=%s y=%d x=%d width=%d height=%d units=%d font=Arial points=10 text=%s",scene,y0+i*step,x,meter.Width(block.Line(i)),
                  meter.LineHeight(),StringLen(block.Line(i)),block.Line(i));
  }
//+------------------------------------------------------------------+
//| OpenScene                                                        |
//+------------------------------------------------------------------+
long OpenScene(void)
  {
   long chart=ChartOpen(_Symbol,PERIOD_H1);
   Sleep(1000);
   StyleChart(chart);
   return(chart);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   PrintFormat("SHOT build=%d dpi=%d status_chars=%d",TerminalInfoInteger(TERMINAL_BUILD),TerminalInfoInteger(TERMINAL_SCREEN_DPI),StringLen(g_status));
//--- scene 1: one label that is cut, one label with a line break, and the wrapped block
   long chart=OpenScene();
   PutLabel(chart,"CTD_plain",40,g_status);
   PutLabel(chart,"CTD_break",80,g_broken);
   PrintFormat("SHOT plain stored=%d of %d | break stored=%d has_lf=%s",StringLen(ObjectGetString(chart,"CTD_plain",OBJPROP_TEXT)),StringLen(g_status),
               StringLen(ObjectGetString(chart,"CTD_break",OBJPROP_TEXT)),StringFind(ObjectGetString(chart,"CTD_break",OBJPROP_TEXT),"\n")>=0 ? "yes" : "no");
   CLabelBlock block;
   block.Init("CTD_block",10,120,InpWidth,"Arial",10,clrBlack,2,chart);
   int n=block.Set(g_status);
   PrintFormat("SHOT block lines=%d verify=%d calls=%d",n,block.Verify(),block.ObjectCalls());
   LogLines("one",block,10,120,block.Height()/MathMax(n,1));
   Shot(chart,"scene1_cut_break_wrapped",900,300);
   block.Clear();
   if(!InpKeep)
      ChartClose(chart);
//--- scene 2: the same text as labels and as a canvas, both 900 pixels wide
   chart=OpenScene();
   CLabelBlock labels;
   labels.Init("CTD_lab",10,40,900,"Arial",10,clrBlack,2,chart);
   int label_lines=labels.Set(g_status);
   CCanvasBlock canvas;
   canvas.Init("CTD_cv",10,40+labels.Height()+30,900,"Arial",10,clrBlack,clrWhite,4,2,chart);
   int canvas_lines=canvas.Set(g_status);
   PrintFormat("SHOT compare labels=%d canvas=%d label_verify=%d canvas_verify=%d",label_lines,canvas_lines,labels.Verify(),canvas.Verify());
   LogLines("two",labels,10,40,labels.Height()/MathMax(label_lines,1));
   Shot(chart,"scene2_labels_vs_canvas",940,300);
   labels.Clear();
   canvas.Clear();
   if(!InpKeep)
      ChartClose(chart);
//--- scene 3: label rows in four fonts and six patterns, for the screenshot check
   chart=OpenScene();
   string fonts[]={"Arial","Arial","Times New Roman","Consolas"};
   int points[]={10,14,10,10};
   string patterns[]={"Demo status: margin level 487.35%, spread 0.4, 3 positions","WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
                      "illicit little lilies fill","jump off","0123456789 0123456789 0123456789","AVAVAV To Ty LT Yo."};
   int y=10;
   for(int f=0; f<ArraySize(fonts); f++)
     {
      CScreenMeter meter;
      meter.Set(fonts[f],points[f]);
      int height=meter.LineHeight();
      for(int k=0; k<ArraySize(patterns); k++)
        {
         PutLabel(chart,StringFormat("CTD_font_%d_%d",f,k),y,patterns[k],fonts[f],points[f]);
         PrintFormat("SHOT row scene=three y=%d x=10 width=%d height=%d units=%d font=%s points=%d text=%s",y,meter.Width(patterns[k]),height,
                     StringLen(patterns[k]),fonts[f],points[f],patterns[k]);
         y+=height+8;
        }
     }
   Shot(chart,"scene3_fonts",1000,y+10);
   ObjectsDeleteAll(chart,"CTD_font_");
   if(!InpKeep)
      ChartClose(chart);
//--- scene 4: the boxes and labels of each text object type and a canvas line, 80 digits each
   chart=OpenScene();
   string sent=Digits(80);
   CScreenMeter arial;
   arial.Set("Arial",10);
   int h=arial.LineHeight();
   int step=h+30;
   PutLabel(chart,"CTD_type_label",20,sent);
   LogType("OBJ_LABEL",20,h,sent,ObjectGetString(chart,"CTD_type_label",OBJPROP_TEXT));
   PutBox(chart,"CTD_type_edit",OBJ_EDIT,20+step,h+8,sent);
   LogType("OBJ_EDIT",20+step,h+8,sent,ObjectGetString(chart,"CTD_type_edit",OBJPROP_TEXT),true);
   PutBox(chart,"CTD_type_button",OBJ_BUTTON,20+2*step,h+8,sent);
   LogType("OBJ_BUTTON",20+2*step,h+8,sent,ObjectGetString(chart,"CTD_type_button",OBJPROP_TEXT),true);
   PutBox(chart,"CTD_type_rect",OBJ_RECTANGLE_LABEL,20+3*step,h+8,sent);
   LogType("OBJ_RECTANGLE_LABEL",20+3*step,h+8,sent,ObjectGetString(chart,"CTD_type_rect",OBJPROP_TEXT),true);
   CCanvasBlock line;
   line.Init("CTD_type_canvas",10,20+4*step,980,"Arial",10,clrBlack,clrWhite,0,2,chart);
   int drawn=line.Set(sent);
   LogType("canvas",20+4*step,h,sent,sent);
   PrintFormat("SHOT type canvas lines=%d verify=%d",drawn,line.Verify());
   Shot(chart,"scene4_types",1000,20+5*step);
   line.Clear();
   ObjectsDeleteAll(chart,"CTD_type_");
   if(!InpKeep)
      ChartClose(chart);
//--- scene 5: OBJ_TEXT alone, placed by time and price, so the check can read the whole chart
   chart=OpenScene();
   ChartSetInteger(chart,CHART_AUTOSCROLL,true);
   ChartSetInteger(chart,CHART_SHIFT,false);
   ChartSetInteger(chart,CHART_SCALE,1);
   ChartRedraw(chart);
   Sleep(300);
   PrintFormat("SHOT scene=five chart height=%d price_max=%.5f price_min=%.5f",(int)ChartGetInteger(chart,CHART_HEIGHT_IN_PIXELS),
               ChartGetDouble(chart,CHART_PRICE_MAX),ChartGetDouble(chart,CHART_PRICE_MIN));
   double high[],low[];
   int copied=MathMin(CopyHigh(_Symbol,PERIOD_H1,0,600,high),CopyLow(_Symbol,PERIOD_H1,0,600,low));
   double mid=(copied>0) ? (high[ArrayMaximum(high)]+low[ArrayMinimum(low)])/2.0 : SymbolInfoDouble(_Symbol,SYMBOL_BID);
   datetime t=iTime(_Symbol,PERIOD_H1,450);
   ResetLastError();
   bool made=ObjectCreate(chart,"CTD_type_text",OBJ_TEXT,0,t,mid);
   int err=GetLastError();
   ObjectSetInteger(chart,"CTD_type_text",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(chart,"CTD_type_text",OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(chart,"CTD_type_text",OBJPROP_FONTSIZE,10);
   ObjectSetString(chart,"CTD_type_text",OBJPROP_FONT,"Arial");
   ObjectSetString(chart,"CTD_type_text",OBJPROP_TEXT,sent);
   PrintFormat("SHOT type text created=%s err=%d time=%s price=%.5f bars=%d",made ? "true" : "false",err,TimeToString(t),mid,copied);
   PrintFormat("SHOT type scene=five kind=OBJ_TEXT y=24 height=270 box=none sent=%d stored=%d width_stored=%d width_sent=%d",StringLen(sent),
               StringLen(ObjectGetString(chart,"CTD_type_text",OBJPROP_TEXT)),arial.Width(ObjectGetString(chart,"CTD_type_text",OBJPROP_TEXT)),
               arial.Width(sent));
   Shot(chart,"scene5_obj_text",1000,300);
   ObjectDelete(chart,"CTD_type_text");
   if(!InpKeep)
      ChartClose(chart);
  }
//+------------------------------------------------------------------+
