Build a text editor

For everything that's not in any way related to PureBasic. General chat etc...
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Build a text editor

Post by Olli »

Hello, this subject is created to talk about text editing in PureBasic.

If, certainly, some other projects exist, no problem to add links.

For my part, I want to talk about a homemade text editor from zero.

1. First simulation
2. First result
3. Second simulation
Last edited by Olli on Sun Jul 06, 2025 3:54 pm, edited 6 times in total.
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

1. Simulate the main behaviour of a text editor.

This first source code simulates the main behaviour of a text editor :
a) white background (lightly broken white)
b) a set of colored areas symbolizing the several text lines, whatever the size.
c) allowing the user to scroll through this geometry. Here, I set the mouse wheel to scroll vertically.

Code: Select all

Global DskW, DskH
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DskW = DesktopWidth(0)
DskH = DesktopHeight(0)
OpenScreen(DskW, DskH, 32, "")
Global X, Y
Global Dim text.s(255)
Global Dim textX.i(255)
Global Dim textY.i(255)
Global Dim textW.i(255)
Global Dim textH.i(255)
Global Dim textC.i(255)
Global Dim textA.i(255)
For i = 0 To 255
  textX(i) = X
  textY(i) = Y
  textW(i) = Random(DskW - 1)
  textH(i) = 16 + Random(Random(DskH / 2) )
  textC(i) = RGB(Random(127), Random(127), Random(127) )
  textA(i) = Random(Random(2) )
  If textA(i) = 1
    textX(i) = (DskW - textW(i) ) / 2
  EndIf
  If textA(i) = 2
    textX(i) = DskW - textW(i)
  EndIf
  Y + textH(i)
Next
minY = 0
maxY = DskH - 1
Repeat
  Delay(15)
  ExamineKeyboard()
  ExamineMouse()
  ClearScreen(RGB(170, 170, 170) )
  StartDrawing(ScreenOutput() )
  j = -1
  For i = 0 To 255
    tX = textX(i)
    tY = textY(i) + DY
    tW = textW(i)
    tH = textH(i)
    If Bool(tY + tH > minY) Or Bool(tY < maxY)
      j = i
      Break
    EndIf
  Next
  If j >=0
    Repeat
      Box(tX, tY, tW, tH, textC(j) )
      j + 1
      If j > 255
        Break
      EndIf
    tX = textX(j)
    tY = textY(j) + DY
    tW = textW(j)
    tH = textH(j)
    Until Bool(tY > maxY)
  EndIf
  StopDrawing()
  FlipBuffers()
  DY + MouseWheel() << 4
