summaryrefslogtreecommitdiff
path: root/toolkit/andlabs/add.go
blob: bebcfde65f2e9987b213e4b79fcd3b8f1338f250 (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
package main

import (
	"github.com/andlabs/ui"
	_ "github.com/andlabs/ui/winmanifest"

	"git.wit.org/wit/gui/toolkit"
)

func actionDump(b bool, a *toolkit.Action) {
	log(b, "actionDump() Widget.Type =", a.ActionType)
	log(b, "actionDump() Widget.S =", a.S)
	log(b, "actionDump() Widget.I =", a.I)
	log(b, "actionDump() WidgetId =", a.WidgetId)
	log(b, "actionDump() ParentId =", a.ParentId)
}

func add(a toolkit.Action) {
	if (andlabs[a.WidgetId] != nil) {
		log(debugError, "add() error. can't make a widget that already exists. id =", a.WidgetId)
		actionDump(debugError, &a)
		return
	}
	if (a.WidgetType == toolkit.Root) {
		rootNode = addWidget(&a, nil)
		return
	}
	n := addWidget(&a, nil)

	p := n.parent
	switch n.WidgetType {
	case toolkit.Window:
		newWindow(n)
		return
	case toolkit.Tab:
		p.newTab(n)
		return
	case toolkit.Label:
		p.newLabel(n)
		return
	case toolkit.Button:
		p.newButton(n)
		return
	case toolkit.Grid:
		p.newGrid(n)
		return
	case toolkit.Checkbox:
		p.newCheckbox(n)
		return
	case toolkit.Spinner:
		p.newSpinner(n)
		return
	case toolkit.Slider:
		p.newSlider(n)
		return
	case toolkit.Dropdown:
		newDropdown(&a)
		return
	case toolkit.Combobox:
		newCombobox(&a)
		return
	case toolkit.Textbox:
		newTextbox(&a)
		return
	case toolkit.Group:
		p.newGroup(n)
		return
	case toolkit.Box:
		p.newBox(n)
		return
	case toolkit.Image:
		p.newImage(n)
		return
	default:
		log(debugError, "add() error TODO: ", n.WidgetType, n.Name)
	}
}

// This routine is very specific to this toolkit
// It's annoying and has to be copied to each widget when there are changes
// it could be 'simplfied' maybe or made to be more generic, but this is as far as I've gotten
// it's probably not worth working much more on this toolkit, the andlabs/ui has been great and got me here!
// but it's time to write direct GTK, QT, macos and windows toolkit plugins
// -- jcarr 2023/03/09

// Grid numbering examples by (X,Y)
// ---------
// -- (1) --
// -- (2) --
// ---------
//
// -----------------------------
// -- (1,1) -- (1,2) -- (1,3) --
// -- (2,1) -- (2,2) -- (2,3) --
// -----------------------------

// internally for andlabs/ui
// (x&y flipped and start at zero) 
// -----------------------------
// -- (0,0) -- (1,0) -- (1,0) --
// -- (0,1) -- (1,1) -- (1,1) --
// -----------------------------
func place(a *toolkit.Action, t *andlabsT, newt *andlabsT) bool {
	log(debugAction, "place() START", a.WidgetType, a.Name)

	// add the structure to the array
	if (andlabs[a.WidgetId] == nil) {
		log(logInfo, "place() MAPPED", a.WidgetId, a.ParentId)
		andlabs[a.WidgetId] = newt
		newt.WidgetType = a.WidgetType
	} else {
		log(debugError, "place() DO WHAT?", a.WidgetId, a.ParentId)
		log(debugError, "place() THIS IS BAD")
	}
	log(logInfo, "place() DONE MAPPED", a.WidgetId, a.ParentId)

	if (newt.uiControl == nil) {
		log(debugError, "place() ERROR uiControl == nil", a.ParentId)
		return false
	}

	where := andlabs[a.ParentId]
	if (where == nil) {
		log(debugError, "place() ERROR where == nil", a.ParentId)
		return false
	}

	log(logInfo, "place() switch", where.WidgetType)
	switch where.WidgetType {
	case toolkit.Grid:
		log(debugGrid, "place() Grid try at Parent X,Y =", a.X, a.Y)
		newt.gridX = a.X
		newt.gridY = a.Y
		log(debugGrid, "place() Grid try at gridX,gridY", newt.gridX, newt.gridY)
		// at the very end, subtract 1 from X & Y since andlabs/ui starts counting at zero
		t.uiGrid.Append(newt.uiControl,
			newt.gridY - 1, newt.gridX - 1, 1, 1,
			false, ui.AlignFill, false, ui.AlignFill)
		return true
	case toolkit.Group:
		if (t.uiBox == nil) {
			t.uiGroup.SetChild(newt.uiControl)
			log(debugGrid, "place() hack Group to use this as the box?", a.Name, a.WidgetType)
			t.uiBox  = newt.uiBox
		} else {
			t.uiBox.Append(newt.uiControl, stretchy)
		}
		return true
	case toolkit.Tab:
		t.uiTab.Append(a.Text, newt.uiControl)
		t.boxC += 1
		return true
	case toolkit.Box:
		log(logInfo, "place() uiBox =", t.uiBox)
		log(logInfo, "place() uiControl =", newt.uiControl)
		t.uiBox.Append(newt.uiControl, stretchy)
		t.boxC += 1
		return true
	case toolkit.Window:
		t.uiWindow.SetChild(newt.uiControl)
		return true
	default:
		log(debugError, "place() how?", a.ParentId)
	}
	return false
}
func (p *node) place(n *node) bool {
	log(logInfo, "place() START", n.WidgetType, n.Name)

	if (p.tk == nil) {
		log(logError, "p.tk == nil", p.Name, p.ParentId, p.WidgetType, p.tk)
		log(logError, "n = ", n.Name, n.ParentId, n.WidgetType, n.tk)
		panic("p.tk == nil")
	}

	log(logInfo, "place() switch", p.WidgetType)
	switch p.WidgetType {
	case toolkit.Grid:
		log(debugGrid, "place() Grid try at Parent X,Y =", n.X, n.Y)
		n.tk.gridX = n.X
		n.tk.gridY = n.Y
		log(debugGrid, "place() Grid try at gridX,gridY", n.tk.gridX, n.tk.gridY)
		// at the very end, subtract 1 from X & Y since andlabs/ui starts counting at zero
		p.tk.uiGrid.Append(n.tk.uiControl,
			n.tk.gridY - 1, n.tk.gridX - 1, 1, 1,
			false, ui.AlignFill, false, ui.AlignFill)
		return true
	case toolkit.Group:
		if (p.tk.uiBox == nil) {
			p.tk.uiGroup.SetChild(n.tk.uiControl)
			log(debugGrid, "place() hack Group to use this as the box?", n.Name, n.WidgetType)
			p.tk.uiBox  = n.tk.uiBox
		} else {
			p.tk.uiBox.Append(n.tk.uiControl, stretchy)
		}
		return true
	case toolkit.Tab:
		if (p.tk.uiTab == nil) {
			log(logError, "p.tk.uiTab == nil", p.tk)
			panic("p.tk.uiTab == nil")
		}
		if (n.tk.uiControl == nil) {
			log(logError, "n.tk.uiControl == nil", n.tk)
			panic("n.tk.uiControl == nil")
		}
		p.tk.uiTab.Append(n.Text, n.tk.uiControl)
		p.tk.boxC += 1
		return true
	case toolkit.Box:
		log(logInfo, "place() uiBox =", p.tk.uiBox)
		log(logInfo, "place() uiControl =", n.tk.uiControl)
		p.tk.uiBox.Append(n.tk.uiControl, stretchy)
		p.tk.boxC += 1
		return true
	case toolkit.Window:
		p.tk.uiWindow.SetChild(n.tk.uiControl)
		return true
	default:
		log(debugError, "place() how? Parent =", p.WidgetId, p.WidgetType)
	}
	return false
}