summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/scrn-bar.mklx
blob: 135552a67d3a5b6492e613b73aa7e13d41dc0f00 (plain)
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
%D \module
%D   [       file=scrn-bar, % was part of scrn-int
%D        version=1995.01.01,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Progress Bars,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Screen Macros / Progress Bars}

\unprotect

%D The code is a bit upgraded to \MKIV\ but the output is mostly the same. In
%D retrospect this should have been a module. We can move some definitions to
%D scrn-run-bar.mkiv if needed. We can also make the code a bit more efficient.

% todo: replace blackrule by stupid rules

%D \starttyping
%D \setupinteraction
%D   [state=start]
%D
%D \setupsubpagenumber
%D   [state=start]
%D
%D \setuplayout
%D   [middle]
%D
%D \setuppapersize
%D   [S4][S4]
%D
%D \startsetups bars
%D     \ruledvbox to \textheight \bgroup
%D         a \ruledhbox{\interactionbar[a]}\vss
%D         b \ruledhbox{\interactionbar[b]}\vss
%D         c \ruledhbox{\interactionbar[c]}\vss
%D         d \ruledhbox{\interactionbar[d]}\vss
%D         e \ruledhbox{\interactionbar[e]}\vss
%D         f \ruledhbox{\interactionbar[f]}\vss
%D         g \ruledhbox{\interactionbar[g]}\vss
%D     \egroup
%D \stopsetups
%D
%D \setuptexttexts[\setups{bars}]
%D
%D \starttext
%D     \dorecurse {12} {
%D         \startstandardmakeup
%D         \stopstandardmakeup
%D     }
%D \stoptext
%D \stoptyping

\installcorenamespace{interactionbar}

\installframedcommandhandler \??interactionbar {interactionbar} \??interactionbar

