//+------------------------------------------------------------------+
//|                                                  PanelDialog.mqh |
//|                   Copyright 2009-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
#include <Controls\Edit.mqh>
#include <Controls\ListView.mqh>
#include <Controls\ComboBox.mqh>
#include <Controls\SpinEdit.mqh>
#include <Controls\RadioGroup.mqh>
#include <Controls\CheckGroup.mqh>
#include <Controls\Edit.mqh>


//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
//--- indents and gaps
#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width)
#define INDENT_TOP                          (11)      // indent from top (with allowance for border width)
#define INDENT_RIGHT                        (11)      // indent from right (with allowance for border width)
#define INDENT_BOTTOM                       (11)      // indent from bottom (with allowance for border width)
#define CONTROLS_GAP_X                      (10)      // gap by X coordinate
#define CONTROLS_GAP_Y                      (10)      // gap by Y coordinate
//--- for buttons
#define BUTTON_WIDTH                        (100)     // size by X coordinate
#define BUTTON_HEIGHT                       (20)      // size by Y coordinate
//--- for the indication area
#define EDIT_HEIGHT                         (20)      // size by Y coordinate

extern double Lots =1;
extern double SlipPage=0;
extern string ____ ="____________";
extern double TrSt1 =15;
extern double TrSt2 =25;
extern double TrSt3 =35;
extern double TrSt4 =45;
extern double TrSt5 =55;
extern string _____="____________";
extern double SL1 =500;
extern double SL2 =1000;
extern double SL3 =1200;
extern double SL4 =1500;
extern double SL5 =2000;
extern string ______ ="___________";
extern double TP1 =500;
extern double TP2 =1000;
extern double TP3 =1200;
extern double TP4 =1500;
extern double TP5 =2000;
int magic=3247;

//+------------------------------------------------------------------+
//| Class CPanelDialog                                               |
//| Usage: main dialog of the SimplePanel application                |
//+------------------------------------------------------------------+
class CPanelDialog : public CAppDialog
  {
private:
   CEdit             m_edit;                          // the display field object
   CButton           m_button1;                       // the button object
   CButton           m_button2;                       // the button object
   CButton           m_button3;                       // the fixed button object
   CListView         m_list_view;                     // the list object
   CRadioGroup       m_radio_group;                   // the radio buttons group object
   CRadioGroup       m_radio_group1;                   // the radio buttons group object   
   CRadioGroup       m_radio_group2;                   // the radio buttons group object   
   CCheckGroup       m_check_group;                   // the check box group object

public:
                     CPanelDialog(void);
                    ~CPanelDialog(void);
   //--- create
   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
   //--- chart event handler
   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);

protected:
   //--- create dependent controls
   bool              CreateEdit(void);
   bool              CreateButton1(void);
   bool              CreateButton2(void);
   bool              CreateButton3(void);
   bool              CreateRadioGroup(void);
   bool              CreateRadioGroup1(void);
   bool              CreateRadioGroup2(void);
   //--- internal event handlers
   virtual bool      OnResize(void);
   //--- handlers of the dependent controls events
   void              OnClickButton1(void);
   void              OnClickButton2(void);
   void              OnClickButton3(void);
   void              OnChangeRadioGroup(void);
   void              OnChangeRadioGroup1(void);
   void              OnChangeRadioGroup2(void);
  };
//+------------------------------------------------------------------+
//| Event Handling                                                   |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CPanelDialog)
   ON_EVENT(ON_CLICK,m_button1,OnClickButton1)
   ON_EVENT(ON_CLICK,m_button2,OnClickButton2)
   ON_EVENT(ON_CLICK,m_button3,OnClickButton3)
   ON_EVENT(ON_CHANGE,m_radio_group,OnChangeRadioGroup)
   ON_EVENT(ON_CHANGE,m_radio_group1,OnChangeRadioGroup1)
   ON_EVENT(ON_CHANGE,m_radio_group2,OnChangeRadioGroup2)