Until KeyboardPushed(#PB_Key_All) Or MouseButton(#PB_MouseButton_Right)
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

2. First result

It is a little bit too slow, the mouse wheel scrolling.

Code: Select all

DY + MouseWheel() << 4
This could be replaced with :

Code: Select all

DY + (MouseWheel() * MouseWheel() * Sign(MouseWheel() ) ) << 4
This allows us to get a scrolling like this :

Code: Select all

wheel scroll
step  step
0     0
1     1
2     4
3     9
A second result, we can see, is that text lines are not represented line after line. This will be cause a 2nd version of short source code below.
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

3. Second simulation source code

Code: Select all

Procedure Box2(X, Y, W, H, C, Height = 16)
  i = 0
  While i + Height =< H
    shift ! 8
    Box(X, Y + i, W, Height, RGB((Red(C) + shift) & 127, (Green(C) + shift) & 127, (Blue(C) + shift) & 127) )
    i + Height
  Wend
EndProcedure
Global DskW, DskH
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DskW = DesktopWidth(0)
DskH = DesktopHeight(0)
OpenScreen(DskW, DskH, 32, "")
Global X, Y
Global Dim text.s(255)
Global Dim textX.i(255)
Global Dim textY.i(255)
Global Dim textW.i(255)
Global Dim textH.i(255)
Global Dim textC.i(255)
Global Dim textA.i(255) ; (A)lignment
Global Dim textS.i(255) ; text (S)ize (Character height)
For i = 0 To 255
  textX(i) = X
  textY(i) = Y
  textW(i) = Random(DskW - 1)
  textS(i) = 16 + 4 * Random(15)
  textH(i) = textS(i) * (1 + Random(DskH / 2 / textS(i) ) )                        ; (DskH / 2 / textS(i) )
  textC(i) = RGB(Random(127), Random(127), Random(127) )
  textA(i) = Random(Random(2) )
  If textA(i) = 1
    textX(i) = (DskW - textW(i) ) / 2
  EndIf
  If textA(i) = 2
    textX(i) = DskW - textW(i)
  EndIf
  Y + textH(i)
Next
minY = 0
maxY = DskH - 1
Repeat
  Delay(15)
  ExamineKeyboard()
  ExamineMouse()
  ClearScreen(RGB(170, 170, 170) )
  StartDrawing(ScreenOutput() )
  j = -1
  For i = 0 To 255
    tX = textX(i)
    tY = textY(i) + DY
    tW = textW(i)
    tH = textH(i)
    If Bool(tY + tH > minY) Or Bool(tY < maxY)
      j = i
      Break
    EndIf
  Next
  If j >=0
    Repeat
      Box2(tX, tY, tW, tH, textC(j), textS(j) )
      j + 1
      If j > 255
        Break
      EndIf
    tX = textX(j)
    tY = textY(j) + DY
    tW = textW(j)
    tH = textH(j)
    Until Bool(tY > maxY)
  EndIf
  StopDrawing()
  FlipBuffers()
  DY + (MouseWheel() * MouseWheel() * Sign(MouseWheel() ) ) << 4
Until KeyboardPushed(#PB_Key_All) Or MouseButton(#PB_MouseButton_Right)
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

4. Second result

Now, we can see how each text line is represented, whatever the character height, of these ones.

Then, 3 things :
a) prepare the source code to be grown;
b) scrolling, yes, but depending of the last displayed line also.
c) what about a easy pages reference ?
d) we will define a convention :

Code: Select all

4-bits floating point numbers

id description
0  0
1  1/6
2  1/5
3  1/4
4  1/3
5  2/5
6  1/2
7  3/5
8  2/3
9  3/4
A  4/5
B  5/6
C  1
D  reserved
E  reserved
F  reserved
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Build a text editor

Post by Thorium »

A text editor from scratch is actually a side project i work on currently.

It's specifically for coding and even more specifically for G-Code with Sinumerik extension. As many editors struggle with this.
First i wanted to use Scintilla but it was kind of confusing to me and i thought i could write my own editor gadget in the time i understand Scintilla.
Well it is much more complicated than anticipated. There is so much you need to consider.

Since this is for coding i use a linked list, each text line is a element in the list. This makes it easy and fast to insert and delete lines. And also only process the line a change happens for syntax highlighting etc.

I have almost all basic functions done including syntax highlighting, undo/redo and search/replace. Still some missing.
I could share the code, if you are interessted but it's very much a "work in progress". Even some design decissions could change.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Build a text editor

Post by Thorium »

Here is a screenshot of it.
As you can see i took inspiration from PureBasic for the coloring, etc. But this is not Scintilla, it's made from scratch with PB 2D drawing. And lots of code to handle all basic editing functions.

Image
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Build a text editor

Post by Rinzwind »

I would be interested to see where and how to start with building an editor without external editor building blocks. Agree that Scintilla is complex and has buildin limitations too.
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Build a text editor

Post by HeX0R »

Do you know PBEdit from Mr.L?
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Build a text editor

Post by Thorium »

HeX0R wrote: Sat Jul 12, 2025 8:57 am Do you know PBEdit from Mr.L?
I didn't knew that. Could have saved me a ton of work. :D
Axolotl
Enthusiast
Enthusiast
Posts: 797
Joined: Wed Dec 31, 2008 3:36 pm

