Add a wxHtmlTag helper parsing both absolute values and percents.
This allows to avoid some code duplication in different handlers. Closes #14868. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73142 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -104,6 +104,8 @@ public:
|
|||||||
// Convenience functions:
|
// Convenience functions:
|
||||||
bool GetParamAsColour(const wxString& par, wxColour *clr) const;
|
bool GetParamAsColour(const wxString& par, wxColour *clr) const;
|
||||||
bool GetParamAsInt(const wxString& par, int *clr) const;
|
bool GetParamAsInt(const wxString& par, int *clr) const;
|
||||||
|
bool GetParamAsIntOrPercent(const wxString& param,
|
||||||
|
int* value, bool& isPercent) const;
|
||||||
|
|
||||||
// Scans param like scanf() functions family does.
|
// Scans param like scanf() functions family does.
|
||||||
// Example : ScanParam("COLOR", "\"#%X\"", &clr);
|
// Example : ScanParam("COLOR", "\"#%X\"", &clr);
|
||||||
|
@@ -587,6 +587,38 @@ bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
wxHtmlTag::GetParamAsIntOrPercent(const wxString& par,
|
||||||
|
int* value,
|
||||||
|
bool& isPercent) const
|
||||||
|
{
|
||||||
|
const wxString param = GetParam(par);
|
||||||
|
if ( param.empty() )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
wxString num;
|
||||||
|
if ( param.EndsWith("%", &num) )
|
||||||
|
{
|
||||||
|
isPercent = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isPercent = false;
|
||||||
|
num = param;
|
||||||
|
}
|
||||||
|
|
||||||
|
long lValue;
|
||||||
|
if ( !num.ToLong(&lValue) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ( lValue > INT_MAX || lValue < INT_MIN )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*value = static_cast<int>(lValue);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
wxString wxHtmlTag::GetAllParams() const
|
wxString wxHtmlTag::GetAllParams() const
|
||||||
{
|
{
|
||||||
// VS: this function is for backward compatibility only,
|
// VS: this function is for backward compatibility only,
|
||||||
|
@@ -676,16 +676,16 @@ TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
|
|||||||
|
|
||||||
if (tag.HasParam(wxT("WIDTH")))
|
if (tag.HasParam(wxT("WIDTH")))
|
||||||
{
|
{
|
||||||
wxString param = tag.GetParam(wxT("WIDTH"));
|
if (tag.GetParamAsIntOrPercent(wxT("WIDTH"), &w, wpercent))
|
||||||
wxSscanf(param.c_str(), wxT("%i"), &w);
|
{
|
||||||
if (param.EndsWith(wxT("%"))) {
|
if (wpercent)
|
||||||
if (w < 0)
|
{
|
||||||
w = 0;
|
if (w < 0)
|
||||||
else if (w > 100)
|
w = 0;
|
||||||
w = 100;
|
else if (w > 100)
|
||||||
wpercent = true;
|
w = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tag.HasParam(wxT("HEIGHT")))
|
if (tag.HasParam(wxT("HEIGHT")))
|
||||||
|
@@ -702,19 +702,18 @@ TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
|
|||||||
{
|
{
|
||||||
if (tag.HasParam(wxT("WIDTH")))
|
if (tag.HasParam(wxT("WIDTH")))
|
||||||
{
|
{
|
||||||
wxString wd = tag.GetParam(wxT("WIDTH"));
|
int width = 0;
|
||||||
|
bool wpercent = false;
|
||||||
if (!wd.empty() && wd[wd.length()-1] == wxT('%'))
|
if (tag.GetParamAsIntOrPercent(wxT("WIDTH"), &width, wpercent))
|
||||||
{
|
{
|
||||||
int width = 0;
|
if (wpercent)
|
||||||
wxSscanf(wd.c_str(), wxT("%i%%"), &width);
|
{
|
||||||
m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT);
|
m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int width = 0;
|
m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS);
|
||||||
wxSscanf(wd.c_str(), wxT("%i"), &width);
|
}
|
||||||
m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user