1 /************************************************************************/
2 /* */
3 /* Ted: Manipulation of TOC fields. */
4 /* */
5 /************************************************************************/
6
7 # include "tedConfig.h"
8
9 # include <stddef.h>
10 # include <stdio.h>
11
12 # include "tedEdit.h"
13 # include "tedDocFront.h"
14 # include "tedDocument.h"
15 # include <docRtfTrace.h>
16 # include "tedLayout.h"
17 # include <docField.h>
18 # include <docTreeType.h>
19 # include <docTreeNode.h>
20 # include <docRecalculateTocField.h>
21 # include <docEditCommand.h>
22
23 # include <appDebugon.h>
24
25 /************************************************************************/
26 /* */
27 /* Refresh a table of contents. */
28 /* */
29 /************************************************************************/
30
tedRefreshTocField(DocumentSelection * dsAroundToc,TedEditOperation * teo,DocumentField * dfToc)31 int tedRefreshTocField( DocumentSelection * dsAroundToc,
32 TedEditOperation * teo,
33 DocumentField * dfToc )
34 {
35 const LayoutContext * lc= &(teo->teoLayoutContext);
36 EditOperation * eo= &(teo->teoEo);
37
38 int headPart= -1;
39 int tailPart= -1;
40
41 int reachedBottom= 0;
42 DocumentSelection dsInsideToc;
43
44 if ( docRecalculateOneTocField( eo->eoDocument, dfToc ) )
45 { LDEB(1); return -1; }
46
47 if ( tedLayoutDocumentBody( &reachedBottom, &(teo->teoLayoutContext) ) )
48 { LDEB(1); return -1; }
49
50 {
51 int page= 0;
52 BufferDocument * bd= eo->eoDocument;
53 RecalculateFields rf;
54
55 docInitRecalculateFields( &rf );
56
57 rf.rfDocument= bd;
58 rf.rfCloseObject= lc->lcCloseObject;
59 rf.rfUpdateFlags= FIELDdoDOC_FORMATTED|FIELDdoDOC_COMPLETE|FIELDdoDOC_INFO;
60 rf.rfFieldsUpdated= 0;
61
62 rf.rfBodySectNode= bd->bdBody.dtRoot->biChildren[0];
63
64 if ( docRecalculateTextLevelFieldsInDocumentTree( &rf, &(bd->bdBody),
65 bd->bdBody.dtRoot->biChildren[0], page ) )
66 { LDEB(1); return -1; }
67
68 if ( rf.rfFieldsUpdated > 0 &&
69 tedLayoutDocumentBody( &reachedBottom,
70 &(teo->teoLayoutContext) ) )
71 { LDEB(1); return -1; }
72 }
73
74 if ( docDelimitFieldInDoc( &dsInsideToc, dsAroundToc,
75 &headPart, &tailPart, eo->eoDocument, dfToc ) )
76 { LDEB(1); return -1; }
77
78 docEditIncludeNodeInReformatRange( eo, dsAroundToc->dsHead.dpNode );
79 docEditIncludeNodeInReformatRange( eo, dsAroundToc->dsTail.dpNode );
80
81 docSetEditPosition( &(eo->eoAffectedRange.erTail),
82 &(dsAroundToc->dsTail) );
83
84 if ( reachedBottom )
85 { teo->teoRefreshScreenRectangle= 1; }
86
87 return 0;
88 }
89
90 /************************************************************************/
91 /* */
92 /* Insert a table of contents. */
93 /* */
94 /************************************************************************/
95
tedDocAddTocField(EditDocument * ed,const TocField * tf,int traced)96 void tedDocAddTocField( EditDocument * ed,
97 const TocField * tf,
98 int traced )
99 {
100 DocumentSelection dsInsideToc;
101 DocumentSelection dsAroundToc;
102
103 TedEditOperation teo;
104 EditOperation * eo= &(teo.teoEo);
105
106 SelectionGeometry sg;
107 SelectionDescription sd;
108
109 TextAttribute taSet;
110 PropertyMask taSetMask;
111
112 int headPart;
113 int tailPart;
114
115 DocumentField * dfToc;
116 DocumentSelection dsTraced;
117
118 FieldInstructions fi;
119
120 const int fullWidth= 0;
121
122 docInitFieldInstructions( &fi );
123
124 if ( docTocFieldSetTocInstructions( &fi, tf ) )
125 { LDEB(1); goto ready; }
126
127 utilInitTextAttribute( &taSet );
128 utilPropMaskClear( &taSetMask );
129
130 tedStartEditOperation( &teo, &sg, &sd, ed, fullWidth, traced );
131
132 if ( eo->eoSelectionScope.ssTreeType != DOCinBODY )
133 { LDEB(eo->eoSelectionScope.ssTreeType); return; }
134
135 if ( tedEditStartReplace( &dsTraced, &teo,
136 EDITcmdREPLACE_BODY_LEVEL, DOClevSPAN, 0 ) )
137 { LDEB(1); goto ready; }
138
139 if ( tedDocReplaceSelectionWithField( &teo, &dfToc,
140 &headPart, &tailPart,
141 &dsInsideToc,
142 &dsAroundToc,
143 &fi, DOCfkTOC,
144 &taSetMask, &taSet ) )
145 { LDEB(1); goto ready; }
146
147 if ( tedRefreshTocField( &dsAroundToc, &teo, dfToc ) )
148 { LDEB(1); }
149
150 /* 5 */
151 tedEditFinishSelection( &teo, &dsAroundToc );
152
153 if ( teo.teoEditTrace )
154 {
155 if ( docRtfTraceNewContents( eo, SELposALL ) )
156 { LDEB(1); goto ready; }
157 }
158
159 tedFinishEditOperation( &teo );
160
161 ready:
162
163 docCleanFieldInstructions( &fi );
164
165 tedCleanEditOperation( &teo );
166
167 return;
168 }
169
170 /************************************************************************/
171 /* */
172 /* Set and/or refresh a table of contents field. */
173 /* */
174 /************************************************************************/
175
tedDocSetTocField(EditDocument * ed,const TocField * tf,int traced)176 int tedDocSetTocField( EditDocument * ed,
177 const TocField * tf,
178 int traced )
179 {
180 int rval= 0;
181 TedEditOperation teo;
182 EditOperation * eo= &(teo.teoEo);
183
184 SelectionGeometry sg;
185 SelectionDescription sd;
186
187 DocumentField * dfToc;
188
189 FieldInstructions fi;
190
191 const int fullWidth= 0;
192
193 docInitFieldInstructions( &fi );
194
195 tedStartEditOperation( &teo, &sg, &sd, ed, fullWidth, traced );
196
197 if ( docTocFieldSetTocInstructions( &fi, tf ) )
198 { LDEB(1); rval= -1; goto ready; }
199
200 dfToc= docFindTypedFieldForPosition( eo->eoDocument,
201 &(eo->eoHeadDp), DOCfkTOC, 0 );
202 if ( ! dfToc )
203 { XDEB(dfToc); rval=-1; goto ready; }
204
205 if ( tedDocUpdField( &teo, dfToc, &fi ) )
206 { LDEB(1); rval= -1; goto ready; }
207
208 ready:
209
210 docCleanFieldInstructions( &fi );
211 tedCleanEditOperation( &teo );
212
213 return rval;
214 }
215