%\module [
%  file=s-cgj.mkiv,
%  version=2012.12.05,
%  title=Context group style file,
%  subtitle=CG-journal base style,
%  author={Adrian Egger, W. Egger, Taco Hoekwater},
%  date=\currentdate,
%  copyright={Context Group}%
%]

\enablemode[cgjpagecolumns]

% Fixme and todo ...

\definecolor[NoteColor]      [g=1,r=.25,b=.25]
\definecolor[WarnColor]      [r=1,g=.25,b=.25]

\def\todo#1%
  {\startframedtext
    [background=color,backgroundcolor=NoteColor]
  {\bf TODO:}~~#1\par
  \stopframedtext }

\def\fixme#1%
  {\startframedtext
    [background=color,backgroundcolor=WarnColor]
  {\bf FIXME:}~~#1\par
  \stopframedtext }

% There is a draft mode, which enables all frames
% \enablemode[draft]

\doifmodeelse
  {draft}
  {\def\OnOff{on}\showframe}
  {\def\OnOff{off}}


% Base set of variables
% The actual values are set in the file CG-journal
\setvariables
  [CG-Journal]
  [Title={Journal},
   RunningTitle={Journal},
   SubTitle={From the base set of variables},
   Version=1.0,
   NOFColumns=2,
  ]
\setupinteraction[
    state=start,
    color=,
    contrastcolor=,
    style=,
    title=Journal,
    author=context group,
]
\placebookmarks[title,chapter,subject,section][chapter]
\setupinteractionscreen[option={doublesided,bookmark}]

\startmode[onecolumn,fullwidth]
\setvariables
  [CG-Journal]
  [NOFColumns=1]
\stopmode

% Fonts setup
\usetypescriptfile[plex]
%\usetypescriptfile[type-inconsolata]

\starttypescript [cgj]
 \definetypeface [cgj] [rm] [serif][ibmplex]    [default]
 \definetypeface [cgj] [ss] [sans] [ibmplex]    [default]
 \definetypeface [cgj] [mm] [math] [palatino]   [default]
 \definetypeface [cgj] [tt] [mono] [ibmplex]    [default] [rscale=0.9]
\stoptypescript

\starttypescript [cgj-light]
 \definetypeface [cgj-light] [rm] [serif][ibmplex]       [default]
 \definetypeface [cgj-light] [ss] [sans] [ibmplex-light] [default]
 \definetypeface [cgj-light] [mm] [math] [palatino]      [default]
 \definetypeface [cgj-light] [tt] [mono] [ibmplex]       [default] [rscale=0.9]
\stoptypescript

\usebodyfont[cgj-light,ss,10pt]
\setupbodyfont[cgj,ss,10pt]

\definebodyfontenvironment[10pt][interlinespace=12pt]
\definebodyfontenvironment[12pt][interlinespace=16pt]
\definebodyfontenvironment[16pt][interlinespace=20pt]
\setupinterlinespace[line=12pt]
\let\sl\it

% Path to the logos
\setupexternalfigures[directory=./Logos]

% Logos
% Black logo only
\useexternalfigure
  [Logo]
  [cg_corp_logo_loop_black_cmyk]
  [width=30mm,height=19mm]
%Black logo with text
\useexternalfigure
  [LogoText]
  [cg_corp_logo_text_black_cmyk]
  [width=50mm,height=35.2mm]

% Colors
\definecolor[CGlightblue][c=1,m=.15,y=0,k=0]
\definecolor[CGdeepblue][c=1,m=.8,y=0,k=.3]
\definecolor[CGgray][c=0,m=0,y=0,k=.1]

% Article styles
\definealternativestyle
   [Articleheading]
   [{\switchtobodyfont[16pt,ss]\bf}]
\definealternativestyle
   [Articlesubheading]
   [{\switchtobodyfont[16pt,ss]\tf}]
\definealternativestyle
   [Authorname]
   [{\switchtobodyfont[12pt,ss]\it}]
\definealternativestyle
   [Sectionheading]
   [{\switchtobodyfont[12pt,ss]\bf\setupinterlinespace[line=12pt]}]
\definealternativestyle
   [Subsectionheading]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf\setupinterlinespace[line=10pt]}]
\definealternativestyle
   [IntroCopy]
   [{\switchtobodyfont[12pt,ss]\tf}]
\definealternativestyle
   [PagenumberStyle]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf}]

