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