VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Box"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
'/******************************************************************/
'/*                                                                */
'/*                      TurboCAD for Windows                      */
'/*                   Copyright (c) 1993 - 2001                    */
'/*             International Microcomputer Software, Inc.         */
'/*                            (IMSI)                              */
'/*                      All rights reserved.                      */
'/*                                                                */
'/******************************************************************/

'DBAPI constants
Const gkGraphic = 11
Const gkArc = 2
Const gkText = 6
Const gfCosmetic = 128&

'Useful math constants
Const Pi# = 3.14159265

'Real variant types!
Const typeEmpty = 0
Const typeInteger = 2
Const typeLong = 3
Const typeSingle = 4
Const typeDouble = 5
Const typeCurrency = 6
Const typeDate = 7
Const typeString = 8
Const typeObject = 9
Const typeBoolean = 11
Const typeVariant = 12
Const typeIntegerEnum = typeInteger + 100
Const typeLongEnum = typeLong + 100
Const typeStringEnum = typeString + 100

'Stock property pages
Const ppStockPen = 1
Const ppStockBrush = 2
Const ppStockText = 4
Const ppStockInsert = 8
Const ppStockViewport = 16
Const ppStockAuto = 32

'Property Ids
Const idRoundness = 1
Const idText = 2

'Property enums

'Number of properties, pages, wizards
Const NUM_PROPERTIES = 2
Const NUM_PAGES = 1
Const NUM_WIZARDS = 0

Private Sub Class_Initialize()
    'Initialize class variables
End Sub

'Returns the user-visible description of this RegenMethod
Public Property Get Description() As String
    Description = "TextBox"
End Property

'Returns the persistent class id for this RegenMethod's property section
Public Property Get ClassID() As String
    ClassID = "{D25185FF-6A20-11d0-A115-00A024158DAF}"
End Property

'Retrieve types and names
Public Function GetPropertyInfo(Names As Variant, Types As Variant, _
    IDs As Variant, Defaults As Variant) As Long
    ReDim Names(NUM_PROPERTIES), Types(NUM_PROPERTIES), _
        IDs(NUM_PROPERTIES), Defaults(NUM_PROPERTIES)
    Names(0) = "Roundness"
    Types(0) = typeDouble
    IDs(0) = idRoundness
    Defaults(0) = 20
    
    Names(1) = "strText"
    Types(1) = typeString
    IDs(1) = idText
    Defaults(1) = "Unknown"
   
    GetPropertyInfo = NUM_PROPERTIES
End Function

'Get the number of property pages supporting this RegenMethod
Public Function GetPageInfo(ByVal AGraphic As Object, StockPages As Long, _
    Names As Variant) As Long
    ReDim Names(NUM_PAGES)

    'Need the form
    Load frmTextBox
    Names(0) = frmTextBox.Caption
    Unload frmTextBox

    StockPages = ppStockBrush + ppStockPen + ppStockAuto
    GetPageInfo = NUM_PAGES
End Function

Public Function GetWizardInfo(Names As Variant) As Long
    ReDim Names(NUM_WIZARDS)
    GetWizardInfo = NUM_WIZARDS
End Function

'Enumerate the names and values of a specified property
Public Function GetEnumNames(ByVal PropID As Long, Names As Variant, Values As Variant) As Long
    GetEnumNames = 0
End Function

Public Function PageControls(ByVal ThisRegenMethod As Object, ByVal Graphic As Object, ByVal PageNumber As Long, ByVal SaveProperties As Boolean) As Boolean
        'Set up error function
        On Error GoTo Failed

        Dim Roundness#
        Dim StrText$
        If SaveProperties Then
            'OK button on property page was clicked
            'Form is still loaded
            With frmTextBox
                'Need On Error statement for the case where you have
                'RRect Turbo Shape and ahother "shape" selected
                On Error Resume Next

                'When the property page is closed, transfer the numeric
                'roundness value from the TextBox to the Graphic
                'Get the value as a double-precision number
                Roundness# = CDbl(.txtRoundness.Text)
                'Make sure it's between 0 and 100
                If Roundness# < 0# Then Roundness# = 0#
                If Roundness# > 100# Then Roundness# = 100#
                'Set the roundness property value in the Graphic
                Graphic.Properties("Roundness") = Roundness#
                
                StrText = .StrText.Text
                Graphic.Properties("strText") = StrText
                
            End With
        Else
            'Property page is about to be opened
            'Make sure the form is loaded
            Load frmTextBox
            With frmTextBox
                'If more than one RRect is selected and they do not
                'have the same properties, don't set up this field
                On Error GoTo NoRType

                'When the property page is opening, transfer the numeric
                'roundness value from the Graphic to the TextBox
                'Get the roundness property value from the Graphic
                Roundness# = Graphic.Properties("Roundness")
                'Set the TextBox control's text
                .txtRoundness.Text = Roundness#
                StrText = Graphic.Properties("strText")
                .StrText.Text = StrText
                
