home *** CD-ROM | disk | FTP | other *** search
- /* ><lst$Dir>.sources.c.Text */
- /* bindings for Little Smalltalk and Risc_OSLib */
-
-
- #include "<OSLib$Dir>.wimp.h"
- #include "<OSLib$Dir>.wimpt.h"
- #include "<OSLib$Dir>.win.h"
- #include "<OSLib$Dir>.event.h"
- #include "<OSLib$Dir>.baricon.h"
- #include "<OSLib$Dir>.res.h"
- #include "<OSLib$Dir>.resspr.h"
- #include "<OSLib$Dir>.menu.h"
- #include "<OSLib$Dir>.template.h"
- #include "<OSLib$Dir>.dbox.h"
- #include "<OSLib$Dir>.werr.h"
- #include "<OSLib$Dir>.flex.h"
- #include "<OSLib$Dir>.txt.h"
-
- #include <stdlib.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <string.h>
-
- #define min(a, b) ((a) < (b) ? a : b)
-
- #define VERSION "1.00 (fms1.0)"
-
- char TOutputBuffer[1024];
- static menu IMenu, TMenu;
-
- txt Text;
- BOOL Execute;
-
- void tprintf(char *s,...)
- {
- va_list va;
-
- va_start(va, s);
-
- vsprintf(TOutputBuffer, s, va);
- s = TOutputBuffer;
- while (*s) {
- if (*s == '\t') *s = ' ';
- s++;
- }
- txt_insertstring(Text, TOutputBuffer);
- txt_movedot(Text, strlen(TOutputBuffer));
-
- va_end(va);
- }
-
- void tputchar(char c)
- {
- c = (c == '\t') ? ' ' : c;
- txt_insertchar(Text, c);
- txt_movedot(Text, 1);
- }
-
- static char DBStr[256];
-
- int extractselection(char **buff, BOOL addcr)
- {
- char *p, *b;
- int n, e, execPtr, execEnd, tLen;
-
- execPtr = txt_selectstart(Text);
- execEnd = txt_selectend(Text);
-
- tLen = (execEnd - execPtr) + 1;
- *buff = malloc((tLen + 1) * sizeof(char));
- if (!buff) {
- werr(0, "Selection too big.");
- return 0;
- }
-
- b = *buff;
- e = 0;
- n = 0;
- while (execPtr < execEnd) {
- if (e == n) {
- txt_arrayseg(Text, execPtr, &p, &n);
- e = 0;
- }
- *b++ = *p++;
- e++;
- execPtr++;
- }
- if (addcr) *b++ = '\n';
- *b = '\0';
-
- return(tLen);
- }
-
-
- char * tgets(char *buff, int n1, FILE *s)
- {
- char *b, *p;
- static char *tBuff, tOffs, tLen;
-
- if (s != stdin) {
- return(fgets(buff, n1, s));
- }
-
- if (!Execute) {
-
- do {
- event_process();
- } while (!Execute);
-
- tLen = extractselection(&tBuff, 1);
- tOffs = 0;
- Execute = tLen;
-
- }
-
- if (Execute) {
- b = tBuff + tOffs;
- p = strchr(b, '\n');
- Execute = (p != tBuff + tLen - 1);
-
- memcpy(buff, b, p - b);
- buff[p - b] = '\n';
- buff[1 + p - b] = '\0';
-
- if (!Execute) {
- free(tBuff);
- }
- else {
- tOffs += 1 + p - b;
- }
- }
-
- return(buff);
- }
-
- void copyselection(void)
- {
- char *sel;
-
- if (txt_selectset(Text)) {
- if (extractselection(&sel, 0)) {
- txt_insertstring(Text, sel);
- free(sel);
- }
- }
- }
-
- void deleteselection(void)
- {
- txt_marker temp;
-
- if (txt_selectset(Text)) {
- txt_newmarker(Text, &temp);
- txt_setdot(Text, txt_selectstart(Text));
- txt_delete(Text, txt_selectend(Text) - txt_selectstart(Text));
- txt_movedottomarker(Text, &temp);
- txt_disposemarker(Text, &temp);
- }
- }
-
-
- void texthandler(txt handle)
- {
- txt_eventcode event;
- txt_index selStart, selEnd, mIndex;
- char *sel;
- int dummy;
- txt_marker temp;
-
- event = txt_get(handle);
- if (event == (txt_EXTRACODE | 0x1FF)) { /* window close box causes this, it seems */
- txt_hide(Text);
- return;
- }
-
- if (event & txt_MOUSECODE) {
-
- mIndex = event & 0x00FFFFFF;
-
- switch (event & 0x7F000000) {
-
- case txt_MSELECT:
- txt_setdot(handle, mIndex);
- break;
-
- case txt_MEXTEND:
- txt_setselect(handle, mIndex, mIndex + 1);
- break;
-
- case txt_MEXTOLD | txt_MEXTEND:
- selStart = txt_selectstart(handle);
- selEnd = txt_selectend(handle);
- if (mIndex <= selStart) {
- selStart = mIndex;
- }
- else {
- selEnd = mIndex;
- }
- txt_setselect(handle, selStart, selEnd);
-
- }
-
- return;
- }
-
- switch(event) {
-
- case 0x03:
- copyselection();
- break;
-
- case 0x18:
- deleteselection();
- break;
-
- case 0x05:
- if (txt_selectset(handle)) {
- Execute = TRUE;
- }
- break;
-
- case 0x0D:
- txt_insertchar(handle, (char) 10);
- txt_movedot(handle, 1);
- break;
-
- case 0x08:
- txt_movedot(handle, -1);
- txt_delete(handle, 1);
- break;
-
- case 0x7F:
- txt_delete(handle, 1);
- break;
-
- case 0x18C:
- txt_movedot(handle, -1);
- break;
-
- case 0x18D:
- txt_movedot(handle, 1);
- break;
-
- case 0x18E:
- txt_movevertical(handle, 1, 0);
- break;
-
- case 0x18F:
- txt_movevertical(handle, -1, 0);
- break;
-
- default:
- if ((event > 31) && (event < 256)) {
- txt_insertchar(handle, (char) event);
- txt_movedot(handle, 1);
- }
- break;
- }
- }
-
-
- #define info_VERSION 4
-
- static void info_about_program(void)
- {
- dbox d; /* Dialogue box handle */
-
- if (d = dbox_new("ProgInfo"), d != NULL) {
- dbox_setfield(d, info_VERSION, VERSION);
- dbox_show(d);
- dbox_fillin(d);
- dbox_dispose(&d);
- }
- }
-
-
- typedef enum {
- tmenu_SAVE = 1,
- tmenu_COPY,
- tmenu_DELETE,
- tmenu_EXECUTE
- } TMenuSel;
-
- void tmenuproc(void *handle, char *hit)
- {
- handle = handle;
-
- switch (hit[0]) {
-
- case tmenu_COPY:
- copyselection();
- break;
-
- case tmenu_DELETE:
- deleteselection();
- break;
-
- case tmenu_EXECUTE:
- if (txt_selectset(Text)) {
- Execute = TRUE;
- }
- break;
-
- }
- }
-
- typedef enum {
- imenu_INFO = 1,
- imenu_QUIT
- } IMenuSel;
-
- /*--- Event handler for the icon menu. ---*/
- void imenuproc(void *handle, char *hit)
- {
- handle = handle; /* We don't need the handle: this stops compiler warning */
-
- /* Find which menu item was hit and take action as appropriate */
- switch (hit[0])
- {
- case imenu_INFO:
- info_about_program();
- break;
-
- case imenu_QUIT:
- /* Exit from the program. The wimp gets rid of the window and icon */
- exit(0);
- }
- }
-
- void iconclick(void)
- {
- txt_show(Text);
- }
-
- void load_file(void)
- {
- char *file;
- int type = xferrecv_checkinsert(&file);
-
- lexinclude(file);
-
- xferrecv_insertfileok();
- }
-
-
- BOOL messageproc(wimp_eventstr *ev, void *handle)
- {
- switch (ev->e) {
-
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- switch (ev->data.msg.hdr.action) {
-
- case wimp_MDATASAVE:
- ev->data.msg.hdr.action = wimp_MDATASAVEOK;
- ev->data.msg.hdr.your_ref = ev->data.msg.hdr.my_ref;
- strcpy((char *) &ev->data.msg.data.datasaveok.name, "<Wimp$Scrap>");
- ev->data.msg.hdr.size = sizeof(wimp_msgstr);
- ev->data.msg.data.datasaveok.estsize = -1;
- wimp_sendmessage(wimp_ESEND, &ev->data.msg, ev->data.msg.hdr.task);
- break;
-
- case wimp_MDATALOAD:
- /* case wimp_MDATAOPEN: */
- load_file();
- break;
-
- }
- }
- }
-
-
- BOOL init_text(void)
- {
-
- wimpt_init("Little SmallTalk"); /* Main wimp initialisation */
- res_init("lst"); /* Resources */
- resspr_init(); /* Application sprites */
- template_init(); /* Templates */
- dbox_init(); /* Dialogue boxes */
- flex_init();
-
- /* set up iconbar, menus etc */
- baricon("!smalltalk", (int) resspr_area(), (baricon_clickproc) iconclick);
- if (IMenu = menu_new("Smalltalk", ">Info,Quit"), IMenu == NULL)
- return FALSE;
-
- if (!event_attachmenu(win_ICONBAR, IMenu, imenuproc, 0))
- return FALSE;
-
- /* set up data load handler */
- win_add_unknown_event_processor(messageproc, NULL);
-
- Text = txt_new("Little SmallTalk Workspace");
-
- if (TMenu = menu_new("Smalltalk", ">Save,Copy (ctl-C),Delete (ctl-X),Execute (ctl-E)"),
- TMenu == NULL)
- return FALSE;
- if (!event_attachmenu(txt_syshandle(Text), TMenu, tmenuproc, 0))
- return FALSE;
-
- txt_show(Text);
- txt_setcharoptions(Text, txt_CARET, txt_CARET);
-
- txt_eventhandler(Text, (txt_event_proc) texthandler, (void *) Text);
-
-
- Execute = FALSE;
-
- return TRUE;
- }
-