Page 1 of 1

Writing a text editor

Posted: Fri Mar 01, 2013 2:40 pm
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

Re: Writing a text editor

Posted: Fri Mar 01, 2013 3:53 pm
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).

Re: Writing a text editor

Posted: Fri Mar 01, 2013 6:58 pm
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.

Re: Writing a text editor

Posted: Fri Mar 01, 2013 9:46 pm
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...

Re: Writing a text editor

Posted: Fri Mar 01, 2013 10:48 pm
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.

Re: Writing a text editor

Posted: Fri Mar 01, 2013 10:58 pm
by Steve Elliott
Thanks! Looks interesting.