-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparagraph_linux.go
133 lines (109 loc) · 2.75 KB
/
paragraph_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package goey
import (
"unsafe"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/internal/syscall"
"github.com/gotk3/gotk3/gtk"
)
type paragraphElement struct {
Control
}
func (a TextAlignment) native() gtk.Justification {
switch a {
case JustifyLeft:
return gtk.JUSTIFY_LEFT
case JustifyCenter:
return gtk.JUSTIFY_CENTER
case JustifyRight:
return gtk.JUSTIFY_RIGHT
case JustifyFull:
return gtk.JUSTIFY_FILL
}
panic("not reachable")
}
func (a TextAlignment) halign() gtk.Align {
switch a {
case JustifyLeft:
return gtk.ALIGN_START
case JustifyCenter:
return gtk.ALIGN_CENTER
case JustifyRight:
return gtk.ALIGN_END
case JustifyFull:
return gtk.ALIGN_START
}
panic("not reachable")
}
func (w *P) mount(parent base.Control) (base.Element, error) {
handle, err := gtk.LabelNew(w.Text)
if err != nil {
return nil, err
}
handle.SetSingleLineMode(false)
parent.Handle.Add(handle)
handle.SetJustify(w.Align.native())
handle.SetHAlign(w.Align.halign())
handle.SetLineWrap(true)
retval := ¶graphElement{Control{&handle.Widget}}
handle.Connect("destroy", paragraphOnDestroy, retval)
handle.Show()
return retval, nil
}
func paragraphOnDestroy(widget *gtk.Label, mounted *paragraphElement) {
mounted.handle = nil
}
func (w *paragraphElement) label() *gtk.Label {
return (*gtk.Label)(unsafe.Pointer(w.handle))
}
func (w *paragraphElement) Props() base.Widget {
label := w.label()
text, err := label.GetText()
if err != nil {
panic("Could not get text, " + err.Error())
}
align := JustifyLeft
switch label.GetJustify() {
case gtk.JUSTIFY_CENTER:
align = JustifyCenter
case gtk.JUSTIFY_RIGHT:
align = JustifyRight
case gtk.JUSTIFY_FILL:
align = JustifyFull
}
return &P{
Text: text,
Align: align,
}
}
func (w *paragraphElement) measureReflowLimits() {
label := w.label()
text, err := label.GetText()
if err != nil {
panic(err)
}
label.SetText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm")
width, _ := label.GetPreferredWidth()
label.SetText(text)
paragraphMaxWidth = base.FromPixelsX(width)
}
func (w *paragraphElement) MinIntrinsicHeight(width base.Length) base.Length {
if width == base.Inf {
width = w.maxReflowWidth()
}
height, _ := syscall.WidgetGetPreferredHeightForWidth(w.handle, width.PixelsX())
return base.FromPixelsY(height)
}
func (w *paragraphElement) MinIntrinsicWidth(height base.Length) base.Length {
if height != base.Inf {
panic("not implemented")
}
width, _ := w.label().GetPreferredWidth()
return min(base.FromPixelsX(int(width)), w.minReflowWidth())
}
func (w *paragraphElement) updateProps(data *P) error {
label := w.label()
label.SetText(data.Text)
label.SetJustify(data.Align.native())
label.SetHAlign(data.Align.halign())
return nil
}