"Fossies" - the Fresh Open Source Software Archive

Member "ivtools-ivtools-2.0.4/src/Unidraw/align.c" (9 Oct 2020, 5646 Bytes) of package /linux/misc/ivtools-ivtools-2.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "align.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 1990, 1991 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  * Alignment command implmentation.
   25  */
   26 
   27 #include <Unidraw/classes.h>
   28 #include <Unidraw/clipboard.h>
   29 #include <Unidraw/editor.h>
   30 #include <Unidraw/grid.h>
   31 #include <Unidraw/iterator.h>
   32 #include <Unidraw/selection.h>
   33 #include <Unidraw/unidraw.h>
   34 #include <Unidraw/viewer.h>
   35 
   36 #include <Unidraw/Commands/align.h>
   37 #include <Unidraw/Commands/datas.h>
   38 #include <Unidraw/Commands/transforms.h>
   39 
   40 #include <Unidraw/Components/grcomp.h>
   41 #include <Unidraw/Components/grview.h>
   42 
   43 #include <Unidraw/Graphic/graphic.h>
   44 
   45 #include <InterViews/transformer.h>
   46 
   47 #include <OS/math.h>
   48 
   49 #include <stream.h>
   50 
   51 #include <IV-2_6/_enter.h>
   52 
   53 /*****************************************************************************/
   54 
   55 ClassId AlignCmd::GetClassId () { return ALIGN_CMD; }
   56 
   57 boolean AlignCmd::IsA (ClassId id) {
   58     return ALIGN_CMD == id || Command::IsA(id);
   59 }
   60 
   61 AlignCmd::AlignCmd (ControlInfo* c, Alignment a1, Alignment a2) : Command(c) {
   62     _align1 = a1;
   63     _align2 = a2;
   64 }
   65 
   66 AlignCmd::AlignCmd (Editor* ed, Alignment a1, Alignment a2) : Command(ed) {
   67     _align1 = a1;
   68     _align2 = a2;
   69 }
   70 
   71 Command* AlignCmd::Copy () {
   72     Command* copy = new AlignCmd(CopyControlInfo(), _align1, _align2);
   73     InitCopy(copy);
   74     return copy;
   75 }
   76 
   77 GraphicComp* AlignCmd::GetReference (GraphicComp* grcomp) {
   78     Clipboard* cb = GetClipboard();
   79     Iterator i;
   80 
   81     cb->SetComp(grcomp, i);
   82     cb->Prev(i);
   83 
   84     if (cb->GetComp(i) == nil) {
   85         cb->First(i);
   86     }
   87     return cb->GetComp(i);
   88 }
   89 
   90 void AlignCmd::GetAlignment (Alignment& a1, Alignment& a2) {
   91     a1 = _align1;
   92     a2 = _align2;
   93 }
   94 
   95 void AlignCmd::Read (istream& in) {
   96     Command::Read(in);
   97     unsigned int align1, align2;
   98     in >> align1 >> align2;
   99     _align1 = align1;
  100     _align2 = align2;
  101 }
  102 
  103 void AlignCmd::Write (ostream& out) {
  104     Command::Write(out);
  105     out << _align1 << " " << _align2 << " ";
  106 }
  107 
  108 /*****************************************************************************/
  109 
  110 ClassId AlignToGridCmd::GetClassId () { return ALIGNTOGRID_CMD; }
  111 
  112 boolean AlignToGridCmd::IsA (ClassId id) {
  113     return ALIGNTOGRID_CMD == id || Command::IsA(id);
  114 }
  115 
  116 AlignToGridCmd::AlignToGridCmd (ControlInfo* c) : Command(c) { }
  117 AlignToGridCmd::AlignToGridCmd (Editor* ed) : Command(ed) { }
  118 
  119 Command* AlignToGridCmd::Copy () {
  120     Command* copy = new AlignToGridCmd(CopyControlInfo());
  121     InitCopy(copy);
  122     return copy;
  123 }
  124 
  125 void AlignToGridCmd::Execute () {
  126     Selection* s = _editor->GetSelection();
  127 
  128     if (!s->IsEmpty()) {
  129         Clipboard* cb = GetClipboard();
  130         Iterator i;
  131 
  132         if (cb == nil) {
  133             for (s->First(i); !s->Done(i); s->Next(i)) {
  134                 s->GetView(i)->Interpret(this);
  135             }
  136 
  137             SetClipboard(cb = new Clipboard);
  138             cb->Init(s);
  139 
  140         } else {
  141             for (cb->First(i); !cb->Done(i); cb->Next(i)) {
  142                 Move(cb->GetComp(i));
  143             }
  144         }
  145         unidraw->Update();
  146     }
  147 }
  148 
  149 void AlignToGridCmd::Unexecute () {
  150     Clipboard* cb = GetClipboard();
  151 
  152     if (cb != nil) {
  153         Iterator i;
  154 
  155         for (cb->Last(i); !cb->Done(i); cb->Prev(i)) {
  156             Unmove(cb->GetComp(i));
  157         }
  158         unidraw->Update();
  159     }
  160 }
  161 
  162 void AlignToGridCmd::Align (GraphicView* gv, float refx, float refy) {
  163     MoveData* md = (MoveData*) Recall(gv->GetGraphicComp());
  164 
  165     if (md == nil) {
  166         Viewer* v = gv->GetViewer();
  167         Grid* grid = (v == nil) ? nil : v->GetGrid();
  168 
  169         if (grid == nil) {
  170             return;
  171         }
  172 
  173         Graphic* g = gv->GetGraphic();
  174         Transformer t;
  175         g->Parent()->TotalTransformation(t);
  176         t.Invert();
  177 
  178         Coord cx = Math::round(refx);
  179         Coord cy = Math::round(refy);
  180 
  181         grid->Constrain(cx, cy);
  182 
  183         float dx, dy, trefx, trefy;
  184 
  185         t.Transform(float(cx), float(cy), dx, dy);
  186         t.Transform(refx, refy, trefx, trefy);
  187 
  188         dx -= trefx;
  189         dy -= trefy;
  190         Store(gv->GetGraphicComp(), new MoveData(dx, dy));
  191 
  192     }
  193     Move(gv->GetGraphicComp());
  194 }
  195     
  196 void AlignToGridCmd::Unalign (GraphicView* gv) { Unmove(gv->GetGraphicComp());}
  197 
  198 void AlignToGridCmd::Move (GraphicComp* gc) {
  199     MoveData* md = (MoveData*) Recall(gc);
  200 
  201     if (md != nil) {
  202         MoveCmd movement(GetEditor(), md->_dx, md->_dy);
  203         gc->Interpret(&movement);
  204     }
  205 }
  206 
  207 void AlignToGridCmd::Unmove (GraphicComp* gc) {
  208     MoveData* md = (MoveData*) Recall(gc);
  209 
  210     if (md != nil) {
  211         MoveCmd movement(GetEditor(), md->_dx, md->_dy);
  212         gc->Uninterpret(&movement);
  213     }
  214 }