summaryrefslogtreecommitdiff
path: root/structs.go
blob: dca855f257a648b7a9f1ae873e82fd1e6dc19ef8 (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
package gui

import (
	"image/color"
	"log"
	"os"

	"github.com/andlabs/ui"
	"golang.org/x/image/font"

	_ "github.com/andlabs/ui/winmanifest"
)

//
// All GUI Data Structures and functions that are external
// If you need cross platform support, these might only
// be the safe way to interact with the GUI
//
var Data GuiData
var Config GuiConfig

type GuiConfig struct {
	Width      int
	Height     int
	Debug      bool
	DebugTable bool
	Exit       func(*GuiWindow)

	depth      int
	counter    int  // used to make unique ID's
	prefix     string
}

type GuiData struct {
	// a fallback default function to handle mouse events
	// if nothing else is defined to handle them
	MouseClick func(*GuiButton)

	// A map of all the entry boxes
	AllEntries []*GuiEntry
	WindowMap  map[string]*GuiWindow

	// Store access to everything via binary tree's
	NodeMap    map[string]*Node
	NodeArray  []*Node
	NodeSlice  []*Node

	// A map of all buttons everywhere on all
	// windows, all tabs, across all goroutines
	// This is "GLOBAL"
	//
	// This has to work this way because of how
	// andlabs/ui & andlabs/libui work
	AllButtons []*GuiButton
	buttonMap  map[*ui.Button]*GuiButton
}

type GuiTab struct {
	Name   string     // field for human readable name
	Number int        // the andlabs/ui tab index
	Window *GuiWindow // the parent Window
}

//
// stores information on the 'window'
//
// This merges the concept of andlabs/ui *Window and *Tab
//
// More than one Window is not supported in a cross platform
// sense & may never be. On Windows and MacOS, you have to have
// 'tabs'. Even under Linux, more than one Window is currently
// unstable
//
// This code will make a 'GuiWindow' regardless of if it is
// a stand alone window (which is more or less working on Linux)
// or a 'tab' inside a window (which is all that works on MacOS
// and MSWindows.
//
// This struct keeps track of what is in the window so you
// can destroy and replace it with something else
//
type GuiWindow struct {
	Name      string // field for human readable name
	Width     int
	Height    int
	Axis      int  // does it add items to the X or Y axis
	TabNumber *int // the andlabs/ui tab index

	// the callback function to make the window contents
	// MakeWindow	func(*GuiBox) *GuiBox

	// the components of the window
	BoxMap   map[string]*GuiBox
	EntryMap map[string]*GuiEntry
	Area     *GuiArea

	node	*Node

	// andlabs/ui abstraction mapping
	UiWindow *ui.Window
	UiTab    *ui.Tab // if this != nil, the window is 'tabbed'
}

func (gw *GuiWindow) Dump() {
	log.Println("gui.GuiWindow.Dump() Name       = ", gw.Name)
	log.Println("gui.GuiWindow.Dump() node       = ", gw.node)
	log.Println("gui.GuiWindow.Dump() Width      = ", gw.Width)
	log.Println("gui.GuiWindow.Dump() Height     = ", gw.Height)
}

// GuiBox is any type of ui.Hbox or ui.Vbox
// There can be lots of these for each GuiWindow
type GuiBox struct {
	Name   string     // field for human readable name
	Axis   int        // does it add items to the X or Y axis
	Window *GuiWindow // the parent Window

	node	*Node

	// andlabs/ui abstraction mapping
	UiBox *ui.Box
}

func (gb *GuiBox) Dump() {
	log.Println("gui.GuiBox.Dump() Name       = ", gb.Name)
	log.Println("gui.GuiBox.Dump() Axis       = ", gb.Axis)
	log.Println("gui.GuiBox.Dump() GuiWindow  = ", gb.Window)
	log.Println("gui.GuiBox.Dump() node       = ", gb.node)
	log.Println("gui.GuiBox.Dump() UiBox      = ", gb.UiBox)
}

func (s GuiBox) SetTitle(title string) {
	log.Println("DID IT!", title)
	if s.Window == nil {
		return
	}
	if s.Window.UiWindow == nil {
		return
	}
	s.Window.UiWindow.SetTitle(title)
	return
}

func (w *GuiWindow) SetNode(n *Node) {
	if (w.node != nil) {
		w.Dump()
		panic("gui.SetNode() Error not nil")
	}
	w.node = n
	if (w.node == nil) {
		w.Dump()
		panic("gui.SetNode() node == nil")
	}
}

func (b *GuiBox) SetNode(n *Node) {
	if (b.node != nil) {
		b.Dump()
		panic("gui.SetNode() Error not nil")
	}
	b.node = n
	if (b.node == nil) {
		b.Dump()
		panic("gui.SetNode() node == nil")
	}
}

func (w *GuiWindow) FindNode() *Node {
	return w.node
}

func (b *GuiBox) FindNode() *Node {
	log.Println("gui.FindNode() on GuiBox")
	if b.node != nil {
		return b.node
	}
	Data.ListChildren(true)
	b.Dump()
	log.Println("gui.FindNode() on GuiBox is nil")
	os.Exit(-1)
	return nil
}

func (s GuiBox) Append(child ui.Control, x bool) {
	if s.UiBox == nil {
		return
	}
	s.UiBox.Append(child, x)
}

/*
func (s *GuiBox) AddTab(title string, custom ui.Control) *ui.Tab {
	if s.Window == nil {
		return nil
	}
	if s.Window.UiTab == nil {
		return nil
	}

	tab := s.Window.UiTab
	tab.Append(title, custom)
	return tab
}
*/

/*
func (s GuiBox) AddBoxTab(title string) *GuiBox {
	uiTab := s.AddTab(title, initBlankWindow())
	tabSetMargined(uiTab)

	var box *GuiBox
	box = HardBox(s.Window, Xaxis, "jcarrAddBoxTab")
	box.Window.UiTab = uiTab
	return box
}
*/

func (n *Node) AddDemoTab(title string) {
	newNode := n.AddTab(title, makeWindowTemplate())
	newNode.Dump()
	tabSetMargined(newNode.uiTab)
}

func (n *Node) AddDebugTab(title string) {
	newNode := n.AddTab(title, makeWindowDebug())
	newNode.Dump()
	tabSetMargined(newNode.uiTab)
}

func tabSetMargined(tab *ui.Tab) {
	log.Println("tabSetMargined() IGNORE THIS")
	return
	c := tab.NumPages()
	for i := 0; i < c; i++ {
		log.Println("tabSetMargined() i =", i)
		// tab.SetMargined(i, true)
	}
}

// Note: every mouse click is handled
// as a 'Button' regardless of where
// the user clicks it. You could probably
// call this 'GuiMouseClick'
type GuiButton struct {
	Name string  // field for human readable name
	Box  *GuiBox // what box the button click was in

	// a callback function for the main application
	Custom func(*GuiButton)
	Values interface{}
	Color  color.RGBA

	// andlabs/ui abstraction mapping
	B  *ui.Button
	FB *ui.FontButton
	CB *ui.ColorButton
}

// text entry fields
type GuiEntry struct {
	Name      string // field for human readable name
	Edit      bool
	Last      string              // the last value
	Normalize func(string) string // function to 'normalize' the data

	B   *GuiButton
	Box *GuiBox

	// andlabs/ui abstraction mapping
	UiEntry *ui.Entry
}

//
// AREA STRUCTURES START
// AREA STRUCTURES START
// AREA STRUCTURES START
//
type GuiArea struct {
	Button *GuiButton // what button handles mouse events
	Box    *GuiBox

	UiAttrstr *ui.AttributedString
	UiArea    *ui.Area
}

type FontString struct {
	S    string
	Size int
	F    font.Face
	W    font.Weight
}

//
// AREA STRUCTURES END
// AREA STRUCTURES END
// AREA STRUCTURES END
//

//
// TABLE DATA STRUCTURES START
// TABLE DATA STRUCTURES START
// TABLE DATA STRUCTURES START
//

//
// This is the structure that andlabs/ui uses to pass information
// to the GUI. This is the "authoritative" data.
//
type TableData struct {
	RowCount             int             // This is the number of 'rows' which really means data elements not what the human sees
	RowWidth             int             // This is how wide each row is
	Rows                 []RowData       // This is all the table data by row
	generatedColumnTypes []ui.TableValue // generate this dynamically

	Cells [20]CellData
	Human [20]HumanMap

	Box *GuiBox

	lastRow    int
	lastColumn int
}

//
// This maps the andlabs/ui & libui components into a "human"
// readable cell reference list. The reason is that there
// are potentially 3 values for each cell. The Text, the Color
// and an image. These are not always needed so the number
// of fields varies between 1 and 3. Internally, the toolkit
// GUI abstraction needs to list all of them, but it's then
// hard to figure out which column goes with the columns that
// you see when you visually are looking at it like a spreadsheet
//
// This makes a map so that we can say "give me the value at
// row 4 and column 2" and find the fields that are needed
//
// TODO: re-add images and the progress bar (works in andlabs/ui)
//
type HumanCellData struct {
	Name    string // what kind of row is this?
	Text    string
	TextID  int
	Color   color.RGBA
	ColorID int
	Button  *GuiButton
}

type HumanMap struct {
	Name    string // what kind of row is this?
	TextID  int
	ColorID int
}

type TableColumnData struct {
	Index    int
	CellType string
	Heading  string
	Color    string
}

type CellData struct {
	Index   int
	HumanID int
	Name    string // what type of cell is this?
}

// hmm. will this stand the test of time?
type RowData struct {
	Name   string // what kind of row is this?
	Status string // status of the row?
	/*
		// TODO: These may or may not be implementable
		// depending on if it's possible to detect the bgcolor or what row is selected
		click		func()			// what function to call if the user clicks on it
		doubleclick	func()			// what function to call if the user double clicks on it
	*/
	HumanData [20]HumanCellData
}

//
// TABLE DATA STRUCTURES END
//