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
|
/**
* yudit unicode editor source file
*
* gNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <swidget/SEditor.h>
#include <swidget/SEditorIF.h>
#include <stoolkit/SEncoder.h>
/**
* All editors are like this one.
*/
SEditor::SEditor(void)
{
cleared = false;
paragBreak = "\n";
editorIF = 0;
vimode = false;
undoIndex = 0;
setup();
}
SEditor::SEditor (const SString& lb)
{
cleared = false;
paragBreak = lb;
editorIF = 0;
vimode = false;
undoIndex = 0;
setup();
}
void
SEditor::setup()
{
inputMethods.append (SS_DEFAULT_IM);
inputMethods.append ("unicode");
inputMethods.append (SS_KINPUT2_IM);
inputMethods.append ("Hungarian");
inputMethods.append ("Kana");
inputMethods.append ("Hangul");
fonts.append (SS_DEFAULT_FONT);
fonts.append ("Bitmap");
fonts.append ("TrueType");
}
/**
* Copy editor and lose undo's
*/
SEditor::SEditor (const SEditor& in)
{
editorIF = in.editorIF;
vimode = in.vimode;
inputMethods = in.inputMethods;
fonts = in.fonts;
undoIndex = 0;
paragBreak = in.paragBreak;
}
/**
* Copy editor and lose undo's
*/
SEditor
SEditor::operator=(const SEditor& in)
{
editorIF = in.editorIF;
vimode = in.vimode;
inputMethods = in.inputMethods;
fonts = in.fonts;
undoIndex = 0;
paragBreak = in.paragBreak;
return *this;
}
SEditor::~SEditor ()
{
}
void
SEditor::setInterface(SEditorIF* _editorIF)
{
editorIF = _editorIF;
}
/**
* called when a key has been pressed.
*/
void
SEditor::keyPressed (SWindowListener::SKey key, const SString& s,
bool ctrl, bool shift, bool meta)
{
if (editorIF == 0) return;
if (key == SWindowListener::Key_Shift_R || key==SWindowListener::Key_Meta_L ||
key==SWindowListener::Key_Meta_R ||
key==SWindowListener::Key_Control_R ||
key==SWindowListener::Key_Control_L ||
key==SWindowListener::Key_Shift_L)
{
return;
}
/* Whoops - we did not receive keyrelease */
//Enable selection with SHIFT,
// Addition by Maarten van Gompel <proycon@anaproy.homeip.net>
if (editorIF->isSelecting() && !meta && !shift && !ctrl )
{
editorIF->endSelect();
}
SString erasedString ;
SCursorIndex b = editorIF->getCursorIndex();
SCursorIndex a;
SCursorIndex ba;
SUndo::SType std;
SS_Embedding embd = editorIF->getDocumentEmbedding();
cleared = false;
SString lineBreak = paragBreak;
if (meta) lineBreak = SS_LB_LS;
if (ctrl) lineBreak = SS_LB_FF;
/* If there's a selection, erase it first before adding new input*/
if ((key != SWindowListener::Key_F1) &&
(key != SWindowListener::Key_F2) &&
(key != SWindowListener::Key_F3) &&
(key != SWindowListener::Key_F4) &&
(key != SWindowListener::Key_F5) &&
(key != SWindowListener::Key_F6) &&
(key != SWindowListener::Key_F7) &&
(key != SWindowListener::Key_F8) &&
(key != SWindowListener::Key_F9) &&
(key != SWindowListener::Key_F10) &&
(key != SWindowListener::Key_F11) &&
(key != SWindowListener::Key_F12) &&
(key != SWindowListener::Key_Control_R) &&
(key != SWindowListener::Key_Control_L) &&
(key != SWindowListener::Key_Meta_R) &&
(key != SWindowListener::Key_Meta_L) &&
(key != SWindowListener::Key_Alt_R) &&
(key != SWindowListener::Key_Alt_L) &&
(key != SWindowListener::Key_Left) &&
(key != SWindowListener::Key_Right) &&
(key != SWindowListener::Key_Up) &&
(key != SWindowListener::Key_Down) &&
(key != SWindowListener::Key_Prior) &&
(key != SWindowListener::Key_Next) &&
(key != SWindowListener::Key_Home) &&
(key != SWindowListener::Key_End) &&
(key != SWindowListener::Key_Escape) &&
(!ctrl) && (!meta))
{
std = SUndo::SS_EraseSelect;
STextIndex mba = editorIF->getSelectedIndex(true);
ba = SCursorIndex (mba.line, mba.index);
erasedString = editorIF->eraseSelectedText();
if (erasedString.size() != 0)
{
a = editorIF->getCursorIndex();
add (SUndo(std, editorIF->getDirection (), embd, erasedString, b, a, ba));
}
}
switch (key)
{
case SWindowListener::Key_Escape:
// vi
if (vimode)
{
editorIF->setEditable(!editorIF->isEditable());
}
else
{
editorIF->focusOut();
}
return;
case SWindowListener::Key_Home:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
if (ctrl || meta)
{
editorIF->setCursorIndex(SCursorIndex(0,0));
if (editorIF->isSelecting())
{
if (meta)
{
editorIF->selectText (editorIF->getCursorIndex());
}
else // ctrl should just go to end without selecting.
{
editorIF->deselectText();
}
}
}
else
{
SCursorIndex ti = editorIF->getCursorIndex();
ti.textIndex.index = 0;
ti.before = true;
editorIF->setCursorIndex(ti);
if (editorIF->isSelecting())
{
editorIF->selectText (editorIF->getCursorIndex());
}
}
break;
case SWindowListener::Key_End:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
if (ctrl || meta)
{
editorIF->setCursorIndex(SCursorIndex(100000000,100000000));
if (editorIF->isSelecting())
{
if (meta)
{
editorIF->selectText (editorIF->getCursorIndex());
}
else // ctrl should just go to end without selecting.
{
editorIF->deselectText();
}
}
}
else
{
SCursorIndex ti = editorIF->getCursorIndex();
ti.textIndex.index = 100000000;
ti.before =true;
editorIF->setCursorIndex(ti);
if (editorIF->isSelecting())
{
editorIF->selectText (editorIF->getCursorIndex());
}
}
break;
case SWindowListener::Key_Prior:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->pageUp();
return;
case SWindowListener::Key_Next:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->pageDown();
return;
case SWindowListener::Key_Up:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->caretUp();
return;
case SWindowListener::Key_Down:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->caretDown();
return;
case SWindowListener::Key_Left:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->caretLeft();
return;
case SWindowListener::Key_Right:
if ((shift || ctrl || meta) && !editorIF->isSelecting()) { editorIF->startSelect(); }
editorIF->caretRight();
return;
/*SGC*/
case SWindowListener::Key_BackSpace:
/* switch - delete a single character */
if (erasedString.size() == 0)
{
std = SUndo::SS_BackSpaceOne;
erasedString = editorIF->backspace();
/* not even used */
ba = editorIF->getCursorIndex();
add (SUndo(std, editorIF->getDirection (), embd, erasedString, b, ba, ba));
}
return;
case SWindowListener::Key_Delete:
/* switch - backspace */
if (erasedString.size() == 0)
{
std = SUndo::SS_EraseOne;
erasedString = editorIF->erase();
/* not even used */
ba = editorIF->getCursorIndex();
add (SUndo(std, editorIF->getDirection (), embd, erasedString, b, ba, ba));
}
return;
case SWindowListener::Key_Enter:
editorIF->insertDirtyText(lineBreak);
ba = editorIF->getCursorIndex();
if (cleared) return;
a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Insert, editorIF->getDirection (), embd, lineBreak, b, a, ba));
break;
case SWindowListener::Key_Return:
editorIF->insertDirtyText(lineBreak);
ba = editorIF->getCursorIndex();
if (cleared) return;
a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Insert, editorIF->getDirection (), embd, lineBreak, b, a, ba));
break;
case SWindowListener::Key_F1:
fKey (0, ctrl|shift|meta);
break;
case SWindowListener::Key_F2:
fKey (1, ctrl|shift|meta);
break;
case SWindowListener::Key_F3:
fKey (2, ctrl|shift|meta);
break;
case SWindowListener::Key_F4:
fKey (3, ctrl|shift|meta);
break;
case SWindowListener::Key_F5:
fKey (4, ctrl|shift|meta);
break;
case SWindowListener::Key_F6:
fKey (5, ctrl|shift|meta);
break;
case SWindowListener::Key_F7:
fKey (6, ctrl|shift|meta);
break;
case SWindowListener::Key_F8:
fKey (7, ctrl|shift|meta);
break;
case SWindowListener::Key_F9:
fKey (8, ctrl|shift|meta);
break;
case SWindowListener::Key_F10:
fKey (9, ctrl|shift|meta);
break;
case SWindowListener::Key_F11:
fKey (10, ctrl|shift|meta);
break;
case SWindowListener::Key_F12:
fKey (11, ctrl|shift|meta);
break;
/* This is used with accelerator anyway - this is never called here */
case SWindowListener::Key_Y:
case SWindowListener::Key_y:
if (ctrl|meta)
{
editorIF->setDirection (SS_DR_L);
break;
}
/* This is used with accelerator anyway - this is never called here */
case SWindowListener::Key_D:
case SWindowListener::Key_d:
if (ctrl|meta)
{
if (editorIF->getDirection () != SS_DR_RO)
{
editorIF->setDirection (SS_DR_RO);
}
else
{
editorIF->setDirection (SS_DR_LO);
}
break;
}
/* This is used with accelerator anyway - this is never called here */
case SWindowListener::Key_E:
case SWindowListener::Key_e:
if (ctrl|meta)
{
if (editorIF->getDirection () != SS_DR_RE)
{
editorIF->setDirection (SS_DR_RE);
}
else
{
editorIF->setDirection (SS_DR_LE);
}
break;
}
// PAGE UP
case SWindowListener::Key_B:
case SWindowListener::Key_b:
if (ctrl|meta)
{
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->pageUp ();
break;
}
// PAGE DOWN
case SWindowListener::Key_F:
case SWindowListener::Key_f:
if (ctrl|meta)
{
// Allow selecting with shift
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->pageDown ();
break;
}
// CURSOR LEFT
case SWindowListener::Key_H:
case SWindowListener::Key_h:
if (ctrl|meta)
{
// Allow selecting with shift
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->caretLeft ();
break;
}
// CURSOR RIGHT
case SWindowListener::Key_L:
case SWindowListener::Key_l:
if (ctrl||meta)
{
// Allow selecting with shift
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->caretRight ();
break;
}
// CURSOR DOWN
case SWindowListener::Key_J:
case SWindowListener::Key_j:
/* Unefffective. Used by Syntax */
//case SWindowListener::Key_N:
//case SWindowListener::Key_n:
if (ctrl||meta)
{
// Allow selecting with shift
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->caretDown ();
break;
}
// CURSOR UP
case SWindowListener::Key_K:
case SWindowListener::Key_k:
if (ctrl|meta)
{
// Allow selecting with shift
if ((shift) && !editorIF->isSelecting()) { editorIF->startSelect(); }
//if (ctrl && editorIF->isSelecting()) editorIF->endSelect();
editorIF->caretUp ();
break;
}
case SWindowListener::Key_M:
case SWindowListener::Key_m:
if (ctrl|meta)
{
editorIF->endSelect();
editorIF->setCursorIndex(SCursorIndex(b.textIndex.line, 0));
STextIndex mba = editorIF->getIndexAfterLineBreak ();
ba = SCursorIndex (mba.line, mba.index);
erasedString = editorIF->eraseText(mba);
if (cleared) return;
if (erasedString.size() != 0)
{
a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Erase, editorIF->getDirection (), embd,
erasedString, b, a, ba));
}
break;
}
case SWindowListener::Key_R:
case SWindowListener::Key_r:
if (ctrl|meta)
{
redo ();
break;
}
case SWindowListener::Key_U:
case SWindowListener::Key_u:
if (ctrl|meta)
{
undo ();
break;
}
case SWindowListener::Key_C:
case SWindowListener::Key_c:
if (ctrl|meta)
{
if (editorIF->isSelecting()) editorIF->endSelect();
editorIF->clipSelect(false);
return; /* Automatic copy */
}
case SWindowListener::Key_X:
case SWindowListener::Key_x:
if (ctrl | meta)
{
std = SUndo::SS_EraseSelect;
editorIF->clipSelect(false);
STextIndex mba = editorIF->getSelectedIndex(true);
ba = SCursorIndex (mba.line, mba.index);
erasedString = editorIF->eraseSelectedText();
if (erasedString.size() != 0)
{
a = editorIF->getCursorIndex();
add (SUndo(std, editorIF->getDirection (), embd, erasedString, b, a, ba));
}
break;
}
case SWindowListener::Key_v:
case SWindowListener::Key_V:
if (ctrl|meta)
{
if (editorIF->isSelecting()) editorIF->endSelect();
// erase selected text and replace it with b
std = SUndo::SS_EraseSelect;
STextIndex mba = editorIF->getSelectedIndex(true);
ba = SCursorIndex (mba.line, mba.index);
erasedString = editorIF->eraseSelectedText();
if (erasedString.size() != 0)
{
a = editorIF->getCursorIndex();
add (SUndo(std, editorIF->getDirection (), embd, erasedString, b, a, ba));
b = editorIF->getCursorIndex();
}
pasteText (b, false);
break;
}
case SWindowListener::Key_Send:
default:
if (vimode && key != SWindowListener::Key_Send &&
s == ":" && !editorIF->isEditable())
{
/* push the focus to the command window. */
editorIF->focusOut();
break;
}
SEncoder enc;
SV_UCS4 ustr = enc.decode(s);
while (ustr.size() && editorIF->addComposing(ustr[0]))
{
std = SUndo::SS_ComposeOne;
a = editorIF->getCursorIndex();
SS_UCS4 u4 = ustr[0];
/* hack, string stores u4 */
add (SUndo(std, editorIF->getDirection (), embd,
SString((char*)&u4, sizeof (u4)), a, a, a));
ustr.remove (0);
}
SString sstr = enc.encode (ustr);
if (sstr.size())
{
b = editorIF->getCursorIndex();
editorIF->insertDirtyText(sstr);
ba = editorIF->getCursorIndex();
a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Insert, editorIF->getDirection (), embd,
sstr, b, a, ba));
}
else /* to update glyph info */
{
a = editorIF->getCursorIndex();
editorIF->setCursorIndex(a);
}
}
}
/**
* called when a key was released.
*/
void
SEditor::keyReleased (SWindowListener::SKey key, const SString& s,
bool ctrl, bool shift, bool meta)
{
if (editorIF == 0) return;
if (key==SWindowListener::Key_Meta_R || key == SWindowListener::Key_Meta_L
|| key == SWindowListener::Key_Alt_R || key==SWindowListener::Key_Alt_L
|| key == SWindowListener::Key_Control_L||key==SWindowListener::Key_Control_R
|| key == SWindowListener::Key_Shift_R || key==SWindowListener::Key_Shift_L)
{
editorIF->endSelect();
return;
}
}
/**
* called when a mouse button was pressed.
*/
void
SEditor::buttonPressed (int button, const SCursorIndex& index)
{
if (editorIF == 0) return;
editorIF->deselectText();
if (button == 1)
{
pasteText(editorIF->getCursorIndex(), true);
return;
}
/*mousewheel*/
if (button == 4) {
editorIF->caretDown();
} else if (button == 3) {
editorIF->caretUp();
}
else {
editorIF->setCursorIndex (index);
editorIF->startSelect();
}
}
/**
* paste a text ftom clip
*/
void
SEditor::pasteText (const SCursorIndex& index, bool isPrimary)
{
if (editorIF == 0) return;
editorIF->deselectText();
editorIF->setCursorIndex(index);
SCursorIndex b = index;
/* stop input methods */
editorIF->insertClipboardText(isPrimary);
SCursorIndex ba = editorIF->getCursorIndex();
SString s = editorIF->getText (b.getTextIndex(), ba.getTextIndex());
SCursorIndex a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Insert, editorIF->getDirection (), editorIF->getDocumentEmbedding(), s, b, a, ba));
}
/**
* Set clean text.
*/
void
SEditor::insertText (const SString& text)
{
if (editorIF == 0) return;
editorIF->deselectText();
SCursorIndex b = editorIF->getCursorIndex();
editorIF->insertText(text);
SCursorIndex ba = editorIF->getCursorIndex();
SString s = text;
SCursorIndex a = editorIF->getCursorIndex();
add (SUndo(SUndo::SS_Insert, editorIF->getDirection (), editorIF->getDocumentEmbedding(), s, b, a, ba));
}
/**
* called when a mouse button was released.
*/
void
SEditor::buttonReleased (int button, const SCursorIndex& index)
{
if (editorIF == 0) return;
if (button == 1)
{
return;
}
editorIF->selectText (index);
editorIF->endSelect ();
}
/**
* called when a mouse button was dragged.
*/
void
SEditor::buttonDragged (int button, const SCursorIndex& index)
{
if (button == 1)
{
return;
}
if (editorIF == 0) return;
editorIF->selectText (index);
}
/**
* called when a mouse button was nulti-clicked.
* It can come only if button was already released.
*/
void
SEditor::multiClicked (int button, const SCursorIndex& index, int count)
{
if (editorIF == 0) return;
if (button == 1)
{
return;
}
editorIF->selectText (index);
editorIF->endSelect ();
if (count == 2)
{
editorIF->selectWord ();
}
else if (count > 2)
{
editorIF->selectLine ();
}
}
/**
* This sets a set of input methods that can be activated with F1-F12.
*/
void
SEditor::setInputMethods (const SStringVector& in)
{
inputMethods = in;
}
/**
* set editor mode.
*/
void
SEditor::setVI (bool vi)
{
vimode = vi;
}
/**
* This sets a set of fonts that can be activated with <ctrl>F1-F12.
*/
void
SEditor::setFonts (const SStringVector& in)
{
fonts = in;
}
/**
* An FKEY was pressed.
* @param num
* <ul>
* <li> F1 - 0 </li>
* <li> F2 - 1 </li>
* <li> Fn - n-1 </li>
* </ul>
*/
void
SEditor::fKey(int num, bool ctrl)
{
if (ctrl)
{
if (fonts.size() >= (unsigned int) num+1)
{
editorIF->setFont(fonts[num]);
}
}
else
{
if (inputMethods.size() >= (unsigned int) num+1)
{
editorIF->setInputMethod(inputMethods[num]);
}
}
}
/**
* Try to undo
* @return false in case it can not be undone.
*/
bool
SEditor::undo()
{
//fprintf (stderr, "undo\n");
if (editorIF == 0) return false;
/* clear state should be visible */
if (editorIF->clearState()) return true;
if (undoIndex == 0)
{
//fprintf (stderr, "SEditor::nothing to undo - undoindex\n");
return false;
}
if (!editorIF->isEditable())
{
return false;
}
if (editorIF->isSelecting())
{
// fprintf (stderr, "SEditor::can not undo while selecting.\n");
return false;
}
undoIndex--;
SUndo u = undoBuffer[undoIndex];
//fprintf (stderr, "Undo:%d.\n", u.type);
switch (u.type)
{
case SUndo::SS_Insert:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.beforeAfter);
editorIF->eraseText (u.before.getTextIndex());
editorIF->setDirection(u.direction);
break;
case SUndo::SS_Erase:
case SUndo::SS_EraseOne:
case SUndo::SS_BackSpaceOne:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.beforeAfter);
editorIF->setCursorIndex(u.after);
editorIF->insertText(u.string, false);
editorIF->setCursorIndex(u.before);
break;
case SUndo::SS_ComposeOne:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.after);
editorIF->setDirection(u.direction);
editorIF->removeComposing();
editorIF->setCursorIndex(u.after);
editorIF->setDirection(u.direction);
/* compose does not move caret */
break;
case SUndo::SS_EraseSelect:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.after);
editorIF->insertText(u.string, false);
editorIF->startSelect();
/* this is the sleection spot */
editorIF->selectText(u.after);
editorIF->endSelect();
editorIF->setCursorIndex(u.after);
break;
}
return true;
}
/**
* Try to redo
* @return false in case it can not be undone.
*/
bool
SEditor::redo()
{
//fprintf (stderr, "redo\n");
if (editorIF == 0) return false;
editorIF->clearState();
if (undoIndex == undoBuffer.size())
{
//fprintf (stderr, "SEditor::redo ends here.\n");
return false;
}
if (!editorIF->isEditable()) return false;
if (editorIF->isSelecting())
{
//fprintf (stderr, "SEditor::can not redo while selecting.\n");
return false;
}
SUndo u = undoBuffer[undoIndex];
undoIndex++;
//fprintf (stderr, "redo:%d.\n", u.type);
switch (u.type)
{
case SUndo::SS_Insert:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.before);
editorIF->insertText(u.string, false);
break;
case SUndo::SS_Erase:
case SUndo::SS_EraseSelect:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.after);
editorIF->eraseText(u.beforeAfter.getTextIndex());
break;
case SUndo::SS_BackSpaceOne:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.before);
editorIF->backspace();
break;
case SUndo::SS_EraseOne:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.before);
editorIF->erase();
break;
case SUndo::SS_ComposeOne:
editorIF->setDocumentEmbedding(u.embedding);
editorIF->setCursorIndex(u.after);
editorIF->setDirection(u.direction);
{
const SS_UCS4* u4 = (const SS_UCS4*) u.string.array();
editorIF->addComposing(u4[0]);
editorIF->setCursorIndex(u.after);
}
break;
}
return true;
}
/**
* add an item to undo buffer
* @param u is the new item.
* TODO: clear buffer after some size.
*/
void
SEditor::add (const SUndo& u)
{
if (undoBuffer.size() != undoIndex)
{
if (undoIndex==0)
{
undoBuffer.clear();
}
else
{
undoBuffer.truncate(undoIndex);
}
undoIndex = undoBuffer.size();
}
undoBuffer.append (u);
undoIndex++;
}
/**
* clear the undo buffer.
*/
void
SEditor::clearUndo()
{
undoBuffer.clear();
undoIndex = 0;
}
void
SEditor::clear ()
{
cleared = true;
vimode = false;
undoIndex = 0;
undoBuffer.clear();
}
void
SEditor::setParagraphSeparator (const SString& _paragBreak)
{
paragBreak = _paragBreak;
}
SString
SEditor::getParagraphSeparator () const
{
return SString (paragBreak);
}
|