Public Class Form1
    Private m_Xwid As Integer = 8
    Private Const TAB_MARGIN As Integer = 3
    ' API to reduce RTB flicker when colorizing words
    Private WithEvents sDoc As HtmlDocument
    Private oEl As HtmlElement
    Private elID As Integer 'Counter

    Private Sub frmDocExplorer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        WebBrowser1.Dispose()
        FastColoredTextBox1.Dispose()
        TreeView1.Dispose()
        PropertyGrid1.Dispose()
    End Sub

    Private Sub frmDocExplorer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sDoc = WebBrowser1.Document
        PropertyGrid1.SelectedObject = sDoc

    End Sub
#Region " Load Doc Outline "

    Private Sub LoadDocumentOutline()
        On Error Resume Next
        TreeView1.Cursor = Cursors.WaitCursor
        TreeView1.BeginUpdate()
        Dim oitm As TreeNode
        Dim oelmt As HtmlElement
        Dim i As Integer
        For Each oelmt In WebBrowser1.Document.Body.Children
            If oelmt.Children.Count > 0 Then
                If IsNothing(oelmt.Id) Then
                    oEl.SetAttribute("ID", "de" & elID)
                End If
                oitm = New TreeNode
                oitm.Text = "<" & oelmt.TagName & ">"
                oitm.Name = oelmt.Id
                oitm.Tag = oelmt.Id
                oitm.ImageIndex = GetIconID(oelmt.TagName)
                oitm.SelectedImageIndex = GetIconID(oelmt.TagName)
                GetChildElements(oitm, oelmt)
                TreeView1.Nodes(0).Nodes(0).Nodes.Add(oitm)
            End If
            elID = elID + 1
        Next
        TreeView1.ExpandAll()
        TreeView1.EndUpdate()
        TreeView1.Cursor = Cursors.Default
    End Sub

    Private Sub GetChildElements(ByVal oNode As TreeNode, ByVal oElement As HtmlElement)
        Dim cl As HtmlElement
        Dim cn As TreeNode
        For Each cl In oElement.Children
            If IsNothing(cl.Id) Then
                cl.SetAttribute("ID", "de" & elID)
            End If
            cn = New TreeNode
            cn.Text = "<" & cl.TagName & ">"
            cn.Name = cl.Id
            cn.Tag = cl.Id
            cn.ImageIndex = GetIconID(cl.TagName)
            cn.SelectedImageIndex = GetIconID(cl.TagName)
            If cl.Children.Count > 0 Then
                GetChildElements(cn, cl)
            End If
            oNode.Nodes.Add(cn)
            elID = elID + 1
        Next
    End Sub

    Private Function GetIconID(ByVal strTag As String) As Integer
        'This code needs to be moved to the control
        Select Case strTag
            Case "TABLE"
                Return 1
            Case "TBODY"
                Return 1
            Case "A"
                Return 7
            Case "B"
                Return 8
            Case "SCRIPT"
                Return 19
            Case "H1"
                Return 24
            Case "H2"
                Return 25
            Case "H3"
                Return 26
            Case "H4"
                Return 27
            Case "H5"
                Return 28
            Case "H6"
                Return 29
            Case "TR"
                Return 4
            Case "TD"
                Return 3
            Case "UL"
                Return 5
            Case "LI"
                Return 16
            Case "SELECT"
                Return 22
            Case "IMG"
                Return 15
            Case "P"
                Return 18
            Case "BUTTON"
                Return 20
            Case "FONT"
                Return 12
            Case "INPUT"
                Return 21
            Case "FORM"
                Return 13
            Case "CENTER"
                Return 9
            Case Else
                Return 2
        End Select
    End Function
