]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
setFont rework + some code simplification
[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 "debug.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "lyxtext.h"
23
24 #include "support/lstrings.h"
25
26 #include <boost/assert.hpp>
27
28
29 using lyx::support::strToDbl;
30 using lyx::support::strToInt;
31
32
33 // An updatable inset is highly editable by definition
34 InsetOld::EDITABLE UpdatableInset::editable() const
35 {
36         return HIGHLY_EDITABLE;
37 }
38
39
40 void UpdatableInset::scroll(BufferView * bv, float s) const
41 {
42         if (!s) {
43                 scx = 0;
44                 return;
45         }
46
47         int const workW = bv->workWidth();
48         int const tmp_xo_ = xo_ - scx;
49
50         if (tmp_xo_ > 0 && tmp_xo_ + width() < workW)
51                 return;
52         if (s > 0 && xo_ > 0)
53                 return;
54
55         scx = int(s * workW / 2);
56
57 #warning metrics?
58         if (tmp_xo_ + scx + width() < workW / 2)
59                 scx = workW / 2 - tmp_xo_ - width();
60 }
61
62
63 void UpdatableInset::scroll(BufferView * bv, int offset) const
64 {
65         if (offset > 0) {
66                 if (!scx && xo_ >= 20)
67                         return;
68                 if (xo_ + offset > 20)
69                         scx = 0;
70                 // scx = - xo_;
71                 else
72                         scx += offset;
73         } else {
74 #warning metrics?
75                 if (!scx && xo_ + width() < bv->workWidth() - 20)
76                         return;
77                 if (xo_ - scx + offset + width() < bv->workWidth() - 20) {
78                         scx += bv->workWidth() - width() - xo_ - 20;
79                 } else {
80                         scx += offset;
81                 }
82         }
83 }
84
85
86 ///  An updatable inset could handle lyx editing commands
87 DispatchResult
88 UpdatableInset::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
89 {
90         switch (cmd.action) {
91         case LFUN_MOUSE_RELEASE:
92                 return DispatchResult(editable() == IS_EDITABLE);
93
94         case LFUN_SCROLL_INSET:
95                 if (!cmd.argument.empty()) {
96                         if (cmd.argument.find('.') != cmd.argument.npos)
97                                 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
98                         else
99                                 scroll(cmd.view(), strToInt(cmd.argument));
100                         cmd.view()->update();
101                         return DispatchResult(true, true);
102                 }
103
104         default:
105                 return DispatchResult(false);
106         }
107 }
108
109
110 void UpdatableInset::getCursorDim(int &, int &) const
111 {
112         BOOST_ASSERT(false);
113 }