<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
use X11::Protocol;
use X11::Protocol::Constants
  qw(Exposure_m ButtonPress_m ButtonRelease_m ButtonMotion_m
     PointerMotionHint_m StructureNotify_m
     Expose ButtonPress ButtonRelease MotionNotify
     ClientMessage ConfigureNotify
     Convex Nonconvex InputOutput CopyFromParent Replace
     Origin);

use IO::Select;
use strict;

sub clamp { $_[1] &lt; $_[0] ? $_[0] : $_[1] &gt; $_[2] ? $_[2] : $_[1] }
sub sign { $_[0] ? $_[0] / abs($_[0]) : 0 }
sub min { $_[0] &lt;= $_[1] ? $_[0] : $_[1] }
sub max { $_[0] &gt;= $_[1] ? $_[0] : $_[1] }

# Look and feel parameters to play with:
my $length = 300;
my $thumb = 100;
my $thickness = 20;
my $padding = 5;
my $depth = 2;
my $relief_frac = .1; # relief area / thickness, 0 =&gt; relief doesn't scale
my $trough_rgb = [0xa3a3, 0xa3a3, 0xb3b3];
my $bg_rgb = [0xc6c6, 0xc6c6, 0xd6d6];
my $fill_rgb = [0xb6b6, 0x3030, 0x6060];
my $shade = .5; # 0 =&gt; shadows black, hilights white; 1 =&gt; no shading
# for relief, 0 =&gt; raised, 1 =&gt; sunk, 2 =&gt; ridge, 3 =&gt; groove
my $prog_relief = 1; my $sbar_relief = 1; my $slider_relief = 0;
my $arrow_relief = 0; my $dimple_relief = 1;
my $arrow_change = 1; # these bits will flip when pressed
my $dimple = .3; # size / scrollbar thickness, 0 for none
my $font_frac = .6; # text fills 60% of the height of the progresss bar
# Note that the progress bar prefers scalable fonts, so that it can keep
# the same proportions when the window is resized. Depending on how modern
# your X installation is, this may be nontrivial.
# * The best case is if you have a font that includes both hand-edited
# bitmaps for small sizes and outlines that can be scaled arbitrarily.
# All recent X releases come with bitmaps provided by Adobe for Helvetica,
# so if you also have a corresponding Type 1 outline, that's the best
# choice: 
# (bitmaps for sizes 8, 10, 11, 12, 14, 17, 18, 20, 24, 25, and 34)
#my $fontname = "-adobe-helvetica-medium-r-normal--%d-*-*-*-*-*-iso8859-1";
# (If you're using Debian Linux like me, you'll need to install the
#  gsfonts and gsfonts-x11 packages to get the Type 1 versions. The
#  outline isn't the genuine Adobe version; it's a free clone that
#  can also be accessed directly (without Adobe's bitmaps) as)
my $fontname = "-urw-nimbus sans l-regular-r-normal--%d-*-*-*-*-*-iso8859-1";
# * Recent X releases also include some scalable fonts, though not any
# sans-serif ones. In the following, adobe-utopia can be replaced by
# adobe-courier, bitstream-courier, or bitstream-charter:
#my $fontname = "-adobe-utopia-medium-r-normal--%d-*-*-*-*-*-iso8859-1";
# * Also, recent X servers can scale bitmaps, though the results are usually
# fairly ugly.
# * If your X system predates XLFD (the 14-hyphen names), your font
# selection is probably pretty miniscule; try to pick something around
# 12 pixels:
#my $fontname = "7x13";
my $cursor_id = 132;
my $initial_delay = 0.15; # secs
my $delay = 0.05; # secs
my $accel = 0.5;
my $smooth_progress = 0; # and un-smooth scrollbar
my $text_shading_style = 1; # 0 =&gt; diagonalish, 1 =&gt; squarish

