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 "funcrequest.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, FuncRequest const & ev)
306 if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
307 if (ev.argument.find('.') != ev.argument.npos) {
308 float const xx = static_cast<float>(strToDbl(ev.argument));
311 int const xx = strToInt(ev.argument);
314 bv->updateInset(this, false);
322 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
326 w = static_cast<UpdatableInset*>
327 (owner())->getMaxWidth(bv, this);
329 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
334 // check for margins left/right and extra right margin "const 5"
335 if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
336 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
337 if (topx_set && owner()) {
340 } else if (owner()) {
341 // this is needed as otherwise we might have a too large inset if
342 // its top_x afterwards changes to LeftMargin so we try to put at
343 // least the default margin as top_x
353 LyXCursor const & Inset::cursor(BufferView * bv) const
356 return owner()->getLyXText(bv, false)->cursor;
357 return bv->text->cursor;
361 WordLangTuple UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
364 // we have to unlock ourself in this function by default!
365 bv->unlockInset(const_cast<UpdatableInset *>(this));
367 return WordLangTuple();
371 bool UpdatableInset::searchForward(BufferView * bv, string const &,
374 // we have to unlock ourself in this function by default!
375 bv->unlockInset(const_cast<UpdatableInset *>(this));
380 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
383 // we have to unlock ourself in this function by default!
384 bv->unlockInset(const_cast<UpdatableInset *>(this));