]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[lyx.git] / src / BufferView.cpp
index 438de83b42cfb85db03c8f0619444566a96e6d06..d050da8228110f8174528a6737e2c839fc84bd17 100644 (file)
@@ -59,7 +59,6 @@
 
 #include "frontends/alert.h"
 #include "frontends/Delegates.h"
-#include "frontends/FileDialog.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 #include "frontends/Selection.h"
@@ -75,8 +74,6 @@
 #include "support/Package.h"
 #include "support/types.h"
 
-#include <boost/current_function.hpp>
-
 #include <cerrno>
 #include <fstream>
 #include <functional>
@@ -449,8 +446,8 @@ void BufferView::updateScrollbar()
                d->offset_ref_ = 0;
        }
 
-       LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION
-               << " Updating scrollbar: height: " << t.paragraphs().size()
+       LYXERR(Debug::GUI, " Updating scrollbar: height: "
+               << t.paragraphs().size()
                << " curr par: " << d->cursor_.bottom().pit()
                << " default height " << defaultRowHeight());
 
@@ -501,7 +498,7 @@ ScrollbarParameters const & BufferView::scrollbarParameters() const
 
 void BufferView::scrollDocView(int value)
 {
-       LYXERR(Debug::GUI, BOOST_CURRENT_FUNCTION << "[ value = " << value << "]");
+       LYXERR(Debug::GUI, "[ value = " << value << "]");
 
        Text & t = buffer_.text();
        TextMetrics & tm = d->text_metrics_[&t];
@@ -863,12 +860,10 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd)
 
 Update::flags BufferView::dispatch(FuncRequest const & cmd)
 {
-       //lyxerr << BOOST_CURRENT_FUNCTION
-       //       << [ cmd = " << cmd << "]" << endl;
+       //lyxerr << [ cmd = " << cmd << "]" << endl;
 
        // Make sure that the cached BufferView is correct.
-       LYXERR(Debug::ACTION, BOOST_CURRENT_FUNCTION
-               << " action[" << cmd.action << ']'
+       LYXERR(Debug::ACTION, " action[" << cmd.action << ']'
                << " arg[" << to_utf8(cmd.argument()) << ']'
                << " x[" << cmd.x << ']'
                << " y[" << cmd.y << ']'
@@ -898,21 +893,6 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                }
                break;
 
-       case LFUN_FILE_INSERT:
-               // FIXME UNICODE
-               menuInsertLyXFile(to_utf8(cmd.argument()));
-               break;
-
-       case LFUN_FILE_INSERT_PLAINTEXT_PARA:
-               // FIXME UNICODE
-               insertPlaintextFile(to_utf8(cmd.argument()), true);
-               break;
-
-       case LFUN_FILE_INSERT_PLAINTEXT:
-               // FIXME UNICODE
-               insertPlaintextFile(to_utf8(cmd.argument()), false);
-               break;
-
        case LFUN_FONT_STATE:
                cur.message(cur.currentState());
                break;
@@ -1308,7 +1288,7 @@ Inset const * BufferView::getCoveringInset(Text const & text, int x, int y)
 
 void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
 {
-       //lyxerr << BOOST_CURRENT_FUNCTION << "[ cmd0 " << cmd0 << "]" << endl;
+       //lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
 
        // This is only called for mouse related events including
        // LFUN_FILE_OPEN generated by drag-and-drop.
@@ -1648,8 +1628,7 @@ bool BufferView::singleParUpdate()
 
        d->update_strategy_ = SingleParUpdate;
 
-       LYXERR(Debug::PAINTING, BOOST_CURRENT_FUNCTION
-               << "\ny1: " << pm.position() - pm.ascent()
+       LYXERR(Debug::PAINTING, "\ny1: " << pm.position() - pm.ascent()
                << " y2: " << pm.position() + pm.descent()
                << " pit: " << bottom_pit
                << " singlepar: 1");
@@ -1724,8 +1703,7 @@ void BufferView::updateMetrics()
        // Take care of descent of last line
        y2 += tm.parMetrics(pit2).descent();
 
-       LYXERR(Debug::PAINTING, BOOST_CURRENT_FUNCTION
-               << "\n y1: " << y1
+       LYXERR(Debug::PAINTING, "\n y1: " << y1
                << " y2: " << y2
                << " pit1: " << pit1
                << " pit2: " << pit2
@@ -1741,57 +1719,21 @@ void BufferView::updateMetrics()
 }
 
 
-void BufferView::menuInsertLyXFile(string const & filenm)
+void BufferView::insertLyXFile(FileName const & fname)
 {
        BOOST_ASSERT(d->cursor_.inTexted());
-       string filename = filenm;
-
-       if (filename.empty()) {
-               // Launch a file browser
-               // FIXME UNICODE
-               string initpath = lyxrc.document_path;
-               string const trypath = buffer_.filePath();
-               // If directory is writeable, use this as default.
-               if (FileName(trypath).isDirWritable())
-                       initpath = trypath;
-
-               // FIXME UNICODE
-               FileDialog dlg(_("Select LyX document to insert"), LFUN_FILE_INSERT);
-               dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
-               dlg.setButton2(_("Examples|#E#e"),
-                       from_utf8(addPath(package().system_support().absFilename(),
-                       "examples")));
-
-               FileDialog::Result result =
-                       dlg.open(from_utf8(initpath),
-                                    FileFilterList(_("LyX Documents (*.lyx)")),
-                                    docstring());
-
-               if (result.first == FileDialog::Later)
-                       return;
-
-               // FIXME UNICODE
-               filename = to_utf8(result.second);
-
-               // check selected filename
-               if (filename.empty()) {
-                       // emit message signal.
-                       message(_("Canceled."));
-                       return;
-               }
-       }
 
        // Get absolute path of file and add ".lyx"
        // to the filename if necessary
-       filename = fileSearch(string(), filename, "lyx").absFilename();
+       FileName filename = fileSearch(string(), fname.absFilename(), "lyx");
 
-       docstring const disp_fn = makeDisplayPath(filename);
+       docstring const disp_fn = makeDisplayPath(filename.absFilename());
        // emit message signal.
        message(bformat(_("Inserting document %1$s..."), disp_fn));
 
        docstring res;
        Buffer buf("", false);
-       if (buf.loadLyXFile(FileName(filename))) {
+       if (buf.loadLyXFile(filename)) {
                ErrorList & el = buffer_.errorList("Parse");
                // Copy the inserted document error list into the current buffer one.
                el = buf.errorList("Parse");
@@ -1803,10 +1745,11 @@ void BufferView::menuInsertLyXFile(string const & filenm)
                res = _("Could not insert document %1$s");
        }
 
+       updateMetrics();
+       buffer_.changed();
        // emit message signal.
        message(bformat(res, disp_fn));
        buffer_.errors("Parse");
-       updateMetrics();
 }
 
 
@@ -2001,30 +1944,8 @@ void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
 
 
 // FIXME: Move this out of BufferView again
-docstring BufferView::contentsOfPlaintextFile(string const & f,
-       bool asParagraph)
+docstring BufferView::contentsOfPlaintextFile(FileName const & fname)
 {
-       FileName fname(f);
-
-       if (fname.empty()) {
-               FileDialog dlg(_("Select file to insert"),
-                                  ( asParagraph
-                                    ? LFUN_FILE_INSERT_PLAINTEXT_PARA 
-                                    : LFUN_FILE_INSERT_PLAINTEXT) );
-
-               FileDialog::Result result =
-                       dlg.open(from_utf8(buffer().filePath()),
-                                    FileFilterList(), docstring());
-
-               if (result.first == FileDialog::Later)
-                       return docstring();
-
-               if (result.second.empty())
-                       return docstring();
-
-               fname = makeAbsPath(to_utf8(result.second));
-       }
-
        if (!fname.isReadableFile()) {
                docstring const error = from_ascii(strerror(errno));
                docstring const file = makeDisplayPath(fname.absFilename(), 50);
@@ -2035,36 +1956,16 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
                return docstring();
        }
 
-       ifstream ifs(fname.toFilesystemEncoding().c_str());
-       if (!ifs) {
-               docstring const error = from_ascii(strerror(errno));
+       if (!fname.isReadableFile()) {
                docstring const file = makeDisplayPath(fname.absFilename(), 50);
                docstring const text =
-                 bformat(_("Could not open the specified document\n"
-                           "%1$s\ndue to the error: %2$s"), file, error);
+                 bformat(_("%1$s\n is not readable."), file);
                Alert::error(_("Could not open file"), text);
                return docstring();
        }
 
-       ifs.unsetf(std::ios::skipws);
-       istream_iterator<char> ii(ifs);
-       istream_iterator<char> end;
-#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
-       // We use this until the compilers get better...
-       std::vector<char> tmp;
-       copy(ii, end, back_inserter(tmp));
-       string const tmpstr(tmp.begin(), tmp.end());
-#else
-       // This is what we want to use and what we will use once the
-       // compilers get good enough.
-       //string tmpstr(ii, end); // yet a reason for using std::string
-       // alternate approach to get the file into a string:
-       string tmpstr;
-       copy(ii, end, back_inserter(tmpstr));
-#endif
-
        // FIXME UNICODE: We don't know the encoding of the file
-       docstring file_content = from_utf8(tmpstr);
+       docstring file_content = fname.fileContents("UTF-8");
        if (file_content.empty()) {
                Alert::error(_("Reading not UTF-8 encoded file"),
                             _("The file is not UTF-8 encoded.\n"
@@ -2072,16 +1973,16 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
                               "If this does not give the correct result\n"
                               "then please change the encoding of the file\n"
                               "to UTF-8 with a program other than LyX.\n"));
-               file_content = from_local8bit(tmpstr);
+               file_content = fname.fileContents("local8bit");
        }
 
        return normalize_c(file_content);
 }
 
 
-void BufferView::insertPlaintextFile(string const & f, bool asParagraph)
+void BufferView::insertPlaintextFile(FileName const & f, bool asParagraph)
 {
-       docstring const tmpstr = contentsOfPlaintextFile(f, asParagraph);
+       docstring const tmpstr = contentsOfPlaintextFile(f);
 
        if (tmpstr.empty())
                return;
@@ -2093,6 +1994,9 @@ void BufferView::insertPlaintextFile(string const & f, bool asParagraph)
                cur.innerText()->insertStringAsParagraphs(cur, tmpstr);
        else
                cur.innerText()->insertStringAsLines(cur, tmpstr);
+
+       updateMetrics();
+       buffer_.changed();
 }
 
 } // namespace lyx