The 'error loading file' dialog that I fear

General TRichView support forum. Please post your questions here
Post Reply
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

The 'error loading file' dialog that I fear

Post by edwinyzh »

So I've just started integrating TRichView to my program.
I first tried persisting the documents in rvf format, until alter I found RichViewXml.

Either with rvf or XML format, from time to time, when I fiddling with the RvfOptions, like use template styles or not. Or changing properties like TRichViewXml, when reloading previously saved documents I got errors like:

Code: Select all

Error loading file.
Possible reasons:
- format of this file is not supported by this application;
- the file is corrupted;
- the file is opened and locked by another application.
Image

This makes fear that in the future, if I changing some of the options and previously saved document won't load...

Any good practice advises as to how to avoid this possibility?

Thanks.
Sergey Tkachenko
Site Admin
Posts: 17291
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: The 'error loading file' dialog that I fear

Post by Sergey Tkachenko »

Please send me this XML document,
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

@Sergey, I'm was fiddling the option and I didn't save the file that can be identified with the options that were used to save it. I'll send you one when I find it makes sense.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

Hi Sergey,

Just a note, the day before yesterday I sent a xml file that couldn't be reloaded back to TRichViewEdit to you and I hope it's not lost in the cyberspace due to spam filtering or whatever other reasons :)
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

OK, I've got some findings.

My TRichViewXml.SaveStyles = False, I set that to False because in this post you suggested that:
You can use the same TRVStyle and RichViewXML.

If RichViewXML.SaveStyles = False, properties of TRVStyle is not saved to XML, including collections of styles.
(but if it is True, all properties of TRVStyle are saved, unlike RVF).
Since my TRVStyle is saved/loaded separately using INI format because my program is about editing multiple smaller documents and merging them into a single document at the end, so I set TRichViewXml.SaveStyles to False.

As a result, RVXMLRoutines.LoadRVStyle won't load anything styles from the xml file but instead it populate `StyleMap: TRVIntegerList` with my TRVStyle object, which has only one entry ( I don't know why, I thought TRVStyle.SaveToIni should save all the styles of the document?).

And when reading the document content from the xml, it has xml tag like <utext textstyleno="1"...>. As a result, `ReadUnicodeText` will access `StyleMap: TRVIntegerList` like the following:

Code: Select all

    Item.StyleNo := StyleMap[Item.StyleNo];
    Item.ParaNo := ParaMap[Item.ParaNo];
And that's why the 'List index out of bounds (1).' error.

What should I do now? Thanks.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

I made the following changes to RVXMLRoutines.pas and now my document can be loaded! However, the previously bolded text become red background with white font color :lol:
Maybe I should use TRichViewXml.SaveStyles = True than?

Code: Select all

    // added `if` checks
    if Item.StyleNo < (StyleMap.Count) then
      Item.StyleNo := StyleMap[Item.StyleNo];
    if Item.ParaNo < (ParaMap.Count) then
      Item.ParaNo := ParaMap[Item.ParaNo];
I'll send you the modified .pas file via email.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

Just a thought - I think both Sergey and TRichViewEdit users should focus more on the XML format, because compared to rvf format, XML is human-readable!

The benefit is huge when you have an issue with the saved data and need to fix things or recover things.
jonjon
Posts: 445
Joined: Sat Aug 27, 2005 4:19 pm

Re: The 'error loading file' dialog that I fear

Post by jonjon »

edwinyzh wrote: Wed Jun 22, 2022 7:22 am Just a thought - I think both Sergey and TRichViewEdit users should focus more on the XML format, because compared to rvf format, XML is human-readable!
The benefit is huge when you have an issue with the saved data and need to fix things or recover things.
I agree! I've postponed using RV XML for a while because of some quirks and limitations, and the XML produced is not very user friendly. As an example, I don't like the use of "br" in this code:

Code: Select all

