txtar 75.9 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Title: c.txtar
 * Purpose: system-dependent parts of text module
 *          arthur/wimp version
 * Author: WRS
 * Status: under development
 * History:
 *  4 august 87 -- split off from initial simple version
 *  16-Jan-88: WRS: conversion into C started.
 *  15-Mar-88: WRS: improvements for inverted 1-bpp fonts
 *  18-Mar-88: WRS: bug in non-TRACE form found.
 *  21-Jun-88: WRS: DATASAVE passed on with DATALOAD as a sh-F2 key.
 *  13-Dec-89: WRS: msgs literal text put back in.
 *  12-Feb-90: IDJ: added user-setting of work area (reset on mode change to max chars for
 *                  screen mode).
 *  21-Feb-90: IDJ: added use of txtopt_get_name for option setting
 *  12-Jun-91: IDJ: made buffer size large enough for reading edit$options
 *  13-Jun-91: IDJ: only write work are edit$option if set in menu
 *  17-Jun-91: IDJ: hourglass on around redraws, because it can be slow!
 *  28-Jun-91: IDJ: experiments with edit$options and wordwrap,overwrite,coltab
 *  01-Jul-91: IDJ: fixed txtar__readoptnum to read numbers properly!!!!!
 *  08-Aug-91: IDJ: 2 sec delay then hourglass around redraws
 *  10-Feb-94: WRS: fix in call to wimpt_checkmode for VIDC20 machines
 */

#define BOOL int
#define TRUE 1
#define FALSE 0
#define NULL 0

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "os.h"
#include "swis.h"
#include "bbc.h"
#include "dbox.h"
#include "werr.h"
#include "akbd.h"
#include "wimp.h"
#include "wimpt.h"
#include "font.h"
#include "trace.h"
#include "win.h"
#include "menu.h"
#include "event.h"
#include "xferrecv.h"
#include "visdelay.h"
#include "msgs.h"
Neil Turton's avatar
Neil Turton committed
66

67 68 69 70 71 72
#include "txt.h"
#include "EditIntern/txtundo.h"
#include "EditIntern/txt1.h"
#include "EditIntern/txt3.h"
#include "EditIntern/txtar.h"
#include "txtscrap.h"
Neil Turton's avatar
Neil Turton committed
73
#ifdef SETOPTIONS
74
#include "txtopt.h"
Neil Turton's avatar
Neil Turton committed
75
#endif
76
#include "VerIntern/messages.h"
Neil Turton's avatar
Neil Turton committed
77

Kevin Bracey's avatar
Kevin Bracey committed
78
#define UNICODE
Robert Sprowson's avatar
Robert Sprowson committed
79 80
#define WIMPSETFONTCOLOUR TRUE
#define INVERTSELECTION TRUE
Kevin Bracey's avatar
Kevin Bracey committed
81 82 83 84 85

/*#ifdef UNICODE
#include "utf8.h"
#endif*/

Neil Turton's avatar
Neil Turton committed
86 87
/* -------- Forward references -------- */

Robert Sprowson's avatar
Robert Sprowson committed
88
static void txtar__redrawtext(txt t);
Neil Turton's avatar
Neil Turton committed
89

Robert Sprowson's avatar
Robert Sprowson committed
90
static void txtar__fontpaintseveral(txt, txt1_call *, wimp_box *, BOOL);
Neil Turton's avatar
Neil Turton committed
91

Robert Sprowson's avatar
Robert Sprowson committed
92
static void txtar__doopenevent(txt t, wimp_openstr *o /*in*/);
Neil Turton's avatar
Neil Turton committed
93

Robert Sprowson's avatar
Robert Sprowson committed
94
static void txtar__dobuttonevent(txt t, wimp_eventstr *e /*in*/);
Neil Turton's avatar
Neil Turton committed
95

Robert Sprowson's avatar
Robert Sprowson committed
96
static void txtar__docharevent(txt t, txt_eventcode e);
Neil Turton's avatar
Neil Turton committed
97

Robert Sprowson's avatar
Robert Sprowson committed
98
static void txtar__dodrag(txt t);
Neil Turton's avatar
Neil Turton committed
99

Robert Sprowson's avatar
Robert Sprowson committed
100
static void txtar__dothumb(txt t, int offset);
Neil Turton's avatar
Neil Turton committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

/* -------- Data for each window. -------- */

#define TITLEBUFMAX 260
#define MAXSYSVARSIZE 256

typedef struct txtar__sysdata {         /* state for an Arthur/Wimp text window. */
  txt t; /* back pointer, needed cos many windows per text */
  struct txtar__sysdata *next; /* list of them all, mainly for caret stuff bodge. */
  wimp_w w;
  wimp_winfo *d;
  BOOL showing;
  char titlebuf[TITLEBUFMAX+1];
  txtar_options o; /* font/colour stuff */
  font fh;
  int baselineoffset;
    /* In os coords, distance from the bottom of the loop of a "g" or
    "y" to where font characters should actually be painted. */
  int italicstripe;
    /* In os coords, the max that a char will overlap to the right
    (in the current font) of where the char claims it ends. e.g. the
    top scroll of an italic "f". */

  int screenmode; /* beeb screen mode */
  int sysfontwidth;  /* in OS-units: mode-dependent */
  int sysfontheight; /* in OS-units: mode-dependent */
  int windowwidth; /* in OS-units, incl scroll bar: mode-dependent */
  int scrollbarwidth; /* windowwidth - (d->info.box.x1 - d->info.box.x0) */

  int imagewidth; /* in os coords */

  int prevmousex;
  int prevmousey;   /* for spotting exact multi-clicks */
  int prevmousetime; /* centisecond count value. */
  BOOL freshredisplay;
} txtar__sysdata;
/* If a setextent happens (I think) after a window is zoomed, the wimp thinks
of this as ending the zoom so that an un-zoom causes no change. Thus, we have
to save the before-zoom box ourselves and synthesise things. See openevent
for this. */

/* the "imagewidth" field is concerned with horizontal scrolling of the
file within the window imagewidth excludes the left hand pixel border. */

/* -------- Static variables -------- */

Robert Sprowson's avatar
Robert Sprowson committed
147
static txtar__sysdata *all = NULL; /* list of all sysdata records. */
Neil Turton's avatar
Neil Turton committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

static BOOL txtar__withinredraw = FALSE;   /* see txtar__redrawwindow */
static BOOL txtar__redrawdone = FALSE;
static wimp_redrawstr txtar__redrawdata;
static BOOL txtar__setsizepending = FALSE;

static BOOL txtar__thumbing = FALSE;
static wimp_mousestr txtar__lastthumbmouse = {0, 100000, 0, 0, 0};
static int txtar__lastthumbdir = 0; /* >= 0 for forwards, <0 for back. */
/* With the continuous-thumbing addition to the wimp, there is a problem
in that continuous redrawing can be caused. We add this kludge: that if
a thumb event arrives, and the current mouse position is the same as the
previous thumb event, we ignore the thumb. */

/* Stuff about disposing of all font handles if the program dies suddenly. */
static BOOL txtar__closefontsregistered = FALSE;

/* -------- Creating and destroying windows. -------- */

#define INITSTARTY 0
static txt1_zits txtar__startx = 100;
static txt1_zits txtar__starty = INITSTARTY;
#define CREATESIZEX (1080-44)
#define CREATESIZEY 448

static int txtar__xppinch(void)
{
  return 180 / wimpt_dx();
}

static int txtar__yppinch(void)
{
  return 180 / wimpt_dy();
}

static int txtar__screenwidth(void)
{
  return (1 + bbc_vduvar(bbc_XWindLimit)) << bbc_vduvar(bbc_XEigFactor);
}

static int txtar__screenheight(void)
{
  return (1 + bbc_vduvar(bbc_YWindLimit)) << bbc_vduvar(bbc_YEigFactor);
}

static int txtar__max(int a, int b) {return(a > b ? a : b);}

static int txtar__min(int a, int b) {return(a < b ? a : b);}

