Sample New Window - Report script

From MediaMonkey Wiki
Jump to navigation Jump to search
' Sample New Window - Report script
'
' This script shows how to add new UI elements to MM. It adds several menu items and 
' a toolbar item that all show a new dialog with a very simple statistics.

Sub OnStartup
  Set UI = SDB.UI

  ' Add a couple of menu items here:

  ' Add a submenu to the View menu...
  Set Mnu = UI.AddMenuItemSub( UI.Menu_View, -1, 1)
  Mnu.Caption = "Custom items"

  ' ... and add Statistics item there
  Set Mnu = UI.AddMenuItem( Mnu, 0, 0)
  Mnu.Caption = "&Statistics"     
  Mnu.UseScript = Script.ScriptPath
  Mnu.OnClickFunc = "ShowIt"
  Mnu.Shortcut = "Ctrl+1"
  Mnu.IconIndex = 35

  ' Add Statistics item to tray icon menu...
  Set Mnu = UI.AddMenuItem( UI.Menu_TrayIcon, -1, 1)
  Mnu.Caption = "&Statistics"
  Mnu.UseScript = Script.ScriptPath
  Mnu.OnClickFunc = "ShowIt"
  Mnu.IconIndex = 35

  ' ... and separate it from other items
  UI.AddMenuItemSep UI.Menu_TrayIcon, -1, 2

  ' And lastly add the item to the main toolbar, again separated from other items
  UI.AddMenuItemSep UI.Menu_TbStandard, 0, 0

  Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
  Mnu.Caption = "Statistics"
  Mnu.UseScript = Script.ScriptPath
  Mnu.OnClickFunc = "ShowIt"
  Mnu.IconIndex = 35
  Mnu.Hint = "Sample script that shows some statistics"
End Sub

Sub ShowIt( Itm)
  Set UI = SDB.UI

  ' Create the window to be shown
  Set Form = UI.NewForm
  Form.Common.SetRect 100, 100, 500, 400
  Form.Common.MinWidth = 200
  Form.Common.MinHeight = 150
  Form.FormPosition = 4   ' Screen Center
  Form.SavePositionName = "Report form"
  Form.Caption = "Statistics"
  Form.StayOnTop = True

  ' Create a panel at the top of the window
  Set Head = UI.NewPanel( Form)
  Head.Common.Align = 1   ' Top
  Head.Common.Height = 30

  ' Create a DropDown for selection of data to be shown
  Set Combo = UI.NewDropDown( Head)
  Combo.Common.SetRect 5, 5, 150, 25
  Combo.Style = 2     ' List
  Combo.AddItem "Summary"
  Combo.AddItem "Artists"
  Combo.ItemIndex = 0
  Combo.UseScript = Script.ScriptPath
  Combo.OnSelectFunc = "OnSelect"

  ' Create a panel at the bottom of the window
  Set Foot = UI.NewPanel( Form)
  Foot.Common.Align = 2   ' Bottom
  Foot.Common.Height = 35

  ' Create a button that closes the window
  Set Btn = UI.NewButton( Foot)
  Btn.Caption = "Close"
  Btn.Common.SetRect Foot.Common.Width - 90, 9, 85, 24
  Btn.Common.Hint = "Close this report"
  Btn.Common.Anchors = 4+8   ' Right+Bottom
  Btn.UseScript = Script.ScriptPath
  Btn.OnClickFunc = "OnClose"
  
  ' Create a web browser component
  Set WB = UI.NewActiveX( Form, "Shell.Explorer")
  WB.Common.Align = 5      ' Fill all client rectangle
  WB.Common.ControlName = "WB"
  WB.Interf.Navigate "about:"          ' A trick to make sure document exists
  Set Doc = WB.Interf.Document

  Form.Common.Visible = True                ' Only show the form, don't wait for user input
  SDB.Objects("Sample Report Form") = Form  ' Save reference to the form somewhere, otherwise it would simply disappear
  OnSelect( Combo)
End Sub

Sub OnClose(Btn)
  SDB.Objects("Sample Report Form") = Nothing ' Remove the last reference to our form which also causes it to disappear
End Sub

Sub OnSelect(DD)
  Set Form = SDB.Objects("Sample Report Form")
  Set WB = Form.Common.ChildControl("WB")
  Set Doc = WB.Interf.Document
  If DD.ItemIndex=0 Then
    Set QueryRes = SDB.Database.OpenSQL( "SELECT COUNT(*) FROM Songs")
    Doc.Write "<h2>Library summary</h2>"
    Doc.Write "Track count: " & QueryRes.StringByIndex(0) & "<br>"
    Set QueryRes = SDB.Database.OpenSQL( "SELECT COUNT(*) FROM Artists")
    Doc.Write "Artist count: " & QueryRes.StringByIndex(0) & "<br>"
    Doc.Close
  End If

  If DD.ItemIndex=1 Then
    Doc.Write "Report 2"
    Doc.Close
  End If
End Sub