Writing a text editor

Just starting out? Need help? Post your questions and find answers here.
Steve Elliott
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Jan 01, 2006 9:34 pm
Location: Wales, UK

Writing a text editor

Post by Steve Elliott »

I'm writing a text editor and I'd like some advice and or code on programming one.

I have it working ok, but notice corruption of the text - repeated text and gibberish after saving sometimes.
It would be nice to have certain words within the editor change color, is that easy to accomplish? And some dotted lines to indicate words in the same column.

Thanks in advance :D

Saving code:

Code: Select all

        Case #MenuItem_SaveAs
          
          StandardFile$ = "TestProg.txt"
          Pattern$ = "Text (*.txt)|*.txt|All files (*.*)|*.*"
          Pattern  = 0    ; use the first of the three possible patterns as standard
          
          File$ = OpenFileRequester("Choose file to save", StandardFile$, Pattern$, Pattern)
          
          If File$
            If OpenFile(0,"TestProg.txt")
              WriteString(0,GetGadgetText(Editor_0))
              CloseFile(0)
            EndIf
          EndIf
Loading code:

Code: Select all

        Case #MenuItem_Load
          
          StandardFile$ = "TestProg.txt"
          Pattern$ = "Text (*.txt)|*.txt|All files (*.*)|*.*"
          Pattern  = 0    ; use the first of the three possible patterns as standard
          
          File$ = OpenFileRequester("Choose file to load", StandardFile$, Pattern$, Pattern)
          
          If File$
            If ReadFile(0,"C:\Programming\TestProg.txt")
              *mem = AllocateMemory(Lof(0))
              ReadData(0, *mem, Lof(0))
              SetGadgetText(Editor_0, PeekS(*mem))
            EndIf
          EndIf
Last edited by Steve Elliott on Fri Mar 01, 2013 4:18 pm, edited 1 time in total.
Project S: loading...
Intel Core2 Duo E7200 @ 2.53Ghz, 3Gb RAM Intel G45 Express
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Writing a text editor

Post by IdeasVacuum »

Not sure - (notice that your file exts do not match)

When saving, instead of OpenFile, use CreateFile to replace the existing file with a new file of the same name, latest content. Specify the format in WriteString (ASCII/UTF8/Unicode).

When you Peek the string, be sure to specify the format (ASCII/UTF8/Unicode). If you know the length of the string, specify that too to be sure that you do not read beyond that length (= possible gibberish added to text).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Steve Elliott
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Jan 01, 2006 9:34 pm
Location: Wales, UK

Re: Writing a text editor

Post by Steve Elliott »

Thanks for the advice, so far so good. Using CreateFile and closing the file after loading into the editor GadgetText seems to have solved the problem.

I'll take a look at the docs to see if I can alter text colour of individual words, but if anybody knows the commands for this it would be appreciated.
Project S: loading...
Intel Core2 Duo E7200 @ 2.53Ghz, 3Gb RAM Intel G45 Express
Steve Elliott
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Jan 01, 2006 9:34 pm
Location: Wales, UK

Re: Writing a text editor

Post by Steve Elliott »

Hmm SetGadgetItemColor seems to be the most versatile, but for a text editor that's not gonna cut it. I guess some sneakiness is required. Any ideas are welcome...
Project S: loading...
Intel Core2 Duo E7200 @ 2.53Ghz, 3Gb RAM Intel G45 Express
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Writing a text editor

Post by davido »

Hi Steve,

TS-Soft has written an rtf include file. It looks very good.

Have a look at the url below:

http://www.purebasic.fr/english/viewtop ... &hilit=rtf

I hope it is useful to you.
DE AA EB
Steve Elliott
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Jan 01, 2006 9:34 pm
Location: Wales, UK

Re: Writing a text editor

Post by Steve Elliott »

Thanks! Looks interesting.
Project S: loading...
Intel Core2 Duo E7200 @ 2.53Ghz, 3Gb RAM Intel G45 Express
Post Reply