input.anubis 36.4 KB
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

   
   
                             The Anubis/Paradize Project.
   
                                  The input widget. 
   
                         Copyright (c) Alain Prouté 2004-2005. 

   
   Authors:   Alain Prouté
              Olivier Duvernois


   
   
read widget.anubis
read tools.anubis
   
   
   In  this file, the  'input' widget  is defined.  The global  term 'input'  covers three
   'inputs' :
      . the text-input, for any alphanumeric characters;
      . the integer-input, for integer number;
      . the float-input, for float number.
   
   
   *** The style   
   
   The input  is a rectangle  where characters are  written and it  is possible to  move a
   caret all along this captured text. Hence, for drawing it, we need the following data :
   
     (1) a background color,
     (2) a font size,
     (3) a font color,
     (4) a caret color,
     (5) the width for the input.

   Use the type  'IntputStyle' to set those parameters.  The parameters 1, 2 and  3 can be
   retrieve    from   WidgetParameters   (see    tools.anubis)   with    the   alternative
   'input_common_style', or define by the user with then alernative 'input_style'.
   
   
public type InputStyle:
   input_common_style
     (
       WidgetParameters            parms,
       Int32                       width           // total width of input-from in pixel
     ).

   
   
   *** The graphic aspect
   
   The size of the input will be : 
      . width  = as defined in InputStyle
      . height = total  font height +  1, to have  a line  between the character  and the
        border of the input.
   
   
   The caret, when the input is not  active, is a empty rectangle (a background-color line
   arrounded by  a caret-color thin-lin). When  the input is  active, the caret is  just a
   vertical stroke. When several characters are  selected, they are written on a rectangle
   (color : lighten caret color) and their color moves to darken.
   
   

   *** The three input 
   
public define Widget
   create_text_input
     (
       InputStyle                  style_of_input_widget,
       Var(String)      text_location_with_initial_value
     ).
   
public define Widget
   create_integer_input
     (
       InputStyle                  style_of_input_widget,
       Var(Int32)    integer_location_with_initial_value
     ).
   
public define Widget
   create_float_input
     (
       InputStyle                  style_of_input_widget,
       Var(Float)      float_location_with_initial_value,
       Int32                          number_of_decimals
     ).
   
   
   
   --- That's all for the public part ! --------------------------------------------------


   *** (1) Tools
      *** (1.1) Retrieve data from the style
      *** (1.2) Caret Position
      *** (1.3) The input rectangle
   
   *** (2) Caret 
   
   *** (3) DrawParameters
   
   *** (4) Compute the visible text in the input
   
   *** (5) Initialize DrawParameters
   
   *** (6) Special keys answer
      *** (6.1) Remove character(s)
      *** (6.2) Backspace Answer
      *** (6.3) Home Answer
      *** (6.4) End Answer
      *** (6.5) Del Answer
      *** (6.6) Left Answer
      *** (6.7) Right Answer
      *** (6.8) Special keys answer manager
   
   *** (7) Input event handler
      *** (7.1) Main function
      *** (7.2) Compute caret position after a left.click.
      *** (7.3) Keyboard Answer
      *** (7.4) Insert character(s)
   
   *** (8) The widget   
      *** (8.1) Create input form
      *** (8.2) Draw the selection rectangle
      *** (8.3) Write the characters
   
   
   
   *** (1) Tools

      *** (1.1) Retrieve data from the style

define RGB
   font_color
     (
       InputStyle ifs
     ) =
    if ifs is
     {
      input_common_style(p,_)  then *font_color_v(p)
     }.
   
define RGB
   caret_color
     (
       InputStyle ifs
     ) =
    if ifs is
     {
      input_common_style(p,_)  then *caret_color_v(p)
     }.
   
   
   
      *** (1.2) Caret Position
   
   Compute the position (in pixels) of the caret from the left of the visible text
   
define Int32
   x_caret
     (
       String            vc,
       Int32            nvc,
       InputStyle       ifs
     ) =
   if sub_string(vc,0,nvc) is
    {
     failure       then 0,
     success(s)    then
        max(if ifs is
             {
               input_common_style(p,_)  then printed_text_width(*font_v(p),s)
              }-1,0)
    }.
   

      *** (1.3) The input rectangle
   
   This rectangle  draws the input, where characters  are written. It is  used in the
   create-widget defintion, and for redrawing after a keyboard event.
   
   
