3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
9 * \author Matthias Ettrich
11 * Full author contact details are available in file CREDITS
17 #pragma implementation
22 #include "BufferView.h"
24 #include "funcrequest.h"
26 #include "lyxcursor.h"
29 #include "WordLangTuple.h"
31 #include "frontends/Painter.h"
32 #include "frontends/mouse_state.h"
34 #include "support/lstrings.h"
35 #include "support/lstrings.h"
39 // Insets default methods
41 // Initialization of the counter for the inset id's,
42 unsigned int Inset::inset_id = 0;
45 : top_x(0), topx_set(false), top_baseline(0), scx(0),
46 id_(inset_id++), owner_(0), par_owner_(0),
47 background_color_(LColor::inherit)
51 Inset::Inset(Inset const & in, bool same_id)
52 : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
53 name_(in.name_), background_color_(in.background_color_)
62 bool Inset::deletable() const
68 bool Inset::directWrite() const
74 Inset::EDITABLE Inset::editable() const
80 void Inset::edit(BufferView *, int, int, mouse_button::state)
84 void Inset::validate(LaTeXFeatures &) const
88 bool Inset::autoDelete() const
94 void Inset::edit(BufferView *, bool)
98 Inset::RESULT Inset::localDispatch(FuncRequest const &)
105 LyXFont const Inset::convertFont(LyXFont const & font) const
110 return LyXFont(font);
116 string const Inset::editMessage() const
118 return _("Opened inset");
122 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
125 return owner()->getLyXText(bv, false);
131 void Inset::setBackgroundColor(LColor::color color)
133 background_color_ = color;
137 LColor::color Inset::backgroundColor() const
139 if (background_color_ == LColor::inherit) {
141 return owner()->backgroundColor();
143 return LColor::background;
145 return background_color_;
149 int Inset::id() const
154 void Inset::id(int id_arg)
159 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
163 bool Inset::forceDefaultParagraphs(Inset const * in) const
166 return owner()->forceDefaultParagraphs(in);
170 int Inset::latexTextWidth(BufferView * bv) const
173 return (owner()->latexTextWidth(bv));
174 return bv->workWidth();
177 // some stuff for inset locking
179 UpdatableInset::UpdatableInset()
180 : Inset(), cursor_visible_(false), block_drawing_(false)
184 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
185 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
189 void UpdatableInset::insetUnlock(BufferView *)
191 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
195 // An updatable inset is highly editable by definition
196 Inset::EDITABLE UpdatableInset::editable() const
198 return HIGHLY_EDITABLE;
202 void UpdatableInset::toggleInsetCursor(BufferView *)
206 void UpdatableInset::showInsetCursor(BufferView *, bool)
210 void UpdatableInset::hideInsetCursor(BufferView *)
214 void UpdatableInset::fitInsetCursor(BufferView *) const
218 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
222 void UpdatableInset::edit(BufferView *, bool)
226 void UpdatableInset::draw(BufferView *, LyXFont const &,
227 int /* baseline */, float & x,
228 bool/*cleared*/) const
231 // ATTENTION: don't do the following here!!!
233 // top_baseline = baseline;
237 void UpdatableInset::scroll(BufferView * bv, float s) const
246 int const workW = bv->workWidth();
247 int const tmp_top_x = top_x - scx;
250 (tmp_top_x + width(bv, font)) < workW)
252 if (s > 0 && top_x > 0)
255 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
256 //int const save_scx = scx;
258 scx = int(s * workW / 2);
262 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
263 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
266 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
269 void UpdatableInset::scroll(BufferView * bv, int offset) const
272 if (!scx && top_x >= 20)
274 if ((top_x + offset) > 20)
276 // scx += offset - (top_x - scx + offset - 20);
281 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
283 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
284 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
289 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
293 /// An updatable inset could handle lyx editing commands
294 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
296 if (ev.action == LFUN_MOUSE_RELEASE)
297 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
299 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
300 if (ev.argument.find('.') != ev.argument.npos) {
301 float const xx = static_cast<float>(strToDbl(ev.argument));
302 scroll(ev.view(), xx);
304 int const xx = strToInt(ev.argument);
305 scroll(ev.view(), xx);
307 ev.view()->updateInset(this, false);
315 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
319 w = static_cast<UpdatableInset*>
320 (owner())->getMaxWidth(bv, this);
322 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
327 // check for margins left/right and extra right margin "const 5"
328 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
329 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
330 if (topx_set && owner()) {
333 } else if (owner()) {
334 // this is needed as otherwise we might have a too large inset if
335 // its top_x afterwards changes to LeftMargin so we try to put at
336 // least the default margin as top_x
346 LyXCursor const & Inset::cursor(BufferView * bv) const
349 return owner()->getLyXText(bv, false)->cursor;
350 return bv->text->cursor;
355 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
357 // we have to unlock ourself in this function by default!
358 bv->unlockInset(const_cast<UpdatableInset *>(this));
360 return WordLangTuple();
364 bool UpdatableInset::searchForward(BufferView * bv, string const &,
367 // we have to unlock ourself in this function by default!
368 bv->unlockInset(const_cast<UpdatableInset *>(this));
373 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
376 // we have to unlock ourself in this function by default!
377 bv->unlockInset(const_cast<UpdatableInset *>(this));