\permanent\tolerant\protected\def\interactionbar[#tag]#spacer[#settings]% somewhat messy
  {\iflocation
     \begingroup
     \ifhastok={#tag}%
       \lettonothing\currentinteractionbar
       \setupcurrentinteractionbar[#tag]%
       \cdef\currentinteractionbar{\interactionbarparameter\c!alternative}%
     \else
       \cdef\currentinteractionbar{#tag}%
       \setupcurrentinteractionbar[#settings]%
     \fi
     \doif{\interactionbarparameter\c!state}\v!start
       {\interactionbarparameter\c!command}%
     \endgroup
   \fi}

\newdimension\d_scrn_bar_width
\newdimension\d_scrn_bar_height
\newdimension\d_scrn_bar_depth
\newdimension\d_scrn_bar_distance

%D Interaction buttons, in fact a row of tiny buttons, are typically only used for
%D navigational purposed. The next macro builds such a row based on a specification
%D list.
%D
%D \startbuffer
%D \interactionbuttons[width=\hsize][page,PreviousJump,ExitViewer]
%D \stopbuffer
%D
%D \typebuffer
%D
%D gives
%D
%D \getbuffer
%D
%D Apart from individual entries, one can use \type{page} and \type {subpage} as
%D shortcuts to their four associated buttons. The symbols are derived from the
%D symbols linked to the entries.

\permanent\tolerant\protected\def\interactionbuttons[#settings]#spacer[#list]%
  {\iflocation
      \ifcstok{\interactionbarparameter\c!state}\v!start
        \ifparameter#list\or
          \scrn_bar_buttons_indeed{#settings}{#list}%
        \else
          \scrn_bar_buttons_indeed{}{#settings}%
        \fi
      \fi
   \fi}

\def\scrn_bar_buttons_indeed#settings#list%
  {\begingroup
  %\lettonothing\currentinteractionbar
   \setupcurrentinteractionbar[#settings]%
   \d_scrn_bar_width   \interactionbarparameter\c!width\relax
   \d_scrn_bar_distance\interactionbarparameter\c!distance\relax
   \ifzeropt\d_scrn_bar_width
     \d_scrn_bar_width1.5\emwidth
   \fi
   \doifnothing{\interactionbarparameter\c!height}{\letinteractionbarparameter\c!height\v!broad}%
   \doifnothing{\interactionbarparameter\c!depth }{\letinteractionbarparameter\c!depth\zeropoint}%%%
   \resetinteractionbarparameter\c!background
   \setbox2\hbox{\inheritedinteractionbarframed{\symbol[\interactionparameter\c!symbolset][\v!previouspage]}}%
   \scratchheight\ht2 % needed because we default to nothing
   \letinteractionbarparameter\c!strut\v!no
 % \letinteractionparameter\c!width\zeropoint
   \scratchcounterone\zerocount % new, was 1
   \processallactionsinset
     [#list]
     [   \v!page=>\advanceby\scratchcounterone\plusfour,
      \v!subpage=>\advanceby\scratchcounterone\plusfour,
      \s!unknown=>\advanceby\scratchcounterone\plusone]%
   \ifzeropt\d_scrn_bar_width
     \scratchdimenone\dimexpr2\emwidth+\d_scrn_bar_distance\relax
     \scratchdimentwo\dimexpr\scratchcounterone\scratchdimenone-\d_scrn_bar_distance\relax
   \else
     \scratchdimenone\d_scrn_bar_width
     \scratchdimentwo\dimexpr\scratchcounterone\d_scrn_bar_distance-\d_scrn_bar_distance\relax
     \advanceby\scratchdimenone -\scratchdimentwo
     \divideby\scratchdimenone \scratchcounterone
     \scratchdimentwo\d_scrn_bar_width
   \fi
   \hbox to \scratchdimentwo
     {\setnostrut
      \startsymbolset[\interactionparameter\c!symbolset]%
      \setupbuttons
        [#settings,%
         \c!height=\the\scratchheight,%
         \c!width=\the\scratchdimenone]%
      \processallactionsinset
        [#list]
        [   \v!page=>\scrn_bar_goto\v!firstpage
                     \scrn_bar_goto\v!nextpage
                     \scrn_bar_goto\v!previouspage
                     \scrn_bar_goto\v!lastpage,
         \v!subpage=>\scrn_bar_goto\v!firstsubpage
                     \scrn_bar_goto\v!nextsubpage
                     \scrn_bar_goto\v!previoussubpage
                     \scrn_bar_goto\v!lastsubpage,
         \s!unknown=>\scrn_bar_goto\commalistelement]%
      \unskip
      \stopsymbolset}%
   \endgroup}

\def\scrn_bar_goto#action%
  {\button{\symbol[#action]}[#action]\hss}

\def\scrn_bar_alternative_a
  {\d_scrn_bar_width   \interactionbarparameter\c!width
   \d_scrn_bar_distance\interactionbarparameter\c!distance
   \d_scrn_bar_height  \interactionbarparameter\c!height
   \d_scrn_bar_depth   \interactionbarparameter\c!depth
   \noindent\hbox to \d_scrn_bar_width \bgroup
     \dontcomplain
     \setupblackrules[\c!height=\v!max,\c!depth=\v!max]%
     \scratchdimentwo\dimexpr\d_scrn_bar_width-4\emwidth\relax
     \processaction
       [\interactionbarparameter\c!step]
       [   \v!small=>\scratchcounter 20,
          \v!medium=>\scratchcounter 10,
             \v!big=>\scratchcounter  5,
         \s!unknown=>\scratchcounter 10]%
     \scratchdimenone\dimexpr\scratchdimentwo/\scratchcounter\relax
     \setupblackrules[\c!width=\scratchdimenone]%
     \setbox\scratchbox\hbox to \d_scrn_bar_width
       {\hskip2\emwidth
        \setbox\scratchbox\hpack{\blackrule[\c!color=\interactionbarparameter\c!backgroundcolor]}%
        \dorecurse\scratchcounter
          {\hss\normalexpanded{\directgotodumbbox{\copy\scratchbox}[page(\the\numexpr\recurselevel*\lastpage/\scratchcounter\relax)]}}%
        \hss
        \hskip2\emwidth}%
     \wd\scratchbox\zeropoint
     \box \scratchbox
     \setupblackrules[\c!width=\emwidth]%
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!firstpage]}%
     \hskip\emwidth
     \ifnum\realpageno>\plusone
       \hskip\zeropoint\s!plus\numexpr\realpageno-\plustwo\relax \s!sp\relax % cm gives overflow
       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!previouspage)]}%
     \fi
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[page(\number\realpageno)]}% todo: \v!currentpage
     \ifnum\realpageno<\lastpage\relax
       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!nextpage]}%
       \hskip\zeropoint\s!plus\numexpr\lastpage-\realpageno-\plusone\relax \s!sp\relax % cm gives overflow
     \fi
     \hskip\emwidth
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!lastpage]}%
   \egroup}

\def\scrn_bar_alternative_b
  {\ifnum\lastpage>\firstpage\relax
     \interactionbuttons[\v!firstpage,\v!previouspage,\v!nextpage,\v!lastpage]%
   \fi}

\def\scrn_bar_alternative_c
  {\ifnum\lastpage>\plusone
     \d_scrn_bar_width\interactionbarparameter\c!width
     \hbox to \d_scrn_bar_width
       {\setupblackrules[\c!height=\interactionbarparameter\c!height,\c!depth=\interactionbarparameter\c!depth,\c!width=\emwidth]%
        \scratchdimen\dimexpr(\d_scrn_bar_width-4\emwidth)/\numexpr\lastpage+\minusone\relax\relax
        \scratchdimenone\numexpr\realpageno+\minusone\relax\scratchdimen
        \scratchdimentwo\numexpr\lastpage-\realpageno\relax\scratchdimen
        \directgotospecbox\interactionbarparameter{\blackrule}[\v!firstpage]%
        \hss
        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\scratchdimenone]}[\v!previouspage]%
        \blackrule[\c!color=\interactionbarparameter\c!contrastcolor]%
        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\scratchdimentwo]}[\v!nextpage]%
        \hss
        \directgotospecbox\interactionbarparameter{\blackrule}[\v!lastpage]}%
   \fi}

