]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
conmsolidate scrolling code
[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 "coordcache.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "lyxtext.h"
25
26 #include "support/convert.h"
27
28 #include <boost/assert.hpp>
29
30
31 // An updatable inset is highly editable by definition
32 InsetBase::EDITABLE UpdatableInset::editable() const
33 {
34         return HIGHLY_EDITABLE;
35 }
36
37
38 int UpdatableInset::scroll(bool) const
39 {
40         return scx;
41 }
42
43
44 void UpdatableInset::setScroll(int maxwidth, double s) const
45 {
46         lyxerr << "UpdatableInset::setScroll: (int) " << maxwidth << std::endl;
47         if (!s) {
48                 scx = 0;
49                 return;
50         }
51
52         int xo_ = theCoords.getInsets().x(this);
53         int const tmp_xo_ = xo_ - scx;
54
55         if (tmp_xo_ > 0 && tmp_xo_ + width() < maxwidth)
56                 return;
57         if (s > 0.0 && xo_ > 0)
58                 return;
59
60         scx = int(s * maxwidth / 2);
61
62 #ifdef WITH_WARNINGS
63 #warning metrics?
64 #endif
65         if (tmp_xo_ + scx + width() < maxwidth / 2)
66                 scx = maxwidth / 2 - tmp_xo_ - width();
67 }
68
69
70 void UpdatableInset::setScroll(int maxwidth, int offset) const
71 {
72         lyxerr << "UpdatableInset::setScroll: (double) " << maxwidth << std::endl;
73         int const xo_ = theCoords.getInsets().x(this);
74         if (offset > 0) {
75                 if (!scx && xo_ >= 20)
76                         return;
77                 if (xo_ + offset > 20)
78                         scx = 0;
79                 // scx = - xo_;
80                 else
81                         scx += offset;
82         } else {
83 #ifdef WITH_WARNINGS
84 #warning metrics?
85 #endif
86                 if (!scx && xo_ + width() < maxwidth - 20)
87                         return;
88
89                 if (xo_ - scx + offset + width() < maxwidth - 20)
90                         scx += maxwidth - width() - xo_ - 20;
91                 else
92                         scx += offset;
93         }
94 }
95
96
97 void UpdatableInset::doDispatch(LCursor & cur, FuncRequest & cmd)
98 {
99         switch (cmd.action) {
100         case LFUN_SCROLL_INSET:
101                 if (cmd.argument.empty()) {
102                         const int maxwidth = cur.bv().workWidth();
103                         if (cmd.argument.find('.') != cmd.argument.npos)
104                                 setScroll(maxwidth, static_cast<float>(convert<double>(cmd.argument)));
105                         else
106                                 setScroll(maxwidth, convert<int>(cmd.argument));
107                 } else
108                         cur.noUpdate();
109                 break;
110
111         default:
112                 InsetBase::dispatch(cur, cmd);
113         }
114 }
115
116
117 void UpdatableInset::getCursorDim(int &, int &) const
118 {
119         BOOST_ASSERT(false);
120 }
121