static void txtar__setmode(txtar__sysdata *s) {
  txt t = s->t;
#ifdef BIG_WINDOWS
  int screenwidth = (1 + bbc_vduvar(bbc_XWindLimit)) << bbc_vduvar(bbc_XEigFactor);
#endif

  t->w->highlight_reversable = wimpt_bpp() <= 4;
  s->screenmode = wimpt_mode();
  s->sysfontwidth = wimpt_dx() * bbc_vduvar(bbc_GCharSpaceX);
  s->sysfontheight = wimpt_dy() * bbc_vduvar(bbc_GCharSpaceY);
#ifdef BIG_WINDOWS
  if (!s->o.big_windows)
Robert Sprowson's avatar
Robert Sprowson committed
209
      s->o.big_window_size = txtar__min(screenwidth/s->sysfontwidth - 3 /*scrollbar*/, BIG_WINDOW_SIZE_LIMIT);
Neil Turton's avatar
Neil Turton committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
#endif
}

static void txtar__settextlimits(txt t)
/* The window has changed shape or something, calculate text limits etc. in
preparation for a total redraw. this will in turn set things to do with
scroll bar, etc. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_winfo *d = s->d;
  wimp_redrawstr r;

    r.w = s->w;
    wimp_getwindowoutline(&r);
    s->windowwidth = r.box.x1 - r.box.x0;
    s->scrollbarwidth = s->windowwidth - (d->info.box.x1 - d->info.box.x0);
    s->imagewidth = d->info.box.x1 - d->info.box.x0 - wimpt_dx();
      /* the one pixel is the left hand border within the window. */

    if (! s->o.wraptowindow) {
#ifdef BIG_WINDOWS
       if (!s->o.big_windows)
#endif
          s->imagewidth = txtar__screenwidth() - (s->windowwidth - s->imagewidth);
#ifdef BIG_WINDOWS
       else
          s->imagewidth = s->o.big_window_size * s->sysfontwidth - wimpt_dx();
#endif
      /* if not wrapping at window, set width to screen - scroll bar. */
    };
    t->w->limx = wimpt_dx() + s->imagewidth;
    t->w->limy = d->info.box.y1 - d->info.box.y0;
    t->w->limx = (t->w->limx / wimpt_dx()) * (72000 / txtar__xppinch());
    tracef2("txtar__settextlimits -> t->w->limx=%i t->w->limy=%i.\n",
      t->w->limx, t->w->limy);
}

/* Verification of various things. */
#if TRACE
void txtar__checkwinfo(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_winfo d;
  d.w = s->w;
  wimpt_noerr(wimp_get_wind_info(&d));

    if ((s->d->info.box.x0 != d.info.box.x0) ||
       (s->d->info.box.y0 != d.info.box.y0) ||
       (s->d->info.box.x1 != d.info.box.x1) ||
       (s->d->info.box.y1 != d.info.box.y1)
    ) {
      tracef("**  screenbox:(%i,%i,%i,%i).\n",
        s->d->info.box.x0, s->d->info.box.y0, s->d->info.box.x1, s->d->info.box.y1);
      tracef("**          v:(%i,%i,%i,%i).\n",
        d.info.box.x0, d.info.box.y0,
        d.info.box.x1, d.info.box.y1);
    };
    if ((s->d->info.ex.x0 != d.info.ex.x0) ||
       (s->d->info.ex.y0 != d.info.ex.y0) ||
       (s->d->info.ex.x1 != d.info.ex.x1) ||
       (s->d->info.ex.y1 != d.info.ex.y1)
    ) {
      tracef("**  wbox:(%i,%i,%i,%i).\n",
        s->d->info.ex.x0, s->d->info.ex.y0, s->d->info.ex.x1, s->d->info.ex.y1);
      tracef("**     v:(%i,%i,%i,%i).\n",
        d.info.ex.x0, d.info.ex.y0,
        d.info.ex.x1, d.info.ex.y1);
    };
}
#endif

static void txtar__verify(txtar__sysdata *s) {
/* Check that the we agree with the wimp on the size etc of the window. If we do
not, force a total redraw. In any case, ensure that s->d is totally up to date. */

  wimp_winfo wi;
  BOOL sizechange;

  wi.w = s->w;

  /* Now read it back and, if the size is wrong, call settextlimits. */
  wimpt_noerr(wimp_get_wind_info(&wi));
  sizechange = (wi.info.box.x1 - wi.info.box.x0 !=
                s->d->info.box.x1 - s->d->info.box.x0) ||
               (wi.info.box.y1 - wi.info.box.y0 !=
                s->d->info.box.y1 - s->d->info.box.y0);
  *(s->d) = wi; /* Keep our copy up to date. */
  if (sizechange) {
    tracef0("size change in openandverify forces total redraw.\n");
    txtar__settextlimits(s->t);
    txtar__redrawtext(s->t);
  };
}

static void txtar__openandverify(txtar__sysdata *s)
/* Open the window described, and keep the windowdesc up to date. */
{
  wimp_openstr d;

  s->d->w = s->w;

  d.w = s->w;
  d.box = s->d->info.box;
  d.x = s->d->info.scx;
  d.y = s->d->info.scy;
  d.behind = (wimp_w) -1;

  wimpt_noerr(wimp_open_wind(&d));
  txtar__verify(s);
}

static void txtar__dohide(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  tracef0("textarthur.dohide\n");
  if (! s->showing) {
    tracef0("already hidden.\n");
  } else {
    s->showing = FALSE;
    win_activedec();
    wimpt_noerr(wimp_close_wind(s->w));
  };
}

static void txtar__doshow(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;

  tracef0("textarthur.doshow\n");
  if (s->showing) {
    tracef0("already showing\n");
  } else {
    s->showing = TRUE;
    win_activeinc();
    txtar__openandverify(s);
    txtar__settextlimits(t);
  };
  txtar__redrawtext(s->t);
  tracef0("doshow done.\n");
}

static void txtar__dosettitle(txt t, char *title)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_redrawstr r;

  tracef0("dosettitle.\n");
  strncpy(&s->titlebuf[0], title, TITLEBUFMAX);
  s->titlebuf[TITLEBUFMAX] = 0;
  r.w = (wimp_w) -1; /* redraw in absolute screen coords */
  r.box = s->d->info.box;
  r.box.y1 += 36;
  r.box.y0 = r.box.y1 - 36;
  wimpt_noerr(wimp_force_redraw(&r));
}

static void txtar__disposewindow(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_caretstr caret;
  BOOL havecaret;

  tracef0("txtar_disposewindow.\n");
  wimpt_noerr(wimp_get_caret_pos(&caret));
  havecaret = caret.w == s->w;
  win_register_event_handler(s->w, (win_event_handler) NULL, 0);
  tracef0("event handler registered.\n");
  wimpt_noerr(wimp_delete_wind(s->w));
  if (s->showing) win_activedec();
  if (! s->o.fixfont) wimpt_noerr(font_lose(s->fh));
  if (havecaret) win_give_away_caret();
  tracef0("window deleted.\n");

  {
    /* Delete s from the list of all such things. */
    txtar__sysdata **p = &all;
    while (1) {
      if (*p == 0) {
        werr(TRUE, msgs_lookup(MSGS_txt50));
      };
      if (*p == s) {
        *p = (*p)->next;  /* cut it out of the chain */
        break;
      };
      p = &((*p)->next);
    };
  };

  free(s->d);
  free(s);
  tracef0("disposewindow done.\n");
}

/* -------- Arthur-specific Public Operations. -------- */

static int txtar__dosyshandle(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  return (int) s->w; /* suitable for hanging menus on */
}

void txtar_getoptions(txt t, txtar_options *o /*out*/)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  *o = s->o;
}

static void txtar__trynofont(txt t)
/* >>>> messy split between this and setoptions */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  t->w->italic = FALSE;
  s->o.fixfont = TRUE;
  t->w->linesep = s->sysfontheight + (s->o.leading < 0 ? 0 : s->o.leading) * wimpt_dy();
  /* 20-Dec-88 WRS: negative line spacing ignored in system font. */
}

static void txtar__closefonts(void) {
  txtar__sysdata *p = all;
  while (p != 0) {
    if (! p->o.fixfont) {
      wimpt_noerr(font_lose(p->fh));
    };
    p = p->next;
  };
}

