예제 #1
0
        /// <summary>
        /// optimise the table layout, and write it;
        /// </summary>
        public void WriteTableLayout(TFormWriter writer, TControlDef LayoutCtrl)
        {
            // calculate the width and height for the columns and rows
            int[] ColumnWidth = new int[FColumnCount];
            int[] RowHeight   = new int[FRowCount];

            // first go: ignore cells spanning rows and columns; second go: check that spanning cells fit as well
            for (int spanRunCounter = 0; spanRunCounter < 2; spanRunCounter++)
            {
                for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
                {
                    // initialise the summary values
                    if (spanRunCounter == 0)
                    {
                        ColumnWidth[columnCounter] = 0;

                        if (columnCounter == 0)
                        {
                            for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                            {
                                RowHeight[rowCounter] = 0;
                            }
                        }
                    }

                    for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                    {
                        if ((FGrid[columnCounter, rowCounter] != null))
                        {
                            TControlDef ctrl = FGrid[columnCounter, rowCounter];

                            int CellWidth = ctrl.Width;
                            TLogging.LogAtLevel(1, "Control '" + ctrl.controlName + "' Width in WriteTableLayout: " + CellWidth.ToString());

                            if ((spanRunCounter == 0) && (ctrl.colSpanWithLabel == 1))
                            {
                                if (CellWidth > ColumnWidth[columnCounter])
                                {
                                    ColumnWidth[columnCounter] = CellWidth;
                                }
                            }
                            else
                            {
                                int CurrentSpanWidth = 0;

                                if (columnCounter + ctrl.colSpanWithLabel > FColumnCount)
                                {
                                    // TODO: make an exception again?
                                    TLogging.Log("Warning: invalid colspan " + ctrl.colSpan.ToString() + " in control " + ctrl.controlName +
                                                 ". There are only " +
                                                 (FColumnCount / 2).ToString() + " columns overall");

                                    ctrl.colSpanWithLabel = ctrl.colSpan;
                                }

                                for (int columnCounter2 = columnCounter; columnCounter2 < columnCounter + ctrl.colSpanWithLabel; columnCounter2++)
                                {
                                    CurrentSpanWidth += ColumnWidth[columnCounter2];
                                }

                                if (CurrentSpanWidth < CellWidth)
                                {
                                    ColumnWidth[columnCounter + ctrl.colSpanWithLabel - 1] += CellWidth - CurrentSpanWidth;
                                }
                            }

                            int CellHeight = ctrl.Height;

                            if (CellHeight == 17)
                            {
                                // for labels, we should consider the margin top as well.
                                CellHeight = 22;
                            }

                            if ((spanRunCounter == 0) && (ctrl.colSpanWithLabel == 1))
                            {
                                if (CellHeight > RowHeight[rowCounter])
                                {
                                    RowHeight[rowCounter] = CellHeight;
                                }
                            }
                            else
                            {
                                int CurrentSpanHeight = 0;

                                for (int rowCounter2 = rowCounter; rowCounter2 < rowCounter + ctrl.rowSpan; rowCounter2++)
                                {
                                    CurrentSpanHeight += RowHeight[rowCounter2];
                                }

                                if (CurrentSpanHeight < CellHeight)
                                {
                                    RowHeight[rowCounter + ctrl.rowSpan - 1] += CellHeight - CurrentSpanHeight;
                                }
                            }
                        }
                    }
                }
            }

            // now apply settings about the column width and row height
            if (FColWidths != null)
            {
                bool simpleColumnWidth = false;

                // for the simple column width specification, you need to provide a width for each column, without the label columns
                if (FColWidths.Count * 2 == FColumnCount)
                {
                    simpleColumnWidth = true;

                    for (int columnCounter = 0; columnCounter < FColWidths.Count; columnCounter++)
                    {
                        if (!FColWidths.ContainsKey(columnCounter))
                        {
                            simpleColumnWidth = false;
                        }
                    }
                }

                for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
                {
                    if (simpleColumnWidth)
                    {
                        // the specified width includes the label column
                        if (FColWidths.ContainsKey(columnCounter / 2))
                        {
                            string[] ColWidthSpec = FColWidths[columnCounter / 2].Split(':');

                            if (ColWidthSpec[0].ToLower() == "fixed")
                            {
                                ColumnWidth[columnCounter] = Convert.ToInt32(ColWidthSpec[1]) / 2;
                            }
                            else if (ColWidthSpec[0].ToLower() == "percent")
                            {
                                // TODO
                            }
                        }
                    }
                    else
                    {
                        if (FColWidths.ContainsKey(columnCounter))
                        {
                            string[] ColWidthSpec = FColWidths[columnCounter].Split(':');

                            if (ColWidthSpec[0].ToLower() == "fixed")
                            {
                                ColumnWidth[columnCounter] = Convert.ToInt32(ColWidthSpec[1]);
                            }
                            else if (ColWidthSpec[0].ToLower() == "percent")
                            {
                                // TODO
                                TLogging.Log("Warning: we currently don't support colwidth in percentage, control " + LayoutCtrl.controlName);
                            }
                        }
                    }
                }
            }

            if (FRowHeights != null)
            {
                for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                {
                    if (FRowHeights.ContainsKey(rowCounter))
                    {
                        string[] RowHeightSpec = FRowHeights[rowCounter].Split(':');

                        if (RowHeightSpec[0].ToLower() == "fixed")
                        {
                            RowHeight[rowCounter] = Convert.ToInt32(RowHeightSpec[1]);
                        }
                        else if (RowHeightSpec[0].ToLower() == "percent")
                        {
                            // TODO
                            TLogging.Log("Warning: we currently don't support rowheight in percentage, control " + LayoutCtrl.controlName);
                        }
                    }
                }
            }

            if (TLogging.DebugLevel >= 4)
            {
                StringCollection widthStringCollection = new StringCollection();

                for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
                {
                    widthStringCollection.Add(ColumnWidth[columnCounter].ToString());
                }

                TLogging.Log("column width for " + LayoutCtrl.controlName + ": " + StringHelper.StrMerge(widthStringCollection, ','));

                for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                {
                    string rowText = string.Empty;

                    for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
                    {
                        if (FGrid[columnCounter, rowCounter] != null)
                        {
                            TControlDef childctrl = FGrid[columnCounter, rowCounter];

                            for (int countspan = 0; countspan < childctrl.colSpanWithLabel; countspan++)
                            {
                                rowText += string.Format("{0}:{1} ", columnCounter + countspan, childctrl.controlName);
                            }
                        }
                    }

                    TLogging.Log(String.Format(" Row{0}: {1}", rowCounter, rowText));
                }
            }

            int Width  = 0;
            int Height = 0;

            int CurrentLeftPosition = Convert.ToInt32(LayoutCtrl.GetAttribute("MarginLeft", MARGIN_LEFT.ToString()));

            for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
            {
                int CurrentTopPosition;

                if (LayoutCtrl.IsHorizontalGridButtonPanel)
                {
                    TLogging.LogAtLevel(1,
                                        "Setting CurrentTopPosition to 3 for all Controls on Control '" + LayoutCtrl.controlName +
                                        "' as it is a horizontal Grid Button Panel (used for Buttons).");
                    CurrentTopPosition = 3;
                }
                else
                {
                    CurrentTopPosition = Convert.ToInt32(LayoutCtrl.GetAttribute("MarginTop", MARGIN_TOP.ToString()));
                }

                // only twice the margin for groupboxes
                if ((LayoutCtrl.controlTypePrefix == "grp") || (LayoutCtrl.controlTypePrefix == "rgr"))
                {
                    CurrentTopPosition += MARGIN_TOP;
                }

                for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                {
                    if (FGrid[columnCounter, rowCounter] != null)
                    {
                        TControlDef childctrl = FGrid[columnCounter, rowCounter];

                        if (childctrl.GetAttribute("Stretch") == "horizontally")
                        {
                            // use the full column width
                            // add up spanning columns
                            int concatenatedColumnWidth = ColumnWidth[columnCounter];

                            for (int colSpanCounter = 1; colSpanCounter < childctrl.colSpanWithLabel; colSpanCounter++)
                            {
                                concatenatedColumnWidth += ColumnWidth[columnCounter + colSpanCounter];
                            }

                            if (concatenatedColumnWidth > 0)
                            {
                                TControlDef ParentControl = childctrl.FCodeStorage.GetControl(childctrl.parentName);
                                System.Windows.Forms.Padding?ParentPad = null;

                                if (ParentControl.HasAttribute("Padding"))
                                {
                                    string ParentPadding = ParentControl.GetAttribute("Padding");
                                    TLogging.LogAtLevel(1, "ParentControl '" + ParentControl.controlName + "' Padding: " + ParentPadding);

                                    if (ParentPadding.IndexOf(',') == -1)
                                    {
                                        ParentPad = new Padding(Convert.ToInt32(ParentPadding));
                                    }
                                    else
                                    {
                                        string[] Paddings = ParentPadding.Split(',');
                                        ParentPad = new Padding(Convert.ToInt32(Paddings[0]), Convert.ToInt32(Paddings[1]),
                                                                Convert.ToInt32(Paddings[2]), Convert.ToInt32(Paddings[3]));
                                    }

                                    TLogging.LogAtLevel(1, "ParentControl Padding (parsed): " + ParentPad.ToString());
                                }

                                writer.SetControlProperty(childctrl, "Size",
                                                          String.Format("new System.Drawing.Size({0}, {1})", concatenatedColumnWidth -
                                                                        (ParentPad.HasValue ? ParentPad.Value.Right : 0),
                                                                        childctrl.Height));
                            }
                        }

                        int ControlTopPosition  = CurrentTopPosition;
                        int ControlLeftPosition = CurrentLeftPosition;
                        TLogging.LogAtLevel(1, "WriteTableLayout for Control '" + childctrl.controlName + "'");
                        // add margin or padding
                        string padding = writer.GetControlProperty(childctrl.controlName, "Padding");

                        if (padding.Length > 0)
                        {
                            string[] values = padding.Substring(padding.IndexOf("(") + 1).Replace(")", "").Split(new char[] { ',' });
                            ControlLeftPosition += Convert.ToInt32(values[0]);
                            ControlTopPosition  += Convert.ToInt32(values[1]);
                            TLogging.LogAtLevel(1, "Removing Padding Property from Control '" + childctrl.controlName + "'!");
                            writer.ClearControlProperty(childctrl.controlName, "Padding");
                        }

                        string margin = writer.GetControlProperty(childctrl.controlName, "Margin");

                        if (margin.Length > 0)
                        {
                            string[] values = margin.Substring(margin.IndexOf("(") + 1).Replace(")", "").Split(new char[] { ',' });
                            ControlLeftPosition += Convert.ToInt32(values[0]);
                            ControlTopPosition  += Convert.ToInt32(values[1]);
                            writer.ClearControlProperty(childctrl.controlName, "Margin");
                        }

                        if ((childctrl.IsOnHorizontalGridButtonPanel) &&
                            (columnCounter != 0) &&
                            !((childctrl.HasAttribute("StartNewButtonGroup")) &&
                              (childctrl.GetAttribute("StartNewButtonGroup").ToLower() == "true")))
                        {
                            TLogging.LogAtLevel(1,
                                                "Adjusted ControlLeftPosition for Control '" + childctrl.controlName +
                                                "' as it is on a horizontal Grid Button Panel.");
                            ControlLeftPosition -= 8;
                        }

                        writer.SetControlProperty(childctrl.controlName,
                                                  "Location",
                                                  String.Format("new System.Drawing.Point({0},{1})",
                                                                ControlLeftPosition.ToString(),
                                                                ControlTopPosition.ToString()),
                                                  false);
                        writer.CallControlFunction(LayoutCtrl.controlName,
                                                   "Controls.Add(this." + childctrl.controlName + ")");

                        if (FTabOrder == "Horizontal")
                        {
                            writer.SetControlProperty(childctrl.controlName, "TabIndex", FCurrentTabIndex.ToString(), false);
                            FCurrentTabIndex += 10;
                        }
                    }

                    CurrentTopPosition += RowHeight[rowCounter];

                    CurrentTopPosition += Convert.ToInt32(LayoutCtrl.GetAttribute("VerticalSpace", VERTICAL_SPACE.ToString()));

                    if (CurrentTopPosition > Height)
                    {
                        Height = CurrentTopPosition;
                    }
                }

                CurrentLeftPosition += ColumnWidth[columnCounter];

                CurrentLeftPosition += Convert.ToInt32(LayoutCtrl.GetAttribute("HorizontalSpace", HORIZONTAL_SPACE.ToString()));

                if (CurrentLeftPosition > Width)
                {
                    Width = CurrentLeftPosition;
                }
            }

            Height +=
                Convert.ToInt32(LayoutCtrl.GetAttribute("MarginBottom", MARGIN_BOTTOM.ToString())) -
                Convert.ToInt32(LayoutCtrl.GetAttribute("VerticalSpace", VERTICAL_SPACE.ToString()));

            if (!LayoutCtrl.HasAttribute("Width"))
            {
                LayoutCtrl.SetAttribute("Width", Width.ToString());
            }
            else
            {
                Width = Convert.ToInt32(LayoutCtrl.GetAttribute("Width"));
            }

            if (!LayoutCtrl.HasAttribute("Height"))
            {
                LayoutCtrl.SetAttribute("Height", Height.ToString());
            }
            else
            {
                Height = Convert.ToInt32(LayoutCtrl.GetAttribute("Height"));
            }

            writer.SetControlProperty(LayoutCtrl, "Location", String.Format("new System.Drawing.Point({0}, {1})", MARGIN_LEFT, MARGIN_TOP));
            writer.SetControlProperty(LayoutCtrl, "Size", String.Format("new System.Drawing.Size({0}, {1})", Width, Height));

            // by default, the TabOrder is by column, Vertical
            if (FTabOrder != "Horizontal")
            {
                for (int rowCounter = 0; rowCounter < FRowCount; rowCounter++)
                {
                    for (int columnCounter = 0; columnCounter < FColumnCount; columnCounter++)
                    {
                        if (FGrid[columnCounter, rowCounter] != null)
                        {
                            TControlDef childctrl = FGrid[columnCounter, rowCounter];

                            writer.SetControlProperty(childctrl.controlName, "TabIndex", FCurrentTabIndex.ToString(), false);
                            FCurrentTabIndex += 10;
                        }
                    }
                }
            }
        }