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 / SQUARES / SFRAME.C < prev    next >
C/C++ Source or Header  |  1992-01-07  |  8KB  |  332 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 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.  * Implementation of the frame around a squares view.
  25.  */
  26.  
  27. #if defined(__cplusplus)
  28. #include "sframe.h"
  29. #include "squares.h"
  30. #include "metaview.h"
  31. #include "view.h"
  32. #endif
  33. #include <InterViews/banner.h>
  34. #include <InterViews/border.h>
  35. #include <InterViews/box.h>
  36. #include <InterViews/button.h>
  37. #include <InterViews/event.h>
  38. #include <InterViews/frame.h>
  39. #include <InterViews/glue.h>
  40. #include <InterViews/menu.h>
  41. #include <InterViews/panner.h>
  42. #include <InterViews/scroller.h>
  43. #include <InterViews/sensor.h>
  44. #include <InterViews/shape.h>
  45. #include <InterViews/tray.h>
  46. #include <InterViews/viewport.h>
  47. #include <InterViews/world.h>
  48. #include <stdlib.h>
  49.  
  50. class SquaresFrame;
  51.  
  52. typedef void (SquaresFrame::*MenuFunc)();
  53.  
  54. class Command : public MenuItem {
  55. public:
  56.     Command(const char*, Alignment, SquaresFrame*, MenuFunc);
  57.  
  58.     virtual void Do();
  59. private:
  60.     SquaresFrame* frame;
  61.     MenuFunc func;
  62. };
  63.  
  64. Command::Command(
  65.     const char* str, Alignment al, SquaresFrame* s, MenuFunc f
  66. ) : (str, al) {
  67.     frame = s;
  68.     func = f;
  69. }
  70.  
  71. void Command::Do() {
  72.     (frame->*func)();
  73. }
  74.  
  75. struct MenuInfo {
  76.     const char* str;
  77.     MenuFunc func;
  78. };
  79.  
  80. #if !defined(__cplusplus)
  81. #include "sframe.h"
  82. #include "squares.h"
  83. #include "metaview.h"
  84. #include "view.h"
  85. #endif
  86.  
  87. static MenuInfo mainmenu[] = {
  88.     { "add square", &SquaresFrame::AddSquare },
  89.     { "new view", &SquaresFrame::NewView },
  90.     { "view setup", &SquaresFrame::ViewSetup },
  91.     { "close", &SquaresFrame::Close },
  92.     { "quit", &SquaresFrame::Quit },
  93.     { nil }
  94. };
  95.  
  96. static MenuInfo adjustmenu[] = {
  97.     { "zoom in", &SquaresFrame::ZoomIn },
  98.     { "zoom out", &SquaresFrame::ZoomOut },
  99.     { "normal size", &SquaresFrame::NormalSize },
  100.     { "center view", &SquaresFrame::CenterView },
  101.     { nil }
  102. };
  103.  
  104. static MenuInfo quitmenu[] = {
  105.     { "yes, quit", &SquaresFrame::YesQuit },
  106.     { "no, don't quit", &SquaresFrame::NoQuit },
  107.     { nil }
  108. };
  109.  
  110. int SquaresFrame::nviews;
  111.  
  112. SquaresFrame::SquaresFrame (SquaresFrame* f) {
  113.     view = new SquaresView(f->view->subject);
  114.     Init();
  115.     style = new SquaresMetaView(f->style);
  116.     MakeFrame();
  117. }
  118.  
  119. SquaresFrame::SquaresFrame (SquaresView* v) {
  120.     const char* a;
  121.  
  122.     view = v;
  123.     Init();
  124.     style = new SquaresMetaView;
  125.     a = GetAttribute("panner");
  126.     style->type = (a == nil) ? AdjustByScrollers : AdjustByPanner;
  127.     a = GetAttribute("adjustersize");
  128.     if (*a == 'm') {
  129.     style->size = Medium;
  130.     } else if (*a == 'l') {
  131.     style->size = Large;
  132.     } else {
  133.     style->size = Small;
  134.     }
  135.     style->right = (GetAttribute("left") == nil);
  136.     style->below = (GetAttribute("above") == nil);
  137.     style->hscroll = (GetAttribute("!hscroll") == nil);
  138.     style->vscroll = (GetAttribute("!vscroll") == nil);
  139.     MakeFrame();
  140. }
  141.  
  142. void SquaresFrame::Init () {
  143.     SetClassName("SquaresFrame");
  144.     menu = MakeMenu(new PopupMenu, mainmenu);
  145.     adjust = MakeMenu(new PopupMenu, adjustmenu);
  146.     quit = MakeMenu(new PopupMenu, quitmenu);
  147.     Unref(input);
  148.     input = updownEvents;
  149.     input->Reference();
  150.     viewport = new Viewport(new Frame(view));
  151.     Propagate(false);
  152.     ++nviews;
  153. }
  154.  
  155. SquaresFrame::~SquaresFrame () {
  156.     delete menu;
  157.     delete quit;
  158.     delete style;
  159. }
  160.  
  161. Menu* SquaresFrame::MakeMenu(Menu* menu, MenuInfo* item) {
  162.     for (MenuInfo* i = item; i->str != nil; i++) {
  163.     menu->Include(new Command(i->str, Center, this, i->func));
  164.     }
  165.     return menu;
  166. }
  167.  
  168. void SquaresFrame::MakeFrame () {
  169.     Scene* p = viewport->Parent();
  170.     Interactor* interior;
  171.  
  172.     if (p != nil) {
  173.     p->Remove(viewport);
  174.     }
  175.     if (style->type == AdjustByPanner) {
  176.     interior = PannerFrameInterior();
  177.     } else if (style->type == AdjustByScrollers) {
  178.     interior = ScrollerFrameInterior();
  179.     }
  180.     Insert(interior);
  181. }
  182.  
  183. Interactor* SquaresFrame::ScrollerFrameInterior () {
  184.     Interactor* v = viewport;
  185.     Tray* t = new Tray;
  186.     Interactor* hs, *vs;
  187.     Border* hb, *vb;
  188.     int size;
  189.  
  190.     if (style->size == Small) {
  191.     size = round(0.15*inches);
  192.     } else if (style->size == Medium) {
  193.     size = round(0.20*inches);
  194.     } else if (style->size == Large) {
  195.     size = round(0.25*inches);
  196.     }
  197.  
  198.     if (style->vscroll) {
  199.     vs = new VScroller(viewport, size);
  200.     vb = new VBorder;
  201.  
  202.     if (style->right) {
  203.         t->HBox(t, v, vb, vs, t);
  204.     } else {
  205.         t->HBox(t, vs, vb, v, t);
  206.     }
  207.     t->VBox(t, vb, t);
  208.     } else {
  209.     t->HBox(t, v, t);
  210.     }
  211.  
  212.     if (style->hscroll) {
  213.     hs = new HScroller(viewport, size);
  214.     hb = new HBorder;
  215.  
  216.     if (style->below) {
  217.         t->VBox(t, v, hb, hs, t);
  218.     } else {
  219.         t->VBox(t, hs, hb, v, t);
  220.     }
  221.     t->HBox(t, hb, t);
  222.     } else {
  223.     t->VBox(t, v, t);
  224.     }
  225.  
  226.     if (style->vscroll && style->hscroll) {
  227.     if (style->below) {
  228.         t->VBox(t, vs, hb);
  229.     } else {
  230.         t->VBox(hb, vs, t);
  231.     }
  232.     if (style->right) {
  233.         t->HBox(t, hs, vb);
  234.     } else {
  235.         t->HBox(vb, hs, t);
  236.     }
  237.  
  238.     } else if (style->vscroll) {
  239.     t->VBox(t, vs, t);
  240.  
  241.     } else if (style->hscroll) {
  242.     t->HBox(t, hs, t);
  243.     }
  244.     return t;
  245. }
  246.  
  247. Interactor* SquaresFrame::PannerFrameInterior () {
  248.     Interactor* v = viewport;
  249.     Tray* t = new Tray(v);
  250.     int size;
  251.  
  252.     if (style->size == Small) {
  253.     size = 0;
  254.     } else if (style->size == Medium) {
  255.     size = round(0.75*inches);
  256.     } else if (style->size == Large) {
  257.     size = round(1.0*inches);
  258.     }
  259.     t->Align(style->align, new Frame(new Panner(v, size)));
  260.     t->Propagate(false);
  261.     return t;
  262. }
  263.  
  264. void SquaresFrame::Handle (Event& e) {
  265.     if (e.eventType == DownEvent) {
  266.     if (e.button == RIGHTMOUSE) {
  267.         adjust->Popup(e);
  268.     } else {
  269.         menu->Popup(e);
  270.     }
  271.     }
  272. }
  273.  
  274. void SquaresFrame::AddSquare() {
  275.     view->subject->Add();
  276. }
  277.  
  278. void SquaresFrame::NewView() {
  279.     GetWorld()->InsertToplevel(new SquaresFrame(this), this);
  280. }
  281.  
  282. void SquaresFrame::ViewSetup() {
  283.     Event e;
  284.     Poll(e);
  285.     e.target = this;
  286.     if (style->Popup(e)) {
  287.     MakeFrame();
  288.     Change();
  289.     }
  290. }
  291.  
  292. void SquaresFrame::Close() {
  293.     if (nviews == 1) {
  294.     Event e;
  295.     Poll(e);
  296.     quit->Popup(e);
  297.     } else {
  298.     --nviews;
  299.     delete this;
  300.     }
  301. }
  302.  
  303. void SquaresFrame::Quit() {
  304.     Event e;
  305.     Poll(e);
  306.     quit->Popup(e);
  307. }
  308.  
  309. void SquaresFrame::ZoomIn() {
  310.     viewport->ZoomBy(2.0, 2.0);
  311. }
  312.  
  313. void SquaresFrame::ZoomOut() {
  314.     viewport->ZoomBy(0.5, 0.5);
  315. }
  316.  
  317. void SquaresFrame::NormalSize() {
  318.     viewport->ZoomTo(1.0, 1.0);
  319. }
  320.  
  321. void SquaresFrame::CenterView() {
  322.     viewport->ScrollTo(0.5, 0.5);
  323. }
  324.  
  325. void SquaresFrame::YesQuit() {
  326.     exit(0);
  327. }
  328.  
  329. void SquaresFrame::NoQuit() {
  330.     /* nop */
  331. }
  332.