]> git.saurik.com Git - wxWidgets.git/commitdiff
Add a wxHtmlTag helper parsing both absolute values and percents.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 8 Dec 2012 00:37:21 +0000 (00:37 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 8 Dec 2012 00:37:21 +0000 (00:37 +0000)
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

include/wx/html/htmltag.h
src/html/htmltag.cpp
src/html/m_image.cpp
src/html/m_tables.cpp

index 35ab27a6c54ecad78b5d6bf37c6815c44b526266..4ff2164beb4ab2a99e8835abc073177c5a98fdaf 100644 (file)
@@ -104,6 +104,8 @@ public:
     // Convenience functions:
     bool GetParamAsColour(const wxString& par, wxColour *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.
     // Example : ScanParam("COLOR", "\"#%X\"", &clr);
index 7bf1fa8d2e302317e3ecc08e46600f9f6ca57517..bf5b298a46140e60b5c7075105be00e7e44d7613 100644 (file)
@@ -587,6 +587,38 @@ bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
     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
 {
     // VS: this function is for backward compatibility only,
index 87aa320f80a392c70eb2c691fc00d08201df45ab..6394fa1b2bec48061ff04142f6f15e2a164c12b8 100644 (file)
@@ -676,16 +676,16 @@ TAG_HANDLER_BEGIN(IMG, "IMG,MAP,AREA")
 
                 if (tag.HasParam(wxT("WIDTH")))
                 {
-                    wxString param = tag.GetParam(wxT("WIDTH"));
-                    wxSscanf(param.c_str(), wxT("%i"), &w);
-                    if (param.EndsWith(wxT("%"))) {
-                        if (w < 0)
-                            w = 0;
-                        else if (w > 100)
-                            w = 100;
-                        wpercent = true;
+                    if (tag.GetParamAsIntOrPercent(wxT("WIDTH"), &w, wpercent))
+                    {
+                        if (wpercent)
+                        {
+                            if (w < 0)
+                                w = 0;
+                            else if (w > 100)
+                                w = 100;
+                        }
                     }
-
                 }
 
                 if (tag.HasParam(wxT("HEIGHT")))
index 825329b2ca0fbb911b3ae849a20debb17bced7af..1b96963be3640d3cc834ac67a11e819b9a571705 100644 (file)
@@ -702,19 +702,18 @@ TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
             {
                 if (tag.HasParam(wxT("WIDTH")))
                 {
-                    wxString wd = tag.GetParam(wxT("WIDTH"));
-
-                    if (!wd.empty() && wd[wd.length()-1] == wxT('%'))
-                    {
-                        int width = 0;
-                        wxSscanf(wd.c_str(), wxT("%i%%"), &width);
-                        m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT);
-                    }
-                    else
+                    int width = 0;
+                    bool wpercent = false;
+                    if (tag.GetParamAsIntOrPercent(wxT("WIDTH"), &width, wpercent))
                     {
-                        int width = 0;
-                        wxSscanf(wd.c_str(), wxT("%i"), &width);
-                        m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS);
+                        if (wpercent)
+                        {
+                            m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT);
+                        }
+                        else
+                        {
+                            m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS);
+                        }
                     }
                 }
                 else