~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxContribItems/KWIC/src/wx/KWIC/LinearRegulator.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/////////////////////////////////////////////////////////////////////////////
 
2
// Name:        LinearRegulator.cpp
 
3
// Purpose:     wxIndustrialControls Library
 
4
// Author:      Marco Cavallini <m.cavallini AT koansoftware.com>
 
5
// Modified by:
 
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
/////////////////////////////////////////////////////////////////////////////
 
9
//
 
10
//      Cleaned up and modified by Gary Harris for wxSmithKWIC, 2010.
 
11
//
 
12
// Fixed bugs that caused the font selection only to be used when m_bShowLimits was true.
 
13
/////////////////////////////////////////////////////////////////////////////
 
14
 
 
15
 
 
16
#ifndef WX_PRECOMP
 
17
        #include "wx/wx.h"
 
18
#endif
 
19
 
 
20
#include "wx/KWIC/LinearRegulator.h"
 
21
#include <wx/event.h>
 
22
#include <math.h>
 
23
 
 
24
IMPLEMENT_DYNAMIC_CLASS(kwxLinearRegulator, wxControl)
 
25
 
 
26
BEGIN_EVENT_TABLE(kwxLinearRegulator,wxControl)
 
27
        EVT_MOUSE_EVENTS(kwxLinearRegulator::OnMouse)
 
28
        EVT_PAINT(kwxLinearRegulator::OnPaint)
 
29
END_EVENT_TABLE()
 
30
 
 
31
kwxLinearRegulator::kwxLinearRegulator(wxWindow* parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, const long int style)
 
32
{
 
33
        Create(parent, id, pos, size, style);
 
34
}
 
35
 
 
36
bool kwxLinearRegulator::Create(wxWindow* parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, const long int style)
 
37
{
 
38
        if(!wxWindow::Create(parent, id, pos, size, style))
 
39
                return false;
 
40
 
 
41
        m_id = id;
 
42
 
 
43
        m_ControlSize = size ;
 
44
 
 
45
        m_nScaledVal = 0;
 
46
        m_nRealVal = 0;
 
47
        m_nTagsNum = 0;
 
48
 
 
49
        m_nStato = 0;
 
50
        m_nMin = 0;
 
51
        m_nMax = 100;
 
52
 
 
53
        m_bDirOrizFlag = true ;
 
54
        m_bShowCurrent = true;
 
55
        m_bShowLimits= true;
 
56
 
 
57
        m_cActiveBar = *wxGREEN;
 
58
        m_cPassiveBar = *wxWHITE;
 
59
        m_cBorderColour = *wxRED ;
 
60
        m_cTagsColour = *wxBLACK;
 
61
        m_cValueColour = *wxRED;
 
62
        m_cLimitColour = *wxBLACK;
 
63
 
 
64
        m_bShowLimits = true;
 
65
        m_bShowCurrent = true;
 
66
 
 
67
        membitmap = new wxBitmap(size.GetWidth(), size.GetHeight());
 
68
        return true;
 
69
}
 
70
 
 
71
kwxLinearRegulator::~kwxLinearRegulator()
 
72
{
 
73
        if (membitmap)
 
74
                delete membitmap ;
 
75
}
 
76
 
 
77
void kwxLinearRegulator::OnPaint(wxPaintEvent &WXUNUSED(event))
 
78
{
 
79
        wxPaintDC old_dc(this);
 
80
 
 
81
        int w,h;
 
82
        int yPoint, rectHeight ;
 
83
 
 
84
        GetClientSize(&w,&h);
 
85
 
 
86
        wxMemoryDC dc;
 
87
        dc.SelectObject(*membitmap);
 
88
 
 
89
 
 
90
        dc.SetBackground(*wxTheBrushList->FindOrCreateBrush(m_cPassiveBar,wxSOLID));
 
91
        dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cPassiveBar,wxSOLID));
 
92
        dc.Clear();
 
93
        ///////////////////////////////////////////////////////////////////////////
 
94
 
 
95
        dc.SetPen(*wxThePenList->FindOrCreatePen(m_cBorderColour, 1, wxSOLID));
 
96
        dc.DrawRectangle(0, 0, w, h);
 
97
 
 
98
        dc.SetPen(*wxThePenList->FindOrCreatePen(m_cActiveBar, 1, wxSOLID));
 
99
        dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cActiveBar,wxSOLID));
 
