nux-1.14.0
SpinBox.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 #include "Nux.h"
00023 #include "TimerProc.h"
00024 #include "HLayout.h"
00025 #include "VLayout.h"
00026 #include "IntegerValidator.h"
00027 #include "SpinBox.h"
00028 
00029 namespace nux
00030 {
00031 
00032   const Color SPINBOX_BUTTON_COLOR = Color (0xFF4D4D4D);
00033   const Color SPINBOX_BUTTON_MOUSEOVER_COLOR = Color (0xFF222222);
00034 
00035   SpinBox::SpinBox (int Value, int Step, int MinValue, int MaxValue, NUX_FILE_LINE_DECL)
00036     : SpinBox_Logic (NUX_FILE_LINE_PARAM)
00037     , m_IntValidator (MinValue, MaxValue)
00038     , m_Step (Step)
00039   {
00040     hlayout = new HLayout (NUX_TRACKER_LOCATION);
00041     vlayout = new VLayout (NUX_TRACKER_LOCATION);
00042 
00043     m_EditLine->SetValidator (&m_IntValidator);
00044     m_EditLine->SetSuffix (TEXT ("") );
00045     m_EditLine->SetPrefix (TEXT ("") );
00046     m_EditLine->SetText (NString::Printf (TEXT ("%d"), m_IntValidator.GetMinimum() ) );
00047 
00048     m_EditLine->SetMinimumSize (1.5 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT);
00049     m_EditLine->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT) );
00050 
00051     m_SpinnerUpBtn->SetMinimumSize (15, 10);
00052     m_SpinnerUpBtn->SetGeometry (Geometry (0, 0, 15, 10) );
00053     m_SpinnerDownBtn->SetMinimumSize (15, 10);
00054     m_SpinnerDownBtn->SetGeometry (Geometry (0, 0, 15, 10) );
00055 
00056     // Set the minimum size of this widget.
00057     // This is use by TextLineEditPropertyItem::GetItemBestHeight
00058     SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00059 
00060     hlayout->AddView (m_EditLine, 1);
00061 
00062     vlayout->AddView(m_SpinnerUpBtn, 1);
00063     vlayout->AddView(m_SpinnerDownBtn, 1);
00064     hlayout->AddLayout(vlayout, 0);
00065 
00066     SetLayout(hlayout);
00067 
00068     SetValue (Value);
00069   }
00070 
00071   SpinBox::~SpinBox()
00072   {
00073   }
00074 
00075   void SpinBox::InitializeWidgets()
00076   {
00077   }
00078 
00079   void SpinBox::InitializeLayout()
00080   {
00081   }
00082 
00083   long SpinBox::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00084   {
00085     long ret = TraverseInfo;
00086     ret = m_SpinnerUpBtn->OnEvent (ievent, ret, ProcessEventInfo);
00087     ret = m_SpinnerDownBtn->OnEvent (ievent, ret, ProcessEventInfo);
00088     ret = m_EditLine->ProcessEvent (ievent, ret, ProcessEventInfo);
00089     ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
00090     return ret;
00091   }
00092 
00093   void SpinBox::Draw (GraphicsEngine &GfxContext, bool force_draw)
00094   {
00095     Geometry base = GetGeometry();
00096     GetPainter().PaintBackground (GfxContext, base);
00097 
00098     if (m_EditLine->IsMouseInside() || m_SpinnerUpBtn->IsMouseInside() || m_SpinnerDownBtn->IsMouseInside() )
00099     {
00100 
00101       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerUpBtn->GetGeometry(), SPINBOX_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00102                                  eCornerTopRight, false);
00103       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerDownBtn->GetGeometry(), SPINBOX_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00104                                  eCornerBottomRight, false);
00105     }
00106     else
00107     {
00108       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerUpBtn->GetGeometry(), SPINBOX_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00109                                  eCornerTopRight, false);
00110       GetPainter().PaintShapeCorner (GfxContext, m_SpinnerDownBtn->GetGeometry(), SPINBOX_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00111                                  eCornerBottomRight, false);
00112     }
00113 
00114     GeometryPositioning gp (eHACenter, eVACenter);
00115     Geometry GeoPo = ComputeGeometryPositioning (m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry (eSPINER_UP), gp);
00116 
00117     if (m_SpinnerUpBtn->IsMouseInside() )
00118       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_UP);
00119     else
00120       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_UP);
00121 
00122 
00123     gp.SetAlignment (eHACenter, eVACenter);
00124     GeoPo = ComputeGeometryPositioning (m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry (eSPINER_DOWN), gp);
00125 
00126     if (m_SpinnerDownBtn->IsMouseInside() )
00127       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_DOWN);
00128     else
00129       GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eSPINER_DOWN);
00130 
00131     m_EditLine->QueueDraw();
00132   }
00133 
00134   void SpinBox::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00135   {
00136     m_EditLine->ProcessDraw (GfxContext, force_draw);
00137   }
00138 
00139   void SpinBox::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00140   {
00141 
00142   }
00143 
00144   void SpinBox::SetValue (int value)
00145   {
00146     m_iValue = m_IntValidator.GetClampedValue (value);
00147     m_EditLine->SetText (NString::Printf ("%d", m_iValue) );
00148     sigValueChanged.emit (this);
00149     sigValue.emit (m_iValue);
00150     QueueDraw();
00151   }
00152 
00153   int SpinBox::GetValue() const
00154   {
00155     return m_iValue;
00156   }
00157 
00158   void SpinBox::SetStep (int i)
00159   {
00160     m_Step = i;
00161 
00162     if (m_Step <= 0)
00163       m_Step = 1;
00164 
00165     QueueDraw();
00166   }
00167 
00168   int SpinBox::GetStep() const
00169   {
00170     return m_Step;
00171   }
00172 
00173   int SpinBox::GetMinValue() const
00174   {
00175     return m_IntValidator.GetMinimum();
00176   }
00177 
00178   int SpinBox::GetMaxValue() const
00179   {
00180     return m_IntValidator.GetMaximum();
00181   }
00182 
00183   void SpinBox::SetRange (int MinValue, int Maxvalue)
00184   {
00185     m_IntValidator.SetMinimum (MinValue);
00186     m_IntValidator.SetMaximum (Maxvalue);
00187     m_iValue = m_IntValidator.GetClampedValue (m_iValue);
00188     sigValueChanged.emit (this);
00189     sigValue.emit (m_iValue);
00190     QueueDraw();
00191   }
00192 
00193   void SpinBox::ImplementIncrementBtn()
00194   {
00195     SetValue (m_iValue + m_Step);
00196 
00197     if (m_iValue < m_IntValidator.GetMaximum() )
00198     {
00199       if (m_UpTimerHandler.IsValid() )
00200         m_UpTimerHandler = GetTimer().AddTimerHandler (100, m_UpTimerCallback, 0);
00201       else
00202         m_UpTimerHandler = GetTimer().AddTimerHandler (800, m_UpTimerCallback, 0);
00203 
00204       QueueDraw();
00205     }
00206 
00207     sigValueChanged.emit (this);
00208     sigIncrement.emit (this);
00209     sigValue.emit (m_iValue);
00210   }
00211 
00212   void SpinBox::ImplementDecrementBtn()
00213   {
00214     SetValue (m_iValue - m_Step);
00215 
00216     if (m_iValue > m_IntValidator.GetMinimum() )
00217     {
00218       if (m_DownTimerHandler.IsValid() )
00219         m_DownTimerHandler = GetTimer().AddTimerHandler (100, m_DownTimerCallback, 0);
00220       else
00221         m_DownTimerHandler = GetTimer().AddTimerHandler (800, m_DownTimerCallback, 0);
00222 
00223       QueueDraw();
00224     }
00225 
00226     sigValueChanged.emit (this);
00227     sigDecrement.emit (this);
00228     sigValue.emit (m_iValue);
00229   }
00230 
00231   void SpinBox::ImplementValidateEntry()
00232   {
00233     double ret = 0;
00234     ret = CharToDouble (m_EditLine->GetCleanText().GetTCharPtr() );
00235     {
00236       m_iValue = m_IntValidator.GetClampedValue (ret);
00237       m_EditLine->SetText (NString::Printf ("%d", m_iValue) );
00238       sigValueChanged.emit (this);
00239       sigValue.emit (m_iValue);
00240 //
00241 //        if(m_iValue < m_IntValidator.GetMinimum())
00242 //        {
00243 //            m_iValue = m_IntValidator.GetMinimum();
00244 //            m_EditLine->SetText(NString::Printf("%d", m_iValue));
00245 //        }
00246 //        if(m_iValue > m_IntValidator.GetMaximum())
00247 //        {
00248 //            m_iValue = m_IntValidator.GetMaximum();
00249 //            m_EditLine->SetText(NString::Printf("%d", m_iValue));
00250 //        }
00251     }
00252 //     else
00253 //     {
00254 //         m_EditLine->SetText(NString::Printf("%d", m_iValue));
00255 //         sigValueChanged.emit(this);
00256 //         sigValue.emit(m_iValue);
00257 //     }
00258   }
00259 
00260 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends