1  implement View;
 2  include "sys.m";
 3     sys: Sys;
 4  include "draw.m";
 5     draw: Draw;
 6     Rect, Display, Image: import draw;
 7  include "bufio.m";
 8  include "imagefile.m";
 9  include "tk.m";
10     tk: Tk;
11  include   "wmlib.m";
12     wmlib: Wmlib;
13  include "string.m";
14     str: String;
15  View: module
16  {
17     init: fn(ctxt: ref Draw->Context,
                argv: list of string);
18  };
19  init(ctxt: ref Draw->Context,
         argv: list of string)
20  {
21     sys   = load Sys Sys->PATH;
22     draw  = load Draw Draw->PATH;
23     tk    = load Tk Tk->PATH;
24     wmlib = load Wmlib Wmlib->PATH;
25     str   = load String String->PATH;
26     wmlib->init();
27     imageremap := load Imageremap
                          Imageremap->PATH;
28     bufio := load Bufio Bufio->PATH;
29     argv = tl argv;
30     if(argv != nil
         && str->prefix("-x ", hd argv))
31        argv = tl argv;
32     viewer := 0;
33     while(argv != nil){
34        file := hd argv;
35        argv = tl argv;
36        im := ctxt.display.open(file);
37        if(im == nil){
38           idec := filetype(file);
39           if(idec == nil)
40              continue;
41           fd := bufio->open(file,
                          Bufio->OREAD);
42           if(fd == nil)
43              continue;
44           idec->init(bufio);
45           (ri, err) := idec->read(fd);
46           if(ri == nil)
47              continue;
48           (im, err) = imageremap->remap(
                      ri, ctxt.display, 1);
49           if(im == nil)
50              continue;
51        }
52        spawn view(ctxt, im, file,
                     viewer++);
53     }
54  }
55  view(ctxt: ref Draw->Context,
         im: ref Image, file: string,
         viewer: int)
56  {
57     corner := string(25+20*(viewer%5));
58     (nil, file) = str->splitr(file, "/");
59     (t, menubut) := wmlib->titlebar(ctxt.screen,
            " -x "+corner+" -y "+corner+
            " -bd 2 -relief raised",
             "View: "+file, Wmlib->Hide);
60     event := chan of string;
61     tk->namechan(t, event, "event");
62     tk->cmd(t, "frame .im -height " +
                  string im.r.dy() +
                  " -width " +
                  string im.r.dx());
63     tk->cmd(t, "bind . <Configure> "+
                  "{send event resize}");
64     tk->cmd(t, "bind . <Map> "+
                  "{send event resize}");
65     tk->cmd(t, "pack .im -side bottom"+
                  " -fill both -expand 1");
66     tk->cmd(t, "update");
67     t.image.draw(posn(t), im, ctxt.display.ones, im.r.min);
68     for(;;) alt{
69     menu := <-menubut =>
70        if(menu == "exit")
71           return;
72        wmlib->titlectl(t, menu);
73     <-event =>
74        t.image.draw(posn(t), im,
              ctxt.display.ones, im.r.min);
75     }
76  }
77  posn(t: ref Tk->Toplevel): Rect
78  {
79     minx := int tk->cmd(t,
                   ".im cget -actx");
80     miny := int tk->cmd(t,
                   ".im cget -acty");
81     maxx := minx + int tk->cmd(t,
                   ".im cget -actwidth");
82     maxy := miny + int tk->cmd(t,
                   ".im cget -actheight");
83     return ((minx, miny), (maxx, maxy));
84  }
85  filetype(file: string): RImagefile
86  {
87     if(len file>4
         && file[len file-4:]==".gif")
88        r := load RImagefile
                   RImagefile->READGIFPATH;
89     if(len file>4
         && file[len file-4:]==".jpg")
90        r = load RImagefile
                   RImagefile->READJPGPATH;
91     return r;
92  }