1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/************************************************************************/
/* */
/* Get headers and footers for a certain page/position. */
/* */
/************************************************************************/
# include "tedConfig.h"
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <appDebugon.h>
# include "docBuf.h"
/************************************************************************/
/* */
/* Determine what header/footer applies for a certain page. */
/* */
/************************************************************************/
int docWhatPageHeader( ExternalItem ** pEi,
BufferItem * bodySectBi,
int page,
const DocumentProperties * dp )
{
const SectionProperties * sp= &(bodySectBi->biSectProperties);
if ( page == bodySectBi->biTopPosition.lpPage && sp->spHasTitlePage )
{
*pEi= &(bodySectBi->biSectFirstPageHeader);
return DOCinFIRST_HEADER;
}
if ( ! dp->dpHasFacingPages )
{
*pEi= &(bodySectBi->biSectHeader);
return DOCinSECT_HEADER;
}
if ( page % 2 )
{
*pEi= &(bodySectBi->biSectLeftPageHeader);
return DOCinLEFT_HEADER;
}
else{
*pEi= &(bodySectBi->biSectRightPageHeader);
return DOCinRIGHT_HEADER;
}
}
int docWhatPageFooter( ExternalItem ** pEi,
BufferItem * bodySectBi,
int page,
const DocumentProperties * dp )
{
const SectionProperties * sp= &(bodySectBi->biSectProperties);
if ( page == bodySectBi->biTopPosition.lpPage && sp->spHasTitlePage )
{
*pEi= &(bodySectBi->biSectFirstPageFooter);
return DOCinFIRST_FOOTER;
}
if ( ! dp->dpHasFacingPages )
{
*pEi= &(bodySectBi->biSectFooter);
return DOCinSECT_FOOTER;
}
if ( page % 2 )
{
*pEi= &(bodySectBi->biSectLeftPageFooter);
return DOCinLEFT_FOOTER;
}
else{
*pEi= &(bodySectBi->biSectRightPageFooter);
return DOCinRIGHT_FOOTER;
}
}
/************************************************************************/
/* */
/* Return a particular header or footer by scope. */
/* */
/************************************************************************/
ExternalItem * docSectionHeaderFooter( BufferItem * bodySectBi,
int which )
{
switch( which )
{
case DOCinBODY:
LDEB(which); return (ExternalItem *)0;
case DOCinSECT_HEADER:
return &(bodySectBi->biSectHeader);
case DOCinFIRST_HEADER:
return &(bodySectBi->biSectFirstPageHeader);
case DOCinLEFT_HEADER:
return &(bodySectBi->biSectLeftPageHeader);
case DOCinRIGHT_HEADER:
return &(bodySectBi->biSectRightPageHeader);
case DOCinSECT_FOOTER:
return &(bodySectBi->biSectFooter);
case DOCinFIRST_FOOTER:
return &(bodySectBi->biSectFirstPageFooter);
case DOCinLEFT_FOOTER:
return &(bodySectBi->biSectLeftPageFooter);
case DOCinRIGHT_FOOTER:
return &(bodySectBi->biSectRightPageFooter);
default:
LDEB(which); return (ExternalItem *)0;
}
}
int docSectionHeaderFooterFirstPage(
int * pExists,
const BufferItem * bodySectBi,
int which,
const DocumentProperties * dp )
{
const SectionProperties * sp= &(bodySectBi->biSectProperties);
int topPage= bodySectBi->biTopPosition.lpPage;
int page;
if ( bodySectBi->biNumberInParent > 0 )
{
const BufferItem * prevBi;
prevBi= bodySectBi->biParent->biChildren[
bodySectBi->biNumberInParent- 1];
if ( prevBi->biBelowPosition.lpPage >= topPage )
{ return -1; }
}
switch( which )
{
case DOCinBODY:
LDEB(which); return -1;
case DOCinSECT_HEADER:
case DOCinSECT_FOOTER:
if ( dp->dpHasFacingPages )
{ return -1; }
if ( sp->spHasTitlePage )
{ page= topPage+ 1; }
else{ page= topPage; }
break;
case DOCinFIRST_HEADER:
case DOCinFIRST_FOOTER:
if ( ! sp->spHasTitlePage )
{ return -1; }
page= topPage;
break;
case DOCinLEFT_HEADER:
case DOCinLEFT_FOOTER:
if ( ! dp->dpHasFacingPages )
{ return -1; }
if ( topPage % 2 )
{
if ( sp->spHasTitlePage )
{ page= topPage+ 2; }
else{ page= topPage; }
}
else{ page= topPage+ 1; }
break;
case DOCinRIGHT_HEADER:
case DOCinRIGHT_FOOTER:
if ( ! dp->dpHasFacingPages )
{ return -1; }
if ( ! ( topPage % 2 ) )
{
if ( sp->spHasTitlePage )
{ page= topPage+ 2; }
else{ page= topPage; }
}
else{ page= topPage+ 1; }
break;
default:
LDEB(which); return -1;
}
*pExists= page <= bodySectBi->biBelowPosition.lpPage;
return page;
}
/************************************************************************/
/* */
/* Return a particular separator of notice by scope. */
/* */
/************************************************************************/
ExternalItem * docDocumentNoteSeparator(
BufferDocument * bd,
int which )
{
switch( which )
{
case DOCinFTNSEP:
return &(bd->bdEiFtnsep);
case DOCinFTNSEPC:
return &(bd->bdEiFtnsepc);
case DOCinFTNCN:
return &(bd->bdEiFtncn);
case DOCinAFTNSEP:
return &(bd->bdEiAftnsep);
case DOCinAFTNSEPC:
return &(bd->bdEiAftnsepc);
case DOCinAFTNCN:
return &(bd->bdEiAftncn);
default:
LDEB(which); return (ExternalItem *)0;
}
}
/************************************************************************/
/* */
/* Get the external item that an item is contained in. */
/* */
/************************************************************************/
int docGetRootOfSelectionScope( ExternalItem ** pEi,
BufferItem ** pSelRootBi,
BufferItem ** pBodySectBi,
BufferDocument * bd,
const SelectionScope * ss )
{
BufferItem * docBi= &(bd->bdItem);
BufferItem * bodySectBi;
BufferItem * selRootBi;
ExternalItem * ei;
int noteIndex;
DocumentNote * dn;
switch( ss->ssInExternalItem )
{
case DOCinBODY:
selRootBi= docBi;
bodySectBi= (BufferItem *)0;
ei= (ExternalItem *)0;
break;
case DOCinSECT_HEADER:
case DOCinFIRST_HEADER:
case DOCinLEFT_HEADER:
case DOCinRIGHT_HEADER:
case DOCinSECT_FOOTER:
case DOCinFIRST_FOOTER:
case DOCinLEFT_FOOTER:
case DOCinRIGHT_FOOTER:
bodySectBi= docBi->biChildren[ss->ssSectNrExternalTo];
if ( ! bodySectBi || bodySectBi->biLevel != DOClevSECT )
{ XDEB(bodySectBi); return -1; }
ei= docSectionHeaderFooter( bodySectBi, ss->ssInExternalItem );
if ( ! ei )
{ LXDEB(ss->ssInExternalItem,ei); return -1; }
selRootBi= ei->eiItem;
break;
case DOCinFOOTNOTE:
case DOCinENDNOTE:
noteIndex= ss->ssNoteArrayIndex;
if ( noteIndex < 0 || noteIndex >= bd->bdNoteCount )
{
LLDEB(noteIndex,bd->bdNoteCount);
return -1;
}
dn= bd->bdNotes+ noteIndex;
ei= &(dn->dnExternalItem);
selRootBi= ei->eiItem;
if ( dn->dnSectNr < 0 || dn->dnSectNr >= docBi->biChildCount )
{ LLDEB(dn->dnSectNr,docBi->biChildCount); return -1; }
bodySectBi= docBi->biChildren[dn->dnSectNr];
if ( ! bodySectBi || bodySectBi->biLevel != DOClevSECT )
{ XDEB(bodySectBi); return -1; }
break;
case DOCinFTNSEP:
case DOCinFTNSEPC:
case DOCinFTNCN:
case DOCinAFTNSEP:
case DOCinAFTNSEPC:
case DOCinAFTNCN:
ei= docDocumentNoteSeparator( bd, ss->ssInExternalItem );
if ( ! ei )
{ LXDEB(ss->ssInExternalItem,ei); return -1; }
selRootBi= ei->eiItem;
bodySectBi= (BufferItem *)0;
break;
default:
LDEB(ss->ssInExternalItem);
return -1;
}
*pEi= ei;
*pSelRootBi= selRootBi;
*pBodySectBi= bodySectBi;
return 0;
}
int docGetExternalItem( ExternalItem ** pEi,
BufferItem ** pBodySectBi,
BufferDocument * bd,
BufferItem * bi )
{
BufferItem * selRootBi;
BufferItem * selSectBi;
BufferItem * bodySectBi;
ExternalItem * ei;
SelectionScope * ss;
while( bi && bi->biLevel != DOClevSECT )
{ bi= bi->biParent; }
if ( ! bi )
{ XDEB(bi); return -1; }
selSectBi= bi;
ss= &(selSectBi->biSectSelectionScope);
if ( docGetRootOfSelectionScope( &ei, &selRootBi, &bodySectBi, bd, ss ) )
{ LDEB(1); return -1; }
if ( ss->ssInExternalItem == DOCinBODY )
{ bodySectBi= selSectBi; }
*pEi= ei;
*pBodySectBi= bodySectBi;
return 0;
}
/************************************************************************/
/* */
/* What BufferItem is the common root of the selection. */
/* */
/************************************************************************/
BufferItem * docGetSelectionRoot(
ExternalItem ** pEi,
BufferItem ** pBodySectBi,
BufferDocument * bd,
const DocumentSelection * ds )
{
BufferItem * bi1= ds->dsBegin.dpBi;
BufferItem * bi2= ds->dsEnd.dpBi;
ExternalItem * ei= (ExternalItem *)0;
BufferItem * selRootBi= (BufferItem *)0;
BufferItem * bodySectBi= (BufferItem *)0;
if ( ! bi1 || ! bi2 )
{ XXDEB(bi1,bi2); return (BufferItem *)0; }
while( bi1 && bi1->biLevel > bi2->biLevel )
{ bi1= bi1->biParent; }
if ( ! bi1 )
{
XDEB(bi1);
LLDEB(ds->dsBegin.dpBi->biLevel,ds->dsEnd.dpBi->biLevel);
/*
return (BufferItem *)0;
*/
}
while( bi2 && bi2->biLevel > bi1->biLevel )
{ bi2= bi2->biParent; }
while( bi1 &&
bi2 &&
bi1 != bi2 )
{ bi1= bi1->biParent; bi2= bi2->biParent; }
if ( ! bi1 || ! bi2 )
{ XXDEB(bi1,bi2); return (BufferItem *)0; }
selRootBi= bi1;
if ( selRootBi->biInExternalItem != DOCinBODY )
{
if ( docGetExternalItem( &ei, &bodySectBi, bd, bi1 ) )
{ LDEB(1); return (BufferItem *)0; }
}
if ( pEi )
{ *pEi= ei; }
if ( pBodySectBi )
{ *pBodySectBi= bodySectBi; }
return selRootBi;
}
/************************************************************************/
/* */
/* Return the Header/Footer corrsponding to 'which'. If the selection */
/* itself is in an external item, go to the section in the body to */
/* get the header or footer. */
/* */
/************************************************************************/
int docGetHeaderFooter( ExternalItem ** pEi,
BufferItem ** pBodySectBi,
const DocumentSelection * ds,
BufferDocument * bd,
int which )
{
ExternalItem * ei;
BufferItem * bodySectBi;
if ( docGetExternalItem( &ei, &bodySectBi, bd, ds->dsBegin.dpBi ) )
{ LDEB(1); return -1; }
ei= docSectionHeaderFooter( bodySectBi, which );
if ( ! ei )
{ XDEB(ei); return -1; }
*pEi= ei; *pBodySectBi= bodySectBi; return 0;
}
|