| 1 | // Copyright (C) 2006-2009 by Tor Andersson, Jesse McGrew. |
| 2 | // Copyright (C) 2010 by Ben Cressey, Chris Spiegel. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <array> |
| 22 | #include <cstdint> |
| 23 | #include <cstring> |
| 24 | #include <memory> |
| 25 | #include <new> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "glk.h" |
| 29 | #include "garglk.h" |
| 30 | |
| 31 | // how many pixels we add to left/right margins |
| 32 | #define SLOP (2 * GLI_SUBPIX) |
| 33 | |
| 34 | static void |
| 35 | put_text(window_textbuffer_t *dwin, const char *buf, int len, int pos, int oldlen); |
| 36 | static void |
| 37 | put_text_uni(window_textbuffer_t *dwin, glui32 *buf, int len, int pos, int oldlen); |
| 38 | static bool |
| 39 | put_picture(window_textbuffer_t *dwin, const std::shared_ptr<picture_t> &pic, glui32 align, glui32 linkval); |
| 40 | static void |
| 41 | scrolloneline(window_textbuffer_t *dwin, bool forced, bool flow_break); |
| 42 | |
| 43 | static void touch(window_textbuffer_t *dwin, int line) |
| 44 | { |
| 45 | window_t *win = dwin->owner; |
| 46 | int y = win->bbox.y0 + gli_tmarginy + (dwin->height - line - 1) * gli_leading; |
| 47 | dwin->lines[line].dirty = true; |
| 48 | gli_clear_selection(); |
| 49 | winrepaint(win->bbox.x0, y - 2, win->bbox.x1, y + gli_leading + 2); |
| 50 | } |
| 51 | |
| 52 | static void touchscroll(window_textbuffer_t *dwin) |
| 53 | { |
| 54 | window_t *win = dwin->owner; |
| 55 | int i; |
| 56 | gli_clear_selection(); |
| 57 | winrepaint(win->bbox.x0, win->bbox.y0, win->bbox.x1, win->bbox.y1); |
| 58 | for (i = 0; i < dwin->scrollmax; i++) { |
| 59 | dwin->lines[i].dirty = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | std::vector<char> gli_get_text(window_textbuffer_t *dwin) |
| 64 | { |
| 65 | int s = dwin->scrollmax < SCROLLBACK ? dwin->scrollmax : SCROLLBACK - 1; |
| 66 | |
| 67 | std::vector<char> text; |
| 68 | for (int lineidx = s; lineidx >= 0; lineidx--) { |
| 69 | auto line = dwin->lines[lineidx]; |
| 70 | for (int charidx = 0; charidx < line.len; charidx++) { |
| 71 | std::array<char, 4> buf; |
| 72 | auto n = gli_encode_utf8(line.chars[charidx], buf.data(), 4); |
| 73 | for (int i = 0; i < n; i++) { |
| 74 | text.push_back(buf[i]); |
| 75 | } |
| 76 | } |
| 77 | // Only add newline if this was an actual paragraph break, not a wrapped line |
| 78 | if (line.newline) { |
| 79 | text.push_back(0x0a); // Unicode linefeed |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return text; |
| 84 | } |
| 85 | |
| 86 | static void reflow(window_t *win) |
| 87 | { |
| 88 | window_textbuffer_t *dwin = win->winbuffer(); |
| 89 | int inputbyte = -1; |
| 90 | attr_t curattr; |
| 91 | attr_t oldattr; |
| 92 | int i, k, p, s; |
| 93 | int x, f; |
| 94 | |
| 95 | if (dwin->height < 4 || dwin->width < 20) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | dwin->lines[0].len = dwin->numchars; |
| 100 | |
| 101 | std::vector<attr_t> attrbuf; |
| 102 | std::vector<glui32> charbuf; |
| 103 | std::vector<int> alignbuf; |
| 104 | std::vector<std::shared_ptr<picture_t>> pictbuf; |
| 105 | std::vector<glui32> hyperbuf; |
| 106 | std::vector<int> offsetbuf; |
| 107 | std::vector<int> flowbreakbuf; |
| 108 | |
| 109 | // allocate temp buffers |
| 110 | try { |
| 111 | attrbuf.resize(SCROLLBACK * TBLINELEN); |
| 112 | charbuf.resize(SCROLLBACK * TBLINELEN); |
| 113 | alignbuf.resize(SCROLLBACK); |
| 114 | pictbuf.resize(SCROLLBACK); |
| 115 | hyperbuf.resize(SCROLLBACK); |
| 116 | offsetbuf.resize(SCROLLBACK); |
| 117 | flowbreakbuf.resize(SCROLLBACK); |
| 118 | } catch (const std::bad_alloc &) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // copy text to temp buffers |
| 123 | |
| 124 | oldattr = win->attr; |
| 125 | curattr.clear(); |
| 126 | |
| 127 | x = 0; |
| 128 | f = 0; |
| 129 | p = 0; |
| 130 | s = dwin->scrollmax < SCROLLBACK ? dwin->scrollmax : SCROLLBACK - 1; |
| 131 | |
| 132 | for (k = s; k >= 0; k--) { |
| 133 | if (k == 0 && win->line_request) { |
| 134 | inputbyte = p + dwin->infence; |
| 135 | } |
| 136 | |
| 137 | if (dwin->lines[k].lpic) { |
| 138 | offsetbuf[x] = p; |
| 139 | alignbuf[x] = imagealign_MarginLeft; |
| 140 | pictbuf[x] = dwin->lines[k].lpic; |
| 141 | hyperbuf[x] = dwin->lines[k].lhyper; |
| 142 | x++; |
| 143 | } |
| 144 | |
| 145 | if (dwin->lines[k].rpic) { |
| 146 | offsetbuf[x] = p; |
| 147 | alignbuf[x] = imagealign_MarginRight; |
| 148 | pictbuf[x] = dwin->lines[k].rpic; |
| 149 | hyperbuf[x] = dwin->lines[k].rhyper; |
| 150 | x++; |
| 151 | } |
| 152 | |
| 153 | auto flow_break_pos = dwin->lines[k].flow_break_pos; |
| 154 | if (flow_break_pos >= 0) { |
| 155 | flowbreakbuf[f] = p + flow_break_pos; |
| 156 | f++; |
| 157 | } |
| 158 | |
| 159 | for (i = 0; i < dwin->lines[k].len; i++) { |
| 160 | attrbuf[p] = curattr = dwin->lines[k].attrs[i]; |
| 161 | charbuf[p] = dwin->lines[k].chars[i]; |
| 162 | p++; |
| 163 | } |
| 164 | |
| 165 | if (dwin->lines[k].newline) { |
| 166 | attrbuf[p] = curattr; |
| 167 | charbuf[p] = '\n'; |
| 168 | p++; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | offsetbuf[x] = -1; |
| 173 | |
| 174 | // clear window |
| 175 | |
| 176 | win_textbuffer_clear(win); |
| 177 | |
| 178 | // and dump text back |
| 179 | |
| 180 | x = 0; |
| 181 | f = 0; |
| 182 | for (i = 0; i < p; i++) { |
| 183 | if (i == inputbyte) { |
| 184 | break; |
| 185 | } |
| 186 | win->attr = attrbuf[i]; |
| 187 | |
| 188 | if (offsetbuf[x] == i) { |
| 189 | put_picture(dwin, pictbuf[x], alignbuf[x], hyperbuf[x]); |
| 190 | x++; |
| 191 | } |
| 192 | |
| 193 | if (flowbreakbuf[f] == i) { |
| 194 | scrolloneline(dwin, false, true); |
| 195 | f++; |
| 196 | } |
| 197 | |
| 198 | win_textbuffer_putchar_uni(win, charbuf[i]); |
| 199 | } |
| 200 | |
| 201 | // terribly sorry about this... |
| 202 | dwin->lastseen = 0; |
| 203 | dwin->scrollpos = 0; |
| 204 | |
| 205 | if (inputbyte != -1) { |
| 206 | dwin->infence = dwin->numchars; |
| 207 | put_text_uni(dwin, charbuf.data() + inputbyte, p - inputbyte, dwin->numchars, 0); |
| 208 | dwin->incurs = dwin->numchars; |
| 209 | } |
| 210 | |
| 211 | win->attr = oldattr; |
| 212 | |
| 213 | touchscroll(dwin); |
| 214 | } |
| 215 | |
| 216 | void win_textbuffer_rearrange(window_t *win, rect_t *box) |
| 217 | { |
| 218 | window_textbuffer_t *dwin = win->winbuffer(); |
| 219 | int newwid, newhgt; |
| 220 | int rnd; |
| 221 | |
| 222 | dwin->owner->bbox = *box; |
| 223 | |
| 224 | newwid = (box->x1 - box->x0 - gli_tmarginx * 2 - gli_scroll_width) / gli_cellw; |
| 225 | newhgt = (box->y1 - box->y0 - gli_tmarginy * 2) / gli_cellh; |
| 226 | |
| 227 | // align text with bottom |
| 228 | rnd = newhgt * gli_cellh + gli_tmarginy * 2; |
| 229 | win->yadj = (box->y1 - box->y0 - rnd); |
| 230 | dwin->owner->bbox.y0 += (box->y1 - box->y0 - rnd); |
| 231 | |
| 232 | if (newwid != dwin->width) { |
| 233 | dwin->width = newwid; |
| 234 | reflow(win); |
| 235 | } |
| 236 | |
| 237 | if (newhgt != dwin->height) { |
| 238 | // scroll up if we obscure new lines |
| 239 | if (dwin->lastseen >= newhgt - 1) { |
| 240 | dwin->scrollpos += (dwin->height - newhgt); |
| 241 | } |
| 242 | |
| 243 | dwin->height = newhgt; |
| 244 | |
| 245 | // keep window within 'valid' lines |
| 246 | if (dwin->scrollpos > dwin->scrollmax - dwin->height + 1) { |
| 247 | dwin->scrollpos = dwin->scrollmax - dwin->height + 1; |
| 248 | } |
| 249 | if (dwin->scrollpos < 0) { |
| 250 | dwin->scrollpos = 0; |
| 251 | } |
| 252 | touchscroll(dwin); |
| 253 | |
| 254 | dwin->copybuf.clear(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static int calcwidth(window_textbuffer_t *dwin, |
| 259 | const glui32 *chars, const attr_t *attrs, |
| 260 | int startchar, int numchars, int spw) |
| 261 | { |
| 262 | int w = 0; |
| 263 | int a, b; |
| 264 | |
| 265 | a = startchar; |
| 266 | for (b = startchar; b < numchars; b++) { |
| 267 | if (attrs[a] != attrs[b]) { |
| 268 | w += gli_string_width_uni(attrs[a].font(dwin->styles), |
| 269 | chars + a, b - a, spw); |
| 270 | a = b; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | w += gli_string_width_uni(attrs[a].font(dwin->styles), |
| 275 | chars + a, b - a, spw); |
| 276 | |
| 277 | return w; |
| 278 | } |
| 279 | |
| 280 | static int calcwidth(window_textbuffer_t *dwin, |
| 281 | const std::array<glui32, TBLINELEN> &chars, const std::array<attr_t, TBLINELEN> &attrs, |
| 282 | int startchar, int numchars, int spw) |
| 283 | { |
| 284 | return calcwidth(dwin, chars.data(), attrs.data(), startchar, numchars, spw); |
| 285 | } |
| 286 | |
| 287 | void win_textbuffer_redraw(window_t *win) |
| 288 | { |
| 289 | window_textbuffer_t *dwin = win->winbuffer(); |
| 290 | tbline_t ln; |
| 291 | int linelen; |
| 292 | int nsp, spw, pw; |
| 293 | int x0, y0, x1, y1; |
| 294 | int x, y, w; |
| 295 | int a, b; |
| 296 | glui32 link; |
| 297 | int i; |
| 298 | int hx0, hx1, hy0, hy1; |
| 299 | bool selbuf, selrow, selchar; |
| 300 | // NOTE: GCC complains these might be used uninitialized; they're |
| 301 | // not, but do this to silence the warning. |
| 302 | int sx0 = 0, sx1 = 0; |
| 303 | bool selleft = false, selright = false; |
| 304 | int tx, tsc, tsw, lsc, rsc; |
| 305 | |
| 306 | dwin->lines[0].len = dwin->numchars; |
| 307 | |
| 308 | x0 = (win->bbox.x0 + gli_tmarginx) * GLI_SUBPIX; |
| 309 | x1 = (win->bbox.x1 - gli_tmarginx - gli_scroll_width) * GLI_SUBPIX; |
| 310 | y0 = win->bbox.y0 + gli_tmarginy; |
| 311 | y1 = win->bbox.y1 - gli_tmarginy; |
| 312 | |
| 313 | pw = x1 - x0 - 2 * GLI_SUBPIX; |
| 314 | |
| 315 | // check if any part of buffer is selected |
| 316 | selbuf = gli_check_selection(x0 / GLI_SUBPIX, y0, x1 / GLI_SUBPIX, y1); |
| 317 | |
| 318 | for (i = dwin->scrollpos + dwin->height - 1; i >= dwin->scrollpos; i--) { |
| 319 | // top of line |
| 320 | y = y0 + (dwin->height - (i - dwin->scrollpos) - 1) * gli_leading; |
| 321 | |
| 322 | // check if part of line is selected |
| 323 | if (selbuf) { |
| 324 | int ux, uy; |
| 325 | selrow = gli_get_selection(x0 / GLI_SUBPIX, y, |
| 326 | x1 / GLI_SUBPIX, y + gli_leading, |
| 327 | &ux, &uy); |
| 328 | sx0 = ux; |
| 329 | sx1 = uy; |
| 330 | selleft = (sx0 == x0 / GLI_SUBPIX); |
| 331 | selright = (sx1 == x1 / GLI_SUBPIX); |
| 332 | } else { |
| 333 | selrow = false; |
| 334 | } |
| 335 | |
| 336 | // mark selected line dirty |
| 337 | if (selrow) { |
| 338 | dwin->lines[i].dirty = true; |
| 339 | } |
| 340 | |
| 341 | ln = dwin->lines[i]; |
| 342 | |
| 343 | // skip if we can |
| 344 | if (!ln.dirty && !ln.repaint && !gli_force_redraw && dwin->scrollpos == 0) { |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | // repaint previously selected lines if needed |
| 349 | if (ln.repaint && !gli_force_redraw) { |
| 350 | gli_redraw_rect(x0 / GLI_SUBPIX, y, x1 / GLI_SUBPIX, y + gli_leading); |
| 351 | } |
| 352 | |
| 353 | // keep selected line dirty and flag for repaint |
| 354 | if (!selrow) { |
| 355 | dwin->lines[i].dirty = false; |
| 356 | dwin->lines[i].repaint = false; |
| 357 | } else { |
| 358 | dwin->lines[i].repaint = true; |
| 359 | } |
| 360 | |
| 361 | // leave bottom line blank for [more] prompt |
| 362 | if (i == dwin->scrollpos && i > 0) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | linelen = ln.len; |
| 367 | |
| 368 | // kill spaces at the end unless they're a different color |
| 369 | Color color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor; |
| 370 | while (i > 0 && linelen > 1 && ln.chars[linelen - 1] == ' ' |
| 371 | && ln.attrs[linelen - 1].bgcolor == color |
| 372 | && !ln.attrs[linelen - 1].reverse) { |
| 373 | linelen--; |
| 374 | } |
| 375 | |
| 376 | // kill characters that would overwrite the scroll bar |
| 377 | while (linelen > 1 && calcwidth(dwin, ln.chars, ln.attrs, 0, linelen, -1) >= pw) { |
| 378 | linelen--; |
| 379 | } |
| 380 | |
| 381 | // count spaces and width for justification |
| 382 | if (gli_conf_justify && !ln.newline && i > 0) { |
| 383 | for (a = 0, nsp = 0; a < linelen; a++) { |
| 384 | if (ln.chars[a] == ' ') { |
| 385 | nsp++; |
| 386 | } |
| 387 | } |
| 388 | w = calcwidth(dwin, ln.chars, ln.attrs, 0, linelen, 0); |
| 389 | if (nsp != 0) { |
| 390 | spw = (x1 - x0 - ln.lm - ln.rm - 2 * SLOP - w) / nsp; |
| 391 | } else { |
| 392 | spw = 0; |
| 393 | } |
| 394 | } else { |
| 395 | spw = -1; |
| 396 | } |
| 397 | |
| 398 | // find and highlight selected characters |
| 399 | if (selrow && !gli_claimselect) { |
| 400 | lsc = 0; |
| 401 | rsc = 0; |
| 402 | selchar = false; |
| 403 | // optimized case for all chars selected |
| 404 | if (selleft && selright) { |
| 405 | rsc = linelen > 0 ? linelen - 1 : 0; |
| 406 | selchar = ((calcwidth(dwin, ln.chars, ln.attrs, lsc, rsc, spw) / GLI_SUBPIX) != 0); |
| 407 | } else { |
| 408 | // optimized case for leftmost char selected |
| 409 | if (selleft) { |
| 410 | tsc = linelen > 0 ? linelen - 1 : 0; |
| 411 | selchar = ((calcwidth(dwin, ln.chars, ln.attrs, lsc, tsc, spw) / GLI_SUBPIX) != 0); |
| 412 | } else { |
| 413 | // find the substring contained by the selection |
| 414 | tx = (x0 + SLOP + ln.lm) / GLI_SUBPIX; |
| 415 | // measure string widths until we find left char |
| 416 | for (tsc = 0; tsc < linelen; tsc++) { |
| 417 | tsw = calcwidth(dwin, ln.chars, ln.attrs, 0, tsc, spw) / GLI_SUBPIX; |
| 418 | if (tsw + tx >= sx0 || |
| 419 | (tsw + tx + GLI_SUBPIX >= sx0 && ln.chars[tsc] != ' ')) { |
| 420 | lsc = tsc; |
| 421 | selchar = true; |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | if (selchar) { |
| 427 | // optimized case for rightmost char selected |
| 428 | if (selright) { |
| 429 | rsc = linelen > 0 ? linelen - 1 : 0; |
| 430 | } else { |
| 431 | // measure string widths until we find right char |
| 432 | for (tsc = lsc; tsc < linelen; tsc++) { |
| 433 | tsw = calcwidth(dwin, ln.chars, ln.attrs, lsc, tsc, spw) / GLI_SUBPIX; |
| 434 | if (tsw + sx0 < sx1) { |
| 435 | rsc = tsc; |
| 436 | } |
| 437 | } |
| 438 | if (lsc != 0 && rsc == 0) { |
| 439 | rsc = lsc; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | // reverse colors for selected chars |
| 445 | if (selchar) { |
| 446 | for (tsc = lsc; tsc <= rsc; tsc++) { |
| 447 | ln.attrs[tsc].reverse = !ln.attrs[tsc].reverse; |
| 448 | dwin->copybuf.push_back(ln.chars[tsc]); |
| 449 | } |
| 450 | } |
| 451 | // add newline only if this is a real paragraph break, not just a wrapped line |
| 452 | if ((ln.len == 0 || ln.len == (rsc + 1)) && ln.newline) { |
| 453 | dwin->copybuf.push_back('\n'); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // clear any stored hyperlink coordinates |
| 458 | gli_put_hyperlink(0, x0 / GLI_SUBPIX, y, |
| 459 | x1 / GLI_SUBPIX, y + gli_leading); |
| 460 | |
| 461 | // fill in background colors |
| 462 | color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor; |
| 463 | gli_draw_rect(x0 / GLI_SUBPIX, y, |
| 464 | (x1 - x0) / GLI_SUBPIX, gli_leading, |
| 465 | color); |
| 466 | |
| 467 | x = x0 + SLOP + ln.lm; |
| 468 | a = 0; |
| 469 | for (b = 0; b < linelen; b++) { |
| 470 | if (ln.attrs[a] != ln.attrs[b]) { |
| 471 | link = ln.attrs[a].hyper; |
| 472 | auto font = ln.attrs[a].font(dwin->styles); |
| 473 | color = ln.attrs[a].bg(dwin->styles); |
| 474 | w = gli_string_width_uni(font, &ln.chars[a], b - a, spw); |
| 475 | gli_draw_rect(x / GLI_SUBPIX, y, |
| 476 | w / GLI_SUBPIX, gli_leading, |
| 477 | color); |
| 478 | if (link != 0) { |
| 479 | if (gli_underline_hyperlinks) { |
| 480 | gli_draw_rect(x / GLI_SUBPIX + 1, y + gli_baseline + 1, |
| 481 | w / GLI_SUBPIX + 1, 1, |
| 482 | gli_link_color); |
| 483 | } |
| 484 | gli_put_hyperlink(link, x / GLI_SUBPIX, y, |
| 485 | x / GLI_SUBPIX + w / GLI_SUBPIX, |
| 486 | y + gli_leading); |
| 487 | } |
| 488 | x += w; |
| 489 | a = b; |
| 490 | } |
| 491 | } |
| 492 | link = ln.attrs[a].hyper; |
| 493 | auto font = ln.attrs[a].font(dwin->styles); |
| 494 | color = ln.attrs[a].bg(dwin->styles); |
| 495 | w = gli_string_width_uni(font, &ln.chars[a], b - a, spw); |
| 496 | gli_draw_rect(x / GLI_SUBPIX, y, w / GLI_SUBPIX, |
| 497 | gli_leading, color); |
| 498 | if (link != 0) { |
| 499 | if (gli_underline_hyperlinks) { |
| 500 | gli_draw_rect(x / GLI_SUBPIX + 1, y + gli_baseline + 1, |
| 501 | w / GLI_SUBPIX + 1, 1, |
| 502 | gli_link_color); |
| 503 | } |
| 504 | gli_put_hyperlink(link, x / GLI_SUBPIX, y, |
| 505 | x / GLI_SUBPIX + w / GLI_SUBPIX, |
| 506 | y + gli_leading); |
| 507 | } |
| 508 | x += w; |
| 509 | |
| 510 | color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor; |
| 511 | gli_draw_rect(x / GLI_SUBPIX, y, |
| 512 | x1 / GLI_SUBPIX - x / GLI_SUBPIX, gli_leading, |
| 513 | color); |
| 514 | |
| 515 | // |
| 516 | // draw caret |
| 517 | // |
| 518 | |
| 519 | if (gli_focuswin == win && i == 0 && (win->line_request || win->line_request_uni)) { |
| 520 | w = calcwidth(dwin, dwin->chars, dwin->attrs, 0, dwin->incurs, spw); |
| 521 | if (w < pw - gli_caret_shape * 2 * GLI_SUBPIX) { |
| 522 | gli_draw_caret(x0 + SLOP + ln.lm + w, y + gli_baseline); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // |
| 527 | // draw text |
| 528 | // |
| 529 | |
| 530 | x = x0 + SLOP + ln.lm; |
| 531 | a = 0; |
| 532 | for (b = 0; b < linelen; b++) { |
| 533 | if (ln.attrs[a] != ln.attrs[b]) { |
| 534 | link = ln.attrs[a].hyper; |
| 535 | font = ln.attrs[a].font(dwin->styles); |
| 536 | color = link != 0 ? gli_link_color : ln.attrs[a].fg(dwin->styles); |
| 537 | x = gli_draw_string_uni(x, y + gli_baseline, |
| 538 | font, color, &ln.chars[a], b - a, spw); |
| 539 | a = b; |
| 540 | } |
| 541 | } |
| 542 | link = ln.attrs[a].hyper; |
| 543 | font = ln.attrs[a].font(dwin->styles); |
| 544 | color = link != 0 ? gli_link_color : ln.attrs[a].fg(dwin->styles); |
| 545 | gli_draw_string_uni(x, y + gli_baseline, |
| 546 | font, color, &ln.chars[a], linelen - a, spw); |
| 547 | } |
| 548 | |
| 549 | // |
| 550 | // draw more prompt |
| 551 | // |
| 552 | |
| 553 | if (dwin->scrollpos != 0 && dwin->height > 1) { |
| 554 | x = x0 + SLOP; |
| 555 | y = y0 + (dwin->height - 1) * gli_leading; |
| 556 | |
| 557 | gli_put_hyperlink(0, x0 / GLI_SUBPIX, y, |
| 558 | x1/GLI_SUBPIX, y + gli_leading); |
| 559 | |
| 560 | Color color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor; |
| 561 | gli_draw_rect(x / GLI_SUBPIX, y, |
| 562 | x1 / GLI_SUBPIX - x / GLI_SUBPIX, gli_leading, |
| 563 | color); |
| 564 | |
| 565 | w = gli_string_width_uni(gli_more_font, |
| 566 | gli_more_prompt.data(), gli_more_prompt_len, -1); |
| 567 | |
| 568 | if (gli_more_align == 1) { // center |
| 569 | x = x0 + SLOP + (x1 - x0 - w - SLOP * 2) / 2; |
| 570 | } else if (gli_more_align == 2) { // right |
| 571 | x = x1 - SLOP - w; |
| 572 | } |
| 573 | |
| 574 | color = gli_override_fg.has_value() ? gli_more_color : win->fgcolor; |
| 575 | gli_draw_string_uni(x, y + gli_baseline, |
| 576 | gli_more_font, color, |
| 577 | gli_more_prompt.data(), gli_more_prompt_len, -1); |
| 578 | y1 = y; // don't want pictures overdrawing "[more]" |
| 579 | |
| 580 | // try to claim the focus |
| 581 | dwin->owner->more_request = true; |
| 582 | gli_more_focus = true; |
| 583 | } else { |
| 584 | dwin->owner->more_request = false; |
| 585 | y1 = y0 + dwin->height * gli_leading; |
| 586 | } |
| 587 | |
| 588 | // |
| 589 | // draw the images |
| 590 | // |
| 591 | |
| 592 | for (i = 0; i < dwin->scrollback; i++) { |
| 593 | ln = dwin->lines[i]; |
| 594 | |
| 595 | y = y0 + (dwin->height - (i - dwin->scrollpos) - 1) * gli_leading; |
| 596 | |
| 597 | if (ln.lpic) { |
| 598 | if (y < y1 && y + ln.lpic->h > y0) { |
| 599 | gli_draw_picture(ln.lpic.get(), |
| 600 | x0 / GLI_SUBPIX, y, |
| 601 | x0 / GLI_SUBPIX, y0, x1 / GLI_SUBPIX, y1); |
| 602 | link = ln.lhyper; |
| 603 | hy0 = y > y0 ? y : y0; |
| 604 | hy1 = y + ln.lpic->h < y1 ? y + ln.lpic->h : y1; |
| 605 | hx0 = x0 / GLI_SUBPIX; |
| 606 | hx1 = x0 / GLI_SUBPIX + ln.lpic->w < x1 / GLI_SUBPIX |
| 607 | ? x0 / GLI_SUBPIX + ln.lpic->w |
| 608 | : x1 / GLI_SUBPIX; |
| 609 | gli_put_hyperlink(link, hx0, hy0, hx1, hy1); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (ln.rpic) { |
| 614 | if (y < y1 && y + ln.rpic->h > y0) { |
| 615 | gli_draw_picture(ln.rpic.get(), |
| 616 | x1 / GLI_SUBPIX - ln.rpic->w, y, |
| 617 | x0 / GLI_SUBPIX, y0, x1 / GLI_SUBPIX, y1); |
| 618 | link = ln.rhyper; |
| 619 | hy0 = y > y0 ? y : y0; |
| 620 | hy1 = y + ln.rpic->h < y1 ? y + ln.rpic->h : y1; |
| 621 | hx0 = x1 / GLI_SUBPIX - ln.rpic->w > x0 / GLI_SUBPIX |
| 622 | ? x1 / GLI_SUBPIX - ln.rpic->w |
| 623 | : x0 / GLI_SUBPIX; |
| 624 | hx1 = x1 / GLI_SUBPIX; |
| 625 | gli_put_hyperlink(link, hx0, hy0, hx1, hy1); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // |
| 631 | // Draw the scrollbar |
| 632 | // |
| 633 | |
| 634 | // try to claim scroll keys |
| 635 | dwin->owner->scroll_request = dwin->scrollmax > dwin->height; |
| 636 | |
| 637 | if (dwin->owner->scroll_request && gli_scroll_width != 0) { |
| 638 | int t0, t1; |
| 639 | x0 = win->bbox.x1 - gli_scroll_width; |
| 640 | x1 = win->bbox.x1; |
| 641 | y0 = win->bbox.y0 + gli_tmarginy; |
| 642 | y1 = win->bbox.y1 - gli_tmarginy; |
| 643 | |
| 644 | gli_put_hyperlink(0, x0, y0, x1, y1); |
| 645 | |
| 646 | y0 += gli_scroll_width / 2; |
| 647 | y1 -= gli_scroll_width / 2; |
| 648 | |
| 649 | // pos = thbot, pos - ht = thtop, max = wtop, 0 = wbot |
| 650 | t0 = (dwin->scrollmax - dwin->scrollpos) - (dwin->height - 1); |
| 651 | t1 = (dwin->scrollmax - dwin->scrollpos); |
| 652 | if (dwin->scrollmax > dwin->height) { |
| 653 | t0 = t0 * (y1 - y0) / dwin->scrollmax + y0; |
| 654 | t1 = t1 * (y1 - y0) / dwin->scrollmax + y0; |
| 655 | } else { |
| 656 | t0 = t1 = y0; |
| 657 | } |
| 658 | |
| 659 | gli_draw_rect(x0 + 1, y0, x1 - x0 - 2, y1 - y0, gli_scroll_bg); |
| 660 | gli_draw_rect(x0 + 1, t0, x1 - x0 - 2, t1 - t0, gli_scroll_fg); |
| 661 | |
| 662 | for (i = 0; i < gli_scroll_width / 2 + 1; i++) { |
| 663 | gli_draw_rect(x0 + gli_scroll_width / 2-i, |
| 664 | y0 - gli_scroll_width / 2 + i, |
| 665 | i * 2, 1, gli_scroll_fg); |
| 666 | gli_draw_rect(x0 + gli_scroll_width / 2 - i, |
| 667 | y1 + gli_scroll_width / 2 - i, |
| 668 | i * 2, 1, gli_scroll_fg); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // send selected text to clipboard |
| 673 | if (selbuf && !dwin->copybuf.empty()) { |
| 674 | gli_claimselect = true; |
| 675 | gli_clipboard_copy(dwin->copybuf); |
| 676 | dwin->copybuf.clear(); |
| 677 | } |
| 678 | |
| 679 | // no more prompt means all text has been seen |
| 680 | if (!dwin->owner->more_request) { |
| 681 | dwin->lastseen = 0; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | static void scrollresize(window_textbuffer_t *dwin) |
| 686 | { |
| 687 | int i; |
| 688 | |
| 689 | dwin->lines.resize(dwin->scrollback + SCROLLBACK); |
| 690 | |
| 691 | dwin->chars = dwin->lines[0].chars.data(); |
| 692 | dwin->attrs = dwin->lines[0].attrs.data(); |
| 693 | |
| 694 | for (i = dwin->scrollback; i < (dwin->scrollback + SCROLLBACK); i++) { |
| 695 | dwin->lines[i].dirty = false; |
| 696 | dwin->lines[i].repaint = false; |
| 697 | dwin->lines[i].lm = 0; |
| 698 | dwin->lines[i].rm = 0; |
| 699 | dwin->lines[i].lpic.reset(); |
| 700 | dwin->lines[i].rpic.reset(); |
| 701 | dwin->lines[i].lhyper = 0; |
| 702 | dwin->lines[i].rhyper = 0; |
| 703 | dwin->lines[i].len = 0; |
| 704 | dwin->lines[i].flow_break_pos = -1; |
| 705 | dwin->lines[i].newline = false; |
| 706 | dwin->lines[i].chars.fill(' '); |
| 707 | dwin->lines[i].attrs.fill(attr_t{}); |
| 708 | } |
| 709 | |
| 710 | dwin->scrollback += SCROLLBACK; |
| 711 | } |
| 712 | |
| 713 | static void scrolloneline(window_textbuffer_t *dwin, bool forced, bool flow_break = false) |
| 714 | { |
| 715 | int i; |
| 716 | int lines_to_scroll = 1; |
| 717 | |
| 718 | if (flow_break) { |
| 719 | if (dwin->ladjn == 0 && dwin->radjn == 0) { |
| 720 | auto &line = dwin->lines[0]; |
| 721 | line.flow_break_pos = line.len; |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | lines_to_scroll = std::max(dwin->ladjn, dwin->radjn); |
| 726 | } |
| 727 | |
| 728 | dwin->lastseen += lines_to_scroll; |
| 729 | dwin->scrollmax += lines_to_scroll; |
| 730 | |
| 731 | if (dwin->scrollmax > dwin->scrollback - lines_to_scroll |
| 732 | || dwin->lastseen > dwin->scrollback - lines_to_scroll) { |
| 733 | scrollresize(dwin); |
| 734 | } |
| 735 | |
| 736 | if (dwin->lastseen >= dwin->height) { |
| 737 | dwin->scrollpos += lines_to_scroll; |
| 738 | } |
| 739 | |
| 740 | if (dwin->scrollpos > dwin->scrollmax - dwin->height + lines_to_scroll) { |
| 741 | dwin->scrollpos = dwin->scrollmax - dwin->height + lines_to_scroll; |
| 742 | } |
| 743 | if (dwin->scrollpos < 0) { |
| 744 | dwin->scrollpos = 0; |
| 745 | } |
| 746 | |
| 747 | if (forced || flow_break) { |
| 748 | dwin->dashed = 0; |
| 749 | } |
| 750 | dwin->spaced = 0; |
| 751 | |
| 752 | auto &line_0 = dwin->lines[0]; |
| 753 | line_0.len = dwin->numchars; |
| 754 | line_0.newline = forced; |
| 755 | if (flow_break) { |
| 756 | line_0.flow_break_pos = line_0.len; |
| 757 | } |
| 758 | |
| 759 | for (i = dwin->scrollback - 1; i > lines_to_scroll - 1; i--) { |
| 760 | dwin->lines[i] = dwin->lines[i - lines_to_scroll]; |
| 761 | if (i < dwin->height) { |
| 762 | touch(dwin, i); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | if (flow_break) { |
| 767 | dwin->radjn = 0; |
| 768 | dwin->ladjn = 0; |
| 769 | } |
| 770 | |
| 771 | if (dwin->radjn != 0) { |
| 772 | dwin->radjn--; |
| 773 | } |
| 774 | if (dwin->radjn == 0) { |
| 775 | dwin->radjw = 0; |
| 776 | } |
| 777 | if (dwin->ladjn != 0) { |
| 778 | dwin->ladjn--; |
| 779 | } |
| 780 | if (dwin->ladjn == 0) { |
| 781 | dwin->ladjw = 0; |
| 782 | } |
| 783 | |
| 784 | for (i = 0; i < lines_to_scroll; i++) { |
| 785 | touch(dwin, i); |
| 786 | dwin->lines[i].len = 0; |
| 787 | dwin->lines[i].flow_break_pos = -1; |
| 788 | dwin->lines[i].newline = false; |
| 789 | dwin->lines[i].lm = dwin->ladjw; |
| 790 | dwin->lines[i].rm = dwin->radjw; |
| 791 | dwin->lines[i].lpic.reset(); |
| 792 | dwin->lines[i].rpic.reset(); |
| 793 | dwin->lines[i].lhyper = 0; |
| 794 | dwin->lines[i].rhyper = 0; |
| 795 | dwin->lines[i].chars.fill(' '); |
| 796 | dwin->lines[i].attrs.fill(attr_t{}); |
| 797 | } |
| 798 | |
| 799 | dwin->numchars = 0; |
| 800 | |
| 801 | touchscroll(dwin); |
| 802 | } |
| 803 | |
| 804 | // only for input text |
| 805 | static void put_text(window_textbuffer_t *dwin, const char *buf, int len, int pos, int oldlen) |
| 806 | { |
| 807 | int diff = len - oldlen; |
| 808 | |
| 809 | if (dwin->numchars + diff >= TBLINELEN) { |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | if (diff != 0 && pos + oldlen < dwin->numchars) { |
| 814 | std::memmove(dwin->chars + pos + len, |
| 815 | dwin->chars + pos + oldlen, |
| 816 | (dwin->numchars - (pos + oldlen)) * 4); |
| 817 | std::memmove(dwin->attrs + pos + len, |
| 818 | dwin->attrs + pos + oldlen, |
| 819 | (dwin->numchars - (pos + oldlen)) * sizeof(attr_t)); |
| 820 | } |
| 821 | if (len > 0) { |
| 822 | int i; |
| 823 | for (i = 0; i < len; i++) { |
| 824 | dwin->chars[pos + i] = static_cast<unsigned char>(buf[i]); |
| 825 | dwin->attrs[pos + i].set(style_Input); |
| 826 | } |
| 827 | } |
| 828 | dwin->numchars += diff; |
| 829 | |
| 830 | if (dwin->inbuf != nullptr) { |
| 831 | if (dwin->incurs >= pos + oldlen) { |
| 832 | dwin->incurs += diff; |
| 833 | } else if (dwin->incurs >= pos) { |
| 834 | dwin->incurs = pos + len; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | touch(dwin, 0); |
| 839 | } |
| 840 | |
| 841 | static void put_text_uni(window_textbuffer_t *dwin, glui32 *buf, int len, int pos, int oldlen) |
| 842 | { |
| 843 | int diff = len - oldlen; |
| 844 | |
| 845 | if (dwin->numchars + diff >= TBLINELEN) { |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | if (diff != 0 && pos + oldlen < dwin->numchars) { |
| 850 | std::memmove(dwin->chars + pos + len, |
| 851 | dwin->chars + pos + oldlen, |
| 852 | (dwin->numchars - (pos + oldlen)) * 4); |
| 853 | std::memmove(dwin->attrs + pos + len, |
| 854 | dwin->attrs + pos + oldlen, |
| 855 | (dwin->numchars - (pos + oldlen)) * sizeof(attr_t)); |
| 856 | } |
| 857 | if (len > 0) { |
| 858 | int i; |
| 859 | std::memmove(dwin->chars + pos, buf, len * 4); |
| 860 | for (i = 0; i < len; i++) { |
| 861 | dwin->attrs[pos + i].set(style_Input); |
| 862 | } |
| 863 | } |
| 864 | dwin->numchars += diff; |
| 865 | |
| 866 | if (dwin->inbuf != nullptr) { |
| 867 | if (dwin->incurs >= pos + oldlen) { |
| 868 | dwin->incurs += diff; |
| 869 | } else if (dwin->incurs >= pos) { |
| 870 | dwin->incurs = pos + len; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | touch(dwin, 0); |
| 875 | } |
| 876 | |
| 877 | // Return true if a following quotation mark should be an opening mark, |
| 878 | // false if it should be a closing mark. Opening quotation marks will |
| 879 | // appear following an open parenthesis, open square bracket, or |
| 880 | // whitespace. |
| 881 | static bool leftquote(std::uint32_t c) |
| 882 | { |
| 883 | switch(c) { |
| 884 | case '(': case '[': |
| 885 | |
| 886 | // The following are Unicode characters in the "Separator, Space" category. |
| 887 | case 0x0020: case 0x00a0: case 0x1680: case 0x2000: |
| 888 | case 0x2001: case 0x2002: case 0x2003: case 0x2004: |
| 889 | case 0x2005: case 0x2006: case 0x2007: case 0x2008: |
| 890 | case 0x2009: case 0x200a: case 0x202f: case 0x205f: |
| 891 | case 0x3000: |
| 892 | return true; |
| 893 | default: |
| 894 | return false; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | void win_textbuffer_putchar_uni(window_t *win, glui32 ch) |
| 899 | { |
| 900 | window_textbuffer_t *dwin = win->winbuffer(); |
| 901 | std::array<glui32, TBLINELEN> bchars; |
| 902 | std::array<attr_t, TBLINELEN> battrs; |
| 903 | int pw; |
| 904 | int bpoint; |
| 905 | int saved; |
| 906 | int i; |
| 907 | int linelen; |
| 908 | |
| 909 | // Don't speak if the current text style is input, under the |
| 910 | // assumption that the interpreter is trying to display the user's |
| 911 | // input. This is how Bocfel uses style_Input, and without this |
| 912 | // test, extraneous input text is spoken. Other formats/interpreters |
| 913 | // don't have this issue, but since this affects all Z-machine |
| 914 | // games, it's probably worth the hacky solution here. If there are |
| 915 | // Glulx games which use input style for text that the user did not |
| 916 | // enter, that text will not get spoken. If that turns out to be a |
| 917 | // problem, a new Gargoyle-specific function will probably be needed |
| 918 | // that Bocfel can use to signal that it's writing input text from |
| 919 | // the user vs input text from elsewhere. |
| 920 | // |
| 921 | // Note that this already affects history playback in Bocfel: since |
| 922 | // it styles previous user input with style_Input during history |
| 923 | // playback, the user input won't be spoken. That's annoying but |
| 924 | // probably not quite as important as getting the expected behavior |
| 925 | // during normal gameplay. |
| 926 | // |
| 927 | // See https://github.com/garglk/garglk/issues/356 |
| 928 | if (win->attr.style != style_Input) { |
| 929 | gli_tts_speak(&ch, 1); |
| 930 | } |
| 931 | |
| 932 | pw = (win->bbox.x1 - win->bbox.x0 - gli_tmarginx * 2 - gli_scroll_width) * GLI_SUBPIX; |
| 933 | pw = pw - 2 * SLOP - dwin->radjw - dwin->ladjw; |
| 934 | |
| 935 | Color color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor; |
| 936 | |
| 937 | // oops ... overflow |
| 938 | if (dwin->numchars + 1 >= TBLINELEN) { |
| 939 | scrolloneline(dwin, false); |
| 940 | } |
| 941 | |
| 942 | if (ch == '\n') { |
| 943 | scrolloneline(dwin, true); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | if (gli_conf_quotes != 0) { |
| 948 | // fails for 'tis a wonderful day in the '80s |
| 949 | if (gli_conf_quotes == 2 && ch == '\'') { |
| 950 | if (dwin->numchars == 0 || leftquote(dwin->chars[dwin->numchars - 1])) { |
| 951 | ch = UNI_LSQUO; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (ch == '`') { |
| 956 | ch = UNI_LSQUO; |
| 957 | } |
| 958 | |
| 959 | if (ch == '\'') { |
| 960 | ch = UNI_RSQUO; |
| 961 | } |
| 962 | |
| 963 | if (ch == '"') { |
| 964 | if (dwin->numchars == 0 || leftquote(dwin->chars[dwin->numchars - 1])) { |
| 965 | ch = UNI_LDQUO; |
| 966 | } else { |
| 967 | ch = UNI_RDQUO; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // This tracks whether the font "should" be monospace, not whether |
| 973 | // the font file itself is actually monospace: if the font is monor, |
| 974 | // monob, monoi, or monoz, then this will be true, regardless of |
| 975 | // what font the user actually set as the monospace font. |
| 976 | bool monospace = gli_tstyles[win->attr.style].font.monospace; |
| 977 | |
| 978 | if (gli_conf_dashes != 0 && !monospace) { |
| 979 | if (ch == '-') { |
| 980 | dwin->dashed++; |
| 981 | if (dwin->dashed == 2) { |
| 982 | dwin->numchars--; |
| 983 | if (gli_conf_dashes == 2) { |
| 984 | ch = UNI_NDASH; |
| 985 | } else { |
| 986 | ch = UNI_MDASH; |
| 987 | } |
| 988 | } |
| 989 | if (dwin->dashed == 3) { |
| 990 | dwin->numchars--; |
| 991 | ch = UNI_MDASH; |
| 992 | dwin->dashed = 0; |
| 993 | } |
| 994 | } else { |
| 995 | dwin->dashed = 0; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (gli_conf_spaces != 0 && !monospace |
| 1000 | && dwin->styles[win->attr.style].bg == color |
| 1001 | && !dwin->styles[win->attr.style].reverse) { |
| 1002 | // turn (period space space) into (period space) |
| 1003 | if (gli_conf_spaces == 1) { |
| 1004 | if (ch == '.') { |
| 1005 | dwin->spaced = 1; |
| 1006 | } else if (ch == ' ' && dwin->spaced == 1) { |
| 1007 | dwin->spaced = 2; |
| 1008 | } else if (ch == ' ' && dwin->spaced == 2) { |
| 1009 | dwin->spaced = 0; |
| 1010 | return; |
| 1011 | } else { |
| 1012 | dwin->spaced = 0; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // turn (per sp x) into (per sp sp x) |
| 1017 | else if (gli_conf_spaces == 2) { |
| 1018 | if (ch == '.') { |
| 1019 | dwin->spaced = 1; |
| 1020 | } else if (ch == ' ' && dwin->spaced == 1) { |
| 1021 | dwin->spaced = 2; |
| 1022 | } else if (ch != ' ' && dwin->spaced == 2) { |
| 1023 | dwin->spaced = 0; |
| 1024 | win_textbuffer_putchar_uni(win, ' '); |
| 1025 | } else { |
| 1026 | dwin->spaced = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | dwin->chars[dwin->numchars] = ch; |
| 1032 | dwin->attrs[dwin->numchars] = win->attr; |
| 1033 | dwin->numchars++; |
| 1034 | |
| 1035 | // kill spaces at the end for line width calculation |
| 1036 | linelen = dwin->numchars; |
| 1037 | while (linelen > 1 && dwin->chars[linelen - 1] == ' ' |
| 1038 | && dwin->attrs[linelen - 1].bgcolor == color |
| 1039 | && !dwin->attrs[linelen - 1].reverse) { |
| 1040 | linelen--; |
| 1041 | } |
| 1042 | |
| 1043 | if (calcwidth(dwin, dwin->chars, dwin->attrs, 0, linelen, -1) >= pw) { |
| 1044 | bpoint = dwin->numchars; |
| 1045 | |
| 1046 | for (i = dwin->numchars - 1; i > 0; i--) { |
| 1047 | if (dwin->chars[i] == ' ') { |
| 1048 | bpoint = i + 1; // skip space |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | saved = dwin->numchars - bpoint; |
| 1054 | |
| 1055 | std::memcpy(bchars.data(), dwin->chars + bpoint, saved * 4); |
| 1056 | std::memcpy(battrs.data(), dwin->attrs + bpoint, saved * sizeof(attr_t)); |
| 1057 | dwin->numchars = bpoint; |
| 1058 | |
| 1059 | scrolloneline(dwin, false); |
| 1060 | |
| 1061 | std::memcpy(dwin->chars, bchars.data(), saved * 4); |
| 1062 | std::memcpy(dwin->attrs, battrs.data(), saved * sizeof(attr_t)); |
| 1063 | dwin->numchars = saved; |
| 1064 | } |
| 1065 | |
| 1066 | touch(dwin, 0); |
| 1067 | } |
| 1068 | |
| 1069 | bool win_textbuffer_unputchar_uni(window_t *win, glui32 ch) |
| 1070 | { |
| 1071 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1072 | if (dwin->numchars > 0 && glk_char_to_upper(dwin->chars[dwin->numchars - 1]) == glk_char_to_upper(ch)) { |
| 1073 | dwin->numchars--; |
| 1074 | touch(dwin, 0); |
| 1075 | return true; |
| 1076 | } |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
| 1080 | void win_textbuffer_clear(window_t *win) |
| 1081 | { |
| 1082 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1083 | int i; |
| 1084 | |
| 1085 | win->attr.fgcolor = gli_override_fg; |
| 1086 | win->attr.bgcolor = gli_override_bg; |
| 1087 | win->attr.reverse = false; |
| 1088 | |
| 1089 | dwin->ladjw = dwin->radjw = 0; |
| 1090 | dwin->ladjn = dwin->radjn = 0; |
| 1091 | |
| 1092 | dwin->spaced = 0; |
| 1093 | dwin->dashed = 0; |
| 1094 | |
| 1095 | dwin->numchars = 0; |
| 1096 | |
| 1097 | for (i = 0; i < dwin->scrollback; i++) { |
| 1098 | dwin->lines[i].len = 0; |
| 1099 | dwin->lines[i].flow_break_pos = -1; |
| 1100 | |
| 1101 | dwin->lines[i].lpic.reset(); |
| 1102 | dwin->lines[i].rpic.reset(); |
| 1103 | |
| 1104 | dwin->lines[i].lhyper = 0; |
| 1105 | dwin->lines[i].rhyper = 0; |
| 1106 | dwin->lines[i].lm = 0; |
| 1107 | dwin->lines[i].rm = 0; |
| 1108 | dwin->lines[i].newline = false; |
| 1109 | dwin->lines[i].dirty = true; |
| 1110 | dwin->lines[i].repaint = false; |
| 1111 | } |
| 1112 | |
| 1113 | dwin->lastseen = 0; |
| 1114 | dwin->scrollpos = 0; |
| 1115 | dwin->scrollmax = 0; |
| 1116 | |
| 1117 | for (i = 0; i < dwin->height; i++) { |
| 1118 | touch(dwin, i); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | // Prepare the window for line input. |
| 1123 | static void win_textbuffer_init_impl(window_t *win, void *buf, int maxlen, int initlen, bool unicode) |
| 1124 | { |
| 1125 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1126 | int pw; |
| 1127 | |
| 1128 | gli_tts_flush(); |
| 1129 | |
| 1130 | // because '>' prompt is ugly without extra space |
| 1131 | if (dwin->numchars != 0 && dwin->chars[dwin->numchars - 1] == '>') { |
| 1132 | win_textbuffer_putchar_uni(win, ' '); |
| 1133 | } |
| 1134 | if (dwin->numchars != 0 && dwin->chars[dwin->numchars - 1] == '?') { |
| 1135 | win_textbuffer_putchar_uni(win, ' '); |
| 1136 | } |
| 1137 | |
| 1138 | // make sure we have some space left for typing... |
| 1139 | pw = (win->bbox.x1 - win->bbox.x0 - gli_tmarginx * 2) * GLI_SUBPIX; |
| 1140 | pw = pw - 2 * SLOP - dwin->radjw + dwin->ladjw; |
| 1141 | if (calcwidth(dwin, dwin->chars, dwin->attrs, 0, dwin->numchars, -1) >= pw * 3 / 4) { |
| 1142 | win_textbuffer_putchar_uni(win, '\n'); |
| 1143 | } |
| 1144 | |
| 1145 | dwin->inbuf = buf; |
| 1146 | dwin->inunicode = unicode; |
| 1147 | dwin->inmax = maxlen; |
| 1148 | dwin->infence = dwin->numchars; |
| 1149 | dwin->incurs = dwin->numchars; |
| 1150 | dwin->origattr = win->attr; |
| 1151 | win->attr.set(style_Input); |
| 1152 | |
| 1153 | if (initlen != 0) { |
| 1154 | touch(dwin, 0); |
| 1155 | if (unicode) { |
| 1156 | put_text_uni(dwin, static_cast<glui32 *>(buf), initlen, dwin->incurs, 0); |
| 1157 | } else { |
| 1158 | put_text(dwin, static_cast<char *>(buf), initlen, dwin->incurs, 0); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (gli_register_arr != nullptr) { |
| 1163 | dwin->inarrayrock = (*gli_register_arr)(dwin->inbuf, maxlen, const_cast<char *>(unicode ? "&+#!Iu" : "&+#!Cn")); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | void win_textbuffer_init_line(window_t *win, char *buf, int maxlen, int initlen) |
| 1168 | { |
| 1169 | win_textbuffer_init_impl(win, buf, maxlen, initlen, false); |
| 1170 | } |
| 1171 | |
| 1172 | void win_textbuffer_init_line_uni(window_t *win, glui32 *buf, int maxlen, int initlen) |
| 1173 | { |
| 1174 | win_textbuffer_init_impl(win, buf, maxlen, initlen, true); |
| 1175 | } |
| 1176 | |
| 1177 | // Abort line input, storing whatever's been typed so far. |
| 1178 | void win_textbuffer_cancel_line(window_t *win, event_t *ev) |
| 1179 | { |
| 1180 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1181 | gidispatch_rock_t inarrayrock; |
| 1182 | int ix; |
| 1183 | int len; |
| 1184 | void *inbuf; |
| 1185 | int inmax; |
| 1186 | bool inunicode; |
| 1187 | |
| 1188 | if (dwin->inbuf == nullptr) { |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | inbuf = dwin->inbuf; |
| 1193 | inmax = dwin->inmax; |
| 1194 | inarrayrock = dwin->inarrayrock; |
| 1195 | inunicode = dwin->inunicode; |
| 1196 | |
| 1197 | len = dwin->numchars - dwin->infence; |
| 1198 | if (win->echostr != nullptr) { |
| 1199 | gli_stream_echo_line_uni(win->echostr, dwin->chars + dwin->infence, len); |
| 1200 | } |
| 1201 | |
| 1202 | if (len > inmax) { |
| 1203 | len = inmax; |
| 1204 | } |
| 1205 | |
| 1206 | if (!inunicode) { |
| 1207 | for (ix = 0; ix < len; ix++) { |
| 1208 | glui32 ch = dwin->chars[dwin->infence + ix]; |
| 1209 | if (ch > 0xff) { |
| 1210 | ch = '?'; |
| 1211 | } |
| 1212 | (static_cast<char *>(inbuf))[ix] = static_cast<char>(ch); |
| 1213 | } |
| 1214 | } else { |
| 1215 | for (ix = 0; ix < len; ix++) { |
| 1216 | (static_cast<glui32 *>(inbuf))[ix] = dwin->chars[dwin->infence + ix]; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | win->attr = dwin->origattr; |
| 1221 | |
| 1222 | ev->type = evtype_LineInput; |
| 1223 | ev->win = win; |
| 1224 | ev->val1 = len; |
| 1225 | ev->val2 = 0; |
| 1226 | |
| 1227 | win->line_request = false; |
| 1228 | win->line_request_uni = false; |
| 1229 | dwin->inbuf = nullptr; |
| 1230 | dwin->inmax = 0; |
| 1231 | |
| 1232 | if (win->echo_line_input) { |
| 1233 | win_textbuffer_putchar_uni(win, '\n'); |
| 1234 | } else { |
| 1235 | dwin->numchars = dwin->infence; |
| 1236 | touch(dwin, 0); |
| 1237 | } |
| 1238 | |
| 1239 | if (gli_unregister_arr != nullptr) { |
| 1240 | const char *typedesc = (inunicode ? "&+#!Iu" : "&+#!Cn"); |
| 1241 | (*gli_unregister_arr)(inbuf, inmax, const_cast<char *>(typedesc), inarrayrock); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | // Keybinding functions. |
| 1246 | |
| 1247 | // Any key, when text buffer is scrolled. |
| 1248 | bool gcmd_accept_scroll(window_t *win, glui32 arg) |
| 1249 | { |
| 1250 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1251 | int pageht = dwin->height - 2; // 1 for prompt, 1 for overlap |
| 1252 | bool startpos = dwin->scrollpos != 0; |
| 1253 | |
| 1254 | switch (arg) { |
| 1255 | case keycode_PageUp: |
| 1256 | dwin->scrollpos += pageht; |
| 1257 | break; |
| 1258 | case keycode_End: |
| 1259 | dwin->scrollpos = 0; |
| 1260 | break; |
| 1261 | case keycode_Up: |
| 1262 | dwin->scrollpos++; |
| 1263 | break; |
| 1264 | case keycode_Down: |
| 1265 | case keycode_Return: |
| 1266 | dwin->scrollpos--; |
| 1267 | break; |
| 1268 | case keycode_MouseWheelUp: |
| 1269 | dwin->scrollpos += 3; |
| 1270 | startpos = true; |
| 1271 | break; |
| 1272 | case keycode_MouseWheelDown: |
| 1273 | dwin->scrollpos -= 3; |
| 1274 | startpos = true; |
| 1275 | break; |
| 1276 | case ' ': |
| 1277 | case keycode_PageDown: |
| 1278 | if (pageht != 0) { |
| 1279 | dwin->scrollpos -= pageht; |
| 1280 | } else { |
| 1281 | dwin->scrollpos = 0; |
| 1282 | } |
| 1283 | break; |
| 1284 | } |
| 1285 | |
| 1286 | if (dwin->scrollpos > dwin->scrollmax - dwin->height + 1) { |
| 1287 | dwin->scrollpos = dwin->scrollmax - dwin->height + 1; |
| 1288 | } |
| 1289 | if (dwin->scrollpos < 0) { |
| 1290 | dwin->scrollpos = 0; |
| 1291 | } |
| 1292 | touchscroll(dwin); |
| 1293 | |
| 1294 | return startpos || dwin->scrollpos != 0; |
| 1295 | } |
| 1296 | |
| 1297 | // Any key, during character input. Ends character input. |
| 1298 | void gcmd_buffer_accept_readchar(window_t *win, glui32 arg) |
| 1299 | { |
| 1300 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1301 | glui32 key; |
| 1302 | |
| 1303 | if (dwin->height < 2) { |
| 1304 | dwin->scrollpos = 0; |
| 1305 | } |
| 1306 | |
| 1307 | if (dwin->scrollpos != 0 |
| 1308 | || arg == keycode_PageUp |
| 1309 | || arg == keycode_MouseWheelUp) { |
| 1310 | gcmd_accept_scroll(win, arg); |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | switch (arg) { |
| 1315 | case keycode_Erase: |
| 1316 | key = keycode_Delete; |
| 1317 | break; |
| 1318 | case keycode_MouseWheelUp: |
| 1319 | case keycode_MouseWheelDown: |
| 1320 | return; |
| 1321 | default: |
| 1322 | key = arg; |
| 1323 | } |
| 1324 | |
| 1325 | gli_tts_purge(); |
| 1326 | |
| 1327 | if (key > 0xff && key < (0xffffffff - keycode_MAXVAL + 1)) { |
| 1328 | if (!(win->char_request_uni) || key > 0x10ffff) { |
| 1329 | key = keycode_Unknown; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | win->char_request = false; |
| 1334 | win->char_request_uni = false; |
| 1335 | gli_event_store(evtype_CharInput, win, key, 0); |
| 1336 | } |
| 1337 | |
| 1338 | // Return or enter, during line input. Ends line input. |
| 1339 | static void acceptline(window_t *win, glui32 keycode) |
| 1340 | { |
| 1341 | int ix; |
| 1342 | int len; |
| 1343 | void *inbuf; |
| 1344 | int inmax; |
| 1345 | bool inunicode; |
| 1346 | gidispatch_rock_t inarrayrock; |
| 1347 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1348 | |
| 1349 | if (dwin->inbuf == nullptr) { |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | inbuf = dwin->inbuf; |
| 1354 | inmax = dwin->inmax; |
| 1355 | inarrayrock = dwin->inarrayrock; |
| 1356 | inunicode = dwin->inunicode; |
| 1357 | |
| 1358 | len = dwin->numchars - dwin->infence; |
| 1359 | if (win->echostr != nullptr) { |
| 1360 | gli_stream_echo_line_uni(win->echostr, dwin->chars + dwin->infence, len); |
| 1361 | } |
| 1362 | |
| 1363 | gli_tts_purge(); |
| 1364 | if (gli_conf_speak_input) { |
| 1365 | gli_tts_speak(dwin->chars + dwin->infence, len); |
| 1366 | std::array<glui32, 1> newline = {'\n'}; |
| 1367 | gli_tts_speak(newline.data(), 1); |
| 1368 | } |
| 1369 | |
| 1370 | // Store in history. |
| 1371 | // A history entry should not repeat the string from the entry before it. |
| 1372 | if (len != 0) { |
| 1373 | // If the iterator's not at the beginning, that means the user is in the |
| 1374 | // middle of a history cycle. If that's the case, the first history |
| 1375 | // entry is the currently-typed text, which is no longer relevant. Drop it. |
| 1376 | if (dwin->history_it != dwin->history.begin()) { |
| 1377 | dwin->history.pop_front(); |
| 1378 | } |
| 1379 | |
| 1380 | std::vector<glui32> line(&dwin->chars[dwin->infence], &dwin->chars[dwin->numchars]); |
| 1381 | if (dwin->history.empty() || dwin->history.front() != line) { |
| 1382 | dwin->history.push_front(line); |
| 1383 | } |
| 1384 | |
| 1385 | while (dwin->history.size() > HISTORYLEN) { |
| 1386 | dwin->history.pop_back(); |
| 1387 | } |
| 1388 | |
| 1389 | dwin->history_it = dwin->history.begin(); |
| 1390 | } |
| 1391 | |
| 1392 | // Store in event buffer. |
| 1393 | |
| 1394 | if (len > inmax) { |
| 1395 | len = inmax; |
| 1396 | } |
| 1397 | |
| 1398 | if (!inunicode) { |
| 1399 | for (ix = 0; ix < len; ix++) { |
| 1400 | glui32 ch = dwin->chars[dwin->infence + ix]; |
| 1401 | if (ch > 0xff) { |
| 1402 | ch = '?'; |
| 1403 | } |
| 1404 | (static_cast<char *>(inbuf))[ix] = static_cast<char>(ch); |
| 1405 | } |
| 1406 | } else { |
| 1407 | for (ix = 0; ix < len; ix++) { |
| 1408 | (static_cast<glui32 *>(inbuf))[ix] = dwin->chars[dwin->infence + ix]; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | win->attr = dwin->origattr; |
| 1413 | |
| 1414 | if (!win->line_terminators.empty()) { |
| 1415 | glui32 val2 = keycode; |
| 1416 | if (val2 == keycode_Return) { |
| 1417 | val2 = 0; |
| 1418 | } |
| 1419 | gli_event_store(evtype_LineInput, win, len, val2); |
| 1420 | } else { |
| 1421 | gli_event_store(evtype_LineInput, win, len, 0); |
| 1422 | } |
| 1423 | win->line_request = false; |
| 1424 | win->line_request_uni = false; |
| 1425 | dwin->inbuf = nullptr; |
| 1426 | dwin->inmax = 0; |
| 1427 | |
| 1428 | if (win->echo_line_input) { |
| 1429 | win_textbuffer_putchar_uni(win, '\n'); |
| 1430 | } else { |
| 1431 | dwin->numchars = dwin->infence; |
| 1432 | touch(dwin, 0); |
| 1433 | } |
| 1434 | |
| 1435 | if (gli_unregister_arr != nullptr) { |
| 1436 | const char *typedesc = (inunicode ? "&+#!Iu" : "&+#!Cn"); |
| 1437 | (*gli_unregister_arr)(inbuf, inmax, const_cast<char *>(typedesc), inarrayrock); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | // Any key, during line input. |
| 1442 | void gcmd_buffer_accept_readline(window_t *win, glui32 arg) |
| 1443 | { |
| 1444 | window_textbuffer_t *dwin = win->winbuffer(); |
| 1445 | |
| 1446 | if (dwin->height < 2) { |
| 1447 | dwin->scrollpos = 0; |
| 1448 | } |
| 1449 | |
| 1450 | if (dwin->scrollpos != 0 |
| 1451 | || arg == keycode_PageUp |
| 1452 | || arg == keycode_MouseWheelUp) { |
| 1453 | gcmd_accept_scroll(win, arg); |
| 1454 | return; |
| 1455 | } |
| 1456 | |
| 1457 | if (dwin->inbuf == nullptr) { |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | if (!win->line_terminators.empty() && gli_window_check_terminator(arg)) { |
| 1462 | if (std::find(win->line_terminators.begin(), win->line_terminators.end(), arg) != win->line_terminators.end()) { |
| 1463 | acceptline(win, arg); |
| 1464 | return; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | switch (arg) { |
| 1469 | |
| 1470 | // History keys (up and down) |
| 1471 | |
| 1472 | case keycode_Up: |
| 1473 | // There is no stored history, so do nothing. |
| 1474 | if (dwin->history.empty()) { |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | // There is stored history, and this is the start of a cycle through |
| 1479 | // it. Store the currently-typed text (which may be empty) at the |
| 1480 | // front of the history buffer, and point the iterator there. |
| 1481 | if (dwin->history_it == dwin->history.begin()) { |
| 1482 | dwin->history.emplace_front(&dwin->chars[dwin->infence], &dwin->chars[dwin->numchars]); |
| 1483 | dwin->history_it = dwin->history.begin(); |
| 1484 | } |
| 1485 | |
| 1486 | // The iterator is on the current history entry, so load the |
| 1487 | // previous (older) one, if any (if not, that means this is the end |
| 1488 | // of history, so do nothing). |
| 1489 | if (dwin->history_it + 1 != dwin->history.end()) { |
| 1490 | ++dwin->history_it; |
| 1491 | put_text_uni(dwin, dwin->history_it->data(), dwin->history_it->size(), dwin->infence, dwin->numchars - dwin->infence); |
| 1492 | } |
| 1493 | break; |
| 1494 | |
| 1495 | case keycode_Down: |
| 1496 | // Already at the beginning (i.e. not actively cycling through |
| 1497 | // history), so do nothing. |
| 1498 | if (dwin->history_it == dwin->history.begin()) { |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | // Load the next (newer) history entry. |
| 1503 | --dwin->history_it; |
| 1504 | put_text_uni(dwin, dwin->history_it->data(), dwin->history_it->size(), dwin->infence, dwin->numchars - dwin->infence); |
| 1505 | |
| 1506 | // If we're at the beginning now, we're done cycling, and have |
| 1507 | // reloaded the user's currently-typed text. Since cycling is over, |
| 1508 | // drop the text from the history and repoint the iterator to the |
| 1509 | // previous history entry. |
| 1510 | if (dwin->history_it == dwin->history.begin()) { |
| 1511 | dwin->history.pop_front(); |
| 1512 | dwin->history_it = dwin->history.begin(); |
| 1513 | } |
| 1514 | break; |
| 1515 | |
| 1516 | // Cursor movement keys, during line input. |
| 1517 | |
| 1518 | case keycode_Left: |
| 1519 | if (dwin->incurs <= dwin->infence) { |
| 1520 | return; |
| 1521 | } |
| 1522 | dwin->incurs--; |
| 1523 | break; |
| 1524 | |
| 1525 | case keycode_Right: |
| 1526 | if (dwin->incurs >= dwin->numchars) { |
| 1527 | return; |
| 1528 | } |
| 1529 | dwin->incurs++; |
| 1530 | break; |
| 1531 | |
| 1532 | case keycode_Home: |
| 1533 | if (dwin->incurs <= dwin->infence) { |
| 1534 | return; |
| 1535 | } |
| 1536 | dwin->incurs = dwin->infence; |
| 1537 | break; |
| 1538 | |
| 1539 | case keycode_End: |
| 1540 | if (dwin->incurs >= dwin->numchars) { |
| 1541 | return; |
| 1542 | } |
| 1543 | dwin->incurs = dwin->numchars; |
| 1544 | break; |
| 1545 | |
| 1546 | case keycode_SkipWordLeft: |
| 1547 | while (dwin->incurs > dwin->infence && dwin->chars[dwin->incurs - 1] == ' ') { |
| 1548 | dwin->incurs--; |
| 1549 | } |
| 1550 | while (dwin->incurs > dwin->infence && dwin->chars[dwin->incurs - 1] != ' ') { |
| 1551 | dwin->incurs--; |
| 1552 | } |
| 1553 | break; |
| 1554 | |
| 1555 | case keycode_SkipWordRight: |
| 1556 | while (dwin->incurs < dwin->numchars && dwin->chars[dwin->incurs] != ' ') { |
| 1557 | dwin->incurs++; |
| 1558 | } |
| 1559 | while (dwin->incurs < dwin->numchars && dwin->chars[dwin->incurs] == ' ') { |
| 1560 | dwin->incurs++; |
| 1561 | } |
| 1562 | break; |
| 1563 | |
| 1564 | // Delete keys, during line input. |
| 1565 | |
| 1566 | case keycode_Delete: |
| 1567 | if (dwin->incurs <= dwin->infence) { |
| 1568 | return; |
| 1569 | } |
| 1570 | put_text_uni(dwin, nullptr, 0, dwin->incurs - 1, 1); |
| 1571 | break; |
| 1572 | |
| 1573 | case keycode_Erase: |
| 1574 | if (dwin->incurs >= dwin->numchars) { |
| 1575 | return; |
| 1576 | } |
| 1577 | put_text_uni(dwin, nullptr, 0, dwin->incurs, 1); |
| 1578 | break; |
| 1579 | |
| 1580 | case keycode_Escape: |
| 1581 | if (dwin->infence >= dwin->numchars) { |
| 1582 | return; |
| 1583 | } |
| 1584 | put_text_uni(dwin, nullptr, 0, dwin->infence, dwin->numchars - dwin->infence); |
| 1585 | break; |
| 1586 | |
| 1587 | // Regular keys |
| 1588 | |
| 1589 | case keycode_Return: |
| 1590 | acceptline(win, arg); |
| 1591 | break; |
| 1592 | |
| 1593 | default: |
| 1594 | if (arg >= 32 && arg <= 0x10FFFF) { |
| 1595 | if (gli_conf_caps && (arg > 0x60 && arg < 0x7b)) { |
| 1596 | arg -= 0x20; |
| 1597 | } |
| 1598 | put_text_uni(dwin, &arg, 1, dwin->incurs, 0); |
| 1599 | } |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | touch(dwin, 0); |
| 1604 | } |
| 1605 | |
| 1606 | static bool put_picture(window_textbuffer_t *dwin, const std::shared_ptr<picture_t> &pic, glui32 align, glui32 linkval) |
| 1607 | { |
| 1608 | if (align == imagealign_MarginRight) { |
| 1609 | if (dwin->lines[0].rpic || dwin->numchars != 0) { |
| 1610 | return false; |
| 1611 | } |
| 1612 | |
| 1613 | dwin->radjw = (pic->w + gli_tmarginx) * GLI_SUBPIX; |
| 1614 | dwin->radjn = (pic->h + gli_cellh - 1) / gli_cellh; |
| 1615 | auto &line = dwin->lines[0]; |
| 1616 | line.rpic = pic; |
| 1617 | line.rm = dwin->radjw; |
| 1618 | line.rhyper = linkval; |
| 1619 | line.flow_break_pos = -1; |
| 1620 | } |
| 1621 | |
| 1622 | else { |
| 1623 | if (align != imagealign_MarginLeft && dwin->numchars != 0) { |
| 1624 | win_textbuffer_putchar_uni(dwin->owner, '\n'); |
| 1625 | } |
| 1626 | |
| 1627 | if (dwin->lines[0].lpic || dwin->numchars != 0) { |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
| 1631 | dwin->ladjw = (pic->w + gli_tmarginx) * GLI_SUBPIX; |
| 1632 | dwin->ladjn = (pic->h + gli_cellh - 1) / gli_cellh; |
| 1633 | auto &line = dwin->lines[0]; |
| 1634 | line.lpic = pic; |
| 1635 | line.lm = dwin->ladjw; |
| 1636 | line.lhyper = linkval; |
| 1637 | line.flow_break_pos = -1; |
| 1638 | |
| 1639 | if (align != imagealign_MarginLeft) { |
| 1640 | win_textbuffer_flow_break(dwin); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
| 1647 | bool win_textbuffer_draw_picture(std::shared_ptr<picture_t> pic, window_textbuffer_t *dwin, glui32 window_width, |
| 1648 | glui32 align, glui32 width, glui32 height, glui32 maxwidth) |
| 1649 | { |
| 1650 | glui32 hyperlink; |
| 1651 | |
| 1652 | if (maxwidth != 0) { |
| 1653 | auto limit = window_width * (maxwidth / 65536.0); |
| 1654 | if (width > limit) { |
| 1655 | double scaleby = static_cast<double>(limit) / width; |
| 1656 | width *= scaleby; |
| 1657 | height *= scaleby; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | pic = gli_picture_scale(pic.get(), gli_zoom_int(width), gli_zoom_int(height)); |
| 1662 | |
| 1663 | hyperlink = dwin->owner->attr.hyper; |
| 1664 | |
| 1665 | return put_picture(dwin, pic, align, hyperlink); |
| 1666 | } |
| 1667 | |
| 1668 | void win_textbuffer_flow_break(window_textbuffer_t *dwin) |
| 1669 | { |
| 1670 | scrolloneline(dwin, false, true); |
| 1671 | } |
| 1672 | |
| 1673 | void win_textbuffer_click(window_textbuffer_t *dwin, int sx, int sy) |
| 1674 | { |
| 1675 | window_t *win = dwin->owner; |
| 1676 | bool gh = false; |
| 1677 | bool gs = false; |
| 1678 | |
| 1679 | if (win->line_request || win->char_request |
| 1680 | || win->line_request_uni || win->char_request_uni |
| 1681 | || win->more_request || win->scroll_request) { |
| 1682 | gli_focuswin = win; |
| 1683 | } |
| 1684 | |
| 1685 | if (win->hyper_request) { |
| 1686 | glui32 linkval = gli_get_hyperlink(sx, sy); |
| 1687 | if (linkval != 0) { |
| 1688 | gli_event_store(evtype_Hyperlink, win, linkval, 0); |
| 1689 | win->hyper_request = false; |
| 1690 | if (gli_conf_safeclicks) { |
| 1691 | gli_forceclick = true; |
| 1692 | } |
| 1693 | gh = true; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | if (sx > win->bbox.x1 - gli_scroll_width) { |
| 1698 | if (sy < win->bbox.y0 + gli_tmarginy + gli_scroll_width) { |
| 1699 | gcmd_accept_scroll(win, keycode_Up); |
| 1700 | } else if (sy > win->bbox.y1 - gli_tmarginy - gli_scroll_width) { |
| 1701 | gcmd_accept_scroll(win, keycode_Down); |
| 1702 | } else if (sy < (win->bbox.y0 + win->bbox.y1) / 2) { |
| 1703 | gcmd_accept_scroll(win, keycode_PageUp); |
| 1704 | } else { |
| 1705 | gcmd_accept_scroll(win, keycode_PageDown); |
| 1706 | } |
| 1707 | gs = true; |
| 1708 | } |
| 1709 | |
| 1710 | if (!gh && !gs) { |
| 1711 | gli_copyselect = true; |
| 1712 | gli_start_selection(sx, sy); |
| 1713 | } |
| 1714 | } |