home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / toswinsc.zoo / event.c < prev    next >
C/C++ Source or Header  |  1992-10-27  |  9KB  |  453 lines

  1. /*
  2.  * Copyright 1992 Eric R. Smith. All rights reserved.
  3.  * Redistribution is permitted only if the distribution
  4.  * is not for profit, and only if all documentation
  5.  * (including, in particular, the file "copying")
  6.  * is included in the distribution in unmodified form.
  7.  * THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY, NOT
  8.  * EVEN THE IMPLIED WARRANTIES OF MERCHANTIBILITY OR
  9.  * FITNESS FOR A PARTICULAR PURPOSE. USE AT YOUR OWN
  10.  * RISK.
  11.  */
  12. #include "xgem.h"
  13. #include <osbind.h>
  14. #include <ctype.h>
  15. #include <stdlib.h>
  16. #include "twdefs.h"
  17. #include "twproto.h"
  18.  
  19. /*
  20.  * global variables for any GEM/AES application
  21.  */
  22.  
  23. /* VDI physical and virtual workstation handles */
  24. int phys_handle, vdi_handle = -1;
  25.  
  26. /* AES application ID */
  27. int gl_apid;
  28.  
  29. /* system character size */
  30. int gl_hchar, gl_wchar, gl_wbox, gl_hbox;
  31.  
  32. /* size of the desktop area */
  33. int xdesk, ydesk, hdesk, wdesk;
  34.  
  35. /* number of fonts available */
  36. int gl_numfonts = 1;
  37.  
  38. /* rectangle we're watching for events */
  39. int gl_rx, gl_ry, gl_rw, gl_rh, gl_rstate;
  40.  
  41. /* number of planes on screen */
  42. int gl_screenplanes;
  43.  
  44. /*
  45.  * global variables used by the xgem library
  46.  */
  47.  
  48. int gl_button;        /* button state */
  49. int gl_done;        /* set when the user wants to quit */
  50. long gl_timer;        /* how many milliseconds before evnt_loop
  51.                times out */
  52. void (*fn_timeout)();    /* what to do when a time out occurs */
  53. void (*fn_message)();    /* function for handling non-standard messages */
  54. void (*fn_mouse)();    /* mouse handler */
  55. int (*fn_key)();    /* default keyboard handler */
  56. void (*fn_rect)();    /* handler for rectangle events */
  57.  
  58. /* variable that tells us about GDOS
  59.  * 0:  no GDOS
  60.  * 1:  old GDOS, or a clone
  61.  * 2:  FontGDOS
  62.  * 3:  FSMGDOS
  63.  */
  64.  
  65. #define GDOS 1
  66. #define FontGDOS 2
  67. #define FSMGDOS 3
  68. int gdos_active;
  69.  
  70. /*
  71.  * local variables
  72.  */
  73.  
  74. static int msgbuff[8];    /* AES message buffer */
  75. static int hidden;    /* mouse hidden? */
  76.  
  77.  
  78. /*
  79.  * some misc. utilities
  80.  */
  81.  
  82. void
  83. hide_mouse()
  84. {
  85.     if (!hidden) {
  86.         graf_mouse(M_OFF, 0x0L);
  87.         hidden = TRUE;
  88.     }
  89. }
  90.  
  91. void
  92. show_mouse()
  93. {
  94.     if (hidden) {
  95.         graf_mouse(M_ON, 0x0L);
  96.         hidden = FALSE;
  97.     }
  98. }
  99.  
  100. /* set clipping rectangle */
  101.  
  102. static int clip[4];
  103.  
  104. void set_clip(x,y,w,h)
  105. int x,y,w,h;
  106. {
  107.     clip[0] = x;
  108.     clip[1] = y;
  109.     clip[2] = clip[0]+w-1;
  110.     clip[3] = clip[1]+h-1;
  111.     vs_clip(vdi_handle, 1, clip);
  112. }
  113.  
  114. /*
  115.  * Call this function to turn clipping off.
  116.  */
  117.  
  118. void
  119. reset_clip()
  120. {
  121.     vs_clip(vdi_handle, 0, clip);
  122.     clip[0] = clip[1] = clip[2] = clip[3] = -1;
  123. }
  124.  
  125. static void
  126. handle_key(code, shift)
  127.     int code, shift;
  128. {
  129.     if (menu_key(code, shift)) {
  130.         return;
  131.     }
  132.     if (window_key(code, shift)) {
  133.         return;
  134.     }
  135.     if (fn_key && (*fn_key)(code))
  136.         return;
  137. }
  138.  
  139. static void
  140. handle_click(mclicks, mx, my, mshift, mbutton)
  141.     int mclicks, mx, my, mshift, mbutton;
  142. {
  143.     extern int window_click();
  144.  
  145.     if (window_click(mclicks, mx, my, mshift, mbutton))
  146.         return;
  147.     if (fn_mouse)
  148.         (*fn_mouse)(mclicks, mx, my, mshift, mbutton);
  149. }
  150.  
  151. /*
  152.  * main event loop for most applications
  153.  */
  154.  
  155. void
  156. evnt_loop()
  157. {
  158.     int event;
  159.     int msx, msy, mbutton, mshift, mbreturn;
  160.     int keycode;
  161.     int evnt_type;
  162.     extern void handle_menu();
  163.     int gx, gy, gw, gh;
  164.     int checkmenu = 0;
  165.  
  166.     gl_done = FALSE;
  167.     do {
  168.         evnt_type = MU_MESAG | MU_BUTTON | MU_KEYBD;
  169.         if (fn_timeout) evnt_type |= MU_TIMER;
  170.         if (fn_rect) evnt_type |= MU_M1;
  171.         if (checkmenu && gl_topwin && gl_topwin->menu &&
  172.             (gl_topwin->wi_handle > 0) &&
  173.             (gl_topwin->wi_kind & INFO)) {
  174.             evnt_type |= MU_M2;
  175.             gx = gl_topwin->wi_x;
  176.             gy = gl_topwin->wi_y - gl_hbox;
  177.             gw = strlen(gl_topwin->infostr)*gl_wchar;
  178.             if (gw > gl_topwin->wi_w)
  179.                 gw = gl_topwin->wi_w;
  180.             gh = gl_hbox;
  181.         } else {
  182.             gx = gy = gw = gh = 0;
  183.         }
  184.         event = evnt_multi(evnt_type,
  185.             0x0101, 0x0003, 0x0000,
  186.             gl_rstate,gl_rx,gl_ry,gl_rw,gl_rh,
  187.             0,gx,gy,gw,gh,
  188.             msgbuff,
  189.             gl_timer,
  190.             &msx, &msy, &mbutton, &mshift,
  191.             &keycode,
  192.             &mbreturn);
  193.  
  194.         if (event & MU_MESAG) {
  195.             switch (msgbuff[0]) {
  196.             case MN_SELECTED:
  197.                 handle_menu(msgbuff[3], msgbuff[4]);
  198.                 break;
  199.             case WM_REDRAW:
  200.             case WM_TOPPED:
  201.             case WM_CLOSED:
  202.             case WM_FULLED:
  203.             case WM_ARROWED:
  204.             case WM_HSLID:
  205.             case WM_VSLID:
  206.             case WM_SIZED:
  207.             case WM_MOVED:
  208.                 handle_window(msgbuff);
  209.                 break;
  210.             default:
  211.                 if (fn_message)
  212.                     (*fn_message)(msgbuff);
  213.                 break;
  214.             }
  215.         }
  216.  
  217.     /* if we got a message, the top window may have been changed,
  218.      * so ignore drop down menu events
  219.      */
  220.         else if (event & MU_M2) {
  221.             if (msx >= gx && msx < gx+gw &&
  222.                 msy >= gy && msy < gy+gh) {
  223.                 dropdown_menu(gl_topwin->menu, gx, gy);
  224.             }
  225.             checkmenu = 0;
  226.         } else {
  227.             checkmenu = 1;
  228.         }
  229.  
  230. /* if we are "pointing to type", then set the focus window appropriately */
  231.         if (point_to_type) {
  232.             WINDOW *newfocus;
  233.             TEXTWIN *oldt, *newt;
  234.  
  235.             oldt = newt = 0;
  236.             newfocus = find_window(msx, msy);
  237.             if (!newfocus) newfocus = gl_topwin;
  238.             if (newfocus != focuswin) {
  239.                 if (focuswin && focuswin->wtype == TEXT_WIN) {
  240.                     oldt = focuswin->extra;
  241.                     curs_off(oldt);
  242.                 }
  243.                 if (newfocus && newfocus->wtype == TEXT_WIN) {
  244.                     newt = newfocus->extra;
  245.                     curs_off(newt);
  246.                 }
  247.                 focuswin = newfocus;
  248.                 if (oldt) { 
  249.                     curs_on(oldt);
  250.                     refresh_textwin(oldt);
  251.                 }
  252.                 if (newt) {
  253.                     curs_on(newt);
  254.                     refresh_textwin(newt);
  255.                 }
  256.             }
  257.         }
  258.         else
  259.             focuswin = gl_topwin;
  260.  
  261.         if (event & MU_BUTTON) {
  262.             handle_click(mbreturn, msx, msy, mshift, mbutton);
  263.         }
  264.  
  265.         if (event & MU_KEYBD) {
  266.             handle_key(keycode, mshift);
  267.         }
  268.  
  269.         if (event & MU_M1) {
  270.             gl_rstate = !gl_rstate;
  271.             (*fn_rect)(msx, msy);
  272.         }
  273.  
  274.         if (event & MU_TIMER) {
  275.             (*fn_timeout)();
  276.         }
  277.     } while (!gl_done);
  278. }
  279.  
  280. static int lasttextcolor, lastfillcolor, lastwrmode, lastheight, lastfont;
  281. static int laststyle, lastindex;
  282. static int lasteffects;
  283.  
  284. void
  285. open_vwork()
  286. {
  287.     int work_in[11];
  288.     int work_out[57];
  289.     int i;
  290.  
  291. /* open a virtual workstation */
  292.     if (vdi_handle > 0)        /* workstation already open */
  293.         return;
  294.     for (i = 1; i < 10; i++)
  295.         work_in[i] = 1;
  296.     work_in[0] = Getrez()+2;
  297.     work_in[10] = 2;
  298.     vdi_handle = phys_handle;
  299.     v_opnvwk(work_in, &vdi_handle, work_out);
  300.     lastheight = lasttextcolor = lastfillcolor = lastwrmode = -1;
  301.     lasteffects = 0;
  302.     lastindex = laststyle = lastfont = 1;
  303.     gl_numfonts = work_out[10];
  304.     if (gdos_active)
  305.         gl_numfonts += vst_load_fonts(vdi_handle, 0);
  306.     vq_extnd(vdi_handle, 1, work_out);
  307.     gl_screenplanes = work_out[4];
  308. }
  309.  
  310. void
  311. close_vwork()
  312. {
  313.     if (vdi_handle > 0) {
  314.         if (gdos_active)
  315.             vst_unload_fonts(vdi_handle, 0);
  316.         v_clsvwk(vdi_handle);
  317.     }
  318.     vdi_handle = -1;
  319. }
  320.  
  321. void
  322. init_gem()
  323. {
  324.     extern int vq_gdos();
  325.     extern short _app;
  326.     long gdosvers;
  327.  
  328.     gl_apid = appl_init();
  329.     phys_handle = graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
  330.     wind_get(0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
  331.     gdos_active = vq_gdos();
  332.     if (gdos_active) {
  333.         gdosvers = vq_vgdos();
  334.         if (gdosvers == GDOS_NONE) {
  335.             gdos_active = 0;
  336.         } else if (gdosvers == GDOS_FSM) {
  337.             gdos_active = FSMGDOS;
  338.         } else if (gdosvers == GDOS_FNT) {
  339.             gdos_active = FontGDOS;
  340.         } else {
  341.             gdos_active = GDOS;
  342.         }
  343.     }
  344.  
  345.     open_vwork();
  346.     if (_app) {
  347.         graf_mouse(ARROW, 0L);
  348.     }
  349. }
  350.  
  351. /*
  352.  * exit a gem application
  353.  */
  354.  
  355. void
  356. exit_gem(status)
  357.     int status;
  358. {
  359.     wind_update(0);    /* just in case */
  360.     end_windows();
  361.     close_vwork();
  362.     appl_exit();
  363.     exit(status);
  364. }
  365.  
  366. /*
  367.  * some more miscellaneous utility routines
  368.  */
  369.  
  370. void
  371. set_fillcolor(col)
  372.     int col;
  373. {
  374.     if (col == lastfillcolor) return;
  375.     vsf_color(vdi_handle, col);
  376.     lastfillcolor = col;
  377. }
  378.  
  379. void
  380. set_textcolor(col)
  381.     int col;
  382. {
  383.     if (col == lasttextcolor) return;
  384.     vst_color(vdi_handle, col);
  385.     lasttextcolor = col;
  386. }
  387.  
  388. void
  389. set_texteffects(effects)
  390.     int effects;
  391. {
  392.     if (effects == lasteffects) return;
  393.     lasteffects = vst_effects(vdi_handle, effects);
  394. }
  395.  
  396. void
  397. set_wrmode(mode)
  398.     int mode;
  399. {
  400.     if (mode == lastwrmode) return;
  401.     vswr_mode(vdi_handle, mode);
  402.     lastwrmode = mode;
  403. }
  404.  
  405. static int
  406. is_fsm_font(font)
  407.     int font;
  408. {
  409.     int i;
  410.  
  411.     for (i = 0; i < gl_numfonts; i++) {
  412.         if (fontdesc[i].fontidx == font)
  413.             return fontdesc[i].isfsm;
  414.     }
  415.     return 0;
  416. }
  417.  
  418. /* This function sets both the font and the size (in points).
  419.  */
  420.  
  421. void
  422. set_font(font, height)
  423.     int font, height;
  424. {
  425.     int cw, ch, bw, bh;
  426.  
  427.     if (font != lastfont) {
  428.         vst_font(vdi_handle, font);
  429.         lastfont = font;
  430.         lastheight = height-1;
  431.     }
  432.     if (height != lastheight) {
  433.         if (gdos_active == FSMGDOS && is_fsm_font(font)) {
  434.             lastheight = vst_arbpt(vdi_handle, height,
  435.                 &cw, &ch, &bw, &bh);
  436.         } else {
  437.             lastheight = vst_point(vdi_handle, height, &cw, &ch,
  438.                  &bw, &bh);
  439.         }
  440.     }
  441. }
  442.  
  443. void
  444. set_fillstyle(style, index)
  445.     int style, index;
  446. {
  447.     if (laststyle != style)
  448.         laststyle = vsf_interior(vdi_handle, style);
  449.  
  450.     if (index != lastindex)
  451.         lastindex = vsf_style(vdi_handle, index);
  452. }
  453.