home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / EXTANI.ZIP / BOXES.C < prev    next >
C/C++ Source or Header  |  1990-04-22  |  5KB  |  156 lines

  1. /***************************************************************************\
  2. * BOXES.C - An example animated desktop extension by John Ridges
  3. \***************************************************************************/
  4.  
  5. #define INCL_DOSPROCESS
  6. #define INCL_DOSSEMAPHORES
  7. #define INCL_GPIPRIMITIVES
  8. #define INCL_WINDIALOGS
  9. #define INCL_WINMESSAGEMGR
  10. #define INCL_WINSYS
  11. #define INCL_WINWINDOWMGR
  12.  
  13. #include <os2.h>
  14. #include <stdlib.h>   /* Needed because rand and labs are called */
  15.  
  16. typedef struct {
  17.     HAB animatehab;
  18.     HPS shadowhps;
  19.     HWND screenhwnd;
  20.     RECTL screenrectl;
  21.     ULONG hpssemaphore;
  22.     ULONG volatile closesemaphore;
  23.     HMODULE thismodule;
  24.     BOOL (far pascal *screenvisible)(void);
  25. } INITBLOCK;
  26.  
  27. char far pascal _loadds animatename(void);
  28. BOOL far pascal _loadds animateinit(INITBLOCK far *);
  29. void far pascal _loadds animatechar(char);
  30. void far pascal _loadds animatedblclk(MPARAM);
  31. void far pascal _loadds animatepaint(HPS, RECTL far *);
  32. void far pascal _loadds animateclose(void);
  33. void far pascal _loadds animatethread(void);
  34.  
  35. static INITBLOCK far *init;
  36.  
  37. char far pascal _loadds animatename()
  38. {
  39.     return 'B';   /* Tell ANIMATE that we are B for Boxes */
  40. }
  41.  
  42. BOOL far pascal _loadds animateinit(initptr)
  43. INITBLOCK far *initptr;
  44. {
  45.     init = initptr;   /* Save the INITBLOCK address locally */
  46.  
  47.     /* Tell ANIMATE that we will be using the shadow HPS */
  48.     return TRUE;
  49. }
  50.  
  51. void far pascal _loadds animatechar(c)
  52. char c;
  53. {
  54.     c;   /* Reference the argument so we don't get compiler warnings */
  55. }
  56.     
  57. void far pascal _loadds animatedblclk(mp1)
  58. MPARAM mp1;
  59. {
  60.     mp1;
  61.  
  62.     WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
  63.         "Boxes example animated desktop extension.\n\nBy John Ridges",
  64.         "ANIMATE",0,MB_OK|MB_NOICON);
  65. }
  66.  
  67. void far pascal _loadds animatepaint(hps,prclpaint)
  68. HPS hps;
  69. RECTL far *prclpaint;
  70. {
  71.     hps;
  72.     prclpaint;
  73. }
  74.  
  75. void far pascal _loadds animateclose()
  76. {
  77. }
  78.  
  79. void far pascal _loadds animatethread()
  80. {
  81.     HAB hab;
  82.     HPS hps;
  83.     POINTL pnt1,pnt2;
  84.     long color,roundh,roundv;
  85.     static long colorset[] = { CLR_WHITE, CLR_BLUE, CLR_RED, CLR_PINK, CLR_GREEN,
  86.         CLR_CYAN, CLR_YELLOW, CLR_DARKGRAY, CLR_DARKBLUE, CLR_DARKRED, CLR_BLACK,
  87.         CLR_DARKPINK, CLR_DARKGREEN, CLR_DARKCYAN, CLR_BROWN, CLR_PALEGRAY };
  88.  
  89.     /* Get an HAB for this thread (since we make PM calls) */
  90.     hab = WinInitialize(0);
  91.  
  92.     /* Enter the critical section so we can use the shadow HPS */
  93.     DosSemRequest(&init->hpssemaphore,SEM_INDEFINITE_WAIT);
  94.  
  95.     /* Paint the shadow screen with the background color */
  96.     WinFillRect(init->shadowhps,&init->screenrectl,SYSCLR_BACKGROUND);
  97.  
  98.     /* Exit the critical section */
  99.     DosSemClear(&init->hpssemaphore);
  100.  
  101.     /* Tell ANIMATE that we are done initializing and painting may proceed */
  102.     WinPostMsg(init->screenhwnd,WM_USER,0,0);
  103.     
  104.     /* Loop until ANIMATE tells us to stop */
  105.     while (!init->closesemaphore)
  106.     
  107.         /* Don't do anything if the screen is not visible */
  108.         if ((init->screenvisible)()) {
  109.  
  110.         /* Pick a random color for the box */
  111.         color = colorset[(long)rand()*sizeof(colorset)/sizeof(long)>>15];
  112.  
  113.         /* Pick two random corner positions for the box */
  114.         pnt1.x = init->screenrectl.xLeft+(long)rand()*
  115.             (init->screenrectl.xRight-init->screenrectl.xLeft)>>15;
  116.         pnt1.y = init->screenrectl.yBottom+(long)rand()*
  117.             (init->screenrectl.yTop-init->screenrectl.yBottom)>>15;
  118.         pnt2.x = init->screenrectl.xLeft+(long)rand()*
  119.             (init->screenrectl.xRight-init->screenrectl.xLeft)>>15;
  120.         pnt2.y = init->screenrectl.yBottom+(long)rand()*
  121.             (init->screenrectl.yTop-init->screenrectl.yBottom)>>15;
  122.  
  123.         /* Pick a random roundness for the corners */
  124.         roundh = labs(pnt1.x-pnt2.x)*rand()>>15;
  125.         roundv = labs(pnt1.y-pnt2.y)*rand()>>15;
  126.  
  127.         /* Enter the critical section so we can get an HPS of the screen */
  128.         DosSemRequest(&init->hpssemaphore,SEM_INDEFINITE_WAIT);
  129.         hps = WinGetPS(init->screenhwnd);
  130.  
  131.         /* Set the color of both the screen HPS and the shadow screen HPS */
  132.         GpiSetColor(hps,color);
  133.         GpiSetColor(init->shadowhps,color);
  134.  
  135.         /* Move to the random corner */
  136.         GpiMove(hps,&pnt1);
  137.         GpiMove(init->shadowhps,&pnt1);
  138.  
  139.         /* Draw the box */
  140.         GpiBox(hps,DRO_FILL,&pnt2,roundh,roundv);
  141.         GpiBox(init->shadowhps,DRO_FILL,&pnt2,roundh,roundv);
  142.  
  143.         /* Release the HPS of the screen and leave the critical section */
  144.         WinReleasePS(hps);
  145.         DosSemClear(&init->hpssemaphore);
  146.     }
  147.     /* We've been told to close, so get rid of the HAB */
  148.     WinTerminate(hab);
  149.  
  150.     /* Make sure the stack doesn't vanish before we're completely gone */
  151.     DosEnterCritSec();
  152.  
  153.     /* Tell ANIMATE that we're gone */
  154.     DosSemClear((HSEM)&init->closesemaphore);
  155. }
  156.