~ubuntu-branches/ubuntu/utopic/tcm/utopic

« back to all changes in this revision

Viewing changes to src/tb/pastecelltextscmd.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 1996, 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 "tableviewer.h"
 
23
#include "table.h"
 
24
#include "cell.h"
 
25
#include "celltext.h"
 
26
#include "cellrow.h"
 
27
#include "celltextbuffer.h"
 
28
#include "grafport.h"
 
29
#include "pastecelltextscmd.h"
 
30
 
 
31
const int PasteCellTextsCmd::MIN_BOX_SIZE = 10;
 
32
 
 
33
PasteCellTextsCmd::PasteCellTextsCmd(Table *t, TableViewer *v): Command(t, v) {
 
34
        tviewer = v;
 
35
        buffer = v->GetBuffer();
 
36
        pastedCells = new List<Cell *>;
 
37
        oldStrings = new List<string *>;
 
38
        cellTexts = 0;
 
39
        if (buffer->IsEmpty()) {
 
40
                GetMainWindow()->SetStatus("aborted: empty paste buffer");
 
41
                return;
 
42
        }
 
43
        cellTexts = buffer->GetTexts();
 
44
        MakeBox();
 
45
}
 
46
 
 
47
PasteCellTextsCmd::~PasteCellTextsCmd() {
 
48
        if (cellTexts)
 
49
                cellTexts->clear();
 
50
        oldStrings->clear();
 
51
        delete pastedCells;
 
52
        delete cellTexts;
 
53
        delete oldStrings;
 
54
}
 
55
 
 
56
void PasteCellTextsCmd::MakeBox() {
 
57
        Point bottomRight;
 
58
        topLeft = buffer->GetTopLeft();
 
59
        bottomRight = buffer->GetBottomRight();
 
60
        width = bottomRight.x - topLeft.x;
 
61
        width = max(width, MIN_BOX_SIZE);
 
62
        height = bottomRight.y - topLeft.y;
 
63
        height = max(height, MIN_BOX_SIZE);
 
64
        tviewer->SetPasting(True);
 
65
        Point pt = Scale(&topLeft);
 
66
        // Start mouse operation
 
67
        TrackMouse(TRACK_PRESS, &pt, &pt, &pt);
 
68
}
 
69
 
 
70
void PasteCellTextsCmd::Execute() {
 
71
        Point p = topLeft + delta;
 
72
        Cell *topLeftCell = tviewer->HitCell(p.x, p.y);
 
73
        if (!topLeftCell) {
 
74
                GetMainWindow()->SetStatus(
 
75
                        "aborted: topleft of the box is out of the table");
 
76
                Command::Abort();
 
77
                return;
 
78
        }
 
79
        Point tl1 = Point(topLeftCell->GetRow()->GetNumber(), 
 
80
                          topLeftCell->GetColumn()->GetNumber());
 
81
        List<Point *> *rcPositions = buffer->GetPositions();
 
82
        for (unsigned i=0; i<cellTexts->count(); i++) {
 
83
                CellRow *row;
 
84
                Cell *cell;
 
85
                CellText *t = (*cellTexts)[i];
 
86
                Point pt = *(*rcPositions)[i];
 
87
                pt = pt - buffer->GetRcTopLeft();
 
88
                pt = pt + tl1;
 
89
                if ((row = tviewer->GiveRow(pt.x))) {
 
90
                        if ((cell = row->NthCell(pt.y))) {
 
91
                                string oldText = *cell->GetText();
 
92
                                pastedCells->add(cell);
 
93
                                oldStrings->add(new string (oldText));
 
94
                                // also Check texts.
 
95
                                if (!((Table *)GetDocument())->UpdateText(
 
96
                                                cell, t->GetText())) {
 
97
                                        cell->UpdateText(&oldText);
 
98
                                        GetMainWindow()->SetStatus(
 
99
                                                "aborted: cannot paste text "
 
100
                                                "in this cell");
 
101
                                        return;
 
102
                                }
 
103
                                if (tviewer->IsAutoResize())
 
104
                                        tviewer->RecomputeSizeCell(cell);
 
105
                        }
 
106
                }
 
107
        } 
 
108
        Command::Execute();
 
109
        GetMainWindow()->FitDocument();
 
110
}
 
111
 
 
112
void PasteCellTextsCmd::UnExecute() {
 
113
        if (pastedCells->first() && oldStrings->first()) {
 
114
                do {
 
115
                        pastedCells->cur()->UpdateText(oldStrings->cur());
 
116
                        if (tviewer->IsAutoResize())
 
117
                                tviewer->RecomputeSizeCell(pastedCells->cur());
 
118
                } while (pastedCells->next() && oldStrings->next());
 
119
        }
 
120
        Command::UnExecute();
 
121
}
 
122
 
 
123
void PasteCellTextsCmd::Abort() {
 
124
        DrawOutLine(&delta);
 
125
        tviewer->SetPasting(False);
 
126
        Command::Abort();
 
127
}
 
128
 
 
129
void PasteCellTextsCmd::TrackMouse(TrackType aPhase, Point *anchorPoint,
 
130
        Point*, Point *nextPoint) {
 
131
        switch(aPhase) {
 
132
                case TRACK_PRESS:
 
133
                        GetMainWindow()->SetCursor(MouseCursor::FLEUR);
 
134
                        // draw
 
135
                        delta = *nextPoint - *anchorPoint;
 
136
                        DrawOutLine(&delta);
 
137
                        // save
 
138
                        anchor = *anchorPoint;
 
139
                        break;
 
140
                case TRACK_DRAG:
 
141
                        // erase
 
142
                        DrawOutLine(&delta);
 
143
                        // draw
 
144
                        delta = *nextPoint - anchor;
 
145
                        DrawOutLine(&delta);
 
146
                        break;
 
147
                case TRACK_UP:
 
148
                        GetMainWindow()->SetCursor(MouseCursor::LEFT_PTR);
 
149
                        // erase
 
150
                        DrawOutLine(&delta);
 
151
                        delta = ScaleCorrect(&delta);
 
152
                        MoveCellTexts(&delta);
 
153
                        tviewer->SetPasting(False);
 
154
                        tviewer->ExecuteCommand();
 
155
                        break;
 
156
                default:
 
157
                        error("unknown track type\n");
 
158
                        break;
 
159
        }
 
160
}
 
161
 
 
162
void PasteCellTextsCmd::MoveCellTexts(const Point *d) {
 
163
        for (cellTexts->first(); !cellTexts->done(); cellTexts->next()) {
 
164
                CellText *t = cellTexts->cur();
 
165
                Point newPos = *t->GetPosition() + *d;
 
166
                t->SetPosition(&newPos);
 
167
        }
 
168
}
 
169
 
 
170
void PasteCellTextsCmd::DrawOutLine(const Point *d) {
 
171
        Point p1 = topLeft;
 
172
        Point p2 = ScaleCorrect(d);
 
173
        Point pt = p1 + p2;
 
174
        GetGrafport()->SetLineWidth(1);
 
175
        GetGrafport()->SetLineStyle(LineStyle::DOTTED);
 
176
        GetGrafport()->DrawRectangle(pt.x, pt.y, width, height);
 
177
}