26#include <wx/stattext.h>
27#include <wx/textentry.h>
36 wxWindow* aValueCtrl, wxStaticText* aUnitLabel ) :
43 wxWindow* aEventSource, wxStaticText* aLabel,
44 wxWindow* aValueCtrl, wxStaticText* aUnitLabel ) :
49 m_iuScale( &aUnitsProvider->GetIuScale() ),
145 text.Trim(
true ).Trim(
false );
146 return text.IsEmpty();
217 std::optional<int> offset;
218 std::optional<double> ratio;
253 std::optional<double>& aRatio )
const
255 aOffset = std::nullopt;
256 aRatio = std::nullopt;
258 wxString input = aInput;
259 input.Trim(
true ).Trim(
false );
261 if( input.IsEmpty() )
269 bool lastWasOperator =
true;
271 for(
size_t i = 0; i < input.Length(); ++i )
273 wxChar ch = input[i];
275 if( ( ch ==
'+' || ch ==
'-' ) && !lastWasOperator )
281 lastWasOperator =
true;
286 lastWasOperator = ( ch ==
'+' || ch ==
'-' );
292 wxString currentTerm;
294 for(
size_t i = 0; i < normalized.Length(); ++i )
296 wxChar ch = normalized[i];
300 if( !currentTerm.IsEmpty() )
302 currentTerm.Trim(
true ).Trim(
false );
303 terms.Add( currentTerm );
313 if( !currentTerm.IsEmpty() )
315 currentTerm.Trim(
true ).Trim(
false );
316 terms.Add( currentTerm );
320 double totalOffset = 0.0;
321 double totalRatio = 0.0;
322 bool hasOffset =
false;
323 bool hasRatio =
false;
326 for(
const wxString& term : terms )
333 else if( term ==
"-" )
340 if( term.EndsWith(
"%" ) )
342 wxString numPart = term.Left( term.Length() - 1 );
343 numPart.Trim(
true ).Trim(
false );
346 if( numPart.ToDouble( &value ) )
348 totalRatio +=
sign * value / 100.0;
362 if(
result.ToDouble( &value ) )
366 totalOffset +=
sign * iuValue;
376 aOffset =
static_cast<int>( totalOffset );
381 return hasOffset || hasRatio || input.IsEmpty();
386 std::optional<double> aRatio )
const
391 if( aOffset.has_value() && aOffset.value() != 0 )
397 if( aRatio.has_value() &&
std::abs( aRatio.value() ) > 1e-9 )
399 double percent = aRatio.value() * 100.0;
405 result += wxString::Format(
" + %.4g%%", percent );
407 result += wxString::Format(
" - %.4g%%", -percent );
411 result = wxString::Format(
"%.4g%%", percent );
421 wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>(
m_valueCtrl );
424 return textEntry->GetValue();
426 return wxEmptyString;
432 wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>(
m_valueCtrl );
435 textEntry->ChangeValue( aValue );
The base frame for deriving all KiCad main window classes.
The base class for create windows for drawing purpose.
bool IsNull() const
Return true if the control holds no value (ie: empty string).
std::optional< int > GetOffsetValue() const
Get the absolute offset value (in Internal Units).
MARGIN_OFFSET_BINDER(EDA_DRAW_FRAME *aParent, wxStaticText *aLabel, wxWindow *aValueCtrl, wxStaticText *aUnitLabel)
std::optional< double > GetRatioValue() const
Get the ratio value as a fraction (e.g., -0.05 for -5%).
const EDA_IU_SCALE * m_iuScale
virtual ~MARGIN_OFFSET_BINDER() override
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
void onSetFocus(wxFocusEvent &aEvent)
bool parseInput(const wxString &aInput, std::optional< int > &aOffset, std::optional< double > &aRatio) const
Parse the input string and extract offset and ratio values.
wxStaticText * m_unitLabel
Can be nullptr.
void SetRatioValue(std::optional< double > aRatio)
Set the ratio value as a fraction (e.g., -0.05 for -5%).
std::optional< int > m_cachedOffset
void Show(bool aShow, bool aResize=false)
Show/hide the label, widget and units label.
std::optional< double > m_cachedRatio
wxString getTextValue() const
Get the current text from the control.
void onKillFocus(wxFocusEvent &aEvent)
void setTextValue(const wxString &aValue)
Set the text in the control without triggering events.
wxString formatValue(std::optional< int > aOffset, std::optional< double > aRatio) const
Format the offset and ratio values into a display string.
void onUnitsChanged(wxCommandEvent &aEvent)
void SetOffsetValue(std::optional< int > aValue)
Set the absolute offset value (in Internal Units).
bool Process(const wxString &aString)
const EDA_IU_SCALE & GetIuScale() const
EDA_UNITS GetUserUnits() const
KICOMMON_API long long int ValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue in aUnits to internal units used by the application.
KICOMMON_API wxString StringFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Return the string from aValue according to aUnits (inch, mm ...) for display.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
int GetUserUnits()
Return the currently selected user unit value for the interface.
wxString result
Test unit parsing edge cases and error handling.
constexpr int sign(T val)