//+------------------------------------------------------------------+ //| WndEvents.mqh | //| Copyright 2015, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #include "Defines.mqh" #include "WndContainer.mqh" #include //+------------------------------------------------------------------+ //| Class for event handling | //+------------------------------------------------------------------+ class CWndEvents : public CWndContainer { protected: CChart m_chart; //--- Identifier and window number of the chart long m_chart_id; int m_subwin; //--- Program name string m_program_name; //--- Short name of the indicator string m_indicator_shortname; //--- private: //--- Event parameters int m_id; long m_lparam; double m_dparam; string m_sparam; //--- protected: CWndEvents(void); ~CWndEvents(void); //--- Virtual event handler of the chart virtual void OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam) {} //--- Timer void OnTimerEvent(void); //--- public: //--- Event handlers of the chart void ChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam); private: void ChartEventCustom(void); void ChartEventClick(void); void ChartEventMouseMove(void); void ChartEventObjectClick(void); void ChartEventEndEdit(void); void ChartEventChartChange(void); //--- Checking events in controls void CheckElementsEvents(void); //--- Identifying the sub-window number void DetermineSubwindow(void); //--- Checking control events void CheckSubwindowNumber(void); //--- Initialization of event parameters void InitChartEventsParams(const int id,const long lparam,const double dparam,const string sparam); //--- Moving the window void MovingWindow(void); //--- Checking events of all controls by timer void CheckElementsEventsTimer(void); //--- protected: //--- Deleting the interface void Destroy(void); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CWndEvents::CWndEvents(void) : m_chart_id(0), m_subwin(0), m_indicator_shortname("") { //--- Enable the timer if(!::MQLInfoInteger(MQL_TESTER)) ::EventSetMillisecondTimer(TIMER_STEP_MSC); //--- Get the ID of the current chart m_chart.Attach(); //--- Enable tracking of mouse events m_chart.EventMouseMove(true); //--- Identifying the sub-window number DetermineSubwindow(); } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ CWndEvents::~CWndEvents(void) { //--- Delete the timer ::EventKillTimer(); //--- Enable management m_chart.MouseScroll(true); m_chart.SetInteger(CHART_DRAG_TRADE_LEVELS,true); //--- Disable tracking of mouse events m_chart.EventMouseMove(false); //--- Detach from the chart m_chart.Detach(); //--- Delete a comment ::Comment(""); } //+------------------------------------------------------------------+ //| Initialization of event variables | //+------------------------------------------------------------------+ void CWndEvents::InitChartEventsParams(const int id,const long lparam,const double dparam,const string sparam) { m_id =id; m_lparam =lparam; m_dparam =dparam; m_sparam =sparam; } //+------------------------------------------------------------------+ //| Program event handling | //+------------------------------------------------------------------+ void CWndEvents::ChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam) { //--- If the array is empty, leave if(CWndContainer::WindowsTotal()<1) return; //--- Initialization of event parameter fields InitChartEventsParams(id,lparam,dparam,sparam); //--- Verification of interface control events CheckElementsEvents(); //--- Event of mouse movement ChartEventMouseMove(); //--- Event of changing the chart properties ChartEventChartChange(); } //+------------------------------------------------------------------+ //| Verification of the control events | //+------------------------------------------------------------------+ void CWndEvents::CheckElementsEvents(void) { int elements_total=CWndContainer::ElementsTotal(0); for(int e=0; e Error when identifying the sub-window number: ",::GetLastError()); return; } //--- If this is not the main window of the chart if(m_subwin>0) { //--- Get the total number of indicators in the specified sub-window int total=::ChartIndicatorsTotal(m_chart_id,m_subwin); //--- Get the short name of the last indicator in the list string indicator_name=::ChartIndicatorName(m_chart_id,m_subwin,total-1); //--- If the sub-window already contains an indicator, remove the program from the chart if(total!=1) { ::Print(__FUNCTION__," > This sub-window already contains an indicator."); ::ChartIndicatorDelete(m_chart_id,m_subwin,indicator_name); return; } } } //+------------------------------------------------------------------+ //| Verifying and updating the number of the program window | //+------------------------------------------------------------------+ void CWndEvents::CheckSubwindowNumber(void) { //--- If the program in the sub-window and the numbers do not match if(m_subwin!=0 && m_subwin!=::ChartWindowFind()) { //--- Identify the sub-window number DetermineSubwindow(); //--- Store in all controls int windows_total=CWndContainer::WindowsTotal(); for(int w=0; w