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;
42 : top_x(0), topx_set(false), top_baseline(0), scx(0),
43 id_(inset_id++), owner_(0), par_owner_(0),
44 background_color_(LColor::inherit)
48 Inset::Inset(Inset const & in, bool same_id)
49 : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
50 name_(in.name_), background_color_(in.background_color_)
59 bool Inset::directWrite() const
65 Inset::EDITABLE Inset::editable() const
71 void Inset::edit(BufferView *, int, int, mouse_button::state)
75 void Inset::validate(LaTeXFeatures &) const
79 bool Inset::autoDelete() const
85 void Inset::edit(BufferView *, bool)
89 Inset::RESULT Inset::localDispatch(FuncRequest const &)
96 LyXFont const Inset::convertFont(LyXFont const & font) const
101 return LyXFont(font);
107 string const Inset::editMessage() const
109 return _("Opened inset");
113 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
116 return owner()->getLyXText(bv, false);
122 void Inset::setBackgroundColor(LColor::color color)
124 background_color_ = color;
128 LColor::color Inset::backgroundColor() const
130 if (background_color_ == LColor::inherit) {
132 return owner()->backgroundColor();
134 return LColor::background;
136 return background_color_;
140 int Inset::id() const
145 void Inset::id(int id_arg)
150 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
154 bool Inset::forceDefaultParagraphs(Inset const * in) const
157 return owner()->forceDefaultParagraphs(in);
161 int Inset::latexTextWidth(BufferView * bv) const
164 return (owner()->latexTextWidth(bv));
165 return bv->workWidth();
168 // some stuff for inset locking
170 UpdatableInset::UpdatableInset()
171 : Inset(), cursor_visible_(false), block_drawing_(false)
175 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
176 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
180 void UpdatableInset::insetUnlock(BufferView *)
182 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
186 // An updatable inset is highly editable by definition
187 Inset::EDITABLE UpdatableInset::editable() const
189 return HIGHLY_EDITABLE;
193 void UpdatableInset::toggleInsetCursor(BufferView *)
197 void UpdatableInset::showInsetCursor(BufferView *, bool)
201 void UpdatableInset::hideInsetCursor(BufferView *)
205 void UpdatableInset::fitInsetCursor(BufferView *) const
209 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
213 void UpdatableInset::edit(BufferView *, bool)
217 void UpdatableInset::draw(BufferView *, LyXFont const &,
218 int /* baseline */, float & x,
219 bool/*cleared*/) const
222 // ATTENTION: don't do the following here!!!
224 // top_baseline = baseline;
228 void UpdatableInset::scroll(BufferView * bv, float s) const
237 int const workW = bv->workWidth();
238 int const tmp_top_x = top_x - scx;
241 (tmp_top_x + width(bv, font)) < workW)
243 if (s > 0 && top_x > 0)
246 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
247 //int const save_scx = scx;
249 scx = int(s * workW / 2);
253 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
254 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
257 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
260 void UpdatableInset::scroll(BufferView * bv, int offset) const
263 if (!scx && top_x >= 20)
265 if ((top_x + offset) > 20)
267 // scx += offset - (top_x - scx + offset - 20);
272 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
274 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
275 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
280 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
284 /// An updatable inset could handle lyx editing commands
285 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
287 if (ev.action == LFUN_MOUSE_RELEASE)
288 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
290 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
291 if (ev.argument.find('.') != ev.argument.npos) {
292 float const xx = static_cast<float>(strToDbl(ev.argument));
293 scroll(ev.view(), xx);
295 int const xx = strToInt(ev.argument);
296 scroll(ev.view(), xx);
298 ev.view()->updateInset(this, false);
306 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
310 w = static_cast<UpdatableInset*>
311 (owner())->getMaxWidth(bv, this);
313 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
318 // check for margins left/right and extra right margin "const 5"
319 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
320 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
321 if (topx_set && owner()) {
324 } else if (owner()) {
325 // this is needed as otherwise we might have a too large inset if
326 // its top_x afterwards changes to LeftMargin so we try to put at
327 // least the default margin as top_x
337 LyXCursor const & Inset::cursor(BufferView * bv) const
340 return owner()->getLyXText(bv, false)->cursor;
341 return bv->text->cursor;
346 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
348 // we have to unlock ourself in this function by default!
349 bv->unlockInset(const_cast<UpdatableInset *>(this));
351 return WordLangTuple();
355 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
357 // we have to unlock ourself in this function by default!
358 bv->unlockInset(const_cast<UpdatableInset *>(this));
363 bool UpdatableInset::searchForward(BufferView * bv, string const &,
366 // we have to unlock ourself in this function by default!
367 bv->unlockInset(const_cast<UpdatableInset *>(this));
372 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
375 // we have to unlock ourself in this function by default!
376 bv->unlockInset(const_cast<UpdatableInset *>(this));