home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / gprogs / pme.icn < prev    next >
Text File  |  2000-07-29  |  5KB  |  181 lines

  1. ############################################################################
  2. #    File:     pme.icn
  3. #    Subject:  Program to edit pixmaps
  4. #    Author:   Clinton L. Jeffery
  5. #
  6. #    Date:     November 22, 1996
  7. ############################################################################
  8. #
  9. #   This file is in the public domain.
  10. #
  11. ############################################################################
  12. #
  13. #    Version:  2.0
  14. #
  15. ############################################################################
  16. #
  17. #     An (color) pixmap editor.
  18. #
  19. #     Left, middle, and right buttons draw different colors.
  20. # Press q or ESC to quit; press s to save.  Capital "S" prompts for
  21. # and saves under a new filename.
  22. # Click on the little picture of the mouse to change one of the
  23. # button's colors.  Not very interesting on a monochrome server.
  24. #
  25. ############################################################################
  26. #
  27. #  Requires:  Version 9 graphics
  28. #
  29. ############################################################################
  30. #
  31. #  Links:  wopen, xcompat
  32. #
  33. ############################################################################
  34.  
  35. link wopen
  36. link xcompat
  37. global w, WIDTH, HEIGHT, XBM, LMARGIN
  38. global colors, colorbinds
  39.  
  40. procedure main(argv)
  41.    local i, f, s, xpos, ypos, i8, j, j8, j8Plus, e, x, y
  42.    colors := [ "red", "green", "blue" ]
  43.    i := 1
  44.    XBM := ".xpm"
  45.    WIDTH  := 32
  46.    HEIGHT := 32
  47.    if *argv>0 & argv[1][1:5]=="-geo" then {
  48.       i +:= 1
  49.       if *argv>1 then argv[2] ? {
  50.      WIDTH := integer(tab(many(&digits))) | stop("geo syntax")
  51.      ="x" | stop("geo syntax")
  52.      HEIGHT := integer(tab(0)) | stop("geo syntax")
  53.      i +:= 1
  54.      }
  55.       }
  56.    LMARGIN := WIDTH
  57.    if LMARGIN < 65 then LMARGIN := 65
  58.    if (*argv >= i) &
  59.       (f := open(s := (argv[i] | (argv[i]||(XBM|".xbm"))))) then {
  60.     close(f)
  61.     w := &window := WOpen("label=PixMap", "image="||s, "cursor=off") |
  62.        stop("cannot open window")
  63.     WIDTH  <:= WAttrib(w, "width")
  64.     HEIGHT <:= WAttrib(w, "height")
  65.     LMARGIN := WIDTH
  66.     if LMARGIN < 65 then LMARGIN := 65
  67.     pos := WAttrib("pos")
  68.     pos ? {
  69.        xpos := tab(many(&digits)) | stop(image(pos))
  70.        =","
  71.        ypos := tab(0)
  72.        }
  73.     WAttrib(w, "posx="||xpos, "posy="||ypos,
  74.         "width="||(WIDTH*8+LMARGIN+5), "height="||(HEIGHT*8))
  75.     Event()
  76.     every i := 0 to HEIGHT-1 do {
  77.        i8 := i*8
  78.        every j := 0 to WIDTH-1 do {
  79.           j8 := j*8
  80.           j8Plus := j8 + LMARGIN + 5
  81.           CopyArea(w, w, j, i, 1, 1, j8Plus,   i8)
  82.           CopyArea(w, w, j, i, 1, 1, j8Plus+1, i8)
  83.           CopyArea(w, w, j8Plus, i8, 2, 1, j8Plus+2,i8)
  84.           CopyArea(w, w, j8Plus, i8, 4, 1, j8Plus+4, i8)
  85.           CopyArea(w, w, j8Plus, i8, 8, 1, j8Plus, i8+1)
  86.           CopyArea(w, w, j8Plus, i8, 8, 2, j8Plus, i8+2)
  87.           CopyArea(w, w, j8Plus, i8, 8, 4, j8Plus, i8+4)
  88.           }
  89.        }
  90.     } else {
  91.        w := &window := WOpen("label=PixMap", "cursor=off",
  92.                 "width="||(LMARGIN+WIDTH*8+5),
  93.                 "height="||(HEIGHT*8+5)) |
  94.              stop("cannot open window")
  95.        }
  96.  
  97.    colorbinds := [ XBind(w,"fg="||colors[1]),
  98.           XBind(w,"fg="||colors[2]),
  99.           XBind(w,"fg="||colors[3]) ]
  100.    every i := 1 to 3 do {
  101.       XDrawArc( 4+i*10, HEIGHT+68, 7, 22)
  102.       XFillArc(colorbinds[i], 5+i*10, HEIGHT+70, 5, 20)
  103.       }
  104.    DrawRectangle( 5, HEIGHT+55, 45, 60)
  105.    DrawRectangle( 25, HEIGHT+50, 5, 5)
  106.    DrawCurve(27, HEIGHT+50,
  107.           27, HEIGHT+47,
  108.           15, HEIGHT+39,
  109.           40, HEIGHT+20,
  110.           25, HEIGHT+5)
  111.  
  112.    Fg( "black")
  113.    every i := 0 to HEIGHT-1 do
  114.       every j := 0 to WIDTH-1 do
  115.          DrawRectangle( j*8+LMARGIN+5, i*8, 8, 8)
  116.  
  117.    DrawLine( 0, HEIGHT, WIDTH, HEIGHT, WIDTH, 0)
  118.  
  119.   repeat {
  120.      case e := Event(w) of {
  121.     "q"|"\e": return
  122.     "s"|"S": {
  123.        if /s | (e=="S") then s := getfilename()
  124.        write("saving image ", s, " with width ", image(WIDTH),
  125.          " height ", image(HEIGHT))
  126.        WriteImage( s, 0, 0, WIDTH, HEIGHT)
  127.        }
  128.     &lpress | &ldrag | &mpress | &mdrag | &rpress | &rdrag : {
  129.  
  130.        x := (&x - LMARGIN - 5) / 8
  131.        y := &y / 8
  132.  
  133.        if (y < 0) | (y > HEIGHT-1) | (x > WIDTH) then next
  134.        if (x < 0) then {
  135.           if &x < 21 then getacolor(1, "left")
  136.           else if &x < 31 then getacolor(2, "middle")
  137.           else getacolor(3, "right")
  138.           until Event(w) === (&mrelease | &lrelease | &rrelease)
  139.           }
  140.        else dot(x, y, (-e-1)%3)
  141.        }
  142.     }
  143.      }
  144. end
  145.  
  146. procedure getacolor(n, s)
  147.    local wtmp, theColor
  148.    wtmp := WOpen("label=" || image(s||" button: "), "lines=1") |
  149.       stop("can't open temp window")
  150.    writes(wtmp,"[",colors[n],"] ")
  151.    theColor := read(wtmp) | stop("read fails")
  152.    close(wtmp)
  153.    wtmp := colorbinds[n] | stop("colorbinds[n] fails")
  154.    Fg(wtmp, theColor) | write("XFG(", theColor, ") fails")
  155.    XFillArc(wtmp, 5+n*10, HEIGHT+70, 5, 20)
  156.    colors[n] := theColor
  157. end
  158.  
  159. procedure dot(x, y, color)
  160.    if (x|y) < 0 then fail
  161.    FillRectangle(colorbinds[color+1], x*8+LMARGIN+5, y*8, 8, 8)
  162.    DrawPoint(colorbinds[color+1], x, y)
  163.    DrawRectangle( x*8+LMARGIN+5, y*8, 8, 8)
  164. end
  165.  
  166. procedure getfilename()
  167.    local s, pos, wprompt, rv
  168.    pos := "pos="
  169.    every s := QueryPointer() do pos||:= (s-10)||","
  170.    wprompt := WOpen("label=Enter a filename to save the pixmap",
  171.            "font=12x24", "lines=1", pos[1:-1]) | stop("can't xprompt")
  172.    rv := read(wprompt)
  173.    close(wprompt)
  174.    if not find(XBM, rv) then rv ||:= XBM
  175.    return rv
  176. end
  177.