table.go 22 KB

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