home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / _penrose / !penrose / c / Control next >
Encoding:
Text File  |  1991-08-11  |  9.4 KB  |  361 lines

  1. /* > c.Control
  2.  * 
  3.  * Title:    Control Purpose:  Graphics Fun - show a display Works with Penrose
  4.  * 
  5.  * Left-clicking on the icon will start the display */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11. #include "wimp.h"               /* access to WIMP SWIs                      */
  12. #include "wimpt.h"              /* wimp task facilities                     */
  13. #include "win.h"                /* registering window handlers              */
  14. #include "event.h"              /* poll loops, etc                          */
  15. #include "baricon.h"            /* putting icon on icon bar                 */
  16. #include "res.h"                /* access to resources                      */
  17. #include "resspr.h"             /* sprite resources                         */
  18. #include "template.h"           /* reading in template file                 */
  19. #include "dbox.h"               /* dialogue box handling                    */
  20. #include "werr.h"               /* error reporting                          */
  21.  
  22. /* externals to display routines */
  23.  
  24. #include "Display.h"
  25.  
  26. /* --- Menu Entry Constants --- */
  27. #define iconmenu_MInfo     1
  28. #define iconmenu_MDisplay  2
  29. #define iconmenu_MPattern  3
  30. #define iconmenu_MQuit     4
  31.  
  32. static int type = 0;
  33.  
  34.  
  35. /***************************** WINDOW HANDLING *****************************/
  36.  
  37.  
  38. /*--- Create the window, yielding its handle. Return TRUE if ok. ---*/
  39. static BOOL display_create_window(display_datas *display, wimp_w *handle)
  40. {
  41.     wimp_wind *window;    /* Pointer to window definition */
  42.  
  43.     /* Find template for the window */
  44.     if ((window = template_syshandle("Display")) == 0)
  45.         return FALSE;
  46.  
  47.     /* Create the window, dealing with errors */
  48.     if (wimpt_complain(wimp_create_wind(window, handle)) != 0)
  49.         return FALSE;
  50.  
  51.     display->w = *handle;
  52.  
  53.     return TRUE;
  54. }
  55.  
  56.  
  57. /*--- Individual event routines for the window ---*/
  58.  
  59. static void display_redo_window(display_datas *display,
  60.     wimp_redrawstr *r, BOOL more)
  61. {
  62.     /* --- refresh the window's contents --- */
  63.     while (more)
  64.     {
  65.         display_show(display, r);
  66.  
  67.         wimp_get_rectangle(r, &more);
  68.     }
  69.  
  70.     /* We do not report any errors - see the manual for the reason why */
  71. }
  72.  
  73.  
  74. static void display_redraw_window(display_datas *display, wimp_w handle)
  75. {
  76.     wimp_redrawstr r;
  77.     BOOL more;
  78.  
  79.     /* --- do the redraw --- */
  80.     r.w = handle;
  81.     wimpt_noerr(wimp_redraw_wind(&r, &more));
  82.  
  83.     if (more)
  84.         display_redo_window(display, &r, more);
  85. }
  86.  
  87.  
  88. static void display_open_window(display_datas *display, wimp_openstr *o)
  89. {
  90.     /* Pass the open request on to the wimp */
  91.     wimpt_noerr(wimp_open_wind(o));
  92. }
  93.  
  94.  
  95. /*--- Close the window. ---*/
  96. static void display_close_window(display_datas *display, wimp_w handle)
  97. {
  98.     /* Close the window */
  99.     wimpt_noerr(wimp_close_wind(handle));
  100.  
  101.     display->displaying = FALSE;
  102.  
  103.     /* Decrement active window count - this will lead to closedown */
  104.     /* win_activedec(); */
  105. }
  106.  
  107.  
  108. /****************************** EVENT HANDLING *****************************/
  109.  
  110.  
  111. /*--- Event handler for window. 'handle' is a pointer to the display. ---*/
  112. static void display_handler(wimp_eventstr *e, void *handle)
  113. {
  114.     display_datas *display = (display_datas *)handle;
  115.  
  116.     /* Deal with event */
  117.     switch (e->e)
  118.     {
  119.     case wimp_EREDRAW:
  120.         display_redraw_window(display, e->data.o.w);
  121.         break;
  122.  
  123.     case wimp_EOPEN:
  124.         display_open_window(display, &e->data.o);
  125.         break;
  126.  
  127.     case wimp_ECLOSE:
  128.         display_close_window(display, e->data.o.w);
  129.         break;
  130.  
  131.     case wimp_ESEND:
  132.     case wimp_ESENDWANTACK:
  133.         switch (e->data.msg.hdr.action)
  134.         {
  135.         case wimp_MHELPREQUEST:
  136.             e->data.msg.hdr.your_ref = e->data.msg.hdr.my_ref;
  137.             e->data.msg.hdr.action = wimp_MHELPREPLY;
  138.             e->data.msg.hdr.size = 256;
  139.             if (e->data.msg.data.helprequest.m.i == -1) /* ie. not on our icon */
  140.                 sprintf(e->data.msg.data.helpreply.text,
  141.                      "This is the display window.|MOnly one can be active");
  142.             else
  143.                 sprintf(e->data.msg.data.helpreply.text,
  144.                 "This is the display icon.|MClick SELECT to start display");
  145.             wimpt_noerr(wimp_sendmessage(wimp_ESEND, &e->data.msg,
  146.                                          e->data.msg.hdr.task));
  147.             break;
  148.  
  149.         default:
  150.             break;
  151.         }
  152.  
  153.     default:                    /* we're not interested in any other events */
  154.         break;
  155.     }
  156. }
  157.  
  158.  
  159. /*--- Event handler called on a left click on the icon. ---*/
  160. static void control_leftclickproc(wimp_i icon)
  161. {
  162.     wimp_wstate state;
  163.     wimp_winfo  info;
  164.     wimp_redrawstr r;
  165.     icon = icon;
  166.  
  167.     display_init(type++);
  168.  
  169.     /* --- open the window we created --- */
  170.     wimpt_noerr(wimp_get_wind_state(display_data.w, &state));
  171.  
  172.     if (display_data.displaying == FALSE)
  173.     {
  174.         state.o.behind = -1;    /* make sure it is opened in front */
  175.         display_open_window(&display_data, &state.o);
  176.  
  177.         r.w = display_data.w;
  178.         r.box.x0 = 0;
  179.         r.box.x1 = 100;
  180.         r.box.y0 = -100;
  181.         r.box.y1 = 0;
  182.         display_data.displaying = TRUE;
  183.     }
  184.     else
  185.     {
  186.         r.w = -1;
  187.         r.box = state.o.box;
  188.         r.w = display_data.w;
  189.         r.box.x0 = state.o.x;
  190.         r.box.x1 = state.o.x+state.o.box.x1-state.o.box.x0;
  191.         r.box.y0 = state.o.y+state.o.box.y0-state.o.box.y1;
  192.         r.box.y1 = state.o.y;
  193.     }
  194.  
  195.     /* --- force a redraw of the whole window --- */
  196.     wimpt_noerr(wimp_force_redraw(&r));
  197. }
  198.  
  199.  
  200. /*--- Event handler called for Pattern menu entry. ---*/
  201. static void control_pattern(wimp_i icon, int pattern)
  202. {
  203.     wimp_wstate state;
  204.     wimp_winfo  info;
  205.     wimp_redrawstr r;
  206.     icon = icon;
  207.  
  208.     if (pattern != -1)
  209.     {
  210.         type = pattern;
  211.         display_init(type);
  212.  
  213.         if (display_data.displaying != FALSE)
  214.         {
  215.             /* --- open the window we created --- */
  216.             wimpt_noerr(wimp_get_wind_state(display_data.w, &state));
  217.  
  218.             r.w = -1;
  219.             r.box = state.o.box;
  220.             r.w = display_data.w;
  221.             r.box.x0 = state.o.x;
  222.             r.box.x1 = state.o.x+state.o.box.x1-state.o.box.x0;
  223.             r.box.y0 = state.o.y+state.o.box.y0-state.o.box.y1;
  224.             r.box.y1 = state.o.y;
  225.  
  226.             /* --- force a redraw of the whole window --- */
  227.             wimpt_noerr(wimp_force_redraw(&r));
  228.         }
  229.     }
  230. }
  231.  
  232.  
  233. static void control_info_aboutprog(void)
  234. {
  235.     dbox d;  /* Dialogue box handle */
  236.  
  237.     /* --- display info about the program in a dialogue box --- */
  238.     if ((d = dbox_new("ProgInfo")) != NULL)
  239.     {
  240.         dbox_showstatic(d);
  241.  
  242.         /* Keep it on the screen as long as needed */
  243.         dbox_fillin(d);
  244.  
  245.         /* Dispose of the dialogue box */
  246.         dbox_dispose(&d);
  247.     }
  248. }
  249.  
  250. /******************************* MENU HANDLING *****************************/
  251.  
  252.  
  253. static menu control_menumaker(void *handle)
  254. {
  255.     menu temp, sub;
  256.     handle = handle;
  257.  
  258.     /* --- create a menu for the icon on the icon bar --- */
  259.     temp = menu_new(display_name, ">Info,Display,Pattern,Quit");
  260.     sub = menu_new("Patterns", "Standard,Darts&Kites,Gingerbread,Roman,Curves,Rhombii");
  261.     menu_submenu(temp, iconmenu_MPattern, sub);
  262.  
  263.     /* --- fade out "start" field if we already have a display --- */
  264.     menu_setflags(temp, iconmenu_MDisplay, 0,
  265.         (display_data.displaying == TRUE) ? 1 : 0);
  266.  
  267.     return temp;
  268. }
  269.  
  270.  
  271. /*--- Event handler for the menu. ---*/
  272. static void control_menuproc(void *handle, char *hit)
  273. {
  274.     handle = handle;
  275.  
  276.     /* --- see which menu entry has been chosen --- */
  277.     switch (hit[0])
  278.     {
  279.     case iconmenu_MInfo:
  280.         control_info_aboutprog();
  281.         break;
  282.  
  283.     case iconmenu_MDisplay:
  284.         control_leftclickproc((wimp_i) 0);
  285.         break;
  286.  
  287.     case iconmenu_MPattern:
  288.         control_pattern((wimp_i) 0, hit[1]-1);
  289.         break;
  290.  
  291.     case iconmenu_MQuit:
  292.         exit(0);
  293.         break;
  294.  
  295.     default:                    /* shouldn't get here */
  296.         break;
  297.     }
  298. }
  299.  
  300. /******************************** INITIALISATION ***************************/
  301.  
  302. static BOOL control_initialise(void)
  303. {
  304.     char baricon_name[30];
  305.     wimp_w handle;
  306.  
  307.     /* --- initialise wimp library modules --- */
  308.     wimpt_init(display_name);
  309.     res_init(display_name);
  310.     resspr_init();
  311.     template_init();
  312.     dbox_init();
  313.  
  314.     /* Initialise display handler */
  315.     type = 0;
  316.     display_init(type);
  317.  
  318.     /* --- create a window for display --- */
  319.     display_create_window(&display_data, &handle);
  320.  
  321.     /* --- attach an event handling function to window --- */
  322.     win_register_event_handler(handle, display_handler, &display_data);
  323.  
  324.     /* --- put our icon on the icon bar --- */
  325.     baricon_name[0] = '!';
  326.     strcpy(baricon_name + 1, display_name);
  327.     baricon(baricon_name, (int) resspr_area(), control_leftclickproc);
  328.  
  329.     /* --- attach a menu to the icon on the icon bar --- */
  330.     event_attachmenumaker(win_ICONBAR, control_menumaker,
  331.         control_menuproc, 0);
  332.  
  333.     event_attachmenumaker(handle, control_menumaker,
  334.         control_menuproc, 0);
  335.  
  336.     /* --- activate saving of floating point registers on poll --- */
  337.     wimp_save_fp_state_on_poll();
  338.  
  339.     /* All went ok */
  340.     return TRUE;
  341. }
  342.  
  343. /******************************* MAIN PROGRAM ******************************/
  344.  
  345.  
  346. int main(int argc, char *argv[])
  347. {
  348.     /* --- initialise the environment --- */
  349.     if (control_initialise())
  350.     {
  351.         /* --- mask off the events we're not interested in --- */
  352.         event_setmask(wimp_EMPTRENTER | wimp_EMPTRLEAVE);
  353.  
  354.         /* --- the main event loop --- */
  355.         while (TRUE)
  356.             event_process();
  357.     }
  358.  
  359.     return 0;
  360. }
  361.