static BOOL txtar__tryfont(txt t, char *fontname, int width, int height)
{
  font f;
  font_info info;
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
#if TRACE
  int version;
  int cacheused;
  int cachesize;
  font_def d;
#endif
  os_error *e;

  if (! txtar__closefontsregistered) {
    atexit(txtar__closefonts);
    txtar__closefontsregistered = TRUE;
  };

#if TRACE
  font_cacheaddress(&version, &cacheused, &cachesize);
  tracef3(" font: %i %i %i.\n",
    version, cacheused, cachesize);
#endif
  e = font_find(fontname, width*16, height*16, 0, 0, &f);
  tracef1(" fonth=%i.\n", f);
  if (f == (font) NULL || e != NULL) {
    tracef0("font not found.\n");
    werr(FALSE, e->errmess);
    return FALSE;
  };

  s->o.fixfont = FALSE;

  s->fh = f;
#if TRACE
  font_readdef(f, &d);
  tracef4("font def: %i %i %i %i",
    d.xsize, d.ysize, d.xres, d.yres);
  tracef2(" %i %i.\n", d.usage, d.age);
#endif

  font_readinfo(f, &info);

#if TRACE
  tracef4(" minx=%i miny=%i maxx=%i maxy=%i.\n",
    info.minx, info.miny, info.maxx, info.maxy);
#endif

#if FALSE
  t->w->italic = info.minx < 0; /* >>>> bad fonts around */
#else
  t->w->italic = TRUE; /* 20-Dec-88 WRS: all fonts now italic. */
#endif

#if TRACE
    if (t->w->italic) {
      tracef("**this font looks italic.\n");
    };
#endif

  t->w->linesep = info.maxy - info.miny;

  if (t->w->linesep % wimpt_dy() != 0) {
#if FALSE
    werr(FALSE, "bad line separation in font");
#endif
    t->w->linesep = t->w->linesep - t->w->linesep % wimpt_dy();
    t->w->italic = TRUE;
  };
  s->baselineoffset = /* wimpt_dy() */ - info.miny;
  s->italicstripe = -info.minx;
  /* s->italicstripe = info.maxx / 3; */

#if FALSE
  /* some fonts seem to give this in pixels? */
  if (t->w->linesep < 2 * height) {
    /* guard against bad fonts */
    werr(FALSE, "bad height in font");
    t->w->italic = TRUE;
    t->w->linesep = 3 * height;
    s->baselineoffset = t->w->linesep / 3 - wimpt_dy(); /* in os coord units */
      /* >>>> why subtract the wimpt_dy()? */
      /*      seems to get it wrong if you don't! */
    s->italicstripe = 0;
  };
#endif

  t->w->linesep += s->o.leading * wimpt_dy();
  if (t->w->linesep < wimpt_dy()) t->w->linesep = wimpt_dy(); /* silly case! */
#if FALSE
  if (s->o.leading > 0) {
    s->baselineoffset += wimpt_dy() * (s->o.leading / 2);
  };
#else
  /* 19-Sep-89 - improvement of caret offset when leading is negative. */
  s->baselineoffset += wimpt_dy() * (s->o.leading / 2);
#endif

  t->w->linesep += 2 * wimpt_dy();
  s->baselineoffset += wimpt_dy();
  /* >>>> This is a kludge! - it appears to be necessary because either the
  fonts, or the fontmgr, are lying about the true possible height of chars. */

  return TRUE;
}

static void txtar__dosetoptions(txt t, txtar_options *o /*in*/)
/* Set s->o, and make the rest of the object follow it. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  txtar_options previous = s->o;
  font previousfont = s->fh;

  s->o = *o;
  if (s->o.fontwidth <= 0) s->o.fontwidth = 10;
  if (s->o.fontheight <= 0) s->o.fontheight = 10;
  if (s->o.fixfont) {
    txtar__trynofont(t);
  } else {
    if (! txtar__tryfont(t, &s->o.fontname[0],
                         s->o.fontwidth, s->o.fontheight)) {
      s->o = previous; /* restore existing state. */
      return; /* no need to redraw */
    };
  };
  if (! previous.fixfont) {
    wimpt_noerr(font_lose(previousfont));
  };
  txtar__settextlimits(t);
  txtar__setsizepending = TRUE;
}

void txtar_setoptions(txt t, txtar_options *o /*in*/)
{
  txtar__dosetoptions(t, o);
  {
    char a[MAXSYSVARSIZE];
    txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
#ifdef SETOPTIONS
Robert Sprowson's avatar
Robert Sprowson committed
576 577
    snprintf(a, MAXSYSVARSIZE-1, "Set %s$Options f%i b%i l%i m%i h%i w%i%s",
      txtopt_get_name(),
Neil Turton's avatar
Neil Turton committed
578
#else
Robert Sprowson's avatar
Robert Sprowson committed
579
    sprintf(a, "Set Edit$Options f%i b%i l%i m%i h%i w%i%s",
Robert Sprowson's avatar
Robert Sprowson committed
580
#endif
Neil Turton's avatar
Neil Turton committed
581 582 583 584 585 586 587 588
      s->o.forecolour,
      s->o.backcolour,
      s->o.leading,
      s->o.margin,
      s->o.fontheight,
      s->o.fontwidth,
      (s->o.wraptowindow ? " r" : ""));
#ifdef SET_MISC_OPTIONS
Robert Sprowson's avatar
Robert Sprowson committed
589 590 591
    if (s->o.overwrite) strcat(a, " O");
    if (!s->o.wordtab) strcat(a, " T");
    if (s->o.wordwrap) strcat(a, " D");
Neil Turton's avatar
Neil Turton committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    if (s->o.undosize != 5000) sprintf(a+strlen(a), " u%i", s->o.undosize);
#endif
#ifdef BIG_WINDOWS
    if (s->o.big_windows)
    {
       sprintf(a+strlen(a), " a%i", s->o.big_window_size);
    }
#endif
    if (! s->o.fixfont) {
      strcat(a, " n");
      strcat(a, s->o.fontname);
    };

    wimpt_complain(os_cli(a));
  };
  txtar__redrawtext(t); /* in case of colour/font change */

}

/* -------- Painting. -------- */

Robert Sprowson's avatar
Robert Sprowson committed
613 614 615 616 617 618 619
#define PAINTBUFSIZE 484
#ifdef BIG_WINDOWS
#if (BIG_WINDOW_SIZE_LIMIT+4) > PAINTBUFSIZE
#error "Can't paint that window reliably"
#endif
#endif

Neil Turton's avatar
Neil Turton committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
static void txtar__rawsetsize(txt t)
/* We can count on t->s->d being accurate about the current state of
things. We must reset the extent of the work area so that arthur redraws
the scroll bars for us. */
/* >>>> Should optimise the case of no change, to not call wimp */
/* in cases where size < screen height, I provide a minimum size
so that drag-size-change works. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_redrawstr e;
  int vissize;
  int size;
  int offset;

    tracef2("textarthur.rawsetsize %i %i\n",
      t->w->ioffset, t->w->isize);
    if (txtar__withinredraw) {
      /* the wimp doesn't like you doing this in a redraw-> */
      /* redraw always ends with one of these with withinredraw==FALSE. */
      tracef0("  (setsize delayed).\n");
      txtar__setsizepending = TRUE;
      return;
    };
    if (t->w->isize == 0) {
      tracef0("  setsize ignored, size=0.\n");
      return;
    };
    if (t->w->isize + t->w->ioffset > 100) {
      t->w->ioffset = 100 - t->w->isize;
    };
#if TRACE
    txtar__checkwinfo(t);
#endif
    vissize = s->d->info.box.y1 - s->d->info.box.y0;
    size = (vissize * 100) / t->w->isize; /* w->isize is a percentage */
    /* size / 100 is the amount of space taken up to represent
    one percent of the file. */
    offset = (size * t->w->ioffset) / 100;
    if (size - offset < vissize) {
      tracef3("bad setsize! size=%i offset=%i vissize=%i.",
        size, offset, vissize);
    };
/* >>>> partial fix, see improved one below->
    if (size < 1024) size = 1024;
*/
    if (offset-size > -txtar__screenheight()) {
      /* This is a danger in two ways. If size<1024 then the wimp won't
      allow a drag-size-change very big, and limits the size of zooms.
      If size>1024 but e.y0>-1024 then a large size increase can
      only generate an open event that moves the origin of the display
      area. This makes the world go generally screwy, with the inabilty
      to do open+setextent in one operation as the main problem. */
      size = txtar__screenheight() + offset;
    };
    tracef1("  size=%i.\n", size);
    e.w = s->w;
    e.box.x0 = 0;
    e.box.y0 = offset - size;
#ifdef BIG_WINDOWS
    if (s->o.big_windows && !s->o.wraptowindow)
      e.box.x1 = s->o.big_window_size*s->sysfontwidth + wimpt_dx();
    else
