]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
New DocBook support
[lyx.git] / src / insets / InsetLine.cpp
1 /**
2  * \file InsetLine.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Abdelrazak Younes
7  * \author André Pönitz
8  * \author Uwe Stöhr
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetLine.h"
16
17 #include "Buffer.h"
18 #include "Cursor.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "Length.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "texstream.h"
29 #include "Text.h"
30
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include <cstdlib>
40 #include <output_docbook.h>
41
42 using namespace std;
43
44 namespace lyx {
45
46 using frontend::Painter;
47
48
49 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
50         : InsetCommand(buf, p), height_(0), offset_(0)
51 {}
52
53
54 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
55 {
56         static ParamInfo param_info_;
57         if (param_info_.empty()) {
58                 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
59                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
60                 param_info_.add("height", ParamInfo::LYX_INTERNAL);
61         }
62         return param_info_;
63 }
64
65
66 docstring InsetLine::screenLabel() const
67 {
68         return _("Horizontal line");
69 }
70
71
72 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
73 {
74         switch (cmd.action()) {
75
76         case LFUN_INSET_MODIFY: {
77                 InsetCommandParams p(LINE_CODE);
78                 // FIXME UNICODE
79                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
80                 if (p.getCmdName().empty()) {
81                         cur.noScreenUpdate();
82                         break;
83                 }
84                 cur.recordUndo();
85                 setParams(p);
86                 break;
87         }
88
89         default:
90                 InsetCommand::doDispatch(cur, cmd);
91                 break;
92         }
93 }
94
95
96 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
97         FuncStatus & status) const
98 {
99         switch (cmd.action()) {
100         case LFUN_INSET_DIALOG_UPDATE:
101         case LFUN_INSET_MODIFY:
102                 status.setEnabled(true);
103                 return true;
104         default:
105                 return InsetCommand::getStatus(cur, cmd, status);
106         }
107 }
108
109
110 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
111 {
112         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
113         int const max_width = mi.base.textwidth;
114
115         Length const width(to_ascii(getParam("width")));
116         dim.wid = mi.base.inPixels(width);
117
118         // assure that the line inset is not outside of the window
119         // check that it doesn't exceed the outer boundary
120         if (dim.wid > max_width)
121                 dim.wid = max_width;
122
123         // set a minimal width
124         int const minw = (dim.wid < 0) ? 24 : 4;
125         dim.wid = max(minw, abs(dim.wid));
126
127         Length height = Length(to_ascii(getParam("height")));
128         height_ = mi.base.inPixels(height);
129
130         // get the length of the parameters in pixels
131         Length offset = Length(to_ascii(getParam("offset")));
132         offset_ = mi.base.inPixels(offset);
133
134         dim.asc = max(fm.maxAscent(), offset_ + height_);
135         dim.des = max(fm.maxDescent(), - offset_);
136 }
137
138
139 void InsetLine::draw(PainterInfo & pi, int x, int y) const
140 {
141         Dimension const dim = dimension(*pi.base.bv);
142
143         // get the surrounding text color
144         Color Line_color = pi.base.font.realColor();
145
146         // the offset is a vertical one
147         // the horizontal dimension must be corrected with the height because
148         // of left and right border of the painted line for big height.
149         pi.pain.line(x + height_/2 + 1,
150                      y - offset_ - height_/2,
151                      x + dim.wid - height_/2 - 2,
152                      y - offset_ - height_/2,
153                      Line_color, Painter::line_solid, height_);
154 }
155
156
157 void InsetLine::latex(otexstream & os, OutputParams const &) const
158 {
159         bool have_offset = true;
160         Length offset_len = Length(to_ascii(getParam("offset")));
161         if (offset_len.value() == 0)
162                 have_offset = false;
163
164         string const offset =
165                 Length(to_ascii(getParam("offset"))).asLatexString();
166         string const width =
167                 Length(to_ascii(getParam("width"))).asLatexString();
168         string const height =
169                 Length(to_ascii(getParam("height"))).asLatexString();
170
171         os << "\\rule";
172         // only output the optional parameter if the offset is not 0
173         if (have_offset)
174                 os      << "[" << from_ascii(offset) << "]";
175         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
176 }
177
178
179 int InsetLine::plaintext(odocstringstream & os,
180         OutputParams const &, size_t) const
181 {
182         os << "\n-------------------------------------------\n";
183         return PLAINTEXT_NEWLINE;
184 }
185
186
187 void InsetLine::docbook(XMLStream & xs, OutputParams const &) const
188 {
189         xs << xml::CR();
190 }
191
192
193 docstring InsetLine::xhtml(XMLStream & xs, OutputParams const &) const
194 {
195         xs << xml::CompTag("hr") << xml::CR();
196         return docstring();
197 }
198
199
200 } // namespace lyx