home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / popups / Docs / ExampleSrc / c / _WimpCol < prev    next >
Encoding:
Text File  |  1993-05-18  |  6.5 KB  |  193 lines

  1. /*  File:     _WimpCol.c
  2.  *  Purpose:  Supplies functionality for a PopUp
  3.  *  Author:   © Copyright 1993 Jason Williams
  4.  *            All rights reserved
  5.  */
  6.  
  7. #include "DeskLib:GFX.h"
  8. #include "DeskLib:WimpSWIs.h"
  9. #include "DeskLib:Template.h"
  10.  
  11. #include "Server.h"
  12.  
  13.  
  14. typedef struct
  15. {
  16.   char colour;
  17. } wimpcol_data;
  18.  
  19.  
  20. /*  First of all, we define a handler_info structure for our PopUp handler
  21.  *  which describes it to the manager. This is referenced externally
  22.  *  by the server to find the procedure to call with events for any PopUp
  23.  *  of this type, etc.
  24.  *  (We also prototype the handler function so we can reference it here)
  25.  */
  26.  
  27. static int Handler_WimpColour(int, ctrl_block *, void *, event_pollblock *);
  28.  
  29. handler_info HandlerInfo_WimpColour =
  30. {
  31.   "WimpColour",                              /* PopUp type/name string       */
  32.   0x00000001,                                /* Flag word                    */
  33.   Handler_WimpColour,                        /* Handler procedure            */
  34.   4                                          /* Instantiation workspace size */
  35. };
  36.  
  37.  
  38. static char invertcolour[] = {7,7,7,7, 0,0,0,0, 0,7,7,0, 7,0,7,7};
  39.  
  40. /*  The Handler_ function referenced in the above info struct will handle all
  41.  *  calls from the server during normal operation...
  42.  */
  43.  
  44. static BOOL Handler_WimpColour(int reasoncode, ctrl_block *ctrl,
  45.                             void *privateworkspace, event_pollblock *event)
  46. {
  47.   switch(reasoncode)
  48.   {
  49.     case REASON_OPEN:
  50.       {
  51.         window_handle windowhandle;
  52.         window_block *window;
  53.         wimpcol_data *info;
  54.  
  55.         /*  Find the template. NOTE that if you provide a STATIC popup, you
  56.          *  MUST support multiple instantiations, so MUST use Template_Clone()
  57.          *  Also note that if bringing up a MENU LEAF window, we must clone
  58.          *  our template data into the RMA so the WIMP does not crash! ;-(
  59.           */
  60.         window = Template_Find("WimpColour");      /* Has NO indirected data */
  61.         if (window == NULL) break;
  62.  
  63.         /*  Create the window, and fill in it's icons with the data given
  64.          *  to us in the pollblock. (DATA_BASE is defined above)
  65.          */
  66.         window->colours[windowcol_WORKBACK] = colour_TRANSPARENT;
  67.         window->flags.data.closeicon = (ctrl->appflags & APPFLAGS_ISLEAF) == 0;
  68.         Wimp_CreateWindow(window, &windowhandle);
  69.  
  70.         info = (wimpcol_data *) (((int)event) + POPUP_DATA_OFFSET);
  71.         if (info->colour >= 0 && info->colour <= 15)
  72.           ((char *)privateworkspace)[0] = info->colour;
  73.  
  74.         ctrl->pollmask.value = ~( (1 << event_CLOSE)  |
  75.                                   (1 << event_REDRAW) |
  76.                                   (1 << event_CLICK) );
  77.         ctrl->basewindow = windowhandle;     /* Let server know windowhandle */
  78.       }
  79.       return(TRUE);
  80.  
  81.  
  82.     case REASON_CLOSE:                            /* Quietly kill our window */
  83.       Wimp_DeleteWindow(ctrl->basewindow);                   
  84.       return(TRUE);
  85.  
  86.  
  87.     case REASON_EVENT:                                /* Handle a WIMP Event */
  88.       switch(event->type)
  89.       {
  90.         case event_REDRAW:
  91.           if (event->data.openblock.window == ctrl->basewindow)
  92.           {
  93.             window_redrawblock r;
  94.             window_state   wstate;
  95.             int  gridsize, x, y, x0, y0;
  96.             BOOL more;
  97.  
  98.             Wimp_GetWindowState(ctrl->basewindow, &wstate);
  99.             x0 = wstate.openblock.screenrect.min.x;
  100.             y0 = wstate.openblock.screenrect.min.y;
  101.             gridsize = (wstate.openblock.screenrect.max.x - x0) >> 2;
  102.  
  103.             r.window = ctrl->basewindow;
  104.             Wimp_RedrawWindow(&r, &more);
  105.             while (more)
  106.             {
  107.               for (x = 0; x < 4; x++)
  108.                 for (y = 0; y < 4; y++)
  109.                 {
  110.                   Wimp_SetColour(x + ((3-y) << 2));
  111.                   GFX_RectangleFill(x0 + (x * gridsize), y0 + (y * gridsize),
  112.                                     gridsize-1, gridsize-1);
  113.                 }
  114.  
  115.               x = ((char *)privateworkspace)[0];
  116.               Wimp_SetColour(invertcolour[x]);
  117.               y = 3 - ((x >> 2) & 3);
  118.               x &= 3;
  119.               GFX_CircleFill(x0 + x * gridsize + (gridsize >> 1),
  120.                              y0 + y * gridsize + (gridsize >> 1),
  121.                              gridsize >> 2);
  122.  
  123.               Wimp_GetRectangle(&r, &more);
  124.             }
  125.             return(TRUE);
  126.           }
  127.           break;
  128.           
  129.  
  130.         case event_CLOSE:
  131.           if (event->data.openblock.window == ctrl->basewindow)
  132.           {
  133.             /*  *ONLY* if this is our window, we kill the current menu, kill
  134.              *  our window, and return TRUE to indicate that we have processed
  135.              *  the event (so no other handlers should get it)
  136.              */
  137.             Wimp_CreateMenu((menu_block *) -1, 0, 0);
  138.             Wimp_DeleteWindow(ctrl->basewindow);
  139.             KillMe();         /* Inform server that we have closed ourselves */
  140.             return(TRUE);
  141.          }
  142.           break;
  143.  
  144.  
  145.         case event_CLICK:
  146.           if (event->data.mouse.window == ctrl->basewindow)
  147.           {
  148.             window_state   wstate;
  149.             int  gridsize, x, y;
  150.  
  151.             Wimp_GetWindowState(ctrl->basewindow, &wstate);
  152.             x = wstate.openblock.screenrect.min.x;
  153.             y = wstate.openblock.screenrect.min.y;
  154.             gridsize = (wstate.openblock.screenrect.max.x - x) >> 2;
  155.  
  156.             x = ((event->data.mouse.pos.x - x) / gridsize) & 3;
  157.             y = ((event->data.mouse.pos.y - y) / gridsize) & 3;
  158.  
  159.             x += ((3 - y) << 2);
  160.             if (x != ((char *)privateworkspace)[0])  /* If new colour picked */
  161.             {
  162.               if (!event->data.mouse.button.data.select)
  163.               {
  164.                 window_redrawblock r;
  165.  
  166.                 ((char *)privateworkspace)[0] = x;    /* Force window redraw */
  167.                 r.window = ctrl->basewindow;
  168.                 r.rect.min.x = r.rect.min.y = -1024;
  169.                 r.rect.max.x = r.rect.max.y = 1024;
  170.                 Wimp_ForceRedraw(&r);
  171.               }
  172.  
  173.               ((char *)event)[0] = x;       /* tell the client of the change */
  174.               SendState(event, 1);
  175.             }
  176.  
  177.             if (event->data.mouse.button.data.select)
  178.             {
  179.               Wimp_CreateMenu((menu_block *) -1, 0, 0); /* Kill menu/window  */
  180.               Wimp_DeleteWindow(ctrl->basewindow);      /* If hit with select*/
  181.               KillMe();       /* Inform server that we have closed ourselves */
  182.             }
  183.             return(TRUE);
  184.           }
  185.           break;
  186.       }
  187.       break;
  188.   }
  189.  
  190.   return(FALSE);          /* If we didn't handle this call, we MUST return 0 */
  191. }
  192.  
  193.