'  
'   Rebex Sample Code License
' 
'   Copyright 2025, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Imports Rebex.TerminalEmulation

''' <summary>
''' The main form of the application. It contains the terminal control.
''' </summary>
Public Class MainForm
    Inherits Form
    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private terminalFontDialog As System.Windows.Forms.FontDialog
    Private terminalOpenFileDialog As System.Windows.Forms.OpenFileDialog
    Private menuItemFile As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents menuItemOpen As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents menuItemClearScreen As System.Windows.Forms.ToolStripMenuItem
    Private menuItemTools As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents menuItemFont As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents menuItemOptions As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents mmenu As System.Windows.Forms.MenuStrip
    Private WithEvents menuItemExit As System.Windows.Forms.ToolStripMenuItem
    Private WithEvents tcConsole As TerminalControl

    ''' <summary>
    ''' Initializes this class.
    ''' </summary>
    Public Sub New()
        InitializeComponent()

        CheckForIllegalCrossThreadCalls = True

        ' set the default encoding
        tcConsole.Options.Encoding = Encoding.UTF8

        ' set the size of the form to 80 columns and 25 rows
        ClientSize = tcConsole.GetControlSize(80, 25)

        ' register the RemoteResize event 
        AddHandler tcConsole.RemoteResize, AddressOf Console_RemoteResize
    End Sub

    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso Not (components Is Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

#Region "Windows Form Designer generated code"

    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
        Me.terminalFontDialog = New System.Windows.Forms.FontDialog
        Me.terminalOpenFileDialog = New System.Windows.Forms.OpenFileDialog
        Me.mmenu = New System.Windows.Forms.MenuStrip
        Me.menuItemFile = New System.Windows.Forms.ToolStripMenuItem
        Me.menuItemOpen = New System.Windows.Forms.ToolStripMenuItem
        Me.menuItemClearScreen = New System.Windows.Forms.ToolStripMenuItem
        Me.menuItemTools = New System.Windows.Forms.ToolStripMenuItem
        Me.menuItemFont = New System.Windows.Forms.ToolStripMenuItem
        Me.menuItemOptions = New System.Windows.Forms.ToolStripMenuItem
        Me.tcConsole = New TerminalControl
        Me.menuItemExit = New System.Windows.Forms.ToolStripMenuItem
        Me.mmenu.SuspendLayout()
        Me.SuspendLayout()
        '
        'terminalOpenFileDialog
        '
        Me.terminalOpenFileDialog.Filter = "ANSI files (*.ans;*.ansi)|*.ans;*.ansi|All files (*.*)|*.*"
        Me.terminalOpenFileDialog.InitialDirectory = "..\SampleFiles"
        '
        'mmenu
        '
        Me.mmenu.Items.AddRange(New System.Windows.Forms.ToolStripMenuItem() {Me.menuItemFile, Me.menuItemTools})
        '
        'menuItemFile
        '
        Me.menuItemFile.MergeIndex = 0
        Me.menuItemFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripMenuItem() {Me.menuItemOpen, Me.menuItemClearScreen, Me.menuItemExit})
        Me.menuItemFile.Text = "&File"
        '
        'menuItemOpen
        '
        Me.menuItemOpen.MergeIndex = 0
        Me.menuItemOpen.Text = "&Open..."
        '
        'menuItemClearScreen
        '
        Me.menuItemClearScreen.MergeIndex = 1
        Me.menuItemClearScreen.Text = "&Clear screen"
        '
        'menuItemTools
        '
        Me.menuItemTools.MergeIndex = 1
        Me.menuItemTools.DropDownItems.AddRange(New System.Windows.Forms.ToolStripMenuItem() {Me.menuItemFont, Me.menuItemOptions})
        Me.menuItemTools.Text = "&Tools"
        '
        'menuItemFont
        '
        Me.menuItemFont.MergeIndex = 0
        Me.menuItemFont.Text = "&Font..."
        '
        'menuItemOptions
        '
        Me.menuItemOptions.MergeIndex = 1
        Me.menuItemOptions.Text = "&Options..."
        '
        'tcConsole
        '
        Me.tcConsole.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tcConsole.AutoAdjustTerminalSize = True
        Me.tcConsole.BackColor = System.Drawing.Color.Magenta
        Me.tcConsole.Location = New System.Drawing.Point(0, 27)
        Me.tcConsole.Name = "tcConsole"
        Me.tcConsole.Recorder = Nothing
        Me.tcConsole.ScrollbackResetOnDisplayActivity = True
        Me.tcConsole.ScrollBarEnabled = True
        Me.tcConsole.Size = New System.Drawing.Size(737, 400)
        Me.tcConsole.TabIndex = 0
        Me.tcConsole.UserInputEnabled = True
        '
        'menuItemExit
        '
        Me.menuItemExit.MergeIndex = 2
        Me.menuItemExit.Text = "E&xit"
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(736, 425)
        Me.Controls.Add(Me.tcConsole)
        Me.Controls.Add(Me.mmenu)
        Me.KeyPreview = True
        Me.MainMenuStrip = Me.mmenu
        Me.Name = "MainForm"
        Me.Text = "Rebex ANSI Viewer"
        Me.mmenu.ResumeLayout(False)
        Me.mmenu.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    ''' <summary>
    ''' Handles the console RemoteResize event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Console_RemoteResize(ByVal sender As Object, ByVal e As RemoteResizeEventArgs)
        ' set the size of the form to the size of the console
        ClientSize = tcConsole.GetControlSize(e.Columns, e.Rows)

        ' console screen is not set immediately, set it manually right now
        tcConsole.SetScreenSize(e.Columns, e.Rows)
    End Sub

    ''' <summary>
    ''' Handles the 'Font' menu item click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub menuItemFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemFont.Click
        ' show the font dialog
        If terminalFontDialog.ShowDialog() <> DialogResult.OK Then
            Return
        End If

        ' check whether the selected font family is supported
        If Not TerminalFont.IsSupportedFamily(terminalFontDialog.Font.FontFamily) Then
            MessageBox.Show(String.Format("Font {0} is not supported. Please choose another.", terminalFontDialog.Font.FontFamily.Name))
            Return
        End If

        ' set selected font to the console
        tcConsole.TerminalFont = New TerminalFont(terminalFontDialog.Font.FontFamily, terminalFontDialog.Font.Size)
    End Sub

    ''' <summary>
    ''' Handles the 'Option' menu item click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub menuItemOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemOptions.Click
        ' show options dialog
        Dim options As New Options(tcConsole.Options)
        options.ShowDialog()
    End Sub

    ''' <summary>
    ''' Handles the 'Open' menu item click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub menuItemOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemOpen.Click
        ' open the file dialog
        If terminalOpenFileDialog.ShowDialog() <> DialogResult.OK Then
            Return
        End If

        ' initialize new StreamReader according to the chosen file and terminal encoding
        Dim reader As StreamReader = Nothing
        Try
            reader = New StreamReader(terminalOpenFileDialog.FileName, tcConsole.Options.Encoding)

            ' read the data from the file
            Dim data As String = reader.ReadToEnd()

            ' write the data on the terminal screen
            tcConsole.Screen.Write(data)
        Finally
            If Not reader Is Nothing Then reader.Close()
        End Try
    End Sub

    ''' <summary>
    ''' Handles the 'Clear screen' menu item click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub menuItemClearScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemClearScreen.Click
        ' clear the terminal screen
        tcConsole.Screen.Clear()
    End Sub

    ''' <summary>
    ''' Handles the 'Exit' menu item click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub menuItemExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemExit.Click
        Close()
    End Sub
End Class
