Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TGML 1

Status
Not open for further replies.

earthandfire

Programmer
Mar 14, 2005
2,924
GB
ZmrAbdulla posed a problem in thread796-1152284, which both ca8msm and I found very interesting and intriguing. In one of his replies ca8msm sugested that it would be a good idea if we could have a mechanism to embed colour tags between [code] [/code] tags.

This got me thinking. I can see two possible solutions.

1)
Create a VS AddIn. This would (hopefully) enable us to see the formatting used and then we would be able to pick up the colours and output a document with the appropriate formatting. I don't, however, see this as the easy option.

2)
Create a program that will parse a block of text looking for key features. For example the apostrophe could indicate that what follows should be green. A list of reserved words could be used to match up blue. Obviously this is a very simplistic summary, but, I think far more achievable than option 1. This could then be extended to support the rainbow of colours used in SQL Server.

I intend to experiment with option two.

I've posted this a separate topic in the hope that it will ignite some interest. I was actually quite surprised that so few contributors posted to the other thread.


[vampire][bat]
 
Almost done.

Extended support for some Tags is still required in the manual TGML section and then to include chrissie's code as the handler to auto-process text between [ignore]
Code:
[/ignore] Tags. I need to make a couple of changes to the RegEx before doing that though. Hopefully tomorrow or Wednesday.

Code:
Imports Microsoft.Win32
[Blue]Public[/Blue] [Blue]Class[/Blue] TGMLRichTextBox
  [Blue]Inherits[/Blue] System.Windows.Forms.RichTextBox

#[Blue]Region[/Blue] [Red]" Windows Form Designer generated code "[/Red]

  [Blue]Public[/Blue] [Blue]Sub[/Blue] [Blue]New[/Blue]()
    [Blue]MyBase[/Blue].[Blue]New[/Blue]()

    [Green]'This call is required by the Windows Form Designer.[/Green]
    InitializeComponent()

    [Green]'Add any initialization after the InitializeComponent() call[/Green]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Green]'UserControl overrides dispose to clean up the component list.[/Green]
  [Blue]Protected[/Blue] [Blue]Overloads[/Blue] [Blue]Overrides[/Blue] [Blue]Sub[/Blue] Dispose([Blue]ByVal[/Blue] disposing [Blue]As[/Blue] [Blue]Boolean[/Blue])
    [Blue]If[/Blue] disposing [Blue]Then[/Blue]
      [Blue]If[/Blue] Not (components [Blue]Is[/Blue] [Blue]Nothing[/Blue]) [Blue]Then[/Blue]
        components.Dispose()
      [Blue]End[/Blue] [Blue]If[/Blue]
    [Blue]End[/Blue] [Blue]If[/Blue]
    [Blue]MyBase[/Blue].Dispose(disposing)
  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Green]'Required by the Windows Form Designer[/Green]
  [Blue]Private[/Blue] components [Blue]As[/Blue] System.ComponentModel.IContainer

  [Green]'NOTE: The following procedure is required by the Windows Form Designer[/Green]
  [Green]'It can be modified using the Windows Form Designer.  [/Green]
  [Green]'Do not modify it using the code editor.[/Green]
  <System.Diagnostics.DebuggerStepThrough()> [Blue]Private[/Blue] [Blue]Sub[/Blue] InitializeComponent()
    components = [Blue]New[/Blue] System.ComponentModel.Container
  [Blue]End[/Blue] [Blue]Sub[/Blue]

#[Blue]End[/Blue] [Blue]Region[/Blue]

#[Blue]Region[/Blue] [Red]"Local Variables"[/Red]

  [Blue]Private[/Blue] WithEvents TGMLListBox [Blue]As[/Blue] ListBox

  [Blue]Private[/Blue] ActivationType [Blue]As[/Blue] ActivationTypes

  [Blue]Private[/Blue] CurrentPosition [Blue]As[/Blue] [Blue]Integer[/Blue] = 0
  [Blue]Private[/Blue] SearchString [Blue]As[/Blue] [Blue]String[/Blue] = [Red]""[/Red]

#[Blue]End[/Blue] [Blue]Region[/Blue]

#[Blue]Region[/Blue] [Red]"Properties"[/Red]

  [Blue]Private[/Blue] FActivateOnRightClick [Blue]As[/Blue] [Blue]Boolean[/Blue]
  [Blue]Private[/Blue] FActivateOnChar() [Blue]As[/Blue] Char

  [Blue]Public[/Blue] Property ActivateOnRightClick() [Blue]As[/Blue] [Blue]Boolean[/Blue]
    Get
      [Blue]Return[/Blue] FActivateOnRightClick
    [Blue]End[/Blue] Get
    Set([Blue]ByVal[/Blue] Value [Blue]As[/Blue] [Blue]Boolean[/Blue])
      FActivateOnRightClick = Value
    [Blue]End[/Blue] Set
  [Blue]End[/Blue] Property

  <System.ComponentModel.Browsable([Blue]False[/Blue])> _
  [Blue]Public[/Blue] Property ActivateOnChar() [Blue]As[/Blue] Char()
    Get
      [Blue]Return[/Blue] FActivateOnChar
    [Blue]End[/Blue] Get
    Set([Blue]ByVal[/Blue] Value [Blue]As[/Blue] Char())
      FActivateOnChar = Value
    [Blue]End[/Blue] Set
  [Blue]End[/Blue] Property

