home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / intuition / windows / twowindows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  7.9 KB  |  270 lines

  1.  
  2. /* twowindows.c                                               */
  3. /* Shows the function of the ACTIVATE flag, and how to wait   */
  4. /* for messages from two windows                              */
  5. /* Compiled with Lattice C v5.02                                     */
  6. /* Compiler invoked with: lc -b1 -cfist -L -v -w                     */
  7.  
  8. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  9.  *
  10.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  11.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  12.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  13.  * information on the correct usage of the techniques and operating system
  14.  * functions presented in this example.  The source and executable code of
  15.  * this example may only be distributed in free electronic form, via bulletin
  16.  * board or as part of a fully non-commercial and freely redistributable
  17.  * diskette.  Both the source and executable code (including comments) must
  18.  * be included, without modification, in any copy.  This example may not be
  19.  * published in printed form or distributed with any commercial product.
  20.  * However, the programming techniques and support routines set forth in
  21.  * this example may be used in the development of original executable
  22.  * software products for Commodore Amiga computers.
  23.  * All other rights reserved.
  24.  * This example is provided "as-is" and is subject to change; no warranties
  25.  * are made.  All use is at your own risk.  No liability or responsibility
  26.  * is assumed.
  27.  */
  28.  
  29. #include <exec/types.h>
  30. #include <intuition/intuition.h>
  31. #include <libraries/dos.h>
  32. #ifdef LATTICE
  33. #include <proto/all.h>
  34. #include <stdlib.h>
  35. int CXBRK(void) {return(0);}
  36. #endif
  37.  
  38. /* Include user-written header files here. For illustration, we show */
  39. /*  two header files which we will use frequently.                   */
  40. #include "hires.h"
  41. #include "graniteWindow.h"
  42. #include "hellogoodbye.h"
  43.  
  44. /* Use lowest non-obselete version that supplies the functions you need. */
  45. #define INTUITION_REV 33
  46. #define GRAPHICS_REV  33
  47. /* TICKS_PER_SECOND (defined in libraries/dos.h)
  48.    NEVER call Delay() with an argument of 0 !
  49. */
  50. #define PAUSE(seconds)    (Delay((seconds) * TICKS_PER_SECOND))
  51.  
  52.  
  53. extern VOID cleanExit( struct Screen *, struct Window *, struct Window *,
  54.                        int returnValue);
  55. extern UBYTE handleIDCMP( struct Window * );
  56.  
  57.  
  58. struct IntuitionBase *IntuitionBase = NULL;
  59. struct GfxBase *GfxBase = NULL;
  60. /* The following two lines are for system requester redirection. */
  61. APTR oldwindowptr = NULL;
  62. struct Process *myProcess = NULL;
  63.  
  64.         
  65. VOID main(int argc, char *argv[])
  66. {
  67.     /* Declare variables here */
  68.  
  69.     ULONG aSignalmask, bSignalmask, signals;
  70.     USHORT aDone = FALSE, bDone = FALSE, i, fontHeight;
  71.     struct Screen *screen1 = NULL;
  72.     struct Window *aWindow = NULL, *bWindow = NULL;
  73.  
  74.     /* Save the value of the old window pointer now,
  75.      * in case cleanExit() needs it.
  76.      */
  77.  
  78.     oldwindowptr = myProcess->pr_WindowPtr;
  79.  
  80.     /* Open the Intuition Library */
  81.     
  82.     IntuitionBase = (struct IntuitionBase *)
  83.                     OpenLibrary( "intuition.library",INTUITION_REV );
  84.  
  85.     if (IntuitionBase == NULL)
  86.         cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  87.  
  88.     /* Open any other required libraries */
  89.  
  90.     GfxBase = (struct GfxBase *)
  91.               OpenLibrary("graphics.library", GRAPHICS_REV);
  92.  
  93.     if ( GfxBase == NULL)
  94.         cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  95.  
  96.     /* Make the assignments that were postponed above */
  97.  
  98.     graniteWindow.Width = 300;
  99.     graniteWindow.Height = 100;
  100.     graniteWindow.Title = "aWindow";
  101.  
  102.     /* Open the screen */
  103.  
  104.     screen1 = OpenScreen(&fullHires);
  105.     if (screen1 == NULL)
  106.         cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  107.  
  108.  
  109.     /* Attach the window to the open screen ... */
  110.  
  111.     graniteWindow.Screen = screen1;
  112.  
  113.     /* ... and open the window */
  114.  
  115.     aWindow = OpenWindow(&graniteWindow);
  116.     if (aWindow == NULL)
  117.         cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  118.  
  119.  
  120.     /* Now is the time to redirect system requesters. */
  121.  
  122.     myProcess = (struct Process *)FindTask(NULL);   /* Finds our process */
  123.     myProcess->pr_WindowPtr = (APTR)aWindow;
  124.  
  125.  
  126.     /* Now find out how big the font is, and write the greeting */
  127.  
  128.     fontHeight = (SHORT)aWindow->RPort->Font->tf_YSize;
  129.     hello.TopEdge = fontHeight;
  130.  
  131.     PrintIText( aWindow->RPort, &hello, 5, (LONG)fontHeight );
  132.  
  133.     PAUSE(3L);
  134.  
  135.     /* The NewWindow structure is now free to be modified for */
  136.     /*  the other window.                                     */
  137.  
  138.     graniteWindow.LeftEdge = 330;
  139.     graniteWindow.Title = "bWindow";
  140.  
  141.     bWindow = OpenWindow(&graniteWindow);
  142.     if (bWindow == NULL)
  143.         cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  144.  
  145.     PrintIText( bWindow->RPort, &hello, 5, (LONG)fontHeight );
  146.  
  147.     /* Now's a good time to finish initializing the IntuiText. */
  148.  
  149.     other[0].NextText = &other[1];
  150.     other[1].NextText = &other[2];
  151.  
  152.     /* Fill in the IntuiText vertical offset for the message */
  153.  
  154.     for ( i = 0; i < 3; i ++ )
  155.         other[i].TopEdge = ( i + 1 ) * fontHeight;
  156.  
  157.     /* Set up the signals that you want to hear about ... */
  158.  
  159.     aSignalmask = 1L << aWindow->UserPort->mp_SigBit;
  160.     bSignalmask = 1L << bWindow->UserPort->mp_SigBit;
  161.  
  162.     /* Call the functions that do the main processing */
  163.  
  164.     /* And wait to hear from your signals */
  165.       
  166.     while( !aDone || !bDone ) {
  167.  
  168.         signals = Wait(aSignalmask | bSignalmask);    
  169.         if (signals & aSignalmask)
  170.             aDone = handleIDCMP(aWindow);
  171.         if (signals & bSignalmask)
  172.             bDone = handleIDCMP(bWindow);
  173.         if (aWindow && aDone) {           /* Close bWindow! */
  174.  
  175.             PrintIText(bWindow->RPort, &other[0], 5L, 20L);
  176.             PAUSE( 3L );
  177.             CloseWindow(bWindow);
  178.             bWindow = NULL;
  179.             bSignalmask = 0L;
  180.         }   
  181.         if (bWindow && bDone) {           /* Close aWindow! */
  182.  
  183.             PrintIText(aWindow->RPort, &other[0], 5L, 50L);
  184.             PAUSE( 3L );
  185.  
  186.             /* We're about to close the window that our Process
  187.              * is pointing to, so we must switch our Process
  188.              * to the other window, first.
  189.             */
  190.             myProcess->pr_WindowPtr = (APTR)bWindow;
  191.             
  192.             CloseWindow(aWindow);
  193.             aWindow = NULL;
  194.             aSignalmask = 0L;
  195.         }
  196.  
  197.         /* If either window has been closed, then the user cannot */
  198.         /* close the remaining window, so we must close it and    */
  199.         /* go away.                                               */
  200.  
  201.         if ( !aWindow || !bWindow )
  202.            break;           
  203.     }
  204.  
  205.     /* Exit the program */
  206.  
  207.     PAUSE( 3L );
  208.     cleanExit( screen1, aWindow, bWindow, RETURN_WARN);
  209.  
  210.  
  211. }
  212.  
  213. UBYTE handleIDCMP( struct Window *win )
  214. {
  215.     UBYTE flag = 0;
  216.     struct IntuiMessage *message = NULL;
  217.     ULONG class;
  218.  
  219.     while( message = (struct IntuiMessage *)GetMsg(win->UserPort) ) {
  220.  
  221.         class = message->Class;
  222.         ReplyMsg( (struct Message *)message);
  223.  
  224.         switch( class ) {
  225.         
  226.             case CLOSEWINDOW:
  227.             
  228.                 flag = 1;
  229.                 break;
  230.                 
  231.             default:
  232.             
  233.                 break;
  234.                 
  235.         }        
  236.     }
  237.  
  238.     return(flag);
  239. }
  240.  
  241. VOID cleanExit( scrn, aWind, bWind, returnValue )
  242. struct Screen *scrn;
  243. struct Window *aWind, *bWind;
  244. int returnValue;
  245. {
  246.     /* Close things in the reverse order of opening */
  247.  
  248.     /* Restore the old window pointer in our process.
  249.      * Don't test 'oldwindowptr', since NULL is a valid
  250.      * value for pr_WindowPtr.
  251.      */
  252.     myProcess->pr_WindowPtr = oldwindowptr;    
  253.  
  254.     /* Close the window and the screen. */
  255.     if (bWind) CloseWindow( bWind );
  256.     if (aWind) CloseWindow( aWind );
  257.  
  258.     if (scrn) CloseScreen( scrn );
  259.  
  260.  
  261.     /* Close the library, and then exit */
  262.     if (GfxBase) CloseLibrary( (struct Library *)GfxBase );
  263.     if (IntuitionBase) CloseLibrary( (struct Library *)IntuitionBase );
  264.  
  265.  
  266.     exit(returnValue);
  267.  
  268. }
  269.  
  270.