Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap

Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents ToolPanel1 As GraphicsSample.ToolPanel
    Friend WithEvents GraphicsPanel1 As GraphicsSample.GraphicsPanel
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents DeleteMenu As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem
    Friend WithEvents PreviousMenu As System.Windows.Forms.MenuItem
    Friend WithEvents BehindMenu As System.Windows.Forms.MenuItem
    Friend WithEvents NewMenu As System.Windows.Forms.MenuItem
    Friend WithEvents OpenMenu As System.Windows.Forms.MenuItem
    Friend WithEvents SaveMenu As System.Windows.Forms.MenuItem
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents PrintMenu As System.Windows.Forms.MenuItem
    Friend WithEvents ExitMenu As System.Windows.Forms.MenuItem
    Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents PageSetMenu As System.Windows.Forms.MenuItem
    Friend WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
        Me.ToolPanel1 = New GraphicsSample.ToolPanel
        Me.GraphicsPanel1 = New GraphicsSample.GraphicsPanel
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.NewMenu = New System.Windows.Forms.MenuItem
        Me.OpenMenu = New System.Windows.Forms.MenuItem
        Me.SaveMenu = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.PageSetMenu = New System.Windows.Forms.MenuItem
        Me.PrintMenu = New System.Windows.Forms.MenuItem
        Me.MenuItem7 = New System.Windows.Forms.MenuItem
        Me.ExitMenu = New System.Windows.Forms.MenuItem
        Me.MenuItem9 = New System.Windows.Forms.MenuItem
        Me.DeleteMenu = New System.Windows.Forms.MenuItem
        Me.MenuItem11 = New System.Windows.Forms.MenuItem
        Me.PreviousMenu = New System.Windows.Forms.MenuItem
        Me.BehindMenu = New System.Windows.Forms.MenuItem
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog
        Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
        Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog
        Me.SuspendLayout()
        '
        'ToolPanel1
        '
        Me.ToolPanel1.Dock = System.Windows.Forms.DockStyle.Right
        Me.ToolPanel1.FillCol = System.Drawing.Color.Empty
        Me.ToolPanel1.LineCol = System.Drawing.Color.Empty
        Me.ToolPanel1.Location = New System.Drawing.Point(480, 0)
        Me.ToolPanel1.Name = "ToolPanel1"
        Me.ToolPanel1.Size = New System.Drawing.Size(144, 462)
        Me.ToolPanel1.TabIndex = 0
        '
        'GraphicsPanel1
        '
        Me.GraphicsPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GraphicsPanel1.FillColor = System.Drawing.Color.Empty
        Me.GraphicsPanel1.LineColor = System.Drawing.Color.Empty
        Me.GraphicsPanel1.Location = New System.Drawing.Point(0, 0)
        Me.GraphicsPanel1.Name = "GraphicsPanel1"
        Me.GraphicsPanel1.Size = New System.Drawing.Size(480, 462)
        Me.GraphicsPanel1.TabIndex = 1
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem9})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.NewMenu, Me.OpenMenu, Me.SaveMenu, Me.MenuItem5, Me.PageSetMenu, Me.PrintMenu, Me.MenuItem7, Me.ExitMenu})
        Me.MenuItem1.Text = "ファイル(&F)"
        '
        'NewMenu
        '
        Me.NewMenu.Index = 0
        Me.NewMenu.Text = "新規作成（&N)"
        '
        'OpenMenu
        '
        Me.OpenMenu.Index = 1
        Me.OpenMenu.Text = "開く(&O)..."
        '
        'SaveMenu
        '
        Me.SaveMenu.Index = 2
        Me.SaveMenu.Text = "保存(&S)"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 3
        Me.MenuItem5.Text = "-"
        '
        'PageSetMenu
        '
        Me.PageSetMenu.Index = 4
        Me.PageSetMenu.Text = "ページ設定(&U)"
        '
        'PrintMenu
        '
        Me.PrintMenu.Index = 5
        Me.PrintMenu.Text = "印刷(&P)..."
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 6
        Me.MenuItem7.Text = "-"
        '
        'ExitMenu
        '
        Me.ExitMenu.Index = 7
        Me.ExitMenu.Text = "終了(&X)"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 1
        Me.MenuItem9.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.DeleteMenu, Me.MenuItem11, Me.PreviousMenu, Me.BehindMenu})
        Me.MenuItem9.Text = "編集(&E)"
        '
        'DeleteMenu
        '
        Me.DeleteMenu.Index = 0
        Me.DeleteMenu.Text = "削除(&D)"
        '
        'MenuItem11
        '
        Me.MenuItem11.Index = 1
        Me.MenuItem11.Text = "-"
        '
        'PreviousMenu
        '
        Me.PreviousMenu.Index = 2
        Me.PreviousMenu.Text = "前に出す（&P)"
        '
        'BehindMenu
        '
        Me.BehindMenu.Index = 3
        Me.BehindMenu.Text = "後ろに送る(&B)"
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.Filter = "XML形式ファイル|*.xml|すべてのファイル|*.*"
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Filter = "XML形式ファイル|*.xml|すべてのファイル|*.*"
        Me.OpenFileDialog1.Title = "ファイルの保存"
        '
        'PrintPreviewDialog1
        '
        Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.PrintPreviewDialog1.Document = Me.PrintDocument1
        Me.PrintPreviewDialog1.Enabled = True
        Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
        Me.PrintPreviewDialog1.Location = New System.Drawing.Point(182, 84)
        Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)
        Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
        Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
        Me.PrintPreviewDialog1.Visible = False
        '
        'PrintDocument1
        '
        '
        'PageSetupDialog1
        '
        Me.PageSetupDialog1.Document = Me.PrintDocument1
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(624, 462)
        Me.Controls.Add(Me.GraphicsPanel1)
        Me.Controls.Add(Me.ToolPanel1)
        Me.Menu = Me.MainMenu1
        Me.Name = "MainForm"
        Me.Text = "お絵かきサンプル"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim objects As GraphicsObjects

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 各種初期化
        Init()
    End Sub

    Private Sub Init()
        ' 各種初期化
        objects = New GraphicsObjects
        GraphicsPanel1.Objects = objects
        ToolPanel1.LineCol = Color.Black
        ToolPanel1.FillCol = Color.Empty
    End Sub

    Private Sub GraphicsPanel1_ResetTool() Handles GraphicsPanel1.ResetTool
        ' 描画が終わり、ツールボタンがクリアされた
        ToolPanel1.ToolButtonReset()
    End Sub

    Private Sub ToolPanel1_ToolChanged(ByVal NewTool As _Const.SelectedTool) Handles ToolPanel1.ToolChanged
        ' ツールが変わった
        GraphicsPanel1.Tool = NewTool
    End Sub

    Private Sub ToolPanel1_LineColorChanged(ByVal NewColor As System.Drawing.Color) Handles ToolPanel1.LineColorChanged
        GraphicsPanel1.LineColor = NewColor
    End Sub

    Private Sub ToolPanel1_FillColorChanged(ByVal NewColor As System.Drawing.Color) Handles ToolPanel1.FillColorChanged
        GraphicsPanel1.FillColor = NewColor
    End Sub

    Private Sub GraphicsPanel1_SelectedObjectChanged(ByVal NewObject As GraphicBase) Handles GraphicsPanel1.SelectedObjectChanged
        ' 選択されたオブジェクトが変化した
        If IsNothing(NewObject) Then
            ' 何も選択されていない
            DeleteMenu.Enabled = False
            PreviousMenu.Enabled = False
            BehindMenu.Enabled = False
        Else
            ' 何か選択されている
            DeleteMenu.Enabled = True
            PreviousMenu.Enabled = True
            BehindMenu.Enabled = True

            ToolPanel1.LineCol = NewObject.LineColor
            ToolPanel1.FillCol = NewObject.FillColor
        End If
    End Sub

    Private Sub DeleteMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteMenu.Click
        ' 削除
        GraphicsPanel1.Delete()
    End Sub

    Private Sub PreviousMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousMenu.Click
        ' 前面に送る
        GraphicsPanel1.MovePrevious()
    End Sub

    Private Sub BehindMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BehindMenu.Click
        ' 背面に送る
        GraphicsPanel1.MoveBehind()
    End Sub

    Private Sub NewMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewMenu.Click
        ' 新規作成
        Init()
    End Sub

    Private Sub SaveMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveMenu.Click
        ' 保存
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            ' Soapフォーマッタを使って保存
            Try
                Dim stm As Stream = SaveFileDialog1.OpenFile()
                Dim formatter As New SoapFormatter
                formatter.Serialize(stm, objects)
                stm.Close()
            Catch ex As Exception
                MsgBox("ファイルの保存に失敗しました")
            End Try
        End If
    End Sub

    Private Sub OpenMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenMenu.Click
        ' ファイルを開く
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Try
                Dim stm As Stream = OpenFileDialog1.OpenFile()
                Dim formatter As New SoapFormatter

                Dim newobj As GraphicsObjects = CType(formatter.Deserialize(stm), GraphicsObjects)
                stm.Close()
                Init()
                objects = newobj
                GraphicsPanel1.Objects = objects
                ToolPanel1.LineCol = Color.Black
                ToolPanel1.FillCol = Color.Empty
            Catch ex As Exception
                MsgBox("ファイルの読み込みに失敗しました")
            End Try
        End If
    End Sub

    Private Sub ExitMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenu.Click
        Me.Close()
    End Sub

    Private Sub PrintMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintMenu.Click
        ' 印刷
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        ' 印刷
        Dim g As Graphics = e.Graphics
        ' 単位をミリメートルにする
        g.PageUnit = GraphicsUnit.Millimeter
        ' オブジェクトを描画
        objects.Draw(g)

        ' これ以上のページはない
        e.HasMorePages = False
    End Sub

    Private Sub PageSetMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetMenu.Click
        ' ページ設定
        PageSetupDialog1.ShowDialog()
    End Sub
End Class