EVENT_MAP_END(CAppDialog)
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CPanelDialog::CPanelDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CPanelDialog::~CPanelDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Create                                                           |
//+------------------------------------------------------------------+
bool CPanelDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
  {
   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
      return(false);
//--- create dependent controls
   if(!CreateEdit())
      return(false);
   if(!CreateButton1())
      return(false);
   if(!CreateButton2())
      return(false);
   if(!CreateButton3())
      return(false);
   if(!CreateRadioGroup())
      return(false);
   if(!CreateRadioGroup1())
      return(false);
   if(!CreateRadioGroup2())
      return(false);
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the display field                                         |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateEdit(void)
  {
//--- coordinates
   int x1=INDENT_LEFT;
   int y1=INDENT_TOP;
   int x2=ClientAreaWidth()-(INDENT_RIGHT+BUTTON_WIDTH+CONTROLS_GAP_X);
   int y2=y1+EDIT_HEIGHT;
//--- create
   if(!m_edit.Create(m_chart_id,m_name+"Edit",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!m_edit.ReadOnly(true))
      return(false);
   if(!Add(m_edit))
      return(false);
   m_edit.Alignment(WND_ALIGN_WIDTH,INDENT_LEFT,0,INDENT_RIGHT+BUTTON_WIDTH+CONTROLS_GAP_X,0);
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the "Button1" button                                      |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateButton1(void)
  {
//--- coordinates
   int x1=ClientAreaWidth()-(INDENT_RIGHT+BUTTON_WIDTH);
   int y1=INDENT_TOP;
   int x2=x1+BUTTON_WIDTH;
   int y2=y1+BUTTON_HEIGHT;
//--- create
   if(!m_button1.Create(m_chart_id,m_name+"BUY",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!m_button1.Text("BUY"))
      return(false);
   if(!Add(m_button1))
      return(false);
   m_button1.Alignment(WND_ALIGN_RIGHT,0,0,INDENT_RIGHT,0);
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the "Button2" button                                      |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateButton2(void)
  {
//--- coordinates
   int x1=ClientAreaWidth()-(INDENT_RIGHT+BUTTON_WIDTH);
   int y1=INDENT_TOP+BUTTON_HEIGHT+CONTROLS_GAP_Y;
   int x2=x1+BUTTON_WIDTH;
   int y2=y1+BUTTON_HEIGHT;
//--- create
   if(!m_button2.Create(m_chart_id,m_name+"SELL",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!m_button2.Text("SELL"))
      return(false);
   if(!Add(m_button2))
      return(false);
   m_button2.Alignment(WND_ALIGN_RIGHT,0,0,INDENT_RIGHT,0);
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the "Button3"                                |
//+------------------------------------------------------------------+

bool CPanelDialog::CreateButton3(void)
  {
//--- coordinates
   int x1=ClientAreaWidth()-(INDENT_RIGHT+BUTTON_WIDTH);
   int y1=ClientAreaHeight()-(INDENT_BOTTOM+BUTTON_HEIGHT);
   int x2=x1+BUTTON_WIDTH;
   int y2=y1+BUTTON_HEIGHT;
//--- create
   if(!m_button3.Create(m_chart_id,m_name+"Close ALL!",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!m_button3.Text("Close ALL!"))
      return(false);
   if(!Add(m_button3))
      return(false);
  // m_button3.Locking(true);
   m_button3.Alignment(WND_ALIGN_RIGHT|WND_ALIGN_BOTTOM,0,0,INDENT_RIGHT,INDENT_BOTTOM);
//--- succeed
   return(true);
  } 
  
//+------------------------------------------------------------------+
//| Create the "RadioGroup" element                                  |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateRadioGroup(void)
  {
   int sx=(ClientAreaWidth()-(INDENT_LEFT+INDENT_RIGHT+BUTTON_WIDTH))/3-CONTROLS_GAP_X;
//--- coordinates
   int x1=INDENT_LEFT;
   int y1=INDENT_TOP+EDIT_HEIGHT+CONTROLS_GAP_Y;
   int x2=x1+sx;
   int y2=ClientAreaHeight()-INDENT_BOTTOM;
//--- create
   if(!m_radio_group.Create(m_chart_id,m_name+"RadioGroup",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!Add(m_radio_group))
      return(false);
   m_radio_group.Alignment(WND_ALIGN_HEIGHT,0,y1,0,INDENT_BOTTOM);
//--- fill out with strings
!m_radio_group.AddItem("TrSt:"+TrSt1,TrSt1);
!m_radio_group.AddItem("TrSt:"+TrSt2,TrSt2);
!m_radio_group.AddItem("TrSt:"+TrSt3,TrSt3);
!m_radio_group.AddItem("TrSt:"+TrSt4,TrSt4);
!m_radio_group.AddItem("TrSt:"+TrSt5,TrSt5);
   return(true);
  } 
//+------------------------------------------------------------------+
//| Create the "RadioGroup1" element                                  |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateRadioGroup1(void)
  {  
  int sx=(ClientAreaWidth()-(INDENT_LEFT+INDENT_RIGHT+BUTTON_WIDTH))/3-CONTROLS_GAP_X;
//--- coordinates
   int x1=INDENT_LEFT+sx+CONTROLS_GAP_X;
   int y1=INDENT_TOP+EDIT_HEIGHT+CONTROLS_GAP_Y;
   int x2=x1+sx;
   int y2=ClientAreaHeight()-INDENT_BOTTOM;  
   
//--- create
   if(!m_radio_group1.Create(m_chart_id,m_name+"RadioGroup1",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!Add(m_radio_group1))
      return(false);
   m_radio_group1.Alignment(WND_ALIGN_HEIGHT,0,y1,0,INDENT_BOTTOM);
//--- fill out with strings
!m_radio_group1.AddItem("TP:"+TP1,TP1);
!m_radio_group1.AddItem("TP:"+TP2,TP2);
!m_radio_group1.AddItem("TP:"+TP3,TP3);
!m_radio_group1.AddItem("TP:"+TP4,TP4);
!m_radio_group1.AddItem("TP:"+TP5,TP5);
   return(true);
  } 
//+------------------------------------------------------------------+
//| Create the "RadioGroup2" element                                  |
//+------------------------------------------------------------------+
bool CPanelDialog::CreateRadioGroup2(void)
  {
int sx=(ClientAreaWidth()-(INDENT_LEFT+INDENT_RIGHT+BUTTON_WIDTH))/3-CONTROLS_GAP_X;
//--- coordinates
   int x1=ClientAreaWidth()-(sx+INDENT_RIGHT+BUTTON_WIDTH+CONTROLS_GAP_X);
   int y1=INDENT_TOP+EDIT_HEIGHT+CONTROLS_GAP_Y;
   int x2=x1+sx;
   int y2=ClientAreaHeight()-INDENT_BOTTOM;

//--- create
   if(!m_radio_group2.Create(m_chart_id,m_name+"RadioGroup2",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!Add(m_radio_group2))
      return(false);
   m_radio_group2.Alignment(WND_ALIGN_HEIGHT,0,y1,0,INDENT_BOTTOM);
//--- fill out with strings

!m_radio_group2.AddItem("SL:"+SL1,SL1);
!m_radio_group2.AddItem("SL:"+SL2,SL2);
!m_radio_group2.AddItem("SL:"+SL3,SL3);
!m_radio_group2.AddItem("SL:"+SL4,SL4);
!m_radio_group2.AddItem("SL:"+SL5,SL5);
   return(true);
   }  
//+------------------------------------------------------------------+
//| Handler of resizing                                              |
//+------------------------------------------------------------------+
bool CPanelDialog::OnResize(void)
  {
//--- call method of parent class
   if(!CAppDialog::OnResize()) return(false);
//--- coordinates
   int x=ClientAreaLeft()+INDENT_LEFT;
   int y=m_radio_group.Top();
   int sx=(ClientAreaWidth()-(INDENT_LEFT+INDENT_RIGHT+BUTTON_WIDTH))/3-CONTROLS_GAP_X;
//--- move and resize the "RadioGroup" element
   m_radio_group.Move(x,y);
   m_radio_group.Width(sx);
   
   //--- move and resize the "RadioGroup1" element
    x=ClientAreaLeft()+INDENT_LEFT+sx+CONTROLS_GAP_X;
   m_check_group.Move(x,y);
   m_check_group.Width(sx);
   
   
   //--- move and resize the "RadioGroup2" element
      x=ClientAreaLeft()+ClientAreaWidth()-(sx+INDENT_RIGHT+BUTTON_WIDTH+CONTROLS_GAP_X);
   m_list_view.Move(x,y);
   m_list_view.Width(sx);
   return(true);
  }
//+------------------------------------------------------------------+
//| Event handler     OnClickButton1                                               |
//+------------------------------------------------------------------+//////////////////////////////////////////////////////////////////////////
void CPanelDialog::OnClickButton1(void)
  {
 PlaySound("ok.wav");
 }
//+------------------------------------------------------------------+///////////////////////////////////////////////////////////////////////////////
//| Event handler               OnClickButton2                                     |
//+------------------------------------------------------------------+
void CPanelDialog::OnClickButton2(void)
  {
    PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//| Event handler               OnClickButton3                                     |
//+------------------------------------------------------------------+
void CPanelDialog::OnClickButton3(void)
  { 
  PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//| Event handler                                                    |
//+------------------------------------------------------------------+
void CPanelDialog::OnChangeRadioGroup(void)
  {
  m_edit.Text(m_radio_group.Value());
  }
//+------------------------------------------------------------------+
//| Event handler                                                    |
//+------------------------------------------------------------------+
void CPanelDialog::OnChangeRadioGroup1(void)
  {
  m_edit.Text(m_radio_group1.Value());
  }
//+------------------------------------------------------------------+
//| Event handler                                                    |
//+------------------------------------------------------------------+
void CPanelDialog::OnChangeRadioGroup2(void)
  {
  m_edit.Text(m_radio_group2.Value());
  }
//////////////////////////////////////////////////////////////////////
void CloseAll()
  {
   double total;
   int cnt;
   while(OrdersTotal()>0)
   {
      total = OrdersTotal();
      for (cnt = total-1; cnt >=0 ; cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
         {
            switch(OrderType())
            {
               case OP_BUY       :
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);break;
                   
               case OP_SELL      :
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet); break;
            }             
         }
      }
      // and close pending
      total = OrdersTotal();      
      for (cnt = total-1; cnt >=0 ; cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
         {
            switch(OrderType())
            {
               case OP_BUYLIMIT  :OrderDelete(OrderTicket()); break;
               case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
               case OP_BUYSTOP   :OrderDelete(OrderTicket()); break;
               case OP_SELLSTOP  :OrderDelete(OrderTicket()); break;
            }
         }
      }
   }
}

void Sell()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  int ticket,iSlipPage;
  double dStopLoss, dTakeProfit,dLots,iSLPips,iTPPips;
  string sText,sArrow;  
iSLPips=m_radio_group2.Value();////Stop Loss
iTPPips=m_radio_group1.Value(); //Take Profit
 // dLots=1;           //Amount of Lots
  //iSlipPage=100;
  sText="My Order";  //Order Text
  sArrow=CLR_NONE;   //Order Arrow Color
  
  dStopLoss=Ask+NormalizeDouble(iSLPips*Point,MarketInfo(Symbol(),MODE_DIGITS));
  dTakeProfit=Bid - NormalizeDouble(iTPPips*Point,MarketInfo(Symbol(),MODE_DIGITS));
  ticket=OrderSend(Symbol(),OP_SELL,1, Bid,0,dStopLoss,dTakeProfit,sText,000,0,sArrow);
  
if(ticket<0) Print("OrderSend failed with error #",GetLastError());
}
void Buy()
 { 
 double MyPoint=Point;
 if(Digits==3 || Digits==5) MyPoint=Point*10;
 int ticket,iSlipPage;
 double dStopLoss, dTakeProfit,dLots,iSLPips,iTPPips;
 string sText,sArrow;
iSLPips=m_radio_group2.Value(); //Stop Loss
iTPPips=m_radio_group1.Value();//Take Profit
  sText="My Order";  //Order Text
  sArrow=CLR_NONE;   //Order Arrow Color
  
  dStopLoss=Bid-NormalizeDouble(iSLPips*Point,MarketInfo(Symbol(),MODE_DIGITS));
  dTakeProfit=Ask + NormalizeDouble(iTPPips*Point,MarketInfo(Symbol(),MODE_DIGITS));  
  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,SlipPage,dStopLoss,dTakeProfit,sText,000,0,sArrow);
  
  if(ticket<0) Print("OrderSend failed with error #",GetLastError()); 
  }    