define WidgetRectangle
   input_rect
     (
       InputStyle ifs
     ) =
   if ifs is
    {
     input_common_style(p,w) then 
        with fi = get_font_info(*font_v(p)),
        rect(0,0,w,word8_to_Word32(height(fi))+word8_to_Word32(depth(fi))+1),
    }.


   
  
   *** (2) Caret 
   
   Caret has two aspects, depends on if several characters aer selected (-> rectangle) or
   not (-> stroke). The value of Caret moves, if it's necessary, after keyboards event.
   
type Caret:
   stroke
     (
       String   vc,    // characters visible in the input 
       Int32   nvc,    // number of visible characters at the left of caret: 0=< n =< length(vc)
       Int32   nlc     // number of invisible characters on the left side of then input
     ),
   rectangle
     (
       String   vc,
       Int32   nvc,
       Int32   nlc,
       Int32   nsc,    // number of selected characters : nsc-1 when left key, nsc+1 when right key 
       Int32    bc     // number of characters (visible and hidden on the left of the input)
                       //  when selection begins.
     ).
   

   
   
   *** (3) DrawParameters 
   
   DrawParameters is a 'tool' type. It is used to keep in memory drawing parameters and to
   define drawing functions used to manage keyboard events. 
   
    with :
      visu_text =  function computing the  visible text (and  how many characters  will be
                    appears) from the  number of invisible characters on  the left side of
                    the input and the all_text.
   
      answer = only the alternative 'handled' with the input-rectangle defined above.

type DrawParameters: 
   draw_parms
     (
       SystemFont                                        font,
       Int32                                total_font_height,
       Int32                                            width,
       Word8 -> Int32                                char_size,
       WidgetAnswer                                    answer,
       (Int32,String) -> Maybe((String,Int32))   mb_visu_text,
       (String -> Int32)                           text_width,     // in pixels
       WidgetRectangle                             input_rect
     ).
   
   
   
   
   *** (4) Compute the visible text in the input
   
   This function gives : 
     . the visible text,
     . the length in pixels of this text,
     . the number of characters of this text
   
   When the  caret will on the  very right of the  all text, it's necessary  to compute it
   from the end of the all text. So the boolan 'reversing'.
   
define (List(Word8),Int32,Int32)
   compute_visible_text
     (
       List(Word8)          all_text,
       Int32                    max,
       Word8 -> Int32      char_size,
       List(Word8)         visu_text,
       Int32            visu_length,
       Int32                visu_nb
     ) =
   if all_text is
     { 
      []       then (visu_text,visu_length,visu_nb),
      [h . t]  then
        with new_vl = visu_length+char_size(h),
        if new_vl > max
           then (visu_text,visu_length,visu_nb) 
           else compute_visible_text(t,max,char_size,[h . visu_text],new_vl,visu_nb+1)
     }.
   
   
define (String,Int32,Int32)
   compute_visible_text
     (
       String              all_text,
       DrawParameters        dparms,
       Bool               reversing
     ) =
   if compute_visible_text
        (if reversing then reverse(explode(all_text)) else explode(all_text),
         width(dparms)-2,char_size(dparms),[],0,0) is (visu_text,visu_length,visu_nb)
   
     then if reversing then (implode(visu_text),visu_length,visu_nb)
                       else (implode(reverse(visu_text)),visu_length,visu_nb).
   
   
   
   
   *** (5) Initialize DrawParameters

define Maybe((String,Int32))
   new_visible_text
    (
      Int32                    nlc,
      String              all_text,
      Int32                    max,
      Word8 -> Int32      char_size,
    ) =
    if sub_string(all_text,nlc,length(all_text)-nlc) is
     {
      failure     then failure,
      success(s)  then 
         if compute_visible_text(explode(s),max,char_size,[],0,0) is (l,_,n) 
              then success((implode(reverse(l)),n))
     }.
   
   
define DrawParameters
   to_draw_parms
     ( 
       InputStyle  ifs
     ) =
   if ifs is
     {
      input_common_style(p,w)     then 
          with fi = get_font_info(*font_v(p)),
               char_size = (Word8 c) |-> word8_to_Word32(width(get_char_info(*font_v(p),c))),
               ir = input_rect(ifs),
          draw_parms(*font_v(p),word8_to_Word32(height(fi))+word8_to_Word32(depth(fi)),
                     w,char_size, handled([ir]),
                     (Int32 nlc,String all) |-> new_visible_text(nlc,all,w,char_size),
                     (String s) |-> printed_text_width(*font_v(p),s),
                     ir
                     )
     }.
   
   
   

   *** (6) Special keys answer
   
      *** (6.1) Remove character(s)

