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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 147 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 209 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 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 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 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 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 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 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 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 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 1019 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 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 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
|
/* Copyright (C) 2019 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkistringprivate.h"
#include "gtktexthistoryprivate.h"
/*
* The GtkTextHistory works in a way that allows text widgets to deliver
* information about changes to the underlying text at given offsets within
* their text. The GtkTextHistory object uses a series of callback functions
* (see GtkTextHistoryFuncs) to apply changes as undo/redo is performed.
*
* The GtkTextHistory object is careful to avoid tracking changes while
* applying specific undo/redo actions.
*
* Changes are tracked within a series of actions, contained in groups. The
* group may be coalesced when gtk_text_history_end_user_action() is
* called.
*
* Calling gtk_text_history_begin_irreversible_action() and
* gtk_text_history_end_irreversible_action() can be used to denote a
* section of operations that cannot be undone. This will cause all previous
* changes tracked by the GtkTextHistory to be discarded.
*/
typedef struct _Action Action;
typedef enum _ActionKind ActionKind;
enum _ActionKind
{
ACTION_KIND_BARRIER = 1,
ACTION_KIND_DELETE_BACKSPACE = 2,
ACTION_KIND_DELETE_KEY = 3,
ACTION_KIND_DELETE_PROGRAMMATIC = 4,
ACTION_KIND_DELETE_SELECTION = 5,
ACTION_KIND_GROUP = 6,
ACTION_KIND_INSERT = 7,
};
struct _Action
{
ActionKind kind;
GList link;
guint is_modified : 1;
guint is_modified_set : 1;
union {
struct {
IString istr;
guint begin;
guint end;
} insert;
struct {
IString istr;
guint begin;
guint end;
struct {
int insert;
int bound;
} selection;
} delete;
struct {
GQueue actions;
guint depth;
} group;
} u;
};
struct _GtkTextHistory
{
GObject parent_instance;
GtkTextHistoryFuncs funcs;
gpointer funcs_data;
GQueue undo_queue;
GQueue redo_queue;
struct {
int insert;
int bound;
} selection;
guint irreversible;
guint in_user;
guint max_undo_levels;
guint can_undo : 1;
guint can_redo : 1;
guint is_modified : 1;
guint is_modified_set : 1;
guint applying : 1;
guint enabled : 1;
};
static void action_free (Action *action);
G_DEFINE_TYPE (GtkTextHistory, gtk_text_history, G_TYPE_OBJECT)
#define return_if_applying(instance) \
G_STMT_START { \
if ((instance)->applying) \
return; \
} G_STMT_END
#define return_if_irreversible(instance) \
G_STMT_START { \
if ((instance)->irreversible) \
return; \
} G_STMT_END
#define return_if_not_enabled(instance) \
G_STMT_START { \
if (!(instance)->enabled) \
return; \
} G_STMT_END
static const char *
action_kind_name (ActionKind kind)
{
switch (kind)
{
case ACTION_KIND_BARRIER: return "Barrier";
case ACTION_KIND_DELETE_BACKSPACE: return "Delete_Backspace";
case ACTION_KIND_DELETE_KEY: return "Delete_Key";
case ACTION_KIND_DELETE_PROGRAMMATIC: return "Delete_Programmatic";
case ACTION_KIND_DELETE_SELECTION: return "Delete_Selection";
case ACTION_KIND_GROUP: return "Group";
case ACTION_KIND_INSERT: return "Insert";
default: return "unknown";
}
}
static inline void
gtk_text_history_printf_space (GString *str,
guint depth)
{
for (guint i = 0; i < depth; i++)
g_string_append (str, " ");
}
static void
gtk_text_history_printf_action (Action *action,
GString *str,
guint depth)
{
gtk_text_history_printf_space (str, depth);
g_string_append_printf (str, "%s {\n", action_kind_name (action->kind));
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "is_modified: %s\n", action->is_modified ? "true" : "false");
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "is_modified_set: %s\n", action->is_modified_set ? "true" : "false");
switch (action->kind)
{
case ACTION_KIND_BARRIER:
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "depth: %u\n", action->u.group.depth);
break;
case ACTION_KIND_DELETE_BACKSPACE:
case ACTION_KIND_DELETE_KEY:
case ACTION_KIND_DELETE_PROGRAMMATIC:
case ACTION_KIND_DELETE_SELECTION:
{
char *escaped;
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "begin: %u\n", action->u.delete.begin);
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "end: %u\n", action->u.delete.end);
gtk_text_history_printf_space (str, depth+1);
g_string_append (str, "selection {\n");
gtk_text_history_printf_space (str, depth+2);
g_string_append_printf (str, "insert: %d\n", action->u.delete.selection.insert);
gtk_text_history_printf_space (str, depth+2);
g_string_append_printf (str, "bound: %d\n", action->u.delete.selection.bound);
gtk_text_history_printf_space (str, depth+1);
g_string_append (str, "}\n");
gtk_text_history_printf_space (str, depth+1);
escaped = g_strescape (istring_str (&action->u.delete.istr), NULL);
g_string_append_printf (str, "text: \"%s\"\n", escaped);
g_free (escaped);
}
break;
case ACTION_KIND_INSERT:
{
char *escaped;
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "begin: %u\n", action->u.insert.begin);
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "end: %u\n", action->u.insert.end);
gtk_text_history_printf_space (str, depth+1);
escaped = g_strescape (istring_str (&action->u.insert.istr), NULL);
g_string_append_printf (str, "text: \"%s\"\n", escaped);
g_free (escaped);
}
break;
case ACTION_KIND_GROUP:
gtk_text_history_printf_space (str, depth+1);
g_string_append_printf (str, "depth: %u\n", action->u.group.depth);
for (const GList *iter = action->u.group.actions.head; iter; iter = iter->next)
{
Action *child = iter->data;
gtk_text_history_printf_space (str, depth+1);
g_string_append (str, "children {\n");
gtk_text_history_printf_action (child, str, depth+2);
gtk_text_history_printf_space (str, depth+1);
g_string_append (str, "}\n");
}
break;
default:
break;
}
gtk_text_history_printf_space (str, depth);
g_string_append (str, "}\n");
}
G_GNUC_UNUSED static char *
gtk_text_history_printf (GtkTextHistory *history)
{
GString *str = g_string_new (NULL);
g_string_append (str, "undo {\n");
for (const GList *iter = history->undo_queue.head; iter; iter = iter->next)
gtk_text_history_printf_action (iter->data, str, 1);
g_string_append (str, "}\n");
g_string_append (str, "redo {\n");
for (const GList *iter = history->redo_queue.head; iter; iter = iter->next)
gtk_text_history_printf_action (iter->data, str, 1);
g_string_append (str, "}\n");
return g_string_free (str, FALSE);
}
static inline void
uint_order (guint *a,
guint *b)
{
if (*a > *b)
{
guint tmp = *a;
*a = *b;
*b = tmp;
}
}
static void
clear_action_queue (GQueue *queue)
{
g_assert (queue != NULL);
while (queue->length > 0)
{
Action *action = g_queue_peek_head (queue);
g_queue_unlink (queue, &action->link);
action_free (action);
}
}
static Action *
action_new (ActionKind kind)
{
Action *action;
action = g_new0 (Action, 1);
action->kind = kind;
action->link.data = action;
return action;
}
static void
action_free (Action *action)
{
if (action->kind == ACTION_KIND_INSERT)
istring_clear (&action->u.insert.istr);
else if (action->kind == ACTION_KIND_DELETE_BACKSPACE ||
action->kind == ACTION_KIND_DELETE_KEY ||
action->kind == ACTION_KIND_DELETE_PROGRAMMATIC ||
action->kind == ACTION_KIND_DELETE_SELECTION)
istring_clear (&action->u.delete.istr);
else if (action->kind == ACTION_KIND_GROUP)
clear_action_queue (&action->u.group.actions);
g_free (action);
}
static gboolean
action_group_is_empty (const Action *action)
{
const GList *iter;
g_assert (action->kind == ACTION_KIND_GROUP);
for (iter = action->u.group.actions.head; iter; iter = iter->next)
{
const Action *child = iter->data;
if (child->kind == ACTION_KIND_BARRIER)
continue;
if (child->kind == ACTION_KIND_GROUP && action_group_is_empty (child))
continue;
return FALSE;
}
return TRUE;
}
static gboolean
action_chain (Action *action,
Action *other,
gboolean in_user_action)
{
g_assert (action != NULL);
g_assert (other != NULL);
if (action->kind == ACTION_KIND_GROUP)
{
Action *tail = g_queue_peek_tail (&action->u.group.actions);
/* Always push new items onto a group, so that we can coalesce
* items when gtk_text_history_end_user_action() is called.
*
* But we don't care if this is a barrier since we will always
* apply things as a group anyway.
*/
if (other->kind == ACTION_KIND_BARRIER)
{
/* If we're not in a user action, this barrier is meant to
* stop items from coalescing into this group.
*/
if (!in_user_action && action->u.group.depth == 0)
return FALSE;
action_free (other);
return TRUE;
}
/* Try to chain onto the tail item in the group to increase
* the chances we have a single action within the group. That
* way we are more likely to hoist out of the group when the
* user action is ended.
*/
if (tail != NULL && tail->kind == other->kind)
{
if (action_chain (tail, other, in_user_action))
return TRUE;
}
g_queue_push_tail_link (&action->u.group.actions, &other->link);
return TRUE;
}
/* The rest can only be merged to themselves */
if (action->kind != other->kind)
return FALSE;
switch (action->kind)
{
case ACTION_KIND_INSERT: {
/* Make sure the new insert is at the end of the previous */
if (action->u.insert.end != other->u.insert.begin)
return FALSE;
/* If we are not within a user action, be more selective */
if (!in_user_action)
{
/* Avoid pathological cases */
if (other->u.insert.istr.n_chars > 1000)
return FALSE;
/* We will coalesce space, but not new lines. */
if (istring_contains_unichar (&action->u.insert.istr, '\n') ||
istring_contains_unichar (&other->u.insert.istr, '\n'))
return FALSE;
/* Chain space to items that ended in space. This is generally
* just at the start of a line where we could have indentation
* space.
*/
if ((istring_empty (&action->u.insert.istr) ||
istring_ends_with_space (&action->u.insert.istr)) &&
istring_only_contains_space (&other->u.insert.istr))
goto do_chain;
/* Starting a new word, don't chain this */
if (istring_starts_with_space (&other->u.insert.istr))
return FALSE;
/* Check for possible paste (multi-character input) or word input that
* has spaces in it (and should treat as one operation).
*/
if (other->u.insert.istr.n_chars > 1 &&
istring_contains_space (&other->u.insert.istr))
return FALSE;
}
do_chain:
istring_append (&action->u.insert.istr, &other->u.insert.istr);
action->u.insert.end += other->u.insert.end - other->u.insert.begin;
action_free (other);
return TRUE;
}
case ACTION_KIND_DELETE_PROGRAMMATIC:
/* We can't tell if this should be chained because we don't
* have a group to coalesce. But unless each action deletes
* a single character, the overhead isn't too bad as we embed
* the strings in the action.
*/
return FALSE;
case ACTION_KIND_DELETE_SELECTION:
/* Don't join selection deletes as they should appear as a single
* operation and have selection reinstanted when performing undo.
*/
return FALSE;
case ACTION_KIND_DELETE_BACKSPACE:
if (other->u.delete.end == action->u.delete.begin)
{
istring_prepend (&action->u.delete.istr,
&other->u.delete.istr);
action->u.delete.begin = other->u.delete.begin;
action_free (other);
return TRUE;
}
return FALSE;
case ACTION_KIND_DELETE_KEY:
if (action->u.delete.begin == other->u.delete.begin)
{
if (!istring_contains_space (&other->u.delete.istr) ||
istring_only_contains_space (&action->u.delete.istr))
{
istring_append (&action->u.delete.istr, &other->u.delete.istr);
action->u.delete.end += other->u.delete.istr.n_chars;
action_free (other);
return TRUE;
}
}
return FALSE;
case ACTION_KIND_BARRIER:
/* Only allow a single barrier to be added. */
action_free (other);
return TRUE;
case ACTION_KIND_GROUP:
default:
g_return_val_if_reached (FALSE);
}
}
static void
gtk_text_history_do_change_state (GtkTextHistory *self,
gboolean is_modified,
gboolean can_undo,
gboolean can_redo)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
self->funcs.change_state (self->funcs_data, is_modified, can_undo, can_redo);
}
static void
gtk_text_history_do_insert (GtkTextHistory *self,
guint begin,
guint end,
const char *text,
guint len)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
g_assert (text != NULL);
uint_order (&begin, &end);
self->funcs.insert (self->funcs_data, begin, end, text, len);
}
static void
gtk_text_history_do_delete (GtkTextHistory *self,
guint begin,
guint end,
const char *expected_text,
guint len)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
uint_order (&begin, &end);
self->funcs.delete (self->funcs_data, begin, end, expected_text, len);
}
static void
gtk_text_history_do_select (GtkTextHistory *self,
guint selection_insert,
guint selection_bound)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
self->funcs.select (self->funcs_data, selection_insert, selection_bound);
}
static void
gtk_text_history_truncate_one (GtkTextHistory *self)
{
if (self->undo_queue.length > 0)
{
Action *action = g_queue_peek_head (&self->undo_queue);
g_queue_unlink (&self->undo_queue, &action->link);
action_free (action);
}
else if (self->redo_queue.length > 0)
{
Action *action = g_queue_peek_tail (&self->redo_queue);
g_queue_unlink (&self->redo_queue, &action->link);
action_free (action);
}
else
{
g_assert_not_reached ();
}
}
static void
gtk_text_history_truncate (GtkTextHistory *self)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
if (self->max_undo_levels == 0)
return;
while (self->undo_queue.length + self->redo_queue.length > self->max_undo_levels)
gtk_text_history_truncate_one (self);
}
static void
gtk_text_history_finalize (GObject *object)
{
GtkTextHistory *self = (GtkTextHistory *)object;
clear_action_queue (&self->undo_queue);
clear_action_queue (&self->redo_queue);
G_OBJECT_CLASS (gtk_text_history_parent_class)->finalize (object);
}
static void
gtk_text_history_class_init (GtkTextHistoryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gtk_text_history_finalize;
}
static void
gtk_text_history_init (GtkTextHistory *self)
{
self->enabled = TRUE;
self->selection.insert = -1;
self->selection.bound = -1;
}
static gboolean
has_actionable (const GQueue *queue)
{
const GList *iter;
for (iter = queue->head; iter; iter = iter->next)
{
const Action *action = iter->data;
if (action->kind == ACTION_KIND_BARRIER)
continue;
if (action->kind == ACTION_KIND_GROUP)
{
if (has_actionable (&action->u.group.actions))
return TRUE;
else
continue;
}
return TRUE;
}
return FALSE;
}
static void
gtk_text_history_update_state (GtkTextHistory *self)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
if (self->irreversible || self->in_user)
{
self->can_undo = FALSE;
self->can_redo = FALSE;
}
else
{
self->can_undo = has_actionable (&self->undo_queue);
self->can_redo = has_actionable (&self->redo_queue);
}
gtk_text_history_do_change_state (self, self->is_modified, self->can_undo, self->can_redo);
}
static void
gtk_text_history_push (GtkTextHistory *self,
Action *action)
{
Action *peek;
gboolean in_user_action;
g_assert (GTK_IS_TEXT_HISTORY (self));
g_assert (self->enabled);
g_assert (action != NULL);
while (self->redo_queue.length > 0)
{
peek = g_queue_peek_head (&self->redo_queue);
g_queue_unlink (&self->redo_queue, &peek->link);
action_free (peek);
}
peek = g_queue_peek_tail (&self->undo_queue);
in_user_action = self->in_user > 0;
if (peek == NULL || !action_chain (peek, action, in_user_action))
g_queue_push_tail_link (&self->undo_queue, &action->link);
gtk_text_history_truncate (self);
gtk_text_history_update_state (self);
}
GtkTextHistory *
gtk_text_history_new (const GtkTextHistoryFuncs *funcs,
gpointer funcs_data)
{
GtkTextHistory *self;
g_return_val_if_fail (funcs != NULL, NULL);
self = g_object_new (GTK_TYPE_TEXT_HISTORY, NULL);
self->funcs = *funcs;
self->funcs_data = funcs_data;
return g_steal_pointer (&self);
}
gboolean
gtk_text_history_get_can_undo (GtkTextHistory *self)
{
g_return_val_if_fail (GTK_IS_TEXT_HISTORY (self), FALSE);
return self->can_undo;
}
gboolean
gtk_text_history_get_can_redo (GtkTextHistory *self)
{
g_return_val_if_fail (GTK_IS_TEXT_HISTORY (self), FALSE);
return self->can_redo;
}
static void
gtk_text_history_apply (GtkTextHistory *self,
Action *action,
Action *peek)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
g_assert (action != NULL);
switch (action->kind)
{
case ACTION_KIND_INSERT:
gtk_text_history_do_insert (self,
action->u.insert.begin,
action->u.insert.end,
istring_str (&action->u.insert.istr),
action->u.insert.istr.n_bytes);
/* If the next item is a DELETE_SELECTION, then we want to
* pre-select the text for the user. Otherwise, just place
* the cursor were we think it was.
*/
if (peek != NULL && peek->kind == ACTION_KIND_DELETE_SELECTION)
gtk_text_history_do_select (self,
peek->u.delete.begin,
peek->u.delete.end);
else
gtk_text_history_do_select (self,
action->u.insert.end,
action->u.insert.end);
break;
case ACTION_KIND_DELETE_BACKSPACE:
case ACTION_KIND_DELETE_KEY:
case ACTION_KIND_DELETE_PROGRAMMATIC:
case ACTION_KIND_DELETE_SELECTION:
gtk_text_history_do_delete (self,
action->u.delete.begin,
action->u.delete.end,
istring_str (&action->u.delete.istr),
action->u.delete.istr.n_bytes);
gtk_text_history_do_select (self,
action->u.delete.begin,
action->u.delete.begin);
break;
case ACTION_KIND_GROUP: {
const GList *actions = action->u.group.actions.head;
for (const GList *iter = actions; iter; iter = iter->next)
gtk_text_history_apply (self, iter->data, NULL);
break;
}
case ACTION_KIND_BARRIER:
break;
default:
g_assert_not_reached ();
}
if (action->is_modified_set)
self->is_modified = action->is_modified;
}
static void
gtk_text_history_reverse (GtkTextHistory *self,
Action *action)
{
g_assert (GTK_IS_TEXT_HISTORY (self));
g_assert (action != NULL);
switch (action->kind)
{
case ACTION_KIND_INSERT:
gtk_text_history_do_delete (self,
action->u.insert.begin,
action->u.insert.end,
istring_str (&action->u.insert.istr),
action->u.insert.istr.n_bytes);
gtk_text_history_do_select (self,
action->u.insert.begin,
action->u.insert.begin);
break;
case ACTION_KIND_DELETE_BACKSPACE:
case ACTION_KIND_DELETE_KEY:
case ACTION_KIND_DELETE_PROGRAMMATIC:
case ACTION_KIND_DELETE_SELECTION:
gtk_text_history_do_insert (self,
action->u.delete.begin,
action->u.delete.end,
istring_str (&action->u.delete.istr),
action->u.delete.istr.n_bytes);
if (action->u.delete.selection.insert != -1 &&
action->u.delete.selection.bound != -1)
gtk_text_history_do_select (self,
action->u.delete.selection.insert,
action->u.delete.selection.bound);
else if (action->u.delete.selection.insert != -1)
gtk_text_history_do_select (self,
action->u.delete.selection.insert,
action->u.delete.selection.insert);
break;
case ACTION_KIND_GROUP: {
const GList *actions = action->u.group.actions.tail;
for (const GList *iter = actions; iter; iter = iter->prev)
gtk_text_history_reverse (self, iter->data);
break;
}
case ACTION_KIND_BARRIER:
break;
default:
g_assert_not_reached ();
}
if (action->is_modified_set)
self->is_modified = !action->is_modified;
}
static void
move_barrier (GQueue *from_queue,
Action *action,
GQueue *to_queue,
gboolean head)
{
g_queue_unlink (from_queue, &action->link);
if (head)
g_queue_push_head_link (to_queue, &action->link);
else
g_queue_push_tail_link (to_queue, &action->link);
}
void
gtk_text_history_undo (GtkTextHistory *self)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
#if 0
{
char *str = gtk_text_history_printf (self);
g_print ("%s\n", str);
g_free (str);
}
#endif
if (gtk_text_history_get_can_undo (self))
{
Action *action;
self->applying = TRUE;
action = g_queue_peek_tail (&self->undo_queue);
if (action->kind == ACTION_KIND_BARRIER)
{
move_barrier (&self->undo_queue, action, &self->redo_queue, TRUE);
action = g_queue_peek_tail (&self->undo_queue);
}
g_queue_unlink (&self->undo_queue, &action->link);
g_queue_push_head_link (&self->redo_queue, &action->link);
gtk_text_history_reverse (self, action);
gtk_text_history_update_state (self);
self->applying = FALSE;
}
}
void
gtk_text_history_redo (GtkTextHistory *self)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
if (gtk_text_history_get_can_redo (self))
{
Action *action;
Action *peek;
self->applying = TRUE;
action = g_queue_peek_head (&self->redo_queue);
if (action->kind == ACTION_KIND_BARRIER)
{
move_barrier (&self->redo_queue, action, &self->undo_queue, FALSE);
action = g_queue_peek_head (&self->redo_queue);
}
g_queue_unlink (&self->redo_queue, &action->link);
g_queue_push_tail_link (&self->undo_queue, &action->link);
peek = g_queue_peek_head (&self->redo_queue);
gtk_text_history_apply (self, action, peek);
gtk_text_history_update_state (self);
self->applying = FALSE;
}
}
void
gtk_text_history_begin_user_action (GtkTextHistory *self)
{
Action *group;
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
self->in_user++;
group = g_queue_peek_tail (&self->undo_queue);
if (group == NULL || group->kind != ACTION_KIND_GROUP)
{
group = action_new (ACTION_KIND_GROUP);
gtk_text_history_push (self, group);
}
group->u.group.depth++;
gtk_text_history_update_state (self);
}
void
gtk_text_history_end_user_action (GtkTextHistory *self)
{
Action *peek;
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
clear_action_queue (&self->redo_queue);
peek = g_queue_peek_tail (&self->undo_queue);
if (peek->kind != ACTION_KIND_GROUP)
{
g_warning ("miss-matched %s end_user_action. Expected group, got %d",
G_OBJECT_TYPE_NAME (self),
peek->kind);
return;
}
self->in_user--;
peek->u.group.depth--;
/* Unless this is the last user action, short-circuit */
if (peek->u.group.depth > 0)
return;
/* Unlikely, but if the group is empty, just remove it */
if (action_group_is_empty (peek))
{
g_queue_unlink (&self->undo_queue, &peek->link);
action_free (peek);
goto update_state;
}
/* If there is a single item within the group, we can hoist
* it up increasing the chances that we can join actions.
*/
if (peek->u.group.actions.length == 1)
{
GList *link_ = peek->u.group.actions.head;
Action *replaced = link_->data;
replaced->is_modified = peek->is_modified;
replaced->is_modified_set = peek->is_modified_set;
g_queue_unlink (&peek->u.group.actions, link_);
g_queue_unlink (&self->undo_queue, &peek->link);
action_free (peek);
gtk_text_history_push (self, replaced);
goto update_state;
}
/* Now insert a barrier action so we don't allow
* joining items to this node in the future.
*/
gtk_text_history_push (self, action_new (ACTION_KIND_BARRIER));
update_state:
gtk_text_history_update_state (self);
}
void
gtk_text_history_begin_irreversible_action (GtkTextHistory *self)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
if (self->in_user)
{
g_warning ("Cannot begin irreversible action while in user action");
return;
}
self->irreversible++;
clear_action_queue (&self->undo_queue);
clear_action_queue (&self->redo_queue);
gtk_text_history_update_state (self);
}
void
gtk_text_history_end_irreversible_action (GtkTextHistory *self)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
if (self->in_user)
{
g_warning ("Cannot end irreversible action while in user action");
return;
}
self->irreversible--;
clear_action_queue (&self->undo_queue);
clear_action_queue (&self->redo_queue);
gtk_text_history_update_state (self);
}
static void
gtk_text_history_clear_modified (GtkTextHistory *self)
{
const GList *iter;
for (iter = self->undo_queue.head; iter; iter = iter->next)
{
Action *action = iter->data;
action->is_modified = FALSE;
action->is_modified_set = FALSE;
}
for (iter = self->redo_queue.head; iter; iter = iter->next)
{
Action *action = iter->data;
action->is_modified = FALSE;
action->is_modified_set = FALSE;
}
}
void
gtk_text_history_modified_changed (GtkTextHistory *self,
gboolean modified)
{
Action *peek;
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
/* If we have a new save point, clear all previous modified states. */
gtk_text_history_clear_modified (self);
if ((peek = g_queue_peek_tail (&self->undo_queue)))
{
if (peek->kind == ACTION_KIND_BARRIER)
{
if (!(peek = peek->link.prev->data))
return;
}
peek->is_modified = !!modified;
peek->is_modified_set = TRUE;
}
if ((peek = g_queue_peek_head (&self->redo_queue)))
{
if (peek->kind == ACTION_KIND_BARRIER)
{
if (peek->link.next == NULL ||
!(peek = peek->link.next->data))
return;
}
peek->is_modified = TRUE;
peek->is_modified_set = TRUE;
}
self->is_modified = !!modified;
self->is_modified_set = TRUE;
if (!self->irreversible)
gtk_text_history_update_state (self);
}
void
gtk_text_history_selection_changed (GtkTextHistory *self,
int selection_insert,
int selection_bound)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
self->selection.insert = CLAMP (selection_insert, -1, G_MAXINT);
self->selection.bound = CLAMP (selection_bound, -1, G_MAXINT);
}
void
gtk_text_history_text_inserted (GtkTextHistory *self,
guint position,
const char *text,
int len)
{
Action *action;
guint n_chars;
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
if (len < 0)
len = strlen (text);
n_chars = g_utf8_strlen (text, len);
action = action_new (ACTION_KIND_INSERT);
action->u.insert.begin = position;
action->u.insert.end = position + n_chars;
istring_set (&action->u.insert.istr, text, len, n_chars);
gtk_text_history_push (self, action);
}
void
gtk_text_history_text_deleted (GtkTextHistory *self,
guint begin,
guint end,
const char *text,
int len)
{
Action *action;
ActionKind kind;
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
return_if_not_enabled (self);
return_if_applying (self);
return_if_irreversible (self);
if (len < 0)
len = strlen (text);
if (self->selection.insert == -1 && self->selection.bound == -1)
kind = ACTION_KIND_DELETE_PROGRAMMATIC;
else if (self->selection.insert == end && self->selection.bound == -1)
kind = ACTION_KIND_DELETE_BACKSPACE;
else if (self->selection.insert == begin && self->selection.bound == -1)
kind = ACTION_KIND_DELETE_KEY;
else
kind = ACTION_KIND_DELETE_SELECTION;
action = action_new (kind);
action->u.delete.begin = begin;
action->u.delete.end = end;
action->u.delete.selection.insert = self->selection.insert;
action->u.delete.selection.bound = self->selection.bound;
istring_set (&action->u.delete.istr, text, len, MAX (end, begin) - MIN (end, begin));
gtk_text_history_push (self, action);
}
gboolean
gtk_text_history_get_enabled (GtkTextHistory *self)
{
g_return_val_if_fail (GTK_IS_TEXT_HISTORY (self), FALSE);
return self->enabled;
}
void
gtk_text_history_set_enabled (GtkTextHistory *self,
gboolean enabled)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
enabled = !!enabled;
if (self->enabled != enabled)
{
self->enabled = enabled;
if (!self->enabled)
{
self->irreversible = 0;
self->in_user = 0;
clear_action_queue (&self->undo_queue);
clear_action_queue (&self->redo_queue);
}
gtk_text_history_update_state (self);
}
}
guint
gtk_text_history_get_max_undo_levels (GtkTextHistory *self)
{
g_return_val_if_fail (GTK_IS_TEXT_HISTORY (self), 0);
return self->max_undo_levels;
}
void
gtk_text_history_set_max_undo_levels (GtkTextHistory *self,
guint max_undo_levels)
{
g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
if (self->max_undo_levels != max_undo_levels)
{
self->max_undo_levels = max_undo_levels;
gtk_text_history_truncate (self);
}
}
|