\protected\def\scrn_bar_goto_a#whereto%
  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!next\fi]}

\protected\def\scrn_bar_goto_b#whereto%
  {\vrule\s!height\d_scrn_bar_height\s!depth\d_scrn_bar_depth\s!width\scratchdimenone\relax}

\protected\def\scrn_bar_goto_c#whereto%
  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!somewhere\or\v!somewhere\or\v!next\fi}

\protected\def\scrn_bar_goto_d#whereto%
  {\vrule \s!width\scratchdimenone \ifcase#whereto%
     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \or
     \s!height.5\d_scrn_bar_height \s!depth.5\d_scrn_bar_depth \or
     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \or
     \s!height.5\d_scrn_bar_height \s!depth.5\d_scrn_bar_depth \else
     \s!height  \d_scrn_bar_height \s!depth  \d_scrn_bar_depth \fi}

\newconstant\c_scrn_bar_mode

\let\scrn_bar_goto_indeed\relax

\protected\def\scrn_bar_goto_x#command%
  {\doifelse{\interactionbarparameter\c!symbol}\v!yes
     {\setupsymbolset[\interactionparameter\c!symbolset]%
      \let\scrn_bar_goto_indeed\scrn_bar_goto_a}
     {\let\scrn_bar_goto_indeed\scrn_bar_goto_b}%
   \dorecurse\nofsubpages
    %{\scratchcounter\numexpr\recurselevel+\firstsubpage+\minusone\relax
     {\scratchcounter\therealsubpageno\recurselevel
      \c_scrn_bar_mode
        \ifnum\scratchcounter<\realpageno \zerocount \else
        \ifnum\scratchcounter=\realpageno \plusone   \else
                                          \plustwo   \fi\fi
      \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[page(\the\scratchcounter)]}%
      #command}%
   \unskip}

\def\scrn_bar_alternative_d
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \scratchdimenone\d_scrn_bar_width
     \noindent\hbox{\scrn_bar_goto_x{\hskip\d_scrn_bar_distance}}% \hpack ?
   }\fi}

\def\scrn_bar_alternative_e
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \scratchdimentwo\dimexpr\nofsubpages\d_scrn_bar_distance-\d_scrn_bar_distance\relax % (n-1)
     \scratchdimenone\dimexpr(\d_scrn_bar_width-\scratchdimentwo)/\nofsubpages\relax
     \ifdim\scratchdimenone<\d_scrn_bar_distance
       \scrn_bar_alternative_f
     \else
       \noindent\hbox to \d_scrn_bar_width{\scrn_bar_goto_x{\hss}\unskip}% \hpack ?
     \fi
   }\fi}

