A Way to Update Style Templates After Changes?

General TRichView support forum. Please post your questions here
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: A Way to Update Style Templates After Changes?

Post by Sergey Tkachenko »

After thinking more about it, I am now afraid that this design is not possible.

Shared collections of TextStyles and ParaStyles are not compatible with using StyleTemplates.

When you modify a collection of StyleTemplates, changes must be applied:
1. To the active editor
2. To other opened editors
3. To previously saved documents.

(1) is not a problem, it is performed by default.
(3) is implemented only for "shared StyleTemplates" option ("Shared StyleTemplates" means that only StyleTemplates are shared, TextStyles and ParaStyles are not shared!). "Shared StyleTemplates" are implemented for RVF, I'll implement them for RichViewXML in the next update.
(3) is not implemented, but can be implemented easily. I'll do it in the next update, but only for option of "shared StyleTemplates").

So the only option for you is waiting for the next update and implement "shared StyleTemplates": sharing StyleTemplates but not sharing TextStyles and ParaStyles. From the user point of view, there will be no difference from your original design.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: A Way to Update Style Templates After Changes?

Post by edwinyzh »

Great! Thank you Sergey.

In the meantime, since currently I haven't implemented the document tabs yet (I load only one document at a time), maybe I should use styles merging when loading different documents into the same TRichViewEdit object?
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: A Way to Update Style Templates After Changes?

Post by Sergey Tkachenko »

"Style merging" is the default option for insertion in document, so it is recommended.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: A Way to Update Style Templates After Changes?

Post by edwinyzh »

Sergey Tkachenko wrote: Thu Jun 16, 2022 11:51 am After thinking more about it...So the only option for you is waiting for the next update and implement "shared StyleTemplates": sharing StyleTemplates but not sharing TextStyles and ParaStyles. From the user point of view, there will be no difference from your original design.
Also after thinking more about it more on my side, it turned out that, actually I want to share both the StyleTemplates and TextStyles (not sure about ParaStyles, can you comment after reading my requirement below?).

Because, you can imagine my program - write multiple child-documents in multiple editors, and at the end merge them into the final document. So obviously each child-document must share the same Heading 1...9 styles, footnote style, and so on.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: A Way to Update Style Templates After Changes?

Post by Sergey Tkachenko »

But you do not need to share TextStyles for this task! It's enough to share StyleTemplates, i.e. real named styles like "Heading 1", etc.
When used to contain arbitrary formatting, items in TextStyles do not have specific meaning, and sharing them does not give any advantage.

Shared StyleTemplates allow making all small documents consistent.
Shared TextStyles (if they are used to represent arbitrary text formatting) do not help here.

For example.
If the user creates a StyleTemplate named "Keywords" and applies it to all keywords in all documents, they will be identical, and the user can change all of them if he/shy modifies properties of this StyleTemplate.
However, if the user uses direct text formatting for keywords instead (bold in one document, italic in another one, red color in the third one), nothing will help to make them consistent, even if these bold/italic/red TextStyles are stored in the same collection of TextStyles.

PS: actually, for the purpose of building a single document consisting of smaller documents, shared StyleTemplates are not 100% necessary. With the default StyleTemplateInsertMode = rvstimUseTargetFormatting, styles of all these small documents will be converted to the style of the main document, so formatting of all parts in result will be identical (of course, if they were made in a smart way, using named styles (StyleTemplates), rather than changing text attributes directly).
But of course, while shared StyleTemplates are not needed to create a good resulting document, they will be useful to keep all these small documents consistent while editing.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: A Way to Update Style Templates After Changes?

Post by edwinyzh »

Thanks Sergey, now I have a better understanding of the problem I'm trying to solve!

May I extend this question a little bit? It's about my understanding of the difference of TextStyles and ParaStyles.

Say I have a bunch of small documents, now I want to generate a combined document. In the target document the headings are generated.

Now my method is like: Take the "Heading 1" StyleTemplate and generate "Heading 1" TextStyle, the method is based on MakeTextStyle you provided here, except that I also set the `StyleName` property so that it can be identified.

And the result is Some heading styles are missing after saved as .docx. Which looks good, but also has a little issue I described there and you've reviewed.

My question, take "Heading 1" as an example, should I generate a "Heading 1" ParaStyle instead of TextStyle? And this maybe the source of the problem of the generated .docx file?

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

Re: A Way to Update Style Templates After Changes?

Post by Sergey Tkachenko »

Actually, TextStyles[].StyleName and ParaStyles[].StyleName do not make much sense when TextStyles and ParaStyles are used to represent arbitrary formatting, and you can simply ignore them (and may be it's better to keep them empty, to prevent saving unnecessary information in XML/RVF file).

When you apply some formatting (for example, make text italic) to a text formatted by some text style, a new text style may be created basing on existing text style, and this new style inherits all unmodified properties of the existing style, including its StyleName. So, when styles are added on editing operations, StyleName cannot be used to identify TextStyles and ParaStyles. If you use StyleTemplates, only names of these style templates make sense.

How to generate a paragraph formatted with "Heading 1"?
1) Create a paragraph style linked to this StyleTemplate.
2) Create a text style that is linked to "Heading 1" as a paragraph StyleTemplate, not as a text StyleTemplate. When exported to MS Word, this text will not be linked to a named style, but will inherit properties of "Heading 1" applied to its paragraph.

Code: Select all

var
  ParaNo, StyleNo: Integer;
  HeadingStyleTemplate: TRVStyleTemplate;

...

HeadingStyleTemplate := RVStyle1.StyleTemplates.FindItemByName('heading 1');
ParaNo := MakeParaStyle(RVStyle1, HeadingStyleTemplate);
StyleNo := MakeTextStyle(RVStyle1, nil, HeadingStyleTemplate);
RichViewEdit1.AddNL('This is Heading 1', StyleNo, ParaNo);
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: A Way to Update Style Templates After Changes?

Post by edwinyzh »

Great! Now the exported .docx file has all the Heading 1...3 properly shown in its Styles pane.

But one thing puzzles me - nil is provided for the `ParaStyleTemplate` parameter of MakeTextStyle, however, `StyleTemplate.ApplyToTextStyle()` will always be called inside that procedure, without nil-checking. But it works without raising any error. Not sure why...

The second questions is, after executing MakeParaStyle and MakeTextStyle, the StyleNames of the generated ParaStyle/TextStyle are 'Paragraph Style' and 'Font Style' respectively. Since above you said we'd better leaving these values empty. Should I reset those default StyleNames?

Thanks again!
Post Reply