% Headerstyles - Breadcrumbs
\definealternativestyle
   [BreadcrumbMd]
   [{\switchtobodyfont[10pt,ss]\bf}]
\definealternativestyle
   [BreadcrumbRg]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf}]
\definealternativestyle
   [BreadcrumbLt]
   [{\switchtobodyfont[10pt,ss]\tf}]
\definealternativestyle
   [BreadcrumbTh]
   [{\switchtobodyfont[cgj-light,10pt,ss]\tf}]

% Captionstyles
\definealternativestyle
   [Captionheading]
   [{\switchtobodyfont[8pt,ss]\bf\setupinterlinespace[line=8pt]}]
\definealternativestyle
   [Captiontext]
   [{\switchtobodyfont[8pt,ss]\tf\setupinterlinespace[line=8pt]}]

% Article signature
\definealternativestyle
   [Signaturestyle]
   [{\switchtobodyfont[10pt,ss]\it}]

% Article footnotes
\definealternativestyle
   [Articlefootnotes]
   [{\switchtobodyfont[8pt,ss]\tf}]

% Index /TOC styles
% It  looks like the inheritance is gone.
%\definealternativestyle
%   [IndexContents][Articleheading]
\definealternativestyle
   [IndexContents]
   [{\switchtobodyfont[16pt,ss]\bf\setupinterlinespace[line=18pt]}]
\definealternativestyle
   [IndexArticleTitle]
   [{\switchtobodyfont[12pt,ss]\tf\setupinterlinespace[line=20pt]}]
\definealternativestyle
   [IndexNumber]
   [{\switchtobodyfont[12pt,ss]\bf\setupinterlinespace[line=20pt]}]
\definealternativestyle
   [IndexAuthor]
   [{\switchtobodyfont[12pt,ss]\it\setupinterlinespace[line=20pt]}]

% Math
% --> still missing

% Verbatim
\definealternativestyle
   [DisplayMonospaced]
   [{\switchtobodyfont[10pt,tt]\setupinterlinespace[line=12pt]}]
\definealternativestyle
   [DisplayMonospacedX]
   [{\switchtobodyfont[8pt,tt]\setupinterlinespace[line=11pt]}]
\definealternativestyle
   [DisplayMonospacedS]
   [{\switchtobodyfont[9pt,tt]\setupinterlinespace[line=12pt]}]

% \blank adjustment
\defineblank[CGblank][6pt]
\defineblank[BigCGblank][24pt]
\defineblank[MediumCGblank][12pt]

% Paper definition
\definepapersize[Journal][width=210mm,height=266mm]
\setuppapersize[Journal][Journal]


% General layout
\definelayout
  [General]
  [topspace=20mm,
   backspace=28mm,
   header=5mm,
   headerdistance=5mm,
   footer=3mm,
   footerdistance=5mm,
   width=157mm,
   height=224mm,
   marking=on,
  ]

\definelayout
  [Content]
  [topspace=20mm,
   backspace=28mm,
   header=5mm,
   headerdistance=5mm,
   footer=0mm,
   footerdistance=0mm,
   width=157mm,
   height=224mm,
   margindistance=4mm,
   rightmargin=21mm,
   leftmargin=21mm,
   marking=on,
  ]

\definelayout
  [Imprint]
  [topspace=20mm,
   backspace=28mm,
   header=5mm,
   headerdistance=5mm,
   footer=3mm,
   footerdistance=5mm,
   width=107mm,
   height=224mm,
   margindistance=4mm,
   rightmargin=45.3mm,
   leftmargin=21mm,
   marking=on,
  ]

\definelayout
  [SingleColumn]
  [topspace=20mm,
   backspace=55mm,
   header=5mm,
   headerdistance=5mm,
   footer=3mm,
   footerdistance=5mm,
   width=130mm,
   height=224mm,
   margindistance=4mm,
   leftmargin=48mm,
   rightmargin=21mm,
   marking=on,
  ]

% Pagenumbering: Pagenumber is set in the footer
\setuppagenumbering[location=,alternative=doublesided]