\def\scrn_bar_alternative_f
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \noindent \hbox to \d_scrn_bar_width \bgroup
       \doloop
         {\scratchcounterthree\numexpr(\nofsubpages/\recurselevel)+\plusone\relax % rounding
          \scratchdimentwo\d_scrn_bar_distance
          \multiplyby\scratchdimentwo \scratchcounterthree
          \advanceby\scratchdimentwo -\d_scrn_bar_distance
          \scratchdimenone\d_scrn_bar_width
          \advanceby\scratchdimenone -\scratchdimentwo
          \divideby\scratchdimenone \scratchcounterthree
          \ifdim\scratchdimenone<\d_scrn_bar_distance\else
            \scratchcountertwo\recurselevel
            \exitloop
          \fi}%
       \ifnum\scratchcounterthree>\plusone
         % this is not that well tested
         \advanceby\scratchcounterthree \minustwo
         \scratchdimenone-\d_scrn_bar_distance
         \scratchdimenone\scratchcounterthree\scratchdimenone
         \advanceby\scratchdimenone \d_scrn_bar_width
         \advanceby\scratchcounterthree \plusone
         \divideby\scratchdimenone \scratchcounterthree
       \fi
       \doifelse{\interactionbarparameter\c!symbol}\v!yes
         {\setupsymbolset[\interactionparameter\c!symbolset]%
          \let\scrn_bar_goto_indeed\scrn_bar_goto_c}%
         {\let\scrn_bar_goto_indeed\scrn_bar_goto_d}%
       \scratchcounterthree\numexpr\realpageno-\plustwo\relax
       \scratchcounterfour\numexpr\realpageno+\plustwo\relax
       \ifnum\scratchcounterthree<\plusone \scratchcounterthree\plusone \fi
       \scratchcounterfive\zerocount
       \dostepwiserecurse\firstsubpage\lastsubpage\plusone
         {\donefalse
          \advanceby\scratchcounterfive \plusone
          \ifnum\recurselevel=\firstsubpage\relax \donetrue \fi
          \ifnum\recurselevel=\lastsubpage \relax \donetrue \fi
          \scratchcountersix\therealsubpageno\recurselevel\relax
          \c_scrn_bar_mode
            \ifdone
              \ifnum\scratchcountersix<\realpageno
                \zerocount
              \orelse\ifnum\scratchcountersix>\realpageno
                \plustwo
              \else
                \plusfour
              \fi
            \else
              \ifnum\scratchcounterfive=\scratchcountertwo
                \ifnum\scratchcountersix<\realpageno
                  \plusone
                \orelse\ifnum\scratchcountersix>\realpageno
                  \plusthree
                \else
                  \plustwo
                \fi
              \else
                \minusone
              \fi
            \fi
          \ifnum\c_scrn_bar_mode<\zerocount\else
            \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[realpage(\the\scratchcountersix)]}%
            \hss
            \scratchcounterfive\zerocount
          \fi}%
       \unskip
     \egroup
   }\fi}

\def\scrn_bar_alternative_g
  {\ifnum\lastsubpage>\firstsubpage\relax % no test for state?
     \interactionbuttons[\v!firstsubpage,\v!previoussubpage,\v!nextsubpage,\v!lastsubpage]%
   \fi}

\setupinteractionbar
  [\c!state=\v!start,
   \c!alternative=a,
   \c!symbol=\v!no,
   \c!width=10\emwidth,
   \c!height=.5\emwidth,
   \c!depth=\zeropoint,
   \c!distance=.5\emwidth,
   \c!step=\v!medium,
   \c!foregroundcolor=\interactionbarparameter\c!color,
   \c!foregroundstyle=\interactionbarparameter\c!style,
   \c!color=\interactionparameter\c!color,
   \c!contrastcolor=\interactionparameter\c!contrastcolor,
   \c!style=,
   \c!frame=\v!on,
   \c!background=color,
   \c!backgroundcolor=gray,
   \c!samepage=\v!yes]

\defineinteractionbar[a][\c!command=\scrn_bar_alternative_a]
\defineinteractionbar[b][\c!command=\scrn_bar_alternative_b,\c!height=\v!broad]
\defineinteractionbar[c][\c!command=\scrn_bar_alternative_c,\c!height=\v!max,\c!depth=\v!max]
\defineinteractionbar[d][\c!command=\scrn_bar_alternative_d,\c!width=.5\emwidth]
\defineinteractionbar[e][\c!command=\scrn_bar_alternative_e]
\defineinteractionbar[f][\c!command=\scrn_bar_alternative_f]
\defineinteractionbar[g][\c!command=\scrn_bar_alternative_g,\c!height=\v!broad]

\protect \endinput