summaryrefslogtreecommitdiff
path: root/size.go
blob: 038386315c6b5109bf3dd891e6c1d16c1237515b (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"

	"go.wit.com/widget"
)

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

	// don't count hidden widgets in size calculations
	if tk.Hidden() {
		return 0, 0
	}

	switch tk.WidgetType() {
	case widget.Window:
		var maxH int = 0
		var maxW int = 0
		for _, child := range tk.children {
			if tk.Hidden() {
				continue
			}
			sizeW, sizeH := child.Size()
			maxW += sizeW
			if sizeH > maxH {
				maxH = sizeH
			}

		}
		return maxW, maxH
	case widget.Grid:
		return tk.sizeGrid()
	case widget.Box:
		return tk.sizeBox()
	case widget.Group:
		// move the group to the parent's next location
		maxW := tk.gocuiSize.Width()
		maxH := tk.gocuiSize.Height()

		for _, child := range tk.children {
			if tk.Hidden() {
				continue
			}
			sizeW, sizeH := child.Size()

			// increment straight down
			maxH += sizeH
			if sizeW > maxW {
				maxW = sizeW
			}
		}
		return maxW + me.GroupPadW + 3, maxH
	case widget.Label:
		return len(tk.String()) + 2, 1
	case widget.Textbox:
		return len(tk.String()) + 10, 3 // TODO: compute this based on 'window dense'
	case widget.Checkbox:
		return len(tk.String()) + 2, 3 // TODO: compute this based on 'window dense'
	case widget.Button:
		if tk.isWindowDense() {
			return len(tk.String()) + 2, 0
		}
		return len(tk.String()) + 2, 3 // TODO: compute this based on 'window dense'
	}
	if tk.isFake {
		return 0, 0
	}
	return len(tk.String()), 3
}

func (w *guiWidget) sizeGrid() (int, int) {
	if w.Hidden() {
		return 0, 0
	}

	// first compute the max sizes of the rows and columns
	for _, child := range w.children {
		if w.Hidden() {
			continue
		}
		sizeW, sizeH := child.Size()

		// set the child's realWidth, and grid offset
		if w.widths[child.GridW()] < sizeW {
			w.widths[child.GridW()] = sizeW
		}
		if w.heights[child.GridH()] < sizeH {
			w.heights[child.GridH()] = sizeH
		}
	}

	// find the width and height offset of the grid for AtW,AtH
	var totalW int = 0
	var totalH int = 0
	for _, width := range w.widths {
		totalW += width
	}
	for _, h := range w.heights {
		totalH += h
	}
	return totalW + me.GridPadW, totalH
}

func (w *guiWidget) sizeBox() (int, int) {
	if w.WidgetType() != widget.Box {
		return 0, 0
	}
	if w.Hidden() {
		return 0, 0
	}
	var maxW int = 0
	var maxH int = 0

	for _, child := range w.children {
		if w.Hidden() {
			continue
		}
		sizeW, sizeH := child.Size()
		if child.Direction() == widget.Vertical {
			maxW += sizeW
			if sizeH > maxH {
				maxH = sizeH
			}
		} else {
			maxH += sizeH
			if sizeW > maxW {
				maxW = sizeW
			}
		}
	}
	return maxW + me.BoxPadW, maxH
}

/*
var wtf bool

func (tk *guiWidget) verifyRect() bool {
	if !tk.Visible() {
		// log.Info("verifyRect() tk is not visible", tk.cuiName)
		return false
	}
	vw0, vh0, vw1, vh1, err := me.baseGui.ViewPosition(tk.cuiName)
	if err != nil {
		// log.Printf("verifyRect() gocui err=%v cuiName=%s v.Name=%s", err, tk.cuiName, tk.v.Name())
		vw0, vh0, vw1, vh1, err = me.baseGui.ViewPosition(tk.v.Name())
		if err != nil {
			log.Printf("verifyRect() ACTUAL FAIL gocui err=%v cuiName=%s v.Name=%s", err, tk.cuiName, tk.v.Name())
			return false
		}
		// return false
	}
	var ok bool = true
	if vw0 != tk.full.w0 {
		// log.Info("verifyRect() FIXING w0", tk.cuiName, vw0, vw1, vh0, vh1, tk.gocuiSize.w0, "w0 =", vw0)
		tk.full.w0 = vw0
		ok = false
	}
	if vw1 != tk.full.w1 {
		// log.Info("verifyRect() FIXING w1", tk.cuiName, vw0, vw1, vh0, vh1, tk.gocuiSize.w1, "w1 =", vw1)
		tk.full.w1 = vw1
		ok = false
	}
	if vh0 != tk.full.h0 {
		// log.Info("verifyRect() FIXING h0", tk.cuiName, vw0, vw1, vh0, vh1, tk.gocuiSize.h0)
		tk.full.h0 = vh0
		ok = false
	}
	if vh1 != tk.full.h1 {
		// log.Info("verifyRect() FIXING h1", tk.cuiName, vw0, vw1, vh0, vh1, tk.gocuiSize.h1)
		tk.full.h1 = vh1
		ok = false
	}
	if !ok {
		// log.Info("verifyRect() NEED TO FIX RECT HERE", tk.cuiName)
		// tk.dumpWidget("verifyRect() FIXME")
	}
	// log.Printf("verifyRect() OK cuiName=%s v.Name=%s", tk.cuiName, tk.v.Name())
	return true
}
*/