define WidgetAnswer
   remove_character
     (
       Var(String)       all_text,
       Var(Caret)           caret,
       DrawParameters      dparms,
       Int32              nb_left,  // nb of characters at the left of caret (or selection) from all_text 
       Int32                  nsc,
       Int32              new_nvc
     ) =
   with lt = if sub_string(*all_text,0,nb_left) is
              {
               failure    then "", 
               success(s) then s
              },
        rt = if sub_string(*all_text,nb_left+abs(nsc), length(*all_text)-nb_left-abs(nsc)) is
              {
               failure    then "",
               success(s) then s
              },
   (
    all_text <- lt+rt;
    caret <- if *caret is
               {
                stroke(vc,nvc,nlc)            then
                  with new_nlc = max(0,nlc-(if nvc=0 then 1 else 0)),
                  if mb_visu_text(dparms)(new_nlc,*all_text) is
                   {
                    failure    then stroke("",max(0,nvc-1),new_nlc),
                    success(s) then if s is (new_vc,nb_vc) then 
                       stroke(new_vc,new_nvc,new_nlc)
                   },
                rectangle(vc,nvc,nlc,nsc,bg)  then 
                  with new_nlc = max(0,nlc-(if nvc-max(0,nsc)=<0 then nvc-max(0,nsc) else 0)),
                  if mb_visu_text(dparms)(new_nlc,*all_text) is
                   {
                    failure    then stroke("",max(0,nvc),new_nlc),
                    success(s) then if s is (new_vc,nb_vc) then 
                       stroke(new_vc,new_nvc,new_nlc)
                   }
               }
   ); answer(dparms).

   
      *** (6.2) Backspace Answer
   
