#!srexx

/*
 * sedisplay:
 *
 *   arg(1)                   Not used
 *   arg(2)                   Number of buttons
 *   arg(3 --> arg(1)+2)      Text of buttons
 *   arg(arg(1)+3 --> arg())  Text to display. If omitted, skips a line
 *
 * Return code:
 *
 *       -1    Dialog box unmapped by user action
 *       +i    Button number i
 *
 *   Copyright (C) 1991-1997 by Benaroya et Compagnie
 *   All rights reserved.
 */

sedisplay:procedure
signal on novalue
option setenv mixed

OVER_BU = 5

/* g      = lower(arg(1)) */
g      = 'g'
$nb_bu = arg(2)
nb     = arg()

call dy_start()

line      = 2        /* Line to display */
width_box = 0

do i = $nb_bu+3 to nb
  if arg(i, 'e') then
   { call dy_label 1, line, arg(i)
     width_box = max(width_box, length(arg(i)))
   }
  line = line + 1
end i
width_box = width_box + 3

line_bu = line+2

la_bu = 0
do i = 3 to $nb_bu+2
   k = i-2
   bu.k = arg(i)
   la_bu.k = length(bu.k) + OVER_BU
   la_bu = la_bu + la_bu.k
end i

width_box = max(width_box, la_bu)

if $nb_bu = 1 then
 { /*
    * One button: centered
    */
    pb1 = (width_box - la_bu)/2 - 1
    $bu_display.1 = dy_button(pb1, line_bu, bu.1)
 }
else
 { /*
    * Two or more buttons
    */
    $bu_display.1  = dy_button(2, line_bu, bu.1)
    pbl            = width_box - la_bu.$nb_bu
    $bu_display.$nb_bu = dy_button(pbl, line_bu, bu.$nb_bu)
    if $nb_bu > 2 then
     { /* trace x */
       nb_bl = pbl - (2+la_bu.1)
       do i = 2 to $nb_bu-1
         nb_bl = nb_bl - la_bu.i
       end i
       nb_bl = max(nb_bl, 0)
       nb_bl = nb_bl/($nb_bu-1)
       pz = 2 + la_bu.1 + nb_bl
       do i = 2 to $nb_bu-1
          $bu_display.i = dy_button(pz, line_bu, bu.i)
          pz = pz + la_bu.i + nb_bl
       end i
     }
 }

$rc_display = -1  /* Default */
org_warp = dy_warp()
if g[1] = 'g' then call dy_warp 'on'
else               call dy_warp 'off'
$handle_display = dy_end("sedisplay_proc", 0, 0, width_box, line_bu+3, g)
call dy_map $handle_display
call dy_warp org_warp

call dy_destroy $handle_display
rr = $rc_display

do i = 1 to $nb_bu
   call unsetenv '$bu_display.'i
end
call unsetenv '$nb_bu'
call unsetenv '$handle_display'
call unsetenv '$rc_display'


return rr
