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"
22 #include "commandtags.h"
23 #include "support/lstrings.h"
26 #include "lyxcursor.h"
31 // Insets default methods
33 // Initialization of the counter for the inset id's,
34 unsigned int Inset::inset_id = 0;
37 : top_x(0), topx_set(false), top_baseline(0), scx(0),
38 id_(inset_id++), owner_(0), par_owner_(0),
39 background_color_(LColor::inherit)
43 Inset::Inset(Inset const & in, bool same_id)
44 : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
45 name_(in.name_), background_color_(in.background_color_)
54 bool Inset::deletable() const
60 bool Inset::directWrite() const
66 Inset::EDITABLE Inset::editable() const
72 void Inset::validate(LaTeXFeatures &) const
76 bool Inset::autoDelete() const
82 void Inset::edit(BufferView *, int, int, unsigned int)
86 void Inset::edit(BufferView *, bool)
91 LyXFont const Inset::convertFont(LyXFont const & font) const
102 string const Inset::editMessage() const
104 return _("Opened inset");
108 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
111 return owner()->getLyXText(bv, false);
117 void Inset::setBackgroundColor(LColor::color color)
119 background_color_ = color;
123 LColor::color Inset::backgroundColor() const
125 if (background_color_ == LColor::inherit) {
127 return owner()->backgroundColor();
129 return LColor::background;
131 return background_color_;
135 int Inset::id() const
140 void Inset::id(int id_arg)
145 void Inset::setFont(BufferView *, LyXFont const &, bool, bool )
149 bool Inset::forceDefaultParagraphs(Inset const * in) const
152 return owner()->forceDefaultParagraphs(in);
156 int Inset::latexTextWidth(BufferView * bv) const
159 return (owner()->latexTextWidth(bv));
160 return bv->workWidth();
163 // some stuff for inset locking
165 UpdatableInset::UpdatableInset()
166 : Inset(), cursor_visible_(false), block_drawing_(false)
170 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
171 : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
175 void UpdatableInset::insetButtonPress(BufferView *, int x, int y, int button)
177 lyxerr[Debug::INFO] << "Inset Button Press x=" << x
178 << ", y=" << y << ", button=" << button << endl;
182 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, int button)
184 lyxerr[Debug::INFO] << "Inset Button Release x=" << x
185 << ", y=" << y << ", button=" << button << endl;
190 void UpdatableInset::insetKeyPress(XKeyEvent *)
192 lyxerr[Debug::INFO] << "Inset Keypress" << endl;
196 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, int state)
198 lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
199 << ", y=" << y << ", state=" << state << endl;
203 void UpdatableInset::insetUnlock(BufferView *)
205 lyxerr[Debug::INFO] << "Inset Unlock" << endl;
209 // An updatable inset is highly editable by definition
210 Inset::EDITABLE UpdatableInset::editable() const
212 return HIGHLY_EDITABLE;
216 void UpdatableInset::toggleInsetCursor(BufferView *)
220 void UpdatableInset::showInsetCursor(BufferView *, bool)
224 void UpdatableInset::hideInsetCursor(BufferView *)
228 void UpdatableInset::fitInsetCursor(BufferView *) const
232 void UpdatableInset::edit(BufferView *, int, int, unsigned int)
236 void UpdatableInset::edit(BufferView *, bool)
240 void UpdatableInset::draw(BufferView *, LyXFont const &,
241 int /* baseline */, float & x,
242 bool/*cleared*/) const
245 // ATTENTION: don't do the following here!!!
247 // top_baseline = baseline;
251 void UpdatableInset::scroll(BufferView * bv, float s) const
260 int const workW = bv->workWidth();
261 int const tmp_top_x = top_x - scx;
264 (tmp_top_x + width(bv, font)) < workW)
266 if (s > 0 && top_x > 0)
269 // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
270 //int const save_scx = scx;
272 scx = int(s * workW / 2);
276 if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
277 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
280 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
283 void UpdatableInset::scroll(BufferView * bv, int offset) const
286 if (!scx && top_x >= 20)
288 if ((top_x + offset) > 20)
290 // scx += offset - (top_x - scx + offset - 20);
295 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
297 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
298 scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
303 // bv->updateInset(const_cast<UpdatableInset *>(this), false);
307 /// An updatable inset could handle lyx editing commands
308 UpdatableInset::RESULT
309 UpdatableInset::localDispatch(BufferView * bv,
310 kb_action action, string const & arg)
312 if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
313 if (arg.find('.') != arg.npos) {
314 float const xx = static_cast<float>(strToDbl(arg));
317 int const xx = strToInt(arg);
320 bv->updateInset(this, false);
328 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
332 w = static_cast<UpdatableInset*>
333 (owner())->getMaxWidth(bv, this);
335 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
340 // check for margins left/right and extra right margin "const 5"
341 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
342 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
343 if (topx_set && owner()) {
346 } else if (owner()) {
347 // this is needed as otherwise we might have a too large inset if
348 // its top_x afterwards changes to LeftMargin so we try to put at
349 // least the default margin as top_x
359 LyXCursor const & Inset::cursor(BufferView * bv) const
362 return owner()->getLyXText(bv, false)->cursor;
363 return bv->text->cursor;
367 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
370 // we have to unlock ourself in this function by default!
371 bv->unlockInset(const_cast<UpdatableInset *>(this));
377 bool UpdatableInset::searchForward(BufferView * bv, string const &,
380 // we have to unlock ourself in this function by default!
381 bv->unlockInset(const_cast<UpdatableInset *>(this));
386 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
389 // we have to unlock ourself in this function by default!
390 bv->unlockInset(const_cast<UpdatableInset *>(this));