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"
20 #include "support/lstrings.h"
21 #include "frontends/Painter.h"
22 #include "frontends/mouse_state.h"
23 #include "commandtags.h"
24 #include "support/lstrings.h"
27 #include "lyxcursor.h"
32 // Insets default methods
34 // Initialization of the counter for the inset id's,
35 unsigned int Inset::inset_id = 0;
38 : top_x(0), topx_set(false), top_baseline(0), scx(0),
39 id_(inset_id++), owner_(0), par_owner_(0),
40 background_color_(LColor::inherit)
44 Inset::Inset(Inset const & in, bool same_id)
45 : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
46 name_(in.name_), background_color_(in.background_color_)
55 bool Inset::deletable() const
61 bool Inset::directWrite() const
67 Inset::EDITABLE Inset::editable() const
73 void Inset::validate(LaTeXFeatures &) const
77 bool Inset::autoDelete() const
83 void Inset::edit(BufferView *, int, int, mouse_button::state)
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::insetButtonPress(BufferView *, int x, int y, mouse_button::state button)
178 lyxerr[Debug::INFO] << "Inset Button Press x=" << x
179 << ", y=" << y << ", button=" << button << endl;
183 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, mouse_button::state button)
185 lyxerr[Debug::INFO] << "Inset Button Release x=" << x
186 << ", y=" << y << ", button=" << button << endl;
191 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, mouse_button::state state)
193 lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
194 << ", y=" << y << ", state=" << state << endl;
198 void UpdatableInset::insetUnlock(BufferView *)
200 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
204 // An updatable inset is highly editable by definition
205 Inset::EDITABLE UpdatableInset::editable() const
207 return HIGHLY_EDITABLE;
211 void UpdatableInset::toggleInsetCursor(BufferView *)
215 void UpdatableInset::showInsetCursor(BufferView *, bool)
219 void UpdatableInset::hideInsetCursor(BufferView *)
223 void UpdatableInset::fitInsetCursor(BufferView *) const
227 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
231 void UpdatableInset::edit(BufferView *, bool)
235 void UpdatableInset::draw(BufferView *, LyXFont const &,
236 int /* baseline */, float & x,
237 bool/*cleared*/) const
240 // ATTENTION: don't do the following here!!!
242 // top_baseline = baseline;
246 void UpdatableInset::scroll(BufferView * bv, float s) const
255 int const workW = bv->workWidth();
256 int const tmp_top_x = top_x - scx;
259 (tmp_top_x + width(bv, font)) < workW)
261 if (s > 0 && top_x > 0)
264 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
265 //int const save_scx = scx;
267 scx = int(s * workW / 2);
271 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
272 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
275 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
278 void UpdatableInset::scroll(BufferView * bv, int offset) const
281 if (!scx && top_x >= 20)
283 if ((top_x + offset) > 20)
285 // scx += offset - (top_x - scx + offset - 20);
290 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
292 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
293 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
298 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
302 /// An updatable inset could handle lyx editing commands
303 UpdatableInset::RESULT
304 UpdatableInset::localDispatch(BufferView * bv,
305 kb_action action, string const & arg)
307 if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
308 if (arg.find('.') != arg.npos) {
309 float const xx = static_cast<float>(strToDbl(arg));
312 int const xx = strToInt(arg);
315 bv->updateInset(this, false);
323 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
327 w = static_cast<UpdatableInset*>
328 (owner())->getMaxWidth(bv, this);
330 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
335 // check for margins left/right and extra right margin "const 5"
336 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
337 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
338 if (topx_set && owner()) {
341 } else if (owner()) {
342 // this is needed as otherwise we might have a too large inset if
343 // its top_x afterwards changes to LeftMargin so we try to put at
344 // least the default margin as top_x
354 LyXCursor const & Inset::cursor(BufferView * bv) const
357 return owner()->getLyXText(bv, false)->cursor;
358 return bv->text->cursor;
362 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
365 // we have to unlock ourself in this function by default!
366 bv->unlockInset(const_cast<UpdatableInset *>(this));
372 bool UpdatableInset::searchForward(BufferView * bv, string const &,
375 // we have to unlock ourself in this function by default!
376 bv->unlockInset(const_cast<UpdatableInset *>(this));
381 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
384 // we have to unlock ourself in this function by default!
385 bv->unlockInset(const_cast<UpdatableInset *>(this));