summaryrefslogtreecommitdiff
path: root/place.go
blob: 0dd756883d0808a7263f9aa57a417045ca91aecd (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"strings"

	"go.wit.com/log"
	"go.wit.com/toolkits/tree"
	"go.wit.com/widget"
)

/*
	gocui defines the offset like this:

 	   width -> increases to the right
	----------------------------------    hieght
	|            H = 1               |   increases
	|                                |       |
	| W = 1                   W = 18 |       |
	|                                |       v
	|            H = 5               |   downwards
	-------------------------------------
*/

// moves the gocui view to the W and H offset on the screen
func (tk *guiWidget) MoveToOffset(W, H int) {
	tk.gocuiSetWH(W, H)
}

// returns where the corner of widget starts (upper left)
func (tk *guiWidget) Position() (int, int) {
	return tk.gocuiSize.w0, tk.gocuiSize.h0
}

func (w *guiWidget) placeBox(startW int, startH int) {
	if w.WidgetType != widget.Box {
		return
	}

	newW := startW
	newH := startH

	for _, child := range w.children {
		sizeW, sizeH := child.Size()
		log.Log(INFO, "BOX START size(W,H) =", sizeW, sizeH, "new(W,H) =", newW, newH)
		child.placeWidgets(newW, newH)
		// re-get the Size (they should not have changed, but maybe they can?)
		// TODO: figure this out or report that they did
		sizeW, sizeH = child.Size()
		if w.node.State.Direction == widget.Vertical {
			log.Log(INFO, "BOX IS VERTICAL  ", w.String(), "newWH()", newW, newH, "child()", sizeW, sizeH, child.String())
			// expand based on the child height
			newH += sizeH
		} else {
			log.Log(INFO, "BOX IS HORIZONTAL", w.String(), "newWH()", newW, newH, "child()", sizeW, sizeH, child.String())
			// expand based on the child width
			newW += sizeW
		}
		log.Log(INFO, "BOX END   size(W,H) =", sizeW, sizeH, "new(W,H) =", newW, newH)
	}
}

func (tk *guiWidget) placeWidgets(startW int, startH int) (int, int) {
	if tk == nil {
		return 0, 0
	}
	if me.treeRoot == nil {
		return 0, 0
	}

	tk.startW = startW
	tk.startH = startH

	switch tk.WidgetType {
	case widget.Window:
		newW := startW
		newH := startH
		var maxH int = 0
		for _, child := range tk.children {
			child.placeWidgets(newW, newH)
			sizeW, sizeH := child.Size()
			if sizeW < 20 {
				sizeW = 20
			}
			newW += sizeW
			if sizeH > maxH {
				maxH = sizeH
			}

		}
		return newW - startW, maxH
	case widget.Tab:
	case widget.Grid:
		return tk.placeGrid(startW, startH)
	case widget.Box:
		tk.placeBox(startW, startH)
		return 0, 0
	case widget.Group:
		// move the group to the parent's next location
		tk.gocuiSetWH(startW, startH)

		newW := startW + me.GroupPadW
		newH := startH + 1 // normal hight of the group label
		var maxW int = 0
		// now move all the children aka: run place() on them
		for _, child := range tk.children {
			sizeW, sizeH := child.placeWidgets(newW, newH)
			// newR := child.realGocuiSize()
			// w := newR.w1 - newR.w0
			// h := newR.h1 - newR.h0

			// increment straight down
			newH += sizeH + 1
			if sizeW > maxW {
				maxW = sizeW
			}
			log.Log(INFO, "REAL HEIGHT sizeW:", sizeW, "sizeH:", sizeH)
		}
		return maxW, newH - startH
	default:
		tk.gocuiSetWH(startW, startH)
		return tk.gocuiSize.Width(), tk.gocuiSize.Height()
	}
	return 0, 0
}

