home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / xlate / tblprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.6 KB  |  215 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*
  19. ** This file contains a couple of routines which might be useful
  20. ** in a cross platform way for dealing with the calculations
  21. ** involved in table printing.
  22. */
  23.  
  24. #include "xlate_i.h"
  25.  
  26. PRIVATE XP_Bool
  27. too_big(MWContext* cx, int size)
  28. {
  29.   return size >= (cx->prInfo->page_height/8)*7;
  30. }
  31.  
  32. /*
  33. ** Called by the FE_Display* functions with the Y span of the item to be
  34. ** displayed.  Used to find page breaks and to reject items which are
  35. ** not on the current page.
  36. **
  37. ** Returns:
  38. **     TRUE means display the item
  39. **     FALSE means do not display the item
  40. **
  41. ** Requires:
  42. **    cx->prInfo to point to a record which contains the following
  43. **    information:
  44. **
  45. **        page_topy:    DOC Y coord of top of this page.
  46. **        page_break:    During layout, initialize this to
  47. **                page_topy+page_height;
  48. **        page_height:    The physical height of a piece of paper,
  49. **                minus top and bottom margins.
  50. **        phase:        XL_<mumble>_PHASE
  51. **
  52. **    If invoked with XP_LayoutForPrint and XP_DrawForPrint, this
  53. **    will be done automatically.
  54. */
  55.  
  56. PUBLIC XP_Bool
  57. XP_CheckElementSpan(MWContext*cx, int top, int height)
  58. {
  59.   int bottom;
  60.  
  61.   /*
  62.   ** During load phase, or if this doesn't appear to be a print
  63.   ** related call, we do nothing.
  64.   */
  65.   
  66.   if (cx->prInfo == NULL || cx->prInfo->phase == 0)
  67.       return TRUE;
  68.       
  69.   if (cx->prInfo->phase == XL_LOADING_PHASE)
  70.     return FALSE;
  71.  
  72.   bottom = top + height - 1;
  73.  
  74.   /*
  75.   ** Layout phase is used purely to compute the page breaks for each page.
  76.   ** we always return FALSE, but look carefully at items which span the
  77.   ** page boundry and pick for the actual page break the y minimum of
  78.   ** "acceptable" items which span the physical page boundry.
  79.   */
  80.   if (cx->prInfo->phase == XL_LAYOUT_PHASE)
  81.   {
  82.     if (!too_big(cx, height)
  83.     && top >= cx->prInfo->page_topy
  84.     && top < cx->prInfo->page_topy + cx->prInfo->page_height
  85.     && bottom >= cx->prInfo->page_topy + cx->prInfo->page_height)
  86.     {
  87.       /*
  88.       ** This item won't fit on this page, and is small enough to consider
  89.       ** breaking a page for.
  90.       */
  91.       if (top < cx->prInfo->page_break) {
  92.     cx->prInfo->page_break = top;
  93.       }
  94.     }
  95.     return FALSE;
  96.   }
  97.   
  98.   /*
  99.   ** Which leaves only the actual generation phase.
  100.   */
  101.  
  102.   /*
  103.   ** Trivial reject of items which aren't on the page at all
  104.   */
  105.   if (bottom < cx->prInfo->page_topy || top > cx->prInfo->page_break) {
  106.     return FALSE;
  107.   }
  108.  
  109.   /*
  110.   ** Complete acceptance of items which fall entirely on the current page
  111.   */
  112.   if (top >= cx->prInfo->page_topy && bottom <= cx->prInfo->page_break) {
  113.     return TRUE;
  114.   }
  115.  
  116.   /*
  117.   ** Also print items which overhang the soft boundry, but not the hard
  118.   ** page boundry.
  119.   */
  120.   if (bottom < cx->prInfo->page_topy + cx->prInfo->page_height)
  121.   {
  122.     return TRUE;
  123.   }
  124.  
  125.   /*
  126.   ** It straddles the page break.  If it's big, then print it, and let it
  127.   ** get chopped off.  If it is small, it will display on the next page.
  128.   */
  129.   return too_big(cx, height);
  130. }
  131.  
  132. /*
  133. ** Call to set up the page counting fields in the printinfo structure
  134. */
  135. PUBLIC void
  136. XP_InitializePrintInfo(MWContext *cx)
  137. {
  138.   if (cx->prInfo == NULL) {
  139.     PrintInfo *p;
  140.  
  141.     p = XP_NEW_ZAP(PrintInfo);
  142.     if (!p) return;
  143.     cx->prInfo = p;
  144.   }
  145.   cx->prInfo->n_pages = 0;
  146.   cx->prInfo->phase = XL_DRAW_PHASE;
  147. }
  148.  
  149. PUBLIC void
  150. XP_CleanupPrintInfo(MWContext *cx)
  151. {
  152.   PrintInfo *p = cx->prInfo;
  153.  
  154.   if (p) {
  155.     if (p->pages) {
  156.       free(p->pages);
  157.       p->pages = NULL;
  158.     }
  159.     free(p);
  160.     cx->prInfo = NULL;
  161.   }
  162. }
  163.  
  164. PUBLIC void
  165. XP_LayoutForPrint(MWContext *cx, int32 doc_height)
  166. {
  167.   int32 y;
  168.   int saveit;
  169.  
  170.   saveit = cx->prInfo->phase;
  171.   cx->prInfo->phase = XL_LAYOUT_PHASE;
  172.   y = 0;
  173.   while (y < doc_height) {
  174.     cx->prInfo->page_topy = y;
  175.     cx->prInfo->page_break = y + cx->prInfo->page_height;
  176.     LO_RefreshArea(cx, 0, y,
  177.                cx->prInfo->page_width, cx->prInfo->page_height);
  178.     if (cx->prInfo->n_pages >= cx->prInfo->pt_size) {
  179.       cx->prInfo->pt_size += 100;
  180.       cx->prInfo->pages = (PageBreaks*)realloc(
  181.                   cx->prInfo->pages,
  182.                   cx->prInfo->pt_size * sizeof (PageBreaks));
  183.         if ( !cx->prInfo->pages )
  184.         {
  185.             extern int MK_OUT_OF_MEMORY;
  186.             cx->prInfo->n_pages = MK_OUT_OF_MEMORY;
  187.             return;
  188.         }
  189.     }
  190.     cx->prInfo->pages[cx->prInfo->n_pages].y_top = y;
  191.     cx->prInfo->pages[cx->prInfo->n_pages].y_break = cx->prInfo->page_break-1;
  192.     cx->prInfo->n_pages++;
  193.     y = cx->prInfo->page_break;
  194.   }
  195.   cx->prInfo->phase = saveit;
  196. }
  197.  
  198. PUBLIC void
  199. XP_DrawForPrint(MWContext *cx, int page)
  200. {
  201.   int32 t;
  202.   int saveit;
  203.  
  204.   saveit = cx->prInfo->phase;
  205.   cx->prInfo->phase = XL_DRAW_PHASE;
  206.  
  207.   t = cx->prInfo->pages[page].y_top;
  208.   cx->prInfo->page_topy = t;
  209.   cx->prInfo->page_break = cx->prInfo->pages[page].y_break;
  210.   LO_RefreshArea(cx,
  211.       0, t,
  212.       cx->prInfo->page_width, cx->prInfo->page_height);
  213.   cx->prInfo->phase = saveit;
  214. }
  215.