1 /* AbiWord
2 * Copyright (C) 1998 AbiSource, Inc.
3 * BIDI Copyright (c) 2001,2002 Tomas Frydrych
4 * (C) Martin Sevior 2004, , <msevior@physics.unimelb.edu.au>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA.
20 */
21
22
23 #include "fp_FieldTOCNum.h"
24 #include "fl_BlockLayout.h"
25 #include "pt_Types.h"
26 #include "pd_Document.h"
27 #include "fl_DocLayout.h"
28 #include "fp_Line.h"
29 #include "ut_debugmsg.h"
30 #include "fl_TOCLayout.h"
31 #include <string.h>
32
fp_FieldTOCNumRun(fl_BlockLayout * pBL,UT_uint32 iOffsetFirst,UT_uint32 iLen)33 fp_FieldTOCNumRun::fp_FieldTOCNumRun(fl_BlockLayout* pBL,UT_uint32 iOffsetFirst, UT_uint32 iLen) : fp_FieldRun(pBL,iOffsetFirst, iLen)
34 {
35 UT_ASSERT(pBL);
36 _setDirection(pBL->getDominantDirection());
37 }
38
calculateValue(void)39 bool fp_FieldTOCNumRun::calculateValue(void)
40 {
41 UT_UCSChar sz_ucs_FieldValue[FPFIELD_MAX_LENGTH + 1];
42 //
43 // First Find page number.
44 //
45 pf_Frag_Strux* sdh = getBlock()->getStruxDocHandle();
46 PD_Document * pDoc = getBlock()->getDocument();
47 PT_DocPosition pos = pDoc->getStruxPosition(sdh)+1;
48 FL_DocLayout * pLayout = getBlock()->getDocLayout();
49 fl_BlockLayout * pBlockInDoc = pLayout->findBlockAtPosition(pos);
50 if(pBlockInDoc == NULL)
51 {
52 sz_ucs_FieldValue[0] = static_cast<UT_UCSChar>(' ');
53 sz_ucs_FieldValue[1] = 0;
54 return _setValue(sz_ucs_FieldValue);
55 }
56 fp_Line * pLine = static_cast<fp_Line *>(pBlockInDoc->getFirstContainer());
57 UT_sint32 kk = 0;
58 bool b_goodLine = false;
59 while (pLine && !b_goodLine)
60 {
61 for (kk = 0; kk < pLine->getNumRunsInLine(); kk++)
62 {
63 if(pLine->getRunFromIndex(kk)->getType() == FPRUN_TEXT)
64 {
65 b_goodLine = true;
66 break;
67 }
68 }
69 if (!b_goodLine)
70 {
71 pLine = static_cast<fp_Line *>(pLine->getNext());
72 }
73 }
74 if(pLine == NULL)
75 {
76 sz_ucs_FieldValue[0] = static_cast<UT_UCSChar>(' ');
77 sz_ucs_FieldValue[1] = 0;
78 return _setValue(sz_ucs_FieldValue);
79 }
80
81 fp_Page * pPage = pLine->getPage();
82 if (pPage == NULL)
83 {
84 UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
85 return false;
86 }
87 UT_sint32 iPage = pPage->getFieldPageNumber();
88 bool b_hasSetFieldPageNumber = false;
89 if(iPage < 0)
90 {
91 pPage->resetFieldPageNumber();
92 iPage = pPage->getFieldPageNumber();
93 b_hasSetFieldPageNumber = true;
94 if (iPage < 0)
95 {
96 sz_ucs_FieldValue[0] = static_cast<UT_UCSChar>(' ');
97 sz_ucs_FieldValue[1] = 0;
98 return _setValue(sz_ucs_FieldValue);
99 }
100 }
101 UT_String sVal("");
102 FootnoteType iType = getBlock()->getTOCNumType();
103 pLayout->getStringFromFootnoteVal(sVal,iPage,iType);
104 const char * psz = sVal.c_str();
105
106 if (b_hasSetFieldPageNumber)
107 {
108 // We need to set the field page number value to -1 so that we
109 // recalculate the page number next time we enter this function
110 pPage->setFieldPageNumber(-1);
111 }
112 bool bStop = false;
113 UT_sint32 i = 0;
114 sz_ucs_FieldValue[0] = static_cast<UT_UCSChar>(' ');
115 for(i=1; (i<FPFIELD_MAX_LENGTH) && !bStop; i++)
116 {
117 sz_ucs_FieldValue[i] = static_cast<UT_UCSChar>(*psz);
118 if(*psz == 0)
119 {
120 bStop = true;
121 }
122 else
123 {
124 psz++;
125 }
126 }
127 return _setValue(sz_ucs_FieldValue);
128 }
129
_draw(dg_DrawArgs * pDA)130 void fp_FieldTOCNumRun::_draw(dg_DrawArgs* pDA)
131 {
132 _defaultDraw(pDA);
133 }
134
_lookupProperties(const PP_AttrProp * pSpanAP,const PP_AttrProp * pBlockAP,const PP_AttrProp * pSectionAP,GR_Graphics * pG)135 void fp_FieldTOCNumRun::_lookupProperties(const PP_AttrProp * pSpanAP,
136 const PP_AttrProp * pBlockAP,
137 const PP_AttrProp * pSectionAP,
138 GR_Graphics * pG)
139 {
140 fp_FieldRun::_lookupProperties (pSpanAP, pBlockAP, pSectionAP,pG) ;
141 }
142
143 /////////////////////////////////////////////////////////////////////////////
144
145
fp_FieldTOCListLabelRun(fl_BlockLayout * pBL,UT_uint32 iOffsetFirst,UT_uint32 iLen)146 fp_FieldTOCListLabelRun::fp_FieldTOCListLabelRun(fl_BlockLayout* pBL,UT_uint32 iOffsetFirst, UT_uint32 iLen) : fp_FieldRun(pBL,iOffsetFirst, iLen)
147 {
148 UT_ASSERT(pBL);
149 _setDirection(pBL->getDominantDirection());
150 _setLength(0);
151 }
152
calculateValue(void)153 bool fp_FieldTOCListLabelRun::calculateValue(void)
154 {
155 UT_UCSChar sz_ucs_FieldValue[FPFIELD_MAX_LENGTH + 1];
156 //
157 // First get owning TOC.
158 //
159 UT_ASSERT(getLength() == 0);
160 fl_TOCLayout * pTOCL = static_cast<fl_TOCLayout *>(getBlock()->myContainingLayout());
161 UT_ASSERT(pTOCL->getContainerType() == FL_CONTAINER_TOC);
162 UT_String str = pTOCL->getTOCListLabel(getBlock()).utf8_str();
163 if(str.size() == 0)
164 {
165 sz_ucs_FieldValue[0] = 0;
166 return _setValue(sz_ucs_FieldValue);
167 }
168 UT_sint32 i = 0;
169 bool bStop = false;
170 for(i=0; (i<FPFIELD_MAX_LENGTH) && !bStop; i++)
171 {
172 sz_ucs_FieldValue[i] = static_cast<UT_UCSChar>(str[i]);
173 if(str[i] == 0)
174 {
175 bStop = true;
176 }
177 }
178 return _setValue(sz_ucs_FieldValue);
179 }
180
_draw(dg_DrawArgs * pDA)181 void fp_FieldTOCListLabelRun::_draw(dg_DrawArgs* pDA)
182 {
183 _defaultDraw(pDA);
184 }
185
_lookupProperties(const PP_AttrProp * pSpanAP,const PP_AttrProp * pBlockAP,const PP_AttrProp * pSectionAP,GR_Graphics * pG)186 void fp_FieldTOCListLabelRun::_lookupProperties(const PP_AttrProp * pSpanAP,
187 const PP_AttrProp * pBlockAP,
188 const PP_AttrProp * pSectionAP,
189 GR_Graphics * pG)
190 {
191 fp_FieldRun::_lookupProperties (pSpanAP, pBlockAP, pSectionAP,pG) ;
192 _setLength(0);
193 }
194
195 ////////////////////////////////////////////////////////////////////////////
196
197
fp_FieldTOCHeadingRun(fl_BlockLayout * pBL,UT_uint32 iOffsetFirst,UT_uint32 iLen)198 fp_FieldTOCHeadingRun::fp_FieldTOCHeadingRun(fl_BlockLayout* pBL,UT_uint32 iOffsetFirst, UT_uint32 iLen) : fp_FieldRun(pBL,iOffsetFirst, iLen)
199 {
200 UT_ASSERT(pBL);
201 _setDirection(pBL->getDominantDirection());
202 _setLength(0);
203 }
204
calculateValue(void)205 bool fp_FieldTOCHeadingRun::calculateValue(void)
206 {
207 UT_UCSChar sz_ucs_FieldValue[FPFIELD_MAX_LENGTH + 1];
208 //
209 // First get owning TOC.
210 //
211 UT_ASSERT(getLength() == 0);
212 fl_TOCLayout * pTOCL = static_cast<fl_TOCLayout *>(getBlock()->myContainingLayout());
213 UT_ASSERT(pTOCL->getContainerType() == FL_CONTAINER_TOC);
214 UT_UCS4String str = pTOCL->getTOCHeading().ucs4_str();
215 if(str.size() == 0)
216 {
217 sz_ucs_FieldValue[0] = 0;
218 return _setValue(sz_ucs_FieldValue);
219 }
220 UT_sint32 i = 0;
221 bool bStop = false;
222
223 for(i=0; (i<FPFIELD_MAX_LENGTH) && !bStop; i++)
224 {
225 sz_ucs_FieldValue[i] = static_cast<UT_UCSChar>(str[i]);
226 if(str[i] == 0)
227 {
228 bStop = true;
229 }
230 }
231 return _setValue(sz_ucs_FieldValue);
232 }
233
_draw(dg_DrawArgs * pDA)234 void fp_FieldTOCHeadingRun::_draw(dg_DrawArgs* pDA)
235 {
236 _defaultDraw(pDA);
237 }
238
_lookupProperties(const PP_AttrProp * pSpanAP,const PP_AttrProp * pBlockAP,const PP_AttrProp * pSectionAP,GR_Graphics * pG)239 void fp_FieldTOCHeadingRun::_lookupProperties(const PP_AttrProp * pSpanAP,
240 const PP_AttrProp * pBlockAP,
241 const PP_AttrProp * pSectionAP,
242 GR_Graphics * pG)
243 {
244 fp_FieldRun::_lookupProperties (pSpanAP, pBlockAP, pSectionAP,pG) ;
245 _setLength(0);
246 }
247
248
249
250
251