table.go 23 KB

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