EasyAndFastMod/Controls/ColorButton.mqh

237 lines
19 KiB
MQL5
Raw Permalink Normal View History

2026-01-15 07:23:17 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ColorButton.mqh |
//| Copyright 2015, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#include "..\Element.mqh"
#include "Button.mqh"
//+------------------------------------------------------------------+
//| ;0AA 4;O A>740=8O :=>?:8 2K7>20 F25B>2>9 ?0;8B@K |
//+------------------------------------------------------------------+
class CColorButton : public CElement
{
private:
//--- -:75<?;O@K 4;O A>740=8O M;5<5=B0
CButton m_button;
//--- K1@0==K9 F25B
color m_current_color;
//--- <O @5AC@A0 4;O 87>1@065=8O <0@:5@0 F25B0 =0 :=>?:5
string m_resource_name;
//---
public:
CColorButton(void);
~CColorButton(void);
//--- 5B>4K 4;O A>740=8O M;5<5=B0
bool CreateColorButton(const string text,const int x_gap,const int y_gap);
//---
private:
void InitializeProperties(const string text,const int x_gap,const int y_gap);
bool CreateCanvas(void);
bool CreateButton(void);
//---
public:
//--- >72@0I05B C:070B5;8 =0 :=>?:C
CButton *GetButtonPointer(void) { return(::GetPointer(m_button)); }
color CurrentColor(void) const { return(m_current_color); }
void CurrentColor(const color clr);
//---
public:
//--- 1@01>BG8: A>1KB89 3@0D8:0
virtual void OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
//--- 8AC5B M;5<5=B
virtual void Draw(void);
//---
private:
//--- 8AC5B :0@B8=:C
virtual void DrawImage(void);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CColorButton::CColorButton(void) : m_current_color(C'35,205,255'),
m_resource_name("color_marker.bmp")
{
//--- !>E@0=8< 8<O :;0AA0 M;5<5=B0 2 107>2>< :;0AA5
CElementBase::ClassName(CLASS_NAME);
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CColorButton::~CColorButton(void)
{
}
//+------------------------------------------------------------------+
//| 1@01>BG8: A>1KB89 |
//+------------------------------------------------------------------+
void CColorButton::OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
//--- 1@01>B:0 A>1KB8O ?5@5<5I5=8O :C@A>@0
if(id==CHARTEVENT_MOUSE_MOVE)
{
//--- 5@5@8A>20BL M;5<5=B
if(CheckCrossingBorder())
Update(true);
//---
return;
}
}
//+------------------------------------------------------------------+
//| !>740QB >1J5:B =>?:0 |
//+------------------------------------------------------------------+
bool CColorButton::CreateColorButton(const string text,const int x_gap,const int y_gap)
{
//--- K9B8, 5A;8 =5B C:070B5;O =0 3;02=K9 M;5<5=B
if(!CElement::CheckMainPointer())
return(false);
//--- =8F80;870F8O A2>9AB2
InitializeProperties(text,x_gap,y_gap);
//--- !>740=85 M;5<5=B0
if(!CreateCanvas())
return(false);
if(!CreateButton())
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| =8F80;870F8O A2>9AB2 |
//+------------------------------------------------------------------+
void CColorButton::InitializeProperties(const string text,const int x_gap,const int y_gap)
{
m_x =CElement::CalculateX(x_gap);
m_y =CElement::CalculateY(y_gap);
m_x_size =(m_x_size<1)? 100 : m_x_size;
m_y_size =(m_y_size<1)? 20 : m_y_size;
m_label_text =text;
m_back_color =(m_back_color!=clrNONE)? m_back_color : m_main.BackColor();
//--- BABC?K 8 F25B B5:AB>2>9 <5B:8
m_label_x_gap =(m_label_x_gap!=WRONG_VALUE)? m_label_x_gap : 0;
m_label_y_gap =(m_label_y_gap!=WRONG_VALUE)? m_label_y_gap : 4;
m_label_color =(m_label_color!=clrNONE)? m_label_color : clrBlack;
m_label_color_hover =(m_label_color_hover!=clrNONE)? m_label_color_hover : C'0,120,215';
m_label_color_locked =(m_label_color_locked!=clrNONE)? m_label_color_locked : clrSilver;
//--- BABC?K >B :@09=59 B>G:8
CElementBase::XGap(x_gap);
CElementBase::YGap(y_gap);
}
//+------------------------------------------------------------------+
//| !>740QB >1J5:B 4;O @8A>20=8O |
//+------------------------------------------------------------------+
bool CColorButton::CreateCanvas(void)
{
//--- $>@<8@>20=85 8<5=8 >1J5:B0
string name=CElementBase::ElementName("color_button");
//--- !>740=85 >1J5:B0
if(!CElement::CreateCanvas(name,m_x,m_y,m_x_size,m_y_size))
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| !>740QB :=>?:C |
//+------------------------------------------------------------------+
bool CColorButton::CreateButton(void)
{
//--- !>E@0=8< C:070B5;L =0 3;02=K9 M;5<5=B
m_button.MainPointer(this);
//--- 07<5@K
int x_size=(m_button.XSize()<1)? 80 : m_button.XSize();
//--- >>@48=0BK
int x =(m_button.XGap()<1)? x_size : m_button.XGap();
int y =0;
//--- BABC?K 4;O :0@B8=:8
int icon_x_gap =(m_button.IconXGap()<1)? 4 : m_button.IconXGap();
int icon_y_gap =(m_button.IconYGap()<1)? 3 : m_button.IconYGap();
//--- BABC?K 4;O B5:AB0
int label_x_gap =(m_button.LabelXGap()<1)? 24 : m_button.LabelXGap();
int label_y_gap =(m_button.LabelYGap()<1)? 4 : m_button.LabelYGap();
//--- !2>9AB20
m_button.XSize(x_size);
m_button.YSize(m_y_size);
m_button.IconXGap(icon_x_gap);
m_button.IconYGap(icon_y_gap);
m_button.LabelXGap(label_x_gap);
m_button.LabelYGap(label_y_gap);
//--- #AB0=>2:0 F25B0 4;O :=>?:8
CurrentColor(m_current_color);
m_button.IconFile(m_resource_name);
m_button.IconFileLocked(m_resource_name);
//--- !>74048< M;5<5=B C?@02;5=8O
if(!m_button.CreateButton(::ColorToString(m_current_color),x,y))
return(false);
//--- >1028BL M;5<5=B 2 <0AA82
CElement::AddToArray(m_button);
return(true);
}
//+------------------------------------------------------------------+
//| 7<5=O5B B5:CI89 F25B ?0@0<5B@0 |
//+------------------------------------------------------------------+
void CColorButton::CurrentColor(const color clr)
{
//--- !>E@0=8< F25B
m_current_color=clr;
//--- 07<5@ 87>1@065=8O 8 <0AA820 (image_size x image_size)
uint image_size=14;
uint image_data[196];
//--- =8F80;870F8O <0AA820 F25B><
for(uint y=0,i=0; y<image_size; y++)
{
for(uint x=0; x<image_size; x++,i++)
{
if(y<1 || y==image_size-1 || x<1 || x==image_size-1)
image_data[i]=::ColorToARGB(C'160,160,160');
else
image_data[i]=::ColorToARGB(m_current_color);
}
}
//--- !>740=85 @5AC@A0 2 EX5-?@8;>65=88
::ResetLastError();
if(!::ResourceCreate(m_resource_name,image_data,image_size,image_size,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE))
::Print(__FUNCTION__," > error: ",::GetLastError());
//---
if(m_button.IconXGap()<1)
{
m_button.IconXGap(4);
m_button.IconYGap(3);
}
//--- #AB0=>2:0 ?CB8 : 87>1@065=8N 2 A2>9AB20E :=>?:8
m_button.IconFile(m_resource_name);
m_button.IconFileLocked(m_resource_name);
//--- #AB0=>2:0 B5:AB0 4;O :=>?:8
m_button.LabelText(::ColorToString(m_current_color));
}
//+------------------------------------------------------------------+
//| 8AC5B M;5<5=B |
//+------------------------------------------------------------------+
void CColorButton::Draw(void)
{
//--- 0@8A>20BL D>=
CElement::DrawBackground();
//--- 0@8A>20BL :0@B8=:C
CColorButton::DrawImage();
//--- 0@8A>20BL B5:AB
CElement::DrawText();
}
//+------------------------------------------------------------------+
//| 8AC5B :0@B8=:C |
//+------------------------------------------------------------------+
void CColorButton::DrawImage(void)
{
//--- K9B8, 5A;8 (1) G5:-1>:A =5 =C65= 8;8 (2) :0@B8=:0 =5 >?@545;5=0
if(CElement::IconFile()=="")
return;
//--- ?@545;8< 8=45:A
uint image_index=(m_is_pressed)? 2 : 0;
//--- A;8 M;5<5=B =5 701;>:8@>20=
if(!CElementBase::IsLocked())
{
if(CElementBase::MouseFocus())
image_index=(m_is_pressed)? 2 : 0;
}
else
image_index=(m_is_pressed)? 3 : 1;
//--- 8AC5< :0@B8=:C
CElement::DrawImage();
}
//+------------------------------------------------------------------+