]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
Update the GuiLabel view when the document is read-only. Also disable the text field.
[lyx.git] / src / buffer_funcs.cpp
1 /**
2  * \file buffer_funcs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "DocIterator.h"
20 #include "Counters.h"
21 #include "ErrorList.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
25 #include "Language.h"
26 #include "LaTeX.h"
27 #include "Layout.h"
28 #include "LyX.h"
29 #include "TextClass.h"
30 #include "Paragraph.h"
31 #include "ParagraphList.h"
32 #include "ParagraphParameters.h"
33 #include "ParIterator.h"
34 #include "TexRow.h"
35 #include "Text.h"
36 #include "TocBackend.h"
37
38 #include "frontends/alert.h"
39
40 #include "insets/InsetBibitem.h"
41 #include "insets/InsetInclude.h"
42
43 #include "support/lassert.h"
44 #include "support/convert.h"
45 #include "support/debug.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/lstrings.h"
49 #include "support/textutils.h"
50
51 using namespace std;
52 using namespace lyx::support;
53
54 namespace lyx {
55
56 namespace Alert = frontend::Alert;
57
58
59 Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
60 {
61         // File already open?
62         Buffer * checkBuffer = theBufferList().getBuffer(filename);
63         if (checkBuffer) {
64                 // sometimes (when setting the master buffer from a child)
65                 // we accept a dirty buffer right away (otherwise we'd get
66                 // an infinite loop (bug 5514)
67                 if (checkBuffer->isClean() || acceptDirty)
68                         return checkBuffer;
69                 docstring const file = makeDisplayPath(filename.absFilename(), 20);
70                 docstring text = bformat(_(
71                                 "The document %1$s is already loaded and has unsaved changes.\n"
72                                 "Do you want to abandon your changes and reload the version on disk?"), file);
73                 if (Alert::prompt(_("Reload saved document?"),
74                                 text, 0, 1,  _("&Reload"), _("&Keep Changes")))
75                         return checkBuffer;
76
77                 // FIXME: should be LFUN_REVERT
78                 checkBuffer->markClean();
79                 theBufferList().release(checkBuffer);
80                 // Load it again.
81                 return checkAndLoadLyXFile(filename);
82         }
83
84         if (filename.exists()) {
85                 if (!filename.isReadableFile()) {
86                         docstring text = bformat(_("The file %1$s exists but is not "
87                                 "readable by the current user."),
88                                 from_utf8(filename.absFilename()));
89                         Alert::error(_("File not readable!"), text);
90                         return 0;
91                 }
92                 Buffer * b = theBufferList().newBuffer(filename.absFilename());
93                 if (!b) {
94                         // Buffer creation is not possible.
95                         return 0;
96                 }
97                 if (!b->loadLyXFile(filename)) {
98                         theBufferList().release(b);
99                         return 0;
100                 }
101                 return b;
102         }
103
104         docstring text = bformat(_("The document %1$s does not yet "
105                 "exist.\n\nDo you want to create a new document?"),
106                 from_utf8(filename.absFilename()));
107         if (!Alert::prompt(_("Create new document?"),
108                         text, 0, 1, _("&Create"), _("Cancel")))
109                 return newFile(filename.absFilename(), string(), true);
110
111         return 0;
112 }
113
114
115 // FIXME newFile() should probably be a member method of Application...
116 Buffer * newFile(string const & filename, string const & templatename,
117                  bool const isNamed)
118 {
119         // get a free buffer
120         Buffer * b = theBufferList().newBuffer(filename);
121         if (!b)
122                 // Buffer creation is not possible.
123                 return 0;
124
125         FileName tname;
126         // use defaults.lyx as a default template if it exists.
127         if (templatename.empty())
128                 tname = libFileSearch("templates", "defaults.lyx");
129         else
130                 tname = makeAbsPath(templatename);
131
132         if (!tname.empty()) {
133                 if (!b->readFile(tname)) {
134                         docstring const file = makeDisplayPath(tname.absFilename(), 50);
135                         docstring const text  = bformat(
136                                 _("The specified document template\n%1$s\ncould not be read."),
137                                 file);
138                         Alert::error(_("Could not read template"), text);
139                         theBufferList().release(b);
140                         return 0;
141                 }
142         }
143
144         if (!isNamed) {
145                 b->setUnnamed();
146                 b->setFileName(filename);
147         }
148
149         b->setReadonly(false);
150         b->setFullyLoaded(true);
151
152         return b;
153 }
154
155
156 Buffer * newUnnamedFile(FileName const & path, string const & prefix,
157                                                 string const & templatename)
158 {
159         static map<string, int> file_number;
160
161         FileName filename;
162
163         do {
164                 filename.set(path, 
165                         prefix + convert<string>(++file_number[prefix]) + ".lyx");
166         }
167         while (theBufferList().exists(filename) || filename.isReadableFile());
168                 
169         return newFile(filename.absFilename(), templatename, false);
170 }
171
172
173 /* 
174  * FIXME : merge with countChars. The structures of the two functions
175  * are similar but, unfortunately, they seem to have a different
176  * notion of what to count. Since nobody ever complained about that,
177  * this proves (again) that any number beats no number ! (JMarc)
178  */
179 int countWords(DocIterator const & from, DocIterator const & to)
180 {
181         int count = 0;
182         bool inword = false;
183         for (DocIterator dit = from ; dit != to ; ) {
184                 if (!dit.inTexted()) {
185                         dit.forwardPos();
186                         continue;
187                 }
188                 
189                 Paragraph const & par = dit.paragraph();
190                 pos_type const pos = dit.pos();
191                 
192                 // Copied and adapted from isWordSeparator() in Paragraph
193                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
194                         Inset const * ins = par.getInset(pos);
195                         if (ins && !ins->producesOutput()) {
196                                 //skip this inset
197                                 ++dit.top().pos();
198                                 continue;
199                         }
200                         if (par.isWordSeparator(pos)) 
201                                 inword = false;
202                         else if (!inword) {
203                                 ++count;
204                                 inword = true;
205                         }
206                 }
207                 dit.forwardPos();
208         }
209
210         return count;
211 }
212
213
214 int countChars(DocIterator const & from, DocIterator const & to, 
215                bool with_blanks)
216 {
217         int chars = 0;
218         int blanks = 0;
219         for (DocIterator dit = from ; dit != to ; ) {
220                 if (!dit.inTexted()) {
221                         dit.forwardPos();
222                         continue;
223                 }
224                 
225                 Paragraph const & par = dit.paragraph();
226                 pos_type const pos = dit.pos();
227                 
228                 if (pos != dit.lastpos() && !par.isDeleted(pos)) {
229                         if (Inset const * ins = par.getInset(pos)) {
230                                 if (!ins->producesOutput()) {
231                                         //skip this inset
232                                         ++dit.top().pos();
233                                         continue;
234                                 }
235                                 if (ins->isLetter())
236                                         ++chars;
237                                 else if (with_blanks && ins->isSpace())
238                                         ++blanks;
239                         } else {
240                                 char_type const c = par.getChar(pos);
241                                 if (isPrintableNonspace(c))
242                                         ++chars;
243                                 else if (isSpace(c) && with_blanks)
244                                         ++blanks;
245                         }
246                 }
247                 dit.forwardPos();
248         }
249
250         return chars + blanks;
251 }
252
253
254 Buffer * loadIfNeeded(FileName const & fname)
255 {
256         Buffer * buffer = theBufferList().getBuffer(fname);
257         if (!buffer) {
258                 if (!fname.exists())
259                         return 0;
260
261                 buffer = theBufferList().newBuffer(fname.absFilename());
262                 if (!buffer)
263                         // Buffer creation is not possible.
264                         return 0;
265
266                 if (!buffer->loadLyXFile(fname)) {
267                         //close the buffer we just opened
268                         theBufferList().release(buffer);
269                         return 0;
270                 }
271         }
272         return buffer;
273 }
274
275
276 } // namespace lyx