]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
4666658635f27badbefa1db93c811d665b944d94
[lyx.git] / src / insets / updatableinset.C
1 /**
2  * \file updatableinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Matthias Ettrich
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "updatableinset.h"
17
18 #include "BufferView.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "dispatchresult.h"
22 #include "funcrequest.h"
23 #include "lyxtext.h"
24
25 #include "support/lstrings.h"
26
27 #include <boost/assert.hpp>
28
29 using lyx::support::strToDbl;
30 using lyx::support::strToInt;
31
32
33
34 // An updatable inset is highly editable by definition
35 InsetOld::EDITABLE UpdatableInset::editable() const
36 {
37         return HIGHLY_EDITABLE;
38 }
39
40
41 void UpdatableInset::scroll(BufferView & bv, float s) const
42 {
43         if (!s) {
44                 scx = 0;
45                 return;
46         }
47
48         int const workW = bv.workWidth();
49         int const tmp_xo_ = xo_ - scx;
50
51         if (tmp_xo_ > 0 && tmp_xo_ + width() < workW)
52                 return;
53         if (s > 0 && xo_ > 0)
54                 return;
55
56         scx = int(s * workW / 2);
57
58 #warning metrics?
59         if (tmp_xo_ + scx + width() < workW / 2)
60                 scx = workW / 2 - tmp_xo_ - width();
61 }
62
63
64 void UpdatableInset::scroll(BufferView & bv, int offset) const
65 {
66         if (offset > 0) {
67                 if (!scx && xo_ >= 20)
68                         return;
69                 if (xo_ + offset > 20)
70                         scx = 0;
71                 // scx = - xo_;
72                 else
73                         scx += offset;
74         } else {
75 #warning metrics?
76                 if (!scx && xo_ + width() < bv.workWidth() - 20)
77                         return;
78                 if (xo_ - scx + offset + width() < bv.workWidth() - 20) {
79                         scx += bv.workWidth() - width() - xo_ - 20;
80                 } else {
81                         scx += offset;
82                 }
83         }
84 }
85
86
87 ///  An updatable inset could handle lyx editing commands
88 DispatchResult
89 UpdatableInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
90 {
91         switch (cmd.action) {
92         case LFUN_MOUSE_RELEASE:
93                 return DispatchResult(editable() == IS_EDITABLE);
94
95         case LFUN_SCROLL_INSET:
96                 if (!cmd.argument.empty()) {
97                         if (cmd.argument.find('.') != cmd.argument.npos)
98                                 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
99                         else
100                                 scroll(cur.bv(), strToInt(cmd.argument));
101                         cur.bv().update();
102                         return DispatchResult(true, true);
103                 }
104
105         default:
106                 return DispatchResult(false);
107         }
108 }
109
110
111 void UpdatableInset::getCursorDim(int &, int &) const
112 {
113         BOOST_ASSERT(false);
114 }