scrn-bar.mklx /size: 16 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=scrn-bar, % was part of scrn-int
3%D        version=1995.01.01,
4%D          title=\CONTEXT\ Core Macros,
5%D       subtitle=Progress Bars,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Screen Macros / Progress Bars}
15
16\unprotect
17
18%D The code is a bit upgraded to \MKIV\ but the output is mostly the same. In
19%D retrospect this should have been a module. We can move some definitions to
20%D scrn-run-bar.mkiv if needed. We can also make the code a bit more efficient.
21
22% todo: replace blackrule by stupid rules
23
24%D \starttyping
25%D \setupinteraction
26%D   [state=start]
27%D
28%D \setupsubpagenumber
29%D   [state=start]
30%D
31%D \setuplayout
32%D   [middle]
33%D
34%D \setuppapersize
35%D   [S4][S4]
36%D
37%D \startsetups bars
38%D     \ruledvbox to \textheight \bgroup
39%D         a \ruledhbox{\interactionbar[a]}\vss
40%D         b \ruledhbox{\interactionbar[b]}\vss
41%D         c \ruledhbox{\interactionbar[c]}\vss
42%D         d \ruledhbox{\interactionbar[d]}\vss
43%D         e \ruledhbox{\interactionbar[e]}\vss
44%D         f \ruledhbox{\interactionbar[f]}\vss
45%D         g \ruledhbox{\interactionbar[g]}\vss
46%D     \egroup
47%D \stopsetups
48%D
49%D \setuptexttexts[\setups{bars}]
50%D
51%D \starttext
52%D     \dorecurse {12} {
53%D         \startstandardmakeup
54%D         \stopstandardmakeup
55%D     }
56%D \stoptext
57%D \stoptyping
58
59\installcorenamespace{interactionbar}
60
61\installframedcommandhandler \??interactionbar {interactionbar} \??interactionbar
62
63\permanent\tolerant\protected\def\interactionbar[#tag]#spacer[#S#settings]% somewhat messy
64  {\iflocation
65     \begingroup
66     \ifhastok={#tag}%
67       \lettonothing\currentinteractionbar
68       \setupcurrentinteractionbar[#tag]%
69       \cdef\currentinteractionbar{\interactionbarparameter\c!alternative}%
70     \else
71       \cdef\currentinteractionbar{#tag}%
72       \setupcurrentinteractionbar[#settings]%
73     \fi
74     \ifcstok{\interactionbarparameter\c!state}\v!start
75       \interactionbarparameter\c!command
76     \fi
77     \endgroup
78   \fi}
79
80\newdimension\d_scrn_bar_width
81\newdimension\d_scrn_bar_height
82\newdimension\d_scrn_bar_depth
83\newdimension\d_scrn_bar_distance
84
85%D Interaction buttons, in fact a row of tiny buttons, are typically only used for
86%D navigational purposed. The next macro builds such a row based on a specification
87%D list.
88%D
89%D \startbuffer
90%D \interactionbuttons[width=\hsize][page,PreviousJump,ExitViewer]
91%D \stopbuffer
92%D
93%D \typebuffer
94%D
95%D gives
96%D
97%D \getbuffer
98%D
99%D Apart from individual entries, one can use \type{page} and \type {subpage} as
100%D shortcuts to their four associated buttons. The symbols are derived from the
101%D symbols linked to the entries.
102
103\permanent\tolerant\protected\def\interactionbuttons[#S#settings]#spacer[#list]%
104  {\iflocation
105      \ifcstok{\interactionbarparameter\c!state}\v!start
106        \ifparameter#list\or
107          \scrn_bar_buttons_indeed{#settings}{#list}%
108        \else
109          \scrn_bar_buttons_indeed{}{#settings}%
110        \fi
111      \fi
112   \fi}
113
114\def\scrn_bar_buttons_indeed#settings#list%
115  {\begingroup
116  %\lettonothing\currentinteractionbar
117   \setupcurrentinteractionbar[#settings]%
118   \d_scrn_bar_width   \interactionbarparameter\c!width\relax
119   \d_scrn_bar_distance\interactionbarparameter\c!distance\relax
120   \ifzeropt\d_scrn_bar_width
121     \d_scrn_bar_width1.5\emwidth
122   \fi
123   \ifempty{\interactionbarparameter\c!height}%
124     \letinteractionbarparameter\c!height\v!broad
125   \fi
126   \ifempty{\interactionbarparameter\c!depth}%
127     \letinteractionbarparameter\c!depth\zeropoint
128   \fi
129   \resetinteractionbarparameter\c!background
130   \setbox2\hbox{\inheritedinteractionbarframed{\symbol[\interactionparameter\c!symbolset][\v!previouspage]}}%
131   \scratchheight\ht2 % needed because we default to nothing
132   \letinteractionbarparameter\c!strut\v!no
133 % \letinteractionparameter\c!width\zeropoint
134   \scratchcounterone\zerocount % new, was 1
135   \processallactionsinset
136     [#list]
137     [   \v!page=>\advanceby\scratchcounterone\plusfour,
138      \v!subpage=>\advanceby\scratchcounterone\plusfour,
139      \s!unknown=>\advanceby\scratchcounterone\plusone]%
140   \ifzeropt\d_scrn_bar_width
141     \scratchdimenone\dimexpr2\emwidth+\d_scrn_bar_distance\relax
142     \scratchdimentwo\dimexpr\scratchcounterone\scratchdimenone-\d_scrn_bar_distance\relax
143   \else
144     \scratchdimenone\d_scrn_bar_width
145     \scratchdimentwo\dimexpr\scratchcounterone\d_scrn_bar_distance-\d_scrn_bar_distance\relax
146     \advanceby\scratchdimenone -\scratchdimentwo
147     \divideby\scratchdimenone \scratchcounterone
148     \scratchdimentwo\d_scrn_bar_width
149   \fi
150   \hbox to \scratchdimentwo
151     {\setnostrut
152      \startsymbolset[\interactionparameter\c!symbolset]%
153      \setupbuttons
154        [#settings,%
155         \c!height=\the\scratchheight,%
156         \c!width=\the\scratchdimenone]%
157      \processallactionsinset
158        [#list]
159        [   \v!page=>\scrn_bar_goto\v!firstpage
160                     \scrn_bar_goto\v!nextpage
161                     \scrn_bar_goto\v!previouspage
162                     \scrn_bar_goto\v!lastpage,
163         \v!subpage=>\scrn_bar_goto\v!firstsubpage
164                     \scrn_bar_goto\v!nextsubpage
165                     \scrn_bar_goto\v!previoussubpage
166                     \scrn_bar_goto\v!lastsubpage,
167         \s!unknown=>\scrn_bar_goto\commalistelement]%
168      \unskip
169      \stopsymbolset}%
170   \endgroup}
171
172\def\scrn_bar_goto#action%
173  {\button{\symbol[#action]}[#action]\hss}
174
175\def\scrn_bar_alternative_a
176  {\d_scrn_bar_width   \interactionbarparameter\c!width
177   \d_scrn_bar_distance\interactionbarparameter\c!distance
178   \d_scrn_bar_height  \interactionbarparameter\c!height
179   \d_scrn_bar_depth   \interactionbarparameter\c!depth
180   \noindent\hbox to \d_scrn_bar_width \bgroup
181     \dontcomplain
182     \setupblackrules[\c!height=\v!max,\c!depth=\v!max]%
183     \scratchdimentwo\dimexpr\d_scrn_bar_width-4\emwidth\relax
184     \processaction
185       [\interactionbarparameter\c!step]
186       [   \v!small=>\scratchcounter 20,
187          \v!medium=>\scratchcounter 10,
188             \v!big=>\scratchcounter  5,
189         \s!unknown=>\scratchcounter 10]%
190     \scratchdimenone\dimexpr\scratchdimentwo/\scratchcounter\relax
191     \setupblackrules[\c!width=\scratchdimenone]%
192     \setbox\scratchbox\hbox to \d_scrn_bar_width
193       {\hskip2\emwidth
194        \setbox\scratchbox\hpack{\blackrule[\c!color=\interactionbarparameter\c!backgroundcolor]}%
195        \dorecurse\scratchcounter
196          {\hss\normalexpanded{\directgotodumbbox{\copy\scratchbox}[page(\the\numexpr\recurselevel*\lastpage/\scratchcounter\relax)]}}%
197        \hss
198        \hskip2\emwidth}%
199     \wd\scratchbox\zeropoint
200     \box \scratchbox
201     \setupblackrules[\c!width=\emwidth]%
202     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!firstpage]}%
203     \hskip\emwidth
204     \ifnum\realpageno>\plusone
205       \hskip\zeropoint\s!plus\numexpr\realpageno-\plustwo\relax \s!sp\relax % cm gives overflow
206       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!previouspage)]}%
207     \fi
208     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[page(\the\realpageno)]}% todo: \v!currentpage
209     \ifnum\realpageno<\lastpage\relax
210       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!nextpage]}%
211       \hskip\zeropoint\s!plus\numexpr\lastpage-\realpageno-\plusone\relax \s!sp\relax % cm gives overflow
212     \fi
213     \hskip\emwidth
214     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!lastpage]}%
215   \egroup}
216
217\def\scrn_bar_alternative_b
218  {\ifnum\lastpage>\firstpage\relax
219     \interactionbuttons[\v!firstpage,\v!previouspage,\v!nextpage,\v!lastpage]%
220   \fi}
221
222\def\scrn_bar_alternative_c
223  {\ifnum\lastpage>\plusone
224     \d_scrn_bar_width\interactionbarparameter\c!width
225     \hbox to \d_scrn_bar_width
226       {\setupblackrules[\c!height=\interactionbarparameter\c!height,\c!depth=\interactionbarparameter\c!depth,\c!width=\emwidth]%
227        \scratchdimen\dimexpr(\d_scrn_bar_width-4\emwidth)/\numexpr\lastpage+\minusone\relax\relax
228        \scratchdimenone\numexpr\realpageno+\minusone\relax\scratchdimen
229        \scratchdimentwo\numexpr\lastpage-\realpageno\relax\scratchdimen
230        \directgotospecbox\interactionbarparameter{\blackrule}[\v!firstpage]%
231        \hss
232        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\scratchdimenone]}[\v!previouspage]%
233        \blackrule[\c!color=\interactionbarparameter\c!contrastcolor]%
234        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\scratchdimentwo]}[\v!nextpage]%
235        \hss
236        \directgotospecbox\interactionbarparameter{\blackrule}[\v!lastpage]}%
237   \fi}
238
239\protected\def\scrn_bar_goto_a#whereto%
240  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!next\fi]}
241
242\protected\def\scrn_bar_goto_b#whereto%
243  {\vrule\s!height\d_scrn_bar_height\s!depth\d_scrn_bar_depth\s!width\scratchdimenone\relax}
244
245\protected\def\scrn_bar_goto_c#whereto%
246  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!somewhere\or\v!somewhere\or\v!next\fi}
247
248\protected\def\scrn_bar_goto_d#whereto%
249  {\vrule \s!width\scratchdimenone \ifcase#whereto%
250     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \or
251     \s!height.5\d_scrn_bar_height \s!depth.5\d_scrn_bar_depth \or
252     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \or
253     \s!height.5\d_scrn_bar_height \s!depth.5\d_scrn_bar_depth \else
254     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \fi}
255
256\newconstant\c_scrn_bar_mode
257
258\let\scrn_bar_goto_indeed\relax
259
260\protected\def\scrn_bar_goto_x#command%
261  {\ifcstok{\interactionbarparameter\c!symbol}\v!yes
262     \setupsymbolset[\interactionparameter\c!symbolset]%
263     \let\scrn_bar_goto_indeed\scrn_bar_goto_a
264   \else
265     \let\scrn_bar_goto_indeed\scrn_bar_goto_b
266   \fi
267   \dorecurse\nofsubpages
268    %{\scratchcounter\numexpr\recurselevel+\firstsubpage+\minusone\relax
269     {\scratchcounter\therealsubpageno\recurselevel
270      \c_scrn_bar_mode
271        \ifnum\scratchcounter<\realpageno \zerocount \else
272        \ifnum\scratchcounter=\realpageno \plusone   \else
273                                          \plustwo   \fi\fi
274      \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[page(\the\scratchcounter)]}%
275      #command}%
276   \unskip}
277
278\def\scrn_bar_alternative_d
279  {\ifnum\nofsubpages>\plusone
280     \ifcstok{\namedcounterparameter\s!subpage\c!state}\v!start
281       \d_scrn_bar_width   \interactionbarparameter\c!width
282       \d_scrn_bar_distance\interactionbarparameter\c!distance
283       \d_scrn_bar_height  \interactionbarparameter\c!height
284       \d_scrn_bar_depth   \interactionbarparameter\c!depth
285       \scratchdimenone\d_scrn_bar_width
286       \noindent\hbox{\scrn_bar_goto_x{\hskip\d_scrn_bar_distance}}% \hpack ?
287     \fi
288   \fi}
289
290\def\scrn_bar_alternative_e
291  {\ifnum\nofsubpages>\plusone
292     \ifcstok{\namedcounterparameter\s!subpage\c!state}\v!start
293       \d_scrn_bar_width   \interactionbarparameter\c!width
294       \d_scrn_bar_distance\interactionbarparameter\c!distance
295       \d_scrn_bar_height  \interactionbarparameter\c!height
296       \d_scrn_bar_depth   \interactionbarparameter\c!depth
297       \scratchdimentwo\dimexpr\nofsubpages\d_scrn_bar_distance-\d_scrn_bar_distance\relax % (n-1)
298       \scratchdimenone\dimexpr(\d_scrn_bar_width-\scratchdimentwo)/\nofsubpages\relax
299       \ifdim\scratchdimenone<\d_scrn_bar_distance
300         \scrn_bar_alternative_f
301       \else
302         \noindent\hbox to \d_scrn_bar_width{\scrn_bar_goto_x{\hss}\unskip}% \hpack ?
303       \fi
304     \fi
305   \fi}
306
307\def\scrn_bar_alternative_f
308  {\ifnum\nofsubpages>\plusone
309     \ifcstok{\namedcounterparameter\s!subpage\c!state}\v!start
310       \d_scrn_bar_width   \interactionbarparameter\c!width
311       \d_scrn_bar_distance\interactionbarparameter\c!distance
312       \d_scrn_bar_height  \interactionbarparameter\c!height
313       \d_scrn_bar_depth   \interactionbarparameter\c!depth
314       \noindent \hbox to \d_scrn_bar_width \bgroup
315         \doloop
316           {\scratchcounterthree\numexpr(\nofsubpages/\recurselevel)+\plusone\relax % rounding
317            \scratchdimentwo\d_scrn_bar_distance
318            \multiplyby\scratchdimentwo \scratchcounterthree
319            \advanceby\scratchdimentwo -\d_scrn_bar_distance
320            \scratchdimenone\d_scrn_bar_width
321            \advanceby\scratchdimenone -\scratchdimentwo
322            \divideby\scratchdimenone \scratchcounterthree
323            \ifdim\scratchdimenone<\d_scrn_bar_distance\else
324              \scratchcountertwo\recurselevel
325              \exitloop
326            \fi}%
327         \ifnum\scratchcounterthree>\plusone
328           % this is not that well tested
329           \advanceby\scratchcounterthree \minustwo
330           \scratchdimenone-\d_scrn_bar_distance
331           \scratchdimenone\scratchcounterthree\scratchdimenone
332           \advanceby\scratchdimenone \d_scrn_bar_width
333           \advanceby\scratchcounterthree \plusone
334           \divideby\scratchdimenone \scratchcounterthree
335         \fi
336         \ifcstok{\interactionbarparameter\c!symbol}\v!yes
337           \setupsymbolset[\interactionparameter\c!symbolset]%
338           \let\scrn_bar_goto_indeed\scrn_bar_goto_c
339         \else
340           \let\scrn_bar_goto_indeed\scrn_bar_goto_d
341         \fi
342         \scratchcounterthree\numexpr\realpageno-\plustwo\relax
343         \scratchcounterfour\numexpr\realpageno+\plustwo\relax
344         \ifnum\scratchcounterthree<\plusone \scratchcounterthree\plusone \fi
345         \scratchcounterfive\zerocount
346         \dostepwiserecurse\firstsubpage\lastsubpage\plusone
347           {\donefalse
348            \advanceby\scratchcounterfive \plusone
349            \ifnum\recurselevel=\firstsubpage\relax \donetrue \fi
350            \ifnum\recurselevel=\lastsubpage \relax \donetrue \fi
351            \scratchcountersix\therealsubpageno\recurselevel\relax
352            \c_scrn_bar_mode
353              \ifdone
354                \ifnum\scratchcountersix<\realpageno
355                  \zerocount
356                \orelse\ifnum\scratchcountersix>\realpageno
357                  \plustwo
358                \else
359                  \plusfour
360                \fi
361              \else
362                \ifnum\scratchcounterfive=\scratchcountertwo
363                  \ifnum\scratchcountersix<\realpageno
364                    \plusone
365                  \orelse\ifnum\scratchcountersix>\realpageno
366                    \plusthree
367                  \else
368                    \plustwo
369                  \fi
370                \else
371                  \minusone
372                \fi
373              \fi
374            \ifnum\c_scrn_bar_mode<\zerocount\else
375              \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[realpage(\the\scratchcountersix)]}%
376              \hss
377              \scratchcounterfive\zerocount
378            \fi}%
379         \unskip
380       \egroup
381     \fi
382   \fi}
383
384\def\scrn_bar_alternative_g
385  {\ifnum\lastsubpage>\firstsubpage\relax % no test for state?
386     \interactionbuttons[\v!firstsubpage,\v!previoussubpage,\v!nextsubpage,\v!lastsubpage]%
387   \fi}
388
389\setupinteractionbar
390  [\c!state=\v!start,
391   \c!alternative=a,
392   \c!symbol=\v!no,
393   \c!width=10\emwidth,
394   \c!height=.5\emwidth,
395   \c!depth=\zeropoint,
396   \c!distance=.5\emwidth,
397   \c!step=\v!medium,
398   \c!foregroundcolor=\interactionbarparameter\c!color,
399   \c!foregroundstyle=\interactionbarparameter\c!style,
400   \c!color=\interactionparameter\c!color,
401   \c!contrastcolor=\interactionparameter\c!contrastcolor,
402   \c!style=,
403   \c!frame=\v!on,
404   \c!background=color,
405   \c!backgroundcolor=gray,
406   \c!samepage=\v!yes]
407
408\defineinteractionbar[a][\c!command=\scrn_bar_alternative_a]
409\defineinteractionbar[b][\c!command=\scrn_bar_alternative_b,\c!height=\v!broad]
410\defineinteractionbar[c][\c!command=\scrn_bar_alternative_c,\c!height=\v!max,\c!depth=\v!max]
411\defineinteractionbar[d][\c!command=\scrn_bar_alternative_d,\c!width=.5\emwidth]
412\defineinteractionbar[e][\c!command=\scrn_bar_alternative_e]
413\defineinteractionbar[f][\c!command=\scrn_bar_alternative_f]
414\defineinteractionbar[g][\c!command=\scrn_bar_alternative_g,\c!height=\v!broad]
415
416\protect \endinput
417