home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / languages / progs / curses / amiga / c / window < prev   
Encoding:
Text File  |  1993-07-26  |  6.6 KB  |  278 lines

  1. /************************ RISCOS stuff **************************/
  2.  
  3. #include "wimp.h"
  4. #include "wimpt.h"
  5. #include "event.h"
  6. #include "sprite.h"
  7. #include "res.h"
  8. #include "template.h"
  9. #include "werr.h"
  10. #include "colourtran.h"
  11. #include "kernel.h"
  12. #include "swis.h"
  13. #include "swiv.h"
  14. #include "bbc.h"
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #include "window.h"
  21.  
  22. static int wimpversion;
  23. static wimp_w w;
  24. static sprite_state s;
  25. static sprite_area *area;
  26. static int *save_area;
  27. static char *pixtrans;
  28. static int osx,osy;
  29. static int xeig=1,yeig=2;
  30. static int charx,chary;
  31. static int mode = 12;
  32. static int l2bpp = 2;
  33. static int bpp = 4;
  34.  
  35. static int pal16[] = { /* bbggrrnn */
  36.   0x00000000, 0xf0f0f001, 0x00f0f002, 0x0080f003, 0xf0000004, 0xf000f005, 0xf0f00006, 0xf0f0f007, 
  37.   0x00206008, 0x0050e009, 0x10f0900a, 0x00b0e00b, 0xf050500c, 0xf020900d, 0x80f0000e, 0xc0c0c00f, 
  38. };
  39.  
  40.  
  41. static void coltable
  42. ( void )
  43. {
  44.   wimpt_noerr(colourtran_select_table(mode,(wimp_paletteword *)pal16,-1,(wimp_paletteword *)-1,pixtrans));
  45. }
  46.  
  47.  
  48. static sprite_id *SprPtr
  49. (sprite_header *s)
  50. {
  51.         static sprite_id id;
  52.         id.tag = sprite_id_addr;
  53.         id.s.addr = s;
  54.         return &id;
  55. }
  56.  
  57. static sprite_id *SprNam
  58. (char *s)
  59. {
  60.         static sprite_id id;
  61.         id.tag = sprite_id_name;
  62.         id.s.name = s;
  63.         return &id;
  64. }
  65.  
  66. static void grabcaret
  67. ( void )
  68. { wimp_caretstr c;
  69.   c.w = w;
  70.   c.i = -1;
  71.   c.x = 100;
  72.   c.y = -100;
  73.   c.height = 0|(1<<25); /* invisible */
  74.   c.index = 0;
  75.   wimpt_noerr(wimp_set_caret_pos(&c));
  76. }
  77.  
  78. void window_init( int m, int x, int y )
  79. {
  80.   wimp_wind *t;
  81.   wimp_redrawstr r;
  82.   wimp_wstate state;
  83.   int size;
  84.   mode = m;
  85.   if (!x)
  86.     x = bbc_modevar(mode,bbc_XWindLimit)+1;
  87.   if (!y)
  88.     y = bbc_modevar(mode,bbc_YWindLimit)+1;
  89.   l2bpp = bbc_modevar(mode,bbc_Log2BPP);
  90.   bpp = 1 << l2bpp;
  91.   xeig=bbc_modevar(mode,bbc_XEigFactor);
  92.   yeig=bbc_modevar(mode,bbc_YEigFactor);
  93.   osx = x<<xeig;
  94.   osy = y<<yeig;
  95.   charx = 8 << xeig;
  96.   chary = (bbc_modevar(mode,bbc_ModeFlags)&(1<<5)?16:8) << yeig;
  97.     wimpversion = wimpt_init("Curses");
  98. #ifdef DEBUG
  99.     signal(SIGABRT, SIG_DFL);
  100.     signal(SIGFPE, SIG_DFL);
  101.     signal(SIGILL, SIG_DFL);
  102.     signal(SIGSEGV, SIG_DFL);
  103.     signal(SIGTERM, SIG_DFL);
  104. #endif
  105.     res_init("curses");
  106.     template_init();
  107.   t = template_syshandle( "window" );
  108.   wimpt_noerr( wimp_create_wind( t, &w ) );
  109.   size = sizeof(sprite_area)+sizeof(sprite_header)+x*y*bpp/8;
  110.   area = malloc(size);
  111.   sprite_area_initialise(area, size);
  112.   wimpt_noerr(sprite_create(area, "screen", sprite_nopalette, x, y, mode));
  113.   wimpt_noerr(sprite_sizeof_spritecontext(area, SprNam("screen"), &size));
  114.   save_area = malloc(size);
  115.   save_area[0]=0;
  116.   pixtrans = malloc(1<<bpp);
  117.   coltable();
  118.   r.w = w;
  119.   r.box.x0 = 0; r.box.x1 = osx; r.box.y0 = -osy; r.box.y1 = 0;
  120.   wimpt_noerr( wimp_get_wind_state( w, &state ) );
  121.   if (state.o.box.x1 > state.o.box.x0+osx)
  122.       state.o.box.x1 = state.o.box.x0+osx;
  123.   if (state.o.box.y0 < state.o.box.y1-osy)
  124.       state.o.box.y0 = state.o.box.y1-osy;
  125.   if (state.o.x < osx - (state.o.box.x1 - state.o.box.x0))
  126.       state.o.x = osx - (state.o.box.x1 - state.o.box.x0);
  127.   if (state.o.y < (state.o.box.y1 - state.o.box.y0) - osy)
  128.       state.o.y = (state.o.box.y1 - state.o.box.y0) - osy;
  129.   wimpt_noerr(wimp_open_wind(&state.o));
  130.   wimpt_noerr(wimp_set_extent(&r));
  131.   wimpt_noerr(wimp_force_redraw(&r));
  132.   grabcaret();
  133.   wimpt_noerr(sprite_outputtosprite(area,  SprNam("screen"), save_area, &s));
  134.   _swix(OS_ChangedBox,_IN(R0),1);
  135.   _swix(OS_ChangedBox,_IN(R0),2);
  136. }
  137.  
  138. void window_finish
  139. (void)
  140. {
  141.   wimpt_noerr(sprite_restorestate(s));
  142.   wimpt_noerr(wimp_closedown());
  143.   free(area);
  144.   free(save_area);
  145.   free(pixtrans);
  146. }
  147.  
  148. void window_sleep
  149. (int n)
  150. {
  151. /* should use PollIdle */
  152.   n = _swi(OS_ReadMonotonicTime,0)+n*100;
  153.   do
  154.   { window_getchar();
  155.   } while (n>_swi(OS_ReadMonotonicTime,0));
  156. }
  157.  
  158. int window_getchar
  159. (void)
  160. {
  161.   int *box;
  162.   wimp_redrawstr r;
  163.   wimp_eventstr ev,*e=&ev;
  164.   int key;
  165.   int more;
  166.   _swix(OS_ChangedBox,_IN(R0)|_OUT(R1),-1,&box);
  167.   r.w = w;
  168.   r.box.x0 = box[1]<<xeig;
  169.   r.box.y0 = (box[2]<<yeig)-osy;
  170.   r.box.x1 = (box[3]+1<<xeig);
  171.   r.box.y1 = (box[4]+1<<yeig)-osy;
  172.   _swix(OS_ChangedBox,_IN(R0),2);
  173.   if (r.box.x1>r.box.x0 && r.box.y1>r.box.y0)
  174.     wimpt_noerr(wimp_force_redraw(&r));
  175.   wimpt_noerr(sprite_restorestate(s));
  176.   wimp_save_fp_state_on_poll();
  177.   for (;;)
  178.   { wimpt_noerr(wimp_poll(0,e));
  179.     switch ( e->e )
  180.     {
  181.     case wimp_ENULL:
  182.       key = 0; goto ret;
  183.     case wimp_EREDRAW:
  184.       if (wimpt_checkmode())
  185.         coltable();
  186.       r.w = e->data.o.w;
  187.       wimpt_noerr(wimp_redraw_wind(&r,&more));
  188.       while (more)
  189.       {
  190.         wimpt_noerr(sprite_put_scaled( area, SprNam("screen"), 0,
  191.                  r.box.x0-r.scx, r.box.y1-r.scy-osy, NULL, pixtrans));
  192.         wimpt_noerr(wimp_get_rectangle(&r,&more));
  193.       }
  194.       break;
  195.     case wimp_EOPEN:
  196.       wimp_open_wind( &e->data.o );
  197.       break;
  198.     case wimp_EBUT:
  199.       {
  200.         switch ( e->data.but.m.bbits )
  201.         { case wimp_BCLICKLEFT:
  202.           case wimp_BCLICKRIGHT:
  203.           case wimp_BDRAGLEFT:
  204.           case wimp_BDRAGRIGHT:
  205.           case wimp_BRIGHT:
  206.           case wimp_BLEFT:
  207.             grabcaret();
  208.             break;
  209.         }
  210.         break;
  211.       }
  212.     case wimp_ECLOSE:
  213.       exit(0);
  214.     case wimp_ESEND: case wimp_ESENDWANTACK:
  215.       switch ( e->data.msg.hdr.action )
  216.       { case wimp_MCLOSEDOWN:
  217.           exit(0);
  218.           break;
  219.       }
  220.     case wimp_EKEY:
  221.       { key = e->data.key.chcode;
  222.         if (key>0xFF)
  223.           wimp_processkey(key);
  224.         else
  225.         {
  226. ret:      wimpt_noerr(sprite_outputtosprite(area, SprNam("screen"), save_area, &s));
  227.           return key;
  228.         }
  229.         continue;
  230.       }
  231.     }
  232.   }
  233. }
  234.  
  235. #define A_STANDOUT    0020000
  236. #define A_UNDERLINE   0000400
  237. #define A_REVERSE     0001000
  238. #define A_BLINK       0002000
  239. #define A_DIM         0004000
  240. #define A_BOLD        0010000
  241.  
  242. void TextAt(int x,int y,int attr,char *p,int n)
  243. { int fg,bg,t;
  244.   _kernel_swi_regs r;
  245.  
  246.   fg = attr&0xf;
  247.   bg = (attr>>4)&0xf;
  248.   if (attr&(A_STANDOUT|A_REVERSE))
  249.     t=fg,fg=bg,bg=t;
  250.   bbc_tab(x,y);
  251.   bbc_colour(fg);
  252.   bbc_colour(bg+128);
  253.   r.r[0] = (int) p; r.r[1] = n;
  254.   _kernel_swi(OS_WriteN,&r,&r);
  255.   if (attr&A_BOLD)
  256.   { bbc_vdu(bbc_TextToGraph);
  257.     bbc_gcol(0,fg);
  258.     bbc_move(x*charx+(1<<xeig),osy-y*chary-(1<<yeig));
  259.     r.r[0] = (int) p; r.r[1] = n;
  260.     _kernel_swi(OS_WriteN,&r,&r);
  261.     bbc_vdu(bbc_TextToText);
  262.   }
  263.   if (attr&A_UNDERLINE)
  264.   { bbc_gcol(0,fg);
  265.     bbc_move(x*charx,osy-(y+1)*chary);
  266.     bbc_drawby(n*charx,0);
  267.   }
  268. }
  269.  
  270. void TextRectFill(int x, int y,int w, int h, int c)
  271. {
  272.   if (c==-1)
  273.     bbc_gcol(4,0);
  274.   else
  275.     bbc_gcol(0,(c>>4)&0xf);
  276.   bbc_rectanglefill(x*charx,osy-(y+h)*chary,w*charx-(1<<xeig),h*chary-(1<<yeig));
  277. }
  278.