% Head-definitions
% The distances according to the style-guide
% H1 = Article title
% H2 = Section title (1. Bla bla)
% H3 = Subsection title (1.1 Bla bla)
% H4 = Subsubsection title (1.1.1 Bla bla)
%
% Between abstract and body: 2 lines 10/12pt --> 24 pt: BigCGblank
% Between H2 and bodycopy/H3: 1 line 6/6pt   -->  6 pt: CGblank
% Between H3 and bodycopy/H4: 1 line 6/6pt   -->  6 pt: CGblank
% End H2 paragraph: 2 lines 10/12pt          --> 24 pt: BigCGblank
% End H3 paragraph: 1 line 6/6pt             -->  6 pt: CGblank

\setuphead
  [title]
  [style=Articleheading,
   after={\blank[BigCGblank]},
   page=yes]
\setuphead
  [part]
  [placehead=no,
   after=,
   before=,
   page=no,
   numbercommand=,
   expansion=yes]
\setuphead
  [section,subject]
  [style=Sectionheading,
   before={\blank[BigCGblank]},
   after={\blank[CGblank]},
   sectionstopper={.},
   distance=4pt,
   align={flushleft,nothyphenated},
   resetnumber=yes,
   continue=yes,
  ]
\setuphead
  [subsection]
  [style=Subsectionheading,
   before={\blank[CGblank]},
   after={\blank[CGblank]},
   sectionstopper=,
   distance=4pt,
   align={flushleft,nothyphenated},
   resetnumber=yes,
   continue=yes,
  ]
%\setuphead
%  [subject]
%  [style=Sectionheading,before={\blank[BigCGblank]},after=]
\setuphead
  [subsubject]
  [style=Subsectionheading,
   before={\blank[CGblank]},
   after={\blank[CGblank]},
   sectionstopper=,
  ]
% Columns
% The setup of the columns is done at the moment columns are started (see end of file)

% Index/TOC page setups
\unprotect

   \def\listboxproperties       {\strc_lists_get_reference_attribute}
   \def\listrenderingsetup      {\the\t_lists_every_renderingtext}
   \def\listrenderingsynchronize{\the\t_lists_every_renderingsynchronize}

\protect

% now you can say:

\definelistalternative
 [CGJ:Index]
 [renderingsetup=CGJ:Indexheading]
\definelistalternative
 [CGJ:Index:Chapter]
 [renderingsetup=CGJ:Indexheading:Chapter]


 \startsetups[CGJ:Indexheading:Chapter]
    \listparameter{before}
    \vbox \listboxproperties{all} {
        \forgetall
        \dontleavehmode
        \listrenderingsynchronize
        \hbox to 15mm{
        \bgroup
            \useliststyleandcolor{numberstyle}{numbercolor}
            \currentlistentrypagenumber
        \egroup}
        % \bgroup
        %             \useliststyleandcolor{numberstyle}{numbercolor}
        %             \currentlistsymbol
        %         \egroup
        % .\space
        \bgroup
            \useliststyleandcolor{textstyle}{textcolor}
            \smash{\currentlistentrytitle}%
        \egroup
        % \par
    }
    \par
    \listparameter{after}
 \stopsetups

\startsetups[CGJ:Indexheading]
   \listparameter{before}
   \vbox \listboxproperties{all} {
       \forgetall
       \dontleavehmode
       \listrenderingsynchronize
       \hbox to 15mm{
       \bgroup
           %\useliststyleandcolor{pagestyle}{pagecolor}
           \useliststyleandcolor{numberstyle}{numbercolor}
           \currentlistentrypagenumber
       \egroup}
       % \bgroup
       %            \useliststyleandcolor{numberstyle}{numbercolor}
       %            \currentlistsymbol
       %        \egroup
       %        .\space
       \bgroup
           \useliststyleandcolor{textstyle}{textcolor}
           \currentlistentrytitle
       \egroup
       \par
   }
   \par
   \listparameter{after}
\stopsetups

\setuplist
 [part]
 [before={\blank[CGblank]},
  after=,
  style=IndexContents,
  numberstyle=\IndexNumber,
  textstyle=\IndexArticleTitle,
  prefix=no,
  alternative=CGJ:Index:Chapter]

%\setuplist
% [section]
% [before={\blank[CGblank]},
%  after={},
%  style=IndexHeaderB,
%  % numberstyle=\bf,
%  %   textstyle=\bfa,
%  prefix=no,
%  alternative=CGJ:Index]
%
%\setuplist
% [subsection]
% [before={},
%  after={},
%  style=IndexHeaderC,
%  % numberstyle=\bf,
%  % textstyle=\bfa,
%  prefix=no,
%  alternative=CGJ:Index]