define WidgetAnswer
   backspace_answer
     (
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
   if *caret is
    {
     stroke(vc,nvc,nlc)            then
        remove_character(all_text,caret,dparms,nlc+nvc-1,1,nvc-1),
     rectangle(vc,nvc,nlc,nsc,bc)  then 
        remove_character(all_text,caret,dparms,nlc+nvc-max(0,nsc),nsc,max(0,nvc-max(0,nsc))),
    }.
   
   
   
     *** (6.3) Home Answer
   
define WidgetAnswer
   home_answer
     (
       KeyboardState                ks,
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
   if nvc(*caret)=0 & nlc(*caret)=0 then not_handled([]) /* it's already home */
   else if shift(ks) 
         then if *caret is
               {
                stroke(vc,nvc,nlc)           then
                  caret <- if mb_visu_text(dparms)(0,*all_text) is 
                             {
                               failure    then rectangle("",0,0,-nlc-nvc,nlc+nvc),
                               success(s) then if s is (vc,_) then rectangle(vc,0,0,-nlc-nvc,nlc+nvc)
                             }; answer(dparms)
                rectangle(vc,nvc,lvc,nsc,bc) then 
                  caret <- if mb_visu_text(dparms)(0,*all_text) is 
                             {
                               failure    then rectangle(vc,0,0,-lvc-nvc+min(0,nsc),bc),
                               success(s) then if s is (new_vc,_) 
                                     then rectangle(vc,0,0,-lvc-nvc+min(0,nsc),bc)
                             }; answer(dparms)
               }
         else caret <- if mb_visu_text(dparms)(0,*all_text) is 
                          {
                            failure    then stroke("",0,0),
                             success(s) then if s is (vc,_) then stroke(vc,0,0)
                          }; answer(dparms).
   
   
      *** (6.4) End Answer
   
define WidgetAnswer
   end_answer
     (
       KeyboardState                ks,
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
   with n = length(*all_text),
   if nvc(*caret)+nlc(*caret)=n  /* it's already the end */
     then not_handled([])
     else 
      (if compute_visible_text(*all_text,dparms,true) is (new_vc,_,new_nvc) then 
      if shift(ks) 
       then if *caret is
             {
              stroke(vc,lvc,nvc)           then 
                caret <- rectangle(new_vc,new_nvc,n-new_nvc,length(*all_text)-lvc-nvc,nvc),
              rectangle(vc,lvc,nvc,nsc,bc) then 
                caret <- rectangle(new_vc,new_nvc,n-new_nvc,length(*all_text)-lvc-nvc+nsc,bc)
             }
       else caret <- stroke(new_vc,new_nvc,n-new_nvc)
       );  answer(dparms).

   
     *** (6.5) Del Answer
   
define WidgetAnswer
   del_answer
     (
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
   if *caret is
    {
     stroke(vc,nvc,nlc)            then
        remove_character(all_text,caret,dparms,nlc+nvc,1,nvc),
     rectangle(vc,nvc,nlc,nsc,bc)  then 
        remove_character(all_text,caret,dparms,nlc+nvc-max(0,nsc),nsc,max(0,nvc-max(0,nsc))),
    }.
   
   
   
     *** (6.6) Left Answer
   
define WidgetAnswer
   left_answer
     (
       Var(String)                                all_text,
       DrawParameters                               dparms,
       String                                           vc,
       Int32                                           nvc,
       Int32                                           nlc,
       Int32                                           nsc,
       Int32                                            bc,
       (String,Int32,Int32,Int32,Int32) -> One   new_caret
     ) =
         if nvc=0 & nlc=0 then not_handled([])  /* caret at far-left and no more invisible characters */
    else if nvc=0 & nlc>0   /* caret at far-left, and there is at least 1 invisible characters */
       then if mb_visu_text(dparms)(nlc-1,*all_text) is 
              {
               failure    then not_handled([]),
               success(s) then 
                if s is (new_vc,_) then new_caret(new_vc,0,nlc-1,nsc-1,bc); answer(dparms)
              }
       else new_caret(vc,nvc-1,nlc,nsc-1,bc);answer(dparms).
   
   
define WidgetAnswer
   left_answer
     (
       KeyboardState                ks,
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
     if shift(ks)
      then /* shift is down */
        if *caret is
         {
          stroke(vc,nvc,nlc)           then
             left_answer(all_text,dparms,vc,nvc,nlc,0,nvc,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                             caret <- rectangle(_vc,_nvc,_nlc,_nsc,_bc)),
          rectangle(vc,nvc,nlc,nsc,bc) then 
             left_answer(all_text,dparms,vc,nvc,nlc,nsc,bc,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                             caret <- if nsc-1=0
                                        then stroke(_vc,_nvc,_nlc)
                                        else rectangle(_vc,_nvc,_nlc,_nsc,_bc)),
         }
      else /* shift is up */
           /* The answer is equal for the both alternative of caret */
             left_answer(all_text,dparms,vc(*caret),nvc(*caret),nlc(*caret),0,0,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                          caret <- stroke(_vc,_nvc,_nlc)).
   

      *** (6.7) Right Answer
   
define WidgetAnswer
   right_answer
     (
       Var(String)                                 all_text,
       DrawParameters                                dparms,
       String                                            vc,
       Int32                                            nvc,
       Int32                                            nlc,
       Int32                                            nsc,
       Int32                                             bc,
       (String,Int32,Int32,Int32,Int32) -> One    new_caret
     ) =
          if nvc+nlc=length(*all_text) then not_handled([]) /* no more characters at right */
     else if nvc=length(vc) /* caret is at the right of the visible text  */
                            /*  => re-compute the visible text with one more invisible character at left*/
           then if mb_visu_text(dparms)(nlc+1,*all_text) is 
                  {
                   failure     then not_handled([]),
                   success(s)  then if s is (new_vc,new_nvc)
                        then new_caret(new_vc,new_nvc,nlc+1,nsc+1,bc); answer(dparms),
                  }
           else new_caret(vc,nvc+1,nlc,nsc+1,bc); answer(dparms).
   
define WidgetAnswer
   right_answer
     (
       KeyboardState                ks,
       Var(String)            all_text,
       Var(Caret)                caret,
       DrawParameters           dparms
     ) = 
     if shift(ks)
      then /* shift is down */
        if *caret is
         {
          stroke(vc,nvc,nlc)            then 
            right_answer(all_text,dparms,vc,nvc,nlc,0,nvc,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                             caret <- rectangle(_vc,_nvc,_nlc,_nsc,_bc)),
          rectangle(vc,nvc,nlc,nsc,bc)  then 
            right_answer(all_text,dparms,vc,nvc,nlc,nsc,nvc,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                             caret <- if nsc+1=0
                                        then stroke(_vc,_nvc,_nlc)
                                        else rectangle(_vc,_nvc,_nlc,_nsc,_bc)),
         }
      else /* shift is up */
           /* The answer is equal for the both alternative of caret */
            right_answer(all_text,dparms,vc(*caret),nvc(*caret),nlc(*caret),0,0,
                         (String _vc,Int32 _nvc,Int32 _nlc,Int32 _nsc,Int32 _bc) |->
                             caret <- stroke(_vc,_nvc,_nlc)).
   
  
   
   
   
      *** (6.8) Special keys answer manager
   
define WidgetAnswer
   keyboard_special_key_answer
     (
      KeyboardState                 ks,
      KeyboardSpecialKey           ksk,
      Var(String)             all_text,
      Var(Bool)                  inser,
      Var(Caret)                 caret,
      DrawParameters            dparms
     ) =
   if ksk is
     {
      escape      then not_handled([]),
      f1          then not_handled([]), 
      f2          then not_handled([]),
      f3          then not_handled([]),
      f4          then not_handled([]),
      f5          then not_handled([]),
      f6          then not_handled([]),
      f7          then not_handled([]),
      f8          then not_handled([]),
      f9          then not_handled([]), 
      f10         then not_handled([]),
      backspace   then backspace_answer(all_text,caret,dparms),
      home        then home_answer(ks,all_text,caret,dparms),
      end         then end_answer(ks,all_text,caret,dparms),
      tab         then not_handled([]), 
      enter       then not_handled([]),
      insert      then (inser <- not(*inser)); not_handled([]),
      del         then del_answer(all_text,caret,dparms),
      page_up     then not_handled([]),
      page_down   then not_handled([]),
      up          then not_handled([]),
      down        then not_handled([]),
      left        then left_answer(ks,all_text,caret,dparms),
      right       then right_answer(ks,all_text,caret,dparms)
     }. 

   
   *** (7) Input event handler
   
      *** (7.4) Insert character(s)
   
define WidgetAnswer
   insert_character
     (
      Var(String)       all_text,
      Var(Caret)           caret,
      DrawParameters      dparms,
      Word8              new_char,
      Int32              nb_left,    // 
      Int32                  nsc
     ) =
   with lt = if sub_string(*all_text,0,nb_left) is
              {
               failure    then "", 
               success(s) then s
              },
        rt = if sub_string(*all_text,nb_left+abs(nsc), length(*all_text)-nb_left-abs(nsc)) is
              {
               failure    then "",
               success(s) then s
              },
      (all_text <- lt+implode([new_char])+rt;
      if text_width(dparms)(vc(*caret)+implode([new_char])) >= width(dparms)
        then /* With the new character, the current visible text is wider than the width of the  */
             /* input. Two cases must be checked :                      */
             /*   (1) the caret is far-right of the input              */
             /*   (2) the caret is in the middle of the input_form     */
            if nvc(*caret)=length(vc(*caret))
            then (if compute_visible_text(*all_text,dparms,true) is (new_vc,vc_length,new_nvc) then
                 caret <- stroke(new_vc,new_nvc,length(*all_text)-new_nvc))
             else 
              caret <- if mb_visu_text(dparms)(nlc(*caret)+1,*all_text) is 
                       { 
                        failure    then stroke(*all_text,nvc(*caret)+1,nlc(*caret)),
                        success(s) then if s is (new_vc,nb_vc) then 
                         (print("new_vc="+new_vc+"\n");
                         stroke(new_vc,nvc(*caret),nlc(*caret)+1))
                       }
        else /* With the new character, current visible text is still smaller than the width of the */
             /* input */
            caret <- if mb_visu_text(dparms)(nlc(*caret),*all_text) is 
                       { 
                        failure    then stroke(*all_text,nvc(*caret)+1,nlc(*caret)),
                        success(s) then if s is (new_vc,nb_vc) then 
                         stroke(new_vc,min(nvc(*caret)+1-max(0,nsc),nb_vc),nlc(*caret))
                       });
      answer(dparms).

   
      *** (7.3) Keyboard Answer
   
define WidgetAnswer
   keyboard_answer
     (
       (Word8,String) -> Bool     ok_chars,
       Var(String)               all_text,
       Var(Caret)                   caret,
       Var(Bool)                    inser,
       DrawParameters              dparms,
       KeyboardState                   ks,
       KeyboardKey                      k
     ) = 
     if k is 
      {
       unknown      then not_handled([]),
       char(c)      then 
        if ok_chars(c,*all_text)
         then 
          if *inser 
           then 
             if *caret is
              {
               stroke(vc,nvc,nlc)           then 
                 insert_character(all_text,caret,dparms,c,nlc+nvc,0),
               rectangle(vc,nvc,nlc,nsc,bc) then 
                 insert_character(all_text,caret,dparms,c,nlc+nvc-max(0,nsc),nsc),
              }
           else /* Overwrite                                                             */
                /* ---------                                                             */
                /* Substitute the character at the left of the caret by the new one.    */
                /* When the caret is a stroke, it's the same effect than a selection of */
                /* one character with the right arrow. */
             if *caret is
              {
               stroke(vc,nvc,nlc)           then 
                 insert_character(all_text,caret,dparms,c,nlc+nvc-1,1),
               rectangle(vc,nvc,nlc,nsc,bc) then 
                 insert_character(all_text,caret,dparms,c,nlc+nvc-max(0,nsc),nsc),
              }
          else not_handled([]),  /* ok_chars=false */

       unicode(_,_) then alert, 
       special(ksk) then keyboard_special_key_answer(ks,ksk,all_text,inser,caret,dparms)
      }.
       
define One
   input_capture_keyboard
     (
       WidgetEventToolBox                   etb,
       (Word8,String) -> Bool       ok_caracters,
       Var(One -> Bool)          still_captured,
       Var(String)                     all_text,
       Var(Caret)                         caret,
       Var(Bool)                          inser,
       DrawParameters                    dparms
     ) = 
   still_captured <- 
        capture_keyboard
         ( 
          etb,
         (KeyboardState ks, KeyboardKey k) |->
            keyboard_answer(ok_caracters,all_text,caret,inser,dparms,ks,k),
          (List(WidgetRectangle) -> One redraw) |-> redraw([input_rect(dparms)])).

   
   
      *** (7.2) Compute caret position after a left-click.
   
   The purpose is to put  the caret on the glue between 2 letters and  to avoid to see it
   on a  letter. The final position will  be on the left  side of the letter  if the click
   occured on the left-half of the letter, on the right side if it was on the right-half.
   
define Int32
   caret_position
     (
       Int32                mx,      // mouse x
       List(Word8)         text,      // visible text, not necessary all
       Int32               pos,      // position in pixels of the caret
       Int32           nb_char,      // initialize at 0
       Word8 -> Int32    char_w,
     ) =
   if text is
    {
     []        then nb_char,
     [h . t]   then 
       with new_pos  = pos+char_w(h),
            new_pos2 = pos+(char_w(h)/2),
            if mx < new_pos2 then nb_char
       else if mx < new_pos  then nb_char+1
       else caret_position(mx,t,new_pos,nb_char+1,char_w)
    }.
   

      *** (7.1) Main function

define (KeyboardState ks, WidgetMouseCapturedEvent e) -> WidgetAnswer
   mouse_captured_handler
     (
       Var(String)         all_text,
       Var(Caret)              caret,
       DrawParameters         dparms,
       Int32                     mx,
     ) =
   (KeyboardState ks, WidgetMouseCapturedEvent e) |->
      if e is
        {
         moved(x,y)      then
                if x=<0 & nlc(*caret)=0 then not_handled([])
           else if x=<0 & nlc(*caret)>0 then
                if mb_visu_text(dparms)(nlc(*caret)-1,*all_text) is
                  {
                   failure    then not_handled([]),
                   success(s) then if s is (new_vc,_) then
                    caret <- if *caret is
                               {
                                stroke(vc,nvc,nlc)           then rectangle(new_vc,0,nlc-1,-1,nvc),
                                rectangle(vc,nvc,nlc,nsc,bc) then rectangle(new_vc,0,nlc-1,nsc-1,bc)
                               }; answer(dparms)
                  }
            else if x>=width(dparms) & nlc(*caret)+length(vc(*caret))=length(*all_text) 
                 then not_handled([])            
                 else if x>=width(dparms) & nvc(*caret)=length(vc(*caret)) then
                (if mb_visu_text(dparms)(nlc(*caret)+1,*all_text) is
                  {
                   failure    then not_handled([]),
                   success(s) then if s is (new_vc,new_nvc) then
                    caret <- if *caret is
                               {
                                stroke(vc,nvc,nlc)           then rectangle(new_vc,new_nvc,nlc+1,1,nvc),
                                rectangle(vc,nv,nlc,nsc,bc)  then rectangle(new_vc,new_nvc,nlc+1,nsc+1,bc)
                               }; answer(dparms)
                  })
             else(with cp=if *caret is
                             {
                              stroke(_,nvc,_)          then 
                               caret_position(mx,explode(vc(*caret)),0,0,char_size(dparms)),
                              rectangle(_,nvc,_,nsc,_) then nvc 
                             },
                       ptw=caret_position(x,explode(vc(*caret)),0,0,char_size(dparms)),
                    if cp=ptw then not_handled([])
               else if ptw>cp then 
                       caret <- if *caret is
                                  {
                                   stroke(vc,nvc,nlc)           then rectangle(vc,nvc+1,nlc,1,nvc),
                                   rectangle(vc,nvc,nlc,nsc,bc) then rectangle(vc,nvc+1,nlc,nsc+1,bc),
                                  };answer(dparms)
               else /* x<ptw */ 
                      caret <- if *caret is
                                 {
                                  stroke(vc,nvc,nlc)           then rectangle(vc,nvc-1,nlc,-1,nvc),
                                  rectangle(vc,nvc,nlc,nsc,bc) then rectangle(vc,nvc-1,nlc,nsc-1,bc),
                                  };answer(dparms)),
            
         liberated(x,y)  then not_handled([])
        }.
   
define WidgetAnswer
   input_form_handler
     (
       WidgetEventToolBox                  etb,
       WidgetNormalEvent                     e,
       (Word8,String) -> Bool      ok_caracters,
       Var(One -> Bool)         still_captured,
       Var(String)                    all_text,
       Var(Caret)                        caret,
       Var(Bool)                         inser,
       DrawParameters                   dparms
     ) =
   if e is
    {
     mouse_gone               then not_handled([]),
     mouse_move(ks,_,_)       then not_handled([]),
     mouse_click(ks,mc,mx,my) then 
       if mc is
        {
         left_down   then 
           caret <- stroke(vc(*caret),
                           caret_position(mx,explode(vc(*caret)),0,0,char_size(dparms)),
                           nlc(*caret));
           input_capture_keyboard(etb,ok_caracters,still_captured,all_text,caret,inser,dparms);
           forget(capture_mouse(etb,mouse_captured_handler(all_text,caret,dparms,mx)));
           answer(dparms),
   
         left_up     then not_handled([]),
         middle_down  then not_handled([]),
         middle_up    then not_handled([]),
         right_down  then not_handled([]),
         right_up    then not_handled([])
        }
    }.
      
   
   
   
   
   *** (8) The widget
   
      *** (8.3) Write the characters
   
define Int32
   write_text
     (
       WidgetDrawToolBox       dtb,
       InputStyle              ifs,
       String             visu_text
     ) =
   if ifs is
     {
      input_common_style(p,_)  then 
        with fs = *font_v(p),
        draw(dtb)(visu_text,fs,*font_color_v(p),
                  position(0,word8_to_Word32(height(get_font_info(fs))))),
     }.
   
      *** (8.2) Draw the selection rectangle
   
define One
   draw_selection_rectangle
     (
       WidgetDrawToolBox           dtb,
       String                       vc,
       Int32                       nvc,
       Int32                       nsc,
       DrawParameters           dparms,
       RGB                 caret_color,
       RGB                  font_color
     ) =
     if sub_string(vc,max(0,nvc-max(0,nsc)),min(abs(nsc),length(vc))) is
       {
        failure             then unique, 
        success(sel_chars)  then 
         with left_pos= if sub_string(vc,0,nvc-max(0,nsc)) is
                         {
                          failure     then 0,
                          success(s)  then text_width(dparms)(s),
                         },
        draw(dtb)(rect(left_pos,1,left_pos+text_width(dparms)(sel_chars),total_font_height(dparms)),
                  lighten(caret_color));
        forget(draw(dtb)(sel_chars,font(dparms),darken(font_color),
                         position(left_pos,
                                  word8_to_Word32(height(get_font_info(font(dparms)))))))
       }.

      *** (8.1) Create input
   
define Widget
   create_input
     (
       InputStyle                    ifs,
       Var(String)              all_text,
       (Word8,String) -> Bool    ok_chars
     ) =
   with /* true means that insert is running */
          inser  = var((Bool) true),       
        /* to check if the text-input is active or not and drawing the caret */
          still_captured = var((One -> Bool) (One u) |-> false),
   
        /* initialize caret value : as the visible text is computed reversing the all_text,  */
        /* the number of invisible characters on the left_side of the input is the       */
        /* difference between the length of the both string all-text and visible-text         */
        caret =
          if compute_visible_text(*all_text,to_draw_parms(ifs),true) is (visu_text,visu_length,_) then 
                with lvt = length(visu_text),
                var((Caret) stroke(visu_text,lvt,length(*all_text)-lvt)),
   
   create_widget(
   /* set child positions */
   (WidgetPositionToolBox ptb) |-> unique,
   
   /* get_size */
   (One u) |-> (width(ifs),
                if ifs is
                  {
                   input_common_style(p,_) then 
                     with fi = get_font_info(*font_v(p)),
                      word8_to_Word32(height(fi))+word8_to_Word32(depth(fi)),
                  }+1),
   
   /* redraw */
   (WidgetDrawToolBox dtb) |-> 
       draw(dtb)(input_rect(ifs),
                 if ifs is
                   {
                    input_common_style(p,_)  then *background_color_v(p)
                   });
            forget(write_text(dtb,ifs,vc(*caret)));
   
             with data = 
                    if ifs is
                      {
                       input_common_style(p,_) then 
                          with fi = get_font_info(*font_v(p)),
                          (word8_to_Word32(height(fi))+word8_to_Word32(depth(fi)),*background_color_v(p))
                      },
        if data is (total_font_height,bg_color) then 
        (if (*still_captured)(unique)
         then 
             if *caret is
               {
                 stroke(vc,nvc,nlc)         then 
                  with x_caret = x_caret(vc,nvc,ifs),
                  draw(dtb)(rect(x_caret,1,x_caret+1,total_font_height),caret_color(ifs)),
                 rectangle(vc,nvc,nlc,nsc,bc)   then 
                   draw_selection_rectangle
                     (dtb,vc,nvc,nsc,to_draw_parms(ifs),caret_color(ifs),font_color(ifs))
               }
         else
             if compute_visible_text(*all_text,to_draw_parms(ifs),false) is (_,visu_length,_) then 
//              draw(dtb)(rect(visu_length+1,1,visu_length+2,total_font_height),lighten(caret_color(ifs)))
  
             (draw(dtb)(rect(visu_length+1,1,visu_length+4,total_font_height),caret_color(ifs));
              draw(dtb)(rect(visu_length+2,2,visu_length+3,total_font_height-1),bg_color))
         ),
   
   /* duplicate */
   (One u) |-> create_input(ifs,all_text,ok_chars),
   
   /* Change size */
   (Int32 w, Int32 h) |-> unique,
   
   /* event handling */
   (WidgetEventToolBox etb, WidgetNormalEvent e) |->
      input_form_handler(etb,e,ok_chars,still_captured,all_text,caret,inser,to_draw_parms(ifs)),
   
   /* monitoring */
   (List(WidgetRegistration)) []
   ).
   
   
   
   
public define Widget
   create_text_input
     (
       InputStyle       ifs,
       Var(String)        t
     ) =
   create_input(ifs,t,(Word8 c, String s) |-> true).

   
   
   
define Bool
   integer_characters
    (
      Word8    c
    ) =
    with _c = word8_to_Word32(c),
    (48 =< _c) & (_c =< 57).

public define Widget
   create_integer_input
     (
       InputStyle         ifs,
       Var(Int32)           i
     ) =
   create_input(ifs,var((String) integer_to_string(*i)),(Word8 c,String s) |-> integer_characters(c)).

   
   
   
define Bool
   float_characters
    (
      Word8    c,
      String  s
    ) =
    with _c = word8_to_Word32(c),
    ( (48 =< _c) & (_c =< 57) )
   | (if c = ',' | c = '.'
      then (not(member(',',s)) & not(member('.',s)))
      else false).
   
   
public define Widget
   create_float_input
     (
       InputStyle        ifs,
       Var(Float)          f,
       Int32               n // the number of decimals
     ) =
    create_input(ifs,var((String) float_to_string(*f,n)),(Word8 c,String s) |-> float_characters(c,s)).