Recent

Author Topic: RichMemo - open mytext.txt pls!  (Read 2545 times)

Nicole

  • Hero Member
  • *****
  • Posts: 970
RichMemo - open mytext.txt pls!
« on: July 19, 2022, 04:39:12 pm »
I made an edit interface for rtf files, works fine.
I use an open dialog, which opens rtf-files fine.
What do I have to change, that my txt and ini files (or may be all others as well) are displayed too?

And to want it all:
Best would be a size check and an optional request as, 'do you really want to open the 10 GB-file in your text-editor'?

Thanks for hints.

My open dialog looks like this:

Code: Pascal  [Select][+][-]
  1. object OpenDialog_RichMemo1: TOpenDialog
  2.   DefaultExt = '.rtf'
  3.   InitialDir = 'c:\'
  4.   Left = 552
  5.   Top = 416
  6. end

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #1 on: July 19, 2022, 04:57:50 pm »
What do I have to change, that my txt and ini files (or may be all others as well) are displayed too?
You can put this in TOpenDialog.Filter
My special files (rtf,txt,ini)|*.RTF;*.TXT;*.INI

(Everything before the | is a description, everything after is a list of *.ext separated by ; )

Best would be a size check and an optional request as, 'do you really want to open the 10 GB-file in your text-editor'?

Not sure if that's possible.
You can always check the size after selecting it and asking "Do you really want to open a xx-GB file" directly after.
« Last Edit: July 19, 2022, 05:00:39 pm by rvk »

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: RichMemo - open mytext.txt pls!
« Reply #2 on: July 19, 2022, 05:07:58 pm »
thank you!
I will do this.
And thanks for the hint about the pipe-symbol.

Is it possible to let the user open ALL files, if he wants to?
May be on a second click as "warning"?
There is software allowing *.*, but most times it does not work well and needs some unlogic clicks.
« Last Edit: July 19, 2022, 05:10:02 pm by Nicole »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #3 on: July 19, 2022, 05:18:29 pm »
Is it possible to let the user open ALL files, if he wants to?
May be on a second click as "warning"?
There is software allowing *.*, but most times it does not work well and needs some unlogic clicks.
You can expand the pipe sign with other options.
For example:
My special files|*.RTF;*.TXT;*.INI|Other files|*.*

This will give you initially the "My special files" and the user can choose "Other files" for *.* from the dropdown.

You can give even more than 2 options.
My special files|*.RTF;*.TXT;*.INI|MeMe files|*.MEME|Other files|*.*

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: RichMemo - open mytext.txt pls!
« Reply #4 on: July 19, 2022, 05:22:27 pm »
Thanks.

A different problem occurs:
The text-file is not displayed.
So the mytext.rtf shows content, the mytext.txt shows nothing but a blanc side.

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: RichMemo - open mytext.txt pls!
« Reply #5 on: July 19, 2022, 05:27:23 pm »
Is it possible to let the user open ALL files, if he wants to?
May be on a second click as "warning"?

You can use Filter. You can read the documentation of TOpenDialog's parent component TFileDialog.Filter
https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/tfiledialog.html

About the warning, you can show a ok/cancel query box before loading the file if the chosen file's extension is not what you think it should be.

For example on how to use filter, you can see the attached image, which I used for my glSlideShow.

For mytext.txt issue, start a test project and try opening the file directly using the RichMemo component or maybe TMemo. If it fails, maybe that file has non-standard character or maybe BOM issue.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #6 on: July 19, 2022, 05:33:24 pm »
Thanks.

A different problem occurs:
The text-file is not displayed.
So the mytext.rtf shows content, the mytext.txt shows nothing but a blanc side.
Are you trying to load the txt in the same component as the rtf?

Not sure what happens if you try to load txt in trichmemo but for a txt you should use tmemo (without any markup options).

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: RichMemo - open mytext.txt pls!
« Reply #7 on: July 19, 2022, 05:47:48 pm »
Code: Text  [Select][+][-]
  1. Are you trying to load the txt in the same component as the rtf?

