home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_oth / rkrmlib2.lha / RKRM_Lib2.lha / Graphics_Libraries / Layers / clipping.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  7KB  |  239 lines

  1. /* clipping.c
  2. **
  3. ** SAS/C 5.10a
  4. ** lc -b1 -cfist -v -y clipping
  5. ** blink FROM LIB:c.o clipping.o TO clipping LIB LIB:lc.lib LIB:amiga.lib
  6.  
  7.  
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /* Force use of new variable names to help prevent errors */
  34. #define INTUI_V36_NAMES_ONLY
  35.  
  36. #include <exec/types.h>
  37. #include <intuition/intuition.h>
  38. #include <intuition/intuitionbase.h>
  39. #include <graphics/displayinfo.h>
  40.  
  41. #include <clib/exec_protos.h>
  42. #include <clib/dos_protos.h>
  43. #include <clib/intuition_protos.h>
  44. #include <clib/graphics_protos.h>
  45. #include <clib/layers_protos.h>
  46.  
  47. #include <stdio.h>
  48.  
  49. #ifdef LATTICE
  50. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  51. int chkabort(void) { return(0); }  /* really */
  52. #endif
  53.  
  54. #define MY_WIN_WIDTH  (300)
  55. #define MY_WIN_HEIGHT (100)
  56.  
  57. struct IntuitionBase *IntuitionBase;
  58. struct GfxBase *GfxBase;
  59. struct Library *LayersBase;
  60.  
  61. /*
  62. ** unclipWindow()
  63. **
  64. ** Used to remove a clipping region installed by clipWindow() or
  65. ** clipWindowToBorders(), disposing of the installed region and
  66. ** reinstalling the region removed.
  67. */
  68. void unclipWindow(struct Window *win)
  69. {
  70. struct Region     *old_region;
  71.  
  72. /* Remove any old region by installing a NULL region,
  73. ** then dispose of the old region if one was installed.
  74. */
  75. if (NULL != (old_region = InstallClipRegion(win->WLayer, NULL)))
  76.     DisposeRegion(old_region);
  77. }
  78.  
  79. /*
  80. ** clipWindow()
  81. ** Clip a window to a specified rectangle (given by upper left and
  82. ** lower right corner.)  the removed region is returned so that it
  83. ** may be re-installed later.
  84. */
  85. struct Region *clipWindow(struct Window *win,
  86.     LONG minX, LONG minY, LONG maxX, LONG maxY)
  87. {
  88. struct Region    *new_region;
  89. struct Rectangle  my_rectangle;
  90.  
  91. /* set up the limits for the clip */
  92. my_rectangle.MinX = minX;
  93. my_rectangle.MinY = minY;
  94. my_rectangle.MaxX = maxX;
  95. my_rectangle.MaxY = maxY;
  96.  
  97. /* get a new region and OR in the limits. */
  98. if (NULL != (new_region = NewRegion()))
  99.     {
  100.     if (FALSE == OrRectRegion(new_region, &my_rectangle))
  101.         {
  102.         DisposeRegion(new_region);
  103.         new_region = NULL;
  104.         }
  105.     }
  106.  
  107. /* Install the new region, and return any existing region.
  108. ** If the above allocation and region processing failed, then
  109. ** new_region will be NULL and no clip region will be installed.
  110. */
  111. return(InstallClipRegion(win->WLayer, new_region));
  112. }
  113.  
  114. /*
  115. ** clipWindowToBorders()
  116. ** clip a window to its borders.
  117. ** The removed region is returned so that it may be re-installed later.
  118. */
  119. struct Region *clipWindowToBorders(struct Window *win)
  120. {
  121. return(clipWindow(win, win->BorderLeft, win->BorderTop,
  122.     win->Width - win->BorderRight - 1, win->Height - win->BorderBottom - 1));
  123. }
  124.  
  125. /*
  126. ** Wait for the user to select the close gadget.
  127. */
  128. VOID wait_for_close(struct Window *win)
  129. {
  130. struct IntuiMessage *msg;
  131. SHORT done;
  132.  
  133. done = FALSE;
  134. while (FALSE == done)
  135.     {
  136.     /* we only have one signal bit, so we do not have to check which
  137.     ** bit broke the Wait().
  138.     */
  139.     Wait(1L << win->UserPort->mp_SigBit);
  140.  
  141.     while ( (FALSE == done) &&
  142.             (NULL != (msg = (struct IntuiMessage *)GetMsg(win->UserPort))))
  143.         {
  144.         /* use a switch statement if looking for multiple event types */
  145.         if (msg->Class == IDCMP_CLOSEWINDOW)
  146.             done = TRUE;
  147.  
  148.         ReplyMsg((struct Message *)msg);
  149.         }
  150.     }
  151. }
  152.  
  153. /*
  154. ** Simple routine to blast all bits in a window with color three to show
  155. ** where the window is clipped.  After a delay, flush back to color zero
  156. ** and refresh the window borders.
  157. */
  158. VOID draw_in_window(struct Window *win, UBYTE *message)
  159. {
  160. printf("%s...", message); fflush(stdout);
  161. SetRast(win->RPort, 3);
  162. Delay(200);
  163. SetRast(win->RPort, 0);
  164. RefreshWindowFrame(win);
  165. printf("done\n");
  166. }
  167.  
  168.  
  169. /*
  170. ** Show drawing into an unclipped window, a window clipped to the
  171. ** borders and a window clipped to a random rectangle.  It is possible
  172. ** to clip more complex shapes by AND'ing, OR'ing and exclusive-OR'ing
  173. ** regions and rectangles to build a user clip region.
  174. **
  175. ** This example assumes that old regions are not going to be re-used,
  176. ** so it simply throws them away.
  177. */
  178. VOID clip_test(struct Window *win)
  179. {
  180. struct Region    *old_region;
  181.  
  182. draw_in_window(win,"Window with no clipping");
  183.  
  184. /* if the application has never installed a user clip region,
  185. ** then old_region will be NULL here.  Otherwise, delete the
  186. ** old region (you could save it and re-install it later...)
  187. */
  188. if (NULL != (old_region = clipWindowToBorders(win)))
  189.     DisposeRegion(old_region);
  190. draw_in_window(win,"Window clipped to window borders");
  191. unclipWindow(win);
  192.  
  193. /* here we know old_region will be NULL, as that is what we
  194. ** installed with unclipWindow()...
  195. */
  196. if (NULL != (old_region = clipWindow(win,20,20,100,50)))
  197.     DisposeRegion(old_region);
  198. draw_in_window(win,"Window clipped from (20,20) to (100,50)");
  199. unclipWindow(win);
  200.  
  201. wait_for_close(win);
  202. }
  203.  
  204.  
  205.  
  206. /*
  207. ** Open and close resources, call the test routine when ready.
  208. */
  209. VOID main(int argc, char **argv)
  210. {
  211. struct Window *win;
  212.  
  213. if (NULL != (IntuitionBase =
  214.         (struct IntuitionBase *)OpenLibrary("intuition.library",37)))
  215.     {
  216.     if (NULL != (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",37)))
  217.         {
  218.         if (NULL != (LayersBase = OpenLibrary("layers.library",37)))
  219.             {
  220.             if (NULL != (win = OpenWindowTags(NULL,
  221.                                 WA_Width,       MY_WIN_WIDTH,
  222.                                 WA_Height,      MY_WIN_HEIGHT,
  223.                                 WA_IDCMP,       IDCMP_CLOSEWINDOW,
  224.                                 WA_CloseGadget, TRUE,
  225.                                 WA_DragBar,     TRUE,
  226.                                 WA_Activate,    TRUE,
  227.                                 TAG_END)))
  228.                 {
  229.                 clip_test(win);
  230.  
  231.                 CloseWindow(win);
  232.                 }
  233.             CloseLibrary(LayersBase);
  234.             }
  235.         CloseLibrary((struct Library *)GfxBase);
  236.         }
  237.     CloseLibrary((struct Library *)IntuitionBase);
  238.     }
  239. }