home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / toolkt21 / c / samples / tp / colblk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  5.3 KB  |  177 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)colblk.c 1.3 1/22/92 16:09:11 [1/26/92] (c)IBM Corp. 1992";
  3. #endif
  4.  
  5. /*
  6.  * This class is adapted from the book
  7.  *   Class Construction in C and C++, Object Oriented Fundamentals
  8.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  9.  * Reprinted with permission.
  10.  */
  11.  
  12. #define columnBlock_Class_Source
  13. #include "colblk.ih"
  14. #include "textline.h"
  15.  
  16. #define SPACE_BETWEEN 3
  17. #define HEADER_SPACE 3
  18. #define FOOTER_SPACE 3
  19.  
  20. /* ************************************************************ */
  21. SOM_Scope void SOMLINK cbInit(columnBlock * somSelf,
  22.                    int newColumns,
  23.                    int newHeight,
  24.                    int newWidth)
  25. {
  26.     columnBlockData *somThis = columnBlockGetData(somSelf);
  27.     int n;
  28.     linkedList *newBlock;
  29.     columnBlockMethodDebug("columnBlock", "cbInit");
  30.  
  31.     _columnWidth = newWidth - ((newColumns - 1) * SPACE_BETWEEN);
  32.     _columnWidth = _columnWidth / newColumns;
  33.  
  34.     _llSetMax(somSelf, newColumns);
  35.     for (n = 0; n < newColumns; n++) {
  36.     newBlock = linkedListNew();
  37.     _llSetMax(newBlock, newHeight - HEADER_SPACE - FOOTER_SPACE);
  38.     parent_llAddTail(somSelf, (baseType *) newBlock);
  39.     }
  40.     _llHead(somSelf);
  41. }
  42.  
  43. /* ************************************************************ */
  44. SOM_Scope int SOMLINK cbGetColumnWidth(columnBlock * somSelf)
  45. {
  46.     columnBlockData *somThis = columnBlockGetData(somSelf);
  47.     columnBlockMethodDebug("columnBlock", "cbGetColumnWidth");
  48.     return _columnWidth;
  49. }
  50.  
  51. /* ************************************************************ */
  52. SOM_Scope int SOMLINK cbLinesLeft(columnBlock * somSelf)
  53. {
  54.     columnBlockData *somThis = columnBlockGetData(somSelf);
  55.     linkedList *thisBlock;
  56.     columnBlockMethodDebug("columnBlock", "cbLinesLeft");
  57.  
  58.     thisBlock = (linkedList *) _llRetrieve(somSelf);
  59.     return (_llLeft(thisBlock) || !_llIsTail(somSelf)) ? 1 : 0;
  60. }
  61.  
  62. /* ************************************************************ */
  63. SOM_Scope baseType *SOMLINK llAddTail(columnBlock * somSelf,
  64.                        baseType * newLine)
  65. {
  66.     columnBlockData *somThis = columnBlockGetData(somSelf);
  67.     linkedList *thisBlock;
  68.     columnBlockMethodDebug("columnBlock", "llAddTail");
  69.  
  70.     thisBlock = (linkedList *) _llRetrieve(somSelf);
  71.     if (!thisBlock)
  72.     return (baseType *) NULL;
  73.     if (!_llLeft(thisBlock)) {
  74.     if (!(thisBlock = (linkedList *) _llNext(somSelf))) {
  75.         return (baseType *) NULL;
  76.     }
  77.     }
  78.     return _llAddTail(thisBlock, newLine);
  79. }
  80.  
  81. /* ************************************************************ */
  82. SOM_Scope void SOMLINK print(columnBlock * somSelf,
  83.                   FILE * output)
  84. {
  85. /* Set up.
  86.    ------- */
  87.     columnBlockData *somThis = columnBlockGetData(somSelf);
  88.     int n, blockLength, blankLines;
  89.     linkedList *thisBlock;
  90.     textLine *thisLine;
  91.     columnBlockMethodDebug("columnBlock", "print");
  92.  
  93. /* Determine number of lines in typical block.
  94.    ------------------------------------------- */
  95.     thisBlock = (linkedList *) _llHead(somSelf);
  96.     blockLength = _llLength(thisBlock);
  97.     blankLines = _llLeft(thisBlock);
  98.  
  99. /* For each horizontal line...
  100.    --------------------------- */
  101.     for (n = 0; n < blockLength; n++) {
  102.  
  103. /*     For each text block...
  104.        ---------------------- */
  105.     thisBlock = (linkedList *) _llHead(somSelf);
  106.     while (thisBlock) {
  107.  
  108. /*      Print the column line.
  109.       ---------------------- */
  110.         thisLine = (textLine *) _llHead(thisBlock);
  111.         if (thisLine) {
  112.         _print(thisLine, output);
  113.         _llFreeContents(thisLine);
  114.         _cbPrintBlanks(somSelf, output, _tlCharsLeft(thisLine));
  115.         }
  116.         else {
  117.         _cbPrintBlanks(somSelf, output, _cbGetColumnWidth(somSelf));
  118.         }
  119.         _llRemoveHead(thisBlock);
  120.  
  121. /*      Print space between columns.
  122.       ---------------------------- */
  123.         _cbPrintBlanks(somSelf, output, SPACE_BETWEEN);
  124.  
  125. /*      Move to next block.
  126.       ------------------- */
  127.         thisBlock = (linkedList *) _llNext(somSelf);
  128.     }
  129.     fprintf(output, "\n");
  130.     }
  131. /* Print blank lines.
  132.    ------------------ */
  133.     for (n = 0; n < blankLines; n++) {
  134.     fprintf(output, "\n");
  135.     }
  136.  
  137. }
  138.  
  139. /* ************************************************************ */
  140. SOM_Scope void SOMLINK somInit(columnBlock * somSelf)
  141. {
  142.     columnBlockData *somThis = columnBlockGetData(somSelf);
  143.     columnBlockMethodDebug("columnBlock", "somInit");
  144.     parent_somInit(somSelf);
  145. }
  146.  
  147. /* ************************************************************ */
  148. SOM_Scope void SOMLINK somUninit(columnBlock * somSelf)
  149. {
  150.     columnBlockData *somThis = columnBlockGetData(somSelf);
  151.     columnBlockMethodDebug("columnBlock", "somUninit");
  152.     parent_somUninit(somSelf);
  153. }
  154.  
  155. /* ************************************************************ */
  156. SOM_Scope void SOMLINK somDumpSelfInt(columnBlock * somSelf,
  157.                        int level)
  158. {
  159.     columnBlockData *somThis = columnBlockGetData(somSelf);
  160.     columnBlockMethodDebug("columnBlock", "somDumpSelfInt");
  161.     parent_somDumpSelfInt(somSelf, level);
  162. }
  163.  
  164. /* ************************************************************ */
  165. SOM_Scope void SOMLINK cbPrintBlanks(columnBlock * somSelf,
  166.                       FILE * output,
  167.                       int nblanks)
  168. {
  169.     columnBlockData *somThis = columnBlockGetData(somSelf);
  170.     int n;
  171.     columnBlockMethodDebug("columnBlock", "cbPrintBlanks");
  172.  
  173.     for (n = 0; n < nblanks; n++) {
  174.     putc(' ', output);
  175.     }
  176. }
  177.