Best approach to build a line number view for a text editor

Post

Posted
Rating:
#1 (In Topic #1288)
Regular
sergioabreu is in the usergroup ‘Regular’
 Hi
I am playing on a text editor
what is the best approach/components to implement the line number view ?

Would be a of scrollview, textarea and something together?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
How about this? Run the code in a Graphical Application.

Code (gambas)

  1. TextAreaLineNos As TextArea
  2. TextArea1 As TextArea
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   BuildForm
  7.   LineNos
  8.   Start
  9.  
  10.  
  11. ' Gambas class file
  12.  
  13. Public Sub Start()
  14.  
  15.   Dim sText As String = "To be, or not to be, that is the question:\nWhether 'tis nobler in the mind to suffer\nThe slings and arrows of outrageous fortune,\nOr to take arms against a sea of troubles\nAnd by opposing end them. To die—to sleep,\nNo more; and by a sleep to say we end\nThe heart-ache and the thousand natural shocks\nThat flesh is heir to: 'tis a consummation\nDevoutly to be wish'd. To die, to sleep;\nTo sleep, perchance to dream—ay, there's the rub:\nFor in that sleep of death what dreams may come,\nWhen we have shuffled off this mortal coil,\nMust give us pause—there's the respect\nThat makes calamity of so long life.\nFor who would bear the whips and scorns of time,\nTh'oppressor's wrong, the proud man's contumely,\nThe pangs of dispriz'd love, the law's delay,\nThe insolence of office, and the spurns\nThat patient merit of th'unworthy takes,\nWhen he himself might his quietus make\nWith a bare bodkin? Who would fardels bear,\nTo grunt and sweat under a weary life,\nBut that the dread of something after death,\nThe undiscovere'd country, from whose bourn\nNo traveller returns, puzzles the will,\nAnd makes us rather bear those ills we have\nThan fly to others that we know not of?\nThus conscience doth make cowards of us all,\nAnd thus the native hue of resolution\nIs sicklied o'er with the pale cast of thought,\nAnd enterprises of great pith and moment\nWith this regard their currents turn awry\nAnd lose the name of action.\n"
  16.  
  17.   TextArea1.Text = sText
  18.  
  19.  
  20. Public Sub LineNos()
  21.  
  22.   For iLoop As Short = 1 To 500
  23.     TextAreaLineNos.Text &= Str(iLoop) & gb.NewLine
  24.   Next
  25.  
  26.  

<IMG src="https://www.cogier.com/gambas/LineNumbers.png"> </IMG>

EDIT: -
You can also use the built in Text Editor gb.form.editor
Online now: Yes Back to the top

Post

Posted
Rating:
#3
Regular
sergioabreu is in the usergroup ‘Regular’
 I am doing something similar as it too.
Another textarea with the same font

Thanks anyway.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
use gb.form.editor

TextEditor.class is what the gambas IDE uses and has many of the same features.

TextEditor1.ShowLineNumbers = True

/comp/gb.form.editor/texteditor - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
sergioabreu is in the usergroup ‘Regular’
 Wow that's amazing!! thanks
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
sergioabreu is in the usergroup ‘Regular’

BruceSteers said

use gb.form.editor

TextEditor.class is what the gambas IDE uses and has many of the same features.

TextEditor1.ShowLineNumbers = True

/comp/gb.form.editor/texteditor - Gambas Documentation

One doubt:
Textarea had .pos that gives like a "String.Left" position of the  entire text  
What is the equivalent in TextEditor class?
I found only: Line, Column, Current (that returns a line) but I didn't find an equivalent to .pos yet

Can you help?
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
It does not have a Pos property like Textarea, it has ToPos that converts line/column to a pos.

I make my own Pos Property like this…

Save the following text as a file called TextEditor.class in your project source folder then you should then have TextEditor.Pos property.

Code (gambas)

  1. ' Gambas class file  (TextEditor.class overrides)
  2.  
  3.  
  4.  
  5.  
  6.   Return Me.ToPos(Me.Line, Me.Column)
  7.  
  8.  
  9. Private Sub Pos_Write(Value As Integer)
  10.  
  11.   Dim iPos, iLine As Integer
  12.   While iPos < Me._GetDocument().Count - 1
  13.     iPos += Me._GetDocument().Lines[iLine].Len
  14.     If iPos > Value Then Break
  15.     Inc iPos
  16.     Inc iLine
  17.   Wend
  18.  
  19. Me.Goto(iLine, iPos - Value)
  20.  
  21.  
  22.  

PS. probably the Pos_Write method could be better.
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
sergioabreu is in the usergroup ‘Regular’
Great
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
sergioabreu is in the usergroup ‘Regular’

BruceSteers said

It does not have a Pos property like Textarea, it has ToPos that converts line/column to a pos.

I make my own Pos Property like this…

Save the following text as a file called TextEditor.class in your project source folder then you should then have TextEditor.Pos property.

Code (gambas)

  1. ' Gambas class file  (TextEditor.class overrides)
  2.  
  3.  
  4.  
  5.  
  6.   Return Me.ToPos(Me.Line, Me.Column)
  7.  
  8.  
  9. Private Sub Pos_Write(Value As Integer)
  10.  
  11.   Dim iPos, iLine As Integer
  12.   While iPos < Me._GetDocument().Count - 1
  13.     iPos += Me._GetDocument().Lines[iLine].Len
  14.     If iPos > Value Then Break
  15.     Inc iPos
  16.     Inc iLine
  17.   Wend
  18.  
  19. Me.Goto(iLine, iPos - Value)
  20.  
  21.  
  22.  

PS. probably the Pos_Write method could be better.

Topos seems to be a Graphical position (pixels on screen) Is'nt it?
TextEditor.ToPos (gb.form.editor)
Function ToPos ( [ Line As Integer, Column As Integer ] ) As Point
Return the relative position in pixels of a specific cursor position.
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
sergioabreu is in the usergroup ‘Regular’
If I ran:

 

Code

Texto.topos(line, col)

it gives me [x y]

and I am looking for String.Len( of current position)
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’

sergioabreu said

If I ran:

 

Code

Texto.topos(line, col)

it gives me [x y]

and I am looking for String.Len( of current position)

oops yep , I hadn't even realized ToPos gives a pixel position not a character position !
(I copied that code from my editor program, and it's wrong lol, cheers for spotting the bug)

Now I'm using this….

Code (gambas)

  1.  
  2.   Dim iPos As Integer
  3.  
  4.   If Me.Line Then  ' if not on Line[0] then add the length of each previous line (+1 for the LF).
  5.     For c As Integer = 0 To Me.Line - 1
  6.       iPos += Me[c].Length + 1
  7.     Next
  8.  
  9.   iPos += Me.Column  ' add current lines cursor position
  10.  
  11.   Return iPos
  12.  
  13.  
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
sergioabreu is in the usergroup ‘Regular’
 Fine

I thank you for showing me how to extend a class in gambas. It opens a universe lol
Online now: No Back to the top
1 guest and 0 members have just viewed this.