100
        dc.SetFont(m_Font);
 
101
 
 
102
 
 
103
        if(m_bDirOrizFlag)
 
104
                dc.DrawRectangle(1, 1, m_nScaledVal, h - 2);
 
105
        else
 
106
        {
 
107
                yPoint = h - m_nScaledVal ;
 
108
 
 
109
                if (m_nScaledVal == 0 )
 
110
                        rectHeight = m_nScaledVal ;
 
111
                else
 
112
                {
 
113
                        if (m_nRealVal == m_nMax)
 
114
                        {
 
115
                                rectHeight = m_nScaledVal;
 
116
                                yPoint -= 1 ;
 
117
                        }
 
118
                        else
 
119
                                rectHeight = m_nScaledVal - 1 ;
 
120
                }
 
121
 
 
122
                dc.DrawRectangle(1, yPoint, w - 2, rectHeight);
 
123
        }
 
124
 
 
125
        if (m_bShowCurrent)
 
126
                DrawCurrent(dc);        //valore attuale
 
127
 
 
128
        if (m_bShowLimits)
 
129
                DrawLimits(dc); //valore minimo e massimo
 
130
 
 
131
        if (m_nTagsNum > 0 )
 
132
                DrawTags(dc) ;
 
133
 
 
134
 
 
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);
 
138
}
 
139
 
 
140
void kwxLinearRegulator::OnMouse(wxMouseEvent& event)
 
141
{
 
142
        if (m_nStato == 0 && event.Entering())  //dentro nel controllo
 
143
                m_nStato = 1 ;
 
144
        else if (m_nStato >= 1 && event.Leaving())      //fuori dal controllo
 
145
                m_nStato = 0 ;
 
146
        else if (m_nStato == 1 && event.LeftDown())     //click o inizio trascinamento
 
147
        {
 
148
//              wxLogTrace("Mouse Premuto") ;
 
149
                m_nStato = 2 ;
 
150
                m_mousePosition = event.GetPosition();
 
151
                DrawPosition() ;
 
152
        }
 
153
        else if (m_nStato == 2 && event.LeftIsDown())   //trascinamento
 
154
        {
 
155
//              wxLogTrace("Mouse trascinato") ;
 
156
                m_mousePosition = event.GetPosition();
 
157
                DrawPosition() ;
 
158
        }
 
159
        else if (m_nStato == 2 && event.LeftUp())       //fine trascinamento o rilascio click
 
160
        {
 
161
//              wxLogTrace("Mouse rilasciato") ;
 
162
                m_nStato = 1 ;
 
163
        }
 
164
}
 
165
 
 
166
void kwxLinearRegulator::DrawPosition()
 
167
{
 
168
        double coeff = 0 ;
 
169
        bool draw = true ;
 
170
 
 
171
        if (m_bDirOrizFlag)
 
172
        {
 
173
                m_nScaledVal = m_mousePosition.x ;
 
174
                if ( m_nScaledVal < 0 || m_nScaledVal >= m_ControlSize.GetWidth() - 1)
 
175
                        draw = false ;
 
176
                else
 
177
                        coeff = (m_ControlSize.GetWidth() - 2) / (double)(m_nMax - m_nMin);
 
178
        }
 
179
        else
 
180
        {
 
181
                m_nScaledVal = m_ControlSize.GetHeight() - m_mousePosition.y;
 
182
                if ( m_nScaledVal < 0 || m_nScaledVal >= m_ControlSize.GetHeight() - 1)
 
183
                        draw = false ;
 
184
                else
 
185
                        coeff = (m_ControlSize.GetHeight() - 2) / (double)(m_nMax - m_nMin);
 
186
        }
 
187
 
 
188
 
 
189
        if (m_nRealVal <= m_nMax && draw)
 
190
        {
 
191
                m_nRealVal = floor(m_nScaledVal / coeff) ;
 
192
                Refresh() ;
 
193
                Change() ;
 
194
        }
 
195
}
 
196
 
 
197
void kwxLinearRegulator::AddTag(int val)
 
198
{
 
199
        m_aTagsVal[m_nTagsNum++] = val ;
 
200
}
 
201
 
 
202
//evento cambio valore
 
203
void kwxLinearRegulator::Change()
 
204
{
 
205
        wxCommandEvent event(kwxEVT_LINEARREG_CHANGE, GetId());
 
206
        event.SetEventObject(this);
 
207
//      ProcessCommand(event) ;
 
208
    GetEventHandler()->ProcessEvent(event);
 
209
}
 