% Floats

% \setupfloat
%   [figure]
%   []

% Captions
\setupcaptions
  [suffix={.},
   width=max,
   headstyle=\Captionheading,
   style=\Captiontext,
   distance=2pt
  ]

\setupcaption[figure][way=bypart]
\setupcaption[table] [way=bypart]

% Datacollection: article parameters (other fields and defaults)
\def\CGJBibData[#1]%
  {\getparameters
    [CGJ]
    [SubTitle=,
    RunningAuthor=,
    RunningTitle=Example,
    Email=,
    Address=,
    Page=1,
    Title={My Article},
    Author={Example Author},
    Period=,
    Number=,
    Year=,
    TocAuthor=,
    TocTitle=,
    #1]%
   \doifnothing
      {\CGJRunningTitle}
      {\let\CGJRunningTitle\CGJTitle}%
   \doifnothing
      {\CGJRunningAuthor}
      {\let\CGJRunningAuthor\CGJAuthor}%
   \doifnothing
      {\CGJTocAuthor}
      {\let\CGJTocAuthor\CGJAuthor}%
   \doifnothing
      {\CGJTocTitle}
      {\let\CGJTocTitle\CGJTitle}%
   \setvariables[CGJToc][Author=\CGJTocAuthor,
                         Title=\CGJTocTitle]% for TOC
  }

\def\dostartArticle[#1]{%
  \CGJBibData[#1]
  % \pageno=\CGJPage
  % \setupuserpagenumber[start=\CGJPage]
  \doifelse
      {\getvariable{CG-Journal}{NOFColumns}}
      {1}
      {\doifmodeelse{fullwidth}
          {\setuplayout[General]}
          {\setuplayout[SingleColumn]}}
      {\setuplayout
          [General]}%
   \doifmode{cgjpagecolumns}
     {\definepagecolumns [cgjpagecolumns][n=\getvariable{CG-Journal}{NOFColumns},distance=12pt,page=no]}
  \bgroup
    {\switchtobodyfont[16pt]\Articleheading\CGJTitle\par}
    {\doifsomething{\CGJSubTitle}
      {\switchtobodyfont[16pt]\Articlesubheading\CGJSubTitle\par}}
    {\doifsomething
      {\CGJAuthor}
      {\switchtobodyfont[12pt]\Authorname \CGJAuthor}\par}
    \part{\getvariable{CGJToc}{Title}%
          \doifsomething{\getvariable{CGJToc}{Author}}{ \emdash\ }%
          \IndexAuthor\getvariable{CGJToc}{Author}}
    \godown[16pt]
  \egroup
  \hyphenpenalty1000}

\def\startArticle{\dosingleempty\dostartArticle}

\def\signArticle{}

\def\CGJRunningTitle {}
\def\CGJRunningAuthor {}
\unprotect
\def\mystoppagecolumns%
  {\page \endgroup \page_otr_command_set_vsize \page_otr_command_set_hsize \page \endgroup}
\protect

\def\stopArticle{%
  \par\signArticle
  \doifmodeelse{cgjcolumnsets}
    {\stopcolumnset \page}
    {\doifmodeelse{cgjpagecolumns}
      {\mystoppagecolumns}
      {\doifmodeelse{cgjmixedcolumns}
        {\stopmixedcolumns \page}
        {\stopcolumns \page}%
      }%
    }%
  }

\def\startAbstract{%
  \bgroup
    \switchtobodyfont[12pt]
    \IntroCopy}


\def\finishAbstract{%
  \blank[BigCGblank]%
  \doif
      {\getvariable{CG-Journal}{NOFColumns}}
      {>1}
      {\setupcolumns
        [n=\getvariable{CG-Journal}{NOFColumns},
         distance=4mm,
         balance=yes,
        ]
      }
    \doifmodeelse{cgjcolumnsets}
      {\definecolumnset[cgjcolumnsets][n=\getvariable{CG-Journal}{NOFColumns},balance=yes]%
       \startcolumnset[cgjcolumnsets]}
      {\doifmodeelse{cgjpagecolumns}
         {\par\ifnum \getvariable{CG-Journal}{NOFColumns}>1 \hbox{}\par\fi\startpagecolumns[cgjpagecolumns]}
         {\doifmodeelse{cgjmixedcolumns}
           {\definemixedcolumns[cgjmixedcolumns][n=\getvariable{CG-Journal}{NOFColumns},balance=yes,blank={line,fixed}]%
            \startmixedcolumns[cgjmixedcolumns]}
           {\startcolumns[n=\getvariable{CG-Journal}{NOFColumns}]}%
         }%
       }%
}

\def\stopAbstract{%
  \par
  \egroup
  \finishAbstract}

\def\noAbstract{\kern -24pt \finishAbstract}


% Headertexts
\startsetups[Header:texts]
  \setupheadertexts
    []
    [{{\BreadcrumbLt\CGJRunningTitle}{\BreadcrumbTh\doifsomething{\CGJRunningAuthor}{\ > }\CGJRunningAuthor}}] %odd
    [{\BreadcrumbMd contextgroup}{\BreadcrumbRg\ > \getvariable{CG-Journal}{RunningTitle}}] %even
    []
\stopsetups


% Footertexts
\startsetups[Footer:texts]
  \setupfootertexts
    []
    [\PagenumberStyle\pagenumber]
    [\PagenumberStyle\pagenumber]
    []
\stopsetups

% Setup tolerance, stretch
\setuptolerance[tolerant,stretch]

% Setting up footnotes

\setupnotation
  [footnote]
  [way=bypart,
   rule=on]

% Adjustments to the container containing the footnotes
\setupnote
  [footnote]
  [frame=off,
   topframe=off ,
   framecolor=black,
   background=,
   rulecolor=black,    %% the line above the inserts
   rulethickness=1pt,
   style=Articlefootnotes]


% Setup typing
\setupnarrower[middle=3mm]
\setupnarrower[left=5mm]

\definetextbackground
  [typingbg]
  [frame=off,
   location=paragraph,
   background=color,
   backgroundcolor=CGgray,
   width=\hsize,
   leftoffset=2mm, % doesnt work in the current beta
   align=flushleft]
   
\startsetups typing:before
  \blank[MediumCGblank]
  \starttextbackground[typingbg][style=DisplayMonospacedS]
  \hfuzz=7.02pt % don't care if it bleeds in to the frame a little
  \setupnarrower[left=2mm] % because leftoffset does not work in the current beta
  \startnarrower[left]
\stopsetups

\startsetups typing:before:small
  \blank[MediumCGblank]
  \starttextbackground[typingbg][style=DisplayMonospacedX]
  \hfuzz=6.3pt % don't care if it bleeds in to the frame a little
  \setupnarrower[left=2mm]
  \startnarrower[left]
\stopsetups

\startsetups typing:after
  \stopnarrower\par
  \stoptextbackground
  \blank[MediumCGblank]
\stopsetups

\startsetups typing:before:item
%  \blank[3pt]
  \startframedtext
    [frame=off,
     before=,
     after=,
     background=color,
     backgroundcolor=CGgray,
     width=\the\dimexpr\hsize-10.5mm\relax,
     offset=3mm,
     align=flushleft,
     style=DisplayMonospacedS,
    ]
  \hfuzz=7.02pt % don't care if it bleeds in to the frame a little
\stopsetups

\startsetups typing:after:item
  \stopframedtext
%  \blank[3pt]
\stopsetups

\setuptyping
  [typing]
  [style=DisplayMonospaced,
   before=\setups{typing:before},
   after=\setups{typing:after}]

\definetyping[smalltyping]
\setuptyping
  [smalltyping]
  [style=DisplayMonospacedX,
   before=\setups{typing:before:small},
   after=\setups{typing:after}]


% Bullet lists
\setupitemgroup
  [itemize]
  [each]
  []
  [symbol=1,
   indentnext=no,
   align=right,
   before={\blank[MediumCGblank]\startnarrower[left]},
   after={\stopnarrower\blank[MediumCGblank]},
   inbetween={\blank[CGblank]},
  ]

\def\Href#1{\underbar{\hyphenatedurl{#1}}}

\setupformulas[align=flushleft,margin=5mm]

\setupquotation[before={\blank[CGblank]\switchtobodyfont[8pt]\setupinterlinespace[line=12pt]},
                after={\par\blank[CGblank]}]

\definedescription
  [description]
  [location=hanging,
   width=broad,
   before={\blank},
   after={\blank}]

\unexpanded\def\CG    {\ConTeXt\ group}

\usemodule[abr-02]

\endinput