func (w *guiWidget) placeGrid(startW int, startH int) (int, int) {
	// w.showWidgetPlacement("grid0:")
	if w.WidgetType != widget.Grid {
		return 0, 0
	}

	// first compute the max sizes of the rows and columns
	for _, child := range w.children {
		childW, childH := child.placeWidgets(child.startW, child.startH)

		// set the child's realWidth, and grid offset
		if w.widths[child.node.State.AtW] < childW {
			w.widths[child.node.State.AtW] = childW
		}
		if w.heights[child.node.State.AtH] < childH {
			w.heights[child.node.State.AtH] = childH
		}
		// child.showWidgetPlacement("grid: ")
		log.Log(INFO, "placeGrid:", child.String(), "child()", childW, childH, "At()", child.node.State.AtW, child.node.State.AtH)
	}

	var maxW int = 0
	var maxH int = 0

	// find the width and height offset of the grid for AtW,AtH
	for _, child := range w.children {
		// child.showWidgetPlacement("grid1:")

		var totalW, totalH int
		for i, w := range w.widths {
			if i < child.node.State.AtW {
				totalW += w
			}
		}
		for i, h := range w.heights {
			if i < child.node.State.AtH {
				totalH += h
			}
		}

		// the new corner to move the child to
		newW := startW + totalW
		newH := startH + totalH

		if totalW > maxW {
			maxW = totalW
		}
		if totalH > maxH {
			maxH = totalH
		}

		log.Log(INFO, "placeGrid:", child.String(), "new()", newW, newH, "At()", child.node.State.AtW, child.node.State.AtH)
		child.placeWidgets(newW, newH)
		// child.showWidgetPlacement("grid2:")
	}
	// w.showWidgetPlacement("grid3:")
	return maxW, maxH
}

// computes the real, actual size of all the gocli objects in a widget
func (w *guiWidget) realGocuiSize() *rectType {
	var f func(tk *guiWidget, r *rectType)
	newR := new(rectType)
	// initialize the values to opposite
	newR.w0 = outputW
	newR.h0 = outputH
	if me.baseGui != nil {
		maxW, maxH := me.baseGui.Size()
		newR.w0 = maxW
		newR.h0 = maxH
	}
	newR.w1 = 0
	newR.h1 = 0

	// expand the rectangle to the biggest thing displayed
	f = func(tk *guiWidget, r *rectType) {
		newR := tk.gocuiSize
		if !tk.isFake {
			if r.w0 > newR.w0 {
				r.w0 = newR.w0
			}
			if r.h0 > newR.h0 {
				r.h0 = newR.h0
			}
			if r.w1 < newR.w1 {
				r.w1 = newR.w1
			}
			if r.h1 < newR.h1 {
				r.h1 = newR.h1
			}
		}
		for _, child := range tk.children {
			f(child, r)
		}
	}
	f(w, newR)
	return newR
}

func textSize(n *tree.Node) (int, int) {
	var tk *guiWidget
	tk = n.TK.(*guiWidget)
	var width, height int

	for _, s := range strings.Split(widget.GetString(tk.value), "\n") {
		if width < len(s) {
			width = len(s)
		}
		height += 1
	}
	return width, height
}

func (tk *guiWidget) gocuiSetWH(sizeW, sizeH int) {
	w := len(widget.GetString(tk.value))
	lines := strings.Split(widget.GetString(tk.value), "\n")
	h := len(lines)

	// tk := n.tk
	if tk.isFake {
		tk.gocuiSize.w0 = sizeW
		tk.gocuiSize.h0 = sizeH
		tk.gocuiSize.w1 = tk.gocuiSize.w0 + w + me.FramePadW
		tk.gocuiSize.h1 = tk.gocuiSize.h0 + h + me.FramePadH
		return
	}

	if tk.frame {
		tk.gocuiSize.w0 = sizeW
		tk.gocuiSize.h0 = sizeH
		tk.gocuiSize.w1 = tk.gocuiSize.w0 + w + me.FramePadW
		tk.gocuiSize.h1 = tk.gocuiSize.h0 + h + me.FramePadH
	} else {
		tk.gocuiSize.w0 = sizeW - 1
		tk.gocuiSize.h0 = sizeH - 1
		tk.gocuiSize.w1 = tk.gocuiSize.w0 + w + 1
		tk.gocuiSize.h1 = tk.gocuiSize.h0 + h + 1
	}
}