#endif
    e.box.x1 = txtar__screenwidth() - s->scrollbarwidth;
      /* imagewidth no good, because you can't then drag it wider. */
    e.box.y1 = offset;
    tracef2("  setting miny=%i maxy=%i.\n", e.box.y0, e.box.y1);
    s->d->info.ex = e.box;
    wimpt_noerr(wimp_set_extent(&e));
#ifdef BIG_WINDOWS
    /* IDJ 12-Feb-90: this seems necessary */
    if (s->o.big_windows)
    { wimp_wstate ws;
      wimp_get_wind_state(s->w, &ws);
      wimp_open_wind(&ws.o);
    }
#endif
}
/* >>>> If it's necessary to squeeze more performance out of the scrolling
display of text, it would be possible to delay calls to rawsetsize until
until a null event came along. this would reduce redrawing of the scroll bar.
An out counter would probably be necessary too, to force update every few
lines of output. avoid this for now. */

/* >>>> It would also be nice if, during a scroll, the size of the "blob"
did not change. Its current wobbling is rather disconcerting to the user,
and could be quietly minimised. e.g. only wobble if it changes in size by
more than a pixel. This is already fixed, to some extent, in txt1, but
the wimp can wobble the size by up to one pixel without me being able to
affect it. */

static char txtar__hexch(int c)
{
  c = c % 16;
  if (c < 10) {
    return '0' + c;
  } else {
    return 'a' + c - 10;
  };
}

static int txtar__charwidth(char c) {
/* Measure the width of a single character. */
/* The font must already be set up using font_setfont. */
  font_string str;
  char a[2];
  a[0] = c;
  a[1] = 0;
  str.s = &a[0];
  str.x = 10000000;
  str.y = 10000000;
  str.split = -1;
  str.term = 1;
  wimpt_noerr(font_strwidth(&str));
  return(str.x);
}

Robert Sprowson's avatar
Robert Sprowson committed
737
#if FALSE
Kevin Bracey's avatar
Kevin Bracey committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
static int txtar__UCScharwidth(UCS4 c) {
/* Measure the width of a UCS character. */
/* The font must already be set up using font_setfont. */
  os_regset regs;
  UCS4 a[2];

  a[0] = c;
  a[1] = 0;

  regs.r[1] = (int) a;
  regs.r[2] = font_32BIT | font_LENGTH;
  regs.r[3] = 10000000;
  regs.r[4] = 10000000;
  regs.r[7] = 4;
  wimpt_noerr(os_swix(Font_ScanString, &regs));
  return(regs.r[3]);
}
#endif

Neil Turton's avatar
Neil Turton committed
757 758 759 760 761
static void txtar__expandchars(
  txtar__sysdata *s,
  char *a,
  int n,                          /* number of chars */
  char *cbuf /*out*/,             /* where to put expanded chars */
Robert Sprowson's avatar
Robert Sprowson committed
762
  short *obuf /*out*/,            /* matching offset into original string */
Neil Turton's avatar
Neil Turton committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776
  int bufsize,                    /* cbuf and obuf sizes */
  int *nchars /*inout*/)          /* no of output chars */
/* Expands the characters of the string out into the buffer, with funny
characters being replaced by [xx]. Expands out the string (zero-terminating
this) and builds a matching table of offsets so that, if measure stops
half-way through, you can tell instantly how far you got in the original
string. Buffer sizes within 5..255. */
/* If nchars!=0 on entry it implies that there are some characters already in
place. Thus, this can be used to combine strings from several sources. This
is what happens in fontpaintseveral, to improve drawing of italic fonts. */
{
  int srci = 0;
  int dsti = *nchars;
  char c;
Kevin Bracey's avatar
Kevin Bracey committed
777 778 779 780 781
#ifdef UNICODE
  int alphabet=127, dummy;

  os_byte(71, &alphabet, &dummy); /* Read current alphabet number */
#endif
Neil Turton's avatar
Neil Turton committed
782 783 784 785 786

  tracef2("expandchars ad=%i n=%i.\n", (int) a, n);
  while (1) {
    if (dsti >= bufsize - 4) break;
    if (srci >= n) break;
Kevin Bracey's avatar
Kevin Bracey committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
#ifdef UNICODEx
    if (alphabet == 111) {
      int l = UTF8_to_UCS4(a+srci, &c);
      if (c < 32 || c == 127 || (c>=256 && s->o.fixfont)
          || (c >= 127 && (! s->o.fixfont) && txtar__UCScharwidth(c) == 0)
      ) {
        /* funny character */
        cbuf[dsti] = '[';
        obuf[dsti++] = srci;
        if (c & 0xF0000000) {
          cbuf[dsti] = txtar__hexch(c >> 28);
          obuf[dsti++] = srci;
        }
        if (c & 0xFF000000) {
          cbuf[dsti] = txtar__hexch(c >> 24);
          obuf[dsti++] = srci;
        }
        if (c & 0xFFF00000) {
          cbuf[dsti] = txtar__hexch(c >> 20);
          obuf[dsti++] = srci;
        }
        if (c & 0xFFFF0000) {
          cbuf[dsti] = txtar__hexch(c >> 16);
          obuf[dsti++] = srci;
        }
        if (c & 0xFFFFF000) {
          cbuf[dsti] = txtar__hexch(c >> 12);
          obuf[dsti++] = srci;
        }
        if (c & 0xFFFFFF00) {
          cbuf[dsti] = txtar__hexch(c >> 8);
          obuf[dsti++] = srci;
        }
        if (c & 0xFFFFFFF0) {
          cbuf[dsti] = txtar__hexch(c >> 4);
          obuf[dsti++] = srci;
        }
        cbuf[dsti] = txtar__hexch(c);
        obuf[dsti++] = srci;

        cbuf[dsti] = ']';
        obuf[dsti++] = srci;
      }
      else {
        memcpy(cbuf+dsti, a+srci, l);
        memset(obuf+dsti, srci, l);
        dsti+=l;
      }
      srci+=l;
    }
    else {
#endif
Neil Turton's avatar
Neil Turton committed
839 840
    c = a[srci];
    if (c < 32 || c == 127
Kevin Bracey's avatar
Kevin Bracey committed
841
        || (c >= 127 /* && c < 128+32 */ && (! s->o.fixfont) && alphabet != 111 && txtar__charwidth(c) == 0)
Neil Turton's avatar
Neil Turton committed
842 843 844
    ) {
      /* funny character */
      cbuf[dsti] = '[';
845
      cbuf[dsti+1] = txtar__hexch(c >> 4);
Neil Turton's avatar
Neil Turton committed
846 847 848 849 850 851 852 853 854 855 856 857 858
      cbuf[dsti+2] = txtar__hexch(c);
      cbuf[dsti+3] = ']';
      obuf[dsti] = srci;
      obuf[dsti+1] = srci;
      obuf[dsti+2] = srci;
      obuf[dsti+3] = srci;
      dsti += 4;
    } else {
      cbuf[dsti] = c;
      obuf[dsti] = srci;
      dsti++;
    };
    srci++;
Kevin Bracey's avatar
Kevin Bracey committed
859 860 861
#ifdef UNICODEx
    }
#endif
Neil Turton's avatar
Neil Turton committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
  };
  *nchars = dsti;
  cbuf[dsti] = 0;
  obuf[dsti] = srci;
  tracef2("exit nchars=%i obuf[nchars]=%i.\n",
    *nchars, obuf[*nchars]);
}

#if TRACE
void txtar__safewrch(char c)
{
  if ((c >= 32) && (c < 127)) {
    tracef1("%c", c);
  } else {
    tracef1("(%i)", c);
  };
}
#endif

static void txtar__rawmeasure(
  txt t,
  char **ad /*inout*/,
  int *n /*inout*/,
  int *spacewidth /*inout*/) /* initially max allowed */
