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