home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / clipping.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  6.0 KB  |  219 lines

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