/* There is an annoying mismatch here between the arthur facilities and
the facilities that we require. The trouble is that control characters
in the text object must be expanded out in order to build [xx] sequences. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
Robert Sprowson's avatar
Robert Sprowson committed
891 892
  char cbuf[PAINTBUFSIZE];
  short obuf[PAINTBUFSIZE];
Neil Turton's avatar
Neil Turton committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  int nchars;
  int ncharsdone;
  int x;
  int nhalfdone;
#if TRACE
  int i;
#endif
  font_string str;

#if TRACE
  tracef3("fontmeasure %i %i %i (", (int) *ad, *n, *spacewidth);
  for (i=0; i<*n; i++) txtar__safewrch((*ad)[i]);
  tracef0(")\n");
#endif

  if (! s->o.fixfont) {
    /* Set current font. */
    /* >>>> Any way of optimising this? not particularly simple... */
    wimpt_noerr(font_setfont(s->fh));
  };

  while (TRUE) {
    ncharsdone = 0;
Robert Sprowson's avatar
Robert Sprowson committed
916
    txtar__expandchars(s, *ad, *n, &cbuf[0], &obuf[0], PAINTBUFSIZE, &ncharsdone);
Neil Turton's avatar
Neil Turton committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    nchars = ncharsdone;
    x = *spacewidth;
    if (s->o.fixfont) {
      int fixfontw = 400 * s->sysfontwidth;
        /* width in millipoints: 400 == 72000/180 */
      if (nchars * fixfontw > x) {
        /* we are space limited */
        nchars = x / fixfontw;
        /* align to whole text char */
        while (nchars > 0 && obuf[nchars] == obuf[nchars-1]) nchars--;
      };
      x = nchars * fixfontw;
    } else {
      str.s = &cbuf[0];
      str.x = x;
      str.y = 10000000; /* no limits imposed on y space consumed */
      str.split = -1;
      str.term = nchars;
      wimpt_noerr(font_strwidth(&str));
      nchars = str.term;
      x = str.x;
    };
    *spacewidth -= x;
    if (nchars != ncharsdone  /* not enough space even for that much */
    || obuf[ncharsdone] == *n  /* finished all chars */
    ) {                       /* we'll be exiting soon */
      *ad += obuf[nchars];
      *n -= obuf[nchars];
      if (*n != 0) {
        nhalfdone = 0;
        while (nchars > 0
        && obuf[nchars] == obuf[nchars-1]
        ) {
          /* half-way through expanded char: see explanation below. */
          tracef0("half-way through expanded char.\n");
          nchars--;
          nhalfdone++;
        };
        if (nhalfdone != 0) {
          str.x = 10000000;
          str.y = 10000000;
          str.split = -1;
          str.term = nhalfdone;
          wimpt_noerr(font_strwidth(&str));
          tracef1("added %i back again\n", x);
          x = str.x;
          nhalfdone = str.term;
          *spacewidth += str.x;
        };
      };
      break;
    } else {                      /* we have to do another buffer-full */
      *ad += obuf[nchars];
      *n -= obuf[nchars];
      /* and loop */
    };
  };
  tracef3("        returning %i %i %i\n", (int) *ad, *n, *spacewidth);
}
/* >>>> If one character expands to many then the font stuff is not right at
the moment, if there's not enough room for the expanded character then our x
return currently deducts what display characters of the expanded character
did fit. */

/* If the thing is going to be space-limited and in fact is met very early,
we wastefully expand out bufmax characters. Could try to estimate things,
based on the width of a space or something. Not particularly important. */

/* >>>> Change to references to BBC. */

static void txtar__grmoveto(int x, int y)
{
  bbc_plot(4, x, y);
}

static void txtar__setgrcolour(int c)
{
  wimp_setcolour(c);
  /* colour in 0..3, gcol action in 4..6, fg/bg in 7 */
}

static void txtar__paintseveral(
  txt t,
  txt1_call *calls,
  wimp_box *grclip /*in*/,
  BOOL alreadycorrect)
/* As in measure, expandchars is used to handle control chars etc. in the
text. The one procedure paints several calls at once so that adjacent calls
that form part of the same line can be combined into a single call. This is
done here because it's where the characters have to be copied anyway. */
/* >>>> There is quite a lot duplicated between here and fontpaintseveral,
perhaps some work could be done to reduce this. */

{
 txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
 txt1_call thiscall;
#if TRACE
 int i;
#endif
 int scx;
 int scy;
 char cbuf[PAINTBUFSIZE];
Robert Sprowson's avatar
Robert Sprowson committed
1019
 short obuf[PAINTBUFSIZE];
Neil Turton's avatar
Neil Turton committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
 int nchars;

  if (! s->o.fixfont) {
    txtar__fontpaintseveral(t, calls, grclip, alreadycorrect);
    return;
  };

  alreadycorrect &= (wimpt_bpp() <= 4);
  /* XORing doesn't work in 8-bpp modes. */

  while (calls != NULL) {

#if TRACE
    tracef3("paint at (%i,%i) %i:'", calls->x, calls->y, calls->n);
    for (i=0; i<calls->n; i++) txtar__safewrch(calls->ad[i]);
    tracef3("', %i, %i, %i\n",
      calls->width, calls->highlight, calls->callendopt);
    if (alreadycorrect) tracef("inverting display.\n");
#endif

    thiscall = *calls;
    calls = calls->next;

    nchars = 0;
    txtar__expandchars(s, thiscall.ad, thiscall.n,
      &cbuf[0], &obuf[0], PAINTBUFSIZE, &nchars);

    while ((calls != NULL)
    && (thiscall.y == calls->y)
    && (thiscall.x + thiscall.width == calls->x)
    && (thiscall.highlight == calls->highlight)
    ) {
      tracef0("merge with following paint call.\n");
      thiscall.width += calls->width;
      thiscall.callendopt = calls->callendopt;

      txtar__expandchars(
        s, calls->ad, calls->n, &cbuf[0], &obuf[0], PAINTBUFSIZE, &nchars);
          /* add to existing ones */
      calls = calls->next;
    };

    scx = s->d->info.box.x0 - s->d->info.scx +
      wimpt_dx() * (s->o.margin + (thiscall.x * txtar__xppinch()) / 72000);
    scy = (s->d->info.box.y1 /* >>>> - wimpt_dy() */ ) - thiscall.y;

    if (
      scy + t->w->linesep < grclip->y0 ||
      scy > grclip->y1
    ) {
      tracef0("clipped.\n");
      /* go round the loop */
    } else {

      /* mark out the blanking box. */
      /* we blank out the left hand pixel column too if
      in the left hand char position, as not doing this can leave blobs. */
      if (scx == s->o.margin * wimpt_dx() + s->d->info.box.x0) {/* left hand edge */
        txtar__grmoveto(scx - s->o.margin * wimpt_dx(), scy);
      } else {
        txtar__grmoveto(scx, scy);
      };

      if (alreadycorrect) {
        tracef0("invert text line.\n");
        if (thiscall.width > 0) {
          txtar__setgrcolour(
            (s->o.forecolour ^ s->o.backcolour) +        /* GCOL colour */
            + (3<<4)); /* XOR action. */
          bbc_plot(101,
            scx + wimpt_dx() *
              ((thiscall.width * txtar__xppinch()) / 72000 - 1),
            scy + t->w->linesep - 1);
        } else {
          tracef0("null width, already correct: do nothing.\n");
        };
      } else {
        /* fill in the background */
        if (thiscall.highlight) {
          txtar__setgrcolour(128 + s->o.forecolour); /* hilight background */
        } else {
          txtar__setgrcolour(128 + s->o.backcolour); /* normal background */
        };
        /* abs fill rectangle with background */
        if ((thiscall.callendopt != txt1_CECONTINUE) && (! thiscall.highlight)) {
          bbc_plot(103, s->d->info.box.x1,
                        scy + t->w->linesep - 1);
        } else {
          bbc_plot(103, scx + wimpt_dx() *
                             ((thiscall.width * txtar__xppinch()) / 72000),
                        scy + t->w->linesep - 1);
        };

        /* and write the characters */
        if (thiscall.highlight) {
          txtar__setgrcolour(s->o.backcolour); /* hilight background */
        } else {
          txtar__setgrcolour(s->o.forecolour); /* normal background */
        };
        txtar__grmoveto(scx, scy + t->w->linesep - 1);
        /* writing chars to the graphics cursor causes the top pixel of
        the letter to be visible at the cursor pixel level. */
        bbc_vdu(5);
        bbc_stringprint(&cbuf[0]);
        if ((thiscall.callendopt != txt1_CECONTINUE) && thiscall.highlight) {
          /* must blank out a second box in paper colour */
          txtar__setgrcolour(128 + s->o.backcolour); /* normal background */
          txtar__grmoveto(scx + s->sysfontwidth * nchars, scy);
          bbc_plot(103, s->d->info.box.x1,
                        scy + t->w->linesep - 1);
        };
      };

      /* >>>> the use of system calls in setting colours etc could probably
      be tightened up a little. never mind about this for now. */

    };
  };
  tracef0("paintseveral done.\n");
}

#if WIMPSETFONTCOLOUR
#else

