% Author..........: C. Pierquet
% licence.........: Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{customenvs-pastableur}[2026/09/05 0.1.2 Small macros, patchs for pas-tableur]

%====HISTORY
% v 0.1.2  Mise ne conformité avec la version 3.0.1 de S. PAsquet
% v 0.1.1  Patch pour lualatex/pdflatex
% v 0.1.0  Initial version subpackage of customenvs

%====PACKAGES
\RequirePackage{pas-tableur}
\RequirePackage{lucide-icons}

%====PATCH \hlvbox multimoteur
\@ifundefined{helvbx}%
  {%
    \newcommand\helvbx{%
      \@ifundefined{setmainfont}%
        {\fontfamily{phv}\selectfont}%  % pdfLaTeX → comportement original
        {\sffamily}%            % LuaLaTeX/fontspec → sans-serif bold courant
    }%
  }%
  {%
    \renewcommand\helvbx{%
      \@ifundefined{setmainfont}%
        {\fontfamily{phv}\selectfont}%  % pdfLaTeX → comportement original
        {\sffamily}%            % LuaLaTeX/fontspec → sans-serif bold courant
    }%
  }%

%====PATCH baselineskip pour le multilignes (à conserver)
\def\pastableur@baselineskip{1.2em}
\providecommand*\tabcellskip[1]{\def\pastableur@baselineskip{#1}}

\renewcommand*\celtxt@iii[4]
  {%
     \ifPAS@celtxt@ajust
      \IfStrEq{\cmdPAS@celtxt@align}{right}{\def\postab@align{r}}{}
      \IfStrEq{\cmdPAS@celtxt@align}{left}{\def\postab@align{l}}{}
      \IfStrEq{\cmdPAS@celtxt@align}{center}{\def\postab@align{c}}{}
      \node[text width=\dimexpr\cmdPAS@celtxt@width-4pt, align=\cmdPAS@celtxt@align, color=\cmdPAS@celtxt@color, font=\cmdPAS@celtxt@font, execute at begin node=\setlength{\baselineskip}{\pastableur@baselineskip}] at (cell#2-#3) {\begin{tabular}{@{}\postab@align @{}}#4\end{tabular}};% 
     \else
      \celtxt@catcoderegime@end
      \IfBeginWith{#4}{=}{\let\pastab@style\texttt}{\let\pastab@style\@empty}
      \node[text width=\dimexpr\cmdPAS@celtxt@width-4pt,align=\cmdPAS@celtxt@align, color=\cmdPAS@celtxt@color, font=\cmdPAS@celtxt@font, execute at begin node=\setlength{\baselineskip}{\pastableur@baselineskip}] at (cell#2-#3) {\pastab@style{#4}};%
    \fi
  }
  
%====PATCH GRILLE (à conserver)
\renewcommand\pastableur@grid[4] % #1 : width, #2 : name, #3 : numline, #4 : coord.
{%
  \node[celempty,below,minimum height=\line@height,minimum width=#1] (cell#2-#3) at (#4) {};%
  \draw[graySepCell] ($(cell#2-#3.south west)+(0.2pt,0)$) -- (cell#2-#3.south east) -- ($(cell#2-#3.north east)+(0,-0.2pt)$);%
}

%====PATCH CELLULE (à conserver)
\renewcommand\pastab@draw@cell[5]
% #1 : \first@width, #2 : \first@name, #3 : 0=colonne1 / 1=autres colonnes / 
% 2 = ligne 1, #4 : coord., #5 : contenu
{%
  \ifnum#3=0%
    \tikzstyle{cellstyle} = [celhead,minimum width={#1},minimum height=\line@height]%
    \def\pastableur@coord{(0,0)}%
  \else%
    \ifnum#3=1%
      \tikzstyle{cellstyle} = [celhead,right,minimum width={#1},minimum height=\line@height]%
    \else
      \ifnum#3=2%
        \tikzstyle{cellstyle} = [below left,celhead,minimum width={#1},minimum height=\line@height]
      \else%
        \tikzstyle{cellstryle} = [below,celhead,minimum width={#1},minimum height=\line@height]
      \fi%
    \fi%
    \def\pastableur@coord{(#4)}%
  \fi%
  \node[cellstyle] (#2) at \pastableur@coord {\helvbx#5};%
  \fill[grayBottomCell] (#2.south west) rectangle (#2.north east) ;%
  \fill[grayTopCell] (#2.north west) rectangle ($0.5*(#2.south east)+0.5*(#2.north east)$) ;%
  \node[minimum width={#1},minimum height=\line@height,cellstyle] at \pastableur@coord {\helvbx#5};%
}

%====FUSION CELLULES (à conserver)
\defKV[celparam]{align=\def\pastableurfusalign{#1},width=\def\pastableurfuswidth{#1}}
\setKVdefault[celparam]{align=left,width={\col@width}}

\NewDocumentCommand\celfusion{ O{} m m m }{%OK
  \restoreKV[celparam]%
  \setKV[celparam]{#1}%
  \IfStrEq{#4}{}%
    {%
      \draw[draw=none,fill=white] ($(cell#2.south west)+(0.2pt,0.2pt)$) rectangle
                                  ($(cell#3.north east) + (-0.2pt,-0.2pt)$);
    }%
    {%
      \draw[draw=none,fill=white] ($(cell#2.south west)+(0.2pt,0.2pt)$) rectangle
                                  ($(cell#3.north east) + (-0.2pt,-0.2pt)$) 
                                  node[midway,text width=\dimexpr\pastableurfuswidth-4pt,align=\pastableurfusalign] {#4};
    }%
}

%====POINTILLÉS POUR LIGNES (à conserver)
\NewDocumentCommand\celnumbreak{ m m m }{%
  \def\numpointilles{#1}
  \xdef\numavant{\inteval{\numpointilles-1}}%
  \def\numdebut{#2}\def\numfin{#3}
  \foreach \num@line [remember=\num@line as \lastnum (initially \numavant)] in {\numpointilles}
    {%
      \pastab@draw@cell{\numline@width}{numline\num@line}{3}{numline\lastnum.south east}{\dots}%
    }%
  \foreach \num@line [remember=\num@line as \lastnum (initially \numpointilles)] in {\numdebut,\numfin}
    {%
      \pastab@draw@cell{\numline@width}{numline\num@line}{3}{numline\lastnum.south east}{\num@line}%
    }%
}

%====PATCH \multiSeleC (à conserver)
\renewcommand*\multiSelec[2]{%
  \fill[blueSelec,opacity=0.5] ($(cell#1.north west)+(0.2pt,-0.2pt)$)
                               rectangle ($(cell#2.south east)+(-0.2pt,0.2pt)$);%
  \fill[black] ($(cell#2.south east)+(-0.5pt,0.5pt)$) rectangle +(3pt,-3pt);%
  %==> En-têtes colonnes
  \StrChar{#1}{1}[\str@start]%
  \StrChar{#2}{1}[\str@end]%
  \StrBetween{\col@names}{\str@start}{\str@end}[\str@col]%
  \edef\str@col{\str@start\str@col\str@end}%
  \StrLen{\str@col}[\nb@columns]%
  \foreach \i in {1,...,\nb@columns}{%
    \StrChar{\str@col}{\i}[\name]%
    \pastableur@colorcell{\name}{1}%
    \node at (\name) {\helvbx\name};%
  }%
  %==> En-têtes lignes
  \StrGobbleLeft{#1}{2}[\first@num]%
  \StrGobbleLeft{#2}{2}[\second@num]%
  \foreach \i in {\first@num,...,\second@num}{%
    \pastableur@colorcell{numline\i}{0}%
    \GtblrFetchHeight{\i}%
    \node[celhead,minimum width=\numline@width,minimum height=\l@Gtblr@rowht] at (numline\i) {\helvbx\i};%
  }%
  \draw[very thick] (cell#1.south west) rectangle (cell#1.north east);%
}

%====PATCH \multimultiSelec (à conserver)
\renewcommand*\multimultiSelec[3]{%
  \fill[blueSelec,opacity=0.5] ($(cell#1.north west)+(0.2pt,-0.2pt)$)
                               rectangle ($(cell#3.south east)+(-0.2pt,0.2pt)$);%
  \fill[black] ($(cell#3.south east)+(-0.5pt,0.5pt)$) rectangle +(3pt,-3pt);%
  %==> En-têtes colonnes
  \StrChar{#1}{1}[\str@start]%
  \StrChar{#2}{1}[\str@end]%
  \StrBetween{\col@names}{\str@start}{\str@end}[\str@col]%
  \edef\str@col{\str@start\str@col\str@end}%
  \StrLen{\str@col}[\nb@columns]%
  \foreach \i in {1,...,\nb@columns}{%
    \StrChar{\str@col}{\i}[\name]%
    \pastableur@colorcell{\name}{1}%
    \node at (\name) {\helvbx\name};%
  }%
  %==> En-têtes lignes
  \StrGobbleLeft{#1}{2}[\first@num]%
  \StrGobbleLeft{#2}{2}[\second@num]%
  \foreach \i in {\first@num,...,\second@num}{%
    \pastableur@colorcell{numline\i}{0}%
    \GtblrFetchHeight{\i}%
    \node[celhead,minimum width=\numline@width,minimum height=\l@Gtblr@rowht] at (numline\i) {\helvbx\i};%
  }%
  \draw[very thick] (cell#2.south west) rectangle (cell#1.north east);%
}


%====PATCH \renumLineSelect (à conserver)
\renewcommand\renumLineSelect[2]{%
  \GtblrFetchHeight{#1}%
  % Efface
  \fill[white] (numline#1.south west) rectangle (numline#1.north east);%
  % Fills bleus (mêmes points que \renumLine, couleurs blueSelecCell)
  \fill[blueSelecCellBottom] (numline#1.south west) --%
                             (numline#1.south east) --%
                             ($0.5*(numline#1.south east)+0.5*(numline#1.north east)$) --%
                             ($0.5*(numline#1.south west)+0.5*(numline#1.north west)$) --%
                             cycle;%
  \fill[blueSelecCellTop] (numline#1.north west) --%
                          (numline#1.north east) --%
                          ($0.5*(numline#1.south east)+0.5*(numline#1.north east)$) --%
                          ($0.5*(numline#1.south west)+0.5*(numline#1.north west)$) --%
                          cycle;%
  % Bord redessiné PAR-DESSUS les fills (draw=black, comme celhead)
  \draw[black] (numline#1.south west) rectangle (numline#1.north east);%
  % Texte seul — outer sep=0pt, pas de draw pour éviter un second cadre
  \node[outer sep=0pt, minimum width=\numline@width,minimum height=\l@Gtblr@rowht] at (numline#1) {\helvbx #2};%
}

%====CELLULE COIN HG
\NewDocumentCommand\coinTableur{ }{%
  % Nœud de la cellule coin (anchor=east → bord droit collé sur A.west)
  \node[celhead,minimum width=\numline@width,minimum height=\Gtblr@savedht,anchor=east] (Gtblr@coin) at (A.west) {};%
  % Dégradé identique aux en-têtes (grayBottomCell / grayTopCell)
  \fill[grayBottomCell] (Gtblr@coin.south west) --
                        (Gtblr@coin.south east) --
                        ($0.5*(Gtblr@coin.south east)+0.5*(Gtblr@coin.north east)$) --
                        ($0.5*(Gtblr@coin.south west)+0.5*(Gtblr@coin.north west)$) --
                        cycle;%
  \fill[grayTopCell] (Gtblr@coin.north west) --
                     (Gtblr@coin.north east) --
                     ($0.5*(Gtblr@coin.south east)+0.5*(Gtblr@coin.north east)$) --
                     ($0.5*(Gtblr@coin.south west)+0.5*(Gtblr@coin.north west)$) --
                     cycle;%
  % Bord celhead redessiné par-dessus le dégradé
  \node[celhead,minimum width=\numline@width,minimum height=\Gtblr@savedht,anchor=east] (Gtblr@coin) at (A.west) {};%
  % Triangle bas-droite (style Excel "sélectionner tout")
  \fill[line cap=bevel,grayBottomCell!50!black] ([xshift=-1.2pt,yshift=1.2pt]Gtblr@coin.south east) --
                                                ($(Gtblr@coin.south east)+({-\Gtblr@savedht+2.4pt}, 1.2pt)$) --
                                                ($(Gtblr@coin.south east)+(-1.2pt,{\Gtblr@savedht-2.4pt})$) -- cycle;%
}

%====BARRE FORMULE
\newlength{\Gtblr@barre@namebox}  \setlength{\Gtblr@barre@namebox}{27mm}
\newlength{\Gtblr@barre@boutons}  \setlength{\Gtblr@barre@boutons}{18mm}
\newlength{\Gtblr@barre@gap}      \setlength{\Gtblr@barre@gap}{1mm}
\newlength{\Gtblr@barre@ht}
\newlength{\Gtblr@barre@sep@deux}

\NewDocumentCommand\barreFormule{ O{1.375em} m m m }{%
  % #1 = hauteur de la barre   (défaut 1.375em)
  % #2 = référence cellule     (ex : A1)
  % #3 = dernière colonne      (ex : F)
  % #4 = contenu formule       (ex : =\$B\$2*\$C\$2)
  \setlength\Gtblr@barre@ht{#1-8pt}%
  % Coins de la barre
  \coordinate (Barre@SW) at ($(Gtblr@coin.north west)+(0pt,\Gtblr@barre@gap)$);%
  \coordinate (Barre@NE) at  ($(#3.north east)+(0pt,\Gtblr@barre@gap+#1)$);%
  % Séparateur name-box / boutons
  \coordinate (Barre@sep@un) at ($(Barre@SW)+(\Gtblr@barre@namebox,0)$);%
  % Séparateur boutons / formule
  \setlength{\Gtblr@barre@sep@deux}{\dimexpr\Gtblr@barre@namebox+\Gtblr@barre@boutons\relax}%
  \coordinate (Barre@sep@deux) at ($(Barre@SW)+(\Gtblr@barre@sep@deux,0)$);%
  % Fond général
  \draw[fill=grayTopCell] (Barre@SW) rectangle (Barre@NE);%
  % Zone 1 : Name box
  \fill[rounded corners=0.5mm,white] ($(Barre@SW)+(2pt,2pt)$) rectangle
                                     ($(Barre@sep@un)+(0,-2pt)+(0,#1)$);%
  \draw[rounded corners=0.5mm,grayBottomCell!50!black, line width=0.3pt] ($(Barre@SW)+(2pt,2pt)$) rectangle ($(Barre@sep@un)+(0,-2pt)+(0,#1)$);%
  % Texte de référence
  \node[font={\scriptsize},anchor=west,inner sep=0pt] at ($(Barre@SW)+(4pt,0.5*#1)$) {{\helvbx\vphantom{(qÉ)}#2}};%
  % Flèche dropdown ▾
  \node[anchor=east,inner sep=0pt] at ($(Barre@sep@un)+(-3pt,0.5*#1)$) {\lucideicon[height=\Gtblr@barre@ht]{chevron-down}};%
  % Séparateur ⋮ 
  \node[anchor=center] at ($(Barre@sep@un)+(2mm,0.5*#1)$) {\lucideicon[height=\Gtblr@barre@ht]{ellipsis-vertical}};%
  % Zone 2 : Boutons
  \draw[rounded corners=0.5mm,grayBottomCell!50!black, line width=0.3pt,fill=white] ($(Barre@sep@un)+(4mm,0)+(0,2pt)$) rectangle ($(Barre@sep@deux)+(-4mm,-2pt)+(2pt,#1)$);%
  \node[anchor=west,inner sep=0pt] at ($(Barre@sep@un)+(4mm,0)+(2pt,0.5*#1)$) {\textcolor{darkgray}{\lucideicon[height=\Gtblr@barre@ht]{x}}\textcolor{darkgray}{\lucideicon[height=\Gtblr@barre@ht]{check}}\:\lucideicon[height=\Gtblr@barre@ht]{sigma}} ;
  \node[anchor=east,inner sep=0pt] at ($(Barre@sep@deux)+(-4mm,0)+(0,0.5*#1)$) {\lucideicon[height=\Gtblr@barre@ht]{chevron-down}} ;
  % Zone 3 : Formule (fond blanc)
  \node[anchor=center] at ($(Barre@sep@deux)+(-2mm+2pt,0.5*#1)$) {\lucideicon[height=\Gtblr@barre@ht]{ellipsis-vertical}};%
  \draw[rounded corners=0.5mm,grayBottomCell!50!black, line width=0.3pt,fill=white] ($(Barre@sep@deux)+(2pt,2pt)$) rectangle ($(Barre@NE)+(-2pt,-2pt)$);%
  \node[font={\scriptsize}, anchor=west, inner sep=0pt] at ($(Barre@sep@deux)+(4pt,0.5*#1)$) {{\helvbx\vphantom{(qÉ)}#4}};%
  \node[anchor=east,inner sep=0pt] at ($(Barre@NE)+(-5pt,-0.5*#1)$) {\lucideicon[height=\Gtblr@barre@ht]{chevron-down}};%
}

\endinput