Sample Option Sheets script

From MediaMonkey Wiki
Jump to navigation Jump to search
' Sample Option Sheets script
'
' This script adds three basic sheets to Options dialog. It shows how to add sheets
' there, how to put controls to the sheets and how to manage data entered in the controls.

Sub OnStartup
  ' Create our own option sheet
  ind = SDB.UI.AddOptionSheet( "Test", Script.ScriptPath, "InitSheet", "SaveSheet", 0)
  ' Create another sheet as a child of the previous one
  SDB.UI.AddOptionSheet "Test2", Script.ScriptPath, "InitSheet2", "", ind
  ' Create one more sheet that is a child of Player sheet
  SDB.UI.AddOptionSheet "Player test", Script.ScriptPath, "InitSheet2", "", -2
End Sub

Sub InitSheet( Sheet)
  ' Create a simple sheet with an edit line and a button
  Set UI=SDB.UI

  ' Create an edit line
  Set Edt = UI.NewEdit( Sheet)
  Edt.Common.SetRect 20, 20, 150, 20
  Edt.Text = "Some text here"
  Edt.Common.ControlName = "Edit1"

  ' Create a button
  Set Btn = UI.NewButton( Sheet)
  Btn.Caption = "My Button"
  Btn.Common.SetRect 80, 80, 100, 25
  Btn.Common.Hint = "Hello!"         ' Just for fun - we can even show tooltips
  Btn.UseScript = Script.ScriptPath
  Btn.OnClickFunc = "BtnClicked"

  ' Retrieve already entered value from registry
  Set Regs = SDB.Registry
  If Regs.OpenKey( "Sample Option Sheet", True) Then
    If Regs.ValueExists( "Edit value") Then
      Edt.Text = Regs.StringValue( "Edit value")
    End If
    Regs.CloseKey
  End If
End Sub

Sub SaveSheet( Sheet)
   ' Save entered value to registry in order to be able to shown it next time
  Set Regs = SDB.Registry
  Set Edt = Sheet.Common.ChildControl( "Edit1")
  If Regs.OpenKey( "Sample Option Sheet", True) Then
    Regs.StringValue("Edit value") = Edt.Text
    Regs.CloseKey
  End If
End Sub

Sub BtnClicked( ClickedBtn)
  Set UI = SDB.UI

  ' Create the window to be shown
  Set Form = UI.NewForm
  Form.Common.SetRect 100, 100, 300, 200
  Form.FormPosition = 4   ' Screen Center
  Form.SavePositionName = "Test form"
  Form.Caption = "Clicked!"
  Form.BorderStyle = 3   ' Dialog

  ' Create a label on the form
  Set Lbl = UI.NewLabel( Form)
  Lbl.Common.SetRect 20, 20, 200, 20
  Lbl.Caption = "The following text was entered in the Option sheet:"

  ' Create an edit line on the form
  Set Edt = UI.NewEdit( Form)
  Edt.Common.SetRect 20, 60, 220, 20
     ' The following line finds the edit line in the option sheet.
     ' First of all it gets the sheet itself (TopParent) and then
     ' it finds the edit line by its name - ChildControl( "Edit1").
  Set SheetEdt = ClickedBtn.Common.TopParent.Common.ChildControl( "Edit1")
  Edt.Text = SheetEdt.Text

  ' And create a button that can be used to close the form
  Set Btn = UI.NewButton( Form)
  Btn.Common.SetRect 80, 110, 100, 30
  Btn.Caption = "Cool!"
  Btn.ModalResult = 1

  ' Show the window and wait until user closes it
  Form.ShowModal
End Sub

Sub InitSheet2( Sheet)
  Set UI=SDB.UI

  Set Btn = UI.NewButton( Sheet)
  Btn.Caption = "My Button 2"
  Btn.Common.Left = 50
  Btn.Common.Top = 50
  Btn.Common.Width = 150
  Btn.Common.Height = 30
  Btn.Common.Hint = "Hello2!"
End Sub