Robert Sprowson's avatar
Robert Sprowson committed
1144
static void txtar__fonthilight(char *cbuf, int *n /*inout*/)
Neil Turton's avatar
Neil Turton committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
/* The sequence to turn on highlighting. */
{
#if INVERTSELECTION
        int bpp = wimpt_bpp();
        int cols = bpp == 8 ? 8 : (bpp == 4 ? 8 : 1 << bpp);
        cbuf[(*n)++] = 18;
        cbuf[(*n)++] = cols-1;
        cbuf[(*n)++] = cols-2;
        cbuf[(*n)++] = (2-cols) & 0xff;
#else
    /* underline */
    cbuf[(*n)++] = 25;
    cbuf[(*n)++] = 234;
      /* -32, offset from base line in 1/256 of height */
    cbuf[(*n)++] = 16; /* width of underline in same units */
#endif
}

Robert Sprowson's avatar
Robert Sprowson committed
1163
static void txtar__fontnohilight(char *cbuf, int *n)
Neil Turton's avatar
Neil Turton committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/* The sequence to turn off highlighting. */
{
#if INVERTSELECTION
        int bpp = wimpt_bpp();
        int cols = bpp == 8 ? 8 : (bpp == 4 ? 8 : 1 << bpp);
        cbuf[(*n)++] = 18;
        cbuf[(*n)++] = 0;
        cbuf[(*n)++] = 1;
        cbuf[(*n)++] = cols-2;
#else
    /* no underline */
    cbuf[(*n)++] = 25;
    cbuf[(*n)++] = 234;
    cbuf[(*n)++] = 0; /* no underline */
#endif
}
#endif

#ifndef UROM
static int txtar__roundtoxpix(int s) {
  return (s / wimpt_dx()) * wimpt_dx();
}
#endif

Robert Sprowson's avatar
Robert Sprowson committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
static int txtar__font_paint_error = FALSE;
/* >>>> There's a bug in 1.2 such that chr(18) sequences in the middle of a
string do not work. invert really is better than underline. must be
severely fudged, and will flicker, in 1.2 os. also, will not work for
disjoint selection portions. */
#define CHAR18BUG TRUE
/* Could make these variable if I wanted. */
/* >>>> Char 18 turns out not to help. Changes at the leading edge of
the character, not at the actual start of the character. */

static void txtar__fontpaintseveral(
Neil Turton's avatar
Neil Turton committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  txt t,
  txt1_call *calls,
  wimp_box *grclip /*in*/,
  BOOL alreadycorrect)
/* As in measure, expandchars is used to handle control chars etc. in the
text. The one procedure paints several calls at once so that adjacent calls
that form part of the same line can be combined into a single call. This is
done here because it's where the characters have to be copied anyway. It
improves the display of italic characters. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  txt1_call thiscall;
#if TRACE
  int i;
#endif
  int scx;
  int scy;
  int mpx;       /* x,y in millipoints */
  int mpy;
  char cbuf[PAINTBUFSIZE];
Robert Sprowson's avatar
Robert Sprowson committed
1219
  short obuf[PAINTBUFSIZE];
Neil Turton's avatar
Neil Turton committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
  int nchars;

  alreadycorrect &= (wimpt_bpp() <= 4);
  /* XORing doesn't work in 8-bpp modes. */

  if (s->o.forecolour <= 7
  && s->o.backcolour <= 7
  && s->o.forecolour + s->o.backcolour != 7)
  {
    /* anti-aliased grey scales that don't quite form an orderly
    sequence: XOR'ing won't work. */
    alreadycorrect = FALSE;
  };

  while (calls != NULL) {

  if (txtar__font_paint_error) return;

#if TRACE
    tracef3("fontpaint at (%i,%i) %i:'", calls->x,
      calls->y, calls->n);
    for (i=0; i<calls->n; i++) txtar__safewrch(calls->ad[i]);
    tracef3("', %i, %i, %i\n",
      calls->width, calls->highlight, calls->callendopt);
    if (alreadycorrect) tracef("inverting display.\n");
#endif

    thiscall = *calls;
    calls = calls->next;

    nchars = 0;
#if WIMPSETFONTCOLOUR
#else
    if ((thiscall.highlight != (s->o.forecolour < 4))) {
      txtar__fonthilight(&cbuf[0], &nchars);
    };
#endif
    txtar__expandchars(s, thiscall.ad, thiscall.n,
      &cbuf[0], &obuf[0], PAINTBUFSIZE, &nchars);

    while ((calls != NULL)
    && (thiscall.y == calls->y)
    && (thiscall.x + thiscall.width == calls->x)
/*  && (thiscall.highlight == calls->highlight) ( >>>> current restriction )*/
    &&
      ((thiscall.highlight == calls->highlight)
       ||
      (! INVERTSELECTION)
       ||
      (! CHAR18BUG))
   ) {
      tracef0("merge with following paint call.\n");
      thiscall.width += calls->width;
      thiscall.callendopt = calls->callendopt;

/* >>>> tried a colour change: seems to cause blank chars?
      cbuf[nchars++] = 17;
      cbuf[nchars++] = 0;
      cbuf[nchars++] = 17;
      cbuf[nchars++] = 128+7;
*/

#if WIMPSETFONTCOLOUR
#else
      if (thiscall.highlight != calls->highlight) {
        thiscall.highlight = calls->highlight;
        if ((thiscall.highlight != (s->o.forecolour < 4))) {
          txtar__fonthilight(&cbuf[0], &nchars);
        } else {
          txtar__fontnohilight(&cbuf[0], &nchars);
        };
      };
#endif

      txtar__expandchars(
        s, calls->ad, calls->n, &cbuf[0], &obuf[0], PAINTBUFSIZE, &nchars);
        /* add to existing ones */
      calls = calls->next;
    };

    scx = s->d->info.box.x0 - s->d->info.scx +
      wimpt_dx() * (s->o.margin + (thiscall.x * txtar__xppinch()) / 72000);
    scy = (s->d->info.box.y1 /* >>>> - wimpt_dy() */ ) - thiscall.y;
    mpx = ((s->d->info.box.x0 - s->d->info.scx) / wimpt_dx() + s->o.margin)
             * (72000 / txtar__xppinch())
           + thiscall.x;
    mpy =
      ((s->d->info.box.y1 + s->baselineoffset - thiscall.y) / wimpt_dy())
      * (72000 / txtar__yppinch());
    if (
      scy + t->w->linesep < grclip->y0 ||
      scy > grclip->y1
    ) {
      tracef0("clipped.\n");
      /* go round the loop */
    } else if (alreadycorrect) {
      tracef0("inverting display.\n");
      if (thiscall.width > 0) {
        bbc_vdu(18); bbc_vdu(3); bbc_vdu(s->o.forecolour ^ s->o.backcolour); /*  swap fore/back */
        if (scx == s->o.margin * wimpt_dx() + s->d->info.box.x0) {/* left hand edge */
          txtar__grmoveto(
            scx - s->o.margin * wimpt_dx(),
            (scy < 0 ? 0 : scy));
        } else {
          txtar__grmoveto(
            (scx < 0 ? 0 : scx),
            (scy < 0 ? 0 : scy));
        };
        bbc_plot(101,
               txtar__max(0,
/*
                 scx +
                   wimpt_dx() *
                     ((thiscall.width * txtar__xppinch()) / 72000) +
                   wimpt_dx() / 2,
*/
                 s->d->info.box.x0 - s->d->info.scx - 1 +
                   wimpt_dx() * (s->o.margin + ((thiscall.x + thiscall.width) * txtar__xppinch()) / 72000)),
                 scy + t->w->linesep - 1);
      };
    } else {

      if (thiscall.highlight
      && INVERTSELECTION
      && (thiscall.callendopt != txt1_CECONTINUE)
      ) {
        if (CHAR18BUG) {
          /* We must blank the tail of the line. done before painting rather
          than after, so that italic overhang still looks right. */
#if WIMPSETFONTCOLOUR
          txtar__setgrcolour(128 + s->o.backcolour);
#else
          if (s->o.forecolour < 4) {
            txtar__setgrcolour(128 + 7);
          } else {
            txtar__setgrcolour(128 + 0); /* normal background */
          };
#endif
          txtar__grmoveto(
            txtar__max(0,
/*
              scx + wimpt_dx() * (1 + (width * xppinch()) / 72000)),
*/
              s->d->info.box.x0 - s->d->info.scx +
                wimpt_dx() * (s->o.margin +
                ((thiscall.x + thiscall.width) * txtar__xppinch()) / 72000)),
            scy);
          bbc_plot(103, s->d->info.box.x1, scy + t->w->linesep - 1);
        } else {
          /* life is a lot easier without char18bug. change colour and
          keep going to end of line. */
#if WIMPSETFONTCOLOUR
#else
          txtar__fontnohilight(&cbuf[0], &nchars);
#endif
        };
      };

      cbuf[nchars] = 0;

      /* Mark out the blanking box. */
      /* We blank out the left hand margin too if
      in the left hand char position, as not doing this can leave blobs. */
      if (scx == s->o.margin * wimpt_dx() + s->d->info.box.x0) {/* left hand edge */
        txtar__grmoveto(scx - s->o.margin * wimpt_dx(), (scy < 0 ? 0 : scy));
      } else {
        txtar__grmoveto((scx < 0 ? 0 : scx), (scy < 0 ? 0 : scy));
      };
      /* Large letters near the bottom of the screen need the conditionals
      above, if their actual base line is at a negative coord or the origin
      is to the left of the screen. */

      /* Set the box to blank out when writing the chars */
      if ((thiscall.callendopt != txt1_CECONTINUE)
      && ! (thiscall.highlight && CHAR18BUG && INVERTSELECTION)
      ) {
        txtar__grmoveto(s->d->info.box.x1,
                 scy + t->w->linesep - 1);
      } else {
        txtar__grmoveto(
          txtar__max(
            0,
            s->d->info.box.x0 - s->d->info.scx - 1 +
              wimpt_dx() * (s->o.margin + ((thiscall.x + thiscall.width) * txtar__xppinch()) / 72000)),
          scy + t->w->linesep - 1);
      };

#if FALSE
      /* actual writing takes place with y increased a little, as the
      fonts are zero'd at the bottom of an "o" not a "g". */
      mpy = mpy + (s->baselineoffset * 72000) / (wimpt_dy() * txtar__yppinch);
#endif

      tracef2(" mpx=%i mpy=%i.\n", mpx, mpy);

      /* Set up colours and font */
      /* >>>> Only worth optimising this if we can cut out the
      setfont entirely from most calls. Move it to paintseveral? */

#if WIMPSETFONTCOLOUR
      {
        if (thiscall.highlight) {
          wimp_setfontcolours(s->o.backcolour, s->o.forecolour);
        } else {
          wimp_setfontcolours(s->o.forecolour, s->o.backcolour);
        };
      };
#else
      {
        int bpp = wimpt_bpp();
        int cols = bpp == 8 ? 8 : (bpp == 4 ? 8 : 1 << bpp);
        if (thiscall.highlight != (s->o.forecolour < 4)) {
          wimpt_noerr(font_setcolour(s->fh, cols-1, cols-2, 2-cols));
        } else {
          wimpt_noerr(font_setcolour(s->fh, 0, 1, cols-2));
        };
      };
#endif

#if TRACE
      tracef0("font_paintchars:\n");
      for (i=0; i<nchars; i++) txtar__safewrch(cbuf[i]);
      tracef0("\n");
#endif

/* alternative to the setfontcolour above.
      wimpt_noerr(font_setfont(s->fh));
*/
      /* at this point, output to the trace window seems to destroy
      the setting up of the colour map. */
      /* >>>> can optimise this a little?
        e.g. one call per fontpaintseveral */

      {
        os_error *e;
        e = wimpt_complain(font_paint(&cbuf[0], font_ABS + font_RUBOUT, mpx, mpy));
        if (e != 0) {
          /* He lost the floppy with the fonts on it. Convert to system font. */
          txtar__font_paint_error = TRUE;
        };
      };

    };

  };
  tracef0("fontpaint done.\n");
}
/* >>>> Things with changes in highlight should also be done as a single
string, as colour-change characters can be placed in the string. */

