home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / ansicdemo.lha / ANSI-C / Intuition / makevisible.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.5 KB  |  239 lines

  1. /*
  2.  * makevisible.c - shows off SPOS_MAKEVISIBLE feature
  3.  *
  4.  * (c) Copyright 1992 Commodore-Amiga, Inc.  All Rights Reserved.
  5.  * Preliminary and Confidential.
  6.  * 
  7.  * Opens an oversized autoscrolling screen.  Use the mouse to draw
  8.  * a rectangle on the screen.  Then, use the mouse to scroll the screen
  9.  * anywhere you like.  Press any key to move the drawn rectangle
  10.  * into view using ScreenPosition( sc, SPOS_MAKEVISIBLE, ... ).
  11.  *
  12.  * The SPOS_MAKEVISIBLE feature can be useful to ensure certain areas
  13.  * are visible, for example the cursor of a word-processor.
  14.  *
  15.  * Press "Q" or <Esc> to exit.
  16.  *
  17.  */
  18.  
  19. /*----------------------------------------------------------------------*/
  20.  
  21. #include <stdio.h>
  22. #include <exec/types.h>
  23. #include <graphics/displayinfo.h>
  24. #include <intuition/intuition.h>
  25.  
  26. #include <pragma/exec_lib.h>
  27. #include <pragma/graphics_lib.h>
  28. #include <pragma/intuition_lib.h>
  29.  
  30. #include <graphics/gfxmacros.h>
  31.  
  32. /*----------------------------------------------------------------------*/
  33.  
  34. void error_exit(STRPTR errorstring);
  35.  
  36. /*----------------------------------------------------------------------*/
  37.  
  38. struct GfxBase *GfxBase = NULL;
  39. struct IntuitionBase *IntuitionBase = NULL;
  40. struct Screen *myscreen = NULL;
  41. struct Window *mywindow = NULL;
  42.  
  43. /*----------------------------------------------------------------------*/
  44.  
  45. UWORD pens[] =
  46. {
  47.     0, /* DETAILPEN */
  48.     1, /* BLOCKPEN    */
  49.     1, /* TEXTPEN    */
  50.     2, /* SHINEPEN    */
  51.     1, /* SHADOWPEN    */
  52.     3, /* FILLPEN    */
  53.     2, /* FILLTEXTPEN    */
  54.     0, /* BACKGROUNDPEN    */
  55.     2, /* HIGHLIGHTTEXTPEN    */
  56.  
  57.     1, /* BARDETAILPEN    */
  58.     2, /* BARBLOCKPEN    */
  59.     1, /* BARTRIMPEN    */
  60.  
  61.     ~0,
  62. };
  63.  
  64. /*----------------------------------------------------------------------*/
  65.  
  66. /*  MYDISPLAYID represents the desired mode, and should be taken from
  67.     graphics/displayinfo.h */
  68. #define MYDISPLAYID    (LORES_KEY)
  69. #define MYWIDTH    900
  70. #define MYHEIGHT 600
  71.  
  72. /*----------------------------------------------------------------------*/
  73.  
  74. main()
  75. {
  76.     BOOL terminated = FALSE;
  77.     BOOL dragging = FALSE;
  78.     struct IntuiMessage *imsg;
  79.     struct Rectangle drawn_rect, drag_rect;
  80.  
  81.     if (!( GfxBase = (struct GfxBase *)
  82.     OpenLibrary("graphics.library", 39L) ))
  83.     {
  84.     error_exit("Couldn't open Gfx V39\n");
  85.     }
  86.  
  87.     if (!( IntuitionBase = (struct IntuitionBase *)
  88.     OpenLibrary("intuition.library", 39L) ))
  89.     {
  90.     error_exit("Couldn't open Intuition V39\n");
  91.     }
  92.  
  93.     if (!(myscreen = OpenScreenTags(NULL,
  94.     SA_DisplayID, MYDISPLAYID,
  95.     SA_Overscan, OSCAN_TEXT,
  96.     /*  Other tags can go here: */
  97.     SA_Width, MYWIDTH,
  98.     SA_Height, MYHEIGHT,
  99.     SA_Depth, 2,
  100.     SA_AutoScroll, 1,
  101.     SA_Pens, pens,
  102.     SA_Title, "Draw a rectangle with the mouse.  Scroll it off-screen.  Hit a key to scroll it into view.  <Esc> to quit.",
  103.     TAG_DONE )))
  104.     {
  105.     error_exit("Couldn't open screen\n");
  106.     }
  107.     if (!( mywindow = OpenWindowTags(NULL,
  108.     WA_Borderless, TRUE,
  109.     WA_Backdrop, TRUE,
  110.     WA_IDCMP, MOUSEBUTTONS|VANILLAKEY,
  111.     WA_NoCareRefresh, TRUE,
  112.     WA_Activate, TRUE,
  113.     WA_SmartRefresh, TRUE,
  114.     WA_CustomScreen, myscreen,
  115.     TAG_DONE ) ))
  116.     {
  117.     error_exit("Couldn't open window\n");
  118.     }
  119.  
  120.     drawn_rect.MinX = 20;
  121.     drawn_rect.MinY = 20;
  122.     drawn_rect.MaxX = 150;
  123.     drawn_rect.MaxY = 100;
  124.     SetABPenDrMd( mywindow->RPort, 3, 0, COMPLEMENT );
  125.     SetOPen( mywindow->RPort, 1 );
  126.     RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  127.     drawn_rect.MaxX, drawn_rect.MaxY );
  128.  
  129.     while ( !terminated )
  130.     {
  131.     Wait( 1 << mywindow->UserPort->mp_SigBit );
  132.     while ( imsg = (struct IntuiMessage *)GetMsg( mywindow->UserPort ) )
  133.     {
  134.         switch ( imsg->Class )
  135.         {
  136.         case VANILLAKEY:
  137.         if ( ( imsg->Code == 0x1B ) || ( imsg->Code == 'q' ) || ( imsg->Code == 'Q' ) )
  138.         {
  139.             terminated = TRUE;
  140.         }
  141.         else
  142.         {
  143.             ScreenPosition( myscreen, SPOS_MAKEVISIBLE,
  144.             drawn_rect.MinX, drawn_rect.MinY,
  145.             drawn_rect.MaxX, drawn_rect.MaxY );
  146.         }
  147.         break;
  148.  
  149.         case MOUSEBUTTONS:
  150.         {
  151.             WORD mousex = imsg->MouseX;
  152.             WORD mousey = imsg->MouseY;
  153.  
  154.             if ( mousex < 0 ) mousex = 0;
  155.             if ( mousex >= myscreen->Width ) mousex = myscreen->Width - 1;
  156.             if ( mousey < 0 ) mousey = 0;
  157.             if ( mousey >= myscreen->Height ) mousey = myscreen->Height - 1;
  158.  
  159.             if ( imsg->Code == SELECTDOWN )
  160.             {
  161.             dragging = TRUE;
  162.             drag_rect.MinX = mousex;
  163.             drag_rect.MinY = mousey;
  164.             }
  165.             else if ( ( imsg->Code == SELECTUP ) && ( dragging ) )
  166.             {
  167.             dragging = FALSE;
  168.             if ( mousex > drag_rect.MinX )
  169.             {
  170.                 drag_rect.MaxX = mousex;
  171.             }
  172.             else
  173.             {
  174.                 drag_rect.MaxX = drag_rect.MinX;
  175.                 drag_rect.MinX = mousex;
  176.             }
  177.             if ( mousey > drag_rect.MinY )
  178.             {
  179.                 drag_rect.MaxY = mousey;
  180.             }
  181.             else
  182.             {
  183.                 drag_rect.MaxY = drag_rect.MinY;
  184.                 drag_rect.MinY = mousey;
  185.             }
  186.             SetOPen( mywindow->RPort, 0 );
  187.             RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  188.                 drawn_rect.MaxX, drawn_rect.MaxY );
  189.             drawn_rect = drag_rect;
  190.             SetOPen( mywindow->RPort, 1 );
  191.             RectFill( mywindow->RPort, drawn_rect.MinX, drawn_rect.MinY,
  192.                 drawn_rect.MaxX, drawn_rect.MaxY );
  193.             }
  194.         }
  195.         break;
  196.         }
  197.     }
  198.     }
  199.     error_exit(NULL);
  200. }
  201.  
  202.  
  203. /*----------------------------------------------------------------------*/
  204.  
  205. void error_exit(STRPTR errorstring)
  206.  
  207.     {
  208.     if (mywindow)
  209.     {
  210.     CloseWindow(mywindow);
  211.     }
  212.  
  213.     if (myscreen)
  214.     {
  215.     CloseScreen(myscreen);
  216.     }
  217.  
  218.     if (IntuitionBase)
  219.     {
  220.     CloseLibrary( ( struct Library *)IntuitionBase);
  221.     }
  222.  
  223.     if (GfxBase)
  224.     {
  225.     CloseLibrary( ( struct Library *)GfxBase);
  226.     }
  227.  
  228.     if (errorstring)
  229.     {
  230.     printf(errorstring);
  231.     exit(20);
  232.     }
  233.  
  234.     exit(0);
  235.     }
  236.  
  237.  
  238. /*----------------------------------------------------------------------*/
  239.