#   +--------------------------------------------------+
#   | main_win	   ^v padding   	[bg]           |
#   | +----------------------------------------------+ |
#   | |#prog_win###########        ^                 |&lt;|
#   | |##########[fill]####        :thickness        |&gt;|
#   | |####################        :                 |:|
#   | |&lt;-------- length -----------:----------------&gt;|:|
#   | |####################        V     [trough]    |:|
#   | +----------------------------------------------+:|
#   |         	     ^v padding 	              :|
#   | +----------------------------------------------+:|
#   | | sbar_win +------------------------+          |:|
#   |&lt;|          |+----+ slider_win +----+| [trough] |:|
#   |&gt;|&lt;-slider-&gt;|| &lt;| |&lt;-lt_win    | |&gt; ||          |:|
#   |:|	  pos  	 |+----+    rt_win-&gt;+----+|          |:|
#   |:|	       	 +------------------------+          |:|
#   |:+----------:------------------------:----------+:|
#   |:         	 :   ^v padding           :           :|
#   +:-----------:------------------------:-----------:+
#    :  	 :	                  :	      :
#    :           :	                  :	      :
#  padding       :&lt;------- thumb --------&gt;:	   padding

my($main_win, $prog_win, $sbar_win, $slider_win, $lt_win, $rt_win);
my($trough_gc, $bg_gc, $fill_gc, $hilite_gc, $shadow_gc); 
my $frac = 0;

my $X = X11::Protocol-&gt;new;
my $cmap = $X-&gt;default_colormap;

my($bg,) = $X-&gt;AllocColor($cmap, (@$bg_rgb)); 
my($trough,) = $X-&gt;AllocColor($cmap, (@$trough_rgb));
my($shadow,) = $X-&gt;AllocColor($cmap, (map($_ * $shade, @$bg_rgb)));
my($hilite,) = $X-&gt;AllocColor($cmap, (map(65535 - $shade * (65535 - $_),
					  @$bg_rgb)));

my $delete_atom = $X-&gt;atom('WM_DELETE_WINDOW');

my $fontsize = $font_frac * $thickness;
my $font = $X-&gt;new_rsrc;
$X-&gt;OpenFont($font, sprintf($fontname, $fontsize));

my $total_wd = 2*$padding + $length;
my $base_wd =  2*$padding + 2*$depth + 4;
my $total_ht = 3*$padding + 2*$thickness;
my $base_ht =  3*$padding + 4*$depth + 3;

my $cursor_font = $X-&gt;new_rsrc;
$X-&gt;OpenFont($cursor_font, "cursor");
my $cursor = $X-&gt;new_rsrc;
$X-&gt;CreateGlyphCursor($cursor, $cursor_font, $cursor_font, $cursor_id,
		      $cursor_id + 1, (0, 0, 0), (65535, 65535, 65535));

$main_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($main_win, $X-&gt;root, InputOutput, CopyFromParent,
		 CopyFromParent, (0, 0), $total_wd, $total_ht, 0,
		 'cursor' =&gt; $cursor, 'background_pixel' =&gt; $bg,
		 'event_mask' =&gt; StructureNotify_m);

$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_ICON_NAME'), $X-&gt;atom('STRING'),
		   8, Replace, "widgets");
$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_NAME'), $X-&gt;atom('STRING'), 8,
		   Replace, "Raw X widgets (X11::Protocol)");
$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_CLASS'), $X-&gt;atom('STRING'), 8,
		   Replace, "widgets\0Widgets");
$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_NORMAL_HINTS'),
		   $X-&gt;atom('WM_SIZE_HINTS'), 32, Replace,
		   pack("Lx16llx16llllllx4", 8|16|128|256, $base_wd, $base_ht,
			3, 2, 1000, 1, $base_wd, $base_ht));
$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_HINTS'), $X-&gt;atom('WM_HINTS'),
		   32, Replace, pack("LLLx24", 1|2, 1, 1));
