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