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
19 #include "BufferView.h"
21 #include "funcrequest.h"
23 #include "lyxcursor.h"
26 #include "WordLangTuple.h"
28 #include "frontends/Painter.h"
29 #include "frontends/mouse_state.h"
31 #include "support/lstrings.h"
32 #include "support/lstrings.h"
36 // Insets default methods
38 // Initialization of the counter for the inset id's,
39 unsigned int Inset::inset_id = 0;
43 top_x(0), topx_set(false), top_baseline(0), scx(0),
44 id_(inset_id++), owner_(0), par_owner_(0),
45 background_color_(LColor::inherit)
49 Inset::Inset(Inset const & in, bool same_id)
51 top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
52 name_(in.name_), background_color_(in.background_color_)
61 bool Inset::directWrite() const
67 Inset::EDITABLE Inset::editable() const
73 void Inset::edit(BufferView *, int, int, mouse_button::state)
77 void Inset::validate(LaTeXFeatures &) const
81 bool Inset::autoDelete() const
87 void Inset::edit(BufferView *, bool)
91 Inset::RESULT Inset::localDispatch(FuncRequest const &)
98 LyXFont const Inset::convertFont(LyXFont const & font) const
103 return LyXFont(font);
109 string const Inset::editMessage() const
111 return _("Opened inset");
115 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
118 return owner()->getLyXText(bv, false);
124 void Inset::setBackgroundColor(LColor::color color)
126 background_color_ = color;
130 LColor::color Inset::backgroundColor() const
132 if (background_color_ == LColor::inherit) {
134 return owner()->backgroundColor();
136 return LColor::background;
138 return background_color_;
142 int Inset::id() const
147 void Inset::id(int id_arg)
152 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
156 bool Inset::forceDefaultParagraphs(Inset const * in) const
159 return owner()->forceDefaultParagraphs(in);
163 int Inset::latexTextWidth(BufferView * bv) const
166 return (owner()->latexTextWidth(bv));
167 return bv->workWidth();
170 // some stuff for inset locking
172 UpdatableInset::UpdatableInset()
173 : Inset(), cursor_visible_(false), block_drawing_(false)
177 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
178 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
182 void UpdatableInset::insetUnlock(BufferView *)
184 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
188 // An updatable inset is highly editable by definition
189 Inset::EDITABLE UpdatableInset::editable() const
191 return HIGHLY_EDITABLE;
195 void UpdatableInset::toggleInsetCursor(BufferView *)
199 void UpdatableInset::showInsetCursor(BufferView *, bool)
203 void UpdatableInset::hideInsetCursor(BufferView *)
207 void UpdatableInset::fitInsetCursor(BufferView *) const
211 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
215 void UpdatableInset::edit(BufferView *, bool)
219 void UpdatableInset::draw(BufferView *, LyXFont const &,
220 int /* baseline */, float & x,
221 bool/*cleared*/) const
224 // ATTENTION: don't do the following here!!!
226 // top_baseline = baseline;
230 void UpdatableInset::scroll(BufferView * bv, float s) const
239 int const workW = bv->workWidth();
240 int const tmp_top_x = top_x - scx;
243 (tmp_top_x + width(bv, font)) < workW)
245 if (s > 0 && top_x > 0)
248 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
249 //int const save_scx = scx;
251 scx = int(s * workW / 2);
255 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
256 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
259 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
262 void UpdatableInset::scroll(BufferView * bv, int offset) const
265 if (!scx && top_x >= 20)
267 if ((top_x + offset) > 20)
269 // scx += offset - (top_x - scx + offset - 20);
274 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
276 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
277 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
282 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
286 /// An updatable inset could handle lyx editing commands
287 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
289 if (ev.action == LFUN_MOUSE_RELEASE)
290 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
292 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
293 if (ev.argument.find('.') != ev.argument.npos) {
294 float const xx = static_cast<float>(strToDbl(ev.argument));
295 scroll(ev.view(), xx);
297 int const xx = strToInt(ev.argument);
298 scroll(ev.view(), xx);
300 ev.view()->updateInset(this, false);
308 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
312 w = static_cast<UpdatableInset*>
313 (owner())->getMaxWidth(bv, this);
315 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
320 // check for margins left/right and extra right margin "const 5"
321 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
322 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
323 if (topx_set && owner()) {
326 } else if (owner()) {
327 // this is needed as otherwise we might have a too large inset if
328 // its top_x afterwards changes to LeftMargin so we try to put at
329 // least the default margin as top_x
339 LyXCursor const & Inset::cursor(BufferView * bv) const
342 return owner()->getLyXText(bv, false)->cursor;
343 return bv->text->cursor;
348 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
350 // we have to unlock ourself in this function by default!
351 bv->unlockInset(const_cast<UpdatableInset *>(this));
353 return WordLangTuple();
357 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
359 // we have to unlock ourself in this function by default!
360 bv->unlockInset(const_cast<UpdatableInset *>(this));
365 bool UpdatableInset::searchForward(BufferView * bv, string const &,
368 // we have to unlock ourself in this function by default!
369 bv->unlockInset(const_cast<UpdatableInset *>(this));
374 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
377 // we have to unlock ourself in this function by default!
378 bv->unlockInset(const_cast<UpdatableInset *>(this));