nux-1.14.0
SpinBoxDouble.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 
00025 #include "TimerProc.h"
00026 #include "HLayout.h"
00027 #include "VLayout.h"
00028 #include "DoubleValidator.h"
00029 #include "SpinBox_Logic.h"
00030 
00031 #include "SpinBoxDouble.h"
00032 namespace nux
00033 {
00034   const Color SPINBOX_DOUBLE_BUTTON_COLOR = Color (0xFF4D4D4D);
00035   const Color SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR = Color (0xFF222222);
00036 
00037   SpinBoxDouble::SpinBoxDouble (double Value, double Step, double MinValue, double MaxValue, NUX_FILE_LINE_DECL)
00038     : SpinBox_Logic (NUX_FILE_LINE_PARAM)
00039     , m_DoubleValidator (MinValue, MaxValue)
00040     , m_Step (Step)
00041   {
00042     InitializeLayout();
00043     InitializeWidgets();
00044     SetValue (Value);
00045   }
00046 
00047   SpinBoxDouble::~SpinBoxDouble()
00048   {
00049   }
00050 
00051   void SpinBoxDouble::InitializeWidgets()
00052   {
00053     m_EditLine->SetValidator (&m_DoubleValidator);
00054     m_EditLine->SetSuffix (TEXT ("") );
00055     m_EditLine->SetPrefix (TEXT ("") );
00056     m_EditLine->SetText (NString::Printf (TEXT ("%.3f"), m_DoubleValidator.GetMinimum() ) );
00057 
00058     m_EditLine->SetMinimumSize (1.5 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT);
00059     m_EditLine->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT) );
00060 
00061     m_SpinnerUpBtn->SetMinimumSize (15, 10);
00062     m_SpinnerUpBtn->SetGeometry (Geometry (0, 0, 15, 10) );
00063     m_SpinnerDownBtn->SetMinimumSize (15, 10);
00064     m_SpinnerDownBtn->SetGeometry (Geometry (0, 0, 15, 10) );
00065 
00066 
00067     // Set the minimum size of this widget.
00068     // This is use by TextLineEditPropertyItem::GetItemBestHeight
00069     SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00070 
00071     m_hlayout->AddView (m_EditLine, 1);
00072 
00073     m_vlayout->AddView (m_SpinnerUpBtn, 1);
00074     m_vlayout->AddView (m_SpinnerDownBtn, 1);
00075     m_hlayout->AddLayout (m_vlayout, 0);
00076 
00077     SetCompositionLayout (m_hlayout);
00078   }
00079 
00080   void SpinBoxDouble::InitializeLayout()
00081   {
00082     m_hlayout = new HLayout (NUX_TRACKER_LOCATION);
00083     m_vlayout = new VLayout (NUX_TRACKER_LOCATION);
00084   }
00085 
00086   long SpinBoxDouble::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00087   {
00088     long ret = TraverseInfo;
00089     ret = m_SpinnerUpBtn->OnEvent (ievent, ret, ProcessEventInfo);
00090     ret = m_SpinnerDownBtn->OnEvent (ievent, ret, ProcessEventInfo);
00091     ret = m_EditLine->ProcessEvent (ievent, ret, ProcessEventInfo);
00092     ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
00093     return ret;
00094   }
00095 
00096   void SpinBoxDouble::Draw (GraphicsEngine &GfxContext, bool force_draw)
00097   {
00098     Geometry base = GetGeometry();
00099     GetPainter().PaintBackground (GfxContext, base);
00100 
00101     if (m_EditLine->IsMouseInside() || m_SpinnerUpBtn->IsMouseInside() || m_SpinnerDownBtn->IsMouseInside() )
00102     {
00103 
00104       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerUpBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00105                                  eCornerTopRight, false);
00106       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerDownBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00107                                  eCornerBottomRight, false);
00108     }
00109     else
00110     {
00111       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerUpBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00112                                  eCornerTopRight, false);
00113       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerDownBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00114                                  eCornerBottomRight, false);
00115     }
00116 
00117     GeometryPositioning gp (eHACenter, eVACenter);
00118     Geometry GeoPo = ComputeGeometryPositioning (m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry (eSPINER_UP), gp);
00119 
00120     if (m_SpinnerUpBtn->IsMouseInside() )
00121       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_UP);
00122     else
00123       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_UP);
00124 
00125 
00126     gp.SetAlignment (eHACenter, eVACenter);
00127     GeoPo = ComputeGeometryPositioning (m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry (eSPINER_DOWN), gp);
00128 
00129     if (m_SpinnerDownBtn->IsMouseInside() )
00130       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_DOWN);
00131     else
00132       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_DOWN);
00133 
00134     m_EditLine->QueueDraw();
00135   }
00136 
00137   void SpinBoxDouble::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00138   {
00139     m_EditLine->ProcessDraw (GfxContext, force_draw);
00140   }
00141 
00142   void SpinBoxDouble::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00143   {
00144 
00145   }
00146 
00147   void SpinBoxDouble::SetValue (double value)
00148   {
00149     m_Value = m_DoubleValidator.GetClampedValue (value);
00150 //    if(m_Value < m_DoubleValidator.GetMinimum())
00151 //        m_Value = m_DoubleValidator.GetMinimum();
00152 //    if(m_Value > m_DoubleValidator.GetMaximum())
00153 //        m_Value = m_DoubleValidator.GetMaximum();
00154     m_EditLine->SetText (NString::Printf ("%.3f", m_Value));
00155     sigValueChanged.emit (this);
00156     sigValue.emit (m_Value);
00157     QueueDraw();
00158   }
00159 
00160   double SpinBoxDouble::GetValue() const
00161   {
00162     return m_Value;
00163   }
00164 
00165   void SpinBoxDouble::SetStep (double d)
00166   {
00167     m_Step = d;
00168 
00169     if (m_Step <= 0)
00170       m_Step = 1.0;
00171 
00172     QueueDraw();
00173   }
00174 
00175   double SpinBoxDouble::GetStep() const
00176   {
00177     return m_Step;
00178   }
00179 
00180   double SpinBoxDouble::GetMinValue() const
00181   {
00182     return m_DoubleValidator.GetMinimum();
00183   }
00184 
00185   double SpinBoxDouble::GetMaxValue() const
00186   {
00187     return m_DoubleValidator.GetMaximum();
00188   }
00189 
00190   void SpinBoxDouble::SetRange (double MinValue, double Maxvalue)
00191   {
00192     m_DoubleValidator.SetMinimum (MinValue);
00193     m_DoubleValidator.SetMaximum (Maxvalue);
00194     m_Value = m_DoubleValidator.GetClampedValue (m_Value);
00195     sigValueChanged.emit (this);
00196     sigValue.emit (m_Value);
00197     QueueDraw();
00198   }
00199 
00200   void SpinBoxDouble::ImplementIncrementBtn()
00201   {
00202     SetValue (m_Value + m_Step);
00203 
00204     if (m_Value < m_DoubleValidator.GetMaximum() )
00205     {
00206       if (m_UpTimerHandler.IsValid() )
00207         m_UpTimerHandler = GetTimer().AddTimerHandler (100, m_UpTimerCallback, 0);
00208       else
00209         m_UpTimerHandler = GetTimer().AddTimerHandler (800, m_UpTimerCallback, 0);
00210 
00211       QueueDraw();
00212     }
00213 
00214     sigValueChanged.emit (this);
00215     sigIncrement.emit (this);
00216     sigValue.emit (m_Value);
00217   }
00218 
00219   void SpinBoxDouble::ImplementDecrementBtn()
00220   {
00221     SetValue (m_Value - m_Step);
00222 
00223     if (m_Value > m_DoubleValidator.GetMinimum() )
00224     {
00225       if (m_DownTimerHandler.IsValid() )
00226         m_DownTimerHandler = GetTimer().AddTimerHandler (100, m_DownTimerCallback, 0);
00227       else
00228         m_DownTimerHandler = GetTimer().AddTimerHandler (800, m_DownTimerCallback, 0);
00229 
00230       QueueDraw();
00231     }
00232 
00233     sigValueChanged.emit (this);
00234     sigDecrement.emit (this);
00235     sigValue.emit (m_Value);
00236   }
00237 
00238   void SpinBoxDouble::ImplementValidateEntry()
00239   {
00240     double ret = 0;
00241     ret = CharToDouble (m_EditLine->GetCleanText().GetTCharPtr() );
00242     {
00243       m_Value = m_DoubleValidator.GetClampedValue (ret);
00244       m_EditLine->SetText (NString::Printf ("%.3f", m_Value) );
00245       sigValueChanged.emit (this);
00246       sigValue.emit (m_Value);
00247 //
00248 //        if(m_Value < m_DoubleValidator.GetMinimum())
00249 //        {
00250 //            m_Value = m_DoubleValidator.GetMinimum();
00251 //            m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00252 //        }
00253 //        if(m_Value > m_DoubleValidator.GetMaximum())
00254 //        {
00255 //            m_Value = m_DoubleValidator.GetMaximum();
00256 //            m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00257 //        }
00258     }
00259 //     else
00260 //     {
00261 //         m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00262 //         sigValueChanged.emit(this);
00263 //         sigValue.emit(m_Value);
00264 //     }
00265   }
00266 
00267 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends