1 /************************************************************************/
2 /* */
3 /* Ted: Management of the selection and the current position. */
4 /* */
5 /************************************************************************/
6
7 # include "tedConfig.h"
8
9 # include <stddef.h>
10 # include <stdio.h>
11 # include <ctype.h>
12
13 # include "tedApp.h"
14 # include "tedRuler.h"
15 # include "tedLayout.h"
16 # include "tedSelect.h"
17 # include "tedAppResources.h"
18 # include "tedToolFront.h"
19 # include "tedDocument.h"
20 # include <docParaParticules.h>
21 # include <docEditCommand.h>
22
23 # include <appDebugon.h>
24
25 /************************************************************************/
26 /* */
27 /* Is the selection a position between two chars? */
28 /* */
29 /************************************************************************/
30
tedHasIBarSelection(const EditDocument * ed)31 int tedHasIBarSelection( const EditDocument * ed )
32 {
33 const TedDocument * td= (const TedDocument *)ed->edPrivateData;
34
35 return docIsIBarSelection( &(td->tdSelection) );
36 }
37
tedHasSelection(const EditDocument * ed)38 int tedHasSelection( const EditDocument * ed )
39 {
40 const TedDocument * td= (const TedDocument *)ed->edPrivateData;
41 const DocumentSelection * ds= &(td->tdSelection);
42
43 if ( ds->dsHead.dpNode )
44 { return 1; }
45
46 return 0;
47 }
48
tedGetSelection(DocumentSelection * ds,SelectionGeometry * sg,SelectionDescription * sd,DocumentTree ** pEi,BufferItem ** pBodySectNode,const EditDocument * ed)49 int tedGetSelection( DocumentSelection * ds,
50 SelectionGeometry * sg,
51 SelectionDescription * sd,
52 DocumentTree ** pEi,
53 BufferItem ** pBodySectNode,
54 const EditDocument * ed )
55 {
56 const TedDocument * td= (const TedDocument *)ed->edPrivateData;
57 BufferDocument * bd= td->tdDocument;
58 const DocumentSelection * dss= &(td->tdSelection);
59
60 BufferItem * bodySectNode= (BufferItem *)0;
61 DocumentTree * ei= (DocumentTree *)0;
62
63 if ( ! dss->dsHead.dpNode )
64 { return 1; }
65
66 if ( pEi || pBodySectNode )
67 {
68 if ( docGetTreeOfNode( &ei, &bodySectNode, bd, dss->dsHead.dpNode ) )
69 { LDEB(1); return -1; }
70 }
71
72 *ds= td->tdSelection;
73 *sg= td->tdSelectionGeometry;
74 *sd= td->tdSelectionDescription;
75 if ( pBodySectNode )
76 { *pBodySectNode= bodySectNode; }
77 if ( pEi )
78 { *pEi= ei; }
79
80 return 0;
81 }
82
83 /************************************************************************/
84 /* */
85 /* Determine the derived properties such as line number and */
86 /* coordinates of the current selection. */
87 /* */
88 /************************************************************************/
89
tedDelimitCurrentSelection(EditDocument * ed)90 void tedDelimitCurrentSelection( EditDocument * ed )
91 {
92 TedDocument * td= (TedDocument *)ed->edPrivateData;
93
94 DocumentSelection dsDoc;
95 SelectionGeometry sgDoc;
96 SelectionDescription sdDoc;
97
98 BufferItem * bodySectNode;
99
100 int lastLine;
101
102 LayoutContext lc;
103
104 layoutInitContext( &lc );
105 tedSetScreenLayoutContext( &lc, ed );
106
107 if ( tedGetSelection( &dsDoc, &sgDoc, &sdDoc,
108 (DocumentTree **)0, &bodySectNode, ed ) )
109 { LDEB(1); return; }
110
111 lastLine= sdDoc.sdIsIBarSelection &&
112 ( td->tdSelectionGeometry.sgHead.pgPositionFlags & POSflagLINE_HEAD );
113
114 tedSelectionGeometry( &(td->tdSelectionGeometry), &dsDoc, bodySectNode,
115 lastLine, &lc );
116
117 return;
118 }
119
120 /************************************************************************/
121 /* */
122 /* Adapt tools and rulers to the current position. */
123 /* */
124 /************************************************************************/
125
tedAdaptOptions(TedDocument * td,const unsigned char * cmdEnabled,const SelectionDescription * sd)126 static void tedAdaptOptions( TedDocument * td,
127 const unsigned char * cmdEnabled,
128 const SelectionDescription * sd )
129 {
130 const EditTrace * et= &(td->tdEditTrace);
131
132 /* modifications */
133
134 guiEnableWidget( td->tdInsHyperlinkOption,
135 cmdEnabled[EDITcmdSET_HYPERLINK] );
136
137 guiEnableWidget( td->tdInsBookmarkOption,
138 cmdEnabled[EDITcmdSET_BOOKMARK] );
139
140 guiEnableWidget( td->tdCutOption, cmdEnabled[EDITcmdDELETE_SELECTION] );
141
142 guiEnableWidget( td->tdPasteOption, cmdEnabled[EDITcmdREPLACE] );
143
144 guiEnableWidget( td->tdInsPictOption, cmdEnabled[EDITcmdREPLACE] );
145 guiEnableWidget( td->tdInsFileOption, cmdEnabled[EDITcmdREPLACE] );
146 guiEnableWidget( td->tdInsSymbolOption, cmdEnabled[EDITcmdREPLACE] );
147
148 guiEnableWidget( td->tdFormatOneParaOption,
149 cmdEnabled[EDITcmdMERGE_PARAS] );
150
151 guiEnableWidget( td->tdInsInsertFootnoteOption,
152 cmdEnabled[EDITcmdINS_NOTE] );
153
154 guiEnableWidget( td->tdInsInsertEndnoteOption,
155 cmdEnabled[EDITcmdINS_NOTE] );
156
157 guiEnableWidget( td->tdInsInsertChftnsepOption,
158 cmdEnabled[EDITcmdREPLACE_FTNSEP] );
159 guiEnableWidget( td->tdInsInsertTableOption,
160 cmdEnabled[EDITcmdINSERT_TABLE] );
161 guiEnableWidget( td->tdInsInsertPageNumberOption,
162 cmdEnabled[EDITcmdREPLACE] );
163 guiEnableWidget( td->tdInsInsertLineBreakOption,
164 cmdEnabled[EDITcmdREPLACE] );
165 guiEnableWidget( td->tdInsInsertPageBreakOption,
166 cmdEnabled[EDITcmdREPLACE_BODY_LEVEL] );
167 guiEnableWidget( td->tdInsInsertColumnBreakOption,
168 cmdEnabled[EDITcmdREPLACE_BODY_LEVEL] );
169 guiEnableWidget( td->tdInsInsertSectBreakOption,
170 cmdEnabled[EDITcmdSPLIT_SECT] );
171
172 guiEnableWidget( td->tdTabInsertTableOption,
173 cmdEnabled[EDITcmdINSERT_TABLE] );
174 guiEnableWidget( td->tdTabAddRowOption,
175 cmdEnabled[EDITcmdAPPEND_ROW] );
176 guiEnableWidget( td->tdTabAddColumnOption,
177 cmdEnabled[EDITcmdINSERT_COLUMN] );
178 guiEnableWidget( td->tdTabDeleteTableOption,
179 cmdEnabled[EDITcmdDELETE_TABLE] );
180 guiEnableWidget( td->tdTabDeleteRowOption,
181 cmdEnabled[EDITcmdDELETE_ROW] );
182 guiEnableWidget( td->tdTabDeleteColumnOption,
183 cmdEnabled[EDITcmdDELETE_COLUMN] );
184
185 guiEnableWidget( td->tdFontBoldOption,
186 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
187 guiEnableWidget( td->tdFontItalicOption,
188 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
189 guiEnableWidget( td->tdFontUnderlinedOption,
190 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
191 guiEnableWidget( td->tdFontSuperscriptOption,
192 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
193 guiEnableWidget( td->tdFontSubscriptOption,
194 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
195 guiEnableWidget( td->tdFontPasteOption,
196 cmdEnabled[EDITcmdUPD_SPAN_PROPS] );
197
198 guiEnableWidget( td->tdFormatPasteRulerOption,
199 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
200 guiEnableWidget( td->tdFormatIncreaseIndentOption,
201 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
202 guiEnableWidget( td->tdFormatDecreaseIndentOption,
203 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
204 guiEnableWidget( td->tdFormatAlignLeftOption,
205 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
206 guiEnableWidget( td->tdFormatAlignRightOption,
207 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
208 guiEnableWidget( td->tdFormatAlignCenterOption,
209 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
210 guiEnableWidget( td->tdFormatAlignJustifyOption,
211 cmdEnabled[EDITcmdUPD_PARA_PROPS] );
212
213 {
214 int n;
215 const int direction= -1;
216
217 n= docEditGetTraceStep( (const TraceStep **)0, (int *)0,
218 direction, et, et->etIndex );
219
220 guiEnableWidget( td->tdUndoOption, n >= 0 );
221 }
222
223 {
224 const TraceStep * ts= (const TraceStep *)0;
225 int n;
226 const int direction= 1;
227 int isRepeat;
228
229 n= docEditGetTraceStep( &ts, &isRepeat, direction, et, et->etIndex );
230 if ( n < 0 )
231 { guiEnableWidget( td->tdRepeatOption, 0 ); }
232 else{
233 if ( ! isRepeat )
234 { guiEnableWidget( td->tdRepeatOption, 1 ); }
235 else{
236 int cmd= ts->tsCommand;
237
238 if ( cmd == EDITcmdEXTEND_REPLACE )
239 { cmd= EDITcmdREPLACE; }
240 if ( cmd < 0 || cmd >= EDITcmd_COUNT )
241 { LDEB(cmd); guiEnableWidget( td->tdRepeatOption, 0 ); }
242 else{
243 if ( cmd == EDITcmdDEL_FIELD ||
244 cmd == EDITcmdUPD_FIELD )
245 {
246 guiEnableWidget( td->tdRepeatOption,
247 cmdEnabled[cmd] &&
248 sd->sdHeadFieldKind == ts->tsFieldKind );
249 }
250 else{
251 guiEnableWidget( td->tdRepeatOption, cmdEnabled[cmd] );
252 }
253 }
254 }
255 }
256 }
257
258 /* read-only */
259
260 guiEnableWidget( td->tdCopyOption, ! sd->sdIsIBarSelection );
261
262 guiEnableWidget( td->tdTabSelectTableOption, sd->sdHeadInTable );
263 guiEnableWidget( td->tdTabSelectRowOption, sd->sdHeadInTable );
264 guiEnableWidget( td->tdTabSelectColumnOption, sd->sdHeadInTable );
265
266 return;
267 }
268
tedAdaptToolsToSelection(EditDocument * ed)269 void tedAdaptToolsToSelection( EditDocument * ed )
270 {
271 EditApplication * ea= ed->edApplication;
272 TedAppResources * tar= (TedAppResources *)ea->eaResourceData;
273 TedDocument * td= (TedDocument *)ed->edPrivateData;
274
275 DocumentSelection ds;
276 SelectionGeometry sg;
277 SelectionDescription sd;
278 DocumentTree * ei;
279 BufferItem * bodySectNode;
280
281 unsigned char cmdEnabled[EDITcmd_COUNT];
282
283 if ( tedGetSelection( &ds, &sg, &sd, &ei, &bodySectNode, ed ) )
284 { LDEB(1); return; }
285
286 docEnableEditCommands( cmdEnabled, &sd );
287
288 tedDocAdaptTopRuler( ed, &ds, &sg, &sd, bodySectNode );
289
290 tedSetBottomRulerValues( ed->edBottomRuler, ed->edBottomRulerWidget,
291 sd.sdTailPage,
292 sd.sdDocumentPages,
293 sd.sdTailSection,
294 sd.sdDocumentSections );
295
296 tedAdaptFontIndicatorsToSelection( ed );
297
298 tedAdaptOptions( td, cmdEnabled, &(td->tdSelectionDescription) );
299
300 if ( tar->tarInspector )
301 {
302 const int choosePage= 0;
303
304 tedFormatToolAdaptToSelection( tar->tarInspector, ed, choosePage, ei,
305 &(td->tdSelection),
306 &(td->tdSelectionGeometry),
307 &(td->tdSelectionDescription),
308 cmdEnabled );
309 }
310 }
311
312 /************************************************************************/
313 /* */
314 /* Adapt format tool to the circumstances. */
315 /* */
316 /************************************************************************/
317
tedAdaptFormatToolToDocument(EditDocument * ed,int choosePage)318 void tedAdaptFormatToolToDocument( EditDocument * ed,
319 int choosePage )
320 {
321 EditApplication * ea= ed->edApplication;
322 TedAppResources * tar= (TedAppResources *)ea->eaResourceData;
323
324 DocumentTree * ei;
325 DocumentSelection ds;
326 SelectionGeometry sg;
327 SelectionDescription sd;
328
329 unsigned char cmdEnabled[EDITcmd_COUNT];
330
331 if ( ! tar->tarInspector )
332 { return; }
333
334 if ( tedGetSelection( &ds, &sg, &sd, &ei, (BufferItem **)0, ed ) )
335 { LDEB(1); return; }
336
337 docEnableEditCommands( cmdEnabled, &sd );
338
339 tedFormatToolAdaptToSelection( tar->tarInspector, ed, choosePage,
340 ei, &ds, &sg, &sd, cmdEnabled );
341
342 return;
343 }
344
345