home *** CD-ROM | disk | FTP | other *** search
/ Dream 48 / Amiga_Dream_48.iso / Atari / c / libs / xaes_new.lzh / EDIT.C < prev    next >
C/C++ Source or Header  |  1994-10-26  |  13KB  |  394 lines

  1. /********************************************************************
  2.  *                                                                0.20*
  3.  *    XAES: Custom editing routines                                    *
  4.  *    Code by Ken Hollis, GNU C Extensions by Sascha Blank            *
  5.  *                                                                    *
  6.  *    Copyright (C) 1994, Bitgate Software                            *
  7.  *                                                                    *
  8.  *    Oh no, my first attempt at custom editing routines and they're    *
  9.  *    slow as hell!  I better 'borrow' Christian Grunenberg's OBJCEDIT*
  10.  *    routines if I want to get anywhere...                            *
  11.  *                                                                    *
  12.  ********************************************************************/
  13.  
  14. #include <string.h>
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18.  
  19. #include "xaes.h"
  20. #include "nkcc.h"
  21.  
  22. int edit_old_x, edit_old_y;
  23.  
  24. GLOBAL char *WGetEditText(char *te_ptmplt, char *te_ptext)
  25. {
  26.     char *ret;
  27.     int i, j, len;
  28.  
  29.     ret = (char *) malloc(strlen(te_ptmplt));
  30.  
  31.     len = (int)(strlen(te_ptmplt) - (int)(strlen(te_ptext)));
  32.  
  33.     i = 0;
  34.     while(i != len) {
  35.         ret[i] = te_ptmplt[i++];
  36.     }
  37.  
  38.     len = (int)(strlen(te_ptext));
  39.  
  40.     j = 0;
  41.     while(j != len) {
  42.         ret[i++] = te_ptext[j++];
  43.     }
  44.  
  45.     ret[i] = '\0';
  46.  
  47.     return(ret);
  48. }
  49.  
  50. GLOBAL void WObjc_Edit(WINDOW *win, int mode, int key, int kstate)
  51. {
  52.     int tophandle;
  53.  
  54.     WWindGet(win, WF_TOP, &tophandle);
  55.     win = WFindHandle(tophandle);
  56.  
  57.     if (win) {
  58.         WMoveWindow(win, -1, -1, -1, -1);
  59.  
  60.     if ((WFindOwner(tophandle)) && (win->edobject) && !(win->state & W_ICONIFIED)) {
  61.     if (mode == ED_INIT)
  62.         if ((win->tree[win->edobject].ob_type & 0xFF) != G_USERDEF) {
  63.             int pos = ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) - 
  64.                        (win->tree[win->edobject].ob_spec.tedinfo->te_txtlen)) +
  65.                         win->edpos;
  66.             int x, y, ourw, ourh, dummy;
  67.  
  68.             vst_height(VDIhandle, win->tree[win->edobject].ob_spec.tedinfo->te_font == 3 ? large_font : small_font,
  69.                     &dummy, &dummy, &ourw, &ourh);
  70.  
  71.             if (win->style & W_CUSTOMWINDOW) {
  72.                 if (win->wind_type == WC_WINDOW) {
  73.                     x = win->size.g_x + win->wind[25].ob_x + win->tree[win->edobject].ob_x;
  74.                     y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  75.                 }
  76.  
  77.                 if (win->wind_type == WC_SWINDOW) {
  78.                     x = win->size.g_x + win->wind[32].ob_x + win->tree[win->edobject].ob_x;
  79.                     y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  80.                 }
  81.             } else {
  82.                 x = win->tree->ob_x + win->tree[win->edobject].ob_x;
  83.                 y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  84.             }
  85.  
  86.             x += (pos * ourw);
  87.             x += 2; y += 2;
  88.             y++;
  89.  
  90.             if (win->editmode == EDIT_INSERT)
  91.                 WObjc_Draw(win, x, y, 1, ourh);
  92.             else
  93.                 WObjc_Draw(win, x, y, ourw + 1, ourh);
  94.  
  95.             vswr_mode(VDIhandle, MD_XOR);
  96.             vsl_color(VDIhandle, BLACK);
  97.             vsf_interior(VDIhandle, 1);
  98.  
  99.             WGrafMouse(M_OFF);
  100.  
  101.             if (win->editmode == EDIT_INSERT)
  102.                 line(x, y, x, y + ourh);
  103.             else
  104.                 box_fill(x, y, ourw + 1, ourh, BLACK);
  105.  
  106.             WGrafMouse(M_ON);
  107.             vsf_interior(VDIhandle, 0);
  108.             vswr_mode(VDIhandle, MD_REPLACE);
  109.         }
  110.  
  111.     if (mode == ED_END)
  112.     if ((win->tree[win->edobject].ob_type & 0xFF) != G_USERDEF) {
  113.             int pos = ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) - 
  114.                        (win->tree[win->edobject].ob_spec.tedinfo->te_txtlen)) +
  115.                         win->edpos;
  116.             int x, y, ourw, ourh, dummy;
  117.  
  118.             vst_height(VDIhandle, win->tree[win->edobject].ob_spec.tedinfo->te_font == 3 ? large_font : small_font,
  119.                     &dummy, &dummy, &ourw, &ourh);
  120.  
  121.             if (win->style & W_CUSTOMWINDOW) {
  122.                 if (win->wind_type == WC_WINDOW) {
  123.                     x = win->size.g_x + win->wind[25].ob_x + win->tree[win->edobject].ob_x;
  124.                     y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  125.                 }
  126.  
  127.                 if (win->wind_type == WC_SWINDOW) {
  128.                     x = win->size.g_x + win->wind[32].ob_x + win->tree[win->edobject].ob_x;
  129.                     y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  130.                 }
  131.             } else {
  132.                 x = win->tree->ob_x + win->tree[win->edobject].ob_x;
  133.                 y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  134.             }
  135.  
  136.             x += (pos * ourw);
  137.             x += 2; y += 2;
  138.  
  139.             y++;
  140.  
  141.             WGrafMouse(M_OFF);
  142.  
  143.             if (win->editmode == EDIT_INSERT)
  144.                 WObjc_Draw(win, x, y, 1, ourh);
  145.             else
  146.                 WObjc_Draw(win, x, y, ourw + 1, ourh);
  147.  
  148.             if (win->style & W_CUSTOMWINDOW) {
  149.                 if (win->wind_type == WC_WINDOW)
  150.                     x = win->size.g_x + win->wind[25].ob_x + win->tree[win->edobject].ob_x +
  151.                         ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) * ourw) - 5;
  152.  
  153.                 if (win->wind_type == WC_SWINDOW)
  154.                     x = win->size.g_x + win->wind[32].ob_x + win->tree[win->edobject].ob_x +
  155.                         ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) * ourw) - 5;
  156.             } else
  157.                 x = win->size.g_x + win->tree[win->edobject].ob_x +
  158.                     ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) * ourw) - 5;
  159.  
  160.             if (win->editmode == EDIT_REPLACE)
  161.                 if (win->edpos == (win->tree[win->edobject].ob_spec.tedinfo->te_txtlen - 1))
  162.                     WObjc_Draw_Out(win, x, y, ourw + 2, ourh);
  163.  
  164.             WGrafMouse(M_ON);
  165.  
  166.             vsf_interior(VDIhandle, 0);
  167.             vswr_mode(VDIhandle, MD_REPLACE);
  168.         }
  169.  
  170.     if (mode == ED_CHAR)
  171.         if ((win->tree[win->edobject].ob_type & 0xFF) != G_USERDEF) {
  172.             int pos = ((win->tree[win->edobject].ob_spec.tedinfo->te_tmplen) - 
  173.                        (win->tree[win->edobject].ob_spec.tedinfo->te_txtlen)) +
  174.                         win->edpos;
  175.             int x, y, n_key, ourw, ourh, dummy;
  176.  
  177.             int maxpos = win->tree[win->edobject].ob_spec.tedinfo->te_txtlen;
  178.             int tmplen = win->tree[win->edobject].ob_spec.tedinfo->te_tmplen;
  179.             BOOL cango = FALSE;
  180.  
  181.             n_key = nkc_tconv((key & 0xff) | (((long) key & 0xff00) << 8) | ((long) kstate << 24)) & ~(NKF_FUNC | NKF_RESVD | NKF_NUM);
  182.  
  183.             vst_height(VDIhandle, win->tree[win->edobject].ob_spec.tedinfo->te_font == 3 ? large_font : small_font,
  184.                     &dummy, &dummy, &ourw, &ourh);
  185.  
  186.             if (win->style & W_CUSTOMWINDOW) {
  187.                 if (win->wind_type == WC_WINDOW) {
  188.                     x = win->size.g_x + win->wind[25].ob_x + win->tree[win->edobject].ob_x;
  189.                     y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  190.                 }
  191.  
  192.                 if (win->wind_type == WC_SWINDOW) {
  193.                     x = win->size.g_x + win->wind[32].ob_x + win->tree[win->edobject].ob_x;
  194.                     y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  195.                 }
  196.             } else {
  197.                 x = win->tree->ob_x + win->tree[win->edobject].ob_x;
  198.                 y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  199.             }
  200.  
  201.             x += (pos * ourw);
  202.  
  203.             x += 2; y += 2;
  204.  
  205.             WObjc_Edit(win, ED_END, 0, 0);
  206.  
  207.             switch(n_key) {
  208.                 case NKF_CTRL | ' ':
  209.                 case NKF_CTRL | '`':
  210.                 case NKF_CTRL | '2':
  211.                 case NKF_RESVD:
  212.                 case NK_INVALID:
  213.                 case NKF_CTRL | NK_F1:
  214.                 case NKF_CTRL | NK_F2:
  215.                 case NKF_CTRL | NK_F3:
  216.                 case NKF_CTRL | NK_F4:
  217.                 case NKF_CTRL | NK_F5:
  218.                 case NKF_CTRL | NK_F6:
  219.                 case NKF_CTRL | NK_F7:
  220.                 case NKF_CTRL | NK_F8:
  221.                 case NKF_CTRL | NK_F9:
  222.                 case NKF_CTRL | NK_F10:
  223.                 case NKF_CTRL | NK_UP:
  224.                 case NKF_CTRL | NK_DOWN:
  225.                 case NKF_CTRL | NK_LEFT:
  226.                 case NKF_CTRL | NK_RIGHT:
  227.                     cango = FALSE;
  228.                     break;
  229.  
  230.                 default:
  231.                     cango = TRUE;
  232.                     break;
  233.             }
  234.  
  235.             if ((cango) && !(n_key & NKF_ALT))
  236.                 switch(n_key & 0xFF) {
  237.                     case NK_ESC:
  238.                         if (win->style & W_CUSTOMWINDOW) {
  239.                             if (win->wind_type == WC_WINDOW) {
  240.                                 x = win->size.g_x + win->wind[25].ob_x + (win->tree[win->edobject].ob_x + ((tmplen - maxpos) * ourw));
  241.                                 y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  242.                             }
  243.  
  244.                             if (win->wind_type == WC_SWINDOW) {
  245.                                 x = win->size.g_x + win->wind[32].ob_x + (win->tree[win->edobject].ob_x + ((tmplen - maxpos) * ourw));
  246.                                 y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  247.                             }
  248.                         } else {
  249.                             x = win->tree->ob_x + (win->tree[win->edobject].ob_x + ((tmplen - maxpos) * ourw));
  250.                             y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  251.                         }
  252.  
  253.                         memset(win->tree[win->edobject].ob_spec.tedinfo->te_ptext, 95, maxpos);
  254.                         win->tree[win->edobject].ob_spec.tedinfo->te_ptext[0] = '\0';
  255.                         win->edpos = 0;
  256.  
  257.                         x += 2; y += 2;
  258.  
  259.                         WObjc_Draw(win, x, y - 2, win->tree[win->edobject].ob_width - ((tmplen - maxpos) * ourw), win->tree[win->edobject].ob_height + 4);
  260.                         break;
  261.  
  262.                     case NK_DEL:
  263.                         {
  264.                             int start, finish;
  265.  
  266.                             start = win->edpos;
  267.                             finish = maxpos - 1;
  268.  
  269.                             do {
  270.                                 win->tree[win->edobject].ob_spec.tedinfo->te_ptext[start] =
  271.                                 win->tree[win->edobject].ob_spec.tedinfo->te_ptext[start + 1];
  272.                                 start++;
  273.                             } while (start < finish);
  274.  
  275.                             if (win->style & W_CUSTOMWINDOW) {
  276.                                 if (win->wind_type == WC_WINDOW) {
  277.                                     x = win->size.g_x + win->wind[25].ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  278.                                     y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  279.                                 }
  280.  
  281.                                 if (win->wind_type == WC_SWINDOW) {
  282.                                     x = win->size.g_x + win->wind[32].ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  283.                                     y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  284.                                 }
  285.                             } else {
  286.                                 x = win->tree->ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  287.                                 y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  288.                             }
  289.  
  290.                             x += 2; y += 2;
  291.  
  292.                             WObjc_Draw(win, x, y - 2, win->tree[win->edobject].ob_width - (((tmplen - maxpos) + win->edpos) * ourw), win->tree[win->edobject].ob_height + 4);
  293.                         }
  294.                         break;
  295.  
  296.                     case NK_BS:
  297.                         {
  298.                             int start, finish;
  299.  
  300.                             start = win->edpos;
  301.                             finish = maxpos - 1;
  302.  
  303.                             if (start != 0) {
  304.                                 win->edpos--;
  305.  
  306.                                 if (win->edpos < 0)
  307.                                     win->edpos = 0;
  308.  
  309.                                 start = win->edpos;
  310.  
  311.                                 do {
  312.                                     win->tree[win->edobject].ob_spec.tedinfo->te_ptext[start] =
  313.                                     win->tree[win->edobject].ob_spec.tedinfo->te_ptext[start + 1];
  314.                                     start++;
  315.                                 } while (start < finish);
  316.  
  317.                                 if (win->style & W_CUSTOMWINDOW) {
  318.                                     if (win->wind_type == WC_WINDOW) {
  319.                                         x = win->size.g_x + win->wind[25].ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  320.                                         y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  321.                                     }
  322.  
  323.                                     if (win->wind_type == WC_SWINDOW) {
  324.                                         x = win->size.g_x + win->wind[32].ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  325.                                         y = win->size.g_y + win->wind[32].ob_y + win->tree[win->edobject].ob_y;
  326.                                     }
  327.                                 } else {
  328.                                     x = win->tree->ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  329.                                     y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  330.                                 }
  331.  
  332.                                 x += 2; y += 2;
  333.                             }
  334.  
  335.                             WObjc_Draw(win, x, y - 2, win->tree[win->edobject].ob_width - (((tmplen - maxpos) + win->edpos) * ourw), win->tree[win->edobject].ob_height + 4);
  336.                         }
  337.                         break;
  338.                         
  339.                     default:
  340.                         if (win->editmode == EDIT_REPLACE) {
  341.                             win->tree[win->edobject].ob_spec.tedinfo->te_ptext[win->edpos] = key;
  342.                             win->tree[win->edobject].ob_spec.tedinfo->te_ptext[maxpos - 1] = '\0';
  343.                             WObjc_Draw(win, x, y, gr_cw + 1, gr_ch + 1);
  344.                             win->edpos++;
  345.                         } else {
  346.                             int start, finish;
  347.                             char *str;
  348.  
  349.                             finish = win->edpos - 1;
  350.                             start = maxpos - 2;
  351.  
  352.                             str = win->tree[win->edobject].ob_spec.tedinfo->te_ptext;
  353.  
  354.                             if (start > finish)
  355.                                 do {
  356.                                     str[start + 1] = str[start];
  357.                                     start--;
  358.                                 } while (start > finish);
  359.  
  360.                             win->tree[win->edobject].ob_spec.tedinfo->te_ptext[win->edpos] = key;
  361.                             if ((win->wind) && (win->style & W_CUSTOMWINDOW)) {
  362.                                 x = win->size.g_x + win->wind[25].ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  363.                                 y = win->size.g_y + win->wind[25].ob_y + win->tree[win->edobject].ob_y;
  364.                             } else {
  365.                                 x = win->tree->ob_x + (win->tree[win->edobject].ob_x + (((tmplen - maxpos) + win->edpos) * ourw));
  366.                                 y = win->tree->ob_y + win->tree[win->edobject].ob_y;
  367.                             }
  368.  
  369.                             x += 2; y += 2;
  370.  
  371.                             str[maxpos - 1] = '\0';
  372.                             win->tree[win->edobject].ob_spec.tedinfo->te_ptext = str;
  373.                             win->edpos++;
  374.  
  375.                             if (win->edpos > (maxpos - 1))
  376.                                 win->edpos = (maxpos - 1);
  377.  
  378.                             WObjc_Draw(win, x, y - 2, win->tree[win->edobject].ob_width - (((tmplen - maxpos) + (win->edpos - 1)) * ourw), win->tree[win->edobject].ob_height + 4);
  379.                         }
  380.  
  381.                     break;
  382.                 }
  383.  
  384.             if (win->edpos > (maxpos - 1))
  385.                 win->edpos = (maxpos - 1);
  386.  
  387.             WObjc_Edit(win, ED_INIT, 0, 0);
  388.  
  389.             vsf_interior(VDIhandle, 0);
  390.             vswr_mode(VDIhandle, MD_REPLACE);
  391.         }
  392.     }
  393.     }
  394. }