#[Blue]End[/Blue] [Blue]Region[/Blue]

  [Blue]Public[/Blue] [Blue]Function[/Blue] ProcessTGML() [Blue]As[/Blue] [Blue]String[/Blue]

  [Blue]End[/Blue] [Blue]Function[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] PopulateListBox()

    With TGMLListBox
      .Items.Add([Red]"Code"[/Red])      [Green]'Needs additional handling to be done later[/Green]
      .Items.Add([Red]"Bold"[/Red])
      .Items.Add([Red]"Underline"[/Red])
      .Items.Add([Red]"Italic"[/Red])
      [Green]'.Items.Add([Red]"Colour"[/Red])   'Needs special handling to be added later[/Green]
      [Green]'.Items.Add([Red]"Quote"[/Red])    'Needs special handling to be added later[/Green]
      [Green]'.Items.Add([Red]"Image"[/Red])    'Needs special handling to be added later[/Green]
      [Green]'.Items.Add([Red]"Url"[/Red])      'Needs special handling to be added later[/Green]
    [Blue]End[/Blue] With

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] DisplayListBox([Blue]ByVal[/Blue] CurPoint [Blue]As[/Blue] Point)

    [Green]'Use Registry to get BorderWidth[/Green]
    [Green]'===============================[/Green]
    [Green]'A bit of experimentation in the different coding styles here[/Green]
    [Green]'All three work so I've left them in.[/Green]

    [Green]'Style 1: Step By Step[/Green]
    [Green]'=====================[/Green]
    [Green]'Dim CurrentUser As RegistryKey = Registry.CurrentUser[/Green]
    [Green]'Dim ControlPanel As RegistryKey = CurrentUser.OpenSubKey([Red]"Control Panel"[/Red])[/Green]
    [Green]'Dim Desktop As RegistryKey = ControlPanel.OpenSubKey([Red]"Desktop"[/Red])[/Green]
    [Green]'Dim WindowMetrics As RegistryKey = Desktop.OpenSubKey([Red]"WindowMetrics"[/Red])[/Green]

    [Green]'Dim BorderWidth As Integer = Math.Abs(Convert.ToInt32(WindowMetrics.GetValue([Red]"BorderWidth"[/Red])))[/Green]

    [Green]'WindowMetrics.Close()[/Green]
    [Green]'Desktop.Close()[/Green]
    [Green]'ControlPanel.Close()[/Green]
    [Green]'CurrentUser.Close()[/Green]

    [Green]'Style 2: Use a single variable for the Registry Key[/Green]
    [Green]'===================================================[/Green]
    [Green]'Dim WindowMetrics As RegistryKey = Registry.CurrentUser.OpenSubKey([Red]"Control Panel"[/Red]).OpenSubKey([Red]"Desktop"[/Red]).OpenSubKey([Red]"WindowMetrics"[/Red])[/Green]
    [Green]'Dim BorderWidth As Integer = Math.Abs(Convert.ToInt32(WindowMetrics.GetValue([Red]"BorderWidth"[/Red])))[/Green]
    [Green]'WindowMetrics.Close()[/Green]

    [Green]'Style 3: No Registry Key variable[/Green]
    [Green]'=================================[/Green]
    [Green]'    - does these mean that the Registry Key(s) are left open???[/Green]
    [Green]'    - if so will that cause a problem???[/Green]
    [Blue]Dim[/Blue] BorderWidth [Blue]As[/Blue] [Blue]Integer[/Blue] = Math.Abs(Convert.ToInt32(Registry.CurrentUser.OpenSubKey([Red]"Control Panel"[/Red]).OpenSubKey([Red]"Desktop"[/Red]).OpenSubKey([Red]"WindowMetrics"[/Red]).GetValue([Red]"BorderWidth"[/Red])))

    [Green]'Get Inner Dimensions to allow for ScrollBar(s)[/Green]
    [Blue]Dim[/Blue] MyWidth [Blue]As[/Blue] [Blue]Integer[/Blue] = [Blue]Me[/Blue].ClientSize.Width
    [Blue]Dim[/Blue] MyHeight [Blue]As[/Blue] [Blue]Integer[/Blue] = [Blue]Me[/Blue].ClientSize.Height


    [Green]'The above sections need to run whenever the ListBox is displayed because the user[/Green]
    [Green]'could change their Desktop style(s) and thus the BorderWidth, and[/Green]
    [Green]'ScrollBars could appear dynamically[/Green]

    [Green]'=================================================================================[/Green]

    TGMLListBox = [Blue]New[/Blue] ListBox
    With TGMLListBox
      PopulateListBox()
      .BorderStyle = BorderStyle.FixedSingle
      .Cursor = Cursors.Arrow
      CurrentPosition = [Blue]Me[/Blue].SelectionStart
      SearchString = [Red]""[/Red]
      CurPoint.Y += [Blue]Me[/Blue].SelectionFont.Height
      [Blue]If[/Blue] CurPoint.X < 0 [Blue]Then[/Blue] CurPoint.X = BorderWidth
      [Blue]If[/Blue] CurPoint.Y < 0 [Blue]Then[/Blue] CurPoint.Y = BorderWidth
      [Blue]If[/Blue] CurPoint.X + .Width > (MyWidth - BorderWidth) [Blue]Then[/Blue] CurPoint.X = MyWidth - .Width
      [Blue]If[/Blue] CurPoint.Y + .Height > (MyHeight - BorderWidth) [Blue]Then[/Blue] CurPoint.Y = MyHeight - .Height
      .Location = CurPoint
      .Visible = [Blue]True[/Blue]
      [Blue]Me[/Blue].Controls.Add(TGMLListBox)
      .BringToFront()
      .Focus()
    [Blue]End[/Blue] With

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] CloseListBox()

    [Blue]Me[/Blue].Controls.Remove(TGMLListBox)
    TGMLListBox = [Blue]Nothing[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] InsertManualTGML()

    [Blue]If[/Blue] ActivationType = ActivationTypes.Keyboard Or [Blue]Me[/Blue].SelectionLength = 0 [Blue]Then[/Blue]
      [Blue]Me[/Blue].SelectionStart -= 1
      [Blue]Me[/Blue].SelectionLength = 1
      [Blue]Me[/Blue].SelectedText = [Red]"["[/Red] + TGMLListBox.SelectedItem.ToString + [Red]"][/"[/Red] + TGMLListBox.SelectedItem.ToString + [Red]"]"[/Red]
      [Blue]Me[/Blue].SelectionStart -= TGMLListBox.SelectedItem.ToString.Length + 3  [Green]'+3 = [Red]"[/]"[/Red][/Green]
    [Blue]Else[/Blue]
      [Blue]Dim[/Blue] s [Blue]As[/Blue] [Blue]String[/Blue] = [Blue]Me[/Blue].SelectedText
      [Blue]Me[/Blue].SelectedText = [Red]"["[/Red] + TGMLListBox.SelectedItem.ToString + [Red]"]"[/Red] + s + [Red]"[/"[/Red] + TGMLListBox.SelectedItem.ToString + [Red]"]"[/Red]
    [Blue]End[/Blue] [Blue]If[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TGMLRichTextBox_MouseUp([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Windows.Forms.MouseEventArgs) [Blue]Handles[/Blue] [Blue]MyBase[/Blue].MouseUp

    [Blue]If[/Blue] e.Button = MouseButtons.Right [Blue]Then[/Blue]
      [Blue]If[/Blue] FActivateOnRightClick [Blue]Then[/Blue]
        ActivationType = ActivationTypes.Mouse
        DisplayListBox([Blue]New[/Blue] Point(e.X, e.Y - ([Blue]Me[/Blue].Top)))
      [Blue]End[/Blue] [Blue]If[/Blue]
    [Blue]End[/Blue] [Blue]If[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TGMLRichTextBox_KeyPress([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Windows.Forms.KeyPressEventArgs) [Blue]Handles[/Blue] [Blue]MyBase[/Blue].KeyPress

    [Blue]If[/Blue] ActivateOnChar.Length > 0 [Blue]Then[/Blue]
      [Blue]For[/Blue] a [Blue]As[/Blue] [Blue]Integer[/Blue] = 0 [Blue]To[/Blue] ActivateOnChar.Length - 1
        [Blue]If[/Blue] e.KeyChar = ActivateOnChar(a) [Blue]Then[/Blue]
          ActivationType = ActivationTypes.Keyboard
          DisplayListBox([Blue]Me[/Blue].GetPositionFromCharIndex([Blue]Me[/Blue].SelectionStart))
          [Blue]Exit[/Blue] [Blue]For[/Blue]
        [Blue]End[/Blue] [Blue]If[/Blue]
      [Blue]Next[/Blue]
    [Blue]End[/Blue] [Blue]If[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TGMLListBox_Click([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] TGMLListBox.Click

    InsertManualTGML()
    CloseListBox()

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TGMLListBox_KeyDown([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Windows.Forms.KeyEventArgs) [Blue]Handles[/Blue] TGMLListBox.KeyDown

    [Blue]If[/Blue] e.KeyCode = Keys.Escape [Blue]Then[/Blue]
      e.Handled = [Blue]True[/Blue]
      CloseListBox()
    [Blue]End[/Blue] [Blue]If[/Blue]

    Select Case e.KeyCode
      Case Keys.Escape
        e.Handled = [Blue]True[/Blue]
        CloseListBox()
      Case Keys.Enter, Keys.Space
        e.Handled = [Blue]True[/Blue]
        InsertManualTGML()
        CloseListBox()
      Case [Blue]Else[/Blue]
        e.Handled = [Blue]False[/Blue]
    [Blue]End[/Blue] Select

  [Blue]End[/Blue] [Blue]Sub[/Blue]
[Blue]End[/Blue] [Blue]Class[/Blue]

[Blue]Public[/Blue] Enum ActivationTypes
  Mouse
  Keyboard
[Blue]End[/Blue] Enum

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top