widget_attr.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import (
  4. "fmt"
  5. )
  6. // widget attribute
  7. type WidgetAttr struct {
  8. key string
  9. value interface{}
  10. }
  11. // setup widget init enable/disable theme
  12. func WidgetAttrInitUseTheme(use bool) *WidgetAttr {
  13. return &WidgetAttr{"init_use_theme", use}
  14. }
  15. // setup widget font
  16. func WidgetAttrFont(font Font) *WidgetAttr {
  17. if font == nil {
  18. return nil
  19. }
  20. return &WidgetAttr{"font", font.Id()}
  21. }
  22. // setup widget width
  23. func WidgetAttrWidth(width int) *WidgetAttr {
  24. return &WidgetAttr{"width", width}
  25. }
  26. // setup widget height pixel or line
  27. func WidgetAttrHeight(height int) *WidgetAttr {
  28. return &WidgetAttr{"height", height}
  29. }
  30. // setup widget text
  31. func WidgetAttrText(text string) *WidgetAttr {
  32. return &WidgetAttr{"text", text}
  33. }
  34. // setup widget image
  35. func WidgetAttrImage(image *Image) *WidgetAttr {
  36. if image == nil {
  37. return nil
  38. }
  39. return &WidgetAttr{"image", image.Id()}
  40. }
  41. // setup widget border style (tk relief attribute)
  42. func WidgetAttrReliefStyle(style ReliefStyle) *WidgetAttr {
  43. return &WidgetAttr{"relief", style}
  44. }
  45. // setup widget border width
  46. func WidgetAttrBorderWidth(width int) *WidgetAttr {
  47. return &WidgetAttr{"borderwidth", width}
  48. }
  49. // setup widget padding (ttk padding or tk padx/pady)
  50. func WidgetAttrPadding(pad Pad) *WidgetAttr {
  51. return &WidgetAttr{"padding", pad}
  52. }
  53. // setup widget padding (ttk padding or tk padx/pady)
  54. func WidgetAttrPaddingN(padx int, pady int) *WidgetAttr {
  55. return &WidgetAttr{"padding", Pad{padx, pady}}
  56. }
  57. func checkPaddingScript(ttk bool, attr *WidgetAttr) string {
  58. if pad, ok := attr.value.(Pad); ok {
  59. if ttk {
  60. return fmt.Sprintf("-padding {%v %v}", pad.X, pad.Y)
  61. } else {
  62. return fmt.Sprintf("-padx {%v} -pady {%v}", pad.X, pad.Y)
  63. }
  64. }
  65. return ""
  66. }
  67. func checkInitUseTheme(attributes []*WidgetAttr) bool {
  68. for _, attr := range attributes {
  69. if attr != nil && attr.key == "init_use_theme" {
  70. if use, ok := attr.value.(bool); ok {
  71. return use
  72. }
  73. }
  74. }
  75. return mainTheme != nil
  76. }