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
)
138 if (!Value
.IsEmpty())
140 for(size_t i
=0; i
<Value
.Length(); i
++) {
141 wxChar ch
= Value
[i
];
142 wxASSERT_MSG((ch
>='0' && ch
<='9') || ch
=='-' || ch
==' ',
143 wxT("wxLEDNumberCtrl can only display numeric string values."));
149 RecalcInternals(GetClientSize());
157 BEGIN_EVENT_TABLE(wxLEDNumberCtrl
, wxControl
)
158 EVT_ERASE_BACKGROUND(wxLEDNumberCtrl::OnEraseBackground
)
159 EVT_PAINT(wxLEDNumberCtrl::OnPaint
)
160 EVT_SIZE(wxLEDNumberCtrl::OnSize
)
164 void wxLEDNumberCtrl::OnEraseBackground(wxEraseEvent
&WXUNUSED(event
))
169 void wxLEDNumberCtrl::OnPaint(wxPaintEvent
&WXUNUSED(event
))
174 GetClientSize(&Width
, &Height
);
176 wxBitmap
*pMemoryBitmap
= new wxBitmap(Width
, Height
);
179 MemDc
.SelectObject(*pMemoryBitmap
);
180 MemDc
.BeginDrawing();
183 MemDc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
184 MemDc
.DrawRectangle(wxRect(0, 0, Width
, Height
));
185 MemDc
.SetBrush(wxNullBrush
);
187 // Iterate each digit in the value, and draw.
188 const int DigitCount
= m_Value
.Len();
189 for (int i
= 0; i
< DigitCount
; ++i
)
191 // Draw faded lines if wanted.
193 DrawDigit(MemDc
, DIGITALL
, i
);
196 switch (m_Value
.GetChar(i
))
199 DrawDigit(MemDc
, DIGIT0
, i
);
202 DrawDigit(MemDc
, DIGIT1
, i
);
205 DrawDigit(MemDc
, DIGIT2
, i
);
208 DrawDigit(MemDc
, DIGIT3
, i
);
211 DrawDigit(MemDc
, DIGIT4
, i
);
214 DrawDigit(MemDc
, DIGIT5
, i
);
217 DrawDigit(MemDc
, DIGIT6
, i
);
220 DrawDigit(MemDc
, DIGIT7
, i
);
223 DrawDigit(MemDc
, DIGIT8
, i
);
226 DrawDigit(MemDc
, DIGIT9
, i
);
229 DrawDigit(MemDc
, DASH
, i
);
235 wxFAIL_MSG(wxT("Unknown digit value"));
242 // Blit the memory dc to screen.
243 Dc
.Blit(0, 0, Width
, Height
, &MemDc
, 0, 0, wxCOPY
);
244 delete pMemoryBitmap
;
248 void wxLEDNumberCtrl::DrawDigit(wxDC
&Dc
, int Digit
, int Column
)
250 wxColour
LineColor(GetForegroundColour());
252 if (Digit
== DIGITALL
)
254 const int R
= LineColor
.Red() / 3;
255 const int G
= LineColor
.Green() / 3;
256 const int B
= LineColor
.Blue() / 3;
258 LineColor
.Set(R
, G
, B
);
261 int XPos
= m_LeftStartPos
;
264 XPos
+= (Column
* m_LineLength
) + (m_DigitMargin
) * Column
;
266 // Create a pen and draw the lines.
267 wxPen
Pen(LineColor
, m_LineWidth
, wxSOLID
);
272 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineMargin
,
273 XPos
+ m_LineLength
, m_LineMargin
);
278 Dc
.DrawLine(XPos
+ m_LineLength
+ m_LineMargin
, m_LineMargin
*2,
279 XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*2));
284 Dc
.DrawLine(XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*4),
285 XPos
+ m_LineLength
+ m_LineMargin
, m_LineLength
*2 + (m_LineMargin
*3));
290 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineLength
*2 + (m_LineMargin
*4),
291 XPos
+ m_LineLength
, m_LineLength
*2 + (m_LineMargin
*4));
296 Dc
.DrawLine(XPos
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*4),
297 XPos
+ m_LineMargin
, m_LineLength
*2 + (m_LineMargin
*3));
302 Dc
.DrawLine(XPos
+ m_LineMargin
, m_LineMargin
*2,
303 XPos
+ m_LineMargin
, m_LineLength
+ (m_LineMargin
*2));
308 Dc
.DrawLine(XPos
+ m_LineMargin
*2, m_LineLength
+ (m_LineMargin
*3),
309 XPos
+ m_LineMargin
+ m_LineLength
- m_LineMargin
, m_LineLength
+ (m_LineMargin
*3));
312 Dc
.SetPen(wxNullPen
);
316 void wxLEDNumberCtrl::RecalcInternals(const wxSize
&CurrentSize
)
318 const int Height
= CurrentSize
.GetHeight();
320 if ((Height
* 0.07) < 1)
323 m_LineMargin
= (int)(Height
* 0.07);
325 if ((Height
* 0.35) < 1)
328 m_LineLength
= (int)(Height
* 0.35);
330 m_LineWidth
= m_LineMargin
;
332 m_DigitMargin
= m_LineMargin
* 4;
334 const int ValueWidth
= (m_LineLength
+ m_DigitMargin
) * m_Value
.Len();
335 const int ClientWidth
= CurrentSize
.GetWidth();
339 case wxLED_ALIGN_LEFT
:
342 case wxLED_ALIGN_RIGHT
:
343 m_LeftStartPos
= ClientWidth
- ValueWidth
;
345 case wxLED_ALIGN_CENTER
:
346 m_LeftStartPos
= (ClientWidth
- ValueWidth
) / 2;
349 wxFAIL_MSG(wxT("Unknown alignent value for wxLEDNumberCtrl."));
355 void wxLEDNumberCtrl::OnSize(wxSizeEvent
&Event
)
357 RecalcInternals(Event
.GetSize());