1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
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::deletable() const
65 bool Inset::directWrite() const
71 Inset::EDITABLE Inset::editable() const
77 void Inset::edit(BufferView *, int, int, mouse_button::state)
81 void Inset::validate(LaTeXFeatures &) const
85 bool Inset::autoDelete() const
91 void Inset::edit(BufferView *, bool)
95 Inset::RESULT Inset::localDispatch(FuncRequest const &)
102 LyXFont const Inset::convertFont(LyXFont const & font) const
107 return LyXFont(font);
113 string const Inset::editMessage() const
115 return _("Opened inset");
119 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
122 return owner()->getLyXText(bv, false);
128 void Inset::setBackgroundColor(LColor::color color)
130 background_color_ = color;
134 LColor::color Inset::backgroundColor() const
136 if (background_color_ == LColor::inherit) {
138 return owner()->backgroundColor();
140 return LColor::background;
142 return background_color_;
146 int Inset::id() const
151 void Inset::id(int id_arg)
156 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
160 bool Inset::forceDefaultParagraphs(Inset const * in) const
163 return owner()->forceDefaultParagraphs(in);
167 int Inset::latexTextWidth(BufferView * bv) const
170 return (owner()->latexTextWidth(bv));
171 return bv->workWidth();
174 // some stuff for inset locking
176 UpdatableInset::UpdatableInset()
177 : Inset(), cursor_visible_(false), block_drawing_(false)
181 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
182 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
186 void UpdatableInset::insetUnlock(BufferView *)
188 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
192 // An updatable inset is highly editable by definition
193 Inset::EDITABLE UpdatableInset::editable() const
195 return HIGHLY_EDITABLE;
199 void UpdatableInset::toggleInsetCursor(BufferView *)
203 void UpdatableInset::showInsetCursor(BufferView *, bool)
207 void UpdatableInset::hideInsetCursor(BufferView *)
211 void UpdatableInset::fitInsetCursor(BufferView *) const
215 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
219 void UpdatableInset::edit(BufferView *, bool)
223 void UpdatableInset::draw(BufferView *, LyXFont const &,
224 int /* baseline */, float & x,
225 bool/*cleared*/) const
228 // ATTENTION: don't do the following here!!!
230 // top_baseline = baseline;
234 void UpdatableInset::scroll(BufferView * bv, float s) const
243 int const workW = bv->workWidth();
244 int const tmp_top_x = top_x - scx;
247 (tmp_top_x + width(bv, font)) < workW)
249 if (s > 0 && top_x > 0)
252 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
253 //int const save_scx = scx;
255 scx = int(s * workW / 2);
259 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
260 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
263 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
266 void UpdatableInset::scroll(BufferView * bv, int offset) const
269 if (!scx && top_x >= 20)
271 if ((top_x + offset) > 20)
273 // scx += offset - (top_x - scx + offset - 20);
278 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
280 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
281 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
286 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
290 /// An updatable inset could handle lyx editing commands
291 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
293 if (ev.action == LFUN_MOUSE_RELEASE)
294 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
296 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
297 if (ev.argument.find('.') != ev.argument.npos) {
298 float const xx = static_cast<float>(strToDbl(ev.argument));
299 scroll(ev.view(), xx);
301 int const xx = strToInt(ev.argument);
302 scroll(ev.view(), xx);
304 ev.view()->updateInset(this, false);
312 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
316 w = static_cast<UpdatableInset*>
317 (owner())->getMaxWidth(bv, this);
319 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
324 // check for margins left/right and extra right margin "const 5"
325 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
326 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
327 if (topx_set && owner()) {
330 } else if (owner()) {
331 // this is needed as otherwise we might have a too large inset if
332 // its top_x afterwards changes to LeftMargin so we try to put at
333 // least the default margin as top_x
343 LyXCursor const & Inset::cursor(BufferView * bv) const
346 return owner()->getLyXText(bv, false)->cursor;
347 return bv->text->cursor;
352 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
354 // we have to unlock ourself in this function by default!
355 bv->unlockInset(const_cast<UpdatableInset *>(this));
357 return WordLangTuple();
361 bool UpdatableInset::searchForward(BufferView * bv, string const &,
364 // we have to unlock ourself in this function by default!
365 bv->unlockInset(const_cast<UpdatableInset *>(this));
370 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
373 // we have to unlock ourself in this function by default!
374 bv->unlockInset(const_cast<UpdatableInset *>(this));