table.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "fmt"
  7. "math"
  8. "github.com/g3n/engine/math32"
  9. "github.com/g3n/engine/window"
  10. )
  11. const (
  12. OnTableClick = "onTableClick"
  13. )
  14. //
  15. // Table implements a panel which can contains child panels
  16. // organized in rows and columns.
  17. //
  18. type Table struct {
  19. Panel // Embedded panel
  20. styles *TableStyles // pointer to current styles
  21. cols []*TableColumn // array of columns descriptors
  22. colmap map[string]*TableColumn // maps column id to column descriptor
  23. firstRow int // index of the first visible row
  24. lastRow int // index of the last visible row
  25. rows []*tableRow // array of table rows
  26. headerHeight float32 // header height
  27. vscroll *ScrollBar // vertical scroll bar
  28. showHeader bool // header visibility flag
  29. }
  30. // TableColumn describes a table column
  31. type TableColumn struct {
  32. Id string // Column id used to reference the column. Must be unique
  33. Name string // Column name shown in the header
  34. Width float32 // Column preferable width in pixels
  35. Hidden bool // Hidden flag
  36. Format string // Format string for numbers and strings
  37. Alignment Align // Cell content alignment: AlignNone|AlignLeft|AlignCenter|AlignRight
  38. Expand int // Width expansion factor
  39. order int // show order
  40. header *Panel // header panel
  41. label *Label // header label
  42. }
  43. // TableHeaderStyle describes the style of the table header
  44. type TableHeaderStyle struct {
  45. Border BorderSizes
  46. Paddings BorderSizes
  47. BorderColor math32.Color4
  48. BgColor math32.Color
  49. FgColor math32.Color
  50. }
  51. // TableRowStyle describes the style of the table row
  52. type TableRowStyle struct {
  53. Border BorderSizes
  54. Paddings BorderSizes
  55. BorderColor math32.Color4
  56. BgColor math32.Color
  57. FgColor math32.Color
  58. }
  59. // TableRowStyles describes all styles for the table row
  60. type TableRowStyles struct {
  61. Normal TableRowStyle
  62. Selected TableRowStyle
  63. }
  64. // TableStyles describes all styles of the table header and rows
  65. type TableStyles struct {
  66. Header *TableHeaderStyle
  67. Row *TableRowStyles
  68. }
  69. // TableClickEvent describes a mouse click event over a table
  70. // It contains the original mouse event plus additional information
  71. type TableClickEvent struct {
  72. window.MouseEvent // Embedded window mouse event
  73. X float32 // Table content area X coordinate
  74. Y float32 // Table content area Y coordinate
  75. Header bool // True if header was clicked
  76. Row int // Index of table row (may be -1)
  77. Col string // Id of table column (may be empty)
  78. }
  79. // tableRow is panel which contains an entire table row of cells
  80. type tableRow struct {
  81. Panel // embedded panel
  82. selected bool // row selected flag
  83. cells []*tableCell // array of row cells
  84. }
  85. // tableCell is a panel which contains one cell (a label)
  86. type tableCell struct {
  87. Panel // embedded panel
  88. label Label // cell label
  89. value interface{} // cell current value
  90. }
  91. // NewTable creates and returns a pointer to a new Table with the
  92. // specified width, height and columns
  93. func NewTable(width, height float32, cols []TableColumn) (*Table, error) {
  94. t := new(Table)
  95. t.Panel.Initialize(width, height)
  96. t.styles = &StyleDefault.Table
  97. t.showHeader = true
  98. // Checks columns descriptors
  99. t.colmap = make(map[string]*TableColumn)
  100. t.cols = make([]*TableColumn, 0)
  101. for i := 0; i < len(cols); i++ {
  102. // Make a copy of the column descriptor argument and saves its pointer
  103. c := cols[i]
  104. t.cols = append(t.cols, &c)
  105. // Column id must not be empty
  106. if c.Id == "" {
  107. return nil, fmt.Errorf("Column with empty id")
  108. }
  109. // Column id must be unique
  110. if t.colmap[c.Id] != nil {
  111. return nil, fmt.Errorf("Column with duplicate id")
  112. }
  113. // Sets default format and order
  114. if c.Format == "" {
  115. c.Format = "%v"
  116. }
  117. c.order = i
  118. t.colmap[c.Id] = &c
  119. }
  120. // Create header panels
  121. for i := 0; i < len(t.cols); i++ {
  122. c := t.cols[i]
  123. c.header = NewPanel(0, 0)
  124. t.applyHeaderStyle(c.header)
  125. c.label = NewLabel(c.Name)
  126. c.header.Add(c.label)
  127. width := c.Width
  128. if width < c.label.Width()+c.header.MinWidth() {
  129. width = c.label.Width() + c.header.MinWidth()
  130. }
  131. c.header.SetContentSize(width, c.label.Height())
  132. t.headerHeight = c.header.Height()
  133. t.Panel.Add(c.header)
  134. }
  135. t.recalcHeader()
  136. // Subscribe to events
  137. t.Panel.Subscribe(OnMouseUp, t.onMouse)
  138. t.Panel.Subscribe(OnMouseDown, t.onMouse)
  139. t.Panel.Subscribe(OnKeyDown, t.onKeyEvent)
  140. t.Panel.Subscribe(OnKeyRepeat, t.onKeyEvent)
  141. t.Panel.Subscribe(OnResize, func(evname string, ev interface{}) {
  142. t.recalc()
  143. })
  144. return t, nil
  145. }
  146. // ShowHeader shows or hides the table header
  147. func (t *Table) ShowHeader(show bool) {
  148. if t.showHeader == show {
  149. return
  150. }
  151. t.showHeader = show
  152. for i := 0; i < len(t.cols); i++ {
  153. c := t.cols[i]
  154. c.header.SetVisible(t.showHeader)
  155. }
  156. t.recalc()
  157. }
  158. // ShowColumn sets the visibility of the column with the specified id
  159. // If the column id does not exit the function panics.
  160. func (t *Table) ShowColumn(col string, show bool) {
  161. c := t.colmap[col]
  162. if c == nil {
  163. panic("Invalid column id")
  164. }
  165. if c.Hidden == !show {
  166. return
  167. }
  168. c.Hidden = !show
  169. t.recalcHeader()
  170. // Recalculates all rows
  171. for ri := 0; ri < len(t.rows); ri++ {
  172. trow := t.rows[ri]
  173. t.recalcRow(trow)
  174. }
  175. t.recalc()
  176. }
  177. // ShowAllColumns shows all the table columns
  178. func (t *Table) ShowAllColumns() {
  179. recalc := false
  180. for ci := 0; ci < len(t.cols); ci++ {
  181. c := t.cols[ci]
  182. if c.Hidden {
  183. c.Hidden = false
  184. recalc = true
  185. }
  186. }
  187. if !recalc {
  188. return
  189. }
  190. t.recalcHeader()
  191. // Recalculates all rows
  192. for ri := 0; ri < len(t.rows); ri++ {
  193. trow := t.rows[ri]
  194. t.recalcRow(trow)
  195. }
  196. t.recalc()
  197. }
  198. // RowCount returns the current number of rows in the table
  199. func (t *Table) RowCount() int {
  200. return len(t.rows)
  201. }
  202. // SetRows clears all current rows of the table and
  203. // sets new rows from the specifying parameter.
  204. // Each row is a map keyed by the colum id.
  205. // The map value currently can be a string or any number type
  206. // If a row column is not found it is ignored
  207. func (t *Table) SetRows(values []map[string]interface{}) {
  208. // Add missing rows
  209. if len(values) > len(t.rows) {
  210. count := len(values) - len(t.rows)
  211. for row := 0; row < count; row++ {
  212. t.insertRow(len(t.rows), nil)
  213. }
  214. // Remove remaining rows
  215. } else if len(values) < len(t.rows) {
  216. for row := len(values); row < len(t.rows); row++ {
  217. t.removeRow(row)
  218. }
  219. }
  220. // Set rows values
  221. for row := 0; row < len(values); row++ {
  222. t.SetRow(row, values[row])
  223. }
  224. t.firstRow = 0
  225. t.recalc()
  226. }
  227. // SetRow sets the value of all the cells of the specified row from
  228. // the specified map indexed by column id.
  229. func (t *Table) SetRow(row int, values map[string]interface{}) {
  230. if row < 0 || row >= len(t.rows) {
  231. panic("Invalid row index")
  232. }
  233. for ci := 0; ci < len(t.cols); ci++ {
  234. c := t.cols[ci]
  235. cv := values[c.Id]
  236. if cv == nil {
  237. continue
  238. }
  239. t.SetCell(row, c.Id, values[c.Id])
  240. }
  241. t.recalcRow(t.rows[row])
  242. }
  243. // SetCell sets the value of the cell specified by its row and column id
  244. func (t *Table) SetCell(row int, colid string, value interface{}) {
  245. if row < 0 || row >= len(t.rows) {
  246. panic("Invalid row index")
  247. }
  248. c := t.colmap[colid]
  249. if c == nil {
  250. return
  251. }
  252. cell := t.rows[row].cells[c.order]
  253. cell.label.SetText(fmt.Sprintf(c.Format, value))
  254. }
  255. // SetColFormat sets the formatting string (Printf) for the specified column
  256. // Update must be called to update the table.
  257. func (t *Table) SetColFormat(id, format string) error {
  258. c := t.colmap[id]
  259. if c == nil {
  260. return fmt.Errorf("No column with id:%s", id)
  261. }
  262. c.Format = format
  263. return nil
  264. }
  265. // AddRow adds a new row at the end of the table with the specified values
  266. func (t *Table) AddRow(values map[string]interface{}) {
  267. t.InsertRow(len(t.rows), values)
  268. }
  269. // InsertRow inserts the specified values in a new row at the specified index
  270. func (t *Table) InsertRow(row int, values map[string]interface{}) {
  271. t.insertRow(row, values)
  272. t.recalc()
  273. }
  274. // RemoveRow removes from the specified row from the table
  275. func (t *Table) RemoveRow(row int) {
  276. // Checks row index
  277. if row < 0 || row >= len(t.rows) {
  278. panic("Invalid row index")
  279. }
  280. t.removeRow(row)
  281. t.recalc()
  282. }
  283. // SelectedRow returns the index of the currently selected row
  284. // or -1 if no row selected
  285. func (t *Table) SelectedRow() int {
  286. for ri := 0; ri < len(t.rows); ri++ {
  287. if t.rows[ri].selected {
  288. return ri
  289. }
  290. }
  291. return -1
  292. }
  293. // insertRow is the internal version of InsertRow which does not call recalc()
  294. func (t *Table) insertRow(row int, values map[string]interface{}) {
  295. // Checks row index
  296. if row < 0 || row > len(t.rows) {
  297. panic("Invalid row index")
  298. }
  299. // Creates tableRow panel
  300. trow := new(tableRow)
  301. trow.Initialize(0, 0)
  302. trow.cells = make([]*tableCell, 0)
  303. for ci := 0; ci < len(t.cols); ci++ {
  304. // Creates tableRow cell panel
  305. cell := new(tableCell)
  306. cell.Initialize(0, 0)
  307. cell.label.initialize("", StyleDefault.Font)
  308. cell.Add(&cell.label)
  309. trow.cells = append(trow.cells, cell)
  310. trow.Panel.Add(cell)
  311. }
  312. t.Panel.Add(trow)
  313. // Inserts tableRow in the table rows at the specified index
  314. t.rows = append(t.rows, nil)
  315. copy(t.rows[row+1:], t.rows[row:])
  316. t.rows[row] = trow
  317. t.updateRowStyle(row)
  318. // Sets the new row values from the specified map
  319. if values != nil {
  320. t.SetRow(row, values)
  321. }
  322. t.recalcRow(trow)
  323. }
  324. // ScrollDown scrolls the table the specified number of rows down if possible
  325. func (t *Table) scrollDown(n int, selFirst bool) {
  326. // Calculates number of rows to scroll down
  327. maxFirst := t.calcMaxFirst()
  328. maxScroll := maxFirst - t.firstRow
  329. if maxScroll <= 0 {
  330. return
  331. }
  332. if n > maxScroll {
  333. n = maxScroll
  334. }
  335. t.firstRow += n
  336. // Update scroll bar if visible
  337. if t.vscroll != nil && t.vscroll.Visible() {
  338. t.vscroll.SetValue(float32(t.firstRow) / float32(maxFirst))
  339. }
  340. if selFirst {
  341. t.selectRow(t.firstRow)
  342. }
  343. t.recalc()
  344. return
  345. }
  346. // ScrollUp scrolls the table the specified number of rows up if possible
  347. func (t *Table) scrollUp(n int, selLast bool) {
  348. // Calculates number of rows to scroll up
  349. if t.firstRow == 0 {
  350. return
  351. }
  352. if n > t.firstRow {
  353. n = t.firstRow
  354. }
  355. t.firstRow -= n
  356. // Update scroll bar if visible
  357. if t.vscroll != nil && t.vscroll.Visible() {
  358. t.vscroll.SetValue(float32(t.firstRow) / float32(t.calcMaxFirst()))
  359. }
  360. if selLast {
  361. t.selectRow(t.lastRow - n)
  362. }
  363. t.recalc()
  364. }
  365. // removeRow removes from the table the row specified its index
  366. func (t *Table) removeRow(row int) {
  367. // Get row to be removed
  368. trow := t.rows[row]
  369. // Remove row from table
  370. copy(t.rows[row:], t.rows[row+1:])
  371. t.rows[len(t.rows)-1] = nil
  372. t.rows = t.rows[:len(t.rows)-1]
  373. // Dispose the row cell panels and its children
  374. for i := 0; i < len(trow.cells); i++ {
  375. cell := trow.cells[i]
  376. cell.DisposeChildren(true)
  377. cell.Dispose()
  378. }
  379. // Adjusts table first visible row if necessary
  380. //if t.firstRow == row {
  381. // t.firstRow--
  382. // if t.firstRow < 0 {
  383. // t.firstRow = 0
  384. // }
  385. //}
  386. }
  387. // onMouseEvent process subscribed mouse events
  388. func (t *Table) onMouse(evname string, ev interface{}) {
  389. e := ev.(*window.MouseEvent)
  390. t.root.SetKeyFocus(t)
  391. switch evname {
  392. case OnMouseDown:
  393. // Creates and dispatch TableClickEvent
  394. var tce TableClickEvent
  395. tce.MouseEvent = *e
  396. t.findClick(&tce)
  397. t.Dispatch(OnTableClick, tce)
  398. // Select left clicked row
  399. if tce.Button == window.MouseButtonLeft && tce.Row >= 0 {
  400. t.selectRow(tce.Row)
  401. t.recalc()
  402. }
  403. case OnMouseUp:
  404. default:
  405. return
  406. }
  407. t.root.StopPropagation(StopAll)
  408. }
  409. // onKeyEvent receives subscribed key events for the list
  410. func (t *Table) onKeyEvent(evname string, ev interface{}) {
  411. kev := ev.(*window.KeyEvent)
  412. switch kev.Keycode {
  413. case window.KeyUp:
  414. t.selPrev()
  415. case window.KeyDown:
  416. t.selNext()
  417. case window.KeyPageUp:
  418. t.prevPage()
  419. case window.KeyPageDown:
  420. t.nextPage()
  421. }
  422. }
  423. // findClick finds where in the table the specified mouse click event
  424. // occurred updating the specified TableClickEvent with the click coordinates.
  425. func (t *Table) findClick(ev *TableClickEvent) {
  426. x, y := t.ContentCoords(ev.Xpos, ev.Ypos)
  427. ev.X = x
  428. ev.Y = y
  429. ev.Row = -1
  430. // Find column id
  431. colx := float32(0)
  432. for ci := 0; ci < len(t.cols); ci++ {
  433. c := t.cols[ci]
  434. if c.Hidden {
  435. continue
  436. }
  437. colx += c.header.Width()
  438. if x < colx {
  439. ev.Col = c.Id
  440. break
  441. }
  442. }
  443. // If column not found the user clicked at the right of rows
  444. if ev.Col == "" {
  445. return
  446. }
  447. // Checks if is in header
  448. if t.showHeader && y < t.headerHeight {
  449. ev.Header = true
  450. }
  451. // Find row clicked
  452. rowy := float32(0)
  453. if t.showHeader {
  454. rowy = t.headerHeight
  455. }
  456. theight := t.ContentHeight()
  457. for ri := t.firstRow; ri < len(t.rows); ri++ {
  458. trow := t.rows[ri]
  459. rowy += trow.height
  460. if rowy > theight {
  461. break
  462. }
  463. if y < rowy {
  464. ev.Row = ri
  465. break
  466. }
  467. }
  468. }
  469. // selNext selects the next row if possible
  470. func (t *Table) selNext() {
  471. // If selected row is last, nothing to do
  472. sel := t.SelectedRow()
  473. if sel == len(t.rows)-1 {
  474. return
  475. }
  476. // If no selected row, selects first visible row
  477. if sel < 0 {
  478. t.selectRow(t.firstRow)
  479. t.recalc()
  480. return
  481. }
  482. // Selects next row
  483. next := sel + 1
  484. t.selectRow(next)
  485. // Scroll down if necessary
  486. if next > t.lastRow {
  487. t.scrollDown(1, false)
  488. } else {
  489. t.recalc()
  490. }
  491. }
  492. // selPrev selects the previous row if possible
  493. func (t *Table) selPrev() {
  494. // If selected row is first, nothing to do
  495. sel := t.SelectedRow()
  496. if sel == 0 {
  497. return
  498. }
  499. // If no selected row, selects last visible row
  500. if sel < 0 {
  501. t.selectRow(t.lastRow)
  502. t.recalc()
  503. return
  504. }
  505. // Selects previous row and selects previous
  506. prev := sel - 1
  507. t.selectRow(prev)
  508. // Scroll up if necessary
  509. if prev < t.firstRow && t.firstRow > 0 {
  510. t.scrollUp(1, false)
  511. } else {
  512. t.recalc()
  513. }
  514. }
  515. // nextPage increments the first visible row to show next page of rows
  516. func (t *Table) nextPage() {
  517. if t.lastRow == len(t.rows)-1 {
  518. return
  519. }
  520. plen := t.lastRow - t.firstRow
  521. if plen <= 0 {
  522. return
  523. }
  524. t.scrollDown(plen, true)
  525. }
  526. // prevPage advances the first visible row
  527. func (t *Table) prevPage() {
  528. if t.firstRow == 0 {
  529. return
  530. }
  531. plen := t.lastRow - t.firstRow
  532. if plen <= 0 {
  533. return
  534. }
  535. t.scrollUp(plen, true)
  536. }
  537. // selectRow sets the specified row as selected and unselects all other rows
  538. func (t *Table) selectRow(ri int) {
  539. for i := 0; i < len(t.rows); i++ {
  540. trow := t.rows[i]
  541. if i == ri {
  542. trow.selected = true
  543. } else {
  544. trow.selected = false
  545. }
  546. }
  547. }
  548. // recalcHeader recalculates and sets the position and size of the header panels
  549. func (t *Table) recalcHeader() {
  550. posx := float32(0)
  551. for i := 0; i < len(t.cols); i++ {
  552. c := t.cols[i]
  553. if c.Hidden {
  554. c.header.SetVisible(false)
  555. continue
  556. }
  557. c.header.SetPosition(posx, 0)
  558. c.header.SetVisible(true)
  559. posx += c.header.Width()
  560. }
  561. }
  562. // recalc calculates the visibility, positions and sizes of all row cells.
  563. // should be called in the following situations:
  564. // - the table is resized
  565. // - row is added, inserted or removed
  566. // - column alignment and expansion changed
  567. // - column visibility is changed
  568. // - horizontal or vertical scroll position changed
  569. func (t *Table) recalc() {
  570. // Get initial Y coordinate and total height of the table for rows
  571. starty := t.headerHeight
  572. if !t.showHeader {
  573. starty = 0
  574. }
  575. theight := t.ContentHeight()
  576. // Determines if it is necessary to show the scrollbar or not.
  577. scroll := false
  578. py := starty
  579. for ri := 0; ri < len(t.rows); ri++ {
  580. trow := t.rows[ri]
  581. py += trow.height
  582. if py > theight {
  583. scroll = true
  584. break
  585. }
  586. }
  587. t.setVScrollBar(scroll)
  588. // Sets the position and sizes of all cells of the visible rows
  589. py = starty
  590. for ri := 0; ri < len(t.rows); ri++ {
  591. trow := t.rows[ri]
  592. // If row is before first row or its y coordinate is greater the table height,
  593. // sets it invisible
  594. if ri < t.firstRow || py > theight {
  595. trow.SetVisible(false)
  596. continue
  597. }
  598. // Set row y position and visible
  599. trow.SetPosition(0, py)
  600. trow.SetVisible(true)
  601. t.updateRowStyle(ri)
  602. // Set the last completely visible row index
  603. if py+trow.Height() <= theight {
  604. t.lastRow = ri
  605. }
  606. //log.Error("ri:%v py:%v theight:%v", ri, py, theight)
  607. py += trow.height
  608. }
  609. }
  610. // recalcRow recalculates the positions and sizes of all cells of the specified row
  611. // Should be called when the row is created and column visibility or order is changed.
  612. func (t *Table) recalcRow(trow *tableRow) {
  613. // Calculates and sets row height
  614. maxheight := float32(0)
  615. for ci := 0; ci < len(t.cols); ci++ {
  616. // If column is hidden, ignore
  617. c := t.cols[ci]
  618. if c.Hidden {
  619. continue
  620. }
  621. cell := trow.cells[c.order]
  622. cellHeight := cell.MinHeight() + cell.label.Height()
  623. if cellHeight > maxheight {
  624. maxheight = cellHeight
  625. }
  626. }
  627. trow.SetContentHeight(maxheight)
  628. // Sets row cells sizes and positions and sets row width
  629. px := float32(0)
  630. for ci := 0; ci < len(t.cols); ci++ {
  631. // If column is hidden, ignore
  632. c := t.cols[ci]
  633. cell := trow.cells[c.order]
  634. if c.Hidden {
  635. cell.SetVisible(false)
  636. continue
  637. }
  638. // Sets cell position and size
  639. cell.SetPosition(px, 0)
  640. cell.SetVisible(true)
  641. cell.SetSize(c.header.Width(), trow.ContentHeight())
  642. px += c.header.Width()
  643. }
  644. trow.SetContentWidth(px)
  645. }
  646. func (t *Table) sortCols() {
  647. }
  648. // setVScrollBar sets the visibility state of the vertical scrollbar
  649. func (t *Table) setVScrollBar(state bool) {
  650. // Visible
  651. if state {
  652. var scrollWidth float32 = 20
  653. // Creates scroll bar if necessary
  654. if t.vscroll == nil {
  655. t.vscroll = NewVScrollBar(0, 0)
  656. t.vscroll.SetBorders(0, 0, 0, 1)
  657. t.vscroll.Subscribe(OnChange, t.onVScrollBarEvent)
  658. t.Panel.Add(t.vscroll)
  659. }
  660. // Initial y coordinate and height
  661. py := float32(0)
  662. height := t.ContentHeight()
  663. if t.showHeader {
  664. py = t.headerHeight
  665. height -= py
  666. }
  667. t.vscroll.SetSize(scrollWidth, height)
  668. t.vscroll.SetPositionX(t.ContentWidth() - scrollWidth)
  669. t.vscroll.SetPositionY(py)
  670. t.vscroll.recalc()
  671. t.vscroll.SetVisible(true)
  672. // Not visible
  673. } else {
  674. if t.vscroll != nil {
  675. t.vscroll.SetVisible(false)
  676. }
  677. }
  678. }
  679. // onVScrollBarEvent is called when a vertical scroll bar event is received
  680. func (t *Table) onVScrollBarEvent(evname string, ev interface{}) {
  681. pos := t.vscroll.Value()
  682. maxFirst := t.calcMaxFirst()
  683. first := int(math.Floor((float64(maxFirst) * pos) + 0.5))
  684. if first == t.firstRow {
  685. return
  686. }
  687. t.firstRow = first
  688. t.recalc()
  689. }
  690. // calcMaxFirst calculates the maximum index of the first visible row
  691. // such as the remaing rows fits completely inside the table
  692. // It is used when scrolling the table vertically
  693. func (t *Table) calcMaxFirst() int {
  694. // Get table height for rows considering if header is shown or not
  695. total := t.ContentHeight()
  696. if t.showHeader {
  697. total -= t.headerHeight
  698. }
  699. ri := len(t.rows) - 1
  700. if ri < 0 {
  701. return 0
  702. }
  703. height := float32(0)
  704. for {
  705. trow := t.rows[ri]
  706. height += trow.height
  707. if height > total {
  708. break
  709. }
  710. ri--
  711. if ri < 0 {
  712. break
  713. }
  714. }
  715. return ri + 1
  716. }
  717. // updateRowStyle applies the correct style for the specified row
  718. func (t *Table) updateRowStyle(ri int) {
  719. row := t.rows[ri]
  720. if row.selected {
  721. t.applyRowStyle(row, &t.styles.Row.Selected)
  722. return
  723. }
  724. t.applyRowStyle(row, &t.styles.Row.Normal)
  725. }
  726. // applyRowStyle applies the specified style to all cells for the specified table row
  727. func (t *Table) applyRowStyle(row *tableRow, trs *TableRowStyle) {
  728. for i := 0; i < len(row.cells); i++ {
  729. cell := row.cells[i]
  730. cell.SetBordersFrom(&trs.Border)
  731. cell.SetBordersColor4(&trs.BorderColor)
  732. cell.SetPaddingsFrom(&trs.Paddings)
  733. cell.SetColor(&trs.BgColor)
  734. }
  735. }
  736. // applyStyle applies the specified menu body style
  737. func (t *Table) applyHeaderStyle(hp *Panel) {
  738. s := t.styles.Header
  739. hp.SetBordersFrom(&s.Border)
  740. hp.SetBordersColor4(&s.BorderColor)
  741. hp.SetPaddingsFrom(&s.Paddings)
  742. hp.SetColor(&s.BgColor)
  743. }