| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // Copyright 2018 visualfc. All rights reserved.
- package tk
- import (
- "fmt"
- "strconv"
- "strings"
- )
- func GridAttrColumn(n int) *LayoutAttr {
- return &LayoutAttr{"column", n}
- }
- func GridAttrColumnSpan(n int) *LayoutAttr {
- return &LayoutAttr{"columnspan", n}
- }
- func GridAttrRow(n int) *LayoutAttr {
- return &LayoutAttr{"row", n}
- }
- func GridAttrRowSpan(n int) *LayoutAttr {
- return &LayoutAttr{"rowspan", n}
- }
- func GridAttrInMaster(w Widget) *LayoutAttr {
- if !IsValidWidget(w) {
- return nil
- }
- return &LayoutAttr{"in", w.Id()}
- }
- func GridAttrIpadx(padx int) *LayoutAttr {
- return &LayoutAttr{"ipadx", padx}
- }
- func GridAttrIpady(pady int) *LayoutAttr {
- return &LayoutAttr{"ipady", pady}
- }
- func GridAttrPadx(padx int) *LayoutAttr {
- return &LayoutAttr{"padx", padx}
- }
- func GridAttrPady(pady int) *LayoutAttr {
- return &LayoutAttr{"pady", pady}
- }
- func GridAttrSticky(v Sticky) *LayoutAttr {
- return &LayoutAttr{"sticky", v}
- }
- type GridIndexAttr struct {
- key string
- value interface{}
- }
- func GridIndexAttrMinSize(amount int) *GridIndexAttr {
- return &GridIndexAttr{"minsize", amount}
- }
- func GridIndexAttrPad(amount int) *GridIndexAttr {
- return &GridIndexAttr{"pad", amount}
- }
- func GridIndexAttrWeight(value int) *GridIndexAttr {
- return &GridIndexAttr{"weight", value}
- }
- func GridIndexAttrUniform(groupname string) *GridIndexAttr {
- return &GridIndexAttr{"uniform", groupname}
- }
- func Grid(widget Widget, attributes ...*LayoutAttr) error {
- return GridList([]Widget{widget}, attributes...)
- }
- func GridRemove(widget Widget) error {
- if !IsValidWidget(widget) {
- return ErrInvalid
- }
- return eval("grid forget " + widget.Id())
- }
- var (
- gridAttrKeys = []string{
- "column", "columnspan",
- "row", "rowspan",
- "in",
- "ipadx", "ipady",
- "padx", "pady",
- "sticky",
- }
- gridIndexAttrKeys = []string{
- "minsize",
- "pad",
- "weight",
- "uniform",
- }
- )
- func GridList(widgets []Widget, attributes ...*LayoutAttr) error {
- var idList []string
- for _, w := range widgets {
- if IsValidWidget(w) {
- w = checkLayoutWidget(w)
- idList = append(idList, w.Id())
- } else {
- idList = append(idList, "x")
- }
- }
- if len(idList) == 0 {
- return ErrInvalid
- }
- var attrList []string
- for _, attr := range attributes {
- if attr == nil || !isValidKey(attr.key, gridAttrKeys) {
- continue
- }
- attrList = append(attrList, fmt.Sprintf("-%v {%v}", attr.key, attr.value))
- }
- script := fmt.Sprintf("grid %v", strings.Join(idList, " "))
- if len(attrList) > 0 {
- script += " " + strings.Join(attrList, " ")
- }
- return eval(script)
- }
- // row index from 0; -1=all
- func GridRowIndex(master Widget, index int, attributes ...*GridIndexAttr) error {
- return gridIndex(master, true, index, attributes)
- }
- // column index from 0; -1=all
- func GridColumnIndex(master Widget, index int, attributes ...*GridIndexAttr) error {
- return gridIndex(master, false, index, attributes)
- }
- func gridIndex(master Widget, row bool, index int, attributes []*GridIndexAttr) error {
- if master == nil {
- master = rootWindow
- }
- var sindex string
- if index < 0 {
- sindex = "all"
- } else {
- sindex = strconv.Itoa(index)
- }
- var attrList []string
- for _, attr := range attributes {
- if attr == nil || !isValidKey(attr.key, gridIndexAttrKeys) {
- continue
- }
- attrList = append(attrList, fmt.Sprintf("-%v {%v}", attr.key, attr.value))
- }
- var script string
- if row {
- script = fmt.Sprintf("grid rowconfigure %v %v", master.Id(), sindex)
- } else {
- script = fmt.Sprintf("grid columnconfigure %v %v", master.Id(), sindex)
- }
- if len(attrList) > 0 {
- script += " " + strings.Join(attrList, " ")
- }
- return eval(script)
- }
|