Imports System.Drawing.Printing
Imports System.ComponentModel

Imports Softelvdm.Controls
Imports Softelvdm.SftTabsNET

Public Class MainWindow
    Public Shared parentWindow As MainWindow
    Public Shared documentCount As Integer ' static var which keeps track of the document count

    Public Sub New()
        parentWindow = Me

        InitializeComponent()

        documentCount = 0
        CreateDocument("")
    End Sub


Private Sub MenuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileNew.Click
    NewDoc()
End Sub


Private Sub MenuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileOpen.Click
    Open()
End Sub

Private Sub MenuFileClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileClose.Click
    CloseView()
End Sub

Private Sub MenuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileSave.Click
    Save()
End Sub

Private Sub MenuFileSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileSaveAs.Click
    Save()
End Sub

Private Sub MenuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFilePrint.Click
    Print()
End Sub

Private Sub MenuFilePrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFilePrintPreview.Click
    PrintPreview()
End Sub

Private Sub MenuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileExit.Click
    ExitApp()
End Sub

Private Sub MenuEditClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuEditClearAll.Click
    Clear()
End Sub

Private Sub menuPenThick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPenThick.Click
    ThickPen()
End Sub

Private Sub MenuPenWidths_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPenWidths.Click
    PenWidthsDlg()
End Sub

Private Sub MenuViewToolbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuViewToolbar.Click
    toolBar1.Visible = MenuViewToolbar.Checked = Not toolBar1.Visible
End Sub

Private Sub MenuViewStatusbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuViewStatusbar.Click
    statusBar1.Visible = MenuViewStatusbar.Checked = Not statusBar1.Visible
End Sub

Private Sub MenuWindowNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuWindowNew.Click
    NewDoc()
End Sub

Private Sub MenuWindowCascade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuWindowCascade.Click
    Cascade()
End Sub

Private Sub MenuWindowTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuWindowTile.Click
    Tile()
End Sub

Private Sub MenuHelpTopics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuHelpTopics.Click
    ShowHelpTopics()
End Sub

Private Sub MenuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuHelpAbout.Click
    AboutHelp()
End Sub

