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

« back to all changes in this revision

Viewing changes to src/dg/replacealltextscmd.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 1997, 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 "gshape.h"
 
23
#include "shapeview.h"
 
24
#include "diagram.h"
 
25
#include "messagedialog.h"
 
26
#include "replacealltextscmd.h"
 
27
 
 
28
ReplaceAllTextsCmd::ReplaceAllTextsCmd(ShapeView *v, const string *p, 
 
29
                const string *o, bool c, bool s, bool n): 
 
30
        ReplaceTextCmd(v, p, o, c, s, n) {}
 
31
 
 
32
void ReplaceAllTextsCmd::Execute() {
 
33
        int count = 0;
 
34
        int replaces = 0;
 
35
        bool error = False;
 
36
        if (*pattern == *replace) {
 
37
                MessageDialog *d = new MessageDialog(
 
38
                        GetMainWindow()->GetWidget(), MessageDialog::ERROR);
 
39
                d->Show("Error", 
 
40
                        "Replace all: find and replace text are identical");
 
41
                GetMainWindow()->SetStatus(
 
42
                        "aborted: identical find and replace");
 
43
                Abort();
 
44
                return;
 
45
        }
 
46
        List<GShape *> shapes2(*view->GetShapes());
 
47
        view->DeselectAll();
 
48
        for (shapes2.first(); !shapes2.done(); shapes2.next()) {
 
49
                int r = 0;
 
50
                Shape *shape = (Shape *)shapes2.cur();
 
51
                if (nameOnly) {
 
52
                        if (shape->HasNameString(pattern, caseSensitive, 
 
53
                            substring)) {
 
54
                                TextShape *ts;
 
55
                                if (shape->IsTextShape())
 
56
                                        ts = ((GShape *)shape)->GetName();
 
57
                                else
 
58
                                        ts = (TextShape *)shape;
 
59
                                textShapes->add(ts);
 
60
                                Point *pt = new Point(
 
61
                                        shape->GetWidth(), shape->GetHeight()); 
 
62
                                oldSizes->add(pt);
 
63
                                const string *name = ts->GetString();
 
64
                                string copy = *name;
 
65
                                oldStrings->add(new string(copy));
 
66
                                if (!error && (substring || *pattern == copy)) {
 
67
                                        r = copy.replace(*pattern, *replace, 
 
68
                                                         caseSensitive);
 
69
                                        if (copy != *name) {
 
70
                                                if (!GetDiagram()->
 
71
                                                        SetText(ts, &copy))
 
72
                                                        error = True;
 
73
                                        }
 
74
                                }
 
75
                        }
 
76
                }
 
77
                else {
 
78
                        List<TextShape *> tshapes;
 
79
                        if (shape->HasString(pattern, caseSensitive, 
 
80
                                                substring, &tshapes)) {
 
81
                                for (tshapes.first(); !tshapes.done(); 
 
82
                                        tshapes.next()) {
 
83
                                        TextShape *ts = tshapes.cur();
 
84
                                        textShapes->add(ts);
 
85
                                        Point *pt = new Point(shape->GetWidth(),
 
86
                                                           shape->GetHeight()); 
 
87
                                        oldSizes->add(pt);
 
88
                                        oldStrings->add(new 
 
89
                                                string(*(ts->GetString())));
 
90
                                        const string *name = ts->GetString();
 
91
                                        string copy = *name;
 
92
                                        if (!error & (substring || 
 
93
                                                        *pattern == copy)) {
 
94
                                                r = copy.replace(*pattern, 
 
95
                                                        *replace, 
 
96
                                                        caseSensitive);
 
97
                                                if (copy != *name) {
 
98
                                                        if (!GetDiagram()->
 
99
                                                        SetText(ts, &copy))
 
100
                                                                error = True;
 
101
                                                }
 
102
                                        }
 
103
                                }
 
104
                                tshapes.empty();
 
105
                        }
 
106
                }
 
107
                if (r > 0) {
 
108
                        count++;
 
109
                        replaces += r;
 
110
                        view->SelectShape(shapes2.cur());
 
111
                }
 
112
        }
 
113
        if (error) {
 
114
                MessageDialog *d = new MessageDialog(
 
115
                                GetMainWindow()->GetWidget(), 
 
116
                                MessageDialog::WARNING);
 
117
                d->SetCenteredOnPointer(False);
 
118
                d->Show("Warning", 
 
119
                        "Replace all is aborted after\n"
 
120
                        "the first encountered error");
 
121
        }
 
122
        if (count == 0)
 
123
                GetMainWindow()->SetStatus("replace all: pattern not found");
 
124
        else {
 
125
                string txt = "replace all: ";
 
126
                txt += count;
 
127
                txt += " shapes found, made ";
 
128
                txt += replaces;
 
129
                txt += " replacements";
 
130
                GetDocument()->IncChanges();
 
131
                SetCmdDone(True);
 
132
                GetMainWindow()->SetStatus(&txt);
 
133
        }
 
134
}