MCPcopy
hub / github.com/g3n/engine / NewTable

Function NewTable

gui/table.go:193–294  ·  view source on GitHub ↗

NewTable creates and returns a pointer to a new Table with the specified width, height and columns

(width, height float32, cols []TableColumn)

Source from the content-addressed store, hash-verified

191// NewTable creates and returns a pointer to a new Table with the
192// specified width, height and columns
193func NewTable(width, height float32, cols []TableColumn) (*Table, error) {
194
195 t := new(Table)
196 t.Panel.Initialize(t, width, height)
197 t.styles = &StyleDefault().Table
198 t.rowCursor = -1
199
200 // Initialize table header
201 t.header.Initialize(&t.header, 0, 0)
202 t.header.cmap = make(map[string]*tableColHeader)
203 t.header.cols = make([]*tableColHeader, 0)
204
205 // Create column header panels
206 for ci := 0; ci < len(cols); ci++ {
207 cdesc := cols[ci]
208 // Column id must not be empty
209 if cdesc.Id == "" {
210 return nil, fmt.Errorf("Column with empty id")
211 }
212 // Column id must be unique
213 if t.header.cmap[cdesc.Id] != nil {
214 return nil, fmt.Errorf("Column with duplicate id")
215 }
216 // Creates a column header
217 c := new(tableColHeader)
218 c.Initialize(c, 0, 0)
219 t.applyHeaderStyle(&c.Panel, false)
220 c.label = NewLabel(cdesc.Header)
221 c.Add(c.label)
222 c.id = cdesc.Id
223 c.minWidth = cdesc.Minwidth
224 if c.minWidth < tableColMinWidth {
225 c.minWidth = tableColMinWidth
226 }
227 c.width = cdesc.Width
228 if c.width < c.minWidth {
229 c.width = c.minWidth
230 }
231 c.align = cdesc.Align
232 c.format = cdesc.Format
233 c.formatFunc = cdesc.FormatFunc
234 c.expand = cdesc.Expand
235 c.sort = cdesc.Sort
236 c.resize = cdesc.Resize
237 // Adds optional sort icon
238 if c.sort != TableSortNone {
239 c.ricon = NewIcon(string(tableSortedNoneIcon))
240 c.Add(c.ricon)
241 c.ricon.Subscribe(OnMouseDown, func(evname string, ev interface{}) {
242 t.onRicon(evname, c)
243 })
244 }
245 // Sets default format and order
246 if c.format == "" {
247 c.format = "%v"
248 }
249 c.order = ci
250 c.SetVisible(!cdesc.Hidden)

Callers 2

NewStatsTableFunction · 0.92
buildTableFunction · 0.85

Calls 15

StyleDefaultFunction · 0.85
NewLabelFunction · 0.85
NewIconFunction · 0.85
applyHeaderStyleMethod · 0.80
onRiconMethod · 0.80
MinWidthMethod · 0.80
SetContentSizeMethod · 0.80
applyResizerStyleMethod · 0.80
applyStatusStyleMethod · 0.80
SubscribeMethod · 0.65
SetVisibleMethod · 0.65
WidthMethod · 0.65

Tested by

no test coverage detected