nux-1.16.0
|
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 #include "EditTextBox.h" 00025 #include "HLayout.h" 00026 #include "RangeValueInteger.h" 00027 00028 namespace nux 00029 { 00030 00031 RangeValueInteger::RangeValueInteger (int Value, int MinValue, int MaxValue, NUX_FILE_LINE_DECL) 00032 : View (NUX_FILE_LINE_PARAM) 00033 { 00034 m_min = MinValue; 00035 m_max = MaxValue; 00036 m_StartColor = Color (0xff202020); 00037 m_EndColor = Color (0xff202020); 00038 m_ProgressColor = Color (0xff606060); 00039 m_EnableDrawProgress = true; 00040 m_CTRL_KEY = 0; 00041 m_MarkerPosition = 0; 00042 00043 InitializeLayout(); 00044 InitializeWidgets(); 00045 00046 SetValue (Value); 00047 } 00048 00049 RangeValueInteger::~RangeValueInteger() 00050 { 00051 } 00052 00053 void RangeValueInteger::InitializeWidgets() 00054 { 00056 // Set Signals // 00058 m_Percentage->mouse_down.connect ( sigc::mem_fun (this, &RangeValueInteger::OnReceiveMouseDown) ); 00059 m_Percentage->mouse_up.connect ( sigc::mem_fun (this, &RangeValueInteger::OnReceiveMouseUp) ); 00060 m_Percentage->mouse_drag.connect ( sigc::mem_fun (this, &RangeValueInteger::OnReceiveMouseDrag) ); 00061 00062 m_ValueString->sigValidateKeyboardEntry.connect (sigc::mem_fun (this, &RangeValueInteger::OnValidateKeyboardEntry) ); 00063 00065 // Set Geometry // 00067 m_ValueString->SetMinimumSize (DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT); 00068 m_ValueString->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) ); 00069 00070 m_Percentage->SetMinimumSize (2 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT); 00071 m_Percentage->SetGeometry (Geometry (0, 0, DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) ); 00072 00073 // Set the minimum size of this widget. 00074 // This is use by RangeValuePropertyItem::GetItemBestHeight 00075 SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT); 00076 00077 00078 hlayout->AddView (m_ValueString, 0, eCenter, eFull); 00079 hlayout->AddView (m_Percentage, 4, eCenter, eFull); 00080 //hlayout->AddLayout(&vlayout, 4); 00081 hlayout->SetHorizontalExternalMargin (0); 00082 hlayout->SetHorizontalInternalMargin (2); 00083 hlayout->SetVerticalExternalMargin (0); 00084 SetCompositionLayout (hlayout); 00085 } 00086 00087 void RangeValueInteger::InitializeLayout() 00088 { 00089 hlayout = new HLayout (NUX_TRACKER_LOCATION); 00090 m_Percentage = new InputArea (NUX_TRACKER_LOCATION); 00091 m_ValueString = new EditTextBox (TEXT (""), NUX_TRACKER_LOCATION); 00092 } 00093 00094 long RangeValueInteger::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo) 00095 { 00096 m_CTRL_KEY = ievent.GetVirtualKeyState (NUX_VK_LCONTROL); 00097 00098 long ret; 00099 ret = m_Percentage->OnEvent (ievent, TraverseInfo, ProcessEventInfo); 00100 ret = m_ValueString->ProcessEvent (ievent, ret, ProcessEventInfo); 00101 ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo); 00102 00103 if (m_ValueString->IsRedrawNeeded() ) 00104 { 00105 QueueDraw(); 00106 } 00107 00108 return ret; 00109 } 00110 00111 void RangeValueInteger::DrawMarker (GraphicsEngine &GfxContext) 00112 { 00113 int marker_position_x; 00114 int marker_position_y; 00115 00116 GfxContext.PushClippingRectangle (m_Percentage->GetGeometry() ); 00117 00118 marker_position_x = m_Percentage->GetBaseX() + (m_MarkerPosition - m_min) * m_Percentage->GetBaseWidth() * 1 / (m_max - m_min); 00119 marker_position_y = m_Percentage->GetBaseY() + m_Percentage->GetBaseHeight(); 00120 GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 5, marker_position_y, 00121 marker_position_x, marker_position_y - 5, 00122 marker_position_x + 5, marker_position_y, Color (0.0f, 0.0f, 0.0f, 1.0f) ); 00123 00124 GetPainter().Draw2DTriangleColor (GfxContext, marker_position_x - 4, marker_position_y, 00125 marker_position_x, marker_position_y - 4, 00126 marker_position_x + 4, marker_position_y, Color (0.7f, 0.7f, 0.7f, 1.0f) ); 00127 00128 GfxContext.PopClippingRectangle(); 00129 } 00130 00131 void RangeValueInteger::Draw (GraphicsEngine &GfxContext, bool force_draw) 00132 { 00133 Geometry base = GetGeometry(); 00134 00135 // Percentage 00136 Geometry P = m_Percentage->GetGeometry(); 00137 GetPainter().Paint2DQuadColor (GfxContext, P, m_StartColor, m_StartColor, m_EndColor, m_EndColor); 00138 00139 if (m_EnableDrawProgress) 00140 { 00141 P.SetWidth ( (m_MarkerPosition - m_min) * (float) P.GetWidth() / (m_max - m_min) ); 00142 GetPainter().Paint2DQuadColor (GfxContext, P, m_ProgressColor); 00143 } 00144 00145 DrawMarker (GfxContext); 00146 } 00147 00148 void RangeValueInteger::DrawContent (GraphicsEngine &GfxContext, bool force_draw) 00149 { 00150 m_ValueString->ProcessDraw (GfxContext, force_draw); 00151 } 00152 00153 void RangeValueInteger::PostDraw (GraphicsEngine &GfxContext, bool force_draw) 00154 { 00155 00156 } 00157 00158 void RangeValueInteger::SetRange (int min_value, int max_value) 00159 { 00160 if (min_value < max_value) 00161 { 00162 m_min = min_value; 00163 m_max = max_value; 00164 } 00165 else 00166 { 00167 m_min = max_value; 00168 m_max = min_value; 00169 } 00170 00171 if (m_Value < m_min) 00172 m_Value = m_min; 00173 else if (m_Value > m_max) 00174 m_Value = m_max; 00175 00176 SetValue (m_Value); 00177 } 00178 00179 void RangeValueInteger::SetValue (int value) 00180 { 00181 if (value < m_min) 00182 m_Value = m_min; 00183 else if (value > m_max) 00184 m_Value = m_max; 00185 else 00186 m_Value = value; 00187 00188 m_MarkerPosition = m_Value; 00189 m_ValueString->SetText (NString::Printf ("%d", m_Value) ); 00190 QueueDraw(); 00191 } 00192 00193 int RangeValueInteger::GetValue() const 00194 { 00195 return m_Value; 00196 } 00197 00199 // EMITTERS // 00201 void RangeValueInteger::OnReceiveMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags) 00202 { 00203 if (x < 0) 00204 { 00205 m_Value = m_min; 00206 m_MarkerPosition = m_Value; 00207 } 00208 else if (x > m_Percentage->GetBaseWidth() ) 00209 { 00210 m_Value = m_max; 00211 m_MarkerPosition = m_Value; 00212 } 00213 else 00214 { 00215 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00216 m_MarkerPosition = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00217 00218 if (m_MarkerPosition - m_Value > 0.5f) 00219 m_Value++; 00220 } 00221 00222 m_ValueString->SetText (NString::Printf ("%d", m_Value) ); 00223 sigValueChanged.emit (this); 00224 sigMouseDown.emit (m_Value); 00225 sigValueChanged2.emit (m_Value); 00226 00227 QueueDraw(); 00228 } 00229 00230 void RangeValueInteger::OnReceiveMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags) 00231 { 00232 if (x < 0) 00233 m_Value = m_min; 00234 else if (x > m_Percentage->GetBaseWidth() ) 00235 m_Value = m_max; 00236 else 00237 { 00238 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00239 00240 if (m_MarkerPosition - m_Value > 0.5f) 00241 m_Value++; 00242 } 00243 00244 m_MarkerPosition = m_Value; 00245 m_ValueString->SetText (NString::Printf ("%d", m_Value) ); 00246 sigValueChanged.emit (this); 00247 sigMouseUp.emit (m_Value); 00248 sigValueChanged2.emit (m_Value); 00249 00250 QueueDraw(); 00251 } 00252 00253 void RangeValueInteger::OnReceiveMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags) 00254 { 00255 if (x < 0) 00256 { 00257 m_Value = m_min; 00258 m_MarkerPosition = m_Value; 00259 } 00260 else if (x > m_Percentage->GetBaseWidth() ) 00261 { 00262 m_Value = m_max; 00263 m_MarkerPosition = m_Value; 00264 } 00265 else 00266 { 00267 m_Value = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00268 m_MarkerPosition = m_min + (m_max - m_min) * (float) x / (float) m_Percentage->GetBaseWidth(); 00269 00270 if (m_MarkerPosition - m_Value > 0.5f) 00271 m_Value++; 00272 } 00273 00274 m_ValueString->SetText (NString::Printf ("%d", m_Value) ); 00275 sigValueChanged.emit (this); 00276 sigMouseDrag.emit (m_Value); 00277 sigValueChanged2.emit (m_Value); 00278 00279 QueueDraw(); 00280 } 00281 00282 void RangeValueInteger::OnKeyboardFocus() 00283 { 00284 00285 } 00286 00287 void RangeValueInteger::OnLostKeyboardFocus() 00288 { 00289 00290 } 00291 00292 void RangeValueInteger::OnValidateKeyboardEntry (EditTextBox *textbox, const NString &text) 00293 { 00294 int i; 00295 i = CharToInteger (text.GetTCharPtr() ); 00296 SetValue (i); 00297 sigValueChanged.emit (this); 00298 sigSetTypedValue.emit (i); 00299 sigValueChanged2.emit (m_Value); 00300 QueueDraw(); 00301 } 00302 00303 void RangeValueInteger::BroadcastValue () 00304 { 00305 sigValueChanged2.emit (m_Value); 00306 } 00307 }