home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IFB / FSAMPLER.C < prev    next >
C/C++ Source or Header  |  1992-02-04  |  5KB  |  164 lines

  1. /*
  2.  * Copyright (c) 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * display a sample of a named Font
  25.  */
  26.  
  27. #include "fsampler.h"
  28.  
  29. #include <InterViews/font.h>
  30. #include <InterViews/painter.h>
  31. #include <InterViews/pattern.h>
  32. #include <InterViews/sensor.h>
  33. #include <InterViews/shape.h>
  34. #include <InterViews/textbuffer.h>
  35. #include <ctype.h>
  36. #include <string.h>
  37.  
  38. static const int BufferSize = 1000;
  39. static const int SampleLines = 2;
  40. static const int TabWidth = 8;
  41.  
  42. FontSample::FontSample(
  43.     const char* sample
  44. ) : (SampleLines, strlen(sample), TabWidth, Reversed) {
  45.     Init(sample);
  46. }
  47.  
  48. FontSample::FontSample(
  49.     const char* name, const char* sample
  50. ) : (SampleLines, strlen(sample), TabWidth, Reversed) {
  51.     SetInstance(name);
  52.     Init(sample);
  53. }
  54.  
  55. FontSample::~FontSample () {
  56.     delete text;
  57.     delete sample;
  58.     delete fontname;
  59.     Unref(font);
  60. }
  61.  
  62. void FontSample::Init (const char* samp) {
  63.     SetClassName("FontSample");
  64.     font = nil;
  65.     fontname = new char [BufferSize];
  66.     sample = new char [BufferSize];
  67.     text = new TextBuffer(sample, 0, BufferSize);
  68.     text->Insert(0, samp, strlen(samp));
  69.     Edit(text);
  70.     input = new Sensor;
  71.     input->Catch(KeyEvent);
  72.     input->Catch(DownEvent);
  73. }
  74.  
  75. void FontSample::Reconfig () {
  76.     Painter* p = new Painter(output);
  77.     p->Reference();
  78.     Unref(output);
  79.     output = p;
  80.     output->SetFont(font);
  81.     TextEditor::Reconfig();
  82.     shape->vshrink = shape->height - shape->vunits;
  83. }
  84.  
  85. void FontSample::ShowFont (const char* name) {
  86.     if (strcmp(name, fontname) != 0) {
  87.         strcpy(fontname, name);
  88.         font = new Font(fontname);
  89.     font->Reference();
  90.         if (output != nil) {
  91.             if (font->Valid()) {
  92.                 output->FillBg(true);
  93.                 output->SetPattern(solid);
  94.                 Reconfig();
  95.                 Resize();
  96.             } else {
  97.                 Select(Dot());
  98.                 output->ClearRect(canvas, 0, 0, xmax, ymax);
  99.                 output->FillBg(false);
  100.                 output->SetPattern(gray);
  101.             }
  102.             Draw();
  103.         }
  104.     }
  105. }
  106.  
  107. void FontSample::Handle (Event& e) {
  108.     if (!font->Valid()) {
  109.         return;
  110.     }
  111.     switch (e.eventType) {
  112.     case KeyEvent:
  113.         if (e.len > 0) {
  114.             char c = e.keystring[0];
  115.             switch(c) {
  116.             case '\006':
  117.                 ForwardCharacter(1);
  118.                 break;
  119.             case '\002':
  120.                 BackwardCharacter(1);
  121.                 break;
  122.             case '\016':
  123.                 ForwardLine(1);
  124.                 break;
  125.             case '\020':
  126.                 BackwardLine(1);
  127.                 break;
  128.             case '\001':
  129.                 BeginningOfLine();
  130.                 break;
  131.             case '\005':
  132.                 EndOfLine();
  133.                 break;
  134.             case '\004':
  135.             case '\010':
  136.             case '\177':
  137.                 if (Dot() != Mark()) {
  138.                     DeleteSelection();
  139.                 } else {
  140.                     DeleteText(c=='\004' ? 1 : -1);
  141.                 }
  142.                 break;
  143.             default:
  144.                 if (c == '\015' || !iscntrl(c)) {
  145.                     if (Dot() != Mark()) {
  146.                         DeleteSelection();
  147.                     }
  148.                     InsertText(c=='\015' ? "\n" : &c, 1);
  149.                 }
  150.             }
  151.         }
  152.         ScrollToSelection();
  153.         break;
  154.     case DownEvent:
  155.         Select(Locate(e.x, e.y));
  156.         do {
  157.             ScrollToView(e.x, e.y);
  158.             SelectMore(Locate(e.x, e.y));
  159.             Poll(e);
  160.         } while (e.leftmouse || e.middlemouse || e.rightmouse);
  161.         break;
  162.     }
  163. }
  164.