%%
%% This is file `l3pdffield-testphase.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3pdffield.dtx  (with options: `package')
%% 
%% Copyright (C) 2019-2021 The LaTeX Project
%% 
%% It may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License (LPPL), either version 1.3c of
%% this license or (at your option) any later version.  The latest
%% version of this license is in the file:
%% 
%%    https://www.latex-project.org/lppl.txt
%% 
%% This file is part of the "LaTeX PDF management testphase bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%% 
%% File: l3pdfpdffield-checkbox.dtx
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{l3pdffield-testphase}{2021-03-17}{0.95c}%
  {form field checkboxes}
\csname HyField@NeedAppearancesfalse\endcsname % suppress NeedAppearances
\str_new:N \l__pdffield_field_name_str
\str_new:N \l__pdffield_tmpa_str
\str_new:N \l__pdffield_name_tmpa_str
\tl_new:N \l__pdffield_keys_tmpa_tl
\msg_new:nnn {pdffield}{no-period}
  {
    The~field~name~`#1`~contains~a~period. \\
    This~is~not~allowed. `
  }
\msg_new:nnn {pdffield}{empty-name}
  {
    The~field~name~is~empty. \\
    This~is~not~allowed. `
  }
\msg_new:nnn {pdffield}{appearance-missing}
  {
    The~appearance~`#1`~is~missing~for~the~#2~appearance.
  }
\msg_new:nnn {pdffield}{field-keys-ignored}
  {
    The~field~`#1`~is~already~initialized\\
    The~field~keys~`#2`~are~ignored.
  }

\bitset_new:Nn \l__pdffield_Ff_bitset
 {
    ReadOnly          = 1,
    Required          = 2,
    NoExport          = 3,
    Multiline         = 13,%Tx
    Password          = 14,
    NoToggleToOff     = 15,%Btn, radio button
    Radio             = 16,%Btn: Radio:    15=1, 16=0
    Pushbutton        = 17,%Btn: Checkbox: 15=0, 16=0
                           %Btn: Pushbutton: 16=1
    Combo             = 18,%Ch: Combo=1 List=0
    Edit              = 19,%Ch, Combo=1 -> + edit field
    Sort              = 20,%Ch, not relevant for view...
    FileSelect        = 21,%Tx
    MultiSelect       = 22,%Ch
    DoNotSpellCheck   = 23,%Tx, Ch (if Combo + Edit set)
    DoNotScroll       = 24,%Tx
    Comb              = 25,%Tx, requires MaxLen in dict
    RadiosInUnison    = 26,%Btn Radio
    RichText          = 26,%Tx
    CommitOnSelChange = 27
  }

\bitset_new:Nn \l__pdffield_F_bitset
  {
    Invisible      = 1,
    Hidden         = 2,
    Print          = 3,
    NoZoom         = 4,
    NoRotate       = 5,
    NoView         = 6,
    ReadOnly       = 7,
    Locked         = 8,
    ToggleNoView   = 9,
    LockedContents = 10
  }
