]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
Fix text direction issue for InsetInfo in RTL context
[lyx.git] / src / insets / InsetFlex.cpp
1 /**
2  * \file InsetFlex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetFlex.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Cursor.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "Language.h"
23 #include "Lexer.h"
24 #include "ParIterator.h"
25 #include "TextClass.h"
26
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
38         : InsetCollapsible(buf), name_(layoutName)
39 {}
40
41
42 InsetFlex::InsetFlex(InsetFlex const & in)
43         : InsetCollapsible(in), name_(in.name_)
44 {}
45
46
47 // special code for InsetFlex when there is not the explicit Flex:: prefix
48 InsetLayout const & InsetFlex::getLayout() const
49 {
50         if (!buffer_)
51                 return DocumentClass::plainInsetLayout();
52
53         DocumentClass const & dc = buffer().params().documentClass();
54         docstring const dname = from_utf8(name_);
55         if (dc.hasInsetLayout(dname))
56                 return dc.insetLayout(dname);
57         return dc.insetLayout(from_utf8("Flex:" + name_));
58 }
59
60
61 InsetLayout::InsetDecoration InsetFlex::decoration() const
62 {
63         InsetLayout::InsetDecoration const dec = getLayout().decoration();
64         return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
65 }
66
67
68 void InsetFlex::write(ostream & os) const
69 {
70         os << "Flex ";
71         string name;
72         if (name_.empty())
73                 name = "undefined";
74         else {
75                 InsetLayout const & il = getLayout();
76                 // use il.name(), since this resolves obsoleted InsetLayout names
77                 if (il.name() == "undefined")
78                         // This is the name of the plain_insetlayout_. We assume that the
79                         // name resolution has failed.
80                         name = name_;
81                 else {
82                         name = to_utf8(il.name());
83                         // Remove the "Flex:" prefix, if it is present
84                         if (support::prefixIs(name, "Flex:"))
85                                 name = support::split(name, ':');
86                 }
87         }
88         os << name << "\n";
89         InsetCollapsible::write(os);
90 }
91
92
93 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
94                 FuncStatus & flag) const
95 {
96         switch (cmd.action()) {
97         case LFUN_INSET_DISSOLVE:
98                 if (!cmd.argument().empty()) {
99                         InsetLayout const & il = getLayout();
100                         InsetLayout::InsetLyXType const type =
101                                 translateLyXType(to_utf8(cmd.argument()));
102                         if (il.lyxtype() == type) {
103                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
104                                 return InsetCollapsible::getStatus(cur, temp_cmd, flag);
105                         } else
106                                 return false;
107                 }
108                 // fall-through
109         default:
110                 return InsetCollapsible::getStatus(cur, cmd, flag);
111         }
112 }
113
114
115 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
116 {
117         switch (cmd.action()) {
118         case LFUN_INSET_DISSOLVE:
119                 if (!cmd.argument().empty()) {
120                         InsetLayout const & il = getLayout();
121                         InsetLayout::InsetLyXType const type =
122                                 translateLyXType(to_utf8(cmd.argument()));
123
124                         if (il.lyxtype() == type) {
125                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
126                                 InsetCollapsible::doDispatch(cur, temp_cmd);
127                         } else
128                                 cur.undispatched();
129                         break;
130                 }
131                 // fall-through
132         default:
133                 InsetCollapsible::doDispatch(cur, cmd);
134                 break;
135         }
136 }
137
138
139 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
140 {
141         BufferParams const & bp = buffer().masterBuffer()->params();
142         InsetLayout const & il = getLayout();
143         docstring custom_label = translateIfPossible(il.labelstring());
144
145         Counters & cnts = bp.documentClass().counters();
146         docstring const & count = il.counter();
147         bool const have_counter = cnts.hasCounter(count);
148         if (have_counter) {
149                 Paragraph const & par = it.paragraph();
150                 if (!par.isDeleted(it.pos())) {
151                         cnts.step(count, utype);
152                         custom_label += ' ' +
153                                 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
154                 } else
155                         custom_label += ' ' + from_ascii("#");
156         }
157         setLabel(custom_label);
158
159         bool const save_counter = have_counter && utype == OutputUpdate;
160         if (save_counter) {
161                 // we assume the counter is local to this inset
162                 // if this turns out to be wrong in some case, we will
163                 // need a layout flag
164                 cnts.saveLastCounter();
165         }
166         InsetCollapsible::updateBuffer(it, utype);
167         if (save_counter)
168                 cnts.restoreLastCounter();
169 }
170
171
172 } // namespace lyx