summaryrefslogtreecommitdiff
path: root/toolkit/gocui/place.go
blob: 32a9004527642217af48fd5e9e37769570654b32 (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
package main

import (
	"fmt"
//	"github.com/awesome-gocui/gocui"
	"git.wit.org/wit/gui/toolkit"
)

func (w *cuiWidget) placeBox() {
	if (w.widgetType != toolkit.Box) {
		return
	}
	w.startW = w.parent.nextW
	w.startH = w.parent.nextH
	w.nextW = w.parent.nextW
	w.nextH = w.parent.nextH
	w.realWidth = 0
	w.realHeight = 0

	var maxW int
	var maxH int
	for _, child := range w.children {
		w.showWidgetPlacement(logNow, "boxS()")
		child.placeWidgets()
		if (w.horizontal) {
			log(logVerbose, "BOX IS HORIZONTAL")
			// expand based on the child width
			w.nextW += child.realWidth
			w.realWidth += child.realWidth
		} else {
			log(logVerbose, "BOX IS VERTICAL")
			// expand based on the child height
			w.nextH += child.realHeight
			w.realHeight += child.realHeight
		}
		if (maxW < child.realWidth) {
			maxW = child.realWidth
		}
		if (maxH < child.realHeight) {
			maxH = child.realHeight
		}
	}
	if (w.horizontal) {
		w.realHeight = maxH
	} else {
		w.realWidth = maxW
	}
	w.showWidgetPlacement(logNow, "boxE()")
}

func (w *cuiWidget) getGroupWH() {
	p := w.parent // the parent must be a group widget

	// update parent gocuiSize
	p.realWidth = 0
	p.realHeight = 0
	p.realHeight += me.buttonPadding // pad height for the group label
	for _, child := range p.children {
		p.realWidth += child.realWidth
		p.realHeight += child.realHeight
	}

	// compute child offset
	w.startW = p.startW
	w.startH = p.startH
	for _, child := range p.children {
		w.startH += child.realHeight
		if child == w {
			return
		}
	}
	return
}

func (w *cuiWidget) placeWidgets() {
	if (w == nil) {
		return
	}
	if (me.rootNode == nil) {
		return
	}
	p := w.parent
	if (p == nil) {
		log(logInfo, "place()", w.id, "parent == nil")
		return
	}

	switch w.widgetType {
	case toolkit.Window:
		for _, child := range w.children {
			w.startW = me.rawW
			w.startH = me.rawH
			w.nextW = me.rawW
			w.nextH = me.rawH
			w.showWidgetPlacement(logNow, "place()")
			child.placeWidgets()
			if (w.realWidth < child.realWidth) {
				w.realWidth = child.realWidth
			}
			if (w.realHeight < child.realHeight) {
				w.realHeight = child.realHeight
			}
			w.showWidgetPlacement(logNow, "place()")
		}
	case toolkit.Tab:
		for _, child := range w.children {
			w.startW = me.rawW
			w.startH = me.rawH
			w.nextW = me.rawW
			w.nextH = me.rawH
			w.showWidgetPlacement(logNow, "place()")
			child.placeWidgets()
			if (w.realWidth < child.realWidth) {
				w.realWidth = child.realWidth
			}
			if (w.realHeight < child.realHeight) {
				w.realHeight = child.realHeight
			}
			w.showWidgetPlacement(logNow, "place()")
		}
	case toolkit.Grid:
		w.showWidgetPlacement(logNow, "place()")
		w.placeGrid()
		w.showWidgetPlacement(logNow, "place()")
	case toolkit.Box:
		w.showWidgetPlacement(logNow, "place()")
		w.placeBox()
		w.showWidgetPlacement(logNow, "place()")
	case toolkit.Group:
		w.startW = p.nextW
		w.startH = p.nextH
		w.nextW = p.nextW
		w.nextH = p.nextH

		w.moveTo(p.nextW, p.nextH)

		// set real width at the beginning
		w.realWidth = w.gocuiSize.width
		w.realHeight = w.gocuiSize.height

		// indent the widgets for a group
		w.nextW = p.nextW + 4
		w.nextH = p.nextH + 3
		w.showWidgetPlacement(logNow, "place()")
		var maxW int
		for _, child := range w.children {
			child.showWidgetPlacement(logNow, "place()")
			child.placeWidgets()
			child.showWidgetPlacement(logNow, "place()")

			// increment straight down
			w.nextH += child.realHeight
			w.realHeight += child.realHeight

			// track largest width
			if (maxW < child.realWidth) {
				maxW = child.realWidth
			}

		}
		// add real width of largest child
		w.realWidth += maxW
		w.showWidgetPlacement(logNow, "place()")
	default:
		w.startW = p.nextW
		w.startH = p.nextH
		w.nextW = p.nextW
		w.nextH = p.nextH
		w.gocuiSize.w0 = w.startW
		w.gocuiSize.h0 = w.startH
		w.setWH()
		w.showWidgetPlacement(logNow, "place()")
	}
}

func (w *cuiWidget) setWH() {
	w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width
	w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height
}

func (w *cuiWidget) moveTo(leftW int, topH int) {
	if (w.isFake) {
		return
	}
	w.gocuiSize.w0 = leftW
	w.gocuiSize.h0 = topH
	w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width
	w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height
	w.showWidgetPlacement(logInfo, "moveTo()")
}

func (w *cuiWidget) placeGrid() {
	w.showWidgetPlacement(logNow, "grid0:")
	if (w.widgetType != toolkit.Grid) {
		return
	}
	w.startW = w.parent.nextW
	w.startH = w.parent.nextH
	w.nextW = w.parent.nextW
	w.nextH = w.parent.nextH

	var wCount int = 0
	var hCount int = 0
	for _, child := range w.children {
		// increment for the next child
		w.nextW = w.startW + wCount * 20
		w.nextH = w.startH + hCount * 2

		// set the child's realWidth, and grid offset
		child.parentH = hCount
		child.parentW = wCount
		if (w.widths[wCount] < child.realWidth) {
			w.widths[wCount] = child.realWidth
		}
		if (w.heights[hCount] < child.realHeight) {
			w.heights[hCount] = child.realHeight
		}
		log(logVerbose, "grid1: (w,h count)", wCount, hCount, "(X,Y)", w.x, w.y, w.name)
		child.showWidgetPlacement(logNow, "grid1: " + fmt.Sprintf("next()=(%2d,%2d)", w.nextW, w.nextH))

		if ((wCount + 1) < w.y) {
			wCount += 1
		} else {
			wCount = 0
		hCount += 1
		}
	}

	// reset the size of the whole grid
	w.realWidth = 0
	w.realHeight = 0
	for _, val := range w.widths {
		w.realWidth += val
	}
	for _, val := range w.heights {
		w.realHeight += val
	}

	for _, child := range w.children {
		child.showWidgetPlacement(logVerbose, "grid2:")
		var totalW, totalH int
		for i, val := range w.widths {
			if (i < child.parentW) {
				log(logVerbose, "grid2: (w, widths[])", i, val)
				totalW += w.widths[i]
			}
		}
		for i, h := range w.heights {
			if (i < child.parentH) {
				totalH += h
			}
		}

		// the new corner to move the child to
		w.nextW = w.startW + totalW
		w.nextH = w.startH + totalH

		child.placeWidgets()
		child.showWidgetPlacement(logInfo, "grid2:")
		log(logInfo)
	}
	// w.updateLogicalSizes()
	w.showWidgetPlacement(logNow, "grid3:")
}

func (w *cuiWidget) setRealSize() {
}