Does anyone have COCOA replacements for MacOSLib items such as

SetLineDash and SetLineCap

I would like to be able to draw dotted/dashed lines and have flat and round endcaps, but don’t wish to use Carbon Functions

Does anyone have COCOA replacements for MacOSLib items such as

SetLineDash and SetLineCap

I would like to be able to draw dotted/dashed lines and have flat and round endcaps, but don’t wish to use Carbon Functions

Dang… figured it out myself (of course if anyone can make this BETTER I’d appreciate it)

Based on code culled from MacOSLib and converted from CARBON to COCOA

```
Dim lengths(-1) As Double
lengths=Array(1.,3.,4.,2.)
Declare Sub CGContextSetLineDash Lib "Cocoa" ( context As Integer, phase As Single, lengths As Ptr, count As UInt32)
If UBound(lengths) > -1 Then
Dim lengthArray As MemoryBlock
lengthArray= CFloatArray(lengths)
If lengthArray Is Nil Then Return
CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ), 0,lengthArray,lengths.Ubound+1
Else
CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ),0, Nil, 0 ' solid line
End If
g.drawline 0,0,g.Width,g.Height
```

```
FUNCTION CFLoatArray(theList() as double) as MemoryBlock
If UBound(theList) = -1 Then
Return Nil
End If
Const sizeOfSingle = 4
Dim theArray As New MemoryBlock(sizeOfSingle*(1 + UBound(theList)))
Dim offset As Integer = 0
For i As Integer = 0 To UBound(theList)
theArray.SingleValue(offset) = theList(i)
offset = offset + sizeOfSingle
Next
Return theArray
END FUNCTION
```

And if someone could explain why this version bombs

```
Dim lengths(-1) As Double
Dim lengthArray As MemoryBlock
// DO NOT DEFINE A PATTERN lengths=Array(1.,3.,4.,2.)
Declare Sub CGContextSetLineDash Lib "Cocoa" ( context As Integer, phase As Single, lengths As Ptr, count As UInt32)
lengthArray= CFloatArray(lengths)
CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ), 0,lengthArray,lengths.Ubound+1
g.drawline 0,0,g.Width,g.Height
```

In this shorter version, I would have ASSUMED (and the debugger seems to bear me out)

it that LengthArray is NIL and Lengths.Ubound+1 is Zero

matching the parameters in the 2nd call from the first (working) code…

but 2nd version bombs with a NilException (and I expect LengthArray to be NIL)

These calls only appear to work if the line width is 1… does that sound correct?

Works for any PenWidth I have thrown at it… you just need to adjust the values in the pattern array to be larger

Here is a Function I just added to my PaintDS program to support this

```
SUB myLineStyle(g as graphics,pattern_id as integer=0)
Dim lengths(-1) As Double
Dim x As Integer
Dim lengthArray As MemoryBlock
Declare Sub CGContextSetLineDash Lib "Cocoa" ( context As Integer, phase As Single, lengths As Ptr, count As UInt32)
x=g.penwidth
Select Case pattern_id
case -1 ' for lasso
lengths=Array(x*4.,x*4.) '
Case 1
lengths=Array(Ceil(x/2.),x*2.) ' dotted line [.......]
Case 2
lengths=Array(x*8.,x*4.) ' dashed line [- - - -]
Case 3
lengths=Array(x*6.,x*3.,Ceil(x/2.),x*3.) ' dashed dot dash line [- . -]
case 4
lengths=Array(x*6.,x*3.,Ceil(x/2.),x*3.,Ceil(x/2.),x*3.) ' dashed dot dot line [-..-..]
End Select
//
If UBound(lengths) > -1 Then
lengthArray= CFloatArray(lengths)
If lengthArray Is Nil Then Return
CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ), 0,lengthArray,lengths.Ubound+1
Else
CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ),0, Nil, 0 ' solid line
End If
END SUB
```

Should have guessed that. Cheers.

Forum for Xojo Programming Language and IDE. Copyright © 2022 Xojo, Inc.