/* >>>> Unfortunately, you can't change the colours etc. that the wimp gives
you without destroying and creating the window, in the case where the user
changes the colours. Hum! an argument for window being not quite the same
as a literal wimp window handle... */

/* The raw rectangle operations are inclusive of their maximum coordinates.
this is the reason for the -1s in some of the calls above. */

static void txtar__rawpaintseveral(txt t, txt1_call *calls, BOOL alreadycorrect)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  BOOL r;

  if (txtar__withinredraw) {
    /* We are nested within performredraw below. */
    /* "redrawdata" is static, set by performredraw */
    if (! txtar__redrawdone) {
      txtar__redrawdone = TRUE;
      while (1) {
        txtar__paintseveral(t, calls,
          &txtar__redrawdata.g, alreadycorrect);
        wimpt_noerr(wimp_get_rectangle(&txtar__redrawdata, &r));
        if (! r) break;
      }
    };
  } else {
    /* This is direct painting, probably due to some client/user edit. */
    /* >>>> If before first show, just register repaint necessary? */
    tracef0("rawpaintseveral.\n");
    txtar__redrawdata.w = s->w;
      /* could use a local redrawdata: doesn't matter */
    txtar__redrawdata.box = s->d->info.ex;
    txtar__redrawdata.scx = s->d->info.scx;
    txtar__redrawdata.scy = s->d->info.scy;
    wimpt_noerr(wimp_update_wind(&txtar__redrawdata, &r));
    if (r) {
      while (1) {
        txtar__paintseveral(t, calls,
          &txtar__redrawdata.g, alreadycorrect);
        wimpt_noerr(wimp_get_rectangle(&txtar__redrawdata, &r));
        if (! r) break;
      }
    } else {
      tracef0("window totally invisible?\n");
    };
  };
  if (txtar__font_paint_error) {
    txtar_options o;
    txtar__font_paint_error = FALSE;
    o = s->o;
    o.fixfont = TRUE;
    txtar__dosetoptions(t, &o);
    txtar__redrawtext(t);
  };
}

static BOOL txtar__rawcopylines(txt t, txt1_zits srcy, txt1_zits dsty, txt1_zits size)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_winfo *d = s->d;
  wimp_box b;

  tracef3("textarthur.rawcopylines srcy=%i dsty=%i size=%i\n",
    srcy, dsty, size);

  b.x0 = d->info.ex.x0;
  b.y0 = - (srcy + size);
  b.x1 = d->info.ex.x1;
  b.y1 = - srcy;

  wimp_blockcopy(s->w, &b, b.x0, - (dsty + size));

  return TRUE;
}

static int txtar__setarthurcaret(txt t, BOOL visible)
/* Returns x coord of caret, just for rawshowcaret below. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_caretstr c;

    if (txtar__withinredraw) return 0;
    tracef3("textarthur.setarthurcaret (%i,%i) %i\n",
        t->w->caretx, t->w->carety, visible);
    c.w = s->w;
    c.i = (wimp_i) -1;
    c.y = - (t->w->carety + t->w->caretoffsety);
    c.height = (t->w->linesep * 4 ) / 3; /* 8 bits high */
    c.y -= c.height / 8;
    if (! visible) c.height |= (1<<25); /* invisible */
    c.index = 0;
    if (s->o.fixfont) c.height |= (1<<24); /* vdu-type caret */
    c.x = wimpt_dx() * (s->o.margin + ((t->w->caretx +
          t->w->caretoffsetx) * txtar__xppinch()) / 72000);
    wimpt_noerr(wimp_set_caret_pos(&c));

    if (visible) {
      /* position the caret on the screen */
      if (t->w->carety + t->w->caretoffsety <
          txtar__min(t->w->linesep, t->w->limy)) {
        tracef0("auto-shifting, carety<=0.\n");
        txt1_domovevertical(
          t,
          - (1 + (t->w->linesep-1 - (t->w->carety + t->w->caretoffsety)) / t->w->linesep),
          TRUE);
      } else if (t->w->carety + t->w->caretoffsety > t->w->limy) {
        tracef0("auto-shifting, carety > limy.\n");
        txt1_domovevertical(
          t,
          ((t->w->linesep-1 + t->w->carety + t->w->caretoffsety - t->w->limy)
             / t->w->linesep),
          TRUE);
      };
    };

  return c.x;
}
/* >>>> If performance is being sought, it would be possible to leave the
caret invisible until a null event comes along. This would prevent needless
drawing and undrawing of the caret, which might also be visually beneficial
(i.e. reduce flicker). Not done yet. */

static void txtar__rawhidecaret(txt t)
{
  (void) txtar__setarthurcaret(t, 0);
}

