1 // ============================================================================
3 // ============================================================================
6 #pragma implementation "wxLEDNumberCtrl.h"
16 #include "wx/dcclient.h"
17 #include "wx/dcmemory.h"
21 #include "wx/gizmos/ledctrl.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // A LED digit is build up like this, with maximum 7 Lines :
35 // Each number contains combinations of the lines, and they are set up below.
45 const int DIGIT0
= LINE1
| LINE2
| LINE3
| LINE4
| LINE5
| LINE6
;
46 const int DIGIT1
= LINE2
| LINE3
;
47 const int DIGIT2
= LINE1
| LINE2
| LINE4
| LINE5
| LINE7
;
48 const int DIGIT3
= LINE1
| LINE2
| LINE3
| LINE4
| LINE7
;
49 const int DIGIT4
= LINE2
| LINE3
| LINE6
| LINE7
;
50 const int DIGIT5
= LINE1
| LINE3
| LINE4
| LINE6
| LINE7
;
51 const int DIGIT6
= LINE1
| LINE3
| LINE4
| LINE5
| LINE6
| LINE7
;
52 const int DIGIT7
= LINE1
| LINE2
| LINE3
;
53 const int DIGIT8
= LINE1
| LINE2
| LINE3
| LINE4
| LINE5
| LINE6
| LINE7
;
54 const int DIGIT9
= LINE1
| LINE2
| LINE3
| LINE6
| LINE7
;
55 const int DASH
= LINE7
;
57 const int DIGITALL
= -1;
59 // ============================================================================
60 // wxLEDNumberCtrl class implementation
61 // ============================================================================
63 wxLEDNumberCtrl::wxLEDNumberCtrl()
64 : m_Alignment(wxLED_ALIGN_LEFT
),
75 wxLEDNumberCtrl::wxLEDNumberCtrl(wxWindow
*parent
, wxWindowID id
,
76 const wxPoint
& pos
, const wxSize
& size
,
78 : m_Alignment(wxLED_ALIGN_LEFT
),
86 Create(parent
, id
, pos
, size
, style
);
90 bool wxLEDNumberCtrl::Create(wxWindow
*parent
, wxWindowID id
,
91 const wxPoint
& pos
, const wxSize
& size
,
94 bool RetVal
= wxControl::Create(parent
, id
, pos
, size
, style
);
96 if ((style
& wxLED_DRAW_FADED
) != 0)
98 if ((style
& wxLED_ALIGN_MASK
) != 0)
99 SetAlignment((wxLEDValueAlign
)(style
& wxLED_ALIGN_MASK
));
101 SetBackgroundColour(*wxBLACK
);
102 SetForegroundColour(*wxGREEN
);
108 void wxLEDNumberCtrl::SetAlignment(wxLEDValueAlign Alignment
, bool Redraw
)
110 if (Alignment
!= m_Alignment
)
112 m_Alignment
= Alignment
;
113 RecalcInternals(GetClientSize());
121 void wxLEDNumberCtrl::SetDrawFaded(bool DrawFaded
, bool Redraw
)
123 if (DrawFaded
!= m_DrawFaded
)
125 m_DrawFaded
= DrawFaded
;
133 void wxLEDNumberCtrl::SetValue(wxString
const &Value
, bool Redraw
)
135 if (Value
!= m_Value
)
137 if (!Value
.IsEmpty())
139 for(size_t i
=0; i
<Value
.Length(); i
++) {
140 wxChar ch
= Value
[i
];
141 wxASSERT_MSG((ch
>='0' && ch
<='9') || ch
=='-' || ch
==' ',
142 wxT("wxLEDNumberCtrl can only display numeric string values."));
147 RecalcInternals(GetClientSize());
155 BEGIN_EVENT_TABLE(wxLEDNumberCtrl
, wxControl
)
156 EVT_ERASE_BACKGROUND(wxLEDNumberCtrl::OnEraseBackground
)
157 EVT_PAINT(wxLEDNumberCtrl::OnPaint
)
158 EVT_SIZE(wxLEDNumberCtrl::OnSize
)
162 void wxLEDNumberCtrl::OnEraseBackground(wxEraseEvent
&Event
)
167 void wxLEDNumberCtrl::OnPaint(wxPaintEvent
&Event
)
172 GetClientSize(&Width
, &Height
);
174 wxBitmap
*pMemoryBitmap
= new wxBitmap(Width
, Height
);
177 MemDc
.SelectObject(*pMemoryBitmap
);
178 MemDc
.BeginDrawing();
181 MemDc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
182 MemDc
.DrawRectangle(wxRect(0, 0, Width
, Height
));
183 MemDc
.SetBrush(wxNullBrush
);
185 // Iterate each digit in the value, and draw.
186 const int DigitCount
= m_Value
.Len();
187 for (int i
= 0; i
< DigitCount
; ++i
)
189 // Draw faded lines if wanted.
191 DrawDigit(MemDc
, DIGITALL
, i
);
197 DrawDigit(MemDc
, DIGIT0
, i
);
200 DrawDigit(MemDc
, DIGIT1
, i
);
203 DrawDigit(MemDc
, DIGIT2
, i
);
206 DrawDigit(MemDc
, DIGIT3
, i
);
209 DrawDigit(MemDc
, DIGIT4
, i
);
212 DrawDigit(MemDc
, DIGIT5
, i
);
215 DrawDigit(MemDc
, DIGIT6
, i
);
218 DrawDigit(MemDc
, DIGIT7
, i
);
221 DrawDigit(MemDc
, DIGIT8
, i
);
224 DrawDigit(MemDc
, DIGIT9
, i
);
227 DrawDigit(MemDc
, DASH
, i
);
233 wxFAIL_MSG(wxT("Unknown digit value"));
240 // Blit the memory dc to screen.
241 Dc
.Blit(0, 0, Width
, Height
, &MemDc
, 0, 0, wxCOPY
);
242 delete pMemoryBitmap
;
246 void wxLEDNumberCtrl::DrawDigit(wxDC
&Dc
, int Digit
, int Column
)
248 wxColour
LineColor(GetForegroundColour());
250 if (Digit
== DIGITALL
)
252 const int R
= LineColor
.Red() / 3;
253 const int G
= LineColor
.Green() / 3;
254 const int B
= LineColor
.Blue() / 3;
256 LineColor
.Set(R
, G
, B
);
259 int XPos
= m_LeftStartPos
;
262 XPos
+= (Column
* m_LineLength
) + (m_DigitMargin
) * Column
;
264 // Create a pen and draw the lines.
265 wxPen
Pen(LineColor
, m_LineWidth
, wxSOLID
);
270 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineMargin
,
271 XPos
+ m_LineLength
, m_LineMargin
);
276 Dc
.DrawLine(XPos
+ m_LineLength
+ m_LineMargin
, m_LineMargin
*2,
277 XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*2));
282 Dc
.DrawLine(XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*4),
283 XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
*2 + (m_LineMargin
*3));
288 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineLength
*2 + (m_LineMargin
*4),
289 XPos
+ m_LineLength
, m_LineLength
*2 + (m_LineMargin
*4));
294 Dc
.DrawLine(XPos
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*4),
295 XPos
+ m_LineMargin
, m_LineLength
*2 + (m_LineMargin
*3));
300 Dc
.DrawLine(XPos
+ m_LineMargin
, m_LineMargin
*2,
301 XPos
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*2));
306 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineLength
+ (m_LineMargin
*3),
307 XPos
+ m_LineMargin
+ m_LineLength
- m_LineMargin
, m_LineLength
+ (m_LineMargin
*3));
310 Dc
.SetPen(wxNullPen
);
314 void wxLEDNumberCtrl::RecalcInternals(const wxSize
&CurrentSize
)
316 const int Height
= CurrentSize
.GetHeight();
318 if ((Height
* 0.07) < 1)
321 m_LineMargin
= Height
* 0.07;
323 if ((Height
* 0.35) < 1)
326 m_LineLength
= Height
* 0.35;
328 m_LineWidth
= m_LineMargin
;
330 m_DigitMargin
= m_LineMargin
* 4;
332 const int ValueWidth
= (m_LineLength
+ m_DigitMargin
) * m_Value
.Len();
333 const int ClientWidth
= CurrentSize
.GetWidth();
337 case wxLED_ALIGN_LEFT
:
340 case wxLED_ALIGN_RIGHT
:
341 m_LeftStartPos
= ClientWidth
- ValueWidth
;
343 case wxLED_ALIGN_CENTER
:
344 m_LeftStartPos
= (ClientWidth
- ValueWidth
) / 2;
347 wxFAIL_MSG(wxT("Unknown alignent value for wxLEDNumberCtrl."));
353 void wxLEDNumberCtrl::OnSize(wxSizeEvent
&Event
)
355 RecalcInternals(Event
.GetSize());