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

import "log"

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

import "github.com/gookit/config"
import "github.com/davecgh/go-spew/spew"

//
// 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	GuiDataStructure

type GuiDataStructure struct {
	State		string
	MainWindow	*ui.Window
	Width		int
	Height		int
	ButtonClick	func(*ButtonMap)
	CurrentVM	string
	MyArea		*ui.Area

	// general information
	Version		string
	GitCommit	string
	GoVersion	string
	Buildtime	string
	HomeDir		string
	Debug		bool

	// official hostname and IPv6 address for this box
	Hostname	string
	IPv6		string

	// account entry textboxes
	AccNick	string
	AccUser	string
	AccPass	string

	// stuff for the splash screen / setup tabs
	cloudWindow	*ui.Window
	cloudTab	*ui.Tab
	cloudBox	*ui.Box
	smallBox	*ui.Box

	mainwin		*ui.Window
	maintab		*ui.Tab
	tabcount	int
	allButtons	[]ButtonMap

	// stuff for the 'area'
	fontButton	*ui.FontButton
	attrstr		*ui.AttributedString
	splashArea	*ui.Area
}

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

type ButtonMap struct {
	B		*ui.Button
	FB		*ui.FontButton
	onClick		func (int, string)
	onChanged	func (int, string)
	custom		func (*ButtonMap, string)
	Name		string	// the text on the button
	Note		string	// what type of button
	AccNick		string  // what account this button is for
}

/*
func AddNewTab(mytab *ui.Tab, newbox ui.Control, tabOffset int) {
	mytab.Append("Cloud Info", newbox)
	mytab.SetMargined(tabOffset, true)
}
*/

func InitColumns(mh *TableData, parts []TableColumnData) {
	tmpBTindex := 0
	humanID := 0
	for key, foo := range parts {
		log.Println("key, foo =", key, foo)
		// log.Println("mh.Cells =", mh.Cells)
		// log.Println("mh.Human =", mh.Human)

		parts[key].Index = humanID
		humanID += 1

		if (foo.CellType == "BG") {
			mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{})
			initRowBTcolor        (mh, tmpBTindex, parts[key])
			tmpBTindex += 1
		} else if (foo.CellType == "BUTTON") {
			mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString(""))
			initRowButtonColumn   (mh, tmpBTindex,    parts[key].Heading, parts[key])
			tmpBTindex += 1
		} else if (foo.CellType == "TEXTCOLOR") {
			mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString(""))
			mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{})
			initRowTextColorColumn(mh, tmpBTindex, tmpBTindex + 1, parts[key].Heading, ui.TableColor{0.0, 0, 0.9, 1}, parts[key])
			tmpBTindex += 2
		} else if (foo.CellType == "TEXT") {
			mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString(""))
			initRowTextColumn     (mh, tmpBTindex,    parts[key].Heading, parts[key])
			tmpBTindex += 1
		} else {
			panic("I don't know what this is in initColumnNames")
		}
	}
}

func AddTableTab(mytab *ui.Tab, mytabcount int, name string, rowcount int, parts []TableColumnData) *TableData {
	mh := new(TableData)

	mh.RowCount    = rowcount
	mh.Rows        = make([]RowData, mh.RowCount)

	InitColumns(mh, parts)

	model := ui.NewTableModel(mh)
	table := ui.NewTable(
		&ui.TableParams{
			Model:	model,
			RowBackgroundColorModelColumn:	0,	// Row Background color is always index zero
	})

	tmpBTindex := 0
	for key, foo := range parts {
		log.Println(key, foo)
		if (foo.CellType == "BG") {
		} else if (foo.CellType == "BUTTON") {
			tmpBTindex += 1
			table.AppendButtonColumn(foo.Heading, tmpBTindex, ui.TableModelColumnAlwaysEditable)
		} else if (foo.CellType == "TEXTCOLOR") {
			tmpBTindex += 1
			table.AppendTextColumn(foo.Heading, tmpBTindex, ui.TableModelColumnAlwaysEditable,
					&ui.TableTextColumnOptionalParams{
						ColorModelColumn:   tmpBTindex + 1,
			});
			tmpBTindex += 1
		} else if (foo.CellType == "TEXT") {
			tmpBTindex += 1
			table.AppendTextColumn(foo.Heading, tmpBTindex, ui.TableModelColumnAlwaysEditable, nil)
		} else {
			panic("I don't know what this is in initColumnNames")
		}
	}

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)

	vbox.Append(table, true)
	mytab.Append(name, vbox)
	mytab.SetMargined(mytabcount, true)

	vbox.Append(ui.NewVerticalSeparator(), false)

	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	myAddVM := addVmButton("Add Virtual Machine")
	hbox.Append(myAddVM, false)

	myClose := closeButton("Close", mytab)
	hbox.Append(myClose, false)

	vbox.Append(hbox, false)

	return mh
}

