]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / BranchList.cpp
1 /**
2  * \file BranchList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BranchList.h"
15 #include "Color.h"
16 #include "ColorSet.h"
17
18 #include "frontends/Application.h"
19
20 #include "support/convert.h"
21 #include "support/lstrings.h"
22
23 #include <algorithm>
24
25 using namespace std;
26
27
28 namespace lyx {
29
30 docstring const & Branch::branch() const
31 {
32         return branch_;
33 }
34
35
36 void Branch::setBranch(docstring const & s)
37 {
38         branch_ = s;
39 }
40
41
42 bool Branch::isSelected() const
43 {
44         return selected_;
45 }
46
47
48 bool Branch::setSelected(bool b)
49 {
50         if (b == selected_)
51                 return false;
52         selected_ = b;
53         return true;
54 }
55
56
57 bool Branch::hasFileNameSuffix() const
58 {
59         return filenameSuffix_;
60 }
61
62
63 void Branch::setFileNameSuffix(bool b)
64 {
65         filenameSuffix_ = b;
66 }
67
68
69 string const & Branch::color() const
70 {
71         return (theApp() && theApp()->isInDarkMode())
72                         ? dmcolor_ : lmcolor_;
73 }
74
75
76 string const & Branch::lightModeColor() const
77 {
78         return lmcolor_;
79 }
80
81
82 string const & Branch::darkModeColor() const
83 {
84         return dmcolor_;
85 }
86
87
88 void Branch::setColor(string const & col)
89 {
90         if (theApp() && theApp()->isInDarkMode())
91                 setColors(string(), col);
92         else
93                 setColors(col);
94 }
95
96
97 void Branch::setColors(string const & lmcol, string const & dmcol)
98 {
99         if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#"))
100                 lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol)));
101         else if (!lmcol.empty())
102                 lmcolor_ = lmcol;
103         if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#"))
104                 dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol)));
105         else if (!dmcol.empty())
106                 dmcolor_ = dmcol;
107
108         // Update the Color table
109         string lmcolor = lmcolor_;
110         string dmcolor = dmcolor_;
111         if (lmcolor == "none")
112                 lmcolor = lcolor.getX11HexName(Color_background);
113         else if (lmcolor.size() != 7 || lmcolor[0] != '#')
114                 lmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(lmcolor));
115         if (dmcolor == "none")
116                 dmcolor = lcolor.getX11HexName(Color_background, true);
117         else if (dmcolor.size() != 7 || dmcolor[0] != '#')
118                 dmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(dmcolor), true);
119
120         // FIXME UNICODE
121         lcolor.setColor("branch" + convert<string>(branch_list_id_)
122                         + to_utf8(branch_), lmcolor, dmcolor);
123 }
124
125
126 namespace {
127
128 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
129 {
130         return [d](Branch const & b){ return b.branch() == d; };
131 }
132
133 } // namespace
134
135
136 Branch * BranchList::find(docstring const & name)
137 {
138         List::iterator it =
139                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
140         return it == list_.end() ? nullptr : &*it;
141 }
142
143
144 Branch const * BranchList::find(docstring const & name) const
145 {
146         List::const_iterator it =
147                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
148         return it == list_.end() ? nullptr : &*it;
149 }
150
151
152 bool BranchList::add(docstring const & s)
153 {
154         bool added = false;
155         size_t i = 0;
156         while (true) {
157                 size_t const j = s.find_first_of(separator_, i);
158                 docstring name;
159                 if (j == docstring::npos)
160                         name = s.substr(i);
161                 else
162                         name = s.substr(i, j - i);
163                 // Is this name already in the list?
164                 bool const already = find(name);
165                 if (!already) {
166                         added = true;
167                         Branch br;
168                         br.setBranch(name);
169                         br.setSelected(false);
170                         br.setFileNameSuffix(false);
171                         br.setListID(id_);
172                         list_.push_back(br);
173                 }
174                 if (j == docstring::npos)
175                         break;
176                 i = j + 1;
177         }
178         return added;
179 }
180
181
182 bool BranchList::remove(docstring const & s)
183 {
184         size_t const size = list_.size();
185         list_.remove_if(BranchNameIs(s));
186         return size != list_.size();
187 }
188
189
190 bool BranchList::rename(docstring const & oldname,
191         docstring const & newname, bool const merge)
192 {
193         if (newname.empty())
194                 return false;
195         if (find(newname)) {
196                 // new name already taken
197                 if (merge)
198                         return remove(oldname);
199                 return false;
200         }
201
202         Branch * branch = find(oldname);
203         if (!branch)
204                 return false;
205         branch->setBranch(newname);
206         return true;
207 }
208
209
210 docstring BranchList::getFileNameSuffix() const
211 {
212         docstring result;
213         for (auto const & br : list_) {
214                 if (br.isSelected() && br.hasFileNameSuffix())
215                         result += "-" + br.branch();
216         }
217         return support::subst(result, from_ascii("/"), from_ascii("_"));
218 }
219
220 } // namespace lyx