Libraries: Easy Canvas - page 4

 
Super. Convenient and easy to understand.
 

I suggest adding mouse button status. It won't affect speed, but it can be useful )


struct Window
  {
   long              ChartId;     // current window identifier
   uint              Color;       // window background colour
   int               Width;       // window width
   int               Height;      // window height
   int               height[];    // sub_windows height
   int               Left_bar;    // number of the leftmost bar in the window
   double            Right_bar;   // number of the rightmost bar in the window
   double            Total_bars;  // the maximum number of bars in the window
   int               BarsInWind;  // number of visible bars in the window
   double            Y_min;       // The minimum value of the price in the window
   double            Y_max;       // The maximum value of the price in the window
   double            dy_pix;      // price change for one pixel
   int               dx_pix;      // changing the number of bars per pixel
   int               MouseX;      // coordinate X of the current position of the mouse pointer
   int               MouseY;      // coordinate Y of the current position of the mouse pointer
   int               MouseStatus; // mouse buttons status
   double            MouseBar;    // the current bar position of the mouse pointer 
   double            MousePrice;  // the current price of the mouse pointer
   datetime          MouseTime;   // the current time of the mouse pointer
   int               MouseSubWin; // number of the subwindow in which the mouse pointer is located
   int               WindowsTotal;// total subwindows, including the main window
   int               SubWin;      // current subwindow
   datetime          time[];      // array of opening time of all visible bars in the window
  };


void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_MOUSE_MOVE)
     {
      W.MouseX=(int)lparam;
      W.MouseY=(int)dparam;
      W.MouseStatus=(int)sparam;
      W.MouseBar=(double)W.Left_bar+1-(double)W.MouseX/(double)W.dx_pix;
      W.MouseSubWin=XYToTimePrice(W.MouseX,W.MouseY,W.MouseTime,W.MousePrice,id);
      if(W.MouseSubWin>0) for(int i=0;i<W.MouseSubWin;i++) W.MouseY=W.MouseY-W.height[i]-2;
     }
   if(id==CHARTEVENT_CHART_CHANGE) ChartChanged();
   if(OnZ) SetOnChart(sizeArr);
   if(OnChart) MyChartEvent(id,lparam,dparam,sparam);
  }
 
Oleksii Chepurnyi:

I suggest adding mouse button status. It won't affect speed, but it can be useful )

I can't agree, as I don't see much sense in it.

The point is that the status of mouse buttons and keys is an event model, which should be processed only where the occurrence of this event is caught, namely in OnChartEvent.
You are not going to limit an infinite loop of polling the occurrence of some event outside the event handler. So what is the point of putting this status outside OnChartEvent, when all the processing of these clicks should be done in OnChartEvent, the status of which is in lparam and sparam.

Except for the purpose of introducing a new structure of mouse event identifiers to improve code readability.

 
Nikolai Semko:

I can't agree as I don't see much point in it.

The point is that the status of mouse buttons and keys is an event model, which should be handled only where the occurrence of this event is caught, namely in OnChartEvent.
You are not going to limit an infinite loop of polling the occurrence of some event outside the event handler. So what is the point of putting this status outside OnChartEvent, when all the processing of these clicks should be done in OnChartEvent, the status of which is in lparam and sparam.

Except for the purpose of introducing a new structure of mouse event identifiers to improve code readability.

And why store mouse coordinates then? ) Also event-driven )

I didn't understand about the loop...

In particular, I needed the status to avoid drawing a part of the object when dragging with the mouse.

 
Oleksii Chepurnyi:

Why store mouse coordinates then? ) Also event-driven )

Mouse coordinates are a different matter. Current mouse coordinates are not event information. The event is their change.

There may be times when mouse coordinates are needed outside OnChartEvent. For example, here.

Oleksii Chepurnyi:

I didn't understand about the loop...

In particular, the status was needed to avoid drawing a part of the object when dragging with the mouse.

Well, when dragging the mouse, the command to redraw part of the object is called from OnChartEvent at the moment when the mouse coordinate changes and while the mouse is pressed, and OnChartEvent has this status, why embed it in the Window structure?

If you would do the same thing not from OnChartEvent, and at the same time in the instance W of the Window structure there would be MouseStatus, you would have to organise an endless loop of polling of this parameter (MouseStatus) to catch the moment of releasing the mouse button, which would hang the CPU.

This loop is not necessary only in OnChartEvent.

I just want to say that you can and should use the button status only from OnChartEvent, where it is already present.

If I'm wrong, please give me a concrete example where this status would be required not from OnChartEvent.


 
Nikolai Semko:

If I'm wrong, please provide a concrete example where this status would be demanded from something other than OnChartEvent.

I wrote an example above, everything works fine )

In OnChartEvent when moving the mouse we change the parameters and give the command to redraw the canvas. Without a parameter in the structure we would have to pass sparam to an object, from there to another object, and there to the Draw() method.

 
Nikolai Semko:

The mouse coordinates are a different matter. Current mouse coordinates are not event information. The event is their change.

There may be times when mouse coordinates are needed outside OnChartEvent. For example, here.

Well, when you drag the mouse, the command to redraw a part of the object is called from OnChartEvent at the moment when the mouse coordinate changes and while the mouse button is pressed, and OnChartEvent has this status, why embed it in the Window structure?

If you would do the same thing not from OnChartEvent, and at the same time in the instance W of the Window structure there would be MouseStatus, you would have to organise an endless loop of polling of this parameter (MouseStatus) to catch the moment of releasing the mouse button, which would hang the CPU.

This loop is not needed only in OnChartEvent.

I just want to say that you can and should use the button status only from OnChartEvent, which already has it.

If I'm wrong, then give me a concrete example where this status would be required not from OnChartEvent.


Very strange reasoning. There is no logic at all.

Both mouse coordinates and pressed keys are external states. It is passed through parameters to the handler, where if it is put into the internal variables of the object (which is certainly useful), it should be there as a whole, not in parts. A library is by definition something that is made not for one's own needs, but in a generalised form, taking into account the potential needs of others. You can't know all the needs in advance, so you just don't make artificial restrictions like this.

 
Oleksii Chepurnyi:

I wrote an example above, everything works fine )

In OnChartEvent when moving the mouse we change the parameters and give the command to redraw the canvas. Without a parameter in the structure we would have to pass sparam to an object, from there to another object, and there to the Draw() method.

This is a classic of the genre - in all window libraries, the states of mouse buttons are passed in a single structure with the cursor coordinates.

because they all together describe one entity and cannot be separately at all.

 
Okay. You got it.
 

updated to version 1.29

new:

1. added the following defines for simplicity of code writing and improving readability:

#define _Comment Canvas.Comm
#define _X Canvas.X
#define _Y Canvas.Y
#define _TimePos Canvas.TimePos
#define _Bar Canvas.Bar
#define _Price Canvas.Price
#define _CommXY(x,y,str) Canvas.TextPosition(x,y);\
Canvas.Comm(str);
#define _Font Canvas.CurentFont
#define _PixelSet Canvas.PixelSet
#define _MouseX W.MouseX
#define _MouseY W.MouseY
#define _MouseBar W.MouseBar
#define _Width W.Width
#define _Height W.Height
#define _Left_bar W.Left_bar
#define _Right_bar W.Right_bar
#define _BarsInWind W.BarsInWind

2. added the MouseStatus parameter to the Window structure, which can take 4 values, as requested by the workers:

  • NO_PRESSED (0)
  • LEFT_BUTTON_PRESSED (1)
  • RIGHT_BUTTON_PRESSED (2)
  • LEFT_AND_RIGHT_BUTTONS_PRESSED (3)