# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Create a tool with a Spread showing how to modify column name and adding datas.
#
# Topic: FBSpread,FBDragAndDropState
#

from pyfbsdk import *
from pyfbsdk_additions import *

def OnSpreadEvent(control, event):
    print "Type:%s, Action %d, Row:%d, Column:%d Value: %s" % (event.Type, event.Action, event.Row, event.Column, str(control.GetCellValue(event.Row, event.Column)))

def OnDragAndDrop(control, event):
    """
    DragAndDropEvent documentation:
    Accept (): Accept a drag and drop sequence. 
    Add (FBComponent pComponent, int pId=0): Add an item to the drag and drop list. 
    Clear (): Clear drag and drop list. 
    Get (int pIndex): Get the FBComponent specified by pIndex from the Drag and Drop list.  
    GetCount (): Get the number of items in the DragAndDrop list. 
 
    Data: Property: User specified reference. (for example, FBSpread:row) 
    PosX: Property: X position of mouse. 
    PosY: Property: Y position of mouse. 
    State: Property: Drag and drop sub-event. 
    components: list of dragged components
    """
    if event.State == FBDragAndDropState.kFBDragAndDropDrag:
        event.Accept()
    print "Type:%s, State%d, PosX:%d, PosY:%d, NbElements:%d" % (event.Type, event.State, event.PosX, event.PosY, event.GetCount())


def PopulateLayout(mainLyt):
    x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
    y = FBAddRegionParam(0,FBAttachType.kFBAttachTop,"")
    w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
    h = FBAddRegionParam(0,FBAttachType.kFBAttachBottom,"")
    mainLyt.AddRegion("main","main", x, y, w, h)

    s = FBSpread()
    s.Caption = "Spread"
    mainLyt.SetControl("main",s)

    s.ColumnAdd("Col 1")
    s.ColumnAdd("Col 2")
    s.ColumnAdd("Col 3")

    s.RowAdd("Row 1", 0)
    s.RowAdd("Row 2", 1)
    s.RowAdd("Row 3", 2)

    s.OnCellChange.Add(OnSpreadEvent)
    s.OnRowClick.Add(OnSpreadEvent)
    s.OnColumnClick.Add(OnSpreadEvent)
    s.OnDragAndDrop.Add(OnDragAndDrop)

    c = s.GetColumn(0)
    c.Caption = "first column"

    # set some initial values:
    s.SetCellValue(0, 0, 3)
    s.SetCellValue(0, 1, 3.1416)
    s.SetCellValue(0, 2, "py = 3.1416")

def CreateTool():
    # Tool creation will serve as the hub for all other controls
    t = CreateUniqueTool("Spread Example")
    t.StartSizeX = 900
    t.StartSizeY = 400
    PopulateLayout(t)
    ShowTool(t)


CreateTool()