home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / sources / c / text < prev   
Encoding:
Text File  |  1990-02-12  |  7.6 KB  |  412 lines

  1. /* ><lst$Dir>.sources.c.Text */
  2. /* bindings for Little Smalltalk and Risc_OSLib */
  3.  
  4.  
  5. #include "<OSLib$Dir>.wimp.h"
  6. #include "<OSLib$Dir>.wimpt.h"
  7. #include "<OSLib$Dir>.win.h"
  8. #include "<OSLib$Dir>.event.h"
  9. #include "<OSLib$Dir>.baricon.h"
  10. #include "<OSLib$Dir>.res.h"
  11. #include "<OSLib$Dir>.resspr.h"
  12. #include "<OSLib$Dir>.menu.h"
  13. #include "<OSLib$Dir>.template.h"
  14. #include "<OSLib$Dir>.dbox.h"
  15. #include "<OSLib$Dir>.werr.h"
  16. #include "<OSLib$Dir>.flex.h"
  17. #include "<OSLib$Dir>.txt.h"
  18.  
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. #define min(a, b) ((a) < (b) ? a : b)
  25.  
  26. #define VERSION "1.00 (fms1.0)"
  27.  
  28. char TOutputBuffer[1024]; 
  29. static menu IMenu, TMenu;
  30.  
  31. txt Text;
  32. BOOL Execute;
  33.  
  34. void tprintf(char *s,...)
  35. {
  36.   va_list va;
  37.   
  38.   va_start(va, s); 
  39.  
  40.   vsprintf(TOutputBuffer, s, va);
  41.   s = TOutputBuffer;
  42.   while (*s) {
  43.     if (*s == '\t') *s = ' ';
  44.     s++;
  45.     }
  46.   txt_insertstring(Text, TOutputBuffer);
  47.   txt_movedot(Text, strlen(TOutputBuffer));
  48.  
  49.   va_end(va);
  50.   }
  51.  
  52. void tputchar(char c)
  53. {
  54.    c = (c == '\t') ? ' ' : c;
  55.    txt_insertchar(Text, c);
  56.    txt_movedot(Text, 1);
  57.    }
  58.  
  59. static char DBStr[256];
  60.  
  61. int extractselection(char **buff, BOOL addcr)
  62. {
  63.    char *p, *b;
  64.    int n, e, execPtr, execEnd, tLen;
  65.  
  66.    execPtr = txt_selectstart(Text);
  67.    execEnd = txt_selectend(Text);
  68.      
  69.    tLen = (execEnd - execPtr) + 1;
  70.    *buff = malloc((tLen + 1) * sizeof(char));
  71.    if (!buff) {
  72.      werr(0, "Selection too big.");
  73.      return 0;
  74.      }
  75.      
  76.    b = *buff;
  77.    e = 0;
  78.    n = 0;
  79.    while (execPtr < execEnd) {
  80.      if (e == n) {
  81.        txt_arrayseg(Text, execPtr, &p, &n);
  82.        e = 0;
  83.        }
  84.      *b++ = *p++;
  85.      e++;
  86.      execPtr++;
  87.      }
  88.    if (addcr) *b++ = '\n';
  89.    *b = '\0';
  90.  
  91.    return(tLen);
  92.    }
  93.  
  94.  
  95. char * tgets(char *buff, int n1, FILE *s)
  96. {
  97.    char *b, *p;
  98.    static char *tBuff, tOffs, tLen;
  99.                 
  100.    if (s != stdin) {
  101.      return(fgets(buff, n1, s));
  102.      }
  103.  
  104.    if (!Execute) {
  105.  
  106.      do {
  107.        event_process();
  108.        } while (!Execute);
  109.  
  110.      tLen = extractselection(&tBuff, 1);
  111.      tOffs = 0;
  112.      Execute = tLen;
  113.  
  114.      }
  115.  
  116.    if (Execute) {
  117.      b = tBuff + tOffs;
  118.      p = strchr(b, '\n');
  119.      Execute = (p != tBuff + tLen - 1);
  120.  
  121.      memcpy(buff, b, p - b);
  122.      buff[p - b] = '\n';
  123.      buff[1 + p - b] = '\0';
  124.  
  125.      if (!Execute) {
  126.        free(tBuff);
  127.        }
  128.      else {
  129.        tOffs += 1 + p - b;
  130.        }
  131.      }
  132.  
  133.    return(buff);
  134.    }
  135.  
  136. void copyselection(void)
  137. {
  138.   char *sel;
  139.  
  140.   if (txt_selectset(Text)) {
  141.     if (extractselection(&sel, 0)) {
  142.       txt_insertstring(Text, sel);
  143.       free(sel);
  144.       }
  145.     }
  146.   }
  147.  
  148. void deleteselection(void)
  149. {
  150.   txt_marker temp;
  151.  
  152.   if (txt_selectset(Text)) {
  153.     txt_newmarker(Text, &temp);
  154.     txt_setdot(Text, txt_selectstart(Text));
  155.     txt_delete(Text, txt_selectend(Text) - txt_selectstart(Text));
  156.     txt_movedottomarker(Text, &temp);
  157.     txt_disposemarker(Text, &temp);
  158.     }
  159.   }
  160.  
  161.  
  162. void texthandler(txt handle)
  163. {
  164.   txt_eventcode event;
  165.   txt_index selStart, selEnd, mIndex;
  166.   char *sel;
  167.   int dummy;
  168.   txt_marker temp;
  169.  
  170.   event = txt_get(handle);
  171.   if (event == (txt_EXTRACODE | 0x1FF)) {  /* window close box causes this, it seems */
  172.     txt_hide(Text);
  173.     return;
  174.     }
  175.  
  176.   if (event & txt_MOUSECODE) {
  177.  
  178.     mIndex = event & 0x00FFFFFF;
  179.  
  180.     switch (event & 0x7F000000) {
  181.     
  182.     case txt_MSELECT:
  183.       txt_setdot(handle, mIndex);
  184.       break;
  185.  
  186.     case txt_MEXTEND:
  187.        txt_setselect(handle, mIndex, mIndex + 1);
  188.        break;
  189.  
  190.      case txt_MEXTOLD | txt_MEXTEND:
  191.        selStart = txt_selectstart(handle);
  192.        selEnd = txt_selectend(handle);
  193.        if (mIndex <= selStart) {
  194.           selStart = mIndex;
  195.           }
  196.        else {
  197.          selEnd = mIndex;
  198.          }
  199.        txt_setselect(handle, selStart, selEnd);
  200.        
  201.        }
  202.  
  203.     return;
  204.     }
  205.     
  206.   switch(event) {
  207.  
  208.   case 0x03:
  209.     copyselection();
  210.     break;
  211.  
  212.   case 0x18:
  213.     deleteselection();
  214.     break;
  215.  
  216.   case 0x05:
  217.     if (txt_selectset(handle)) {
  218.       Execute = TRUE;
  219.       }
  220.     break;
  221.  
  222.   case 0x0D:
  223.     txt_insertchar(handle, (char) 10);
  224.     txt_movedot(handle, 1);
  225.     break;
  226.  
  227.   case 0x08:
  228.     txt_movedot(handle, -1);
  229.     txt_delete(handle, 1);
  230.     break;
  231.  
  232.   case 0x7F:
  233.     txt_delete(handle, 1);
  234.     break;
  235.     
  236.   case 0x18C:
  237.     txt_movedot(handle, -1);
  238.     break;
  239.  
  240.   case 0x18D:
  241.     txt_movedot(handle, 1);
  242.     break;
  243.  
  244.   case 0x18E:
  245.     txt_movevertical(handle, 1, 0);
  246.     break;
  247.  
  248.   case 0x18F:
  249.     txt_movevertical(handle, -1, 0);
  250.     break;
  251.  
  252.   default:
  253.     if ((event > 31) && (event < 256)) {
  254.       txt_insertchar(handle, (char) event);
  255.       txt_movedot(handle, 1);
  256.       }
  257.     break;
  258.     }
  259.   }
  260.  
  261.  
  262. #define info_VERSION 4
  263.  
  264. static void info_about_program(void)
  265. {
  266.   dbox  d;  /* Dialogue box handle */
  267.  
  268.   if (d = dbox_new("ProgInfo"), d != NULL)  {
  269.     dbox_setfield(d, info_VERSION, VERSION);
  270.     dbox_show(d);
  271.     dbox_fillin(d);
  272.     dbox_dispose(&d);
  273.     }
  274.   }
  275.  
  276.  
  277. typedef enum {
  278.   tmenu_SAVE = 1,
  279.   tmenu_COPY,
  280.   tmenu_DELETE,
  281.   tmenu_EXECUTE
  282.   } TMenuSel;
  283.  
  284. void tmenuproc(void *handle, char *hit)
  285. {
  286.   handle = handle;
  287.  
  288.   switch (hit[0]) {
  289.  
  290.   case tmenu_COPY:
  291.     copyselection();
  292.     break;
  293.  
  294.   case tmenu_DELETE:
  295.     deleteselection();
  296.     break;
  297.  
  298.   case tmenu_EXECUTE:
  299.     if (txt_selectset(Text)) {
  300.       Execute = TRUE;
  301.       }
  302.     break;
  303.  
  304.     }
  305.   }
  306.  
  307. typedef enum {
  308.   imenu_INFO = 1,
  309.   imenu_QUIT
  310.   } IMenuSel;
  311.  
  312. /*--- Event handler for the icon menu. ---*/
  313. void imenuproc(void *handle, char *hit)
  314. {
  315.   handle = handle; /* We don't need the handle: this stops compiler warning */
  316.  
  317.   /* Find which menu item was hit and take action as appropriate */
  318.   switch (hit[0])
  319.   {
  320.     case imenu_INFO:
  321.       info_about_program();
  322.       break;
  323.  
  324.     case imenu_QUIT:
  325.       /* Exit from the program. The wimp gets rid of the window and icon */
  326.       exit(0);
  327.     }
  328.   }
  329.  
  330. void iconclick(void)
  331. {
  332.   txt_show(Text);
  333.   }
  334.  
  335. void load_file(void)
  336. {
  337.    char *file;
  338.    int type = xferrecv_checkinsert(&file);
  339.  
  340.    lexinclude(file);
  341.  
  342.    xferrecv_insertfileok();
  343.    }
  344.   
  345.  
  346. BOOL messageproc(wimp_eventstr *ev, void *handle)
  347. {
  348.   switch (ev->e) {
  349.  
  350.   case wimp_ESEND:
  351.   case wimp_ESENDWANTACK:
  352.     switch (ev->data.msg.hdr.action) {
  353.  
  354.     case wimp_MDATASAVE:
  355.       ev->data.msg.hdr.action = wimp_MDATASAVEOK;
  356.       ev->data.msg.hdr.your_ref = ev->data.msg.hdr.my_ref;
  357.       strcpy((char *) &ev->data.msg.data.datasaveok.name, "<Wimp$Scrap>");
  358.       ev->data.msg.hdr.size = sizeof(wimp_msgstr);
  359.       ev->data.msg.data.datasaveok.estsize = -1;
  360.       wimp_sendmessage(wimp_ESEND, &ev->data.msg, ev->data.msg.hdr.task);
  361.       break;
  362.  
  363.     case wimp_MDATALOAD:
  364.     /* case wimp_MDATAOPEN: */
  365.       load_file();
  366.       break;
  367.  
  368.       }
  369.     }
  370.   }
  371.  
  372.  
  373. BOOL init_text(void)
  374. {
  375.  
  376.   wimpt_init("Little SmallTalk");  /* Main wimp initialisation */
  377.   res_init("lst");                 /* Resources */
  378.   resspr_init();                   /* Application sprites */
  379.   template_init();                 /* Templates */
  380.   dbox_init();                     /* Dialogue boxes */
  381.   flex_init();
  382.  
  383.   /* set up iconbar, menus etc */
  384.   baricon("!smalltalk", (int) resspr_area(), (baricon_clickproc) iconclick);
  385.   if (IMenu = menu_new("Smalltalk", ">Info,Quit"), IMenu == NULL)
  386.     return FALSE;
  387.  
  388.   if (!event_attachmenu(win_ICONBAR, IMenu, imenuproc, 0))
  389.     return FALSE; 
  390.  
  391.   /* set up data load handler */
  392.   win_add_unknown_event_processor(messageproc, NULL);
  393.  
  394.   Text = txt_new("Little SmallTalk Workspace");
  395.  
  396.   if (TMenu = menu_new("Smalltalk", ">Save,Copy    (ctl-C),Delete  (ctl-X),Execute (ctl-E)"), 
  397.       TMenu == NULL)
  398.     return FALSE;
  399.   if (!event_attachmenu(txt_syshandle(Text), TMenu, tmenuproc, 0))
  400.     return FALSE; 
  401.  
  402.   txt_show(Text);
  403.   txt_setcharoptions(Text, txt_CARET, txt_CARET);
  404.  
  405.   txt_eventhandler(Text, (txt_event_proc) texthandler, (void *) Text);
  406.  
  407.  
  408.   Execute = FALSE;
  409.  
  410.   return TRUE;
  411.   }
  412.