$X-&gt;ChangeProperty($main_win, $X-&gt;atom('WM_PROTOCOLS'), $X-&gt;atom('ATOM'),
		   32, Replace, pack("L", $delete_atom));

$prog_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($prog_win, $main_win, InputOutput, CopyFromParent,
		 CopyFromParent, ($padding, $padding), $length, $thickness, 0,
		 'background_pixel' =&gt; $trough, 'event_mask' =&gt; Exposure_m);

$sbar_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($sbar_win, $main_win, InputOutput, CopyFromParent,
		 CopyFromParent, ($padding, 2*$padding + $thickness),
		 $length, $thickness, 0,
		 'background_pixel' =&gt; $trough, 'event_mask' =&gt; Exposure_m);

$bg_gc = $X-&gt;new_rsrc;
$X-&gt;CreateGC($bg_gc, $main_win, 'foreground' =&gt; $bg);

$shadow_gc = $X-&gt;new_rsrc;
$X-&gt;CreateGC($shadow_gc, $main_win, 'foreground' =&gt; $shadow);

$hilite_gc = $X-&gt;new_rsrc;
$X-&gt;CreateGC($hilite_gc, $main_win, 'foreground' =&gt; $hilite);

# floor : ceil :: int : away
sub away { sign($_[0]) * int(abs($_[0]) + .9999) }

