1 /************************************************************************/
2 /*									*/
3 /*  Editor functionality.						*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"tedConfig.h"
8 
9 #   include	<stddef.h>
10 #   include	<stdio.h>
11 #   include	<ctype.h>
12 
13 #   include	"tedEdit.h"
14 #   include	"tedDocFront.h"
15 #   include	<docRtfTrace.h>
16 #   include	<docParaParticules.h>
17 #   include	<docTreeType.h>
18 #   include	<docNodeTree.h>
19 #   include	<docEditCommand.h>
20 
21 #   include	<appDebugon.h>
22 
23 /************************************************************************/
24 /*									*/
25 /*  Insert a paragraph Before/After the selection, depending on the	*/
26 /*  value of the 'after' argument.					*/
27 /*									*/
28 /************************************************************************/
29 
tedDocInsertParagraph(EditDocument * ed,int after,int traced)30 int tedDocInsertParagraph(	EditDocument *		ed,
31 				int			after,
32 				int			traced )
33     {
34     int				rval= 0;
35 
36     BufferItem *		paraBi;
37     int				textAttributeNumber;
38 
39     TedEditOperation		teo;
40     EditOperation *		eo= &(teo.teoEo);
41     SelectionGeometry		sg;
42     SelectionDescription	sd;
43 
44     DocumentPosition		dpNew;
45 
46     const int			fullWidth= 1;
47 
48     tedStartEditOperation( &teo, &sg, &sd, ed, fullWidth, traced );
49 
50     if  ( after )
51 	{
52 	if  ( tedEditStartStep( &teo, EDITcmdINSERT_PARA ) )
53 	    { LDEB(1); rval= -1; goto ready;	}
54 	}
55     else{
56 	if  ( tedEditStartStep( &teo, EDITcmdAPPEND_PARA ) )
57 	    { LDEB(1); rval= -1; goto ready;	}
58 	}
59 
60     if  ( after )
61 	{
62 	paraBi= eo->eoTailDp.dpNode;
63 	if  ( ! paraBi )
64 	    { XDEB(paraBi); rval= -1; goto ready;	}
65 
66 	textAttributeNumber= paraBi->biParaParticules[
67 			paraBi->biParaParticuleCount-1].tpTextAttrNr;
68 	}
69     else{
70 	paraBi= eo->eoHeadDp.dpNode;
71 	if  ( ! paraBi )
72 	    { XDEB(paraBi); rval= -1; goto ready;	}
73 
74 	textAttributeNumber= paraBi->biParaParticules[0].tpTextAttrNr;
75 	}
76 
77     tedEditIncludeNodeInRedraw( &teo, paraBi );
78 
79     if  ( docInsertParagraph( eo, &dpNew, paraBi, after, textAttributeNumber ) )
80 	{ LDEB(1); rval= -1; goto ready;	}
81 
82     docSetIBarRange( &(eo->eoAffectedRange), &dpNew );
83     docSetIBarRange( &(eo->eoSelectedRange), &dpNew );
84 
85     tedEditFinishSelectionTail( &teo );
86 
87     if  ( teo.teoEditTrace )
88 	{
89 	const SelectionScope * const s0= (const SelectionScope *)0;
90 
91 	if  ( after )
92 	    { docRtfTraceNewPosition( eo, s0, SELposAFTER );	}
93 	else{ docRtfTraceNewPosition( eo, s0, SELposBEFORE );	}
94 	}
95 
96     tedFinishEditOperation( &teo );
97 
98   ready:
99 
100     tedCleanEditOperation( &teo );
101 
102     return rval;
103     }
104 
105 /************************************************************************/
106 /*									*/
107 /*  Insert a new section before/after the current one.			*/
108 /*									*/
109 /************************************************************************/
110 
tedEditInsertSection(DocumentPosition * dpBeforeSplit,DocumentPosition * dpAfterSplit,TedEditOperation * teo,int split,int after)111 int tedEditInsertSection(	DocumentPosition *	dpBeforeSplit,
112 				DocumentPosition *	dpAfterSplit,
113 				TedEditOperation *	teo,
114 				int			split,
115 				int			after )
116     {
117     BufferItem *		paraNode;
118     BufferItem *		sectNode;
119 
120     int				textAttributeNumber;
121 
122     EditOperation *		eo= &(teo->teoEo);
123 
124     if  ( after )
125 	{
126 	paraNode= eo->eoTailDp.dpNode;
127 	if  ( ! paraNode || paraNode->biTreeType != DOCinBODY )
128 	    { XDEB(paraNode); return -1;	}
129 
130 	sectNode= docGetSectNode( paraNode );
131 	if  ( ! sectNode )
132 	    { XDEB(sectNode); return -1;	}
133 
134 	textAttributeNumber= paraNode->biParaParticules[
135 			paraNode->biParaParticuleCount-1].tpTextAttrNr;
136 	}
137     else{
138 	paraNode= eo->eoHeadDp.dpNode;
139 	if  ( ! paraNode || paraNode->biTreeType != DOCinBODY )
140 	    { XDEB(paraNode); return -1;	}
141 
142 	sectNode= docGetSectNode( paraNode );
143 	if  ( ! sectNode )
144 	    { XDEB(sectNode); return -1;	}
145 
146 	textAttributeNumber= paraNode->biParaParticules[0].tpTextAttrNr;
147 	}
148 
149     tedEditIncludeNodeInRedraw( teo, sectNode );
150 
151     if  ( docInsertSection( eo, dpBeforeSplit, dpAfterSplit, paraNode,
152 					split, after, textAttributeNumber ) )
153 	{ LLDEB(split,after); return -1;	}
154 
155     return 0;
156     }
157 
158 /************************************************************************/
159 /*									*/
160 /*  Insert a new section before/after the current one.			*/
161 /*									*/
162 /************************************************************************/
163 
tedDocInsertSection(EditDocument * ed,int after,int traced)164 int tedDocInsertSection(	EditDocument *		ed,
165 				int			after,
166 				int			traced )
167     {
168     int				rval= 0;
169 
170     TedEditOperation		teo;
171     EditOperation *		eo= &(teo.teoEo);
172     SelectionGeometry		sg;
173     SelectionDescription	sd;
174 
175     DocumentPosition		dpBefore;
176     DocumentPosition		dpAfter;
177 
178     const int			split= 0;
179     const int			fullWidth= 1;
180 
181     tedStartEditOperation( &teo, &sg, &sd, ed, fullWidth, traced );
182 
183     if  ( after )
184 	{
185 	if  ( tedEditStartStep( &teo, EDITcmdAPPEND_SECT ) )
186 	    { LDEB(1); rval= -1; goto ready;	}
187 	}
188     else{
189 	if  ( tedEditStartStep( &teo, EDITcmdINSERT_SECT ) )
190 	    { LDEB(1); rval= -1; goto ready;	}
191 	}
192 
193     if  ( tedEditInsertSection( &dpBefore, &dpAfter, &teo, split, after ) )
194 	{ LDEB(after); rval= -1; goto ready;	}
195 
196     if  ( after )
197 	{
198 	docSetIBarRange( &(eo->eoAffectedRange), &dpAfter );
199 	docSetIBarRange( &(eo->eoSelectedRange), &dpAfter );
200 	}
201     else{
202 	docSetIBarRange( &(eo->eoAffectedRange), &dpBefore );
203 	docSetIBarRange( &(eo->eoSelectedRange), &dpBefore );
204 	}
205 
206     tedEditFinishSelectionTail( &teo );
207 
208     if  ( teo.teoEditTrace )
209 	{
210 	const SelectionScope * const s0= (const SelectionScope *)0;
211 
212 	if  ( after )
213 	    { docRtfTraceNewPosition( eo, s0, SELposAFTER );	}
214 	else{ docRtfTraceNewPosition( eo, s0, SELposBEFORE );	}
215 	}
216 
217     tedFinishEditOperation( &teo );
218 
219   ready:
220 
221     tedCleanEditOperation( &teo );
222 
223     return rval;
224     }
225 
226 /************************************************************************/
227 /*									*/
228 /*  Insert a row close to the current row in a table.			*/
229 /*									*/
230 /*  1)  Start edit operation.						*/
231 /*  2)  Find the position of the beginning/end of the selection in the	*/
232 /*	table.								*/
233 /*  3)  Get the row that serves as a template.				*/
234 /*  4)  Paragraph number of the paragraph that comes directly		*/
235 /*	below/above the fresh rows.					*/
236 /*  5)  Finish..							*/
237 /*									*/
238 /************************************************************************/
239 
tedDocAddRowToTable(EditDocument * ed,int after,int traced)240 int tedDocAddRowToTable(	EditDocument *		ed,
241 				int			after,
242 				int			traced )
243     {
244     int				rval= 0;
245     BufferItem *		paraBi;
246     BufferItem *		parentNode;
247     BufferItem *		refRowBi;
248 
249     int				col;
250     int				row;
251     int				row0;
252     int				row1;
253 
254     const int			rows= 1;
255 
256     TedEditOperation		teo;
257     EditOperation *		eo= &(teo.teoEo);
258     SelectionGeometry		sg;
259     SelectionDescription	sd;
260 
261     DocumentPosition		dpRef;
262     int				part;
263     int				paraNr;
264     int				textAttributeNumber;
265 
266     DocumentSelection		dsRows;
267 
268     int				command;
269 
270     const int			fullWidth= 1;
271 
272     after= ( after != 0 );
273 
274     /*  1  */
275     tedStartEditOperation( &teo, &sg, &sd, ed, fullWidth, traced );
276 
277     if  ( after )
278 	{ command= EDITcmdINSERT_ROW;	}
279     else{ command= EDITcmdAPPEND_ROW;	}
280 
281     if  ( tedEditStartStep( &teo, command ) )
282 	{ LDEB(1); rval= -1; goto ready;	}
283 
284     if  ( after )
285 	{ paraBi= eo->eoTailDp.dpNode;	}
286     else{ paraBi= eo->eoHeadDp.dpNode;	}
287 
288     /*  2  */
289     if  ( docDelimitTable( paraBi, &parentNode, &col, &row0, &row, &row1 ) )
290 	{ LDEB(1); rval= -1; goto ready;	}
291 
292     /*  3  */
293     refRowBi= parentNode->biChildren[row];
294     if  ( after )
295 	{
296 	if  ( docTailPosition( &dpRef, refRowBi ) )
297 	    { LDEB(row); rval= -1; goto ready;	}
298 	if  ( docFindParticuleOfPosition( &part, (int *)0,
299 						    &dpRef, PARAfindFIRST ) )
300 	    { LDEB(dpRef.dpStroff); rval= -1; goto ready;	}
301 	}
302     else{
303 	if  ( docHeadPosition( &dpRef, refRowBi ) )
304 	    { LDEB(row); rval= -1; goto ready;	}
305 	if  ( docFindParticuleOfPosition( &part, (int *)0,
306 						    &dpRef, PARAfindLAST ) )
307 	    { LDEB(dpRef.dpStroff); rval= -1; goto ready;	}
308 	}
309 
310     /*  4  */
311     paraNr= docNumberOfParagraph( dpRef.dpNode )+ after;
312     textAttributeNumber= dpRef.dpNode->biParaParticules[part].tpTextAttrNr;
313 
314     /*  5  */
315     if  ( docInsertTableRows( &dsRows, eo, parentNode, refRowBi,
316 			    &(refRowBi->biRowProperties),
317 			    textAttributeNumber, row+ after, paraNr, rows ) )
318 	{ LDEB(row+after); rval= -1; goto ready;	}
319 
320     /*  5  */
321     docSetIBarRange( &(eo->eoAffectedRange), &(dsRows.dsHead) );
322     docSetIBarRange( &(eo->eoSelectedRange), &(dsRows.dsHead) );
323     tedEditFinishSelectionTail( &teo );
324 
325     if  ( teo.teoEditTrace )
326 	{
327 	const SelectionScope * const s0= (const SelectionScope *)0;
328 
329 	if  ( after )
330 	    { docRtfTraceNewPosition( eo, s0, SELposAFTER );	}
331 	else{ docRtfTraceNewPosition( eo, s0, SELposBEFORE );	}
332 	}
333 
334     tedFinishEditOperation( &teo );
335 
336   ready:
337 
338     tedCleanEditOperation( &teo );
339 
340     return rval;
341     }
342 
343