table.go 23 KB

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