]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlParagraph.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / controllers / ControlParagraph.C
1 /**
2  * \file ControlParagraph.C
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                         action = accept ? 1 : 2;
57                 } else if (!token.empty()) {
58                         // Unrecognised token
59                         return false;
60                 }
61         }
62
63         ParagraphParameters * tmp = new ParagraphParameters;
64         tmp->read(lex);
65
66         // For now, only reset the params on "show".
67         // Don't bother checking if the params are different on "update"
68         if (action == 0) {
69                 params_.reset(tmp);
70         } else {
71                 delete tmp;
72         }
73
74         // Read the rest of the data irrespective of "show" or "update"
75         int nset = 0;
76         while (lex.isOK()) {
77                 lex.next();
78                 string const token = lex.getString();
79
80                 if (token.empty())
81                         continue;
82
83                 int Int = 0;
84                 if (token == "\\alignpossible" ||
85                     token == "\\aligndefault" ||
86                     token == "\\ininset") {
87                         lex.next();
88                         Int = lex.getInteger();
89                 } else {
90                         // Unrecognised token
91                         return false;
92                 }
93
94                 ++nset;
95
96                 if (token == "\\alignpossible") {
97                         alignpossible_ = static_cast<LyXAlignment>(Int);
98                 } else if (token == "\\aligndefault") {
99                         aligndefault_ = static_cast<LyXAlignment>(Int);
100                 } else {
101                         ininset_ = Int;
102                 }
103         }
104         if (nset != 3) {
105                 return false;
106         }
107
108         // If "update", then set the activation status of the button controller
109         if (action > 0) {
110                 bool const accept = action == 1;
111                 dialog().bc().valid(accept);
112         }
113         return true;
114 }
115
116
117 void ControlParagraph::clearParams()
118 {
119         params_.reset();
120 }
121
122
123 void ControlParagraph::dispatchParams()
124 {
125         ostringstream data;
126         params().write(data);
127         FuncRequest const fr(LFUN_PARAGRAPH_PARAMS_APPLY, data.str());
128         kernel().dispatch(fr);
129 }
130
131
132 ParagraphParameters & ControlParagraph::params()
133 {
134         BOOST_ASSERT(params_.get());
135         return *params_;
136 }
137
138
139 ParagraphParameters const & ControlParagraph::params() const
140 {
141         BOOST_ASSERT(params_.get());
142         return *params_;
143 }
144
145
146 bool ControlParagraph::inInset() const
147 {
148         return ininset_;
149 }
150
151
152 LyXAlignment ControlParagraph::alignPossible() const
153 {
154         return alignpossible_;
155 }
156
157
158 LyXAlignment ControlParagraph::alignDefault() const
159 {
160         return aligndefault_;
161 }
162
163 } // namespace frontend
164 } // namespace lyx