table.go 20 KB

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