Adwizard/Utils/Dialog.mqh
892 lines
72 KiB
MQL5
Yuriy Bykov
2025-11-27 18:45:58 +03:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Dialog.mqh |
//| Copyright 2000-2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Controls/WndContainer.mqh>
#include <Controls/WndClient.mqh>
#include <Controls/Panel.mqh>
#include <Controls/Edit.mqh>
#include <Controls/BmpButton.mqh>
#include <Charts/Chart.mqh>
Yuriy Bykov
2025-11-27 21:44:42 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| Resources |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
#resource "/Include/Controls/res/Close.bmp"
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
#resource "/Include/Controls/res/Restore.bmp"
#resource "/Include/Controls/res/Turn.bmp"
//#define CAPTION_TEXT_COLOR clrWhite
//#define CAPTION_COLOR C'0,120,215'
// #define CAPTION_TEXT_COLOR C'0x28,0x29,0x3B'
// #define CAPTION_COLOR C'225,225,225'
#define CAPTION_TEXT_COLOR C'0x28,0x4b,0x63'
Yuriy Bykov
2025-12-16 15:36:46 +03:00
#define CAPTION_COLOR C'0xe2,0xec,0xf3'
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
//| Class CDialog |
//| Usage: base class to create dialog boxes |
//| and indicator panels |
//+------------------------------------------------------------------+
class CDialog : public CWndContainer {
private:
//--- dependent controls
//CPanel m_white_border; // the "white border" object
CPanel m_background; // the background object
CEdit m_caption; // the window title object
CBmpButton m_button_close; // the "Close" button object
CWndClient m_client_area; // the client area object
protected:
//--- flags
bool m_panel_flag; // the "panel in a separate window" flag
//--- flags
bool m_minimized; // "create in minimized state" flag
//--- additional areas
CRect m_min_rect; // minimal area coordinates
CRect m_norm_rect; // normal area coordinates
public:
CDialog(void);
~CDialog(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);
//--- set up
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
string Caption(void) const {
return(m_caption.Text());
}
bool Caption(const string text) {
return(m_caption.Text(text));
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//--- fill
bool Add(CWnd *control);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool Add(CWnd &control);
//--- methods for working with files
Yuriy Bykov
2025-12-16 15:36:46 +03:00
virtual bool Save(const int file_handle);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
virtual bool Load(const int file_handle);
protected:
//--- create dependent controls
virtual bool CreateBackground(void);
virtual bool CreateCaption(void);
virtual bool CreateButtonClose(void);
virtual bool CreateClientArea(void);
//--- handlers of the dependent controls events
virtual void OnClickCaption(void);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
virtual void OnClickButtonClose(void);
//--- access properties of caption
void CaptionAlignment(const int flags, const int left, const int top, const int right, const int bottom) {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
m_caption.Alignment(flags, left, top, right, bottom);
}
//--- access properties of client area
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool ClientAreaVisible(const bool visible) {
return(m_client_area.Visible(visible));
}
int ClientAreaLeft(void) const {
return(m_client_area.Left());
}
int ClientAreaTop(void) const {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(m_client_area.Top());
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
int ClientAreaRight(void) const {
return(m_client_area.Right());
}
int ClientAreaBottom(void) const {
return(m_client_area.Bottom());
}
int ClientAreaWidth(void) const {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(m_client_area.Width());
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
int ClientAreaHeight(void) const {
return(m_client_area.Height());
}
//--- handlers of drag
virtual bool OnDialogDragStart(void);
virtual bool OnDialogDragProcess(void);
virtual bool OnDialogDragEnd(void);
};
//+------------------------------------------------------------------+
//| Common handler of events |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
EVENT_MAP_BEGIN(CDialog)
ON_EVENT(ON_CLICK, m_button_close, OnClickButtonClose)
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//ON_EVENT(ON_CLICK, m_caption, OnClickCaption)
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//ON_EVENT(ON_DRAG_START, m_caption, OnDialogDragStart)
//ON_EVENT_PTR(ON_DRAG_PROCESS, m_drag_object, OnDialogDragProcess)
//ON_EVENT_PTR(ON_DRAG_END, m_drag_object, OnDialogDragEnd)
Yuriy Bykov
2025-12-16 15:36:46 +03:00
EVENT_MAP_END(CWndContainer)
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CDialog::CDialog(void) : m_panel_flag(false),
m_minimized(false) {
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CDialog::~CDialog(void) {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
}
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
//| Create a control |
//+------------------------------------------------------------------+
bool CDialog::Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2) {
//--- call method of parent class
if(!CWndContainer::Create(chart, name, subwin, x1, y1, x2, y2))
return(false);
//--- create dependent controls
if(!CreateBackground())
return(false);
if(!CreateCaption())
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
if(!CreateButtonClose())
return(false);
if(!CreateClientArea())
return(false);
//--- set up additional areas
m_norm_rect.SetBound(m_rect);
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| Add control to the client area (by pointer) |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CDialog::Add(CWnd *control) {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(m_client_area.Add(control));
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| Add control to the client area (by reference) |
//+------------------------------------------------------------------+
bool CDialog::Add(CWnd &control) {
return(m_client_area.Add(control));
}
//+------------------------------------------------------------------+
//| Save |
//+------------------------------------------------------------------+
bool CDialog::Save(const int file_handle) {
//--- check
if(file_handle == INVALID_HANDLE)
return(false);
//--- save
FileWriteStruct(file_handle, m_norm_rect);
FileWriteInteger(file_handle, m_min_rect.left);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
FileWriteInteger(file_handle, m_min_rect.top);
FileWriteInteger(file_handle, m_minimized);
//--- result
return(CWndContainer::Save(file_handle));
}
//+------------------------------------------------------------------+
//| Load |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool CDialog::Load(const int file_handle) {
if(file_handle == INVALID_HANDLE)
return(false);
//--- load
if(!FileIsEnding(file_handle)) {
FileReadStruct(file_handle, m_norm_rect);
int left = FileReadInteger(file_handle);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int top = FileReadInteger(file_handle);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
m_min_rect.Move(left, top);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
m_minimized = FileReadInteger(file_handle);
}
//--- result
return(CWndContainer::Load(file_handle));
}
//+------------------------------------------------------------------+
//| Create background |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CDialog::CreateBackground(void) {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int off = 0;
//--- coordinates
int x1 = off;
int y1 = off;
int x2 = Width() - off;
int y2 = Height() - off;
//--- create
if(!m_background.Create(m_chart_id, m_name + "Back", m_subwin, x1, y1, x2, y2))
return(false);
if(!m_background.ColorBackground(CAPTION_COLOR))
return(false);
color border = CAPTION_COLOR;
if(!m_background.ColorBorder(border))
return(false);
if(!CWndContainer::Add(m_background))
return(false);
m_background.Alignment(WND_ALIGN_CLIENT, off, off, off, off);
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Create window title |
//+------------------------------------------------------------------+
bool CDialog::CreateCaption(void) {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
int off = 0;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- coordinates
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int x1 = off;
int y1 = off;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int x2 = Width() - off;
int y2 = y1 + CONTROLS_DIALOG_CAPTION_HEIGHT + 5;
//--- create
if(!m_caption.Create(m_chart_id, m_name + "Caption", m_subwin, x1, y1, x2, y2))
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(false);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(!m_caption.Color(CAPTION_TEXT_COLOR))
return(false);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(!m_caption.ColorBackground(CAPTION_COLOR))
return(false);
if(!m_caption.ColorBorder(C'0x6a,0x89,0xa1'))
return(false);
//if(!m_caption.Font("Segoe UI"))
// return(false);
if(!m_caption.FontSize(10))
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
if(!m_caption.ReadOnly(true))
return(false);
if(!m_caption.Text(m_name))
return(false);
if(!CWndContainer::Add(m_caption))
return(false);
m_caption.Alignment(WND_ALIGN_WIDTH, off, 0, off, 0);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Create the "Close" button |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CDialog::CreateButtonClose(void) {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int off = 2;
//--- coordinates
int x1 = Width() - off - (CONTROLS_BUTTON_SIZE + CONTROLS_DIALOG_BUTTON_OFF);
int y1 = off + CONTROLS_DIALOG_BUTTON_OFF;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
int x2 = x1 + CONTROLS_BUTTON_SIZE;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int y2 = y1 + CONTROLS_BUTTON_SIZE;
//--- create
if(!m_button_close.Create(m_chart_id, m_name + "Close", m_subwin, x1, y1, x2, y2))
return(false);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
if(!m_button_close.BmpNames("/Include/Controls/res/Close.bmp"))
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
if(!CWndContainer::Add(m_button_close))
return(false);
m_button_close.Alignment(WND_ALIGN_RIGHT, 0, 0, off + CONTROLS_DIALOG_BUTTON_OFF, 0);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- change caption
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//CaptionAlignment(WND_ALIGN_WIDTH, off, 0, off + (CONTROLS_BUTTON_SIZE + CONTROLS_DIALOG_BUTTON_OFF), 0);
//--- succeed
return(true);
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
//| Create client area |
//+------------------------------------------------------------------+
bool CDialog::CreateClientArea(void) {
int off = 0;
//--- coordinates
int x1 = off;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
int y1 = off + CONTROLS_DIALOG_CAPTION_HEIGHT + 3;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int x2 = Width() - (off);
int y2 = Height() - (off);
//--- create
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(!m_client_area.Create(m_chart_id, m_name + "Client", m_subwin, x1, y1, x2, y2))
return(false);
if(!m_client_area.ColorBackground(C'0xFA,0xFA,0xFA'))
return(false);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(!m_client_area.ColorBorder(C'0xF7,0xF7,0xF7'))
return(false);
CWndContainer::Add(m_client_area);
m_client_area.Alignment(WND_ALIGN_CLIENT, x1, y1, x1, x1);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- succeed
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(true);
}
//+------------------------------------------------------------------+
//| Handler of click on the window title |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
void CDialog::OnClickCaption(void) {
}
//+------------------------------------------------------------------+
//| Handler of click on the "Close" button |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
void CDialog::OnClickButtonClose(void) {
Visible(false);
}
//+------------------------------------------------------------------+
//| Start dragging the dialog box |
//+------------------------------------------------------------------+
bool CDialog::OnDialogDragStart(void) {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(m_drag_object == NULL) {
m_drag_object = new CDragWnd;
if(m_drag_object == NULL)
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
}
//--- calculate coordinates
int x1 = Left() - CONTROLS_DRAG_SPACING;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int y1 = Top() - CONTROLS_DRAG_SPACING;
int x2 = Right() + CONTROLS_DRAG_SPACING;
int y2 = Bottom() + CONTROLS_DRAG_SPACING;
//--- create
m_drag_object.Create(m_chart_id, "", m_subwin, x1, y1, x2, y2);
m_drag_object.PropFlags(WND_PROP_FLAG_CAN_DRAG);
//--- constraints
CChart chart;
chart.Attach(m_chart_id);
m_drag_object.Limits(-CONTROLS_DRAG_SPACING, -CONTROLS_DRAG_SPACING,
chart.WidthInPixels() + CONTROLS_DRAG_SPACING,
chart.HeightInPixels(m_subwin) + CONTROLS_DRAG_SPACING);
chart.Detach();
//--- set mouse params
m_drag_object.MouseX(m_caption.MouseX());
m_drag_object.MouseY(m_caption.MouseY());
m_drag_object.MouseFlags(m_caption.MouseFlags());
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Continue dragging the dialog box |
//+------------------------------------------------------------------+
bool CDialog::OnDialogDragProcess(void) {
//--- checking
if(m_drag_object == NULL)
return(false);
//--- calculate coordinates
int x = m_drag_object.Left() + 50;
int y = m_drag_object.Top() + 50;
//--- move dialog
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Move(x, y);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- succeed
return(true);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//+------------------------------------------------------------------+
//| End dragging the dialog box |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CDialog::OnDialogDragEnd(void) {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(m_drag_object != NULL) {
m_caption.MouseFlags(m_drag_object.MouseFlags());
delete m_drag_object;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
m_drag_object = NULL;
}
//--- set up additional areas
//if(m_minimized)
// m_min_rect.SetBound(m_rect);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//else
Yuriy Bykov
2025-11-27 18:45:58 +03:00
m_norm_rect.SetBound(m_rect);
//--- succeed
return(true);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//+------------------------------------------------------------------+
//| Class CAppDialog |
//| Usage: main dialog box of MQL5 application |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
class CAppDialog : public CDialog {
private:
//--- dependent controls
CBmpButton m_button_minmax; // the "Minimize/Maximize" button object
//--- variables
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
string m_program_name; // name of program
string m_instance_id; // unique string ID
ENUM_PROGRAM_TYPE m_program_type; // type of program
string m_indicator_name;
int m_deinit_reason;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- for mouse
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int m_subwin_Yoff; // subwindow Y offset
CWnd* m_focused_wnd; // pointer to object that has mouse focus
Yuriy Bykov
2025-12-16 15:36:46 +03:00
CWnd* m_top_wnd; // pointer to object that has priority over mouse events handling
Yuriy Bykov
2025-11-27 18:45:58 +03:00
protected:
CChart m_chart; // object to access chart
public:
CAppDialog(void);
~CAppDialog(void);
//--- main application dialog creation and destroy
virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2);
virtual void Destroy(const int reason = REASON_PROGRAM);
//--- chart event handler
virtual bool OnEvent(const int id, const long &lparam, const double &dparam, const string &sparam);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- dialog run
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool Run(void);
//--- chart events processing
void ChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam);
//--- set up
void Minimized(const bool flag) {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
m_minimized = flag;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//--- to save/restore state
void IniFileSave(void);
void IniFileLoad(void);
virtual string IniFileName(void) const;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
virtual string IniFileExt(void) const {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(".dat");
}
virtual bool Load(const int file_handle);
virtual bool Save(const int file_handle);
private:
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CreateCommon(const long chart, const string name, const int subwin);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool CreateExpert(const int x1, const int y1, const int x2, const int y2);
bool CreateIndicator(const int x1, const int y1, const int x2, const int y2);
protected:
//--- create dependent controls
virtual bool CreateButtonMinMax(void);
//--- handlers of the dependent controls events
virtual void OnClickButtonClose(void);
virtual void OnClickButtonMinMax(void);
//--- external event handlers
virtual void OnAnotherApplicationClose(const long &lparam, const double &dparam, const string &sparam);
//--- methods
virtual bool Rebound(const CRect &rect);
virtual void Minimize(void);
virtual void Maximize(void);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
string CreateInstanceId(void);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
string ProgramName(void) const {
return(m_program_name);
}
void SubwinOff(void);