1
/////////////////////////////////////////////////////////////////////////////
2
// Name: LinearRegulator.cpp
3
// Purpose: wxIndustrialControls Library
4
// Author: Marco Cavallini <m.cavallini AT koansoftware.com>
6
// Copyright: (C)2004-2006 Copyright by Koan s.a.s. - www.koansoftware.com
7
// Licence: KWIC License http://www.koansoftware.com/kwic/kwic-license.htm
8
/////////////////////////////////////////////////////////////////////////////
10
// Cleaned up and modified by Gary Harris for wxSmithKWIC, 2010.
12
// Fixed bugs that caused the font selection only to be used when m_bShowLimits was true.
13
/////////////////////////////////////////////////////////////////////////////
20
#include "wx/KWIC/LinearRegulator.h"
24
IMPLEMENT_DYNAMIC_CLASS(kwxLinearRegulator, wxControl)
26
BEGIN_EVENT_TABLE(kwxLinearRegulator,wxControl)
27
EVT_MOUSE_EVENTS(kwxLinearRegulator::OnMouse)
28
EVT_PAINT(kwxLinearRegulator::OnPaint)
31
kwxLinearRegulator::kwxLinearRegulator(wxWindow* parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, const long int style)
33
Create(parent, id, pos, size, style);
36
bool kwxLinearRegulator::Create(wxWindow* parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, const long int style)
38
if(!wxWindow::Create(parent, id, pos, size, style))
43
m_ControlSize = size ;
53
m_bDirOrizFlag = true ;
54
m_bShowCurrent = true;
57
m_cActiveBar = *wxGREEN;
58
m_cPassiveBar = *wxWHITE;
59
m_cBorderColour = *wxRED ;
60
m_cTagsColour = *wxBLACK;
61
m_cValueColour = *wxRED;
62
m_cLimitColour = *wxBLACK;
65
m_bShowCurrent = true;
67
membitmap = new wxBitmap(size.GetWidth(), size.GetHeight());
71
kwxLinearRegulator::~kwxLinearRegulator()
77
void kwxLinearRegulator::OnPaint(wxPaintEvent &WXUNUSED(event))
79
wxPaintDC old_dc(this);
82
int yPoint, rectHeight ;
87
dc.SelectObject(*membitmap);
90
dc.SetBackground(*wxTheBrushList->FindOrCreateBrush(m_cPassiveBar,wxSOLID));
91
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cPassiveBar,wxSOLID));
93
///////////////////////////////////////////////////////////////////////////
95
dc.SetPen(*wxThePenList->FindOrCreatePen(m_cBorderColour, 1, wxSOLID));
96
dc.DrawRectangle(0, 0, w, h);
98
dc.SetPen(*wxThePenList->FindOrCreatePen(m_cActiveBar, 1, wxSOLID));
99
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cActiveBar,wxSOLID));
104
dc.DrawRectangle(1, 1, m_nScaledVal, h - 2);
107
yPoint = h - m_nScaledVal ;
109
if (m_nScaledVal == 0 )
110
rectHeight = m_nScaledVal ;
113
if (m_nRealVal == m_nMax)
115
rectHeight = m_nScaledVal;
119
rectHeight = m_nScaledVal - 1 ;
122
dc.DrawRectangle(1, yPoint, w - 2, rectHeight);
126
DrawCurrent(dc); //valore attuale
129
DrawLimits(dc); //valore minimo e massimo
135
// We can now draw into the memory DC...
136
// Copy from this DC to another DC.
137
old_dc.Blit(0, 0, w, h, &dc, 0, 0);
140
void kwxLinearRegulator::OnMouse(wxMouseEvent& event)
142
if (m_nStato == 0 && event.Entering()) //dentro nel controllo
144
else if (m_nStato >= 1 && event.Leaving()) //fuori dal controllo
146
else if (m_nStato == 1 && event.LeftDown()) //click o inizio trascinamento
148
// wxLogTrace("Mouse Premuto") ;
150
m_mousePosition = event.GetPosition();
153
else if (m_nStato == 2 && event.LeftIsDown()) //trascinamento
155
// wxLogTrace("Mouse trascinato") ;
156
m_mousePosition = event.GetPosition();
159
else if (m_nStato == 2 && event.LeftUp()) //fine trascinamento o rilascio click
161
// wxLogTrace("Mouse rilasciato") ;
166
void kwxLinearRegulator::DrawPosition()
173
m_nScaledVal = m_mousePosition.x ;
174
if ( m_nScaledVal < 0 || m_nScaledVal >= m_ControlSize.GetWidth() - 1)
177
coeff = (m_ControlSize.GetWidth() - 2) / (double)(m_nMax - m_nMin);
181
m_nScaledVal = m_ControlSize.GetHeight() - m_mousePosition.y;
182
if ( m_nScaledVal < 0 || m_nScaledVal >= m_ControlSize.GetHeight() - 1)
185
coeff = (m_ControlSize.GetHeight() - 2) / (double)(m_nMax - m_nMin);
189
if (m_nRealVal <= m_nMax && draw)
191
m_nRealVal = floor(m_nScaledVal / coeff) ;
197
void kwxLinearRegulator::AddTag(int val)
199
m_aTagsVal[m_nTagsNum++] = val ;
202
//evento cambio valore
203
void kwxLinearRegulator::Change()
205
wxCommandEvent event(kwxEVT_LINEARREG_CHANGE, GetId());
206
event.SetEventObject(this);
207
// ProcessCommand(event) ;
208
GetEventHandler()->ProcessEvent(event);
211
//per inizializzazione
212
void kwxLinearRegulator::SetValue(int val)
217
GetClientSize(&w,&h);
220
coeff = (w - 2) / (double)(m_nMax - m_nMin);
222
coeff = (h - 2) / (double)(m_nMax - m_nMin);
224
m_nScaledVal = ceil((val - m_nMin) * coeff);
230
///////////////////////////////
231
// FUNZIONI DISEGNO CUSTOM //
232
///////////////////////////////
234
//Disegna valore corrente
235
void kwxLinearRegulator::DrawCurrent(wxDC &dc)
241
GetClientSize(&w, &h);
244
s.Printf(wxT("%d"), m_nRealVal);
245
dc.GetTextExtent(s, &tw, &th);
246
dc.SetTextForeground(m_cValueColour);
247
dc.DrawText(s, w / 2 - tw / 2 , h / 2 - th / 2);
250
//Disegna limiti controllo
251
void kwxLinearRegulator::DrawLimits(wxDC &dc)
257
GetClientSize(&w, &h);
259
dc.SetTextForeground(m_cLimitColour);
264
s.Printf(wxT("%d"), m_nMin);
265
dc.GetTextExtent(s, &tw, &th);
266
dc.DrawText(s, 5, h / 2 - th / 2);
269
s.Printf(wxT("%d"), m_nMax);
270
dc.GetTextExtent(s, &tw, &th);
271
dc.DrawText(s,w - tw - 5, h / 2 - th / 2);
276
s.Printf(wxT("%d"), m_nMin);
277
dc.GetTextExtent(s, &tw, &th);
278
dc.DrawText(s, w / 2 - tw / 2, h - th - 5);
281
s.Printf(wxT("%d"), m_nMax);
282
dc.GetTextExtent(s, &tw, &th);
283
dc.DrawText(s, w / 2 - tw / 2, 5);
287
//Disegna i tag impostati
288
void kwxLinearRegulator::DrawTags(wxDC &dc)
298
GetClientSize(&w,&h);
300
tcoeff = (w - 2) / (double)(m_nMax - m_nMin);
302
tcoeff = (h - 2) / (double)(m_nMax - m_nMin);
304
dc.SetPen(*wxThePenList->FindOrCreatePen(m_cTagsColour, 1, wxSOLID));
305
dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cTagsColour,wxSOLID));
306
dc.SetTextForeground(m_cTagsColour);
308
while (ntag < m_nTagsNum)
310
scalval = floor((m_aTagsVal[ ntag] - m_nMin) * tcoeff);
311
text.Printf(wxT("%d"), m_aTagsVal[ ntag]) ;
315
dc.DrawLine(scalval + 1, h - 2 , scalval + 1, h - 10);
316
dc.GetTextExtent(text, &tw, &th);
317
dc.DrawText(text, scalval + 1 - (tw / 2 ), h - 10 - th);
321
dc.DrawLine(w - 2, h - scalval , w - 10 , h - scalval);
322
dc.GetTextExtent(text, &tw, &th);
323
dc.DrawText(text, w - 10 - tw, h - scalval - (th / 2) );