@@ -52,38 +52,38 @@ Button.properties:
5252 imageMarginLeft
5353 image
5454
55- proc newButton * (r: Rect ): Button =
55+ proc newButton * (w: Window , r: Rect ): Button =
5656 result .new ()
57- result .init (r)
57+ result .init (w, r)
5858
59- proc newButton * (parent: View = nil , position: Point = newPoint (0 , 0 ), size: Size = newSize (100 , 20 ), title: string = " Button" ): Button =
60- result = newButton (newRect (position.x, position.y, size.width, size.height))
59+ proc newButton * (parent: View = nil , w: Window , position: Point = newPoint (0 , 0 ), size: Size = newSize (100 , 20 ), title: string = " Button" ): Button =
60+ result = newButton (w, newRect (position.x, position.y, size.width, size.height))
6161 result .title = title
6262 if not isNil (parent):
6363 parent.addSubview (result )
6464
65- proc newCheckbox * (r: Rect ): Button =
66- result = newButton (r)
65+ proc newCheckbox * (w: Window , r: Rect ): Button =
66+ result = newButton (w, r)
6767 result .style = bsCheckbox
6868 result .behavior = bbToggle
6969
70- proc newRadiobox * (r: Rect ): Button =
71- result = newButton (r)
70+ proc newRadiobox * (w: Window , r: Rect ): Button =
71+ result = newButton (w, r)
7272 result .style = bsRadiobox
7373 result .behavior = bbToggle
7474
75- proc newImageButton * (r: Rect ): Button =
76- result = newButton (r)
75+ proc newImageButton * (w: Window , r: Rect ): Button =
76+ result = newButton (w, r)
7777 result .style = bsImage
7878
79- proc newImageButton * (parent: View = nil , position: Point = newPoint (0 , 0 ), size: Size = newSize (100 , 20 ), image: Image = nil ): Button =
80- result = newImageButton (newRect (position.x, position.y, size.width, size.height))
79+ proc newImageButton * (parent: View = nil , w: Window , position: Point = newPoint (0 , 0 ), size: Size = newSize (100 , 20 ), image: Image = nil ): Button =
80+ result = newImageButton (w, newRect (position.x, position.y, size.width, size.height))
8181 result .image = image
8282 if not isNil (parent):
8383 parent.addSubview (result )
8484
85- method init * (b: Button , frame: Rect ) =
86- procCall b.Control .init (frame)
85+ method init * (b: Button , w: Window , frame: Rect ) =
86+ procCall b.Control .init (w, frame)
8787 b.state = bsUp
8888 b.enabled = true
8989 b.backgroundColor = whiteColor ()
@@ -93,29 +93,31 @@ method init*(b: Button, frame: Rect) =
9393 b.imageMarginTop = 2
9494 b.imageMarginBottom = 2
9595
96- method init * (b: Checkbox , frame: Rect ) =
97- procCall b.Button .init (frame)
96+ method init * (b: Checkbox , w: Window , frame: Rect ) =
97+ procCall b.Button .init (w, frame)
9898 b.style = bsCheckbox
9999 b.behavior = bbToggle
100100
101- method init * (b: Radiobox , frame: Rect ) =
102- procCall b.Button .init (frame)
101+ method init * (b: Radiobox , w: Window , frame: Rect ) =
102+ procCall b.Button .init (w, frame)
103103 b.style = bsRadiobox
104104 b.behavior = bbToggle
105105
106106proc drawTitle (b: Button , xOffset: Coord ) =
107+ template gfxCtx : untyped = b.window.gfxCtx
108+ template fontCtx : untyped = b.window.gfxCtx.fontCtx
109+ template gl : untyped = b.window.gfxCtx.gl
107110 if b.title.len != 0 :
108- let c = currentContext ()
109- c.fillColor = if b.state == bsDown and b.style == bsRegular:
111+ gfxCtx.fillColor = if b.state == bsDown and b.style == bsRegular:
110112 whiteColor ()
111113 else :
112114 blackColor ()
113115
114- let font = systemFont ()
116+ let font = systemFont (fontCtx )
115117 var titleRect = b.bounds
116- var pt = centerInRect (font. sizeOfString (b.title), titleRect)
118+ var pt = centerInRect (sizeOfString (fontCtx, gl, font, b.title), titleRect)
117119 if pt.x < xOffset: pt.x = xOffset
118- c .drawText (font, pt, b.title)
120+ gfxCtx .drawText (font, pt, b.title)
119121
120122var regularButtonComposition = newComposition """
121123uniform vec4 uStrokeColor;
@@ -134,7 +136,7 @@ void compose() {
134136"""
135137
136138proc drawRegularBezel (b: Button ) =
137- regularButtonComposition. draw b.bounds:
139+ draw b.window.gfxCtx, regularButtonComposition, b.bounds:
138140 if b.state == bsUp:
139141 setUniform (" uStrokeColor" , newGrayColor (0.78 ))
140142 setUniform (" uFillColorStart" , if b.enabled: b.backgroundColor else : grayColor ())
@@ -162,10 +164,10 @@ proc drawCheckboxStyle(b: Button, r: Rect) =
162164 let
163165 size = b.bounds.height
164166 bezelRect = newRect (0 , 0 , size, size)
165- c = currentContext ()
167+ c = b.window.gfxCtx
166168
167169 if b.value != 0 :
168- checkButtonComposition. draw bezelRect:
170+ draw c, checkButtonComposition, bezelRect:
169171 setUniform (" uStrokeColor" , selectionColor)
170172 setUniform (" uFillColor" , selectionColor)
171173 setUniform (" uRadius" , 4.0 )
@@ -182,7 +184,7 @@ proc drawCheckboxStyle(b: Button, r: Rect) =
182184 c.drawLine (newPoint (size / 4.0 , size * 1.0 / 2.0 ), newPoint (size / 4.0 * 2.0 , size * 1.0 / 2.0 + size / 5.0 - c.strokeWidth / 2.0 ))
183185 c.drawLine (newPoint (size / 4.0 * 2.0 - c.strokeWidth / 2.0 , size * 1.0 / 2.0 + size / 5.0 ), newPoint (size / 4.0 * 3.0 - c.strokeWidth / 2.0 , size / 4.0 ))
184186 else :
185- checkButtonComposition. draw bezelRect:
187+ draw c, checkButtonComposition, bezelRect:
186188 setUniform (" uStrokeColor" , newGrayColor (0.78 ))
187189 setUniform (" uFillColor" , whiteColor ())
188190 setUniform (" uRadius" , 4.0 )
@@ -206,16 +208,17 @@ void compose() {
206208
207209proc drawRadioboxStyle (b: Button , r: Rect ) =
208210 let bezelRect = newRect (0 , 0 , b.bounds.height, b.bounds.height)
211+ let c = b.window.gfxCtx
209212
210213 # Selected
211214 if b.value != 0 :
212- radioButtonComposition. draw bezelRect:
215+ draw c, radioButtonComposition, bezelRect:
213216 setUniform (" uStrokeColor" , selectionColor)
214217 setUniform (" uFillColor" , selectionColor)
215218 setUniform (" uRadioValue" , bezelRect.height * 0.3 )
216219 setUniform (" uStrokeWidth" , 0.0 )
217220 else :
218- radioButtonComposition. draw bezelRect:
221+ draw c, radioButtonComposition, bezelRect:
219222 setUniform (" uStrokeColor" , newGrayColor (0.78 ))
220223 setUniform (" uFillColor" , whiteColor ())
221224 setUniform (" uRadioValue" , 1.0 )
@@ -224,7 +227,7 @@ proc drawRadioboxStyle(b: Button, r: Rect) =
224227 b.drawTitle (bezelRect.width + 1 )
225228
226229proc drawImage (b: Button ) =
227- let c = currentContext ()
230+ let c = b.window.gfxCtx
228231 let r = b.bounds
229232 if b.imageMarginLeft != 0 or b.imageMarginRight != 0 or
230233 b.imageMarginTop != 0 or b.imageMarginBottom != 0 :
@@ -315,12 +318,8 @@ method onTouchEv*(b: Button, e: var Event): bool =
315318 result = b.handleToggleTouchEv (e)
316319
317320registerClass (Button )
318-
319- const checkBox = proc (): RootRef = newCheckbox (zeroRect)
320- registerClass (Checkbox , checkBox)
321-
322- const radiButton = proc (): RootRef = newRadiobox (zeroRect)
323- registerClass (Radiobox , radiButton)
321+ registerClass (Checkbox )
322+ registerClass (Radiobox )
324323
325324genVisitorCodeForView (Button )
326325genVisitorCodeForView (Checkbox )
0 commit comments