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

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

var fakeStartWidth int = 80
var fakeStartHeight int = 0
func (w *cuiWidget) setFake() {
	if (w.visable) {
		return
	}
	t := len(w.name)
	// setup fake labels for non-visable things off screen
	w.realWidth = t + 2
	w.realHeight = me.defaultHeight
	w.realSize.w0 = fakeStartWidth
	w.realSize.h0 = fakeStartHeight
	w.realSize.w1 =  w.realSize.w0 + w.realWidth
	w.realSize.h1 =  w.realSize.h0 + w.realHeight
	fakeStartHeight += 3
	if (fakeStartHeight > 24) {
		fakeStartHeight = 0
		fakeStartWidth += 20
	}
	w.showWidgetPlacement(logNow, "setFake()")
}

func findPlace(w *cuiWidget) {
	w.visable = true
	switch w.widgetType {
	case toolkit.Root:
		w.visable = false
		w.setFake()
	case toolkit.Flag:
		w.visable = false
		w.setFake()
	case toolkit.Grid:
		w.visable = false
		w.setFake()
	case toolkit.Box:
		w.visable = false
		w.setFake()
	default:
		w.redoBox(true)
	}
}

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

	t := len(w.text)
	w.visable = true
	switch w.widgetType {
	case toolkit.Window:
		for _, child := range w.children {
			child.redoBox(draw)
		}
	case toolkit.Tab:
		for _, child := range w.children {
			child.redoBox(draw)
		}
	case toolkit.Grid:
		// hmm
		w.logicalSize.w0 = p.nextW
		w.logicalSize.h0 = p.nextH
		w.logicalSize.w1 = p.nextW
		w.logicalSize.h1 = p.nextH

		w.nextW = p.nextW
		w.nextH = p.nextH

		var wCount, hCount int
		var b bool = true
		for _, child := range w.children {
			child.redoBox(draw)
			if (b) {
				wCount += 1
				b = false
			} else {
				wCount = 0
				w.nextH += 1
				b = true
			}
			w.nextW = p.nextW + wCount * 20
			w.nextH = p.nextH + hCount * 4
			log(logInfo, "redoBox(GRID) (w,h count)", wCount, hCount, "(X,Y)", w.x, w.y, w.name)
		}
		w.showWidgetPlacement(logNow, "grid:")
	case toolkit.Box:
		w.logicalSize.w0 = p.nextW
		w.logicalSize.h0 = p.nextH
		w.logicalSize.w1 = p.nextW
		w.logicalSize.h1 = p.nextH

		w.nextW = p.nextW
		w.nextH = p.nextH
		for _, child := range w.children {
			child.redoBox(draw)
			if (w.horizontal) {
				log("BOX IS HORIZONTAL", p.nextW, p.nextW, p.name)
				log("BOX IS HORIZONTAL", w.nextW, w.nextH, w.name)
				log("BOX IS HORIZONTAL")
				// expand based on the child width
				w.nextW = child.nextW + me.horizontalPadding
				// reset height to parent
				w.nextH = p.nextH
			} else {
				log("BOX IS VERTICAL", p.nextW, p.nextW, p.name)
				log("BOX IS VERTICAL", w.nextW, w.nextH, w.name)
				log("BOX IS VERTICAL")
				// go straight down
				w.nextW = p.nextW
				// expand based on the child height
				w.nextH = child.nextH
			}
		}
		w.showWidgetPlacement(logNow, "box:")
	case toolkit.Group:
		w.realWidth = t + me.buttonPadding
		w.realHeight = me.defaultHeight

		w.realSize.w0 = p.nextW
		w.realSize.h0 = p.nextH
		w.realSize.w1 = w.realSize.w0 + w.realWidth
		w.realSize.h1 = w.realHeight

		w.logicalSize.w0 = w.realSize.w0
		w.logicalSize.h0 = w.realSize.h0
		w.logicalSize.w1 = w.realSize.w1
		w.logicalSize.h1 = w.realSize.h1

		w.nextW = p.nextW + me.groupPadding
		w.nextH = p.nextH + me.buttonPadding
		for _, child := range w.children {
			child.redoBox(draw)
			// reset nextW to straight down
			w.nextW = p.nextW + 4
			w.nextH = child.nextH
		}
		// expand the height of the parent now that the group is done
		// p.nextW = w.nextW
		// p.nextH = w.nextH
		w.showWidgetPlacement(logNow, "group:")
	default:
		w.realWidth = t + 3
		w.realHeight = me.defaultHeight
		w.realSize.w0 = p.nextW
		w.realSize.h0 = p.nextH
		w.realSize.w1 = p.nextW + w.realWidth
		w.realSize.h1 = p.nextH + w.realHeight

		w.logicalSize.w0 = p.nextW
		w.logicalSize.h0 = p.nextH
		w.logicalSize.w1 = p.nextW + w.realWidth
		w.logicalSize.h1 = p.nextH + w.realHeight

		w.nextW = w.realSize.w1
		w.nextH = w.realSize.h1
	}
}

func (w *cuiWidget) boxedPlace() {
	t := len(w.name)
	if (w.id == 0) {
		w.realWidth = 0
		w.realHeight = 0
		return
	}
	p := w.parent
	if (p == nil) {
		log(logError, "boxedPlace() parentId widget == nil")
		return
	}

	w.realWidth = t + 3
	w.realHeight = me.defaultHeight
	w.realSize.w0 = p.nextW
	w.realSize.h0 = p.nextH
	w.realSize.w1 = p.nextW + w.realWidth
	w.realSize.h1 = p.nextH + w.realHeight

	w.logicalSize.w0 = p.nextW
	w.logicalSize.h0 = p.nextH
	w.logicalSize.w1 = p.nextW + w.realWidth
	w.logicalSize.h1 = p.nextH + w.realHeight

	w.nextW = w.realSize.w1
	w.nextH = w.realSize.h1

	w.showWidgetPlacement(logNow, "bP widget")
}

func (w *cuiWidget) updateLogicalSizes() {
	for _, child := range w.children {
		child.updateLogicalSizes()
		if (w.logicalSize.w0 > child.logicalSize.w0) {
			w.logicalSize.w0 = child.logicalSize.w0
		}
		if (w.logicalSize.w1 < child.logicalSize.w1) {
			w.logicalSize.w1 = child.logicalSize.w1
		}
		if (w.logicalSize.h0 > child.logicalSize.h0) {
			w.logicalSize.h0 = child.logicalSize.h0
		}
		if (w.logicalSize.h1 < child.logicalSize.h1) {
			w.logicalSize.h1 = child.logicalSize.h1
		}
	}
}