\pdfdict_new:n   {l__pdffield/checkbox/field}
\pdfdict_put:nnn {l__pdffield/checkbox/field}{FT}{/Btn}
\cs_new_protected:Npn \__pdffield_checkbox_field_new:n #1
  {
      \group_begin:
      \pdf_object_new:nn {__pdffield_checkbox/field/#1}      {dict}
      \pdf_object_new:nn {__pdffield_checkbox/field/#1/Kids} {array}
      \seq_new:c {g__pdffield_checkbox/field/#1/Kids_seq}
      \hook_gput_code:nnn {shipout/lastpage}{pdffield} %xetex needs this ...
        {
          \pdf_object_write:nx {__pdffield_checkbox/field/#1/Kids}
            {
              \seq_use:cn{g__pdffield_checkbox/field/#1/Kids_seq}{~}
            }
        }
      \pdfdict_put:nnn {l__pdffield/checkbox/field}{T}{(#1)}
     % V,DV are names describing the appearance. With checkboxes
     % the values /Yes and /Off are used.
     % this values are taken from the outside
      \pdfdict_put:nnx {l__pdffield/checkbox/field}
         {Kids}
         {
           \pdf_object_ref:n {__pdffield_checkbox/field/#1/Kids}
         }
      \bitset_set_false:Nn \l__pdffield_Ff_bitset  {Radio}
      \bitset_set_false:Nn \l__pdffield_Ff_bitset  {Pushbutton}
      \pdfdict_put:nnx {l__pdffield/checkbox/field}
        {Ff}
        {\bitset_to_arabic:N \l__pdffield_Ff_bitset }
      \pdfdict_if_empty:nF{l__pdffield/checkbox/field/AA}
        {
          \pdfmeta_standard_verify:nT
            {annot_widget_no_AA}
            {
              \pdfdict_put:nnx
                {l__pdffield/checkbox/field}
                {AA}
                {<<\pdfdict_use:n {l__pdffield/checkbox/field/AA}>>}
            }
        }
      \pdf_object_write:nx {__pdffield_checkbox/field/#1} { \pdfdict_use:n {l__pdffield/checkbox/field} }
      \pdfmanagement_add:nnx
        { Catalog / AcroForm }
        { Fields }
        {\pdf_object_ref:n {__pdffield_checkbox/field/#1} }
      \group_end:
  }

\pdfdict_new:n   {l__pdffield/checkbox/annot}
\pdfdict_put:nnn {l__pdffield/checkbox/annot}{Subtype}{/Widget}
\cs_new_protected:Npn \__pdffield_checkbox_annot_add:nnnn #1 #2 #3 #4 %name, wd, ht, dp,
  {
    \group_begin:
    \pdfdict_put:nnx {l__pdffield/checkbox/annot}{AP}{<<\pdfdict_use:n{l__pdffield/checkbox/annot/AP}>>}
    \pdfmeta_standard_verify:nF
      {annot_flags}
      {
        \bitset_set_true:Nn  \l__pdffield_F_bitset {Print}
        \bitset_set_false:Nn \l__pdffield_F_bitset {Hidden}
        \bitset_set_false:Nn \l__pdffield_F_bitset {Invisible}
        \bitset_set_false:Nn \l__pdffield_F_bitset {NoView}
      }
    \pdfdict_if_empty:nF{l__pdffield/checkbox/annot/AA}
      {
        \pdfmeta_standard_verify:nT
         {annot_widget_no_AA}
         {
            \pdfdict_put:nnx
              {l__pdffield/checkbox/annot}
              {AA}
              {<<\pdfdict_use:n {l__pdffield/checkbox/annot/AA}>>}
         }
      }
    \pdfdict_put:nnx {l__pdffield/checkbox/annot}{F}{ \bitset_to_arabic:N \l__pdffield_F_bitset }
    \pdfdict_set_eq:nn {l__pdfannot/widget}{l__pdffield/checkbox/annot}
    \pdfannot_dict_put:nnx {widget}{Parent}{\pdf_object_ref:n{__pdffield_checkbox/field/#1}}
    \mode_leave_vertical:
    \hbox_to_wd:nn
      { #2  }
      {
        \rule [-#4]{0pt}{\dim_eval:n{#3+#4} }
        \pdfannot_widget_box:nnn
           { #2 }
           { #3 }
           { #4 }
         \hfill
      }
    \seq_gput_right:cx {g__pdffield_checkbox/field/#1/Kids_seq}{ \pdfannot_box_ref_last:}
    \group_end:
  }

\cs_new_protected:Npn \pdffield_store_appearance:nn #1 #2
  {
     \pdfxform_new:nnn {__pdffield_#1}{}{#2}
  }

\cs_new_protected:Nn \__pdffield_store_default_appearances:
  {
     \pdffield_store_appearance:nn {checkbox/default/Yes}
       {
         \normalsize
         \fboxsep 0pt
         \framebox
           [ \dim_eval:n { \box_ht:N\strutbox+\box_dp:N\strutbox } ]
           { \texttimes \strut }
       }
     \pdffield_store_appearance:nn {checkbox/default/Off}
       {
         \normalsize
         \fboxsep 0pt
         \framebox
           [ \dim_eval:n { \box_ht:N\strutbox+\box_dp:N\strutbox } ]
           { \phantom{\texttimes} \strut }
       }
  }

\__pdffield_store_default_appearances:
\pdfdict_new:n   {l__pdffield/checkbox/annot/AP}

\cs_new_protected:Npn \__pdffield_checkbox_add:n #1
  {
    \group_begin:
    \keys_set_filter:nnnN {pdffield / checkbox }{field}{#1}\l__pdffield_keys_tmpa_tl
    \str_if_empty:NT \l__pdffield_field_name_str
      {
        \msg_error:nn {pdffield}{empty-name}
      }
    \exp_args:Nx
      \pdf_object_if_exist:nTF {__pdffield_checkbox/field/\l__pdffield_field_name_str}
       {
          \tl_if_empty:NF \l__pdffield_keys_tmpa_tl
           {
             \msg_warning:nnxx
               {pdffield}
               {field-keys-ignored}
               {\l__pdffield_field_name_str}
               {\l__pdffield_keys_tmpa_tl}
           }
       }
       {
         \keys_set:nV { pdffield/checkbox } \l__pdffield_keys_tmpa_tl
         \exp_args:No
         \__pdffield_checkbox_field_new:n {\l__pdffield_field_name_str}
       }
    \exp_args:No
      \__pdffield_checkbox_annot_add:nnnn
        {\l__pdffield_field_name_str}
        {\l__pdffield_annot_wd_tl }
        {\l__pdffield_annot_ht_tl }
        {\l__pdffield_annot_dp_tl }
    \group_end:
  }
\tl_new:N \l__pdffield_annot_ht_tl
\tl_new:N \l__pdffield_annot_wd_tl
\tl_new:N \l__pdffield_annot_dp_tl

\keys_define:nn { pdffield / checkbox }
  {
    ,width  .tl_set:N = \l__pdffield_annot_wd_tl
    ,height .tl_set:N = \l__pdffield_annot_ht_tl
    ,depth  .tl_set:N = \l__pdffield_annot_dp_tl
    ,width  .initial:n = \normalbaselineskip
    ,height .initial:n = \normalbaselineskip
    ,depth  .initial:n = 0pt
  }
\keys_define:nn { pdffield / checkbox }
  {
    ,name .code:n =
      {
        \pdf_string_from_unicode:nnN {utf8/string-raw}{#1}\l__pdffield_field_name_str
        \str_if_in:NnT \l__pdffield_field_name_str {.}
          {
            \msg_error:nnx {pdffield}{no-period}{\l__pdffield_field_name_str}
          }
        \str_if_empty:NT\l__pdffield_field_name_str
          {
            \msg_error:nn {pdffield}{empty-name}
          }
      }
    ,name .value_required:n = true
    ,name .initial:n = checkbox
    ,altname .code:n =
      {
        \pdf_string_from_unicode:nnN {utf8/string}{#1}\l__pdffield_name_tmpa_str
        \pdfdict_put:nnx { l__pdffield/checkbox/field }{TU}{\l__pdffield_name_tmpa_str}
      }
    ,altname .groups:n = {field}
    ,mappingname .code:n =
      {
        \pdf_string_from_unicode:nnN {utf8/string}{#1}\l__pdffield_name_tmpa_str
        \pdfdict_put:nnx { l__pdffield/checkbox/field }{TM}{\l__pdffield_name_tmpa_str}
      }
    ,mappingname .groups:n = {field}
  }

\keys_define:nn { pdffield / checkbox }
 {
   ,checked .choice:
   ,checked / false .code:n =
     {
       \pdfdict_put:nnn {l__pdffield/checkbox/field}{V}{/Off}
       \pdfdict_put:nnn {l__pdffield/checkbox/annot}{AS}{/Off}
       \pdfdict_put:nnn {l__pdffield/checkbox/field}{DV}{/Off}
     }
   ,checked / true .code:n =
     {
       \pdfdict_put:nnn {l__pdffield/checkbox/field}{V}{/Yes}
       \pdfdict_put:nnn {l__pdffield/checkbox/annot}{AS}{/Yes}
       \pdfdict_put:nnn {l__pdffield/checkbox/field}{DV}{/Yes}
     }
   ,checked .default:n = {true}
   ,checked .initial:n = {false}
 }
\keys_define:nn { pdffield / checkbox }
  {
    ,setfieldflags .code:n =
      {
          \clist_map_inline:nn {#1}
           {
             \bitset_set_true:Nn \l__pdffield_Ff_bitset {##1}
           }
      }
    ,setfieldflags .groups:n = {field}
    ,unsetfieldflags .code:n =
      {
          \clist_map_inline:nn {#1}
           {
             \bitset_set_false:Nn \l__pdffield_Ff_bitset {##1}
           }
      }
    ,unsetfieldflags .groups:n = {field}
    ,setannotflags .code:n =
      {
          \clist_map_inline:nn {#1}
           {
             \bitset_set_true:Nn \l__pdffield_F_bitset {##1}
           }
      }
    ,unsetannotflags .code:n =
      {
          \clist_map_inline:nn {#1}
           {
             \bitset_set_false:Nn \l__pdffield_F_bitset {##1}
           }
      }
  }

\keys_define:nn { pdffield / checkbox }
  {
    appearance .code:n = %value is a name of an appearance
      {
        \pdfxform_if_exist:nTF {  __pdffield_#1/Yes }
          {
            \pdfdict_put:nnn {l__pdffield/checkbox/annot/AP}
              {N}
              {
                 <<
                    /Yes ~ \pdfxform_ref:n { __pdffield_#1/Yes}
                    /Off ~ \pdfxform_ref:n { __pdffield_#1/Off}
                 >>
              }
          }
          {
            \msg_error:nnnn{pdffield}{appearance-missing}{#1}{normal}
          }
      },
    appearance .initial:n = checkbox/default,
  }

\keys_define:nn { pdffield / checkbox }
  {
    rollover-appearance .code:n = %value is a name of an appearance
      {
       \tl_if_empty:nTF {#1}
         {
           \pdfdict_remove:nn {l__pdffield/checkbox/annot/AP} {R}
         }
         {
           \pdfxform_if_exist:nTF {  __pdffield_#1/Yes }
             {
               \pdfdict_put:nnn {l__pdffield/checkbox/annot/AP}
                 {R}
                 {
                    <<
                       /Yes ~ \pdfxform_ref:n { __pdffield_#1/Yes}
                       /Off ~ \pdfxform_ref:n { __pdffield_#1/Off}
                    >>
                 }
              }
              {
                \msg_warning:nnnn{pdffield}{appearance-missing}{#1}{rollover}
              }

         }
      },
  }

\keys_define:nn { pdffield / checkbox }
  {
    down-appearance .code:n = %value is a name of an appearance
      {
       \tl_if_empty:nTF {#1}
         {
           \pdfdict_remove:nn {l__pdffield/checkbox/annot/AP} {D}
         }
         {
           \pdfxform_if_exist:nTF {  __pdffield_#1/Yes }
            {
              \pdfdict_put:nnn {l__pdffield/checkbox/annot/AP}
                {D}
                {
                   <<
                      /Yes ~ \pdfxform_ref:n { __pdffield_#1/Yes}
                      /Off ~ \pdfxform_ref:n { __pdffield_#1/Off}
                   >>
                }
            }
            {
              \msg_warning:nnnn{pdffield}{appearance-missing}{#1}{down}
            }
         }
      },
  }
\pdfdict_new:n {l__pdffield/checkbox/annot/AA}
\pdfdict_new:n {l__pdffield/checkbox/field/AA}

\cs_new_protected:Npn \__pdffield_define_AAaction_key:nnn #1 #2 #3 %#1 key, #2 pdf, #3 dict
  {
    \keys_define:nn { pdffield / checkbox }
      {
         #1 .code:n =
           {
             \pdf_string_from_unicode:nnN {utf8/string}{##1}\l__pdffield_tmpa_str
             \str_if_empty:NTF \l__pdffield_tmpa_str
               {
                 \pdfdict_remove:nn {l__pdffield/checkbox/#3/AA}{#2}
               }
               {
                 \pdfdict_put:nnx {l__pdffield/checkbox/#3/AA}
                  {#2}
                  {<</S/JavaScript/JS\l__pdffield_tmpa_str>>}
               }
           },
        #1 .groups:n = {#3}
      }
  }
\__pdffield_define_AAaction_key:nnn {keystroke}{K}{field}
\__pdffield_define_AAaction_key:nnn {format}   {F}{field}
\__pdffield_define_AAaction_key:nnn {validate} {V}{field}
\__pdffield_define_AAaction_key:nnn {calculate}{C}{field}
\__pdffield_define_AAaction_key:nnn {onfocus}  {Fo}{annot}
\__pdffield_define_AAaction_key:nnn {onblur}   {Bl}{annot}
\__pdffield_define_AAaction_key:nnn {onmousedown}{D}{annot}
\__pdffield_define_AAaction_key:nnn {onmouseup}{U}{annot}
\__pdffield_define_AAaction_key:nnn {onenter}  {E}{annot}
\__pdffield_define_AAaction_key:nnn {onexit}   {X}{annot}
\cs_set_eq:NN \pdffield_checkbox:n \__pdffield_checkbox_add:n

\cs_new_protected:Npn \pdffield_setup:nn #1 #2
 {
   \keys_set:nn {pdffield / #1 } {#2}
 }

\endinput%
%%%%
%% Appearance
%%checkbox
%% 
%%
%% End of file `l3pdffield-testphase.sty'.
