home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNU_C++ / LIB / CFLIB-11.LZH / src / asciitab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-03  |  6.0 KB  |  299 lines

  1. #include "intern.h"
  2. #include "INLINE.H"
  3.  
  4. static int    last_ascii;
  5.  
  6. static void _redraw_obj(OBJECT *tree, int obj)
  7. {
  8.     GRECT    r;
  9.     
  10.     get_objframe(tree, obj, &r);
  11. #ifdef __MTAES__
  12.     objc_draw(tree, obj, 1, &r);
  13. #else
  14.     objc_draw(tree, obj, 1, r.g_x, r.g_y, r.g_w, r.g_h);
  15. #endif
  16. }
  17.  
  18. static void set_numbers(int code)
  19. {
  20.     char    str[6];
  21.  
  22.     sprintf(str, "%03d", code);
  23.     set_string(cf_ascii_tab, AT_DEZ, str);
  24.     _redraw_obj(cf_ascii_tab, AT_DEZ);
  25.  
  26.     sprintf(str, "0x%02X", code);
  27.     set_string(cf_ascii_tab, AT_HEX, str);
  28.     _redraw_obj(cf_ascii_tab, AT_HEX);
  29. }
  30.  
  31. static void draw_ascii(int id, int pts)
  32. {
  33.     int    wc, hc, wb, hb, d, i, x, y;
  34.     GRECT    r;
  35.     char    c[2] = "x";
  36.     
  37.     vst_font(cf_vdi_handle, id);
  38.     vst_point(cf_vdi_handle, pts, &wc, &hc, &wb, &hb);
  39.     vst_alignment(cf_vdi_handle, TA_CENTER, TA_TOP, &d, &d);
  40.     vswr_mode(cf_vdi_handle, MD_TRANS);
  41.     vst_color(cf_vdi_handle, 1);
  42.  
  43.     get_objframe(cf_ascii_tab, AT_BOX, &r);
  44.     set_clipping(cf_vdi_handle, r.g_x, r.g_y, r.g_w, r.g_h, TRUE);
  45.     x = r.g_x + gl_wbox / 2;
  46.     y = r.g_y + 1;
  47.     for (i = 0; i < 256; i++)
  48.     {
  49.         c[0] = i;
  50.         v_gtext(cf_vdi_handle, x, y, c); 
  51.         x += gl_wbox;
  52.         if (((i+1) % 16) == 0)
  53.         {
  54.             y += gl_hbox;
  55.             x = r.g_x + gl_wbox / 2;
  56.         }    
  57.     }
  58.     set_clipping(cf_vdi_handle, r.g_x, r.g_y, r.g_w, r.g_h, FALSE);
  59. }
  60.  
  61. static int get_ascii(int x, int y)
  62. {
  63.     int    x1, y1;
  64.     
  65.     objc_offset(cf_ascii_tab, AT_BOX, &x1, &y1);
  66.     x -= x1;
  67.     y -= y1;
  68.     if ((x < 0) || (y < 0))
  69.         return -1;
  70.  
  71.     x /= gl_wbox;
  72.     y /= gl_hbox;
  73.     if ((x < 16) && (y < 16))
  74.         return (x + y * 16);
  75.     else
  76.         return -1;
  77. }
  78.  
  79. static void select_ascii(int obj)
  80. {
  81.     int    pxy[4], x, y;
  82.  
  83.     objc_offset(cf_ascii_tab, AT_BOX, &x, &y);
  84.     pxy[0] = x + (obj % 16) * gl_wbox;
  85.     pxy[1] = y + (obj / 16) * gl_hbox;
  86.     pxy[2] = pxy[0] + gl_wbox - 1;
  87.     pxy[3] = pxy[1] + gl_hbox - 1;
  88.  
  89.     graf_mouse(M_OFF, NULL);
  90.     vsf_color(cf_vdi_handle, BLACK);
  91.     vsf_interior(cf_vdi_handle, FIS_SOLID);
  92.     vswr_mode(cf_vdi_handle, MD_XOR);
  93.     v_bar(cf_vdi_handle, pxy);
  94.     graf_mouse(M_ON, NULL);
  95. }
  96.  
  97. static void jump_to(int new)
  98. {
  99.     if (last_ascii != -1)
  100.         select_ascii(last_ascii);
  101.     select_ascii(new);
  102.     last_ascii = new;
  103.     set_numbers(new);
  104. }
  105.  
  106. int ascii_table(int id, int pts)
  107. {
  108.     int    event, mx, my, kstate, kreturn, breturn, bclicks, 
  109.             d, obj, code = -1;
  110.     MFDB    background;
  111.     GRECT    r;
  112.     int    quit;
  113. #ifdef __MTAES__
  114.     GRECT        null = {0,0,0,0};
  115.     GRECT        r3;
  116.     EVNTDATA    ev;
  117. #endif
  118.  
  119.     set_string(cf_ascii_tab, AT_DEZ, "000");
  120.     set_string(cf_ascii_tab, AT_HEX, "0x00");
  121.  
  122.     wind_update(BEG_UPDATE);
  123.     wind_update(BEG_MCTRL);
  124. #ifdef __MTAES__
  125.     form_center(cf_ascii_tab, &r);
  126. #else
  127.     form_center(cf_ascii_tab, &r.g_x, &r.g_y, &r.g_w, &r.g_h);
  128. #endif
  129.     save_background(&r, &background);
  130. #ifdef __MTAES__
  131.     form_dial(FMD_START, &null, &r);
  132. #else
  133.     form_dial(FMD_START, 0, 0, 0, 0, r.g_x, r.g_y, r.g_w, r.g_h);
  134. #endif
  135.     graf_mouse(M_OFF, NULL);
  136. #ifdef __MTAES__
  137.     objc_draw(cf_ascii_tab, ROOT, MAX_DEPTH, &r); 
  138. #else
  139.     objc_draw(cf_ascii_tab, ROOT, MAX_DEPTH, r.g_x, r.g_y, r.g_w, r.g_h); 
  140. #endif
  141.     draw_ascii(id, pts); 
  142.     graf_mouse(M_ON, NULL);
  143.  
  144.     last_ascii = -1;
  145.     quit = FALSE;
  146. #ifdef __MTAES__
  147.     graf_mkstate(&ev);
  148. #else
  149.     graf_mkstate(&mx, &my, &d, &d);
  150. #endif
  151.     do
  152.     {
  153. #ifdef __MTAES__
  154.         r3.g_x = ev.x;
  155.         r3.g_y = ev.y;
  156.         r3.g_w = 1;
  157.         r3.g_h = 1;
  158.         event = evnt_multi((MU_BUTTON|MU_KEYBD|MU_M1),
  159.                                     1, 1, 1,
  160.                                     1, &r3,
  161.                                     0, &null,
  162.                                     NULL, 0,
  163.                                     &ev, &kreturn, &bclicks);
  164.         mx = ev.x;
  165.         my = ev.y;
  166.         breturn = ev.bstate;
  167.         kstate = ev.kstate;
  168. #else
  169.         event = evnt_multi(MU_BUTTON|MU_KEYBD|MU_M1,
  170.                                     1, 1, 1,
  171.                                     1, mx, my, 1, 1,
  172.                                     0, 0, 0, 0, 0,
  173.                                     NULL, 0,
  174.                                     &mx, &my, &breturn, &kstate, &kreturn, &bclicks);
  175. #endif
  176.         if (event & MU_M1)
  177.         {
  178.             obj = get_ascii(mx, my);
  179.             if ((obj != -1) && (obj != last_ascii))
  180.                 jump_to(obj);
  181.         }
  182.         if (event & MU_BUTTON)
  183.         {
  184.             obj = get_ascii(mx, my);
  185.             if (obj != -1)
  186.             {
  187.                 code = obj;
  188.                 quit = TRUE;
  189.             }
  190.             else
  191.             {
  192.                 if (objc_find(cf_ascii_tab, ROOT, MAX_DEPTH, mx, my) == AT_ABBRUCH)
  193.                 {
  194.                     code = -1;
  195.                     quit = TRUE;
  196.                 }
  197.                 else
  198.                     Bconout(2, 7);
  199.             }
  200.         }
  201.         if (event & MU_KEYBD)
  202.         {
  203.             int scan, d;
  204.  
  205.             scan = kreturn >> 8;
  206.             switch (scan)
  207.             {
  208.                 case 0x61:                        /* UNDO */
  209.                     code = -1;
  210.                     quit = TRUE;
  211.                     break;
  212.                     
  213.                 case 0x1C:                        /* Return */
  214.                 case 0x72:                        /* Enter */
  215.                     if (last_ascii != -1)
  216.                         code = last_ascii;
  217.                     else
  218.                         code = -1;
  219.                     quit = TRUE;
  220.                     break;
  221.                 
  222.                 case 0x47:                        /* HOME */
  223.                     if (kstate & K_SHIFT)
  224.                         jump_to(255);
  225.                     else
  226.                         jump_to(0);
  227.                     break;
  228.  
  229.                 case 0x4F :                        /* MagiCMac: anti-HOME */
  230.                     jump_to(255);
  231.                     break;
  232.  
  233.                 case 0x4B:                        /* Left */
  234.                     if (last_ascii == -1)
  235.                         jump_to(0);
  236.                     else
  237.                     {
  238.                         if (kstate & K_SHIFT)
  239.                         {
  240.                             d = last_ascii % 16;
  241.                             jump_to(last_ascii - d);
  242.                         }
  243.                         else if (last_ascii > 0)
  244.                             jump_to(last_ascii - 1);
  245.                     }
  246.                     break;
  247.  
  248.                 case 0x4D:                        /* Right */
  249.                     if (last_ascii == -1)
  250.                         jump_to(0);
  251.                     else
  252.                     {
  253.                         if (kstate & K_SHIFT)
  254.                         {
  255.                             d = last_ascii % 16;
  256.                             jump_to(last_ascii + (15 - d));
  257.                         }
  258.                         else if (last_ascii < 255)
  259.                             jump_to(last_ascii + 1);
  260.                     }
  261.                     break;
  262.  
  263.                 case 0x48:                        /* Up */
  264.                     if (last_ascii == -1)
  265.                         jump_to(0);
  266.                     else if (last_ascii > 15)
  267.                         jump_to(last_ascii - 16);
  268.                     break;
  269.                 case 0x50:                        /* Down */
  270.                     if (last_ascii == -1)
  271.                         jump_to(0);
  272.                     else if (last_ascii < 240)
  273.                         jump_to(last_ascii + 16);
  274.                     break;
  275.             }
  276.         }
  277.     }
  278.     while (!quit);
  279.  
  280. #ifdef __MTAES__
  281.     form_dial(FMD_FINISH, &r, &null);
  282. #else
  283.     form_dial(FMD_FINISH, 0, 0, 0, 0, r.g_x, r.g_y, r.g_w, r.g_h);
  284. #endif
  285.     restore_background(&r, &background);
  286.     wind_update(END_MCTRL);
  287.     wind_update(END_UPDATE);
  288.  
  289.     return code;
  290. }
  291.  
  292. void set_asciitable_strings(char *title, char *button)
  293. {
  294.     if (title[0] != EOS)
  295.         set_string(cf_ascii_tab, AT_TITLE, title);
  296.     if (button[0] != EOS)
  297.         set_string(cf_ascii_tab, AT_ABBRUCH, button);
  298. }
  299.