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::validate(LaTeXFeatures &) const
81 bool Inset::autoDelete() const
87 void Inset::edit(BufferView *, int, int, mouse_button::state)
91 void Inset::edit(BufferView *, bool)
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::insetButtonPress(BufferView *, int x, int y, mouse_button::state button)
182 lyxerr[Debug::INFO] << "Inset Button Press x=" << x
183 << ", y=" << y << ", button=" << button << endl;
187 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, mouse_button::state button)
189 lyxerr[Debug::INFO] << "Inset Button Release x=" << x
190 << ", y=" << y << ", button=" << button << endl;
195 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, mouse_button::state state)
197 lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
198 << ", y=" << y << ", state=" << state << endl;
202 void UpdatableInset::insetUnlock(BufferView *)
204 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
208 // An updatable inset is highly editable by definition
209 Inset::EDITABLE UpdatableInset::editable() const
211 return HIGHLY_EDITABLE;
215 void UpdatableInset::toggleInsetCursor(BufferView *)
219 void UpdatableInset::showInsetCursor(BufferView *, bool)
223 void UpdatableInset::hideInsetCursor(BufferView *)
227 void UpdatableInset::fitInsetCursor(BufferView *) const
231 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
235 void UpdatableInset::edit(BufferView *, bool)
239 void UpdatableInset::draw(BufferView *, LyXFont const &,
240 int /* baseline */, float & x,
241 bool/*cleared*/) const
244 // ATTENTION: don't do the following here!!!
246 // top_baseline = baseline;
250 void UpdatableInset::scroll(BufferView * bv, float s) const
259 int const workW = bv->workWidth();
260 int const tmp_top_x = top_x - scx;
263 (tmp_top_x + width(bv, font)) < workW)
265 if (s > 0 && top_x > 0)
268 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
269 //int const save_scx = scx;
271 scx = int(s * workW / 2);
275 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
276 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
279 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
282 void UpdatableInset::scroll(BufferView * bv, int offset) const
285 if (!scx && top_x >= 20)
287 if ((top_x + offset) > 20)
289 // scx += offset - (top_x - scx + offset - 20);
294 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
296 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
297 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
302 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
306 /// An updatable inset could handle lyx editing commands
307 UpdatableInset::RESULT
308 UpdatableInset::localDispatch(FuncRequest const & ev)
310 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
311 if (ev.argument.find('.') != ev.argument.npos) {
312 float const xx = static_cast<float>(strToDbl(ev.argument));
313 scroll(ev.view(), xx);
315 int const xx = strToInt(ev.argument);
316 scroll(ev.view(), xx);
318 ev.view()->updateInset(this, false);
326 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
330 w = static_cast<UpdatableInset*>
331 (owner())->getMaxWidth(bv, this);
333 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
338 // check for margins left/right and extra right margin "const 5"
339 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
340 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
341 if (topx_set && owner()) {
344 } else if (owner()) {
345 // this is needed as otherwise we might have a too large inset if
346 // its top_x afterwards changes to LeftMargin so we try to put at
347 // least the default margin as top_x
357 LyXCursor const & Inset::cursor(BufferView * bv) const
360 return owner()->getLyXText(bv, false)->cursor;
361 return bv->text->cursor;
366 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
368 // we have to unlock ourself in this function by default!
369 bv->unlockInset(const_cast<UpdatableInset *>(this));
371 return WordLangTuple();
375 bool UpdatableInset::searchForward(BufferView * bv, string const &,
378 // we have to unlock ourself in this function by default!
379 bv->unlockInset(const_cast<UpdatableInset *>(this));
384 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
387 // we have to unlock ourself in this function by default!
388 bv->unlockInset(const_cast<UpdatableInset *>(this));