Adwizard/Utils/Dialog.mqh
885 lines
71 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>
Yuriy Bykov
2026-02-07 00:00:15 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
#include <Controls/Edit.mqh>
Yuriy Bykov
2025-11-27 21:44:42 +03:00
#include <Controls/BmpButton.mqh>
Yuriy Bykov
2026-02-07 00:00:15 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
#include <Charts/Chart.mqh>
Yuriy Bykov
2026-02-07 00:00:15 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| Resources |
//+------------------------------------------------------------------+
#resource "\\Include\\Controls\\res\\Close.bmp"
#define CAPTION_TEXT_COLOR C'0x28,0x4b,0x63'
#define CAPTION_COLOR C'0xe2,0xec,0xf3'
#define CAPTION_BORDER C'0x9a,0xb9,0xd1'
//+------------------------------------------------------------------+
//| Class CDialog |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//| Usage: base class to create dialog boxes |
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| 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
string Caption(void) const {
return(m_caption.Text());
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool Caption(const string text) {
return(m_caption.Text(text));
}
//--- 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
virtual bool Save(const int file_handle);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
virtual bool Load(const int file_handle);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
protected:
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- 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);
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));
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int ClientAreaLeft(void) const {
return(m_client_area.Left());
}
int ClientAreaTop(void) const {
return(m_client_area.Top());
}
int ClientAreaRight(void) const {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(m_client_area.Right());
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
int ClientAreaBottom(void) const {
return(m_client_area.Bottom());
}
int ClientAreaWidth(void) const {
return(m_client_area.Width());
}
int ClientAreaHeight(void) const {
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(m_client_area.Height());
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//--- handlers of drag
virtual bool OnDialogDragStart(void);
virtual bool OnDialogDragProcess(void);
virtual bool OnDialogDragEnd(void);
Yuriy Bykov
2026-02-07 00:00:15 +03:00
};
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
//| Common handler of events |
//+------------------------------------------------------------------+
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)
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//ON_EVENT_PTR(ON_DRAG_END, m_drag_object, OnDialogDragEnd)
EVENT_MAP_END(CWndContainer)
//+------------------------------------------------------------------+
//| Constructor |
Yuriy Bykov
2026-02-07 00:00:15 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
CDialog::CDialog(void) : m_panel_flag(false),
m_minimized(false) {
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CDialog::~CDialog(void) {
}
//+------------------------------------------------------------------+
//| Create a control |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
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);
Yuriy Bykov
2026-02-07 00:00:15 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- create dependent controls
if(!CreateBackground())
return(false);
if(!CreateCaption())
return(false);
if(!CreateButtonClose())
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(false);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
if(!CreateClientArea())
return(false);
//--- set up additional areas
m_norm_rect.SetBound(m_rect);
Yuriy Bykov
2026-02-07 00:00:15 +03:00
//--- succeed
return(true);
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) |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
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);
FileWriteInteger(file_handle, m_min_rect.top);
FileWriteInteger(file_handle, m_minimized);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- result
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(CWndContainer::Save(file_handle));
}
//+------------------------------------------------------------------+
//| Load |
//+------------------------------------------------------------------+
bool CDialog::Load(const int file_handle) {
if(file_handle == INVALID_HANDLE)
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
//--- load
if(!FileIsEnding(file_handle)) {
FileReadStruct(file_handle, m_norm_rect);
int left = FileReadInteger(file_handle);
int top = FileReadInteger(file_handle);
m_min_rect.Move(left, top);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
m_minimized = FileReadInteger(file_handle);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- result
return(CWndContainer::Load(file_handle));
}
//+------------------------------------------------------------------+
//| Create background |
//+------------------------------------------------------------------+
bool CDialog::CreateBackground(void) {
int off = 0;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- coordinates
Yuriy Bykov
2025-11-27 18:45:58 +03:00
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);
if(!m_background.ColorBorder(CAPTION_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) {
int off = 0;
//--- coordinates
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;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
int y2 = y1 + CONTROLS_DIALOG_CAPTION_HEIGHT + 5;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- create
Yuriy Bykov
2025-12-16 15:36:46 +03:00
if(!m_caption.Create(m_chart_id, m_name + "Caption", m_subwin, x1, y1, x2, y2))
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
if(!m_caption.Color(CAPTION_TEXT_COLOR))
return(false);
if(!m_caption.ColorBackground(CAPTION_COLOR))
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.ColorBorder(CAPTION_BORDER))
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.Font("Segoe UI"))
// return(false);
if(!m_caption.FontSize(10))
return(false);
if(!m_caption.ReadOnly(true))
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.Text(m_name))
return(false);
if(!CWndContainer::Add(m_caption))
return(false);
m_caption.Alignment(WND_ALIGN_WIDTH, off, 0, 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 the "Close" button |
//+------------------------------------------------------------------+
bool CDialog::CreateButtonClose(void) {
int off = 2;
//--- coordinates
int x1 = Width() - off - (CONTROLS_BUTTON_SIZE + CONTROLS_DIALOG_BUTTON_OFF);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int y1 = off + CONTROLS_DIALOG_BUTTON_OFF;
int x2 = x1 + CONTROLS_BUTTON_SIZE;
int y2 = y1 + CONTROLS_BUTTON_SIZE;
//--- create
if(!m_button_close.Create(m_chart_id, m_name + "Close", m_subwin, x1, y1, x2, y2))
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
return(false);
if(!m_button_close.BmpNames("::Include/Controls/res/Close.bmp"))
return(false);
if(!CWndContainer::Add(m_button_close))
Yuriy Bykov
2025-12-16 15:36:46 +03:00
return(false);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
m_button_close.Alignment(WND_ALIGN_RIGHT, 0, 0, off + CONTROLS_DIALOG_BUTTON_OFF, 0);
//--- change caption
//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 |
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
bool CDialog::CreateClientArea(void) {
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int off = 0;
//--- coordinates
int x1 = off;
int y1 = off + CONTROLS_DIALOG_CAPTION_HEIGHT + 3;
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'))
Yuriy Bykov
2025-12-16 15:36:46 +03:00
// return(false);
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);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
m_client_area.Alignment(WND_ALIGN_CLIENT, x1, y1, x1, x1);
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
//| Handler of click on the window title |
//+------------------------------------------------------------------+
void CDialog::OnClickCaption(void) {
}
//+------------------------------------------------------------------+
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//| Handler of click on the "Close" button |
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
void CDialog::OnClickButtonClose(void) {
Visible(false);
}
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//| Start dragging the dialog box |
//+------------------------------------------------------------------+
bool CDialog::OnDialogDragStart(void) {
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
return(false);
Yuriy Bykov
2025-11-27 18:45:58 +03:00
}
//--- calculate coordinates
Yuriy Bykov
2025-12-16 15:36:46 +03:00
int x1 = Left() - CONTROLS_DRAG_SPACING;
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;
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//--- create
Yuriy Bykov
2025-11-27 18:45:58 +03:00
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
Move(x, y);
//--- 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
//+------------------------------------------------------------------+
Yuriy Bykov
2025-11-27 18:45:58 +03:00
bool CDialog::OnDialogDragEnd(void) {
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
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//if(m_minimized)
Yuriy Bykov
2025-11-27 18:45:58 +03:00
// m_min_rect.SetBound(m_rect);
//else
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 |
Yuriy Bykov
2025-12-16 15:36:46 +03:00
//| Usage: main dialog box of MQL5 application |
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//+------------------------------------------------------------------+
class CAppDialog : public CDialog {
private:
//--- dependent controls
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
CBmpButton m_button_minmax; // the "Minimize/Maximize" button object
//--- variables
string m_program_name; // name of program
string m_instance_id; // unique string ID
ENUM_PROGRAM_TYPE m_program_type; // type of program
Yuriy Bykov
2025-12-16 15:36:46 +03:00
string m_indicator_name;
Yuriy Bykov
2025-11-27 18:45:58 +03:00
int m_deinit_reason;
//--- for mouse
int m_subwin_Yoff; // subwindow Y offset
CWnd* m_focused_wnd; // pointer to object that has mouse focus
CWnd* m_top_wnd; // pointer to object that has priority over mouse events handling
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
protected:
CChart m_chart; // object to access chart
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
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);
//--- dialog run
bool Run(void);
//--- chart events processing
void ChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
//--- set up
void Minimized(const bool flag) {
m_minimized = flag;
}
//--- to save/restore state
void IniFileSave(void);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
void IniFileLoad(void);
virtual string IniFileName(void) const;
virtual string IniFileExt(void) const {
return(".dat");
}
virtual bool Load(const int file_handle);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
virtual bool Save(const int file_handle);
private:
bool CreateCommon(const long chart, const string name, const int subwin);
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);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
Yuriy Bykov
2025-11-27 18:45:58 +03:00
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);
string CreateInstanceId(void);
string ProgramName(void) const {
return(m_program_name);
Yuriy Bykov
2025-12-16 15:36:46 +03:00
}
Yuriy Bykov
2025-11-27 18:45:58 +03:00
void SubwinOff(void);
};
//+------------------------------------------------------------------+
//| Common handler of events |
//+------------------------------------------------------------------+