aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GenericStack.cs
blob: aa5ceeaad3831d4d221dca95a73dd8c5c865319d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace go
{
    public class GenericStack : Group
    {        
        public int widgetSpacing = 5;
        public Orientation Orientation = Orientation.Horizontal;

        public GenericStack()
            : base()
        {
            borderWidth = 0;
            sizeToContent = true;

        }

        int currentXForWidget = 0;
        int currentYForWidget = 0;

        public override void updateLayout()
        {
            //if (!(sizeIsValid && positionIsValid))
            base.updateLayout();

            currentXForWidget = clientBounds.X;
            currentYForWidget = clientBounds.Y;


            Rectangle contentBounds = Rectangle.Zero;

            GraphicObject[] widgets = new GraphicObject[Children.Count];
            Children.CopyTo(widgets);
            foreach (GraphicObject w in widgets)
            {
                if (!enoughtSpaceForWidget(w))
                    advance(w);

                if (enoughtSpaceForWidget(w))
                {
                    w.renderBounds.X = currentXForWidget;
                    w.renderBounds.Y = currentYForWidget;

                    w.positionIsValid = true;

                    contentBounds += w.renderBounds;

                    advance(w);
                }
                else
                    break;
            }

            contentBounds.Width += borderWidth + margin;
            contentBounds.Height += borderWidth + margin;

            if (sizeToContent)
                renderBounds.Size = contentBounds.Size;
            else if (VerticalScrolling)
                renderBounds.Size = new Size(renderBounds.Size.Width, contentBounds.Size.Height);
            else if (HorizontalScrolling)
                renderBounds.Size = new Size(contentBounds.Size.Width, renderBounds.Size.Height);

            if (layoutIsValid)
                registerForRedraw();
        }


        bool enoughtSpaceForWidget(GraphicObject w)
        {
            if (!sizeToContent)
            {
                int nextXForWidget = 0;
                int nextYForWidget = 0;

                if (Orientation == Orientation.Horizontal)
                    nextXForWidget = currentXForWidget + w.renderBounds.Width;
                else
                    nextYForWidget = nextYForWidget + w.renderBounds.Height;


                if (nextXForWidget > clientBounds.Right && !HorizontalScrolling)
                    return false;
                if (currentYForWidget > clientBounds.Bottom && !VerticalScrolling)
                    return false;
            }
            return true;
        }
        void advance(GraphicObject w)
        {
            if (Orientation == Orientation.Horizontal)
                currentXForWidget = currentXForWidget + widgetSpacing + w.renderBounds.Width;
            else
                currentYForWidget = currentYForWidget + widgetSpacing + w.renderBounds.Height;

        }
    }
}