#End Region

    Private Sub tvDoc_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs)
        Select Case e.Node.Tag
            Case "DOC"
                PropertyGrid1.SelectedObject = WebBrowser1.Document
            Case "BODY"
                PropertyGrid1.SelectedObject = WebBrowser1.Document.Body
            Case Else
                If Not IsNothing(e.Node.Tag) Then
                    Dim tmpEl As HtmlElement = sDoc.GetElementById(e.Node.Tag)
                    PropertyGrid1.SelectedObject = tmpEl
                    tmpEl.ScrollIntoView(False)
                End If
        End Select
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        sDoc = WebBrowser1.Document

        LoadDocumentOutline()

        PropertyGrid1.SelectedObject = sDoc
    End Sub

    Private Sub sDoc_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles sDoc.Click
        Try
            Dim MousePoint As New Point(e.MousePosition.X, e.MousePosition.Y)
            oEl = sDoc.GetElementFromPoint(MousePoint)
            PropertyGrid1.SelectedObject = oEl
            Dim oNode As TreeNode = FindNode(TreeView1.Nodes(0), oEl.Id)
            If Not IsNothing(oNode) Then
                TreeView1.SelectedNode = oNode
                TreeView1.Select()
                oNode.EnsureVisible()
            End If
        Catch ex As Exception
            'do nothing
        End Try
    End Sub

    Public Function FindNode(ByVal ParentNode As TreeNode, ByVal SearchVal As String) As TreeNode
        Dim tmpNode As TreeNode
        If ParentNode.Tag = SearchVal Then
            Return ParentNode
        Else
            Dim child As TreeNode
            For Each child In ParentNode.Nodes
                tmpNode = FindNode(child, SearchVal)
                If Not tmpNode Is Nothing Then
                    Return tmpNode
                End If
            Next
        End If
        Return Nothing
    End Function

    Private Sub AbrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbrirToolStripMenuItem.Click
        If TabPage1.Focus = True Then
            Dim Open As New OpenFileDialog()
            Dim myStreamReader As System.IO.StreamReader
            Open.Filter = "Html [*.html*]|*.html*|all files(*.*)|*.*"
            Open.CheckPathExists = True
            Open.Title = "Abrir Archivo"
            Open.ShowDialog(Me)
            Try
                Open.OpenFile()
                myStreamReader = System.IO.File.OpenText(Open.FileName)
                FastColoredTextBox1.Text = myStreamReader.ReadToEnd()
            Catch ex As Exception
            End Try
        End If
        If TabPage2.Focus = True Then
            Dim Open As New OpenFileDialog()
            Dim myStreamReader As System.IO.StreamReader
            Open.Filter = "Style Sheets [*.css*]|*.css*|all files(*.*)|*.*"
            Open.CheckPathExists = True
            Open.Title = "Abrir Archivo"
            Open.ShowDialog(Me)
            Try
                Open.OpenFile()
                myStreamReader = System.IO.File.OpenText(Open.FileName)
                FastColoredTextBox2.Text = myStreamReader.ReadToEnd()
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub CSSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NuevaHojaDeEstiloCSSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Open As New OpenFileDialog()
        Dim myStreamReader As System.IO.StreamReader
        Open.Filter = "Style Sheets [*.css*]|*.html*|all files(*.*)|*.*"
        Open.CheckPathExists = True
        Open.Title = "Abrir Archivo"
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            FastColoredTextBox1.Text = myStreamReader.ReadToEnd()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub NuevaHojaEnBlancoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FastColoredTextBox1.Clear()
        FastColoredTextBox1.Text = ""
    End Sub

    Private Sub GuardarComoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuardarComoToolStripMenuItem.Click
        If TabPage1.Focus = True Then
            Dim save As New SaveFileDialog()
            Dim myStreamwriter As System.IO.StreamWriter
            save.Filter = "Html (*.html)|*.html|html(*.html*)|*.html|php(*.php*)|*.php*|All files(*.*)|*.*"
            save.CheckPathExists = True
            save.Title = "Guardar Como"
            save.ShowDialog(Me)
            Try
                myStreamwriter = System.IO.File.AppendText(save.FileName)
                myStreamwriter.Write(FastColoredTextBox1.Text)
                myStreamwriter.Flush()
            Catch ex As Exception
            End Try
        End If
        If TabPage2.Focus = True Then
            Dim save As New SaveFileDialog()
            Dim myStreamwriter As System.IO.StreamWriter
            save.Filter = "Hojas de estilo CSS(*.css*)|*.css*|All files(*.*)|*.*"
            save.CheckPathExists = True
            save.Title = "Guardar Como"
            save.ShowDialog(Me)
            Try
                myStreamwriter = System.IO.File.AppendText(save.FileName)
                myStreamwriter.Write(FastColoredTextBox2.Text)
                myStreamwriter.Flush()
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub NuevaHojaDeEstilosCSSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form2.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form2.Show()
    End Sub

    Private Sub SalirToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem1.Click
        End
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        splash.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.Parent = TabControl1.SelectedTab

        Me.Text = "Creative web design - " + ToolStripTextBox2.Text
        RichTextBox2.Text = FastColoredTextBox1.Text
        Me.Text = ToolStripTextBox2.Text
        welcome.Show()
        'If WebBrowser1.DocumentText.Contains("1.0") Then
        'Label1.Visible = False
        'Else
        'MsgBox("¡1 Actualización disponible!", MsgBoxStyle.Information)
        'End If
    End Sub

    Private Sub SplitContainer1_Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel2.Paint

    End Sub

    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        Dim creativity As New Form1
        creativity.Show()
    End Sub

    Private Sub GuardarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DescargarActualizacionesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DescargarActualizacionesToolStripMenuItem.Click
        actualizar.Show()
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If WebBrowser1.DocumentText.Contains("Navegación cacelada") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
        If WebBrowser1.DocumentText.Contains("Internet Explorer no puede") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
        If WebBrowser1.DocumentText.Contains("No se puede mostrar esta página") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
        If WebBrowser1.DocumentTitle.Contains("Activación de Servicios") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
        If WebBrowser1.DocumentText.Contains("Se canceló la navegación a la página web") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
        If WebBrowser1.DocumentText.Contains("Este programa no puede") Then
            WebBrowser1.DocumentText = RichTextBox4.Text
        End If
    End Sub

    Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Tab Then
            FastColoredTextBox1.Text = FastColoredTextBox1.Text + "    "
        End If
    End Sub

    Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        'Firstly we'll define some parameters.
        Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
        Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
        Dim FillBrush As New SolidBrush(Color.Black)
        Dim TextBrush As New SolidBrush(Color.Black)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        'If we are currently painting the Selected TabItem we'll 
        'change the brush colors and inflate the rectangle.
        If CBool(e.State And DrawItemState.Selected) Then
            e.Graphics.DrawImage(My.Resources.pestaña1, ItemRect)
            FillBrush.Color = Color.Black
            TextBrush.Color = Color.Black
            ItemRect.Inflate(3, 2)
        Else
            e.Graphics.DrawImage(My.Resources.pestaña_atras, ItemRect)
            FillBrush.Color = Color.DimGray
            TextBrush.Color = Color.DimGray
            ItemRect.Inflate(1, 2)
        End If


        'Set up rotation for left and right aligned tabs
        If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
            Dim RotateAngle As Single = 100
            If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
            Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 3))
            e.Graphics.TranslateTransform(cp.X, cp.Y)
            e.Graphics.RotateTransform(RotateAngle)
            ItemRect = New Rectangle(-(ItemRect.Height \ 3), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
        End If

        ' We draw in the TabRect rather than on e.Bounds

        ' so we can use TabRect later in MouseDown.
        Dim tab_rect As Rectangle = TabControl1.GetTabRect(e.Index)
        ' Allow room for margins.
        Dim layout_rect As New RectangleF( _
            tab_rect.Left + TAB_MARGIN, _
            tab_rect.Y + TAB_MARGIN, _
            tab_rect.Width - 3 * TAB_MARGIN, _
            tab_rect.Height - 3 * TAB_MARGIN)
        Dim string_format As New StringFormat


        'Next we'll paint the TabItem with our Fill Brush
        If CBool(e.State And DrawItemState.Selected) Then
            e.Graphics.DrawImage(My.Resources.pestaña1, ItemRect)
            FillBrush.Color = Color.Black
            TextBrush.Color = Color.Black
            ItemRect.Inflate(3, 1)
        Else
            e.Graphics.DrawImage(My.Resources.pestaña_atras, ItemRect)
            FillBrush.Color = Color.DimGray
            TextBrush.Color = Color.DimGray
            ItemRect.Inflate(1, 3)
        End If

        ' Draw the tab's text centered.
        Dim big_font As New Font(Me.Font, FontStyle.Regular)
        string_format.Alignment = StringAlignment.Center

        e.Graphics.DrawString( _
            TabControl1.TabPages(e.Index).Text, _
            big_font, _
            FillBrush, _
            layout_rect, _
            string_format)

        'draw close button
        e.Graphics.DrawImage(My.Resources.cerrar4, layout_rect.Right - m_Xwid - 10, layout_rect.Top + m_Xwid - 8, 15, 15)

        'Reset any Graphics rotation
        e.Graphics.ResetTransform()


        'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()

    End Sub

    Private Sub SeleccionarTodoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeleccionarTodoToolStripMenuItem.Click
        FastColoredTextBox1.SelectAll()
    End Sub

    Private Sub BorrarTodoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BorrarTodoToolStripMenuItem.Click
        FastColoredTextBox1.Clear()
    End Sub

    Private Sub CopiarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopiarToolStripMenuItem.Click
        FastColoredTextBox1.Copy()
    End Sub

    Private Sub CortarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CortarToolStripMenuItem.Click
        FastColoredTextBox1.Cut()
    End Sub

    Private Sub PegarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PegarToolStripMenuItem.Click
        FastColoredTextBox1.Paste()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        SplitContainer1.SendToBack()
        Panel1.BringToFront()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Panel1.SendToBack()
        SplitContainer1.BringToFront()

        WebBrowser1.DocumentText = ""
        WebBrowser1.DocumentText = FastColoredTextBox1.Text
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        If TabPage1.Focus = True Then
            FastColoredTextBox1.Clear()
        End If
        If TabPage2.Focus = True Then
            FastColoredTextBox2.Clear()
        End If
    End Sub

    Private Sub ToolStripTextBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripTextBox2.Click
        Me.Text = "Creative web design - " + ToolStripTextBox2.Text
    End Sub

    Private Sub ToolStripTextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripTextBox2.TextChanged
        Me.Text = "Creative web design - " + ToolStripTextBox2.Text
    End Sub

    Private Sub ConfiguracionesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfiguracionesToolStripMenuItem.Click
        preferencias.Show()
    End Sub

    Private Sub ToolStrip1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStrip1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Button3.Location = New Point(PointToClient(Cursor.Position))
            If Button3.Top = Button2.Top Then
                MenuStrip1.BringToFront()
            End If
            If Button3.Top = Button4.Top Then
                MenuStrip1.SendToBack()
            End If
        End If
    End Sub

    Private Sub FastColoredTextBox1_TextChanged(ByVal sender As Object, ByVal e As FastColoredTextBoxNS.TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
        RichTextBox2.Text = FastColoredTextBox1.Text
        WebBrowser1.DocumentText = ""
    End Sub

    Private Sub WebBrowser1_DocumentCompleted1(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        sDoc = WebBrowser1.Document

        LoadDocumentOutline()

        PropertyGrid1.SelectedObject = sDoc
    End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
    End Sub
End Class