Re: Build a text editor

Post by Axolotl »

I remember this but have never used it.
[Module] EditorEx (Custom Editor Gadget)
Thorsten1867 did a huge bunch of modules.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Enthusiast
Enthusiast
Posts: 797
Joined: Wed Dec 31, 2008 3:36 pm

Re: Build a text editor

Post by Axolotl »

.....
If, certainly, some other projects exist, no problem to add links
......
(Very) old and windows only ....
RichEdit Functions (OOP) New: with Image-Support from ts-soft.
Important for the usage: Correcting an incompatible library change:

Code: Select all

ProcedureDLL.i New_RichEdit(x.l, y.l, w.l, h.l)
; ..... 
      \ID = EditorGadget(#PB_Any, x, y, w, h)
      \hWnd = GadgetID(\ID)
; don't forget to insert this line here 
      SendMessage_(\hWnd, #EM_SETTEXTMODE, #TM_RICHTEXT, 0)
; ..... 
Or look at a fork on github RichEddie
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

Hello my friends. 1st, I am sorry to pause, like a big philosoph : I just have a work which requires lots of time. This gives (sometimes strange) pauses, we could name "freezes" !

I can see links you suggest, and I think I will copy them to the head message. And if other projects are being made, do not see a competition from my part. 1st reason is I am unable to hold certainly a computing project actually. I can just add step by step the answers I find by testing to technical questions.

A technical question is about the easier ways we have :
- a rich text support on Microsoft
- a pdf support on Linux

This let us imagine a byte code dedicated to rich text.

Note that I test on full screen. I think full screen is a good way to represent a scheme, or any schemes.

We could also note that we have this :

Code: Select all

StartDrawing(anything_output() )
And this part of code cannot be easily set. StartDrawing() requires a drawing output function only as argument.

But this does not prevent us to bring a small project which would be able on two versions :
- full (or windowed) screen => sprites (or textures on Ogre3D)
- OS Gui => images (maybe canvas but... to be continued)

What it paused me far above is the zoom aspect : smooth or not smooth ? Deeper question : floating point numbers or integers ?
That is the reason I wrote a strange convention above : an array of 16 entries which define several values between 0.0 and 1.0.

So, if you have coding experience, your point of view about the choice of the number type interests me. Because it seems I will jump floating points, so if you have advices not to be stopped by a conceptual wall, I take it !

I thank you Thorium for the screenshoot picture. If you have a link (forum page or not), I will copy it to the head message.

Now, I will share a math question I discovered, when I wanted to create a dividing convention. It is a off topic parenthesis I will write in a new subject before going back here...
Axolotl
Enthusiast
Enthusiast
Posts: 797
Joined: Wed Dec 31, 2008 3:36 pm

Re: Build a text editor

Post by Axolotl »

I would also include markdown among the various platform-independent text formats. This can also be found in countless apps, wikis, websites, etc. In terms of syntax, however, compatibility is (apparently) interpreted rather generously here.
A nice implementation by Thorsten1867 (which I never used) is this:
[Module] MarkDown Gadget (all OS)

Maybe you can tell us why you're doing this.
What should the text editor be able to do at the end of the day?
At the moment, I see it as your further education/training project.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Olli
Addict
Addict
Posts: 1194
Joined: Wed May 27, 2020 12:26 pm

Re: Build a text editor

Post by Olli »

Axolotl wrote:Maybe you can tell us why you're doing this.
1) To cross several methods of work.
2) To receive critical feedback.
3) To push myself to go forward in a pseudo-project.
4) Because I need to code and use an text editor.
5) Because I created several pieces of text editing.

At this end of this day ? Ow! You are pushing the grand-ma rolling chair into the nettles !! :D

Next step : an other simulation with more details than the previous simulation.
Post Reply