<text textstyle="Heading" parastyle="Heading">OnLoadControl</text>
<text textstyle="Subheading">Declaration:</text>
<text textstyle="Code">TRichViewXMLLoadControlEvent = </text>
<text textstyle="Keyword" br="0">function</text>
<text textstyle="Code" br="0"> (Sender: TRichViewXML; </text>
<text textstyle="Keyword" br="0">const</text>
<text textstyle="Code" br="0"> ClassName, ObjectData: </text>
<text textstyle="Keyword" br="0">string</text>
which could look more like XHTML:

Code: Select all

<p textstyle="Heading" parastyle="Heading">OnLoadControl</p>
<p textstyle="Subheading">Declaration:</p>
<p textstyle="Code">
	<text>TRichViewXMLLoadControlEvent =</text>
	<text textstyle="Keyword">function</text>
	<text textstyle="Code"> (Sender: TRichViewXML; </text>
	<text textstyle="Keyword">const</text>
	<text> ClassName, ObjectData: </text>
	<text textstyle="Keyword">string</text>
</p>
Sergey Tkachenko
Site Admin
Posts: 17291
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: The 'error loading file' dialog that I fear

Post by Sergey Tkachenko »

Sorry for the delay.

1. Yes, if you save XML/RVF without styles, you must provide that when you load, collections of styles contain all necessary styles.
You said that you saved styles using SaveINI.
I just tested, saved TRVStyle using SaveINI and loaded using LoadINI.
As I can see, all styles are re-loaded.
If you do not have all styles, most probably they are removed at some other place.

2. The main advantage of RVF - it is much faster to save and load, and it is smaller.
It is especially important when RVF is used to copy documents in memory (for example, it is essential for ScaleRichView and ReportWorkshop).
But I am agree, more human-readable format for saving files is useful.
But... I do not like RichViewXML. It was not created initially by me, I'd create it differently, both in code and in XML format.
And probably, it makes sense to implement it like DocX: not a single XML file, but a ZIP file containing documents as XML, images, etc.

3. It would be better if RichViewXML used properties for RVF rather than its own properties.
RVF has an option in RVFOptions: rvfoConvUnknownStylesToZero. If included, when too large indexes of styles are found, instead of reporting an error, TRichView converts them to 0.
I'll implement the same for RichViewXML
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

@jonjon,
Compared to the xml schema, I'm more concerned about the stability.

@Sergay,

For the sake of stability, do you suggest me to use xml or rvf at the moment? You know, we cal always switch file formats in the future, but at the moment, stability is more of a concern.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

Sergey Tkachenko wrote: Wed Jun 22, 2022 9:25 am You said that you saved styles using SaveINI.
I just tested, saved TRVStyle using SaveINI and loaded using LoadINI.
As I can see, all styles are re-loaded.
If you do not have all styles, most probably they are removed at some other place.
Oh, just tested and confirmed you are right! I use a customized `TrvActionNew` to initialize the editor (mainly for getting the standard style templates needed by the app), and `TrvActionNew` clears all my text styles! I don't know, my previous tests showed the action doesn't clear my existing text styles, I'll check it again.

On the other hand, maybe using a modified version of TrvActionNew for initializing the editor is not the best option?
Sergey Tkachenko
Site Admin
Posts: 17291
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: The 'error loading file' dialog that I fear

Post by Sergey Tkachenko »

TrvActionNew resets TextStyles and ParaStyles of the target editor, if the target editor's UseStyleTemplates = True.
In this case, TrvActionNew assigns its StyleTemplates to the target editor's StyleTemplates, clears its existing TextStyles and ParaStyles, and adds one text style and one paragraph style linked with "Normal" StyleTemplate.

If the target editor's UseStyleTemplates = False, TrvActionNew does not modify TextStyles and ParaStyles. More exactly, if RVAControlPanel.AutoDeleteUnusedStyles = True, it deletes styles unused in the target editor.

Well, if you plan to use shared TextStyles and ParaStyles, resetting styles by TrvActionNew is not good.
Also, if TextStyles and ParaStyles are not shared, but StyleTemplates are shared, assigning StyleTemplates to the target editor's StyleTemplates is not good as well.

In the next update, I'll add TrvActionNew.ApplyStyleTemplates property. By default, it will be equal to True, and the behavior will be the same.
But you will be able to assign False. In this mode, the action will not assign its StyleTemplates to StyleTemplates of the target editor, and will not reset TextStyles and ParaStyles. You should also assign RVAControlPanel.AutoDeleteUnusedStyles = False.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

You are right, I've abandoned TrvActionNew for the purpose of initializing the editor.
Sergey Tkachenko
Site Admin
Posts: 17291
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: The 'error loading file' dialog that I fear

Post by Sergey Tkachenko »

FYI, I am currently working on TRichViewXML.
I already implemented all features that we discussed:
- respecting RVFOption for saving only names of StyleTemplates in XML
- events on saving and loading pictures from external files
- detecting graphic format by content, even if graphic classes are not registered
- respecting RVFOption for converting invalid style indexes to 0
- respecting RVFOption for ignoring unknown graphic formats

I want to make an option to save TImageList items like in RVF (without pictures themselves), and I'll upload updated files on the website.
Maybe tomorrow.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: The 'error loading file' dialog that I fear

Post by edwinyzh »

Great job, Sergey! Thanks your efforts!
Post Reply