sub draw_slope_poly {
    my($win, $relief, $dep, $fill, @p) = @_;
    if ($relief &gt; 1) {
	draw_slope_poly($win, $relief ^ 3,  $dep,      $fill,     @p);
	                      $relief &amp;= 1; $dep /= 2; $fill = 0;
    }
    my($tl, $br) = ($hilite_gc, $shadow_gc)[$relief, !$relief];
    my(@gc, @ip); $#gc = $#ip = $#p;
    my $j;
    for $j (-2 .. $#p - 2) {
	my($ix, $iy) = ($p[$j+1][0] - $p[$j][0], $p[$j+1][1] - $p[$j][1]);
	$gc[$j] = $ix &gt; $iy ? $tl : $ix &lt; $iy ? $br : $ix &gt; 0 ? $tl : $br;
	my($ox, $oy) = ($p[$j+2][0] - $p[$j+1][0], $p[$j+2][1] - $p[$j+1][1]);
	if ($ix*$oy &gt; $iy*$ox) {
	    $ix = -$ix; $iy = -$iy;
	} else {
	    $ox = -$ox; $oy = -$oy;
	}
	my($in) = sqrt($ix*$ix + $iy*$iy); $ix /= $in; $iy /= $in; 
	my($on) = sqrt($ox*$ox + $oy*$oy); $ox /= $on; $oy /= $on;
	my($mx, $my) = (($ix + $ox)/2, ($iy + $oy)/2);
	my($mn) = max(abs($mx), abs($my)); $mx /= $mn; $my /= $mn;
	$ip[$j+1][0] = $p[$j+1][0] + away(($dep - 1) * $mx);
	$ip[$j+1][1] = $p[$j+1][1] + away(($dep - 1) * $my);
    }
    $X-&gt;FillPoly($win, $fill, Nonconvex, Origin, map(@{$ip[$_]}, 0 .. $#p))
      if $fill;
    for $j (-1 .. $#p - 1) {
	$X-&gt;FillPoly($win, $gc[$j], Convex, Origin, @{$p[$j]}, @{$ip[$j]},
		     @{$ip[$j + 1]}, @{$p[$j + 1]});
	$X-&gt;PolySegment($win, $gc[$j], @{$p[$j]} =&gt; @{$p[$j+1]},
			@{$ip[$j]} =&gt; @{$ip[$j+1]});
    }
    for $j (-1 .. $#p - 1) {
	$X-&gt;PolySegment($win, $bg_gc, @{$p[$j+1]}, @{$ip[$j+1]})
	  if $gc[$j] != $gc[$j + 1];
    }
}

sub draw_slope {
    my($win, $x, $y, $wd, $ht, $relief) = @_;
    draw_slope_poly($win, $relief, $depth, 0, [$x, $y], [$x + $wd - 1, $y],
		    [$x + $wd - 1, $y + $ht - 1], [$x, $y + $ht - 1]);
}

sub paint_arrow {
    my($win, $x, $y, $s, $dir, $relief) = @_;
    my @s = ($s / 2, $s, $s / 2, 0);
    my @p = ([$x + $s[$dir], $y + $s[$dir - 1]],
	     ($dir &amp; 1 xor $dir &amp; 2) ? [$x, $y] : [$x + $s, $y + $s],
	     ($dir &amp; 2) ? [$x + $s, $y] : [$x, $y + $s]);
    @p[1,2] = @p[2,1] if $dir &amp; 1;
    draw_slope_poly($win, $relief, $depth, $bg_gc, @p);
}

sub paint_slope_circle {
    my($win, $x, $y, $s, $dep, $relief) = @_;
    my($tl, $br) = ($hilite_gc, $shadow_gc)[$relief &amp; 1, !($relief &amp; 1)];
    my @outer = ($x, $y, $s, $s);
    my @inner = ($x + $dep, $y + $dep, $s - 2*$dep, $s - 2*$dep);
    my @tl = (35*64, 160*64);
    my @br = (215*64, 160*64);
    $X-&gt;PolyFillArc($win, $bg_gc, [@outer, 0, 360*64]);
    $X-&gt;PolyFillArc($win, $tl, [@outer, @tl]);
    $X-&gt;PolyArc($win, $tl, [@outer, @tl], [@inner, @tl]);
    $X-&gt;PolyFillArc($win, $br, [@outer, @br]);
    $X-&gt;PolyArc($win, $br, [@outer, @br], [@inner, @br]);
    if ($relief &amp; 2) {
	my @middle = ($x + $depth/2, $y + $depth/2, $s - $depth, $s - $depth);
	$X-&gt;PolyFillArc($win, $br, [@middle, @tl]);
	$X-&gt;PolyFillArc($win, $tl, [@middle, @br]);
    }
    $X-&gt;PolyFillArc($win, $bg_gc, [@inner, 0, 360*64]);
}

my $inner_thick = $thickness - 2 * $depth;
my $slider_pos = $depth;
my $pos_min = $depth;
my $pos_max = $length - $thumb - $depth - 2 * $inner_thick;

$slider_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($slider_win, $sbar_win, InputOutput, CopyFromParent,
		 CopyFromParent, ($slider_pos, $depth),
		 $thumb + 2 * $inner_thick, $inner_thick, 0,
		 'background_pixel' =&gt; $bg,
		 'event_mask' =&gt; Exposure_m | ButtonPress_m | ButtonMotion_m
		                 | PointerMotionHint_m);

$lt_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($lt_win, $slider_win, InputOutput, CopyFromParent,
		 CopyFromParent, (0, 0), $inner_thick, $inner_thick, 0,
		 'background_pixel' =&gt; $trough,
		 'event_mask' =&gt; Exposure_m | ButtonPress_m | ButtonRelease_m);

$rt_win = $X-&gt;new_rsrc;
$X-&gt;CreateWindow($rt_win, $slider_win, InputOutput, CopyFromParent,
		 CopyFromParent, ($thumb + $inner_thick, 0),
		 $inner_thick, $inner_thick, 0,
		 'background_pixel' =&gt; $trough,
		 'event_mask' =&gt; Exposure_m | ButtonPress_m | ButtonRelease_m);

my $lt_state = 0;
my $rt_state = 0;
$X-&gt;MapWindow($lt_win);
$X-&gt;MapWindow($rt_win);

$X-&gt;MapWindow($slider_win);

sub slider_update {
    my($delta, $warp) = @_;
    my $old_pos = $slider_pos;
    $slider_pos = clamp($pos_min, $slider_pos + $delta, $pos_max);
    $X-&gt;WarpPointer(0, 0, 0, 0, 0, 0, $slider_pos - $old_pos, 0) if $warp;
    $X-&gt;ConfigureWindow($slider_win, 'x' =&gt; $slider_pos);
    prog_update(($slider_pos - $pos_min) / ($pos_max - $pos_min), 1);
}


my %extents = $X-&gt;QueryTextExtents($font, "\0001\0000\0000\0%");
my $text_wd = $extents{'overall_width'} + 4+2;
my $text_x = int(($length - $text_wd) / 2);
my $text_baseline = int(($thickness + $extents{'font_ascent'}
			 - $extents{'font_descent'}) / 2) - $depth;

my $prog_pixmap = $X-&gt;new_rsrc;
$X-&gt;CreatePixmap($prog_pixmap, $prog_win, $X-&gt;root_depth,
		 $text_wd, $inner_thick);

$trough_gc = $X-&gt;new_rsrc;
$X-&gt;CreateGC($trough_gc, $prog_pixmap, 'font' =&gt; $font,
	     'foreground' =&gt; $trough);

$fill_gc = $X-&gt;new_rsrc;
my($fill_pixel,) = $X-&gt;AllocColor($cmap, (@$fill_rgb));
$X-&gt;CreateGC($fill_gc, $prog_pixmap, 'font' =&gt; $font,
	     'foreground' =&gt; $fill_pixel);

$X-&gt;ChangeGC($shadow_gc, 'font' =&gt; $font);
$X-&gt;ChangeGC($hilite_gc, 'font' =&gt; $font);
$X-&gt;ChangeGC($bg_gc, 'font' =&gt; $font);

sub paint_shaded_text {
    my($drawable, $x, $y, $text) = @_;
    my($br_gc, $tl_gc) = ($shadow_gc, $hilite_gc);
    $X-&gt;PolyText8($drawable, $br_gc, ($x + 1, $y + 1), @$text)
      if $text_shading_style;
    $X-&gt;PolyText8($drawable, $br_gc, ($x, $y + 1), @$text);
    $X-&gt;PolyText8($drawable, $br_gc, ($x + 1, $y), @$text);

    $X-&gt;PolyText8($drawable, $tl_gc, ($x - 1, $y - 1), @$text)
      if $text_shading_style;
    $X-&gt;PolyText8($drawable, $tl_gc, ($x, $y - 1), @$text);
    $X-&gt;PolyText8($drawable, $tl_gc, ($x - 1, $y), @$text);

    $X-&gt;PolyText8($drawable, $bg_gc, ($x, $y), @$text);
}

my $font_height = $extents{'font_ascent'} + $extents{'font_descent'};

sub prog_update {
    my($newfrac, $increm) = @_;
    my $oldfrac = $frac;
    $frac = $newfrac;
    my $str = int(100 * $frac) . "%";
    my $text = [map([1, $_], split(//, $str))];
    $text-&gt;[1][0] = -$font_height/10 if $text-&gt;[0][1] eq "1"; # kerning
    my $realend = int($frac * ($length - 2 * $depth)) + $depth;
    if ($increm) {
	my $newend = $realend;
	my $oldend = int($oldfrac * ($length - 2 * $depth)) + $depth;
	my $x;
	my($left, $right);
	my $count = 0;
	if ($newend &gt; $oldend) {
	    $right = \$newend; $left = \$oldend;
	} else {
	    $right = \$oldend; $left = \$newend;
	}
	if ($$left &gt;= $text_x and $$left &lt; $text_x + $text_wd) {
	    $$left = $text_x + $text_wd - 1;
	    $count++;
	}
	if ($$right &gt;= $text_x and $$right &lt; $text_x + $text_wd) {
	    $$right = $text_x;
	    $count++;
	}
	if ($count == 2) {
	    # do nothing
	} elsif ($newend &gt; $oldend) {
	    if ($smooth_progress) {
		for ($x = $oldend; $x &lt; $newend; $x++) {
		    $X-&gt;PolySegment($prog_win, $fill_gc, ($x, $depth) =&gt;
				    ($x, $thickness - $depth - 1));
		}
	    } else {		
		$X-&gt;PolyFillRectangle($prog_win, $fill_gc, [($oldend, $depth),
				      $newend - $oldend, $inner_thick]);
	    }
	} elsif ($newend &lt; $oldend) {
	    if ($smooth_progress) {
		for ($x = $oldend - 1; $x &gt;= $newend; $x--) {
		    $X-&gt;PolySegment($prog_win, $trough_gc, ($x, $depth) =&gt;
				    ($x, $thickness - $depth - 1));
		}
	    } else {		
		$X-&gt;PolyFillRectangle($prog_win, $trough_gc,
				      [($newend, $depth), $oldend - $newend,
				       $inner_thick]);
	    }
	}
    } else {
	$X-&gt;PolyFillRectangle($prog_win, $fill_gc, [($depth, $depth),
			      $realend - $depth, $inner_thick]);
    }
    my $end = clamp(0, $realend - $text_x, $text_wd);
    $X-&gt;PolyFillRectangle($prog_pixmap, $fill_gc, [0, 0, $end, $inner_thick])
      if $end &gt; 0;
    $X-&gt;PolyFillRectangle($prog_pixmap, $trough_gc, [$end, 0,
			  $text_wd - $end, $inner_thick])
      if $end &lt; $text_wd;
    $str =~ s/(.)/\0$1/g;
    my $wd = {$X-&gt;QueryTextExtents($font, $str)}-&gt;{'overall_width'};
    paint_shaded_text($prog_pixmap, 1 + int(($text_wd - $wd) / 2),
		      $text_baseline, $text);
    $X-&gt;CopyArea($prog_pixmap, $prog_win, $bg_gc, (0, 0),
		 $text_wd, $inner_thick, ($text_x, $depth));
}

$X-&gt;MapWindow($prog_win);
$X-&gt;MapWindow($sbar_win);
$X-&gt;MapWindow($main_win);

my $fds = IO::Select-&gt;new($X-&gt;connection-&gt;fh);
my $timeout = 0;

my($slider_speed, $pointer_pos, $last_pos);

my(%dirty); my $resize_pending = 0;

# Since this program can't necessarily handle events as fast as the X
# server can generate them, it's important to use some sort of `flow
# control' to throw out excess events when we're behind.

# For pointer motion events, this is accomplished by selecting
# PointerMotionHint on the slider (see above), so that the server
# never sends a sequence of motion events -- instead, it sends one,
# which we throw away but use as our cue to query the pointer
# position. The query_pointer is then a sign to the server that we'd
# be willing to accept one more event, and so on. Notice that this
# requires several round trips between the server and the client for
# each motion, which in C programs is a source of performance
# problems, but here the difference is lost in the noise (we also do a
# round trip to calculate the width of the text when updating the
# progress bar, which could be done on the client side the way Xlib
# does).

# Expose and ConfigureNotify (resize) events have the same problem,
# though it's only noticeable if your window manager supports opaque
# window movement or opaque resize, respectively (the latter is fairly
# rare in X, perhaps because average X clients handle it fairly
# poorly; I for one am quite envious of how smoothly windows resize in
# Windows NT). We can't do anything to tell the server to only send us
# one of these events, but the next best thing is to just ignore them
# until there aren't any other events pending. (In some toolkits this
# would be called `idle-loop' processing). It's always safe to ignore
# intermediate resizes, but with expose events we can only do this
# because we always redraw the whole window, instead of just the
# newly-visible part. A more sophisticated approach would keep track
# of the exposed region, either with a bounding box or some more
# precise data structure, and then clip the drawing to that (either
# client-side or using a clip mask in the GC). Of course, that almost
# certainly wouldn't be a speed win, because it would be doing a lot
# of work in perl to save a few iterations of highly optimized C in
# the server.

$X-&gt;{'event_handler'} = "queue";

for (;;) {
    if ($timeout) {
	while (not $fds-&gt;can_read($timeout)) {
	    slider_update(int $slider_speed, 1);
	    $slider_speed += sign($slider_speed) * $accel;
	    if ($slider_pos == $pos_min or $slider_pos == $pos_max) {
		$timeout = 0;
		last;
	    } else {
		$timeout = $delay;
	    }
	}
    }
    if (not $fds-&gt;can_read(0.001)) {
	if ($resize_pending) {
	    $resize_pending = 0;
	    $total_ht = max($total_ht, $base_ht);
	    $length = $total_wd - 2 * $padding;
	    $thickness = int(($total_ht - 3 * $padding) / 2 + 0.5);
	    $depth = int($relief_frac * $thickness) if $relief_frac;
	    $inner_thick = $thickness - 2*$depth;
	    $thumb = $length / 3;
	    $X-&gt;ConfigureWindow($prog_win, 'width' =&gt; $length,
				'height' =&gt; $thickness);
	    $fontsize = int($font_frac * $thickness);
	    $X-&gt;CloseFont($font);
	    $X-&gt;OpenFont($font, sprintf($fontname, $fontsize));
	    map($X-&gt;ChangeGC($_, 'font' =&gt; $font),
		$bg_gc, $hilite_gc, $shadow_gc);

	    %extents = $X-&gt;QueryTextExtents($font, "\0001\0000\0000\0%");
	    $text_wd = $extents{'overall_width'} + 4+2;
	    $text_x = int(($length - $text_wd) / 2);
	    $text_baseline = int(($thickness + $extents{'font_ascent'}
				  - $extents{'font_descent'}) / 2) - $depth;
	    $font_height = $extents{'font_ascent'} + $extents{'font_descent'};

	    $X-&gt;FreePixmap($prog_pixmap);
	    $X-&gt;CreatePixmap($prog_pixmap, $prog_win, $X-&gt;root_depth,
			     $text_wd, $inner_thick);
	    $X-&gt;ConfigureWindow($sbar_win, 'x' =&gt; $padding,
				'y' =&gt; 2 * $padding + $thickness,
				'width' =&gt; $length, 'height' =&gt; $thickness);
	    $pos_min = $depth;
	    $pos_max = $length - $thumb - $depth - 2 * $inner_thick;
	    $slider_pos = $pos_min + $frac * ($pos_max - $pos_min);
	    $X-&gt;ConfigureWindow($slider_win, 'x' =&gt; $slider_pos, 'y' =&gt; $depth,
				'width' =&gt; $thumb + 2 * $inner_thick,
				'height' =&gt; $inner_thick);
	    $X-&gt;ConfigureWindow($lt_win, 'width' =&gt; $inner_thick,
				'height' =&gt; $inner_thick);
	    $X-&gt;ConfigureWindow($rt_win, 'x' =&gt; $thumb + $inner_thick,
				'width' =&gt; $inner_thick,
				'height' =&gt; $inner_thick)
	}
	if ($dirty{$prog_win}) {
	    draw_slope($prog_win, 0, 0, $length, $thickness, $prog_relief);
	    prog_update($frac, 0);
	    $dirty{$prog_win} = 0;
	}
	if ($dirty{$sbar_win}) {
	    draw_slope($sbar_win, 0, 0, $length, $thickness, $sbar_relief);
	    $dirty{$sbar_win} = 0;
	}
	if ($dirty{$slider_win}) {
	    draw_slope($slider_win, $inner_thick, 0, $thumb,
		       $inner_thick, $slider_relief);
	    paint_slope_circle($slider_win,
			       $thumb / 2 + (2 - $dimple)/2*$inner_thick,
			       (1 - $dimple) * $inner_thick / 2,
			       $dimple * $inner_thick,
			       $depth, $dimple_relief) if $dimple;
	    $dirty{$slider_win} = 0;
	}
	if ($dirty{$lt_win}) {
	    paint_arrow($lt_win, 0, 0, $inner_thick - 1, 3,
			$arrow_relief ^ $lt_state);
	    $dirty{$lt_win} = 0;
	}
	if ($dirty{$rt_win}) {
	    paint_arrow($rt_win, 0, 0, $inner_thick - 1, 1,
			$arrow_relief ^ $rt_state);
	    $dirty{$rt_win} = 0;
	}
    }
    my %e = $X-&gt;next_event;
    if ($e{code} == ClientMessage and unpack("L", $e{data}) == $delete_atom) {
	exit;
    } elsif ($e{code} == ConfigureNotify) {
	if ($e{width} != $total_wd or $e{height} != $total_ht) {
	    $resize_pending++;
 	    ($total_wd, $total_ht) = ($e{width}, $e{height});
	}
    } elsif ($e{code} == Expose) {
	next unless $e{count} == 0;
	my $id = $e{window};
	if ($id == $sbar_win) {
	    if ($e{'x'} &lt; $depth or $e{'y'} &lt; $depth
		or $e{'x'} + $e{width} &gt; $length - $depth
		or $e{'y'} + $e{height} &gt; $thickness - $depth)
	    {
		# In the scrollbar, we throw out exposures that don't
		# include the border (including all the ones caused by
		# moving the slider), since the server fills the
		# trough in with the window's background color
		# automatically.
		$dirty{$sbar_win}++;
	    }
	} else {
	    $dirty{$id}++;	
	}
    } elsif ($e{code} == ButtonPress) {
	my $id = $e{event};
	if ($id == $slider_win) {
	    $pointer_pos = $slider_pos;
	    $last_pos = $e{root_x};
	} elsif ($id == $lt_win) {
	    next if 2*abs($e{event_y} - $inner_thick / 2) &gt; $e{event_x};
	    $lt_state = $arrow_change;
	    slider_update(-1, 1);
	    paint_arrow($lt_win, 0, 0, $inner_thick - 1, 3,
			$arrow_relief ^ $lt_state);
	    $slider_speed = -1;
	    $timeout = $initial_delay;
	} elsif ($id == $rt_win) {
	    next if 2*abs($e{event_y} - $inner_thick / 2)
	      &gt; $inner_thick - $e{event_x};
	    $rt_state = $arrow_change;
	    slider_update(1, 1);
	    paint_arrow($rt_win, 0, 0, $inner_thick - 1, 1,
			$arrow_relief ^ $rt_state);
	    $slider_speed = 1;
	    $timeout = $initial_delay;
	}
    } elsif ($e{code} == MotionNotify) {
	my $id = $e{event};
	if ($id == $slider_win and defined $last_pos) {
	    my %e2 = $X-&gt;QueryPointer($slider_win);
	    $pointer_pos += $e2{'root_x'} - $last_pos;
	    slider_update($pointer_pos - $slider_pos, 0);
	    $last_pos = $e2{'root_x'};
	}
    } elsif ($e{code} == ButtonRelease) {
	my $id = $e{event};
	if ($id == $slider_win and defined $last_pos) {
	    slider_update($e{root_x} - $last_pos, 0);
	    undef $last_pos;
	} elsif ($id == $lt_win) {
	    $lt_state = 0;
	    paint_arrow($lt_win, 0, 0, $inner_thick - 1, 3,
			$arrow_relief ^ $lt_state);
	    $timeout = 0;
	} elsif ($id == $rt_win) {
	    $rt_state = 0;
	    paint_arrow($rt_win, 0, 0, $inner_thick - 1, 1,
			$arrow_relief ^ $rt_state);
	    $timeout = 0;
	}
    }
}
</pre></body></html>