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