210
 
 
211
//per inizializzazione
 
212
void kwxLinearRegulator::SetValue(int val)
 
213
{
 
214
        int w,h;
 
215
        double coeff;
 
216
 
 
217
        GetClientSize(&w,&h);
 
218
 
 
219
        if(m_bDirOrizFlag)
 
220
                coeff = (w - 2) / (double)(m_nMax - m_nMin);
 
221
        else
 
222
                coeff = (h - 2) / (double)(m_nMax - m_nMin);
 
223
 
 
224
        m_nScaledVal = ceil((val - m_nMin) * coeff);
 
225
        m_nRealVal = val;
 
226
 
 
227
        Refresh();
 
228
}
 
229
 
 
230
///////////////////////////////
 
231
//      FUNZIONI DISEGNO CUSTOM  //
 
232
///////////////////////////////
 
233
 
 
234
//Disegna valore corrente
 
235
void kwxLinearRegulator::DrawCurrent(wxDC &dc)
 
236
{
 
237
        int w,h;
 
238
        int tw,th;
 
239
        wxString s;
 
240
 
 
241
        GetClientSize(&w, &h);
 
242
 
 
243
        //valore attuale
 
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);
 
248
}
 
249
 
 
250
//Disegna limiti controllo
 
251
void kwxLinearRegulator::DrawLimits(wxDC &dc)
 
252
{
 
253
        int w,h;
 
254
        int tw,th;
 
255
        wxString s;
 
256
 
 
257
        GetClientSize(&w, &h);
 
258
 
 
259
        dc.SetTextForeground(m_cLimitColour);
 
260
 
 
261
        if(m_bDirOrizFlag)
 
262
        {
 
263
                //valore minimo
 
264
                s.Printf(wxT("%d"), m_nMin);
 
265
                dc.GetTextExtent(s, &tw, &th);
 
266
                dc.DrawText(s, 5, h / 2 - th / 2);
 
267
 
 
268
                //valore massimo
 
269
                s.Printf(wxT("%d"), m_nMax);
 
270
                dc.GetTextExtent(s, &tw, &th);
 
271
                dc.DrawText(s,w - tw - 5, h / 2 - th / 2);
 
272
        }
 
273
        else
 
274
        {
 
275
                //valore minimo
 
276
                s.Printf(wxT("%d"), m_nMin);
 
277
                dc.GetTextExtent(s, &tw, &th);
 
278
                dc.DrawText(s, w / 2 - tw / 2, h - th - 5);
 
279
 
 
280
                //valore massimo
 
281
                s.Printf(wxT("%d"), m_nMax);
 
282
                dc.GetTextExtent(s, &tw, &th);
 
283
                dc.DrawText(s, w / 2 - tw / 2, 5);
 
284
        }
 
285
}
 
286
 
 
287
//Disegna i tag impostati
 
288
void kwxLinearRegulator::DrawTags(wxDC &dc)
 
289
{
 
290
        int ntag = 0 ;
 
291
        int w, h ;
 
292
        int tw,th;
 
293
        int scalval = 0 ;
 
294
        double tcoeff ;
 
295
 
 
296
        wxString text ;
 
297
 
 
298
        GetClientSize(&w,&h);
 
299
        if(m_bDirOrizFlag)
 
300
                tcoeff = (w - 2) / (double)(m_nMax - m_nMin);
 
301
        else
 
302
                tcoeff = (h - 2) / (double)(m_nMax - m_nMin);
 
303
 
 
304
        dc.SetPen(*wxThePenList->FindOrCreatePen(m_cTagsColour, 1, wxSOLID));
 
305
        dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(m_cTagsColour,wxSOLID));
 
306
        dc.SetTextForeground(m_cTagsColour);
 
307
 
 
308
        while (ntag < m_nTagsNum)
 
309
        {
 
310
                scalval = floor((m_aTagsVal[ ntag] - m_nMin) * tcoeff);
 
311
                text.Printf(wxT("%d"), m_aTagsVal[ ntag]) ;
 
312
 
 
313
                if(m_bDirOrizFlag)
 
314
                {
 
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);
 
318
                }
 
319
                else
 
320
                {
 
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) );
 
324
                }
 
325
 
 
326
                ntag++ ;
 
327
        }
 
328
}
 
329
 
 
330