home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / tut-code.lha / tut-code / preview / main.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-03  |  4.6 KB  |  192 lines

  1. #include <IV-look/kit.h>
  2. #include <InterViews/adjust.h>
  3. #include <InterViews/background.h>
  4. #include <InterViews/box.h>
  5. #include <InterViews/character.h>
  6. #include <InterViews/composition.h>
  7. #include <InterViews/discretion.h>
  8. #include <InterViews/font.h>
  9. #include <InterViews/glue.h>
  10. #include <InterViews/label.h>
  11. #include <InterViews/texcomp.h>
  12. #include <InterViews/patch.h>
  13. #include <InterViews/place.h>
  14. #include <InterViews/scrbox.h>
  15. #include <InterViews/session.h>
  16. #include <InterViews/shapeof.h>
  17. #include <InterViews/space.h>
  18. #include <InterViews/strut.h>
  19. #include <InterViews/style.h>
  20. #include <InterViews/window.h>
  21. #include <OS/file.h>
  22. #include <OS/math.h>
  23. #include <OS/string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. class DocumentView : public MonoGlyph {
  28. public:
  29.     DocumentView(InputFile*, Style*);
  30.     virtual ~DocumentView();
  31.  
  32.     virtual Adjustable* adjustable() const;
  33. private:
  34.     Composition* page_;
  35.     ScrollBox* box_;
  36.     Style* style_;
  37.     Glyph* begin_par_;
  38.     Glyph* end_par_;
  39.     Glyph* begin_line_;
  40.     Glyph* end_line_;
  41.     Glyph* word_space_;
  42.     Glyph* interline_;
  43.     Glyph* vfil_glue_;
  44.  
  45.     void add(const String&, Style*);
  46. };
  47.  
  48. int main(int argc, char** argv) {
  49.     Session* session = new Session("Text", argc, argv);
  50.     if (argc != 2) {
  51.     fprintf(stderr, "Usage: %s file\n", argv[0]);
  52.     exit(1);
  53.     }
  54.     Style* style = session->style();
  55.     Kit* kit = Kit::instance();
  56.     InputFile* file = InputFile::open(argv[1]);
  57.     if (file == nil) {
  58.     fprintf(stderr, "can't open %s\n", argv[1]);
  59.     exit(1);
  60.     }
  61.     DocumentView* view = new DocumentView(file, style);
  62.     return session->run_window(
  63.     new ApplicationWindow(
  64.         new LRBox(
  65.         kit->inset_frame(
  66.             kit->vscroll_bar(view->adjustable(), style),
  67.             style
  68.         ),
  69.         kit->inset_frame(
  70.             new Background(
  71.             new NaturalSpan(
  72.                 new VCenter(view, 1.0),
  73.                 Coord(4*72.0), Coord(6*72.0)
  74.             ),
  75.             style->background()
  76.             ),
  77.             style
  78.         )
  79.         )
  80.     )
  81.     );
  82. }
  83.  
  84. DocumentView::DocumentView(InputFile* file, Style* s) {
  85.     style_ = s;
  86.     const Font* f = s->font();
  87.     const Color* fg = s->foreground();
  88.     // word_space_ = new Space(2, 0.5, f, fg);
  89.     word_space_ = new Character(' ', f, fg);
  90.     interline_ = new VGlue(0);
  91.     vfil_glue_ = new VGlue(0, fil, 0);
  92.  
  93.     // left-align
  94.     begin_line_ = new VStrut(0);
  95.     end_line_ = new Strut(f, 0, fil, 0);
  96.     begin_par_ = new VStrut(0);
  97.     end_par_ = new Strut(f, 0, fil, 0);
  98.  
  99.     // right-align
  100.     // begin_line_ = new VStrut(0, 0, 0, fil, 0);
  101.     // end_line_ = new Strut(f);
  102.     // begin_par_ = new VStrut(0, 0, 0, fil, 0);
  103.     // end_par_ = new Strut(f);
  104.  
  105.     // center
  106.     // begin_line_ = new VStrut(0, 0, 0, fil, 0);
  107.     // end_line_ = new Strut(f, 0, fil, 0);
  108.     // begin_par_ = new VStrut(0, 0, 0, fil, 0);
  109.     // end_par_ = new Strut(f, 0, fil, 0);
  110.  
  111.     // justify
  112.     // begin_line_ = new VStrut(0);
  113.     // end_line_ = new Strut(f);
  114.     // begin_par_ = new VStrut(0);
  115.     // end_par_ = new Strut(f, 0, fil, 0);
  116.  
  117.     Resource::ref(begin_par_);
  118.     Resource::ref(end_par_);
  119.     Resource::ref(begin_line_);
  120.     Resource::ref(end_line_);
  121.     Resource::ref(word_space_);
  122.     Resource::ref(interline_);
  123.     Resource::ref(vfil_glue_);
  124.  
  125.     box_ = new TBScrollBox;
  126.     page_ = new LRComposition(
  127.     box_, new TeXCompositor(10), nil, 6*72.0, fil, fil, file->length()
  128.     );
  129.     const char* data;
  130.     for (;;) {
  131.     int len = file->read(data);
  132.     if (len <= 0) {
  133.         break;
  134.     }
  135.     add(String(data, len), s);
  136.     }
  137.     page_->append(vfil_glue_);
  138.     page_->repair();
  139.     body(page_);
  140. }
  141.  
  142. DocumentView::~DocumentView() {
  143.     Resource::unref(begin_par_);
  144.     Resource::unref(end_par_);
  145.     Resource::unref(begin_line_);
  146.     Resource::unref(end_line_);
  147.     Resource::unref(word_space_);
  148.     Resource::unref(interline_);
  149.     Resource::unref(vfil_glue_);
  150. }
  151.  
  152. void DocumentView::add(const String& data, Style* s) {
  153.     const char* p = data.string();
  154.     const char* end = p + data.length();
  155.     const Font* f = s->font();
  156.     const Color* fg = s->foreground();
  157.     Glyph* g[256];
  158.     for (int i = 0; i < 256; i++) {
  159.     g[i] = new Character(i, f, fg);
  160.     }
  161.  
  162.     Resource::unref(g['\n']);
  163.     g['\n'] = new Discretionary(
  164.     PenaltyGood,
  165.     end_par_,
  166.     end_par_,
  167.     new Discretionary(0, interline_, vfil_glue_, nil, nil),
  168.     begin_par_
  169.     );
  170.  
  171.     Resource::unref(g[' ']);
  172.     g[' '] = new Discretionary(
  173.     0,
  174.     word_space_,
  175.     end_line_,
  176.     new Discretionary(0, interline_, vfil_glue_, nil, nil),
  177.     begin_line_
  178.     );
  179.  
  180.     Resource::unref(g['\t']);
  181.     // g['\t'] = new ShapeOf(g['M']);
  182.     g['\t'] = new Label("        ", f, fg);
  183.  
  184.     for (; p < end; p++) {
  185.     page_->append(g[*p]);
  186.     }
  187. }
  188.  
  189. Adjustable* DocumentView::adjustable() const {
  190.     return box_;
  191. }
  192.