]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlParagraph.C
Rename .C => .cpp for files in src/frontends/controllers, step 1
[lyx.git] / src / frontends / controllers / ControlParagraph.C
1 /**
2  * \file ControlParagraph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlParagraph.h"
15 #include "ButtonController.h"
16 #include "funcrequest.h"
17 #include "lyxlex.h"
18 #include "paragraph.h"
19 #include "ParagraphParameters.h"
20
21 #include <sstream>
22
23 using std::istringstream;
24 using std::ostringstream;
25 using std::string;
26
27 namespace lyx {
28 namespace frontend {
29
30 ControlParagraph::ControlParagraph(Dialog & parent)
31         : Dialog::Controller(parent), ininset_(false)
32 {}
33
34
35 bool ControlParagraph::initialiseParams(string const & data)
36 {
37         istringstream is(data);
38         LyXLex lex(0,0);
39         lex.setStream(is);
40
41         // Set tri-state flag:
42         // action == 0: show dialog
43         // action == 1: update dialog, accept changes
44         // action == 2: update dialog, do not accept changes
45         int action = 0;
46
47         if (lex.isOK()) {
48                 lex.next();
49                 string const token = lex.getString();
50
51                 if (token == "show") {
52                         action = 0;
53                 } else if (token == "update") {
54                         lex.next();
55                         bool const accept = lex.getBool();
56                         if (lex) {
57                                 action = accept ? 1 : 2;
58                         } else {
59                                 // Unrecognised update option
60                                 return false;
61                         }
62                 } else if (!token.empty()) {
63                         // Unrecognised token
64                         return false;
65                 }
66         }
67
68         ParagraphParameters * tmp = new ParagraphParameters;
69         tmp->read(lex);
70
71         // For now, only reset the params on "show".
72         // Don't bother checking if the params are different on "update"
73         if (action == 0) {
74                 params_.reset(tmp);
75         } else {
76                 delete tmp;
77         }
78
79         // Read the rest of the data irrespective of "show" or "update"
80         int nset = 0;
81         while (lex.isOK()) {
82                 lex.next();
83                 string const token = lex.getString();
84
85                 if (token.empty())
86                         continue;
87
88                 int Int = 0;
89                 if (token == "\\alignpossible" ||
90                     token == "\\aligndefault" ||
91                     token == "\\ininset") {
92                         lex.next();
93                         Int = lex.getInteger();
94                 } else {
95                         // Unrecognised token
96                         return false;
97                 }
98
99                 ++nset;
100
101                 if (token == "\\alignpossible") {
102                         alignpossible_ = static_cast<LyXAlignment>(Int);
103                 } else if (token == "\\aligndefault") {
104                         aligndefault_ = static_cast<LyXAlignment>(Int);
105                 } else {
106                         ininset_ = Int;
107                 }
108         }
109         if (nset != 3) {
110                 return false;
111         }
112
113         // If "update", then set the activation status of the button controller
114         if (action > 0) {
115                 bool const accept = action == 1;
116                 dialog().bc().valid(accept);
117         }
118         return true;
119 }
120
121
122 void ControlParagraph::clearParams()
123 {
124         params_.reset();
125 }
126
127
128 void ControlParagraph::dispatchParams()
129 {
130         ostringstream data;
131         params().write(data);
132         FuncRequest const fr(LFUN_PARAGRAPH_PARAMS_APPLY, data.str());
133         kernel().dispatch(fr);
134 }
135
136
137 ParagraphParameters & ControlParagraph::params()
138 {
139         BOOST_ASSERT(params_.get());
140         return *params_;
141 }
142
143
144 ParagraphParameters const & ControlParagraph::params() const
145 {
146         BOOST_ASSERT(params_.get());
147         return *params_;
148 }
149
150
151 bool ControlParagraph::inInset() const
152 {
153         return ininset_;
154 }
155
156
157 LyXAlignment ControlParagraph::alignPossible() const
158 {
159         return alignpossible_;
160 }
161
162
163 LyXAlignment ControlParagraph::alignDefault() const
164 {
165         return aligndefault_;
166 }
167
168 } // namespace frontend
169 } // namespace lyx