func (tk *guiWidget) setFullSize() bool {
	r := tk.getFullSize()

	if tk.Hidden() {
		p := tk.parent
		if p != nil {
			// tk.full.w0 = p.full.w0
			// tk.full.w1 = p.full.w1
			// tk.full.h0 = p.full.h0
			// tk.full.h1 = p.full.h1
			tk.full.w0 = 0
			tk.full.w1 = 0
			tk.full.h0 = 0
			tk.full.h1 = 0
		} else {
			tk.full.w0 = 0
			tk.full.w1 = 0
			tk.full.h0 = 0
			tk.full.h1 = 0
		}
		return false
	}

	var changed bool
	if tk.full.w0 != r.w0 {
		tk.full.w0 = r.w0
		changed = true
	}
	// widget might be forced to a certain location
	if tk.full.w0 < tk.force.w0 {
		tk.gocuiSize.w0 = tk.force.w0
		tk.full.w0 = tk.force.w0
		changed = false
	}
	if tk.full.w1 != r.w1 {
		tk.full.w1 = r.w1
		changed = true
	}
	if tk.full.h0 != r.h0 {
		tk.full.h0 = r.h0
		changed = true
	}
	// widget might be forced to a certain location
	if tk.full.h0 < tk.force.h0 {
		tk.gocuiSize.h0 = tk.force.h0
		tk.full.h0 = tk.force.h0
		changed = false
	}
	if tk.full.h1 != r.h1 {
		tk.full.h1 = r.h1
		changed = true
	}
	if tk.WidgetType() == widget.Button {
		tk.full.h1 = tk.full.h0 + 1
	}
	if tk.isWindowDense() && tk.isInGrid() {
		tk.full.h1 = tk.full.h0
	}
	if changed {
		tk.dumpWidget(fmt.Sprintf("setFullSize(changed)"))
	}
	return changed
}

func (tk *guiWidget) gridFullSize() rectType {
	var r rectType
	var first bool = true
	for _, child := range tk.children {
		cr := child.getFullSize()
		if cr.Width() == 0 && cr.Height() == 0 {
			// ignore widgets of zero size?
			continue
		}
		if first {
			// use the lowest width and hight from children widgets
			r.w0 = cr.w0
			r.h0 = cr.h0
			r.w1 = cr.w1
			r.h1 = cr.h1
			first = false
			// child.dumpWidget(fmt.Sprintf("grid(f)"))
			continue
		}
		// child.dumpWidget(fmt.Sprintf("grid()"))
		// use the lowest width and hight from children widgets
		if r.w0 > cr.w0 {
			r.w0 = cr.w0
		}
		if r.h0 > cr.h0 {
			r.h0 = cr.h0
		}
		// use the highest width and hight from children widgets
		if r.w1 < cr.w1 {
			r.w1 = cr.w1
		}
		if r.h1 < cr.h1 {
			r.h1 = cr.h1
		}
	}
	tk.full.w0 = r.w0
	tk.full.w1 = r.w1
	tk.full.h0 = r.h0
	tk.full.h1 = r.h1
	return r
}

func (tk *guiWidget) buttonFullSize() rectType {
	var r rectType
	r.w0 = tk.gocuiSize.w0
	r.w1 = tk.gocuiSize.w1
	r.h0 = tk.gocuiSize.h0
	r.h1 = tk.gocuiSize.h1

	// try setting the full values here ? is this right?
	tk.full.w0 = r.w0
	tk.full.w1 = r.w1
	tk.full.h0 = r.h0
	tk.full.h1 = r.h1

	// total hack. fix this somewhere eventually correctly
	if tk.isWindowDense() { // total hack. fix this somewhere eventually correctly
		tk.full.h0 += 1         // total hack. fix this somewhere eventually correctly
		tk.full.h1 = tk.full.h0 // total hack. fix this somewhere eventually correctly
	}

	return r
}

// this checks a widget to see if it is under (W,H), then checks the widget's children
// anything that matches is passed back as an array of widgets
func (tk *guiWidget) getFullSize() rectType {
	var r rectType

	if tk.Hidden() {
		/*
			p := tk.parent
			if p != nil {
				return p.full
			}
		*/
		var r rectType
		r.w0 = 0
		r.w1 = 0
		r.h0 = 0
		r.h1 = 0
		return r
	}

	if tk.WidgetType() == widget.Grid {
		return tk.gridFullSize()
	}

	// these are 'simple' widgets
	// the full size is exactly what gocui uses
	switch tk.WidgetType() {
	case widget.Label:
		r := tk.buttonFullSize()
		r.w1 += 5
		return r
	case widget.Button:
		r := tk.buttonFullSize()
		r.w1 += 5
		return r
	case widget.Checkbox:
		return tk.buttonFullSize()
	case widget.Dropdown:
		r := tk.buttonFullSize()
		r.w1 += 7 // TODO: fix this to be real
		return r
	default:
	}

	if tk.v == nil {
		r.w0 = tk.full.w0
		r.w1 = tk.full.w1
		r.h0 = tk.full.h0
		r.h1 = tk.full.h1
	} else {
		r.w0 = tk.gocuiSize.w0
		r.w1 = tk.gocuiSize.w1
		r.h0 = tk.gocuiSize.h0
		r.h1 = tk.gocuiSize.h1
	}

	// search through the children widgets in the binary tree
	for _, child := range tk.children {
		cr := child.getFullSize()
		/* this didn't make things work either
		if child.v == nil {
			continue
		}
		*/
		// use the lowest width and hight from children widgets
		if r.w0 > cr.w0 {
			r.w0 = cr.w0
		}
		if r.h0 > cr.h0 {
			r.h0 = cr.h0
		}
		// use the highest width and hight from children widgets
		if r.w1 < cr.w1 {
			r.w1 = cr.w1
		}
		if r.h1 < cr.h1 {
			r.h1 = cr.h1
		}

	}

	// try setting the full values here ? is this right?
	tk.full.w0 = r.w0
	tk.full.w1 = r.w1
	tk.full.h0 = r.h0
	tk.full.h1 = r.h1

	return r
}