From db11ea905f857284b54bd245615001ab9c03627a Mon Sep 17 00:00:00 2001 From: =?utf8?q?W=C5=82odzimierz=20Skiba?= Date: Wed, 25 Aug 2004 18:50:52 +0000 Subject: [PATCH 1/1] Support for comma in contrib gizmo wxLEDNumberCtrl (patches #1006434, #1007063 with modifications). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28893 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/samples/gizmos/led/led.cpp | 102 ++++++++++++++++++++--------- contrib/src/gizmos/ledctrl.cpp | 93 ++++++++++++++++++-------- docs/changes.txt | 1 + 3 files changed, 137 insertions(+), 59 deletions(-) diff --git a/contrib/samples/gizmos/led/led.cpp b/contrib/samples/gizmos/led/led.cpp index 8195289fdc..5de6f0f513 100644 --- a/contrib/samples/gizmos/led/led.cpp +++ b/contrib/samples/gizmos/led/led.cpp @@ -30,7 +30,7 @@ #include "wx/wx.h" #endif -#include "../../../include/wx/gizmos/ledctrl.h" +#include "wx/gizmos/ledctrl.h" #include "wx/sizer.h" #include "wx/panel.h" #include "wx/numdlg.h" @@ -46,6 +46,8 @@ public: void OnIncrement(); void OnDecrement(); + void OnSmallIncrement(); + void OnSmallDecrement(); void OnSetValue(); void OnAlignLeft(); void OnAlignCenter(); @@ -54,6 +56,7 @@ public: private: wxLEDNumberCtrl *m_led; + wxBoxSizer *m_sizer; }; // Define a new application type, each program should derive a class from wxApp @@ -81,6 +84,8 @@ public: void OnQuit(wxCommandEvent& event); void OnIncrement(wxCommandEvent& event); void OnDecrement(wxCommandEvent& event); + void OnSmallIncrement(wxCommandEvent& event); + void OnSmallDecrement(wxCommandEvent& event); void OnSetValue(wxCommandEvent& event); void OnAlignLeft(wxCommandEvent& event); void OnAlignCenter(wxCommandEvent& event); @@ -103,10 +108,12 @@ private: enum { // menu items - LED_Quit = 1, + LED_Quit = wxID_EXIT, - LED_Edit_Increment, + LED_Edit_Increment = wxID_HIGHEST + 1, LED_Edit_Decrement, + LED_Edit_Small_Increment, + LED_Edit_Small_Decrement, LED_Edit_SetValue, LED_Edit_AlignLeft, LED_Edit_AlignCenter, @@ -116,10 +123,7 @@ enum // it is important for the id corresponding to the "About" command to have // this standard value as otherwise it won't be handled properly under Mac // (where it is special and put into the "Apple" menu) - LED_About = wxID_ABOUT, - - MY_PANEL, - MY_LED + LED_About = wxID_ABOUT }; // ---------------------------------------------------------------------------- @@ -133,6 +137,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(LED_Quit, MyFrame::OnQuit) EVT_MENU(LED_Edit_Increment, MyFrame::OnIncrement) EVT_MENU(LED_Edit_Decrement, MyFrame::OnDecrement) + EVT_MENU(LED_Edit_Small_Increment, MyFrame::OnSmallIncrement) + EVT_MENU(LED_Edit_Small_Decrement, MyFrame::OnSmallDecrement) EVT_MENU(LED_Edit_SetValue, MyFrame::OnSetValue) EVT_MENU(LED_Edit_AlignLeft, MyFrame::OnAlignLeft) EVT_MENU(LED_Edit_AlignCenter, MyFrame::OnAlignCenter) @@ -161,7 +167,7 @@ bool MyApp::OnInit() { // create the main application window MyFrame *frame = new MyFrame(_T("LED App"), - wxPoint(50, 50), wxSize(450, 340)); + wxDefaultPosition, wxSize(450, 120)); // and show it (the frames, unlike simple controls, are not shown when // created initially) @@ -192,8 +198,10 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, menuFile->Append(LED_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); wxMenu *editMenu = new wxMenu; - editMenu->Append(LED_Edit_Increment, _T("&Increment LED\tCtrl-I")); - editMenu->Append(LED_Edit_Decrement, _T("&Decrement LED\tCtrl-D")); + editMenu->Append(LED_Edit_Increment, _T("&Increment LED (1)\tCtrl-I")); + editMenu->Append(LED_Edit_Small_Increment, _T("&Increment LED (0.01)\tAlt-I")); + editMenu->Append(LED_Edit_Decrement, _T("&Decrement LED (1)\tCtrl-D")); + editMenu->Append(LED_Edit_Small_Decrement, _T("&Decrement LED (0.01)\tAlt-D")); editMenu->Append(LED_Edit_SetValue, _T("&Set LED Value...\tCtrl-S")); editMenu->AppendSeparator(); editMenu->AppendRadioItem(LED_Edit_AlignLeft, _T("Align &Left")); @@ -236,6 +244,16 @@ void MyFrame::OnDecrement(wxCommandEvent& WXUNUSED(event)) m_panel->OnDecrement(); } +void MyFrame::OnSmallIncrement(wxCommandEvent& WXUNUSED(event)) +{ + m_panel->OnSmallIncrement(); +} + +void MyFrame::OnSmallDecrement(wxCommandEvent& WXUNUSED(event)) +{ + m_panel->OnSmallDecrement(); +} + void MyFrame::OnSetValue(wxCommandEvent& WXUNUSED(event)) { m_panel->OnSetValue(); @@ -275,50 +293,70 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) // -------------------------------------------------------------------------- MyPanel::MyPanel(wxFrame *frame) - : wxPanel(frame, MY_PANEL) + : wxPanel(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN) { - m_led = new wxLEDNumberCtrl(this, MY_LED, - wxPoint(20, 20), wxSize(300, 200), - wxLED_ALIGN_LEFT | wxLED_DRAW_FADED); + m_led = new wxLEDNumberCtrl(this, wxID_ANY, + wxDefaultPosition, wxDefaultSize, + wxLED_ALIGN_LEFT|wxLED_DRAW_FADED|wxFULL_REPAINT_ON_RESIZE); + + m_led->SetValue(_T("01.23 7-8-9")); + + m_sizer = new wxBoxSizer(wxVERTICAL); + m_sizer->Add(m_led, 1, wxEXPAND|wxALL, 10); + m_sizer->Fit(this); - m_led->SetValue(_T("50")); + SetSizer(m_sizer); + SetAutoLayout(true); } void MyPanel::OnIncrement() { wxString strValue = m_led->GetValue(); - if ( strValue == _T("99") ) - return; - long lValue; - strValue.ToLong(&lValue); - ++lValue; - m_led->SetValue(wxString::Format(_T("%ld"), lValue)); + double dValue; + strValue.ToDouble(&dValue); + dValue += 1.0; + m_led->SetValue(wxString::Format(_T("%.2f"), dValue)); } void MyPanel::OnDecrement() { wxString strValue = m_led->GetValue(); - long lValue; - strValue.ToLong(&lValue); - if (lValue == 0) - return; + double dValue; + strValue.ToDouble(&dValue); + dValue -= 1.0; + m_led->SetValue(wxString::Format(_T("%.2f"), dValue)); +} + +void MyPanel::OnSmallIncrement() +{ + wxString strValue = m_led->GetValue(); + + double dValue; + strValue.ToDouble(&dValue); + dValue += 0.01; + m_led->SetValue(wxString::Format(_T("%.2f"), dValue)); +} + +void MyPanel::OnSmallDecrement() +{ + wxString strValue = m_led->GetValue(); - --lValue; - m_led->SetValue(wxString::Format(_T("%ld"), lValue)); + double dValue; + strValue.ToDouble(&dValue); + dValue -= 0.01; + m_led->SetValue(wxString::Format(_T("%.2f"), dValue)); } void MyPanel::OnSetValue() { wxString strValue = m_led->GetValue(); - long lValue; - strValue.ToLong(&lValue); - lValue = ::wxGetNumberFromUser(_T("Please enter a number between 0 and 99"), _T(""), _T("Please enter a number"), lValue, 0, 99, this); + strValue = ::wxGetTextFromUser(_T("Please enter a number for LED display"), _T("Please enter a number"), strValue, this); - if (lValue != -1) - m_led->SetValue(wxString::Format(_T("%ld"), lValue)); + if (strValue != _T("")) + m_led->SetValue(strValue); } void MyPanel::OnAlignLeft() diff --git a/contrib/src/gizmos/ledctrl.cpp b/contrib/src/gizmos/ledctrl.cpp index ffcd8ad150..3f0d109a3c 100644 --- a/contrib/src/gizmos/ledctrl.cpp +++ b/contrib/src/gizmos/ledctrl.cpp @@ -41,6 +41,7 @@ const int LINE4 = 8; const int LINE5 = 16; const int LINE6 = 32; const int LINE7 = 64; +const int DECIMALSIGN = 128; const int DIGIT0 = LINE1 | LINE2 | LINE3 | LINE4 | LINE5 | LINE6; const int DIGIT1 = LINE2 | LINE3; @@ -139,7 +140,7 @@ void wxLEDNumberCtrl::SetValue(wxString const &Value, bool Redraw) { for(size_t i=0; i='0' && ch<='9') || ch=='-' || ch==' ', + wxASSERT_MSG((ch>='0' && ch<='9') || ch=='-' || ch==' ' || ch=='.', wxT("wxLEDNumberCtrl can only display numeric string values.")); } } @@ -186,14 +187,16 @@ void wxLEDNumberCtrl::OnPaint(wxPaintEvent &WXUNUSED(event)) // Iterate each digit in the value, and draw. const int DigitCount = m_Value.Len(); - for (int i = 0; i < DigitCount; ++i) + for (int offset=0, i = 0; offset < DigitCount; ++offset, ++i) { + char c = m_Value.GetChar(offset); + // Draw faded lines if wanted. - if (m_DrawFaded) + if (m_DrawFaded && (c != '.')) DrawDigit(MemDc, DIGITALL, i); // Draw the digits. - switch (m_Value.GetChar(i)) + switch (c) { case '0' : DrawDigit(MemDc, DIGIT0, i); @@ -228,6 +231,11 @@ void wxLEDNumberCtrl::OnPaint(wxPaintEvent &WXUNUSED(event)) case '-' : DrawDigit(MemDc, DASH, i); break; + case '.' : + // Display the decimal in the previous segment + i--; + DrawDigit(MemDc, DECIMALSIGN, i); + break; case ' ' : // just skip it break; @@ -251,17 +259,14 @@ void wxLEDNumberCtrl::DrawDigit(wxDC &Dc, int Digit, int Column) if (Digit == DIGITALL) { - const int R = LineColor.Red() / 3; - const int G = LineColor.Green() / 3; - const int B = LineColor.Blue() / 3; + const int R = LineColor.Red() / 16; + const int G = LineColor.Green() / 16; + const int B = LineColor.Blue() / 16; LineColor.Set(R, G, B); } - int XPos = m_LeftStartPos; - - if (Column > 0) - XPos += (Column * m_LineLength) + (m_DigitMargin) * Column; + int XPos = m_LeftStartPos + Column * (m_LineLength + m_DigitMargin); // Create a pen and draw the lines. wxPen Pen(LineColor, m_LineWidth, wxSOLID); @@ -270,31 +275,31 @@ void wxLEDNumberCtrl::DrawDigit(wxDC &Dc, int Digit, int Column) if ((Digit & LINE1)) { Dc.DrawLine(XPos + m_LineMargin*2, m_LineMargin, - XPos + m_LineLength, m_LineMargin); + XPos + m_LineLength + m_LineMargin*2, m_LineMargin); } if (Digit & LINE2) { - Dc.DrawLine(XPos + m_LineLength + m_LineMargin, m_LineMargin*2, - XPos + m_LineLength + m_LineMargin, m_LineLength + (m_LineMargin*2)); + Dc.DrawLine(XPos + m_LineLength + m_LineMargin*3, m_LineMargin*2, + XPos + m_LineLength + m_LineMargin*3, m_LineLength + (m_LineMargin*2)); } if (Digit & LINE3) { - Dc.DrawLine(XPos + m_LineLength + m_LineMargin, m_LineLength + (m_LineMargin*4), - XPos + m_LineLength + m_LineMargin, m_LineLength*2 + (m_LineMargin*3)); + Dc.DrawLine(XPos + m_LineLength + m_LineMargin*3, m_LineLength + (m_LineMargin*4), + XPos + m_LineLength + m_LineMargin*3, m_LineLength*2 + (m_LineMargin*4)); } if (Digit & LINE4) { - Dc.DrawLine(XPos + m_LineMargin*2, m_LineLength*2 + (m_LineMargin*4), - XPos + m_LineLength, m_LineLength*2 + (m_LineMargin*4)); + Dc.DrawLine(XPos + m_LineMargin*2, m_LineLength*2 + (m_LineMargin*5), + XPos + m_LineLength + m_LineMargin*2, m_LineLength*2 + (m_LineMargin*5)); } if (Digit & LINE5) { Dc.DrawLine(XPos + m_LineMargin, m_LineLength + (m_LineMargin*4), - XPos + m_LineMargin, m_LineLength*2 + (m_LineMargin*3)); + XPos + m_LineMargin, m_LineLength*2 + (m_LineMargin*4)); } if (Digit & LINE6) @@ -306,7 +311,13 @@ void wxLEDNumberCtrl::DrawDigit(wxDC &Dc, int Digit, int Column) if (Digit & LINE7) { Dc.DrawLine(XPos + m_LineMargin*2, m_LineLength + (m_LineMargin*3), - XPos + m_LineMargin + m_LineLength - m_LineMargin, m_LineLength + (m_LineMargin*3)); + XPos + m_LineMargin*2 + m_LineLength, m_LineLength + (m_LineMargin*3)); + } + + if (Digit & DECIMALSIGN) + { + Dc.DrawLine(XPos + m_LineLength + m_LineMargin*4, m_LineLength*2 + (m_LineMargin*5), + XPos + m_LineLength + m_LineMargin*4, m_LineLength*2 + (m_LineMargin*5)); } Dc.SetPen(wxNullPen); @@ -315,32 +326,60 @@ void wxLEDNumberCtrl::DrawDigit(wxDC &Dc, int Digit, int Column) void wxLEDNumberCtrl::RecalcInternals(const wxSize &CurrentSize) { + // Dimensions of LED segments + // + // Size of character is based on the HEIGH of the widget, NOT the width. + // Segment height is calculated as follows: + // Each segment is m_LineLength pixels long. + // There is m_LineMargin pixels at the top and bottom of each line segment + // There is m_LineMargin pixels at the top and bottom of each digit + // + // Therefore, the heigth of each character is: + // m_LineMargin : Top digit boarder + // m_LineMargin+m_LineLength+m_LineMargin : Top half of segment + // m_LineMargin+m_LineLength+m_LineMargin : Bottom half of segment + // m_LineMargin : Bottom digit boarder + // ---------------------- + // m_LineMargin*6 + m_LineLength*2 == Total height of digit. + // Therefore, (m_LineMargin*6 + m_LineLength*2) must equal Height + // + // Spacing between characters can then be calculated as follows: + // m_LineMargin : before the digit, + // m_LineMargin+m_LineLength+m_LineMargin : for the digit width + // m_LineMargin : after the digit + // = m_LineMargin*4 + m_LineLength const int Height = CurrentSize.GetHeight(); - if ((Height * 0.07) < 1) + if ((Height * 0.075) < 1) m_LineMargin = 1; else - m_LineMargin = (int)(Height * 0.07); + m_LineMargin = (int)(Height * 0.075); - if ((Height * 0.35) < 1) + if ((Height * 0.275) < 1) m_LineLength = 1; else - m_LineLength = (int)(Height * 0.35); + m_LineLength = (int)(Height * 0.275); m_LineWidth = m_LineMargin; m_DigitMargin = m_LineMargin * 4; - const int ValueWidth = (m_LineLength + m_DigitMargin) * m_Value.Len(); + // Count the number of characters in the string; '.' characters are not + // included because they do not take up space in the display + int count = 0; + for (unsigned int i = 0; i < m_Value.Len(); i++) + if (m_Value.GetChar(i) != '.') + count++; + const int ValueWidth = (m_LineLength + m_DigitMargin) * count; const int ClientWidth = CurrentSize.GetWidth(); switch (m_Alignment) { case wxLED_ALIGN_LEFT : - m_LeftStartPos = 0; + m_LeftStartPos = m_LineMargin; break; case wxLED_ALIGN_RIGHT : - m_LeftStartPos = ClientWidth - ValueWidth; + m_LeftStartPos = ClientWidth - ValueWidth - m_LineMargin; break; case wxLED_ALIGN_CENTER : m_LeftStartPos = (ClientWidth - ValueWidth) / 2; diff --git a/docs/changes.txt b/docs/changes.txt index 8616b050af..8f6c1f714e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -207,6 +207,7 @@ All: - added wxMicroSleep() and wxMilliSleep() replacing deprecated wxUsleep() - basic UDP sockets support (Lenny Maiorani) - fixed wxDateTime::GetWeekDayName() for some dates (Daniel Kaps) +- support for comma in contrib gizmo wxLEDNumberCtrl (Grant Likely) All (GUI): -- 2.45.2