Recent

Author Topic: [Solved] RichMemo Cocoa, load/save richtext not working  (Read 1529 times)

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
[Solved] RichMemo Cocoa, load/save richtext not working
« on: February 11, 2019, 04:27:22 am »
I'm trying to load and save with a tmemorystream.

No errors, just nothing happening.

Anyone run into this?
« Last Edit: February 12, 2019, 07:16:18 pm by Trenatos »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2052
    • havefunsoft.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #1 on: February 11, 2019, 05:15:40 am »
Make sure that Handle is allocated at the time you try to load RTF.

please provide the RTF file as well (is possible).
or try to create another (simple?) rtf in TextEdit and see if it loads
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #2 on: February 11, 2019, 01:40:54 pm »
Here's an example of the problem.

When clicking the button, the richmemo is cleared but no text appears, and no errors are thrown.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, RichMemo;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     RichMemo1: TRichMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure RichMemo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   holder: TMemoryStream;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.RichMemo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  37. begin
  38.   RichMemo1.SaveRichText(holder);
  39. end;
  40.  
  41. procedure TForm1.FormCreate(Sender: TObject);
  42. begin
  43.   holder := TMemoryStream.Create;
  44. end;
  45.  
  46. procedure TForm1.Button1Click(Sender: TObject);
  47. begin
  48.   RichMemo1.Clear;
  49.   RichMemo1.LoadRichText(holder);
  50. end;
  51.  
  52. end.
  53.  
  54.  
« Last Edit: February 11, 2019, 02:36:13 pm by Trenatos »

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #3 on: February 12, 2019, 02:02:33 am »
Looking around a bit more, it looks like the issue might be with SaveRichText.

If I have a file with some content, created with textEdit and I use a TFileStream above, and set the button to do this:

Code: Pascal  [Select]
  1.   RichMemo1.SaveRichText(fholder);
  2.   RichMemo1.LoadRichText(fholder);
  3.  

It'll only show what I had previous saved.  Any changes to the RichMemo does not get saved to the file even though I use

Code: Pascal  [Select]
  1. TFileStream.Create('/Users/user/Documents/richfiletest.rtf', fmOpenReadWrite);

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #4 on: February 12, 2019, 02:24:12 am »
And a little more research.

If I have this

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fholder: TFileStream;
  4. begin
  5.   try
  6.     fholder := TFileStream.Create('/Users/user/Documents/richfiletest.rtf', fmCreate);
  7.     RichMemo1.SaveRichText(fholder);
  8.   finally
  9.     fholder.free;
  10.   end;
  11. end;
  12.  

I write some text in the richmemo, and click the button.

No errors reported, but when I try to open the file in Finder I get "The document “richfiletest” could not be opened."

And if I open that file in Sublime, it shows up as an empty file.

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #5 on: February 12, 2019, 02:38:08 am »
Pretty sure something's broken but not sure with what...

OK, if I have this:

Code: Pascal  [Select]
  1. procedure TForm1.RichMemo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   holder.seek(0, soBeginning);
  4.   RichMemo1.SaveRichText(holder);
  5. end;
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9.   holder := TMemoryStream.Create;
  10.   holder.LoadFromFile('/Users/user/Documents/richfiletest.rtf');
  11.   RichMemo1.LoadRichText(holder);
  12. end;
  13.  
  14. procedure TForm1.FormDestroy(Sender: TObject);
  15. begin
  16.   holder.free;
  17. end;
  18.  
  19. procedure TForm1.Button1Click(Sender: TObject);
  20. begin
  21.   RichMemo1.Clear;
  22.   holder.seek(0, soBeginning);
  23.   RichMemo1.LoadRichText(holder);
  24. end;
  25.  

It'll load and show the content of the file on form load.

Clicking the button will clear the richform and show the original text, no matter if I've typed into the box (Which should trigger the SaveRichText)

So it looks like loading works but saving is broken, possibly with FPC and not with richform?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2052
    • havefunsoft.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #6 on: February 12, 2019, 05:27:40 am »
So it looks like loading works but saving is broken, possibly with FPC and not with richform?
it's not really broken... it doesn't seem to be implemented for Cocoa :)

try r6815
« Last Edit: February 12, 2019, 05:43:37 am by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Trenatos

  • Sr. Member
  • ****
  • Posts: 493
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: RichMemo Cocoa, load/save richtext not working
« Reply #7 on: February 12, 2019, 07:16:01 pm »
That seems to be working!

Many thanks skalogryz  :)