func defaultButtonClick(button *ui.Button) {
	log.Println("defaultButtonClick() button =", button)
	for key, foo := range Data.allButtons {
		log.Println("Data.allButtons =", key, foo)
		if Data.allButtons[key].B == button {
			log.Println("\tBUTTON MATCHED")
			log.Println("\tData.allButtons[key].Name", Data.allButtons[key].Name)
			log.Println("\tData.allButtons[key].Note", Data.allButtons[key].Note)
			if (Data.ButtonClick != nil) {
				Data.ButtonClick( &Data.allButtons[key])
			} else if Data.allButtons[key].custom != nil {
				Data.allButtons[key].custom(nil, "BUTTON DOES NOTHING")
			}
			return
		}
	}
	log.Println("\tBUTTON NOT FOUND")
}

func defaultFontButtonClick(button *ui.FontButton) {
	log.Println("defaultButtonClick() button =", button)
	for key, foo := range Data.allButtons {
		log.Println("Data.allButtons =", key, foo)
	}
}

func CreateButton(name string, note string, custom func(*ButtonMap, string)) *ui.Button {
	newB := ui.NewButton(name)

	newB.OnClicked(defaultButtonClick)

	var newmap ButtonMap
	newmap.B = newB
	newmap.Note = note
	newmap.Name = name
	newmap.custom = custom
	Data.allButtons = append(Data.allButtons, newmap)

	return newB
}

func CreateAccountButton(account string, custom func(*ButtonMap, string)) *ui.Button {
	name := "Show " + account
	newB := ui.NewButton(name)

	newB.OnClicked(defaultButtonClick)

	var newmap ButtonMap
	newmap.B = newB
	newmap.Note = "SHOW"
	newmap.Name = name
	newmap.AccNick = account
	newmap.custom = custom
	Data.allButtons = append(Data.allButtons, newmap)

	return newB
}

func CreateLoginButton(account string, custom func(*ButtonMap, string)) *ui.Button {
	name := "Login " + account
	newB := ui.NewButton(name)

	newB.OnClicked(defaultButtonClick)

	var newmap ButtonMap
	newmap.B = newB
	newmap.Note = "LOGIN"
	newmap.Name = name
	newmap.AccNick = account
	newmap.custom = custom
	Data.allButtons = append(Data.allButtons, newmap)

	return newB
}

func CreateFontButton(name string, note string, custom func(*ButtonMap, string)) *ui.FontButton {
	newB := ui.NewFontButton()

	newB.OnChanged(defaultFontButtonClick)

	var newmap ButtonMap
	newmap.FB = newB
	newmap.Note = note
	newmap.custom = custom
	Data.allButtons = append(Data.allButtons, newmap)

	return newB
}

func closeButtonClick(button *ui.Button) {
	log.Println("closeButtonClick() hostname =", config.String("hostname"), button)
	spew.Dump(button)
}

func closeButton(name string, mytab *ui.Tab) ui.Control {
	tmpButton := ui.NewButton(name)
	tmpButton.OnClicked(defaultButtonClick)

	return tmpButton
}

func addVmButtonClick(button *ui.Button) {
	log.Println("addVMButtonClick()")
	spew.Dump(button)
}

func addVmButton(name string) ui.Control {
	tmpButton := ui.NewButton(name)
	tmpButton.OnClicked(addVmButtonClick)

	return tmpButton
}

func SocketError() {
	ui.MsgBoxError(Data.cloudWindow,
		"There was a socket error",
		"More detailed information can be shown here.")
}

func MessageWindow(msg1 string, msg2 string) {
	ui.MsgBox(Data.cloudWindow, msg1, msg2)
}

func ErrorWindow(msg1 string, msg2 string) {
	ui.MsgBoxError(Data.cloudWindow, msg1, msg2)
}