~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/ed/grid.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
#include "grafport.h"
 
23
#include "scaledialog.h"
 
24
#include "editstubs.h"
 
25
#include "viewer.h"
 
26
#include "config.h"
 
27
#include "grid.h"
 
28
 
 
29
const int Grid::MAX_POINT_DISTANCE = 50;
 
30
const int Grid::MIN_POINT_DISTANCE = 1;
 
31
const int Grid::MAX_GRID_SIZE = 50;
 
32
const int Grid::MIN_GRID_SIZE = 10;
 
33
 
 
34
Grid::Grid(Config *c, DrawWindow *d, Viewer *v) {
 
35
        mainwindow = d;
 
36
        viewer = v;
 
37
        config = c;
 
38
        SetPointDistance(config->GetPointDistance());
 
39
        snap = config->GetPointSnapping();
 
40
        showGrid = config->GetShowGrid();
 
41
        gridSize = min(MAX_GRID_SIZE, max(MIN_GRID_SIZE, config->GetGridSize()));
 
42
        defaultPointDistance = pointDistance;
 
43
        defaultGridSize = gridSize;
 
44
        scaleDialog = new ScaleDialog(d->GetWidget());
 
45
        scaleDialog->Initialize();
 
46
}
 
47
 
 
48
Grid::~Grid() {
 
49
        delete scaleDialog;
 
50
}
 
51
 
 
52
void Grid::SetSnap(bool s) {
 
53
        string txt = "point position is ";
 
54
        if (s)
 
55
                txt += "discrete";
 
56
        else
 
57
                txt += "continuous";
 
58
        mainwindow->SetStatus(&txt);
 
59
        snap = s;
 
60
}
 
61
 
 
62
void Grid::PointDistance() {
 
63
        mainwindow->SetStatus("action: point distance");
 
64
        scaleDialog->SetTitle("Point distance");
 
65
        scaleDialog->SetScaleLabel("Point distance (in pixels)");
 
66
        scaleDialog->SetValueChangedCallback(
 
67
                EditStubs::PointDistanceOKCB, this);
 
68
        scaleDialog->SetScaleValues(MIN_POINT_DISTANCE, MAX_POINT_DISTANCE, 
 
69
                pointDistance, 0, defaultPointDistance);
 
70
        scaleDialog->Popup();
 
71
}
 
72
 
 
73
void Grid::SetPointDistance(int n) {
 
74
        pointDistance = min(MAX_POINT_DISTANCE, max(MIN_POINT_DISTANCE, n));
 
75
        string txt = "point distance set to ";
 
76
        txt += pointDistance;
 
77
        mainwindow->SetStatus(&txt);
 
78
}
 
79
 
 
80
void Grid::SetShowGrid(bool set) {
 
81
        string txt = "grid is ";
 
82
        if (set)
 
83
                txt += "visible";
 
84
        else
 
85
                txt += "invisible";
 
86
        mainwindow->SetStatus(&txt);
 
87
        // if no change then don't draw.
 
88
        if (showGrid == set)
 
89
                return;
 
90
        showGrid = set;
 
91
        DrawGrid();
 
92
}
 
93
 
 
94
void Grid::GridSize() {
 
95
        mainwindow->SetStatus("action: grid size");
 
96
        scaleDialog->SetTitle("Grid size");
 
97
        scaleDialog->SetScaleLabel("Grid size (in pixels)");
 
98
        scaleDialog->SetValueChangedCallback(EditStubs::GridSizeOKCB, this);
 
99
        scaleDialog->SetScaleValues(MIN_GRID_SIZE, MAX_GRID_SIZE, 
 
100
                gridSize, 0, defaultGridSize);
 
101
        scaleDialog->Popup();
 
102
}
 
103
 
 
104
void Grid::SetGridSize(int n) {
 
105
        string txt = "grid size set to ";
 
106
        txt += n;
 
107
        mainwindow->SetStatus(&txt);
 
108
        // if no change then do nothing.
 
109
        if (gridSize == n)
 
110
                return;
 
111
        if (showGrid) {
 
112
                // undraw old.
 
113
                DrawGrid();
 
114
                gridSize = n;
 
115
                gridSize = min(MAX_GRID_SIZE, max(MIN_GRID_SIZE, n));
 
116
                // draw new.
 
117
                DrawGrid();
 
118
        }
 
119
        else
 
120
                gridSize = min(MAX_GRID_SIZE, max(MIN_GRID_SIZE, n));
 
121
}
 
122
 
 
123
void Grid::Snap(Point *pt) {
 
124
        pt->x = Snap(pt->x);
 
125
        pt->y = Snap(pt->y);
 
126
}
 
127
 
 
128
int Grid::Snap(int x) {
 
129
        if (snap) {
 
130
                int save = x;
 
131
                x = (x / pointDistance) * pointDistance;
 
132
                int rest = (save % pointDistance) * 2;
 
133
                if ( rest >= pointDistance)
 
134
                        x = x + pointDistance;
 
135
        }
 
136
        return x;
 
137
}
 
138
 
 
139
void Grid::DrawGrid() {
 
140
        Grafport *grafport = viewer->GetGrafport();
 
141
        if (!check(grafport))
 
142
                return;
 
143
        int width = int(0.5 + grafport->GetWidth());
 
144
        int height = int(0.5 + grafport->GetHeight());
 
145
        int n_x = width / gridSize;
 
146
        int n_y = height / gridSize;
 
147
        int i;
 
148
        for(i=0; i<=n_x; i++) {
 
149
                int x = i * gridSize;
 
150
                grafport->DrawEditDottedGridLine(x, 0, x, height-1);
 
151
        }
 
152
        for(i=0; i<=n_y; i++) {
 
153
                int y = i * gridSize;
 
154
                grafport->DrawEditDottedGridLine(0, y, width-1, y);
 
155
        }
 
156
}