Äh...... yes, I though this should work: What is able to read even rtf, could read txt as well (pls do not comment, thanks).

What happens? - Nothing.
This was the reason, why I posted it.

Would you recommend to give up this idea?
To have one quick editor for all "small formats" seemed nice to me (I have a lot of notes, kind of post-its).
Would this need to convert somehow to strings stringLists text and all this stuff or even Streams?

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #8 on: July 19, 2022, 05:55:33 pm »
Just at the top of my head (on mobile now)
You could try
TRichMemo.Lines.LoadFromFile(Filename)

It should load the file as plain text.
But make sure to also do
TRichMemo.Lines.SaveToFile(Filename)
And not the other RTF save function.
(And you will lose any style changes you make because txt doesn't support that)

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: RichMemo - open mytext.txt pls!
« Reply #9 on: July 19, 2022, 07:12:08 pm »
Code: Text  [Select][+][-]
  1. TRichMemo.Lines.LoadFromFile(Filename.txt)
This is what I tried this very moment. Does not work.

And I tried this as well
Code: Pascal  [Select][+][-]
  1.   LoadRTFFile(RichMemo1, file.txt);

Does not work. The field for the text stays blanc.
The above line works fine for file.rft

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #10 on: July 19, 2022, 07:22:17 pm »
Code: Text  [Select][+][-]
  1. TRichMemo.Lines.LoadFromFile(Filename.txt)
This is what I tried this very moment. Does not work.

This works fine for me in TRichMemo.
Code: Pascal  [Select][+][-]
  1.   RichMemo1.Lines.LoadFromFile('c:\temp\test.txt');
(even with a BOM-sig inside the txt)

Could you test it in an empty project with just one TRichMemo and TButton and this in the Button-click event:
(and make sure c:\temp\test.txt exists and is not empty)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   RichMemo1.Lines.LoadFromFile('c:\temp\test.txt');
  4. end;

Or print out your filename (Showmessage(OpenDialog1.Filename) before LoadFromFile to make sure it is as expected.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: RichMemo - open mytext.txt pls!
« Reply #11 on: July 19, 2022, 07:25:59 pm »
Hi!

A lot of components consider the suffix for the file type.

I don't know if RichMemo does.

But anyway: the suffix for RichText is rtf.
To stay compatible with Libre Office and friends.

So do

Code: Pascal  [Select][+][-]
  1. TRichMemo.Lines.LoadFromFile('MyFile.rtf');

Winni

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #12 on: July 19, 2022, 07:31:55 pm »
So do
Code: Pascal  [Select][+][-]
  1. TRichMemo.Lines.LoadFromFile('MyFile.rtf');
Nicole is trying to load a .txt (plain text) into a TRichMemo.
An rtf already works.

Anyway, we also already established in another topic that the TRichMemo.Lines.LoadFromFile doesn't work for rtf (like it does for TRichEdit in Delphi). You need to use a separate function to load from file (or use TRichMemo.LoadFromStream, unfortunately there is no TRichMemo.LoadFromFile).

For me for plain text this does work
Code: Pascal  [Select][+][-]
  1. TRichMemo.Lines.LoadFromFile('MyFile.txt');

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: RichMemo - open mytext.txt pls!
« Reply #13 on: July 20, 2022, 04:49:08 pm »
Too many undone things, sorry for not posting earlier.
Probably it works here as well.

Because:
I only THOUGHT I would have done it with lines.LoadFromFile.
I erred.
I did it with streams.
I will make an alternate open-methode for the txt case. Thx for the hint!

I tried this as well, opens rtfs fine, but does not display txt

Code: Pascal  [Select][+][-]
  1.   LoadRTFFile(RichMemo_Notizen, OpenDialog_RichMemoNotizen.FileName);

This is my open-routine, which works fine, but does not display txt (just rtfs)

Code: Pascal  [Select][+][-]
  1. procedure TForm_Notizen.ToolButton_oeffneClick(Sender: TObject);
  2. var
  3.   fs : TFileStream;
  4. begin
  5.   if OpenDialog_RichMemoNotizen.Execute then begin
  6.     fs := nil;
  7.     try
  8.       // Utf8ToAnsi is required for windows
  9.       fs := TFileStream.Create(Utf8ToAnsi(OpenDialog_RichMemoNotizen.FileName), fmOpenRead or fmShareDenyNone);
  10.       RichMemo_Notizen.LoadRichText(fs);
  11.       Saved_dasRichMemo:=True; // since we opened a saved file
  12.       Filename:=ExtractFileName(OpenDialog_RichMemoNotizen.FileName);
  13.       Pfad_Texte:=ExtractFilePath(OpenDialog_RichMemoNotizen.FileName);
  14.       Caption:=Filename;
  15.     except
  16.     end;
  17.     fs.Free;
  18.   end;
  19. end;

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: RichMemo - open mytext.txt pls!
« Reply #14 on: July 21, 2022, 12:30:27 am »
I tried this as well, opens rtfs fine, but does not display txt
Code: Pascal  [Select][+][-]
  1.   LoadRTFFile(RichMemo_Notizen, OpenDialog_RichMemoNotizen.FileName);
This is my open-routine, which works fine, but does not display txt (just rtfs)
You still can use LoadRTFFile when you are dealing with .rtf extention.
It's just that you need to use TRichMemo.Lines.LoadFromFile when dealing with a .txt or .ini.

This code works for me for .rtf, .ini AND .txt all at one.
(notice the if ExtractFileExt() = '.rtf' so that you can use LoadRTFFile for .rtf and Lines.LoadFromFile otherwise)

Code: Pascal  [Select][+][-]
  1. uses RichMemoUtils;
  2.  
  3. procedure TForm_Notizen.ToolButton_oeffneClick(Sender: TObject);
  4. begin
  5.   if OpenDialog_RichMemoNotizen.Execute then
  6.   begin
  7.     if lowercase(ExtractFileExt(OpenDialog_RichMemoNotizen.FileName)) = '.rtf' then
  8.     begin
  9.       LoadRTFFile(RichMemo_Notizen, OpenDialog_RichMemoNotizen.FileName);
  10.     end
  11.     else
  12.     begin
  13.       // load txt or ini via TStringList Lines property
  14.       RichMemo_Notizen.Lines.LoadFromFile(OpenDialog_RichMemoNotizen.FileName);
  15.  
  16.       // maybe disable any style-buttons here to make sure .txt doesn't include them
  17.       // ...
  18.  
  19.     end;
  20.     Saved_dasRichMemo := True; // since we opened a saved file
  21.     FullFilename := OpenDialog_RichMemoNotizen.FileName;
  22.     Filename := ExtractFileName(OpenDialog_RichMemoNotizen.FileName);
  23.     Pfad_Texte := ExtractFilePath(OpenDialog_RichMemoNotizen.FileName);
  24.     Caption := Filename;
  25.   end;
  26. end;

For .txt and .ini you would ideally disable any style buttons to make sure you can't set bold/italic etc. because if you want to save as .txt or .ini again, those style changes will be lost.

For saving you would do something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Notizen.ToolButton_saveClick(Sender: TObject);
  2. begin
  3.   if FullFilename = '' then exit;
  4.   if lowercase(ExtractFileExt(FullFilename)) = '.rtf' then
  5.   begin
  6.     SaveRTFFile(RichMemo_Notizen, FullFilename);
  7.   end
  8.   else
  9.   begin
  10.     // save txt or ini via TStringList Lines property
  11.     RichMemo_Notizen.Lines.SaveToFile(FullFilename);
  12.  
  13.     // enable any style-buttons again
  14.     // ...
  15.  
  16.   end;
  17.   Saved_dasRichMemo := false;
  18.   FullFilename := '';
  19.   Filename := '';
  20.   Pfad_Texte := '';
  21.   Caption := 'something';
  22. end;

 

TinyPortal © 2005-2018