home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Directory / window.c < prev   
Encoding:
Text File  |  1993-02-03  |  8.6 KB  |  264 lines  |  [TEXT/KAHL]

  1. //-- Window.c --//
  2.  
  3. // Generic window management routines.
  4.  
  5. // The window management routines to create and distroy drawing windows and their data
  6. // structures, to redraw windows, and to append lines to the window line buffer, as well
  7. // to managing scrolling and such.  This is the total of all the routines to manage windows.
  8. // This also contains the code to manage the project window as such. */
  9.  
  10. #include <stdio.h>
  11. #include "struct.h"
  12. #include "res.h"
  13. #include "error.h"
  14.  
  15. //-- GLOBALS --//
  16.  
  17. //-- The globals used for the window structure.
  18.  
  19. struct DrawWindow *drawList;
  20. static struct DrawWindow *whichScroll;    // What window is being scrolled?
  21. Rect openSize;
  22. extern unsigned char MultiWidget;        // State flags.
  23.  
  24. //-- CODE --//
  25.  
  26. //-- The different routines
  27.  
  28. //-- Basic allocation/deallocation of windows.
  29.  
  30. //-- AllocateWindow --//
  31.  
  32. //-- This allocs a new window.  
  33. //-- WARNING:  Errors are returned using the 'Throw' mechanism.
  34.  
  35. struct DrawWindow *AllocateWindow()
  36. {
  37.     int i;
  38.     
  39.     for (i = 0; i < MAXWINDOWS; i++) if (drawList[i].inuse == 0) break;
  40.     if (i == MAXWINDOWS) Throw(OUTWINDOWS);
  41.  
  42.     drawList[i].inuse = 1;
  43.     openSize.top = 38 + i * 20;
  44.     openSize.left = 30 - i * 3;
  45.     openSize.right = -i * 3;
  46.     openSize.bottom = - 10;
  47.     if (MultiWidget) openSize.right -= 75;
  48.     return &(drawList[i]);
  49. }
  50.  
  51.  
  52. //-- FreeWind
  53.  
  54. // This frees a window.  Only do this AFTER disposing of the window.
  55.  
  56. FreeWind(w)
  57. struct DrawWindow *w;
  58. {
  59.     w->inuse = 0;
  60. }
  61.  
  62. //-- Basic window math. --//
  63.  
  64. //-- CalcScroll --//
  65.  
  66. // Given a window, this computes where the scroll bars are to go..
  67.  
  68. CalcScroll(w,x,y)
  69. WindowPtr w;
  70. Rect *x,*y;
  71. {
  72.     Rect r;
  73.     
  74.     r = w->portRect;
  75.     x->top = r.bottom - 15;
  76.     x->bottom = r.bottom + 1;
  77.     x->right = r.right - 14;
  78.     x->left = -1;
  79.     
  80.     y->top = -1;
  81.     y->bottom = r.bottom - 14;
  82.     y->left = r.right - 15;
  83.     y->right = r.right + 1;
  84. }
  85.  
  86.  
  87. //-- CalcClip --//
  88.  
  89. // Calculate the clip area of the window (where to draw, which doesn't include scroll
  90. // bars.
  91.  
  92. CalcClip(w,c)
  93. WindowPtr w;
  94. Rect *c;
  95. {
  96.     Rect r;
  97.  
  98.     r = w->portRect;
  99.     c->top = r.top;
  100.     c->left = r.left;
  101.     c->right = r.right - 15;
  102.     c->bottom = r.bottom - 15;
  103. }
  104.  
  105. //-- Window management. --//
  106.  
  107.  
  108. //-- NewPlan --//
  109.  
  110. // Open a new window.
  111.  
  112. struct DrawWindow *NewPlan(name)
  113. short name;                                /* vRefNum of this object */
  114. {
  115.     int i;
  116.     struct DrawWindow *w;
  117.     short step;
  118.     GrafPtr foo;
  119.     Rect r;
  120.     Rect s;
  121.     int t;
  122.  
  123. // Step 1:  Prepare for disaster.
  124.     
  125.     step = 1;
  126.     if (i = Catch()) {
  127.     
  128. // According to the step number, do the appropriate shutting down.
  129.  
  130.         switch (step) {
  131.             case 2:
  132.                 CloseWindow(w);
  133.                 FreeWind(w);
  134.             case 1:
  135.                 break;
  136.         }
  137.         
  138. // Post the error message, and return.
  139.  
  140.         PostError(i);
  141.         return NULL;
  142.     }
  143.     
  144. // Step 2:  Allocate and open the window.
  145.  
  146.     w = AllocateWindow();
  147.     GetWMgrPort(&foo);
  148.     r = foo->portRect;
  149.     r.right += openSize.right;
  150.     r.bottom += openSize.bottom;
  151.     r.top = openSize.top;
  152.     r.left = openSize.left;
  153.     InsetRect(&r,4,4);
  154.     NewWindow(w,&r,"\pUntitled",1,8,(char *)-1,CanEject(name),0L);
  155.     step = 2;
  156.     
  157.     /*
  158.      *    Step 3:  Initialize different data structures
  159.      */
  160.  
  161.     w->state = 0;                        /* Directories only */
  162.     w->w.windowKind = WK_PLAN;
  163.     ComputePict(name,w);
  164.  
  165.     CalcScroll(w,&r,&s);
  166.     if (NULL == (w->yScroll = NewControl(w,&s,"",1,0,0,0,16,0L))) 
  167.         Throw(OUTMEM);
  168.     
  169.     t = (GetHandleSize(w->data) / sizeof(struct DirectData)) - 1;
  170.     if (t < 0) t = 0;
  171.     SetCtlMax(w->yScroll,t);
  172.  
  173. // All done.  Return.
  174.     
  175.     Uncatch();
  176.     return w;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. //-- ClosePlan --//
  183.  
  184. // What to do when the 'close' option is selected for a particular plan.
  185.  
  186.  
  187. int ClosePlan(w)
  188. struct DrawWindow *w;
  189. {
  190.  
  191. // Dispose of the window and quit.
  192.  
  193.     if (!CanEject(w->vRefNum)) return 0;    /* Failure */
  194.     Eject("",w->vRefNum);
  195.     UnmountVol("",w->vRefNum);
  196.     DisposHandle(w->data);
  197.     CloseWindow(w);
  198.     FreeWind(w);
  199.     return 1;                                /* Success */
  200. }
  201.  
  202.  
  203.  
  204. //-- UpdatePlan --//
  205.  
  206. // This updates the plan (redraws the contents of the window).
  207.  
  208. UpdatePlan(w)
  209. struct DrawWindow *w;
  210. {
  211.     DrawGrowIcon(w);
  212.     DrawControls(w);
  213.     DrawPlanWind(w);
  214. }
  215.  
  216.  
  217.  
  218. //-- DrawPlanWind --//
  219.  
  220. // Actually draw the contents of this drawing window.
  221.  
  222. DrawPlanWind(w)
  223. struct DrawWindow *w;
  224. {
  225.     int x,y;
  226.     Rect r;
  227.     RgnHandle rgn;
  228.     Rect s;
  229.     long l,i;
  230.     struct DirectData *ptr;
  231.     short ind;
  232.     
  233. //-- Properly initialize clipping for redrawing the screen
  234.  
  235.     SetPort(w);
  236.     CalcClip(w,&r);
  237.     rgn = NewRgn();
  238.     GetClip(rgn);
  239.     ClipRect(&r);
  240.     
  241. //-- Draw the contents of the screen
  242.  
  243.     HLock(w->data);
  244.     ptr = *(w->data);
  245.     TextFont(4);
  246.     TextSize(9);
  247.     y = GetCtlValue(w->yScroll);
  248.     l = GetHandleSize(w->data) / sizeof(struct DirectData);
  249.     for (i = y, x = 12; i < l; i++, x += 12) {
  250.         for (ind = 0; ind < ptr[i].indent; ind++) {
  251.             MoveTo(ind*12+15,x-10);
  252.             Line(0,12);
  253.         }
  254.         Movee 4:
  255.             case 5:
  256.             case 6:
  257.                 SetBtn(dlog,4+nv,0);
  258.                 nv = x - 4;
  259.                 SetBtn(dlog,4+nv,1);
  260.                 break;
  261.         }
  262.     }
  263. }
  264.