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 / FBROWSER.C < prev    next >
C/C++ Source or Header  |  1992-02-04  |  10KB  |  318 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.  * a Dialog for browsing fonts
  25.  */
  26.  
  27. #include "fbrowser.h"
  28.  
  29. #include "cyclebut.h"
  30. #include "fsampler.h"
  31. #include "stringbu.h"
  32.  
  33. #include <InterViews/box.h>
  34. #include <InterViews/button.h>
  35. #include <InterViews/deck.h>
  36. #include <InterViews/frame.h>
  37. #include <InterViews/glue.h>
  38. #include <InterViews/message.h>
  39. #include <InterViews/sensor.h>
  40. #include <InterViews/streditor.h>
  41. #include <InterViews/tray.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. class ButtonChoice {
  46. public:
  47.     const char* label;
  48.     void* value;
  49. };
  50.  
  51. static ButtonChoice ModeChoices[] = {
  52.     { "family", "family" },
  53.     { "name", "name" },
  54.     { nil }
  55. };
  56.  
  57. static ButtonChoice FamilyChoices[] = {
  58.     { "helvetica", "helvetica" },
  59.     { "times", "times" },
  60.     { "", "a longish font family name" },
  61.     { nil }
  62. };
  63.  
  64. static ButtonChoice WeightChoices[] = {
  65.     { "medium", "medium" },
  66.     { "bold", "bold" },
  67.     { "", "a weight" },
  68.     { nil }
  69. };
  70.  
  71. static ButtonChoice WidthChoices[] = {
  72.     { "normal", "normal" },
  73.     { "", "a width name" },
  74.     { nil }
  75. };
  76.  
  77. static ButtonChoice SlantChoices[] = {
  78.     { "roman", "r" },
  79.     { "oblique", "o" },
  80.     { "italic", "i" },
  81.     { nil }
  82. };
  83.  
  84. static CycleButtonChoice PointChoices[] = {
  85.     { "24 point", "24" },
  86.     { "18 point", "18" },
  87.     { "14 point", "14" },
  88.     { "12 point", "12" },
  89.     { "10 point", "10" },
  90.     { "8 point", "8" },
  91.     { nil }
  92. };
  93.  
  94. static Interactor* ButtonBox (
  95.     const char* name, ButtonState* state, ButtonChoice* choices
  96. ) {
  97.     HBox* box = new HBox();
  98.     box->Align(Center);
  99.     for (int i = 0; choices[i].label != nil; ++i) {
  100.         if (strlen(choices[i].label) == 0) {
  101.             box->Insert(
  102.                 new StringButton(name, state, (char*)choices[i].value)
  103.             );
  104.         } else {
  105.             box->Insert(
  106.                 new RadioButton(name, choices[i].label, state, choices[i].value)
  107.             );
  108.         }
  109.         box->Insert(new HGlue(round(0.05*inch), round(0.05*inch), 0));
  110.     }
  111.     return box;
  112. }
  113.  
  114. FontBrowser::FontBrowser (ButtonState* s, const char* samp) : (s, nil) {
  115.     Init(samp);
  116. }
  117.  
  118. FontBrowser::FontBrowser (
  119.     const char* name, ButtonState* s, const char* samp
  120. ) : (s, nil) {
  121.     SetInstance(name);
  122.     Init(samp);
  123. }
  124.  
  125. FontBrowser::~FontBrowser () {
  126.     Unref(mode);
  127.     Unref(family);
  128.     Unref(weight);
  129.     Unref(slant);
  130.     Unref(width);
  131.     Unref(point);
  132. }
  133.  
  134. void FontBrowser::Init (const char* samp) {
  135.     SetClassName("FontBrowser");
  136.  
  137.     mode = new ButtonState(ModeChoices[0].value);
  138.     family = new ButtonState(FamilyChoices[0].value);
  139.     weight = new ButtonState(WeightChoices[0].value);
  140.     width = new ButtonState(WidthChoices[0].value);
  141.     slant = new ButtonState(SlantChoices[0].value);
  142.     point = new ButtonState(PointChoices[0].value);
  143.  
  144.     sample = new FontSample(samp);
  145.     fontname = new StringEditor(
  146.         (ButtonState*)nil,
  147.         "a very long font name with space for lots of characters", "\r"
  148.     );
  149.  
  150.     Tray* familymode = new Tray();
  151.     Interactor* familylabel = new Message("label", "Family");
  152.     Interactor* familychoices = ButtonBox("choice", family, FamilyChoices);
  153.     Interactor* weightlabel = new Message("label", "Weight");
  154.     Interactor* weightchoices = ButtonBox("choice", weight, WeightChoices);
  155.     Interactor* widthlabel = new Message("label", "Width");
  156.     Interactor* widthchoices = ButtonBox("choice", width, WidthChoices);
  157.     Interactor* slantlabel = new Message("label", "Slant");
  158.     Interactor* slantchoices = ButtonBox("choice", slant, SlantChoices);
  159.     Interactor* pointlabel = new Message("label", "Size");
  160.     Interactor* pointchoices = new CycleButton("choice", point, PointChoices);
  161.     familymode->VBox(
  162.         familymode,
  163.         familychoices,
  164.         new VGlue(round(0.05*inch), round(0.05*inch), 0),
  165.         weightchoices,
  166.         new VGlue(round(0.05*inch), round(0.05*inch), 0),
  167.         widthchoices
  168.     );
  169.     familymode->VBox(
  170.         widthchoices,
  171.         new VGlue(round(0.05*inch), round(0.05*inch), 0),
  172.         slantchoices,
  173.         new VGlue(round(0.05*inch), round(0.05*inch), 0),
  174.         pointchoices,
  175.         familymode
  176.     );
  177.     familymode->HBox(
  178.         familymode,
  179.         familylabel,
  180.         new HGlue(round(0.2*inch), round(0.1*inch), 0),
  181.         familychoices,
  182.         new HGlue(0, 0, hfil),
  183.         familymode
  184.     );
  185.     familymode->Align(VertCenter, familylabel, familychoices);
  186.     familymode->HBox(
  187.         familymode,
  188.         weightlabel,
  189.         new HGlue(round(0.2*inch), round(0.1*inch), 0),
  190.         weightchoices,
  191.         new HGlue(0, 0, hfil),
  192.         familymode
  193.     );
  194.     familymode->Align(VertCenter, weightlabel, weightchoices);
  195.     familymode->HBox(
  196.         familymode,
  197.         widthlabel,
  198.         new HGlue(round(0.2*inch), round(0.1*inch), 0),
  199.         widthchoices,
  200.         new HGlue(0, 0, hfil),
  201.         familymode
  202.     );
  203.     familymode->Align(VertCenter, widthlabel, widthchoices);
  204.     familymode->HBox(
  205.         familymode,
  206.         slantlabel,
  207.         new HGlue(round(0.2*inch), round(0.1*inch), 0),
  208.         slantchoices,
  209.         new HGlue(0, 0, hfil),
  210.         familymode
  211.     );
  212.     familymode->Align(VertCenter, slantlabel, slantchoices);
  213.     familymode->HBox(
  214.         familymode,
  215.         pointlabel,
  216.         new HGlue(round(0.2*inch), round(0.1*inch), 0),
  217.         pointchoices,
  218.         new HGlue(0, 0, hfil),
  219.         familymode
  220.     );
  221.     familymode->Align(VertCenter, pointlabel, pointchoices);
  222.     familymode->Align(Left,
  223.         familylabel, weightlabel, widthlabel, slantlabel, pointlabel
  224.     );
  225.     familymode->Align(Left,
  226.         familychoices, weightchoices, widthchoices, slantchoices, pointchoices
  227.     );
  228.     Interactor* namemode = new VBox(
  229.         new Frame(new MarginFrame(fontname, 2, 2)),
  230.         new VGlue(0, 0, vfil)
  231.     );
  232.  
  233.     modeoptions = new Deck();
  234.     modeoptions->Insert(familymode);
  235.     modeoptions->Insert(namemode);
  236.  
  237.     Insert(
  238.         new MarginFrame(
  239.             new VBox(
  240.                 new ShadowFrame(new MarginFrame(sample, 2, 2), 2, 2),
  241.                 new VGlue(round(0.3*inch), round(0.2*inch), 0),
  242.                 new HBox(
  243.                     new VBox(
  244.                         new HBox(
  245.                             new Message("label", "Browse by"),
  246.                             new HGlue(round(0.1*inch), 0, 0),
  247.                             ButtonBox("choice", mode, ModeChoices)
  248.                         ),
  249.                         new VGlue(round(0.1*inch), round(0.05*inch), 0),
  250.                         new HBox(
  251.                             new HGlue(round(0.3*inch), round(0.2*inch), 0),
  252.                             modeoptions
  253.                         )
  254.                     ),
  255.                     new HGlue(round(0.3*inch), round(0.2*inch), hfil),
  256.                     new PushButton("Quit", state, true)
  257.                 )
  258.             ),
  259.             round(0.5*inch), round(0.4*inch), round(2.0*inch),
  260.             round(0.3*inch), round(0.2*inch), round(1.0*inch)
  261.         )
  262.     );
  263.  
  264.     UpdateFontname();
  265.     input = new Sensor;
  266.     input->Catch(KeyEvent);
  267. }
  268.  
  269. const char* FontBrowser::Fontname () {
  270.     return fontname->Text();
  271. }
  272.  
  273. boolean FontBrowser::Accept () {
  274.     Event e;
  275.     int v;
  276.     void* m;
  277.  
  278.     state->SetValue(0);
  279.     do {
  280.         Read(e);
  281.         e.target->Handle(e);
  282.         mode->GetValue(m);
  283.         if (strcmp((char*)m, "family") == 0) {
  284.             modeoptions->FlipTo(1);
  285.         } else if (strcmp((char*)m, "name") == 0) {
  286.             modeoptions->FlipTo(2);
  287.         }
  288.         UpdateFontname();
  289.         state->GetValue(v);
  290.     } while (v == 0 && e.target != nil);
  291.     return v == 1 || e.target == nil;
  292. }
  293.  
  294. void FontBrowser::UpdateFontname () {
  295.     void* m;
  296.     mode->GetValue(m);
  297.     if (strcmp((char*)m, "family") == 0) {
  298.         void* v;
  299.         family->GetValue(v);
  300.         const char* fam = (char*)v;
  301.         weight->GetValue(v);
  302.         const char* wgt = (char*)v;
  303.         slant->GetValue(v);
  304.         const char* sl = (char*)v;
  305.         width->GetValue(v);
  306.         const char* wid = (char*)v;
  307.         point->GetValue(v);
  308.         const char* pnt = (char*)v;
  309.         char name[1000];
  310.         sprintf(name,
  311.             "*-*-%s-%s-%s-%s-*-*-%s0-*-*-*-*-iso8859-1",
  312.             fam, wgt, sl, wid, pnt
  313.         );
  314.         fontname->Message(name);
  315.     }
  316.     sample->ShowFont(Fontname());
  317. }
  318.