NoRType:
            End With
        End If

        PageControls = True
        Exit Function

Failed:
        'For debugging purposes, report that an error occurred
        If Err.Number <> 0 Then
            MsgBox "Error in PageControls: " & Err.Description
        End If

        'Return false if an error occurred
        PageControls = False
End Function

Public Function PageDone(ByVal ThisRegenMethod As Object, Optional PageNumber As Variant)
        'Done with form
        Unload frmTextBox
End Function

Public Function PropertyPages(ByVal ThisRegenMethod As Object, Optional PageNumber As Variant) As Boolean
    With frmTextBox
        .Show vbModal
        PropertyPages = Not .DialogCanceled
    End With
End Function

Public Function Wizard(ByVal ThisRegenMethod As Object, Optional WizardNumber As Variant) As Boolean
    Wizard = False
End Function

'Called when vertex has been moved, or other geometry change
Public Function OnGeometryChanged(ByVal Graphic As Object, ByVal GeomID As Long, paramOld As Variant, paramNew As Variant)
    'Do nothing
    'Regen Graphic
End Function

'Called when vertex is moved, or other geometry change
Public Function OnGeometryChanging(ByVal Graphic As Object, ByVal GeomID As Long, paramOld As Variant, paramNew As Variant) As Boolean
    'OK to continue with change
    OnGeometryChanging = True
End Function

Public Function OnNewGraphic(ByVal grfThis As Object, ByVal boolCopy As Boolean) As Boolean
    If boolCopy Then
        'Vertices are already added for us...
        OnNewGraphic = True
        Exit Function
    End If

    On Error GoTo Failed
    'New Graphic being created
    'X, Y, Z, PenDown, Selectable, Snappable, Editable, Linkable
    'First Vertex is "lower left" corner i=0
    grfThis.Vertices.Add 0#, 0, 0#, False, True, False, True, False
    'Second Vertex is "upper right" corner i=1
    grfThis.Vertices.Add 2#, 1, 0#, False, True, False, True, False
'    Middle vertices
    grfThis.Vertices.Add 1#, 0#, 0#, False, False, True, True, True
    grfThis.Vertices.Add 2#, 0.5, 0#, False, False, True, True, True
    grfThis.Vertices.Add 1#, 1#, 0#, False, False, True, True, True
    grfThis.Vertices.Add 0#, 0.5, 0#, False, False, True, True, True
    grfThis.Properties("LimitVertices") = 6
    OnNewGraphic = True
    Exit Function

Failed:
    'Return false on failure
    OnNewGraphic = False
End Function

'Function called whenever a copy of a graphic is being made
Public Function OnCopyGraphic(ByVal grfCopy As Object, ByVal grfSource As Object) As Boolean
    'Return false on failure
    OnCopyGraphic = True
End Function

'Notification function called after graphic property is saved
Public Function OnPropertyChanged(ByVal Graphic As Object, ByVal PropID As Long, _
        ValueOld As Variant, ValueNew As Variant)
    'Do nothing
End Function

'Notification function called when graphic property is saved
Public Function OnPropertyChanging(ByVal Graphic As Object, ByVal PropID As Long, _
        ValueOld As Variant, ValueNew As Variant) As Boolean
    'OK to proceed
    OnPropertyChanging = True
End Function

'Notification function called when graphic property is retrieved
Public Function OnPropertyGet(ByVal Graphic As Object, ByVal PropID As Long)
    'Do nothing
End Function

'Called when we need to update our object
Public Function Regen(ByVal grfThis1 As Object)
        'Setup error handler
        On Error GoTo Failed
