-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
623 lines (535 loc) · 17.9 KB
/
main.go
File metadata and controls
623 lines (535 loc) · 17.9 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package main
import (
"fmt"
"image"
"image/color"
"strings"
imgui "github.com/AllenDang/cimgui-go"
"github.com/AllenDang/giu"
"github.com/jetsetilly/ivycel/cells"
"github.com/jetsetilly/ivycel/engine"
"github.com/jetsetilly/ivycel/engine/ivy"
"github.com/jetsetilly/ivycel/fonts"
"github.com/jetsetilly/ivycel/references"
"github.com/jetsetilly/ivycel/worksheet"
)
type ivycel struct {
ivy ivy.Ivy
worksheet worksheet.Worksheet
cellNormalStyle *giu.StyleSetter
cellReadOnlyStyle *giu.StyleSetter
cellEditStyle *giu.StyleSetter
contextMenuStyle *giu.StyleSetter
headerStyle *giu.StyleSetter
// badge styling should push the badges style first and then the specific badge type
badges *giu.StyleSetter
outputBaseBadge *giu.StyleSetter
inputBaseBadge *giu.StyleSetter
statusBarHeight int
boldFont *giu.FontInfo
// fonts should be prepared as soon as possible in order for them to
// be ready on the frame they are required. without preloading the
// loading may be visible to the user
fontsPreloaded bool
// the current context menu level being shown for cells
cellContextMenuLevel int
}
type worksheetUser struct {
selected *cells.Cell
editing *cells.Cell
// focus either the cell being edited or the formula bar on the next update
focusCell bool
focusFormula bool
}
type cellUser struct {
// the edit cursor position is stored for each cell. however, because only
// one cell can be in the 'editing' mode at once, we can probably keep this
// value in the ivycel type. but this seems more correct
editCursorPosition int
}
// make sure the required fonts have been loaded at the correct size
func (iv *ivycel) preloadFonts() {
if iv.fontsPreloaded {
return
}
iv.fontsPreloaded = true
giu.Style().
SetFont(iv.boldFont).
SetFontSize(fonts.BadgeFontSize).
SetColor(giu.StyleColorText, color.Transparent).
To(giu.Label(""))
}
// insert text into to cell being edited. insertion is done at the current
// cursor position
func (iv *ivycel) insertIntoCellEdit(insert string) {
editCell := iv.worksheet.User.(*worksheetUser).editing
pos := editCell.User.(*cellUser).editCursorPosition
editCell.Entry = fmt.Sprintf("%s%s%s",
editCell.Entry[:pos],
insert,
editCell.Entry[pos:],
)
// advance cursor position of edit cell
editCell.User.(*cellUser).editCursorPosition += len(insert)
// cell being editing will need to be refocused after double-click
iv.worksheet.User.(*worksheetUser).focusCell = true
}
// cell context menu is drawn for cell but not if it's being edited. however, if another cell is
// being edited then that will affect the options offered.
func (iv *ivycel) cellContextMenu(cell *cells.Cell) giu.Widget {
if iv.worksheet.User.(*worksheetUser).editing != nil {
editCell := iv.worksheet.User.(*worksheetUser).editing
menu := giu.ContextMenu().Layout(
giu.Custom(func() {
iv.contextMenuStyle.Push()
defer iv.contextMenuStyle.Pop()
giu.Column(
giu.Label(fmt.Sprintf("Editing %s", editCell.Position().Reference())),
giu.Spacing(),
giu.Separator(),
giu.Spacing(),
).Build()
giu.Label("Insert...").Build()
giu.MenuItem(fmt.Sprintf(" Reference to %s", cell.Position().Reference())).
OnClick(func() {
iv.insertIntoCellEdit(references.WrapCellReference(cell.Position().Reference()))
}).Build()
if cell.Parent() != nil {
giu.MenuItem(fmt.Sprintf(" Reference to parent (%s)", cell.Parent().Position().Reference())).
OnClick(func() {
iv.insertIntoCellEdit(references.WrapCellReference(cell.Parent().Position().Reference()))
}).Build()
} else if cell.HasChildren() {
giu.MenuItem(fmt.Sprintf(" Reference to root of %s", cell.Position().Reference())).
OnClick(func() {
iv.insertIntoCellEdit(references.WrapCellReference(
fmt.Sprintf("%s%s", cell.Position().Reference(), cell.RootIndex()),
))
}).Build()
}
if strings.TrimSpace(cell.Result()) != "" {
giu.MenuItem(fmt.Sprintf(" Literal value of %v", cell.Result())).
OnClick(func() {
iv.insertIntoCellEdit(cell.Result())
}).Build()
}
}),
)
return menu
}
cellBase := cell.Base()
inputBase := func(label string, newBase int) giu.Widget {
return giu.MenuItem(label).Selected(cellBase.Input == newBase).OnClick(func() {
cell.SetBase(engine.Base{Input: newBase, Output: cellBase.Output})
})
}
outputBase := func(label string, newBase int) giu.Widget {
return giu.MenuItem(label).Selected(cellBase.Output == newBase).OnClick(func() {
cell.SetBase(engine.Base{Input: cellBase.Input, Output: newBase})
})
}
return giu.ContextMenu().Layout(
giu.Custom(func() {
iv.contextMenuStyle.Push()
defer iv.contextMenuStyle.Pop()
giu.Column(
giu.Label(fmt.Sprintf("Cell %s", cell.Position().Reference())),
giu.Spacing(),
giu.Separator(),
giu.Spacing(),
giu.MenuItem("Clear").
Enabled(cell.Entry != "").
OnClick(func() {
cell.Entry = ""
cell.Commit(true)
iv.worksheet.RecalculateAll()
}),
giu.Menu("Input Base").Layout(
inputBase("Binary", 2),
inputBase("Octal", 8),
inputBase("Decimal", 10),
inputBase("Hexadecimal", 16),
giu.Spacing(),
giu.Separator(),
giu.Spacing(),
giu.MenuItem("Reset").
Enabled(cellBase != iv.ivy.Base()).
OnClick(func() {
base := cellBase
base.Input = iv.ivy.Base().Input
cell.SetBase(base)
}),
),
giu.Menu("Output Base").Layout(
outputBase("Binary", 2),
outputBase("Octal", 8),
outputBase("Decimal", 10),
outputBase("Hexadecimal", 16),
giu.Spacing(),
giu.Separator(),
giu.Spacing(),
giu.MenuItem("Reset").
Enabled(cellBase != iv.ivy.Base()).
OnClick(func() {
base := cellBase
base.Output = iv.ivy.Base().Output
cell.SetBase(base)
}),
),
).Build()
}),
)
}
func (iv *ivycel) layout() {
iv.preloadFonts()
var selected *giu.LabelWidget
selected = giu.Label(iv.worksheet.User.(*worksheetUser).selected.Position().Reference())
var formula *giu.InputTextWidget
formula = giu.InputText(&iv.worksheet.User.(*worksheetUser).selected.Entry).
Flags(giu.InputTextFlagsEnterReturnsTrue).
OnChange(func() {
iv.worksheet.User.(*worksheetUser).selected.Commit(true)
iv.worksheet.RecalculateAll()
iv.worksheet.User.(*worksheetUser).editing = nil
iv.worksheet.User.(*worksheetUser).focusFormula = true
}).
Size(-1)
// the main body of the spreadsheet is a table
var worksheet *giu.TableWidget
{
rowCount, colCt := iv.worksheet.Size()
worksheet = giu.Table().
Size(-1, -1-float32(iv.statusBarHeight)).
Freeze(1, 1).
Flags(giu.TableFlagsScrollY | giu.TableFlagsScrollX | giu.TableFlagsResizable).
FastMode(true).
NoHeader(true)
// prepare columns for adding to table
var cols []*giu.TableColumnWidget
// first column is the row number and should not be resizeable
cols = append(cols, giu.TableColumn("").Flags(giu.TableColumnFlagsNoResize))
// remaining columns are worksheet columns numbered from "A"
for range colCt {
c := giu.TableColumn("").
Flags(giu.TableColumnFlagsWidthFixed).
InnerWidthOrWeight(100)
cols = append(cols, c)
}
// add columns to table
worksheet.Columns(cols...)
// height of each row if fixed
rowHeight := imgui.CalcTextSize("X").Y
_, y := giu.GetItemInnerSpacing()
rowHeight += y * 2
// width of row header
rowHeaderWidth, _ := giu.CalcTextSize(strings.Repeat("X", len(fmt.Sprintf("%d", rowCount))+1))
// prepare rows for adding to table
var rows []*giu.TableRowWidget
{ // add column headers manually
var rowCols []giu.Widget
rowCols = append(rowCols, giu.Label(""))
for coli := range colCt {
rowCols = append(rowCols,
giu.Custom(func() {
col := cells.NumericToBase26(coli)
iv.headerStyle.To(
giu.Button(col).
Size(-1, fonts.WorksheetHeaderSize),
).Build()
giu.ContextMenu().Layout(giu.Custom(func() {
iv.contextMenuStyle.Push()
defer iv.contextMenuStyle.Pop()
giu.Column(
giu.Selectable(fmt.Sprintf("Insert column before %s", col)).
OnClick(func() {
iv.worksheet.InsertColumn(coli)
}),
).Build()
})).Build()
}),
)
}
rows = append(rows, giu.TableRow(rowCols...))
}
for rowi := range rowCount {
var rowCols []giu.Widget
// first column of each row is the row number
rowCols = append(rowCols, giu.Custom(func() {
lbl := fmt.Sprintf(" %d", rowi+1)
w, _ := giu.CalcTextSize(lbl)
iv.headerStyle.To(
giu.Button(lbl).Size(w, rowHeight).
Size(rowHeaderWidth, rowHeight),
).Build()
giu.ContextMenu().Layout(giu.Custom(func() {
iv.contextMenuStyle.Push()
defer iv.contextMenuStyle.Pop()
giu.Column(
giu.Selectable(fmt.Sprintf("Insert row before row %d", rowi+1)).
OnClick(func() {
iv.worksheet.InsertRow(rowi)
}),
).Build()
})).Build()
}))
for coli := range colCt {
// reference to the cell at row/column number
cell := iv.worksheet.Cell(rowi, coli)
var badges giu.Widget
if !cell.ReadOnly() {
bs := cell.Base()
badges = giu.Custom(func() {
var pos image.Point
giu.SameLine()
pos = giu.GetCursorScreenPos().Sub(image.Point{X: 5, Y: 2})
const badgeSpacing = 8
iv.badges.Push()
defer iv.badges.Pop()
if bs.Output != iv.ivy.Base().Output {
iv.outputBaseBadge.Push()
defer iv.outputBaseBadge.Pop()
txt := fmt.Sprintf("%d", bs.Output)
pos = pos.Sub(image.Point{X: int(imgui.CalcTextSize(txt).X) + badgeSpacing})
giu.SetCursorScreenPos(pos)
giu.Button(txt).Build()
}
if bs.Input != iv.ivy.Base().Input {
iv.inputBaseBadge.Push()
defer iv.inputBaseBadge.Pop()
txt := fmt.Sprintf("%d", bs.Input)
pos = pos.Sub(image.Point{X: int(imgui.CalcTextSize(txt).X) + badgeSpacing})
giu.SetCursorScreenPos(pos)
giu.Button(txt).Build()
}
})
}
// how we display the cell depends on whether the cell is the
// one currently being edited
if iv.worksheet.User.(*worksheetUser).editing == cell {
giu.SetKeyboardFocusHere()
celInp := giu.InputText(&cell.Entry).Size(-1)
// escape key cancels changes and deactivates the input text
// for the cell
if giu.IsKeyPressed(giu.KeyEscape) {
iv.worksheet.User.(*worksheetUser).editing = nil
}
// CalbackAlways flag so we can update the editCursorPosition every keypress
// and EnterReturnsTrue so that OnChange() is not triggered until editing
// has finished
celInp.Flags(giu.InputTextFlagsCallbackAlways | giu.InputTextFlagsEnterReturnsTrue)
// keep track of current cursor position in the input
// widget. we use this to insert cell references at the
// correct point
celInp.Callback(func(data imgui.InputTextCallbackData) int {
if iv.worksheet.User.(*worksheetUser).focusCell {
iv.worksheet.User.(*worksheetUser).focusCell = false
data.SetCursorPos(int32(cell.User.(*cellUser).editCursorPosition))
data.ClearSelection()
}
cell.User.(*cellUser).editCursorPosition = int(data.CursorPos())
return 0
})
// on change function is only called on "enter returns true"
// commit changes
celInp.OnChange(func() {
iv.worksheet.User.(*worksheetUser).editing = nil
cell.Commit(true)
iv.worksheet.RecalculateAll()
})
rowCols = append(rowCols,
giu.Custom(func() {
iv.cellEditStyle.Push()
defer iv.cellEditStyle.Pop()
if iv.worksheet.User.(*worksheetUser).focusCell {
// focusCell flag will be reset in the input widget's callback
// function above. we do this because setting the keyboard focus
// selects the entire contents of the input and we don't want that.
// delaying the flag reset allows us to clear the selection and move
// the input cursor
giu.SetKeyboardFocusHere()
}
celInp.Build()
if badges != nil {
badges.Build()
}
}),
)
} else {
// each cell is a button with a tooltip. the tooltip can be
// an empty widget meaning that it will never appear
var cel *giu.ButtonWidget
var tip giu.Widget
if err := cell.Error(); err != nil {
cel = giu.Button("???")
tip = giu.Tooltip(err.Error())
} else {
cel = giu.Button(cell.Result())
tip = giu.Custom(func() {})
if warn := cell.Warning(); warn != nil {
tip = giu.Tooltip(warn.Error())
}
}
// each cell is the width of the column it is in and the
// height of the row
cel.Size(-1, rowHeight)
// event handler for cell deals with mouse clicks. we prefer
// this to the Button.OnClick() functio
var ev *giu.EventHandler
ev = giu.Event()
ev.OnClick(giu.MouseButtonLeft, func() {
if iv.worksheet.User.(*worksheetUser).editing == nil {
iv.worksheet.User.(*worksheetUser).selected = cell
}
})
ev.OnDClick(giu.MouseButtonLeft, func() {
if iv.worksheet.User.(*worksheetUser).editing != nil {
iv.insertIntoCellEdit(references.WrapCellReference(cell.Position().Reference()))
} else if !cell.ReadOnly() {
iv.worksheet.User.(*worksheetUser).editing = cell
iv.worksheet.User.(*worksheetUser).focusCell = true
}
})
// decide on display style for cell
var sty *giu.StyleSetter
if cell.ReadOnly() {
sty = iv.cellReadOnlyStyle
} else {
sty = iv.cellNormalStyle
}
rowCols = append(rowCols,
giu.Custom(func() {
sty.Push()
defer sty.Pop()
giu.Row(
cel, iv.cellContextMenu(cell),
ev, tip,
).Build()
if badges != nil {
badges.Build()
}
}))
}
}
rows = append(rows, giu.TableRow(rowCols...))
}
// add rows to table
worksheet.Rows(rows...)
}
// the status bar appears at the bottom of the window. the height of the
// status bar is measured during the layout directive below
var statusBar *giu.LabelWidget
{
lastErr := iv.ivy.LastError()
if lastErr == nil {
statusBar = giu.Label("Ready")
} else {
statusBar = giu.Label(lastErr.Error())
}
}
giu.SingleWindowWithMenuBar().Layout(
giu.MenuBar().Layout(
giu.Spacing(),
giu.Menu(string(fonts.FileMenu)).Layout(
giu.Label("File"),
giu.Separator(),
giu.MenuItem("Open..."),
giu.MenuItem("Save..."),
),
),
giu.Style().SetFontSize(fonts.WorksheetFontSize).To(
giu.Row(
selected,
giu.Custom(func() {
if iv.worksheet.User.(*worksheetUser).focusFormula {
giu.SetKeyboardFocusHere()
iv.worksheet.User.(*worksheetUser).focusFormula = false
}
}),
giu.Custom(func() {
if iv.worksheet.User.(*worksheetUser).selected.ReadOnly() {
giu.Style().SetDisabled(true).Push()
defer giu.Style().SetDisabled(true).Pop()
}
formula.Build()
}),
),
worksheet,
),
// measure height of status bar
giu.Custom(func() {
iv.statusBarHeight = giu.GetCursorScreenPos().Y
}),
giu.Spacing(),
statusBar,
giu.Custom(func() {
iv.statusBarHeight = giu.GetCursorScreenPos().Y - iv.statusBarHeight
}),
)
}
func (iv *ivycel) setStyling() {
iv.cellNormalStyle = giu.Style().
SetStyleFloat(giu.StyleVarFrameBorderSize, 0).
SetStyleFloat(giu.StyleVarFrameRounding, 0).
SetStyle(giu.StyleVarButtonTextAlign, 0, 0).
SetColor(giu.StyleColorText, color.RGBA{R: 255, G: 255, B: 255, A: 255})
iv.cellReadOnlyStyle = giu.Style().
SetStyleFloat(giu.StyleVarFrameBorderSize, 0).
SetStyleFloat(giu.StyleVarFrameRounding, 0).
SetStyle(giu.StyleVarButtonTextAlign, 0, 0).
SetColor(giu.StyleColorButton, color.Transparent)
iv.cellEditStyle = giu.Style().
SetStyleFloat(giu.StyleVarFrameBorderSize, 2).
SetStyleFloat(giu.StyleVarFrameRounding, 3).
SetColor(giu.StyleColorBorder, color.RGBA{R: 100, G: 100, B: 200, A: 255})
iv.contextMenuStyle = giu.Style().
SetFontSize(fonts.ContextMenuFontSize)
vcol := imgui.CurrentStyle().Colors()[giu.StyleColorTableRowBg]
col := color.RGBA{R: uint8(vcol.X), G: uint8(vcol.Y), B: uint8(vcol.Z), A: uint8(vcol.W)}
iv.headerStyle = giu.Style().
SetFontSize(fonts.WorksheetHeaderSize).
SetStyleFloat(giu.StyleVarFrameBorderSize, 0).
SetStyle(giu.StyleVarFramePadding, 0, 0).
SetColor(giu.StyleColorButton, col).
SetColor(giu.StyleColorButtonActive, col).
SetColor(giu.StyleColorButtonHovered, col)
iv.badges = giu.Style().
SetFont(iv.boldFont).
SetFontSize(fonts.BadgeFontSize).
SetStyle(giu.StyleVarFramePadding, 2, 2).
SetStyleFloat(giu.StyleVarFrameRounding, 5).
SetStyleFloat(giu.StyleVarFrameBorderSize, 0)
col = color.RGBA{R: 255, G: 100, B: 100, A: 200}
iv.outputBaseBadge = giu.Style().
SetColor(giu.StyleColorButton, col).
SetColor(giu.StyleColorButtonActive, col).
SetColor(giu.StyleColorButtonHovered, col)
col = color.RGBA{R: 100, G: 100, B: 255, A: 200}
iv.inputBaseBadge = giu.Style().
SetColor(giu.StyleColorButton, col).
SetColor(giu.StyleColorButtonActive, col).
SetColor(giu.StyleColorButtonHovered, col)
}
func (iv *ivycel) setFonts() {
// adding more than one default font will merge the two fonts together. the order is important
// because we want the normal alphanumeric glyphs from HackRegular and not FontAwesome
giu.Context.FontAtlas.SetDefaultFontFromBytes(fonts.FontAwesome, fonts.NormalFontSize)
giu.Context.FontAtlas.SetDefaultFontFromBytes(fonts.Hack_Regular, fonts.NormalFontSize)
iv.boldFont = giu.Context.FontAtlas.AddFontFromBytes("Hack-Bold", fonts.Hack_Bold, fonts.NormalFontSize)
}
func main() {
iv := ivycel{
ivy: ivy.New(),
}
addCellUser := func(cell *cells.Cell) {
cell.User = &cellUser{}
}
iv.worksheet = worksheet.NewWorksheet(&iv.ivy, 100, 100, addCellUser)
iv.worksheet.User = &worksheetUser{
selected: iv.worksheet.Cell(0, 0),
focusFormula: true,
}
wnd := giu.NewMasterWindow("Ivycel", 800, 600, 0)
iv.setFonts()
iv.setStyling()
wnd.Run(iv.layout)
}