コード例 #1
0
    public void SetCommonHtmlAttributes(HtmlDocument htmlDoc, HtmlNode field_node, Hashtable field_map,  double x_size_factor ,  double y_size_factor , string field_type)
    {
        XmlUtil x_util = new XmlUtil();
        //init style
        HtmlAttribute style = htmlDoc.CreateAttribute("style", "position:absolute;");
        field_node.Attributes.Append(style);
        HtmlAttribute icon_field = null;
        int MinTextFieldHeight = 30;
        int MinButtonHeight = 30;

        //set default z-index
        if (!field_map.ContainsKey("z_index"))
            field_map["z_index"] = "50";
        if (field_type == "text_field" || field_type == "text_area" || field_type == "label" || field_type == "button")
        {
            if (!field_map.ContainsKey("font_style"))
                field_map["font_style"] = "normal";
            if (!field_map.ContainsKey("font_weight"))
                field_map["font_weight"] = "normal";
            if (!field_map.ContainsKey("text_decoration"))
                field_map["text_decoration"] = "none";
            /*if (field_type == "text_area")
            {
                //add 20 px to height and width of top node and then subtract 20 px for subnode of textarea later in SetTextHtmlAttributes
                field_map["height"] = Convert.ToInt32(field_map["height"].ToString()) + 20;
                field_map["width"] = Convert.ToInt32(field_map["width"].ToString()) + 20;
            }*/
        }
        //process buttons specially
        if (field_type == "button")
        {
            int height = Convert.ToInt32(field_map["height"].ToString());
           // int width = Convert.ToInt32(field_map["width"].ToString());
            //field_map["width"] = (width + 20).ToString();
            int top = Convert.ToInt32(field_map["top"].ToString());
            //int left = Convert.ToInt32(field_map["left"].ToString());
            //field_map["left"] = (left - 10).ToString();
            if (height < MinButtonHeight)
            {
                field_map["height"] = MinButtonHeight.ToString();
                top -= (MinButtonHeight - height) / 2;
                field_map["top"] = top.ToString();
            }
        }
        else if (field_type == "text_field")
        {
            int height = Convert.ToInt32(field_map["height"].ToString());
            int top = Convert.ToInt32(field_map["top"].ToString());
            if (height < MinTextFieldHeight)
            {
                field_map["height"] = MinTextFieldHeight.ToString();
                top -= (MinTextFieldHeight - height) / 2;
                field_map["top"] = top.ToString();
            }
        }

        foreach (string key in field_map.Keys)
        {
            switch (key)
            {
                case "icon_field":
                    if (field_node.Attributes["icon_field"] == null)
                    {
                        icon_field = htmlDoc.CreateAttribute("icon_field", "field:" + field_map[key].ToString() + ";");
                        field_node.Attributes.Append(icon_field);
                    }
                    else
                        field_node.Attributes["icon_field"].Value += "field:" + field_map[key].ToString() + ";";
                    break;
                case "icon_width":
                    if (field_node.Attributes["icon_field"] == null)
                    {
                        icon_field = htmlDoc.CreateAttribute("icon_field", "width:" + field_map[key].ToString() + ";");
                        field_node.Attributes.Append(icon_field);
                    }
                    else
                        field_node.Attributes["icon_field"].Value += "width:" + field_map[key].ToString() + ";";
                    break;
                case "icon_height":
                    if (field_node.Attributes["icon_field"] == null)
                    {
                        icon_field = htmlDoc.CreateAttribute("icon_field", "height:" + field_map[key].ToString() + ";");
                        field_node.Attributes.Append(icon_field);
                    }
                    else
                        field_node.Attributes["icon_field"].Value += "height:" + field_map[key].ToString() + ";";
                    break;
                case "url":
                case "alt":
                case "title":
                case "id":
                case "class":
                case "src":
                    HtmlAttribute attr = htmlDoc.CreateAttribute(key, field_map[key].ToString());
                    field_node.Attributes.Append(attr);
                    break;
                case "value":
                    HtmlAttribute val_attr = htmlDoc.CreateAttribute(key, HttpUtility.HtmlAttributeEncode(x_util.UnescapeXml(field_map[key].ToString())));
                    field_node.Attributes.Append(val_attr);
                    break;
                case "top":
                    int top = Convert.ToInt32(field_map[key].ToString());
                    if (y_size_factor == 1.0D)
                        field_node.Attributes["style"].Value += key + ":" + top.ToString() + "px;";
                    else
                    {
                        double y = Math.Round(Convert.ToDouble(top) * y_size_factor);
                        field_node.Attributes["style"].Value += key + ":" + y.ToString() + "px;";
                    }
                    break;
                case "left":
                    int left = Convert.ToInt32(field_map[key].ToString());
                    if (field_type == "text_field")
                    {
                        left += 5; //because there is border of 1 and padding of 4 added
                    }
                    if (x_size_factor == 1.0D)
                        field_node.Attributes["style"].Value += key + ":" + left.ToString() + "px;";
                    else
                    {
                        double x = Math.Round(Convert.ToDouble(left) * x_size_factor);
                        field_node.Attributes["style"].Value += key + ":" + x.ToString() + "px;";
                    }
                    break;
                case "height":
                    int height = Convert.ToInt32(field_map[key].ToString());
                    if (y_size_factor != 1.0D)
                    {
                        if (field_type == "image" || field_type == "image_button")
                        {
                            //maintain aspect ratios for images
                            height = Convert.ToInt32(Math.Round(Convert.ToDouble(height) * x_size_factor));
                        }
                        else
                        {
                            height = Convert.ToInt32(Math.Round(Convert.ToDouble(height) * y_size_factor));
                        }
                    }

                     field_node.Attributes["style"].Value += key + ":" + height.ToString() + "px;";
                     break;

                 case "width":
                    int width = Convert.ToInt32(field_map[key].ToString());
                    if (field_type == "text_field")
                    {
                        width -= 10; //because there is border of 1 and padding of 4 added
                    }
                    if (x_size_factor != 1.0D)
                    {
                         width = Convert.ToInt32(Math.Round(Convert.ToDouble(width) * x_size_factor));
                    }
                    if (field_type == "slider")
                    {
                        width = Math.Max(width, Convert.ToInt32(150 * x_size_factor));
                        field_node.Attributes["style"].Value += key + ":" + width.ToString() + "px;";
                    }
                    else
                        field_node.Attributes["style"].Value += key + ":" + width.ToString() + "px;";
                    break;
                 case "font_size":
                    int font_size = Convert.ToInt32(field_map[key].ToString());
                    if (y_size_factor != 1.0D)
                    {
                        font_size = Convert.ToInt32(Math.Round(Convert.ToDouble(font_size) * y_size_factor));
                    }
                    field_node.Attributes["style"].Value += key.Replace("_", "-") + ":" + font_size.ToString() + "px;";
                    break;
                 case "z_index":
                    int z_index = Math.Min(Convert.ToInt32(field_map[key].ToString()), 99);
                    field_node.Attributes["style"].Value += key.Replace("_", "-") + ":" + z_index.ToString() + ";";
                    break;
                case "color":
                case "font_style":
                case "font_weight":
                case "text_decoration":
                    field_node.Attributes["style"].Value += key.Replace("_", "-") + ":" + field_map[key].ToString() + ";";
                    break;
                case "font_family":
                    field_node.Attributes["style"].Value += key.Replace("_", "-") + ":" + field_map[key].ToString().ToLower().Replace("tahoma", "helvetica").Replace("verdana", "helvetica").Replace("calibri", "helvetica") + ";";
                    break;
            }
        }
    }