Dim StrText$
Dim grfThis As Graphic
        Set grfThis = grfThis1
        'Set up lock (prevent recursion)
        Dim LockCount&
        LockCount& = grfThis.RegenLock

        'Setup error handler (make sure lock is removed)
        On Error GoTo FailedLock
        If LockCount& = 0 Then
            'Delete any previous cosmetic children
            grfThis.Graphics.Clear gfCosmetic

            'Calculate height, width and radius of corners
            Dim W#, H#, R#, Roundness#
            Dim L#
            Dim Round#
                Round = grfThis.Properties("Roundness")
                Roundness = CDbl(Round)

                StrText = grfThis.Properties("strText")

            'Add child Graphics
            Dim grfChild As Graphic
            Dim x0#, y0#, x1#, y1#, T#
            With grfThis.Vertices
                x0 = .Item(0).X
                y0 = .Item(0).Y
                x1 = .Item(1).X
                y1 = .Item(1).Y
                W# = Abs(x1 - x0)
                H# = Abs(y1 - y0)
            End With
            
   
            If Roundness = 0# Then
                'No rounded corners
                'All children are cosmetic
                Set grfChild = grfThis.Graphics.Add(11)
                grfChild.Cosmetic = True
                'Now add vertices to the child
                With grfChild.Vertices
                    .Add x0#, y0#, 0
                    .Add x0#, y1#, 0, True
                    .Add x1#, y1#, 0, True
                    .Add x1#, y0#, 0, True
                    'Close the rectangle
                    .AddClose True 'PenDown
                End With
            Else
            
Dim Htext#

Dim x3#, x2#, y3#, y2#
                Set grfChild = grfThis.Graphics.AddText(StrText, (x0 + x1) / 2#, y1, 0, y1 - y0, 0, 0, , 2)
                grfChild.Cosmetic = True
                grfChild.Properties.Item("TextMode").Value = 2
                x3 = grfChild.Vertices.Item(3).X
                x2 = grfChild.Vertices.Item(2).X
Dim Matr As Matrix
                Set Matr = grfChild.Scale(0.95 * (x1 - x0) / (x3 - x2), 1, 1)
            With grfThis.Vertices
                 .Item(2).X = (x1 + x0) / 2#
                 .Item(2).Y = y0

                 .Item(3).X = x1
                 .Item(3).Y = (y1 + y0) / 2#

                 .Item(4).X = (x1 + x0) / 2#
                 .Item(4).Y = y1

                 .Item(5).X = x0
                 .Item(5).Y = (y1 + y0) / 2#
            End With

 H = Abs(y1 - y0)
 R = H / 2# * Roundness / 100#
 
                
                'Rounded corners
                'We'll make 4 line children and 4 arc children
                'First line
                'All children are cosmetic
                Set grfChild = grfThis.Graphics.Add(11)
                grfChild.Cosmetic = True
                'Now add vertices to the child
                With grfChild.Vertices
                    .Add x0# + R#, y0#, 0
                    .Add x1# - R#, y0#, 0, True
                End With
                'First arc
                Set grfChild = grfThis.Graphics.Add(gkArc)
                grfChild.Cosmetic = True
                grfChild.ArcSet x1# - R#, y0# + R#, 0#, R#, , 1.5 * Pi#, 0#
                'Second line
                Set grfChild = grfThis.Graphics.Add(11)
                grfChild.Cosmetic = True
                With grfChild.Vertices
                    .Add x1#, y0# + R#, 0
                    .Add x1#, y1# - R#, 0, True
                End With
                'Second arc
                Set grfChild = grfThis.Graphics.Add(gkArc)
                grfChild.Cosmetic = True
                grfChild.ArcSet x1# - R#, y1# - R#, 0#, R#, , 0#, 0.5 * Pi#
                'Third line
                Set grfChild = grfThis.Graphics.Add(11)
                grfChild.Cosmetic = True
                With grfChild.Vertices
                    .Add x1# - R#, y1#, 0
                    .Add x0# + R#, y1#, 0, True
                End With
                'Third arc
                Set grfChild = grfThis.Graphics.Add(gkArc)
                grfChild.Cosmetic = True
                grfChild.ArcSet x0# + R#, y1# - R#, 0#, R#, , 0.5 * Pi#, Pi#
                'Fourth line
                Set grfChild = grfThis.Graphics.Add(11)
                grfChild.Cosmetic = True
                With grfChild.Vertices
                    .Add x0#, y1# - R#, 0
                    .Add x0#, y0# + R#, 0, True
                End With
                'Fourth arc
                Set grfChild = grfThis.Graphics.Add(gkArc)
                grfChild.Cosmetic = True
                grfChild.ArcSet x0# + R#, y0# + R#, 0#, R#, , Pi#, 1.5 * Pi#
            End If
        grfThis.RegenUnlock

            'Add visible child Graphics
        End If

        grfThis.RegenUnlock

        Exit Function

FailedLock:
        'Remove lock
        grfThis.RegenUnlock

Failed:
End Function

Public Function Draw(ByVal grfThis As Object, ByVal view As Object, Optional mat As Variant) As Boolean
    'Return True if we did the redraw (no further processing necessary, no children will be drawn).
    'Since this is just a test, we return False to let TurboCAD do the drawing operation.
    Draw = False
End Function