' About Help
Private Sub AboutHelp()
    MessageBox.Show("Scribble Version 1.0+" + vbCrLf + vbCrLf + "Adapted to use SftTabs/NET for document selection.", "About Scribble", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

' Help Topics
Private Sub ShowHelpTopics()
    Help.ShowHelp(Me, "..\..\help\scribble.chm")
End Sub

' Print
Private Sub Print()
    Try
        printDoc.Print()
    Catch e As Exception
        MessageBox.Show(e.ToString())
    End Try
End Sub

' PrintPage event handler
Private Sub ScribblePrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
    Try
        Dim activeView As ScribbleView = ActiveMdiChild
        Dim activeDoc As ScribbleDoc = activeView.GetDocument()

        For i As Integer = 0 To activeDoc.StrokeList.Count - 1
            Dim st As Stroke = activeDoc.StrokeList(i)
            st.DrawStroke(ev.Graphics)
        Next
        ev.HasMorePages = False
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

' PrintPreview
Private Sub PrintPreview()
    Try
        Dim prevDlg As PrintPreviewDialog = New PrintPreviewDialog()
        prevDlg.Document = printDoc
        prevDlg.Size = New System.Drawing.Size(600, 329)
        prevDlg.ShowDialog()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

' Exit
Private Sub ExitApp()
    Dim childForm() As Form = MdiChildren
    ' Make sure to ask for saving the doc before exiting the app
    For i As Integer = 0 To childForm.Length - 1
        childForm(i).Close()
    Next
    Application.Exit()
End Sub

' Close the View
Private Sub CloseView()
    Dim activeView As ScribbleView = ActiveMdiChild
    activeView.Close()
End Sub

' Tile
Private Sub Tile()
    Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub

' Cascade
Private Sub Cascade()
    LayoutMdi(MdiLayout.Cascade)
End Sub

' Clear the contents of the active document
Private Sub Clear()
    Dim activeView As ScribbleView = ActiveMdiChild
    If Not activeView Is Nothing Then
        Dim activeDoc As ScribbleDoc = activeView.GetDocument()
        activeDoc.DeleteContents()
    End If
End Sub

' Open an existing document
Private Sub Open()
    Dim openDlg As OpenFileDialog = New OpenFileDialog()
    openDlg.Filter = "Scribble Files (*.scb)|*.scb|All Files (*.*)|*.*"
    openDlg.FileName = ""
    openDlg.DefaultExt = ".scb"
    openDlg.CheckFileExists = True
    openDlg.CheckPathExists = True

    Dim res As DialogResult = openDlg.ShowDialog()

    If res = System.Windows.Forms.DialogResult.OK Then
        If Not openDlg.FileName.EndsWith(".scb") And Not openDlg.FileName.EndsWith(".SCB") Then
            MessageBox.Show("Unexpected file format", "Scribble", MessageBoxButtons.OK)
        Else
            If ActiveMdiChild Is Nothing Then
                EnableItems()
            End If

            Dim newDoc As ScribbleDoc = CreateDocument(openDlg.FileName)
            newDoc.OpenDocument(openDlg.FileName)
        End If
    End If
End Sub

' Save the document
Private Sub Save()
    Dim selectedView As ScribbleView = ActiveMdiChild
    Dim saveDlg As SaveFileDialog = New SaveFileDialog()
    saveDlg.Filter = "Scribble Files (*.scb)|*.scb|All Files (*.*)|*.*"
    saveDlg.DefaultExt = ".scb"
    If selectedView.GetDocument().docFileName = "" Then
        saveDlg.FileName = "Untitled"
    Else
        saveDlg.FileName = selectedView.GetDocument().docFileName
    End If
    Dim res As DialogResult = saveDlg.ShowDialog()

    If res = System.Windows.Forms.DialogResult.OK Then
        selectedView.GetDocument().SaveDocument(saveDlg.FileName)
    End If
End Sub

' Open new document
Private Sub NewDoc()
    ' If this is the first child window, enable the Menu and Toolbar items
    If ActiveMdiChild Is Nothing Then
        EnableItems()
    End If
    CreateDocument("")
End Sub

' NewWindow
Private Sub NewWindow()
    Dim activeView As ScribbleView = ActiveMdiChild
    Dim newView As ScribbleView = New ScribbleView(activeView.GetDocument(), parentWindow)
    newView.GetDocument().ViewList.Add(newView)
    newView.SetTitle(activeView.GetDocument().docFileName)
    newView.Show()
End Sub

' Creates a new document
Private Function CreateDocument(ByVal docName As String) As ScribbleDoc
    Dim newDoc As ScribbleDoc = New ScribbleDoc(parentWindow, docName)
    documentCount = documentCount + 1
    Return newDoc
End Function

Private Sub PenWidthsDlg()
    Dim f As Form = New Form()

    ' Get the document of active view
    Dim activeView As ScribbleView = ActiveMdiChild
    Dim activeDoc As ScribbleDoc = activeView.GetDocument()

    f.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    f.Text = "Pen Widths"

    f.ClientSize = New System.Drawing.Size(352, 125)

    Dim button1 As Button = New Button()
    button1.Location = New System.Drawing.Point(264, 20)
    button1.Size = New System.Drawing.Size(75, 23)
    button1.TabIndex = 1
    button1.Text = "OK"
    button1.DialogResult = System.Windows.Forms.DialogResult.OK ' Make this "OK" button

    Dim button2 As Button = New Button()
    button2.Location = New System.Drawing.Point(264, 52)
    button2.Size = New System.Drawing.Size(75, 23)
    button2.TabIndex = 6
    button2.Text = "Cancel"

    Dim textBox1 As TextBox = New TextBox()
    textBox1.Location = New System.Drawing.Point(120, 36)
    textBox1.Text = activeDoc.ThinWidth.ToString()
    textBox1.TabIndex = 1
    textBox1.Size = New System.Drawing.Size(64, 20)

    Dim textBox2 As TextBox = New TextBox()
    textBox2.Location = New System.Drawing.Point(120, 76)
    textBox2.Text = activeDoc.ThickWidth.ToString()
    textBox2.TabIndex = 2
    textBox2.Size = New System.Drawing.Size(64, 20)

    Dim label1 As Label = New Label()
    label1.Location = New System.Drawing.Point(16, 36)
    label1.Text = "Thin Pen Width:"
    label1.Size = New System.Drawing.Size(88, 16)
    label1.TabIndex = 3

    Dim label2 As Label = New Label()
    label2.Location = New System.Drawing.Point(16, 76)
    label2.Text = "Thick Pen Width:"
    label2.Size = New System.Drawing.Size(88, 16)
    label2.TabIndex = 4

    f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    ' Set the MaximizeBox to false to remove the maximize box.
    f.MaximizeBox = False
    ' Set the MinimizeBox to false to remove the minimize box.
    f.MinimizeBox = False
    ' Set the accept button of the form to button1.
    f.AcceptButton = button1
    ' Set the cancel button of the form to button2.
    f.CancelButton = button2

    f.StartPosition = FormStartPosition.CenterScreen

    f.Controls.Add(button1)
    f.Controls.Add(button2)
    f.Controls.Add(label1)
    f.Controls.Add(label2)
    f.Controls.Add(textBox1)
    f.Controls.Add(textBox2)

    Dim res As DialogResult = f.ShowDialog()

    If res = System.Windows.Forms.DialogResult.OK Then
        activeDoc.ThinWidth = UInt32.Parse(textBox1.Text)
        activeDoc.ThickWidth = UInt32.Parse(textBox2.Text)
        activeDoc.ReplacePen()
        f.Close()
    End If
End Sub

Private Sub ThickPen()
    Dim activeView As ScribbleView = ActiveMdiChild
    Dim activeDoc As ScribbleDoc = activeView.GetDocument()
    activeDoc.ThickPen = Not activeDoc.ThickPen
    activeDoc.ReplacePen()
    MenuPenThick.Checked = activeDoc.ThickPen
End Sub

' Disable the menu and toolbar items when there is no active child form
Public Sub DisableItems()
    MenuItemEdit.Visible = False
    MenuItemPen.Visible = False
    MenuItemWindow.Visible = False
    MenuFileClose.Visible = False
    MenuFileSave.Visible = False
    MenuFileSaveAs.Visible = False
    MenuFilePrint.Visible = False
    MenuFilePrintPreview.Visible = False
    saveButton.Enabled = False
    previewButton.Enabled = False
    printButton.Enabled = False
End Sub

'Enable the menu and toolbar items when the first child form is created
Public Sub EnableItems()
    MenuItemEdit.Visible = True
    MenuItemPen.Visible = True
    MenuItemWindow.Visible = True
    MenuFileClose.Visible = True
    MenuFileSave.Visible = True
    MenuFileSaveAs.Visible = True
    MenuFilePrint.Visible = True
    MenuFilePrintPreview.Visible = True
    saveButton.Enabled = True
    previewButton.Enabled = True
    printButton.Enabled = True
End Sub

Private Sub toolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles toolBar1.ButtonClick
    If e.Button Is newButton Then
        NewDoc()
    ElseIf e.Button Is openButton Then
        Open()
    ElseIf e.Button Is saveButton Then
        Save()
    ElseIf e.Button Is previewButton Then
        PrintPreview()
    ElseIf e.Button Is printButton Then
        Print()
    ElseIf e.Button Is helptbButton Then
        ShowHelpTopics()
    End If
End Sub

' App closing handler
Public Sub ClosingMainAppHander(ByVal sender As Object, ByVal e As CancelEventArgs)
    ExitApp()
End Sub

Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' resize the tab control and its container (panel) to use the tab control's optimal height
    ' leave a small gap above the tab control
    sftTabs1.MakeNaturalSize()
    panelTabControl.Height = sftTabs1.Height + 4
    panelTabControl.Top = 4

    MessageBox.Show("This example is based on the ""Scribble"" sample application included with Visual Studio.NET.  It has been modified to use SftTabs/NET for document selection.", "SftTabs/NET Sample", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Private Sub sftTabs1_Switched(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sftTabs1.Switched
    ' A new tab has become active, activate the appropriate child window
    If sftTabs1.Current >= 0 Then
        Dim f As Form = sftTabs1.TabCollection(sftTabs1.Current).Tag1
        If Not f Is Nothing Then
            f.Activate()
        End If
    End If
End Sub

Private Sub MainWindow_MdiChildActivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MdiChildActivate
    ' An MDI child window is being activated, make the appropriate tab active
    Dim iTab As Integer = -1
    For Each t As TabClass In sftTabs1.TabCollection
        If t.Tag1 Is ActiveMdiChild Then
            iTab = t.Index
            Exit For
        End If
    Next
    sftTabs1.Current = iTab
End Sub
End Class