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)
92 LyXFont const Inset::convertFont(LyXFont const & font) const
103 string const Inset::editMessage() const
105 return _("Opened inset");
109 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
112 return owner()->getLyXText(bv, false);
118 void Inset::setBackgroundColor(LColor::color color)
120 background_color_ = color;
124 LColor::color Inset::backgroundColor() const
126 if (background_color_ == LColor::inherit) {
128 return owner()->backgroundColor();
130 return LColor::background;
132 return background_color_;
136 int Inset::id() const
141 void Inset::id(int id_arg)
146 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
150 bool Inset::forceDefaultParagraphs(Inset const * in) const
153 return owner()->forceDefaultParagraphs(in);
157 int Inset::latexTextWidth(BufferView * bv) const
160 return (owner()->latexTextWidth(bv));
161 return bv->workWidth();
164 // some stuff for inset locking
166 UpdatableInset::UpdatableInset()
167 : Inset(), cursor_visible_(false), block_drawing_(false)
171 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
172 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
176 void UpdatableInset::insetUnlock(BufferView *)
178 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
182 // An updatable inset is highly editable by definition
183 Inset::EDITABLE UpdatableInset::editable() const
185 return HIGHLY_EDITABLE;
189 void UpdatableInset::toggleInsetCursor(BufferView *)
193 void UpdatableInset::showInsetCursor(BufferView *, bool)
197 void UpdatableInset::hideInsetCursor(BufferView *)
201 void UpdatableInset::fitInsetCursor(BufferView *) const
205 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
209 void UpdatableInset::edit(BufferView *, bool)
213 void UpdatableInset::draw(BufferView *, LyXFont const &,
214 int /* baseline */, float & x,
215 bool/*cleared*/) const
218 // ATTENTION: don't do the following here!!!
220 // top_baseline = baseline;
224 void UpdatableInset::scroll(BufferView * bv, float s) const
233 int const workW = bv->workWidth();
234 int const tmp_top_x = top_x - scx;
237 (tmp_top_x + width(bv, font)) < workW)
239 if (s > 0 && top_x > 0)
242 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
243 //int const save_scx = scx;
245 scx = int(s * workW / 2);
249 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
250 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
253 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
256 void UpdatableInset::scroll(BufferView * bv, int offset) const
259 if (!scx && top_x >= 20)
261 if ((top_x + offset) > 20)
263 // scx += offset - (top_x - scx + offset - 20);
268 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
270 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
271 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
276 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
280 /// An updatable inset could handle lyx editing commands
281 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
283 if (ev.action == LFUN_MOUSE_RELEASE)
284 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
286 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
287 if (ev.argument.find('.') != ev.argument.npos) {
288 float const xx = static_cast<float>(strToDbl(ev.argument));
289 scroll(ev.view(), xx);
291 int const xx = strToInt(ev.argument);
292 scroll(ev.view(), xx);
294 ev.view()->updateInset(this, false);
302 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
306 w = static_cast<UpdatableInset*>
307 (owner())->getMaxWidth(bv, this);
309 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
314 // check for margins left/right and extra right margin "const 5"
315 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
316 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
317 if (topx_set && owner()) {
320 } else if (owner()) {
321 // this is needed as otherwise we might have a too large inset if
322 // its top_x afterwards changes to LeftMargin so we try to put at
323 // least the default margin as top_x
333 LyXCursor const & Inset::cursor(BufferView * bv) const
336 return owner()->getLyXText(bv, false)->cursor;
337 return bv->text->cursor;
342 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
344 // we have to unlock ourself in this function by default!
345 bv->unlockInset(const_cast<UpdatableInset *>(this));
347 return WordLangTuple();
351 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
353 // we have to unlock ourself in this function by default!
354 bv->unlockInset(const_cast<UpdatableInset *>(this));
359 bool UpdatableInset::searchForward(BufferView * bv, string const &,
362 // we have to unlock ourself in this function by default!
363 bv->unlockInset(const_cast<UpdatableInset *>(this));
368 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
371 // we have to unlock ourself in this function by default!
372 bv->unlockInset(const_cast<UpdatableInset *>(this));