static void txtar__rawshowcaret(txt t)
/* Corrective horizontal scrolling only happens if the caret is shown
by the user. This means, for example, that clicking the scroll bar does
allow the caret to disappear. This is the correct behaviour, I think. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  int x;
  int viswidth;
  BOOL move;

#if TRUE
  /* Experiment: showing the caret clears the caret bit in all other
  windows. To help CLI windows. */
  /* Second version: caret in all other text objects (wasn't right
  when several windows on the same object. */
  {
    txtar__sysdata *p = all;
    while (p != 0) {
      if (p->t != s->t) {
        p->t->charoptionset &= ~txt_CARET;
      };
      p = p->next;
    };
  };
#endif

  if (txtar__withinredraw) return;
  txt3_setprimarywindow(t, s);
  x = txtar__setarthurcaret(t, TRUE);
  move = FALSE;
  viswidth = s->d->info.box.x1 - s->d->info.box.x0;
  if (x < s->d->info.scx) {
    tracef2("force horiz: x=%i scroff.x=%i.\n", x,
      s->d->info.scx);
    wimpt_noerr(wimp_get_wind_info(s->d));
    /* ensure s->d up to date for forthcoming open. */
    s->d->info.scx = txtar__max(0, x - viswidth / 3);
    move = TRUE;
  } else if (x > s->d->info.scx + viswidth) {
    wimpt_noerr(wimp_get_wind_info(s->d));
    s->d->info.scx =
      txtar__min(s->d->info.ex.x1 - viswidth,
                 x - (3 * viswidth) / 4);
    move = TRUE;
  };
  if (move) {
    wimpt_noerr(wimp_open_wind((wimp_openstr*) s->d));
    txtar__verify(s);
  };
}

#ifndef UROM
static void txtar__redisplay(txt t)
/* Instead of calling txt1_redisplay right back, we invalidate the region.
It frequently happens that a major edit is the result of a menu or dialog box
interaction, in which case the unpainting of the menu/dbox is best combined
with the redrawing of the text object. */
{
  txtar__redrawtext(t);
}
#endif

static int txtar__dovisiblelinecount(txt t)
{
  return t->w->limy / t->w->linesep;
}

static int txtar__dovisiblecolcount(txt t)
{
  /* >>>> Should measure the width of a space character. Not done yet. */
  return t->w->limx / 16;
}

/* -------- Input from the user. -------- */

/* Manipulation of the input buffer. */

static BOOL txtar__insertevent(txt t, txt_eventcode e)
/* Insert the given eventcode into the buffer, behind everything currently
there. if (the buffer is full, throw away this new arrival and return FALSE.
Otherwise, return TRUE. */
{
    if (
      t->inbuftail + 1 == t->inbufhead ||
      ((t->inbuftail == txt1_INBUFMAX) && (t->inbufhead == 0))
    ) {
      /* full */
      return FALSE;
    } else {
      t->inbuf[t->inbuftail++] = e;
      if (t->inbuftail > txt1_INBUFMAX) t->inbuftail = 0;
      return TRUE;
    };
}

static void txtar__kbdchar(txt t, txt_eventcode e)
/* Put the char in the buffer, and call the eventproc to say that this
has happened. */
{
    if (txtar__insertevent(t, e)) {
      if (t->eventproc != NULL) {
        tracef0("calling eventproc with char...\n");
        t->eventnest++;
        t->eventproc(t, t->eventprochandle);
        t->eventnest--;
      };
    };
}

static void txtar__mouseevent(txt t, txt_eventcode flags, txt1_zits x, txt1_zits y)
/* Insert the given event into the buffer, if it will fit, and
call the event proc. */
{
  txt_index at;

    tracef3("mouseevent %i at (%i,%i).\n", flags, x, y);
    if (x < 0) {
      at = 0x0ffffff; /* "outside" */
      /* >>>> Does this happen? */
    } else {
      at = txt1_windowcoordstoindex(t, x, y);
    };
    txtar__kbdchar(t, 0x80000000 + flags + at);
}

static BOOL txtar__extractevent(txt t, txt_eventcode *e /*out*/)
/* get the next guy out of the text buffer. */
{
    if (t->inbuftail == t->inbufhead) {
      /* empty */
      return FALSE;
    } else {
      *e = t->inbuf[t->inbufhead++];
      if (t->inbufhead > txt1_INBUFMAX) t->inbufhead = 0;
      return TRUE;
    };
}

static txt_eventcode txtar__doget(txt t)
/* >>>> If nothing is waiting, should this call event_processevent? */
{
  txt_eventcode e;

  if (txtar__extractevent(t, &e)) {
    return e;
  } else {
    tracef0("doget with nothing to get!\n");
    return '?';
  };
}

static int txtar__doqueue(txt t)
{
    if (t->inbuftail >= t->inbufhead) {
      return t->inbuftail - t->inbufhead;
    } else {
      return txt1_INBUFMAX - (t->inbufhead - t->inbuftail);
    };
}

static void txtar__dounget(txt t, int code)
{
Robert Sprowson's avatar
Robert Sprowson committed
1759
#if FALSE
Neil Turton's avatar
Neil Turton committed
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
  txtar__insertevent(t, code);
  if (t->inbufhead == 0) {
    if (t->inbuftail == txt1_INBUFMAX) return; /* full */
    t->inbufhead = txt1_INBUFMAX;
  } else {
    if (t->inbufhead - 1 == t->inbuftail) return; /* full */
    t->inbufhead--;
  };
  t->inbuf[t->inbufhead] = code;
#else
  /* Assume buffer can never be full before this event?? */
  if (t->inbufhead == 0) t->inbufhead = txt1_INBUFMAX; else t->inbufhead--;
  /* Point to where last character came from */
  t->inbuf[t->inbufhead] = code;
  /* Place the character */
  if (t->inbufhead == t->inbuftail)
  {
    /* Buffer now over full, don't think this should happen */
    if (t->inbuftail == 0) t->inbuftail = txt1_INBUFMAX; else t->inbuftail--;
    /* throw away last keyboard event */
  }; /* End if */
#endif
}

static void txtar__newcharoptions(txt t, txt_charoption prev)
{
  t=t;
  prev=prev;
  tracef0("textarthur.newcharoptions.\n");
}

/* -------- Redrawing the window. -------- */

/* There is quite a mess about this in order that, when given several
rectangles to redraw, texts1 only does the measuring once. This gets spotted
in the interactions with rawpaintseveral. The static variables are used for
communicating this, and for preventing the drawing of the caret or setting of
extent during a redraw. We try not to crash in the case where txt1 does
more than one rawpaintseveral from a performredraw, maybe this will be useful
if (in the future) we try to limit use of store in the redraw structure. */

static void txtar__refreshcaret(txt t)
/* txt1 and txt2 have delicately separate ideas of what the caret is,
because txt2 knows that there's really only one of it, while the text
objects think of having one each. Thus, there are times when the caret, even
though visible, should only be drawn if it is already owned. */
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  wimp_caretstr c;

  tracef0("set caret.\n");
  wimpt_noerr(wimp_get_caret_pos(&c));
  if (c.w == s->w) {
    (void) txtar__setarthurcaret(t, 0 != (txt_CARET & t->charoptionset));
  };
}

static void txtar__performredraw(txt t)
{
  txtar__sysdata *s = (txtar__sysdata*) t->w->syshandle;
  BOOL vis;

  tracef0("performredraw.\n");
  txtar__withinredraw = TRUE; /* affects the behaviour of paint etc. */
  txtar__redrawdone = FALSE;
  txtar__redrawdata.w = s->w;
  txtar__redrawdata.box = s->d->info.box;
  txtar__redrawdata.scx = s->d->info.scx;
  txtar__redrawdata.scy = s->d->info.scy;

  wimpt_noerr(wimp_redraw_wind(&txtar__redrawdata, &vis));
  if (vis) {
    txt1_redisplay(t, s->freshredisplay);
    s->freshredisplay = FALSE;
      /* May call rawpaintseveral, setextent, rawsetcaret */
    if (! txtar__redrawdone) {
      while (1) {
        /* Throw away the other rectangles. */
        wimpt_noerr(wimp_get_rectangle(&txtar__redrawdata, &vis));
        if (! vis) break;
      }
    };
  };
  txtar__withinredraw = FALSE;
  tracef0("set size.\n");
  if (txtar__setsizepending) {
    tracef0("setting size.\n