comparison mupdf-source/platform/gl/gl-input.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2022 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 #include "gl-app.h"
24
25 #include <string.h>
26 #include <stdio.h>
27
28 static char *find_string_location(char *s, char *e, float w, float x)
29 {
30 int c;
31 while (s < e)
32 {
33 int n = fz_chartorune(&c, s);
34 float cw = ui_measure_character(c);
35 if (w + (cw / 2) >= x)
36 return s;
37 w += cw;
38 s += n;
39 }
40 return e;
41 }
42
43 static char *find_input_location(struct line *lines, int n, float left, float top, float x, float y)
44 {
45 int i = 0;
46 if (y > top) i = (y - top) / ui.lineheight;
47 if (i >= n) i = n - 1;
48 return find_string_location(lines[i].a, lines[i].b, left, x);
49 }
50
51 static inline int myisalnum(char *s)
52 {
53 int cat, c;
54 fz_chartorune(&c, s);
55 cat = ucdn_get_general_category(c);
56 if (cat >= UCDN_GENERAL_CATEGORY_LL && cat <= UCDN_GENERAL_CATEGORY_LU)
57 return 1;
58 if (cat >= UCDN_GENERAL_CATEGORY_ND && cat <= UCDN_GENERAL_CATEGORY_NO)
59 return 1;
60 return 0;
61 }
62
63 static char *home_line(char *p, char *start)
64 {
65 while (p > start)
66 {
67 if (p[-1] == '\n' || p[-1] == '\r')
68 return p;
69 --p;
70 }
71 return p;
72 }
73
74 static char *end_line(char *p, char *end)
75 {
76 while (p < end)
77 {
78 if (p[0] == '\n' || p[0] == '\r')
79 return p;
80 ++p;
81 }
82 return p;
83 }
84
85 static char *up_line(char *p, char *start)
86 {
87 while (p > start)
88 {
89 --p;
90 if (*p == '\n' || *p == '\r')
91 return p;
92 }
93 return p;
94 }
95
96 static char *down_line(char *p, char *end)
97 {
98 while (p < end)
99 {
100 if (*p == '\n' || *p == '\r')
101 return p+1;
102 ++p;
103 }
104 return p;
105 }
106
107 static char *prev_char(char *p, char *start)
108 {
109 --p;
110 while ((*p & 0xC0) == 0x80 && p > start) /* skip middle and final multibytes */
111 --p;
112 return p;
113 }
114
115 static char *next_char(char *p)
116 {
117 ++p;
118 while ((*p & 0xC0) == 0x80) /* skip middle and final multibytes */
119 ++p;
120 return p;
121 }
122
123 static char *prev_word(char *p, char *start)
124 {
125 while (p > start && !myisalnum(prev_char(p, start))) p = prev_char(p, start);
126 while (p > start && myisalnum(prev_char(p, start))) p = prev_char(p, start);
127 return p;
128 }
129
130 static char *next_word(char *p, char *end)
131 {
132 while (p < end && !myisalnum(p)) p = next_char(p);
133 while (p < end && myisalnum(p)) p = next_char(p);
134 return p;
135 }
136
137 static void ui_input_delete_selection(struct input *input)
138 {
139 char *p = input->p < input->q ? input->p : input->q;
140 char *q = input->p > input->q ? input->p : input->q;
141 memmove(p, q, input->end - q);
142 input->end -= q - p;
143 *input->end = 0;
144 input->p = input->q = p;
145 }
146
147 static void ui_input_paste(struct input *input, const char *buf)
148 {
149 int n = (int)strlen(buf);
150 if (input->widget)
151 {
152 char *newtext;
153 int selStart = input->p - input->text;
154 int selEnd = input->q - input->text;
155 if (pdf_edit_text_field_value(ctx, input->widget, input->text, buf, &selStart, &selEnd, &newtext))
156 {
157 size_t len = strlen(newtext);
158 if (len > sizeof(input->text)-1)
159 len = sizeof(input->text)-1;
160 memcpy(input->text, newtext, len);
161 input->text[len] = 0;
162 fz_free(ctx, newtext);
163 input->p = input->text + selStart;
164 input->q = input->p;
165 input->end = input->text + len;
166 }
167 return;
168 }
169 if (input->p != input->q)
170 ui_input_delete_selection(input);
171 if (input->end + n + 1 < input->text + sizeof(input->text))
172 {
173 memmove(input->p + n, input->p, input->end - input->p);
174 memmove(input->p, buf, n);
175 input->p += n;
176 input->end += n;
177 *input->end = 0;
178 }
179 input->q = input->p;
180 }
181
182 static void ui_do_copy(struct input *input)
183 {
184 if (input->p != input->q)
185 {
186 char buf[sizeof input->text];
187 char *p = input->p < input->q ? input->p : input->q;
188 char *q = input->p > input->q ? input->p : input->q;
189 memmove(buf, p, q - p);
190 buf[q-p] = 0;
191 ui_set_clipboard(buf);
192 }
193 }
194
195 static void ui_do_cut(struct input *input)
196 {
197 if (input->p != input->q)
198 {
199 ui_do_copy(input);
200 ui_input_delete_selection(input);
201 }
202 }
203
204 static void ui_do_paste(struct input *input, int multiline)
205 {
206 const char *buf = ui_get_clipboard();
207 char *p, *oneline = NULL;
208
209 if (!buf)
210 return;
211
212 if (multiline)
213 {
214 ui_input_paste(input, buf);
215 return;
216 }
217
218 p = oneline = strdup(buf);
219 while (*p)
220 {
221 if (*p == '\n' || *p == '\r')
222 *p = ' ';
223 p++;
224 }
225 ui_input_paste(input, oneline);
226 free(oneline);
227 }
228
229 static int ui_input_key(struct input *input, int multiline)
230 {
231 switch (ui.key)
232 {
233 case 0:
234 return UI_INPUT_NONE;
235 case KEY_LEFT:
236 if (ui.mod == GLUT_ACTIVE_CTRL + GLUT_ACTIVE_SHIFT)
237 {
238 input->q = prev_word(input->q, input->text);
239 }
240 else if (ui.mod == GLUT_ACTIVE_CTRL)
241 {
242 if (input->p != input->q)
243 input->p = input->q = input->p < input->q ? input->p : input->q;
244 else
245 input->p = input->q = prev_word(input->q, input->text);
246 }
247 else if (ui.mod == GLUT_ACTIVE_SHIFT)
248 {
249 if (input->q > input->text)
250 input->q = prev_char(input->q, input->text);
251 }
252 else if (ui.mod == 0)
253 {
254 if (input->p != input->q)
255 input->p = input->q = input->p < input->q ? input->p : input->q;
256 else if (input->q > input->text)
257 input->p = input->q = prev_char(input->q, input->text);
258 }
259 break;
260 case KEY_RIGHT:
261 if (ui.mod == GLUT_ACTIVE_CTRL + GLUT_ACTIVE_SHIFT)
262 {
263 input->q = next_word(input->q, input->end);
264 }
265 else if (ui.mod == GLUT_ACTIVE_CTRL)
266 {
267 if (input->p != input->q)
268 input->p = input->q = input->p > input->q ? input->p : input->q;
269 else
270 input->p = input->q = next_word(input->q, input->end);
271 }
272 else if (ui.mod == GLUT_ACTIVE_SHIFT)
273 {
274 if (input->q < input->end)
275 input->q = next_char(input->q);
276 }
277 else if (ui.mod == 0)
278 {
279 if (input->p != input->q)
280 input->p = input->q = input->p > input->q ? input->p : input->q;
281 else if (input->q < input->end)
282 input->p = input->q = next_char(input->q);
283 }
284 break;
285 case KEY_UP:
286 if (ui.mod & GLUT_ACTIVE_SHIFT)
287 input->q = up_line(input->q, input->text);
288 else
289 input->p = input->q = up_line(input->p, input->text);
290 break;
291 case KEY_DOWN:
292 if (ui.mod & GLUT_ACTIVE_SHIFT)
293 input->q = down_line(input->q, input->end);
294 else
295 input->p = input->q = down_line(input->q, input->end);
296 break;
297 case KEY_HOME:
298 if (ui.mod == GLUT_ACTIVE_CTRL + GLUT_ACTIVE_SHIFT)
299 input->q = input->text;
300 else if (ui.mod == GLUT_ACTIVE_SHIFT)
301 input->q = home_line(input->q, input->text);
302 else if (ui.mod == GLUT_ACTIVE_CTRL)
303 input->p = input->q = input->text;
304 else if (ui.mod == 0)
305 input->p = input->q = home_line(input->p, input->text);
306 break;
307 case KEY_END:
308 if (ui.mod == GLUT_ACTIVE_CTRL + GLUT_ACTIVE_SHIFT)
309 input->q = input->end;
310 else if (ui.mod == GLUT_ACTIVE_SHIFT)
311 input->q = end_line(input->q, input->end);
312 else if (ui.mod == GLUT_ACTIVE_CTRL)
313 input->p = input->q = input->end;
314 else if (ui.mod == 0)
315 input->p = input->q = end_line(input->p, input->end);
316 break;
317 case KEY_DELETE:
318 if (ui.mod == GLUT_ACTIVE_SHIFT)
319 {
320 ui_do_cut(input);
321 }
322 else if (input->p != input->q)
323 {
324 ui_input_delete_selection(input);
325 }
326 else if (input->p < input->end)
327 {
328 char *np = next_char(input->p);
329 memmove(input->p, np, input->end - np);
330 input->end -= np - input->p;
331 *input->end = 0;
332 input->q = input->p;
333 }
334 break;
335 case KEY_ESCAPE:
336 ui.focus = NULL;
337 return UI_INPUT_NONE;
338 case KEY_ENTER:
339 if (!multiline)
340 {
341 ui.focus = NULL;
342 return UI_INPUT_ACCEPT;
343 }
344 ui_input_paste(input, "\n");
345 break;
346 case KEY_BACKSPACE:
347 if (input->p != input->q)
348 ui_input_delete_selection(input);
349 else if (input->p > input->text)
350 {
351 char *pp;
352 if (ui.mod == GLUT_ACTIVE_CTRL)
353 pp = prev_word(input->p, input->text);
354 else
355 pp = prev_char(input->p, input->text);
356 memmove(pp, input->p, input->end - input->p);
357 input->end -= input->p - pp;
358 *input->end = 0;
359 input->q = input->p = pp;
360 }
361 break;
362 case KEY_CTL_A:
363 input->p = input->q = input->text;
364 break;
365 case KEY_CTL_E:
366 input->p = input->q = input->end;
367 break;
368 case KEY_CTL_W:
369 if (input->p != input->q)
370 ui_input_delete_selection(input);
371 else
372 {
373 input->p = prev_word(input->p, input->text);
374 ui_input_delete_selection(input);
375 }
376 break;
377 case KEY_CTL_U:
378 input->p = input->q = input->end = input->text;
379 *input->end = 0;
380 break;
381 case KEY_CTL_C:
382 ui_do_copy(input);
383 break;
384 case KEY_CTL_X:
385 ui_do_cut(input);
386 break;
387 case KEY_CTL_V:
388 ui_do_paste(input, multiline);
389 break;
390 case KEY_INSERT:
391 if (ui.mod == GLUT_ACTIVE_CTRL)
392 ui_do_copy(input);
393 if (ui.mod == GLUT_ACTIVE_SHIFT)
394 ui_do_paste(input, multiline);
395 break;
396 default:
397 if (ui.key >= 32 && ui.plain)
398 {
399 int cat = ucdn_get_general_category(ui.key);
400 if (ui.key == ' ' || (cat >= UCDN_GENERAL_CATEGORY_LL && cat < UCDN_GENERAL_CATEGORY_ZL))
401 {
402 char buf[9];
403 int n = fz_runetochar(buf, ui.key);
404 buf[n] = 0;
405 ui_input_paste(input, buf);
406 }
407 }
408 break;
409 }
410 return UI_INPUT_EDIT;
411 }
412
413 void ui_input_init(struct input *input, const char *text)
414 {
415 fz_strlcpy(input->text, text, sizeof input->text);
416 input->end = input->text + strlen(input->text);
417 input->p = input->text;
418 input->q = input->end;
419 input->scroll = 0;
420 }
421
422 int ui_input(struct input *input, int width, int height)
423 {
424 struct line lines[500];
425 fz_irect area;
426 float ax, bx;
427 int ay, sy;
428 char *p, *q;
429 int state;
430 int i, n;
431
432 if (ui.focus == input)
433 state = ui_input_key(input, height > 1);
434 else
435 state = UI_INPUT_NONE;
436
437 area = ui_pack(width, ui.lineheight * height + 6);
438 ui_draw_bevel_rect(area, UI_COLOR_TEXT_BG, 1);
439 area = fz_expand_irect(area, -2);
440
441 if (height > 1)
442 area.x1 -= ui.lineheight;
443
444 n = ui_break_lines(input->text, lines, height > 1 ? nelem(lines) : 1, area.x1-area.x0-2, NULL);
445
446 if (height > 1)
447 ui_scrollbar(area.x1, area.y0, area.x1+ui.lineheight, area.y1, &input->scroll, 1, fz_maxi(0, n-height)+1, NULL);
448 else
449 input->scroll = 0;
450
451 ax = area.x0 + 2;
452 bx = area.x1 - 2;
453 ay = area.y0 + 1;
454 sy = input->scroll * ui.lineheight;
455
456 if (ui_mouse_inside(area))
457 {
458 ui.hot = input;
459 if (!ui.active || ui.active == input)
460 ui.cursor = GLUT_CURSOR_TEXT;
461 if (!ui.active && ui.down)
462 {
463 input->p = find_input_location(lines, n, ax, ay-sy, ui.x, ui.y);
464 ui.active = input;
465 }
466 }
467
468 if (ui.active == input)
469 {
470 input->q = find_input_location(lines, n, ax, ay-sy, ui.x, ui.y);
471 ui.focus = input;
472 }
473
474 p = input->p < input->q ? input->p : input->q;
475 q = input->p > input->q ? input->p : input->q;
476
477 for (i = input->scroll; i < n && i < input->scroll+height; ++i)
478 {
479 char *a = lines[i].a, *b = lines[i].b;
480 if (ui.focus == input)
481 {
482 if (p >= a && p <= b && q >= a && q <= b)
483 {
484 float px = ax + ui_measure_string_part(a, p);
485 float qx = px + ui_measure_string_part(p, q);
486 glColorHex(UI_COLOR_TEXT_SEL_BG);
487 glRectf(px, ay, qx+1, ay + ui.lineheight);
488 glColorHex(UI_COLOR_TEXT_FG);
489 ui_draw_string_part(ax, ay, a, p);
490 glColorHex(UI_COLOR_TEXT_SEL_FG);
491 ui_draw_string_part(px, ay, p, q);
492 glColorHex(UI_COLOR_TEXT_FG);
493 ui_draw_string_part(qx, ay, q, b);
494 }
495 else if (p < a && q >= a && q <= b)
496 {
497 float qx = ax + ui_measure_string_part(a, q);
498 glColorHex(UI_COLOR_TEXT_SEL_BG);
499 glRectf(ax, ay, qx+1, ay + ui.lineheight);
500 glColorHex(UI_COLOR_TEXT_SEL_FG);
501 ui_draw_string_part(ax, ay, a, q);
502 glColorHex(UI_COLOR_TEXT_FG);
503 ui_draw_string_part(qx, ay, q, b);
504 }
505 else if (p >= a && p <= b && q > b)
506 {
507 float px = ax + ui_measure_string_part(a, p);
508 glColorHex(UI_COLOR_TEXT_SEL_BG);
509 glRectf(px, ay, bx, ay + ui.lineheight);
510 glColorHex(UI_COLOR_TEXT_FG);
511 ui_draw_string_part(ax, ay, a, p);
512 glColorHex(UI_COLOR_TEXT_SEL_FG);
513 ui_draw_string_part(px, ay, p, b);
514 }
515 else if (p < a && q > b)
516 {
517 glColorHex(UI_COLOR_TEXT_SEL_BG);
518 glRectf(ax, ay, bx, ay + ui.lineheight);
519 glColorHex(UI_COLOR_TEXT_SEL_FG);
520 ui_draw_string_part(ax, ay, a, b);
521 }
522 else
523 {
524 glColorHex(UI_COLOR_TEXT_FG);
525 ui_draw_string_part(ax, ay, a, b);
526 }
527 }
528 else
529 {
530 glColorHex(UI_COLOR_TEXT_FG);
531 ui_draw_string_part(ax, ay, a, b);
532 }
533 ay += ui.lineheight;
534 }
535
536 return state;
537 }
538
539 void ui_readline_init(struct readline *readline, const char *text)
540 {
541 int i;
542
543 memset(readline->buffer, 0, sizeof readline->buffer);
544 for (i = 0; i < UI_READLINE_SIZE; i++)
545 readline->history[i] = &(readline->buffer[i][0]);
546
547 readline->used = 0;
548 readline->current = -1;
549
550 ui_input_init(&readline->input, text ? text : "");
551 }
552
553 const char *ui_readline(struct readline *readline, int width)
554 {
555 int state;
556
557 /* Override key up/down to navigate history. */
558 switch (ui.key)
559 {
560 case KEY_UP:
561 ui.key = 0;
562
563 if (readline->current == -1) /* no history entries */
564 break;
565 if (readline->current == 0) /* no older entries */
566 break;
567
568 readline->current--;
569 ui_input_init(&readline->input, readline->history[readline->current]);
570 readline->input.p = readline->input.q = readline->input.end;
571 break;
572 case KEY_DOWN:
573 ui.key = 0;
574
575 if (readline->current == -1) /* no history entries */
576 break;
577 if (readline->current == readline->used) /* no newer entries */
578 break;
579 if (readline->current == readline->used - 1) /* at insertion point */
580 {
581 readline->current++;
582 ui_input_init(&readline->input, "");
583 readline->input.p = readline->input.q = readline->input.end;
584 break;
585 }
586
587 readline->current++;
588 ui_input_init(&readline->input, readline->history[readline->current]);
589 readline->input.p = readline->input.q = readline->input.end;
590 break;
591 }
592
593 state = ui_input(&readline->input, width, 1);
594
595 /* Remember line in history. */
596 if (state == UI_INPUT_ACCEPT)
597 {
598 char *accepted;
599
600 if (readline->used == UI_READLINE_SIZE)
601 {
602 char *tmp = readline->history[0];
603 memmove(readline->history, readline->history + 1, (UI_READLINE_SIZE - 1) * sizeof(char *));
604 readline->history[UI_READLINE_SIZE - 1] = tmp;
605
606 fz_strlcpy(readline->history[UI_READLINE_SIZE - 1], readline->input.text, UI_INPUT_SIZE);
607 accepted = readline->history[UI_READLINE_SIZE - 1];
608 readline->current = UI_READLINE_SIZE;
609 }
610 else
611 {
612 fz_strlcpy(readline->history[readline->used], readline->input.text, UI_INPUT_SIZE);
613 accepted = readline->history[readline->used];
614 readline->used++;
615 readline->current = readline->used;
616 }
617
618 return accepted;
619 }
620
621 return NULL;
622 }