julian+alberto classlib merges for 3.4 (10125, 10130, 10133)
[supercollider.git] / common / build / SCClassLibrary / Common / GUI / FlowLayout.sc
blobe48d55e0449d9960b51a5c33a250fc0101654c94
2 FlowLayout {
3         var <bounds, <>margin, <>gap;
4         var <>left, <>top, <>maxHeight,<>maxRight;
6         *new { arg bounds, margin, gap;
7                 ^super.newCopyArgs(bounds, margin, gap).init
8         }
9         init {
10                 gap = gap ? Point(4,4);
11                 margin = margin ? Point(4,4);
12                 this.reset;
13         }
14         reset {
15                 maxRight = left = bounds.left + margin.x;
16                 top = bounds.top + margin.y;
17                 maxHeight  = 0;
18         }
19         place { arg view;
20                 var height, width,vbounds;
21                 vbounds = view.bounds;
22                 width = vbounds.width;
23                 height = vbounds.height;
24                 if ((left + width) > (bounds.right - margin.x), { this.nextLine; });
26                 view.bounds = Rect(left, top, width, height);
28                 maxRight = max(maxRight,left + width);
29                 left = left + width + gap.x;
30                 maxHeight = max(maxHeight, height);
31         }
32         nextLine {
33                 left = bounds.left + margin.x;
34                 top = top + maxHeight + gap.y;
35                 maxHeight = 0;
36         }
37         shift { arg x=0, y=0;
38                 left = left + x;
39                 top = top + y;
40         }
41         innerBounds {
42                 ^bounds.insetBy(margin.x * 2,margin.y * 2)
43         }
44         bounds_ { arg b;
45                 var d;
46                 left = left + ( d = (b.left - bounds.left));
47                 maxRight = maxRight + (d);
48                 top = top + (d = (b.top - bounds.top));
49                 maxHeight = maxHeight + (d);
50                 bounds = b;
51                 // and then you need to re-place all views
52                 // but nextLine will be broken, see FlowView
53         }
54         currentBounds {
55                 var currentBounds;
56                 currentBounds = bounds;
57                 currentBounds.height = top + maxHeight;
58                 ^currentBounds
59         }
60         // rounded out to the nearest rect + margin
61         used {
62                 ^Rect(bounds.left,bounds.top,
63                         maxRight + margin.x - bounds.left,
64                         (top + maxHeight + margin.y ) - bounds.top)
65         }
66         // largest allocatable rect starting in the current row
67         // going down as far as possible
68         indentedRemaining {
69                 var inb;
70                 inb = this.innerBounds;
71                 ^Rect(left,top,
72                         inb.width - (left - inb.left - margin.x),
73                         inb.height - (top - inb.top - margin.y))
74         }