Back to index

nux  3.0.0
SpinBox.cpp
Go to the documentation of this file.
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("");
00045     m_EditLine->SetPrefix("");
00046     m_EditLine->SetText(NString::Printf("%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   void SpinBox::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00084   {
00085     Geometry base = GetGeometry();
00086     GetPainter().PaintBackground(graphics_engine, base);
00087 
00088     if (m_EditLine->IsMouseInside() || m_SpinnerUpBtn->IsMouseInside() || m_SpinnerDownBtn->IsMouseInside())
00089     {
00090 
00091       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerUpBtn->GetGeometry(), SPINBOX_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00092                                  eCornerTopRight, false);
00093       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerDownBtn->GetGeometry(), SPINBOX_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00094                                  eCornerBottomRight, false);
00095     }
00096     else
00097     {
00098       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerUpBtn->GetGeometry(), SPINBOX_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00099                                  eCornerTopRight, false);
00100       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerDownBtn->GetGeometry(), SPINBOX_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00101                                  eCornerBottomRight, false);
00102     }
00103 
00104     GeometryPositioning gp(eHACenter, eVACenter);
00105     Geometry GeoPo = ComputeGeometryPositioning(m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry(eSPINER_UP), gp);
00106 
00107     if (m_SpinnerUpBtn->IsMouseInside())
00108       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_UP);
00109     else
00110       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_UP);
00111 
00112 
00113     gp.SetAlignment(eHACenter, eVACenter);
00114     GeoPo = ComputeGeometryPositioning(m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry(eSPINER_DOWN), gp);
00115 
00116     if (m_SpinnerDownBtn->IsMouseInside())
00117       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_DOWN);
00118     else
00119       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_DOWN);
00120 
00121     m_EditLine->QueueDraw();
00122   }
00123 
00124   void SpinBox::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00125   {
00126     m_EditLine->ProcessDraw(graphics_engine, force_draw);
00127   }
00128 
00129   void SpinBox::PostDraw(GraphicsEngine &graphics_engine, bool force_draw)
00130   {
00131 
00132   }
00133 
00134   void SpinBox::SetValue(int value)
00135   {
00136     m_iValue = m_IntValidator.GetClampedValue(value);
00137     m_EditLine->SetText(NString::Printf("%d", m_iValue));
00138     sigValueChanged.emit(this);
00139     sigValue.emit(m_iValue);
00140     QueueDraw();
00141   }
00142 
00143   int SpinBox::GetValue() const
00144   {
00145     return m_iValue;
00146   }
00147 
00148   void SpinBox::SetStep(int i)
00149   {
00150     m_Step = i;
00151 
00152     if (m_Step <= 0)
00153       m_Step = 1;
00154 
00155     QueueDraw();
00156   }
00157 
00158   int SpinBox::GetStep() const
00159   {
00160     return m_Step;
00161   }
00162 
00163   int SpinBox::GetMinValue() const
00164   {
00165     return m_IntValidator.GetMinimum();
00166   }
00167 
00168   int SpinBox::GetMaxValue() const
00169   {
00170     return m_IntValidator.GetMaximum();
00171   }
00172 
00173   void SpinBox::SetRange(int MinValue, int Maxvalue)
00174   {
00175     m_IntValidator.SetMinimum(MinValue);
00176     m_IntValidator.SetMaximum(Maxvalue);
00177     m_iValue = m_IntValidator.GetClampedValue(m_iValue);
00178     sigValueChanged.emit(this);
00179     sigValue.emit(m_iValue);
00180     QueueDraw();
00181   }
00182 
00183   void SpinBox::ImplementIncrementBtn()
00184   {
00185     SetValue(m_iValue + m_Step);
00186 
00187     if (m_iValue < m_IntValidator.GetMaximum())
00188     {
00189       if (m_UpTimerHandler.IsValid())
00190         m_UpTimerHandler = GetTimer().AddTimerHandler(100, m_UpTimerCallback, 0);
00191       else
00192         m_UpTimerHandler = GetTimer().AddTimerHandler(800, m_UpTimerCallback, 0);
00193 
00194       QueueDraw();
00195     }
00196 
00197     sigValueChanged.emit(this);
00198     sigIncrement.emit(this);
00199     sigValue.emit(m_iValue);
00200   }
00201 
00202   void SpinBox::ImplementDecrementBtn()
00203   {
00204     SetValue(m_iValue - m_Step);
00205 
00206     if (m_iValue > m_IntValidator.GetMinimum())
00207     {
00208       if (m_DownTimerHandler.IsValid())
00209         m_DownTimerHandler = GetTimer().AddTimerHandler(100, m_DownTimerCallback, 0);
00210       else
00211         m_DownTimerHandler = GetTimer().AddTimerHandler(800, m_DownTimerCallback, 0);
00212 
00213       QueueDraw();
00214     }
00215 
00216     sigValueChanged.emit(this);
00217     sigDecrement.emit(this);
00218     sigValue.emit(m_iValue);
00219   }
00220 
00221   void SpinBox::ImplementValidateEntry()
00222   {
00223     double ret = 0;
00224     ret = CharToDouble(m_EditLine->GetCleanText().GetTCharPtr());
00225     {
00226       m_iValue = m_IntValidator.GetClampedValue(ret);
00227       m_EditLine->SetText(NString::Printf("%d", m_iValue));
00228       sigValueChanged.emit(this);
00229       sigValue.emit(m_iValue);
00230 //
00231 //        if (m_iValue < m_IntValidator.GetMinimum())
00232 //        {
00233 //            m_iValue = m_IntValidator.GetMinimum();
00234 //            m_EditLine->SetText(NString::Printf("%d", m_iValue));
00235 //        }
00236 //        if (m_iValue > m_IntValidator.GetMaximum())
00237 //        {
00238 //            m_iValue = m_IntValidator.GetMaximum();
00239 //            m_EditLine->SetText(NString::Printf("%d", m_iValue));
00240 //        }
00241     }
00242 //     else
00243 //     {
00244 //         m_EditLine->SetText(NString::Printf("%d", m_iValue));
00245 //         sigValueChanged.emit(this);
00246 //         sigValue.emit(m_iValue);
00247 //     }
00248   }
00249 
00250 }