home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit3.dms / CBMDevKit3.adf / intuition / makevisible.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  5.6 KB  |  241 lines

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